Skip to content

Commit 33c5099

Browse files
committed
Fix logic bug in 1632ea4
I overlooked that one condition was logically inverted. The fix is a little bit more involved than simply negating the condition, to make the code easier to read. Fix some outdated comments left by the same commit, while at it. Author: Masahiko Sawada <sawada.mshk@gmail.com> Author: Álvaro Herrera <alvherre@alvh.no-ip.org> Reviewed-by: Amit Kapila <amit.kapila16@gmail.com> Discussion: https://postgr.es/m/YMRlmB3/lZw8YBH+@paquier.xyz
1 parent 86b222b commit 33c5099

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/backend/replication/slot.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,9 @@ ReplicationSlotAcquire(const char *name, bool nowait)
410410
if (IsUnderPostmaster)
411411
{
412412
/*
413-
* Get ready to sleep on the slot in case it is active if SAB_Block.
414-
* (We may end up not sleeping, but we don't want to do this while
415-
* holding the spinlock.)
413+
* Get ready to sleep on the slot in case it is active. (We may end
414+
* up not sleeping, but we don't want to do this while holding the
415+
* spinlock.)
416416
*/
417417
if (!nowait)
418418
ConditionVariablePrepareToSleep(&s->active_cv);
@@ -429,22 +429,24 @@ ReplicationSlotAcquire(const char *name, bool nowait)
429429

430430
/*
431431
* If we found the slot but it's already active in another process, we
432-
* either error out, return the PID of the owning process, or retry after
433-
* a short wait, as caller specified.
432+
* wait until the owning process signals us that it's been released, or
433+
* error out.
434434
*/
435435
if (active_pid != MyProcPid)
436436
{
437437
if (!nowait)
438-
ereport(ERROR,
439-
(errcode(ERRCODE_OBJECT_IN_USE),
440-
errmsg("replication slot \"%s\" is active for PID %d",
441-
NameStr(s->data.name), active_pid)));
438+
{
439+
/* Wait here until we get signaled, and then restart */
440+
ConditionVariableSleep(&s->active_cv,
441+
WAIT_EVENT_REPLICATION_SLOT_DROP);
442+
ConditionVariableCancelSleep();
443+
goto retry;
444+
}
442445

443-
/* Wait here until we get signaled, and then restart */
444-
ConditionVariableSleep(&s->active_cv,
445-
WAIT_EVENT_REPLICATION_SLOT_DROP);
446-
ConditionVariableCancelSleep();
447-
goto retry;
446+
ereport(ERROR,
447+
(errcode(ERRCODE_OBJECT_IN_USE),
448+
errmsg("replication slot \"%s\" is active for PID %d",
449+
NameStr(s->data.name), active_pid)));
448450
}
449451
else if (!nowait)
450452
ConditionVariableCancelSleep(); /* no sleep needed after all */

0 commit comments

Comments
 (0)