From ec1a6b0bc6e9616c02acbf9e63a00746019bc83e Mon Sep 17 00:00:00 2001 From: Bharath Rupireddy Date: Tue, 14 Dec 2021 13:31:26 +0000 Subject: [PATCH v1] Add LOG messages when replication slots become active and inactive These logs will be extremely useful on production servers to debug and analyze inactive replication slot issues. --- src/backend/replication/slot.c | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index 90ba9b417d..63bf1140ec 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -318,6 +318,11 @@ ReplicationSlotCreate(const char *name, bool db_specific, SpinLockRelease(&slot->mutex); MyReplicationSlot = slot; + ereport(LOG, + (errmsg("replication slot \"%s\" becomes active", + NameStr(slot->data.name)), + errdetail("The process with PID %d acquired it.", slot->active_pid))); + LWLockRelease(ReplicationSlotControlLock); /* @@ -457,6 +462,11 @@ retry: /* We made this slot active, so it's ours now. */ MyReplicationSlot = s; + + ereport(LOG, + (errmsg("replication slot \"%s\" becomes active", + NameStr(s->data.name)), + errdetail("The process with PID %d acquired it.", s->active_pid))); } /* @@ -499,6 +509,10 @@ ReplicationSlotRelease(void) if (slot->data.persistency == RS_PERSISTENT) { + int pid; + + pid = slot->active_pid; + /* * Mark persistent slot inactive. We're not freeing it, just * disconnecting, but wake up others that may be waiting for it. @@ -506,6 +520,12 @@ ReplicationSlotRelease(void) SpinLockAcquire(&slot->mutex); slot->active_pid = 0; SpinLockRelease(&slot->mutex); + + ereport(LOG, + (errmsg("replication slot \"%s\" becomes inactive", + NameStr(slot->data.name)), + errdetail("The process with PID %d released it.", pid))); + ConditionVariableBroadcast(&slot->active_cv); } @@ -594,6 +614,7 @@ ReplicationSlotDropPtr(ReplicationSlot *slot) { char path[MAXPGPATH]; char tmppath[MAXPGPATH]; + int pid; /* * If some other backend ran this code concurrently with us, we might try @@ -606,6 +627,8 @@ ReplicationSlotDropPtr(ReplicationSlot *slot) sprintf(path, "pg_replslot/%s", NameStr(slot->data.name)); sprintf(tmppath, "pg_replslot/%s.tmp", NameStr(slot->data.name)); + pid = slot->active_pid; + /* * Rename the slot directory on disk, so that we'll no longer recognize * this as a valid slot. Note that if this fails, we've got to mark the @@ -636,6 +659,14 @@ ReplicationSlotDropPtr(ReplicationSlot *slot) slot->active_pid = 0; SpinLockRelease(&slot->mutex); + ereport(LOG, + (errmsg("replication slot \"%s\" becomes inactive", + NameStr(slot->data.name)), + errdetail("The process with PID %d released it.", pid))); + + /* let's not emit the above message twice */ + pid = -1; + /* wake up anyone waiting on this slot */ ConditionVariableBroadcast(&slot->active_cv); @@ -645,6 +676,7 @@ ReplicationSlotDropPtr(ReplicationSlot *slot) path, tmppath))); } + /* * The slot is definitely gone. Lock out concurrent scans of the array * long enough to kill it. It's OK to clear the active PID here without @@ -658,6 +690,13 @@ ReplicationSlotDropPtr(ReplicationSlot *slot) slot->active_pid = 0; slot->in_use = false; LWLockRelease(ReplicationSlotControlLock); + + if (pid > 0) + ereport(LOG, + (errmsg("replication slot \"%s\" becomes inactive", + NameStr(slot->data.name)), + errdetail("The process with PID %d released it.", pid))); + ConditionVariableBroadcast(&slot->active_cv); /* -- 2.25.1