diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index 505445f2dc..72aa5de60c 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -99,6 +99,8 @@ ReplicationSlot *MyReplicationSlot = NULL; int max_replication_slots = 0; /* the maximum number of replication * slots */ +static int ReplicationSlotAcquireInternal(const char *name, + SlotAcquireBehavior behavior, int index); static void ReplicationSlotDropAcquired(void); static void ReplicationSlotDropPtr(ReplicationSlot *slot); @@ -332,25 +334,45 @@ ReplicationSlotCreate(const char *name, bool db_specific, */ int ReplicationSlotAcquire(const char *name, SlotAcquireBehavior behavior) +{ + int i; + + /* + * Search for the named slot and mark it active if we find it. + */ + LWLockAcquire(ReplicationSlotControlLock, LW_SHARED); + for (i = 0; i < max_replication_slots; i++) + { + ReplicationSlot *s = &ReplicationSlotCtl->replication_slots[i]; + + if (s->in_use && strcmp(name, NameStr(s->data.name)) == 0) + break; + } + LWLockRelease(ReplicationSlotControlLock); + + return ReplicationSlotAcquireInternal(name, behavior, i); +} + +static int +ReplicationSlotAcquireInternal(const char *name, + SlotAcquireBehavior behavior, int index) { ReplicationSlot *slot; int active_pid; - int i; retry: Assert(MyReplicationSlot == NULL); /* - * Search for the named slot and mark it active if we find it. If the - * slot is already active, we exit the loop with active_pid set to the PID + * If the slot is already active, we set active_pid to the PID * of the backend that owns it. */ active_pid = 0; slot = NULL; LWLockAcquire(ReplicationSlotControlLock, LW_SHARED); - for (i = 0; i < max_replication_slots; i++) + if (index >= 0 && index < max_replication_slots) { - ReplicationSlot *s = &ReplicationSlotCtl->replication_slots[i]; + ReplicationSlot *s = &ReplicationSlotCtl->replication_slots[index]; if (s->in_use && strcmp(name, NameStr(s->data.name)) == 0) { @@ -378,8 +400,6 @@ retry: else active_pid = MyProcPid; slot = s; - - break; } } LWLockRelease(ReplicationSlotControlLock); @@ -1120,8 +1140,9 @@ restart: for (;;) { - int wspid = ReplicationSlotAcquire(NameStr(slotname), - SAB_Inquire); + int wspid = + ReplicationSlotAcquireInternal(NameStr(slotname), + SAB_Inquire, i); /* no walsender? success! */ if (wspid == 0)