From cd784c50ed0fc928241f109e19f0f5c9afaa01ef Mon Sep 17 00:00:00 2001 From: Bharath Rupireddy Date: Mon, 13 Nov 2023 12:04:11 +0000 Subject: [PATCH v12] Emit messages when replication slots become active and inactive This commit adds log messages (at LOG level when log_replication_commands is set, otherwise at DEBUG1 level) when walsenders acquire and release replication slots. These messages help to know the lifetime of a replication slot - one can know how long a streaming standby or logical subscriber or replication slot consumer is down. In other words, one can know how long a replication slot is inactive - the time between released and acquired slot messages is the inactive replication slot duration. These messages will be extremely useful on production servers to debug and analyze inactive replication slot issues. Note that these messages are emitted only for walsenders when replication slot is acquired and released, but not for backends. This is because walsenders are the ones that typically hold replication slots for longer durations unlike backends which hold replication slots for executing some replication related functions. --- doc/src/sgml/config.sgml | 10 +++++----- src/backend/replication/slot.c | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index fc35a46e5e..1dc014ce84 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -7510,11 +7510,11 @@ log_line_prefix = '%m [%p] %q%u@%d/%a ' - Causes each replication command to be logged in the server log. - See for more information about - replication command. The default value is off. - Only superusers and users with the appropriate SET - privilege can change this setting. + Causes each replication command and slot acquisition/release to be + logged in the server log. See + for more information about replication command. The default value is + off. Only superusers and users with the appropriate + SET privilege can change this setting. diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index 781aa43cc4..fd8b8151b6 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -537,6 +537,12 @@ retry: */ if (SlotIsLogical(s)) pgstat_acquire_replslot(s); + + if (am_walsender) + ereport(log_replication_commands ? LOG : DEBUG1, + errmsg_internal("acquired %s replication slot \"%s\"", + SlotIsPhysical(MyReplicationSlot) ? "physical" : "logical", + NameStr(MyReplicationSlot->data.name))); } /* @@ -549,9 +555,17 @@ void ReplicationSlotRelease(void) { ReplicationSlot *slot = MyReplicationSlot; + char *slotname; + bool is_physical; Assert(slot != NULL && slot->active_pid != 0); + if (am_walsender) + { + slotname = pstrdup(NameStr(MyReplicationSlot->data.name)); + is_physical = SlotIsPhysical(MyReplicationSlot); + } + if (slot->data.persistency == RS_EPHEMERAL) { /* @@ -596,6 +610,15 @@ ReplicationSlotRelease(void) MyProc->statusFlags &= ~PROC_IN_LOGICAL_DECODING; ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags; LWLockRelease(ProcArrayLock); + + if (am_walsender) + { + ereport(log_replication_commands ? LOG : DEBUG1, + errmsg_internal("released %s replication slot \"%s\"", + is_physical ? "physical" : "logical", slotname)); + + pfree(slotname); + } } /* -- 2.34.1