I think passing the slot name when the slot is also passed is useless
and wasteful; it'd be better to pass NULL for the name and ignore the
strcmp() in that case -- in fact I suggest to forbid passing both name
and slot. (Any failure there would risk raising an error during
checkpoint, which is undesirable.)
So I propose the following tweaks to your patch, and otherwise +1.
--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c
index b94e11a8e7..d99d0e9b42 100644
--- a/src/backend/replication/slot.c
+++ b/src/backend/replication/slot.c
@@ -383,24 +383,25 @@ ReplicationSlotAcquireInternal(ReplicationSlot *slot, const char *name,
ReplicationSlot *s;
int active_pid;
+ AssertArg((slot == NULL) ^ (name == NULL));
+
retry:
Assert(MyReplicationSlot == NULL);
LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
/*
- * Search for the slot with the specified name if the slot to
- * acquire is not given. If the slot is not found, we either
- * return -1 or error out.
+ * Search for the slot with the specified name if the slot to acquire is
+ * not given. If the slot is not found, we either return -1 or error out.
*/
- s = (slot == NULL) ? SearchNamedReplicationSlot(name) : slot;
- if (s == NULL || !s->in_use || strcmp(name, NameStr(s->data.name)) != 0)
+ s = slot ? slot : SearchNamedReplicationSlot(name);
+ if (s == NULL || !s->in_use ||
+ (name && strcmp(name, NameStr(s->data.name)) != 0))
{
+ LWLockRelease(ReplicationSlotControlLock);
+
if (behavior == SAB_Inquire)
- {
- LWLockRelease(ReplicationSlotControlLock);
return -1;
- }
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("replication slot \"%s\" does not exist", name)));
@@ -1161,8 +1162,7 @@ restart:
* we have to wait on that CV for the process owning
* the slot to be terminated, later.
*/
- wspid = ReplicationSlotAcquireInternal(s, NameStr(slotname),
- SAB_Inquire);
+ wspid = ReplicationSlotAcquireInternal(s, NULL, SAB_Inquire);
/*
* Exit the loop if we successfully acquired the slot or