Skip to content

Commit 31c4730

Browse files
author
Matthias van de Meent
committed
Fix handling of backgroundworker->WalSender promotion
Additionally, deduplicate some bkendtype-adjusting code in SignalChildren/CountChildren, unifying handling and reducing chances of bugs.
1 parent da5d434 commit 31c4730

File tree

2 files changed

+43
-46
lines changed

2 files changed

+43
-46
lines changed

src/backend/postmaster/pmchild.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,20 @@ ReleasePostmasterChildSlot(PMChild *pmchild)
250250

251251
/* WAL senders start out as regular backends, and share the pool */
252252
if (pmchild->bkend_type == B_WAL_SENDER)
253+
{
253254
pool = &pmchild_pools[B_BACKEND];
255+
256+
/*
257+
* NEON: The released slot may be from a BGWorker upgraded to a
258+
* WalSender, so make sure we release the child back to the right
259+
* pool.
260+
*/
261+
if (pmchild->pid < pool->first_slotno &&
262+
pmchild->pid >= pool->first_slotno + pool->size)
263+
{
264+
pool = &pmchild_pools[B_BG_WORKER];
265+
}
266+
}
254267
else
255268
pool = &pmchild_pools[pmchild->bkend_type];
256269

src/backend/postmaster/postmaster.c

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3475,6 +3475,34 @@ signal_child(PMChild *pmchild, int signal)
34753475
#endif
34763476
}
34773477

3478+
static void
3479+
adjust_bkendtype_if_needed(PMChild *bp, BackendTypeMask targetMask)
3480+
{
3481+
/*
3482+
* If we need to distinguish between B_BACKEND and B_WAL_SENDER, check
3483+
* if any B_BACKEND backends have recently announced that they are
3484+
* actually WAL senders.
3485+
*/
3486+
if (btmask_contains(targetMask, B_WAL_SENDER) != btmask_contains(targetMask, B_BACKEND) &&
3487+
bp->bkend_type == B_BACKEND)
3488+
{
3489+
if (IsPostmasterChildWalSender(bp->child_slot))
3490+
bp->bkend_type = B_WAL_SENDER;
3491+
}
3492+
3493+
/*
3494+
* If we need to distinguish between B_BG_WORKER and B_WAL_SENDER,
3495+
* check if any B_BG_WORKER backends have recently announced that
3496+
* they are actually WAL senders.
3497+
*/
3498+
if (btmask_contains(targetMask, B_WAL_SENDER) != btmask_contains(targetMask, B_BG_WORKER) &&
3499+
bp->bkend_type == B_BG_WORKER)
3500+
{
3501+
if (IsPostmasterChildWalSender(bp->child_slot))
3502+
bp->bkend_type = B_WAL_SENDER;
3503+
}
3504+
}
3505+
34783506
/*
34793507
* Send a signal to the targeted children.
34803508
*/
@@ -3488,29 +3516,7 @@ SignalChildren(int signal, BackendTypeMask targetMask)
34883516
{
34893517
PMChild *bp = dlist_container(PMChild, elem, iter.cur);
34903518

3491-
/*
3492-
* If we need to distinguish between B_BACKEND and B_WAL_SENDER, check
3493-
* if any B_BACKEND backends have recently announced that they are
3494-
* actually WAL senders.
3495-
*/
3496-
if (btmask_contains(targetMask, B_WAL_SENDER) != btmask_contains(targetMask, B_BACKEND) &&
3497-
bp->bkend_type == B_BACKEND)
3498-
{
3499-
if (IsPostmasterChildWalSender(bp->child_slot))
3500-
bp->bkend_type = B_WAL_SENDER;
3501-
}
3502-
3503-
/*
3504-
* If we need to distinguish between B_BG_WORKER and B_WAL_SENDER,
3505-
* check if any B_BG_WORKER backends have recently announced that
3506-
* they are actually WAL senders.
3507-
*/
3508-
if (btmask_contains(targetMask, B_WAL_SENDER) != btmask_contains(targetMask, B_BG_WORKER) &&
3509-
bp->bkend_type == B_BG_WORKER)
3510-
{
3511-
if (IsPostmasterChildWalSender(bp->child_slot))
3512-
bp->bkend_type = B_WAL_SENDER;
3513-
}
3519+
adjust_bkendtype_if_needed(bp, targetMask);
35143520

35153521
if (!btmask_contains(targetMask, bp->bkend_type))
35163522
continue;
@@ -3934,29 +3940,7 @@ CountChildren(BackendTypeMask targetMask)
39343940
{
39353941
PMChild *bp = dlist_container(PMChild, elem, iter.cur);
39363942

3937-
/*
3938-
* If we need to distinguish between B_BACKEND and B_WAL_SENDER, check
3939-
* if any B_BACKEND backends have recently announced that they are
3940-
* actually WAL senders.
3941-
*/
3942-
if (btmask_contains(targetMask, B_WAL_SENDER) != btmask_contains(targetMask, B_BACKEND) &&
3943-
bp->bkend_type == B_BACKEND)
3944-
{
3945-
if (IsPostmasterChildWalSender(bp->child_slot))
3946-
bp->bkend_type = B_WAL_SENDER;
3947-
}
3948-
3949-
/*
3950-
* If we need to distinguish between B_BG_WORKER and B_WAL_SENDER,
3951-
* check if any B_BG_WORKER backends have recently announced that
3952-
* they are actually WAL senders.
3953-
*/
3954-
if (btmask_contains(targetMask, B_WAL_SENDER) != btmask_contains(targetMask, B_BG_WORKER) &&
3955-
bp->bkend_type == B_BG_WORKER)
3956-
{
3957-
if (IsPostmasterChildWalSender(bp->child_slot))
3958-
bp->bkend_type = B_WAL_SENDER;
3959-
}
3943+
adjust_bkendtype_if_needed(bp, targetMask);
39603944

39613945
if (!btmask_contains(targetMask, bp->bkend_type))
39623946
continue;

0 commit comments

Comments
 (0)