public inbox for [email protected]
help / color / mirror / Atom feedFrom: shveta malik <[email protected]>
To: Amit Kapila <[email protected]>
Cc: pgsql-hackers <[email protected]>
Cc: Bharath Rupireddy <[email protected]>
Cc: Zhijie Hou (Fujitsu) <[email protected]>
Cc: shveta malik <[email protected]>
Subject: Re: promotion related handling in pg_sync_replication_slots()
Date: Fri, 5 Apr 2024 10:31:15 +0530
Message-ID: <CAJpy0uBGz_aZM99D+djJbJvhddoyVRqdzoPwTsT10J5Tt62faw@mail.gmail.com> (raw)
In-Reply-To: <CAA4eK1+MVF7xfeB+oprEHfdCL4tdiZv1kQ8=2FhdTq=XL18u2w@mail.gmail.com>
References: <CAJpy0uBefXUS_TSz=oxmYKHdg-fhxUT0qfjASW3nmqnzVC3p6A@mail.gmail.com>
<CAA4eK1+MVF7xfeB+oprEHfdCL4tdiZv1kQ8=2FhdTq=XL18u2w@mail.gmail.com>
On Thu, Apr 4, 2024 at 5:17 PM Amit Kapila <[email protected]> wrote:
>
> On Thu, Apr 4, 2024 at 5:05 PM shveta malik <[email protected]> wrote:
> >
> > Hello hackers,
> >
> > Currently, promotion related handling is missing in the slot sync SQL
> > function pg_sync_replication_slots(). Here is the background on how
> > it is done in slot sync worker:
> > During promotion, the startup process in order to shut down the
> > slot-sync worker, sets the 'stopSignaled' flag, sends the shut-down
> > signal, and waits for slot sync worker to exit. Meanwhile if the
> > postmaster has not noticed the promotion yet, it may end up restarting
> > slot sync worker. In such a case, the worker exits if 'stopSignaled'
> > is set.
> >
> > Since there is a chance that the user (or any of his scripts/tools)
> > may execute SQL function pg_sync_replication_slots() in parallel to
> > promotion, such handling is needed in this SQL function as well, The
> > attached patch attempts to implement the same.
> >
>
> Thanks for the report and patch. I'll look into it.
>
Please find v2. Changes are:
1) Rebased the patch as there was conflict due to recent commit 6f132ed.
2) Added an Assert in update_synced_slots_inactive_since() to ensure
that the slot does not have active_pid.
3) Improved commit msg and comments.
thanks
Shveta
Attachments:
[application/octet-stream] v2-0001-Handle-stopSignaled-during-sync-function-call.patch (4.0K, ../CAJpy0uBGz_aZM99D+djJbJvhddoyVRqdzoPwTsT10J5Tt62faw@mail.gmail.com/2-v2-0001-Handle-stopSignaled-during-sync-function-call.patch)
download | inline diff:
From 29b4a4f2301184665710fbd09fcdb077fd419125 Mon Sep 17 00:00:00 2001
From: Shveta Malik <[email protected]>
Date: Fri, 5 Apr 2024 10:16:03 +0530
Subject: [PATCH v2] Handle stopSignaled during sync function call.
Currently, promotion related handling is missing in the slot sync SQL
function pg_sync_replication_slots(). Here is the background on how
it is done in slot sync worker:
During promotion, the startup process in order to shut down the
slot-sync worker, sets the 'stopSignaled' flag, sends the shut-down
signal, and waits for slot sync worker to exit. Meanwhile if the
postmaster has not noticed the promotion yet, it may end up restarting
slot sync worker. In such a case, the worker exits if 'stopSignaled'
is set.
Since there is a chance that the user (or any of his scripts/tools)
may execute SQL function pg_sync_replication_slots() in parallel to
promotion, such handling is needed in this SQL function as well, The
attached patch attempts to implement the same. Changes are:
1) If pg_sync_replication_slots() is already running when the
promotion is triggered, ShutDownSlotSync() checks the
'SlotSyncCtx->syncing' flag as well and waits for it to become false
i.e. waits till parallel running SQL function is finished.
2) If pg_sync_replication_slots() is invoked when promotion is
already in progress, pg_sync_replication_slots() respects the
'stopSignaled' flag set by the startup process and becomes a no-op.
---
src/backend/replication/logical/slotsync.c | 38 +++++++++++++++++++---
1 file changed, 33 insertions(+), 5 deletions(-)
diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c
index d18e2c7342..0064578e9c 100644
--- a/src/backend/replication/logical/slotsync.c
+++ b/src/backend/replication/logical/slotsync.c
@@ -1395,6 +1395,7 @@ update_synced_slots_inactive_since(void)
if (s->in_use && s->data.synced)
{
Assert(SlotIsLogical(s));
+ Assert(s->active_pid == 0);
/* Use the same inactive_since time for all the slots. */
if (now == 0)
@@ -1411,6 +1412,10 @@ update_synced_slots_inactive_since(void)
/*
* Shut down the slot sync worker.
+ *
+ * It sends signal to shutdown slot sync worker. It also waits till
+ * the slot sync worker has exited and pg_sync_replication_slots()
+ * has finished.
*/
void
ShutDownSlotSync(void)
@@ -1419,7 +1424,11 @@ ShutDownSlotSync(void)
SlotSyncCtx->stopSignaled = true;
- if (SlotSyncCtx->pid == InvalidPid)
+ /*
+ * Return if neither the slot sync worker is running nor the function
+ * pg_sync_replication_slots() is executing.
+ */
+ if ((SlotSyncCtx->pid == InvalidPid) && !SlotSyncCtx->syncing)
{
SpinLockRelease(&SlotSyncCtx->mutex);
update_synced_slots_inactive_since();
@@ -1427,9 +1436,10 @@ ShutDownSlotSync(void)
}
SpinLockRelease(&SlotSyncCtx->mutex);
- kill(SlotSyncCtx->pid, SIGINT);
+ if (SlotSyncCtx->pid != InvalidPid)
+ kill(SlotSyncCtx->pid, SIGINT);
- /* Wait for it to die */
+ /* Wait for worker to exit and SQL function to finish */
for (;;)
{
int rc;
@@ -1447,8 +1457,11 @@ ShutDownSlotSync(void)
SpinLockAcquire(&SlotSyncCtx->mutex);
- /* Is it gone? */
- if (SlotSyncCtx->pid == InvalidPid)
+ /*
+ * Confirm that both the worker and the function
+ * pg_sync_replication_slots() are done.
+ */
+ if ((SlotSyncCtx->pid == InvalidPid) && !SlotSyncCtx->syncing)
break;
SpinLockRelease(&SlotSyncCtx->mutex);
@@ -1557,6 +1570,21 @@ slotsync_failure_callback(int code, Datum arg)
void
SyncReplicationSlots(WalReceiverConn *wrconn)
{
+ /*
+ * Startup process signaled the slot sync to stop, so if meanwhile user
+ * has invoked slot sync SQL function, simply return.
+ */
+ SpinLockAcquire(&SlotSyncCtx->mutex);
+ if (SlotSyncCtx->stopSignaled)
+ {
+ ereport(LOG,
+ errmsg("skipping slot synchronization as slot sync shutdown is signaled during promotion"));
+
+ SpinLockRelease(&SlotSyncCtx->mutex);
+ return;
+ }
+ SpinLockRelease(&SlotSyncCtx->mutex);
+
PG_ENSURE_ERROR_CLEANUP(slotsync_failure_callback, PointerGetDatum(wrconn));
{
validate_remote_info(wrconn);
--
2.34.1
view thread (8+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected], [email protected], [email protected]
Subject: Re: promotion related handling in pg_sync_replication_slots()
In-Reply-To: <CAJpy0uBGz_aZM99D+djJbJvhddoyVRqdzoPwTsT10J5Tt62faw@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox