Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1632ea4

Browse files
committed
Return ReplicationSlotAcquire API to its original form
Per 96540f8; the awkward API introduced by c655077 is no longer needed. Author: Andres Freund <andres@anarazel.de> Reviewed-by: Álvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://postgr.es/m/20210408020913.zzprrlvqyvlt5cyy@alap3.anarazel.de
1 parent b676ac4 commit 1632ea4

File tree

5 files changed

+18
-53
lines changed

5 files changed

+18
-53
lines changed

src/backend/replication/logical/logicalfuncs.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ pg_logical_slot_get_changes_guts(FunctionCallInfo fcinfo, bool confirm, bool bin
225225
else
226226
end_of_wal = GetXLogReplayRecPtr(&ThisTimeLineID);
227227

228-
(void) ReplicationSlotAcquire(NameStr(*name), SAB_Error);
228+
ReplicationSlotAcquire(NameStr(*name), true);
229229

230230
PG_TRY();
231231
{

src/backend/replication/slot.c

+13-40
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ ReplicationSlot *MyReplicationSlot = NULL;
9999
int max_replication_slots = 0; /* the maximum number of replication
100100
* slots */
101101

102-
static int ReplicationSlotAcquireInternal(ReplicationSlot *slot,
103-
const char *name, SlotAcquireBehavior behavior);
104102
static void ReplicationSlotDropAcquired(void);
105103
static void ReplicationSlotDropPtr(ReplicationSlot *slot);
106104

@@ -374,34 +372,16 @@ SearchNamedReplicationSlot(const char *name, bool need_lock)
374372
/*
375373
* Find a previously created slot and mark it as used by this process.
376374
*
377-
* The return value is only useful if behavior is SAB_Inquire, in which
378-
* it's zero if we successfully acquired the slot, -1 if the slot no longer
379-
* exists, or the PID of the owning process otherwise. If behavior is
380-
* SAB_Error, then trying to acquire an owned slot is an error.
381-
* If SAB_Block, we sleep until the slot is released by the owning process.
375+
* An error is raised if nowait is true and the slot is currently in use. If
376+
* nowait is false, we sleep until the slot is released by the owning process.
382377
*/
383-
int
384-
ReplicationSlotAcquire(const char *name, SlotAcquireBehavior behavior)
385-
{
386-
return ReplicationSlotAcquireInternal(NULL, name, behavior);
387-
}
388-
389-
/*
390-
* Mark the specified slot as used by this process.
391-
*
392-
* Only one of slot and name can be specified.
393-
* If slot == NULL, search for the slot with the given name.
394-
*
395-
* See comments about the return value in ReplicationSlotAcquire().
396-
*/
397-
static int
398-
ReplicationSlotAcquireInternal(ReplicationSlot *slot, const char *name,
399-
SlotAcquireBehavior behavior)
378+
void
379+
ReplicationSlotAcquire(const char *name, bool nowait)
400380
{
401381
ReplicationSlot *s;
402382
int active_pid;
403383

404-
AssertArg((slot == NULL) ^ (name == NULL));
384+
AssertArg(name != NULL);
405385

406386
retry:
407387
Assert(MyReplicationSlot == NULL);
@@ -412,17 +392,15 @@ ReplicationSlotAcquireInternal(ReplicationSlot *slot, const char *name,
412392
* Search for the slot with the specified name if the slot to acquire is
413393
* not given. If the slot is not found, we either return -1 or error out.
414394
*/
415-
s = slot ? slot : SearchNamedReplicationSlot(name, false);
395+
s = SearchNamedReplicationSlot(name, false);
416396
if (s == NULL || !s->in_use)
417397
{
418398
LWLockRelease(ReplicationSlotControlLock);
419399

420-
if (behavior == SAB_Inquire)
421-
return -1;
422400
ereport(ERROR,
423401
(errcode(ERRCODE_UNDEFINED_OBJECT),
424402
errmsg("replication slot \"%s\" does not exist",
425-
name ? name : NameStr(slot->data.name))));
403+
name)));
426404
}
427405

428406
/*
@@ -436,7 +414,7 @@ ReplicationSlotAcquireInternal(ReplicationSlot *slot, const char *name,
436414
* (We may end up not sleeping, but we don't want to do this while
437415
* holding the spinlock.)
438416
*/
439-
if (behavior == SAB_Block)
417+
if (!nowait)
440418
ConditionVariablePrepareToSleep(&s->active_cv);
441419

442420
SpinLockAcquire(&s->mutex);
@@ -456,31 +434,26 @@ ReplicationSlotAcquireInternal(ReplicationSlot *slot, const char *name,
456434
*/
457435
if (active_pid != MyProcPid)
458436
{
459-
if (behavior == SAB_Error)
437+
if (!nowait)
460438
ereport(ERROR,
461439
(errcode(ERRCODE_OBJECT_IN_USE),
462440
errmsg("replication slot \"%s\" is active for PID %d",
463441
NameStr(s->data.name), active_pid)));
464-
else if (behavior == SAB_Inquire)
465-
return active_pid;
466442

467443
/* Wait here until we get signaled, and then restart */
468444
ConditionVariableSleep(&s->active_cv,
469445
WAIT_EVENT_REPLICATION_SLOT_DROP);
470446
ConditionVariableCancelSleep();
471447
goto retry;
472448
}
473-
else if (behavior == SAB_Block)
449+
else if (!nowait)
474450
ConditionVariableCancelSleep(); /* no sleep needed after all */
475451

476452
/* Let everybody know we've modified this slot */
477453
ConditionVariableBroadcast(&s->active_cv);
478454

479455
/* We made this slot active, so it's ours now. */
480456
MyReplicationSlot = s;
481-
482-
/* success */
483-
return 0;
484457
}
485458

486459
/*
@@ -588,7 +561,7 @@ ReplicationSlotDrop(const char *name, bool nowait)
588561
{
589562
Assert(MyReplicationSlot == NULL);
590563

591-
(void) ReplicationSlotAcquire(name, nowait ? SAB_Error : SAB_Block);
564+
ReplicationSlotAcquire(name, nowait);
592565

593566
ReplicationSlotDropAcquired();
594567
}
@@ -1271,8 +1244,8 @@ InvalidatePossiblyObsoleteSlot(ReplicationSlot *s, XLogRecPtr oldestLSN)
12711244
WAIT_EVENT_REPLICATION_SLOT_DROP);
12721245

12731246
/*
1274-
* Re-acquire lock and start over; we expect to invalidate the slot
1275-
* next time (unless another process acquires the slot in the
1247+
* Re-acquire lock and start over; we expect to invalidate the
1248+
* slot next time (unless another process acquires the slot in the
12761249
* meantime).
12771250
*/
12781251
LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);

src/backend/replication/slotfuncs.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ pg_replication_slot_advance(PG_FUNCTION_ARGS)
639639
moveto = Min(moveto, GetXLogReplayRecPtr(&ThisTimeLineID));
640640

641641
/* Acquire the slot so we "own" it */
642-
(void) ReplicationSlotAcquire(NameStr(*slotname), SAB_Error);
642+
ReplicationSlotAcquire(NameStr(*slotname), true);
643643

644644
/* A slot whose restart_lsn has never been reserved cannot be advanced */
645645
if (XLogRecPtrIsInvalid(MyReplicationSlot->data.restart_lsn))

src/backend/replication/walsender.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ StartReplication(StartReplicationCmd *cmd)
601601

602602
if (cmd->slotname)
603603
{
604-
(void) ReplicationSlotAcquire(cmd->slotname, SAB_Error);
604+
ReplicationSlotAcquire(cmd->slotname, true);
605605
if (SlotIsLogical(MyReplicationSlot))
606606
ereport(ERROR,
607607
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
@@ -1137,7 +1137,7 @@ StartLogicalReplication(StartReplicationCmd *cmd)
11371137

11381138
Assert(!MyReplicationSlot);
11391139

1140-
(void) ReplicationSlotAcquire(cmd->slotname, SAB_Error);
1140+
ReplicationSlotAcquire(cmd->slotname, true);
11411141

11421142
if (XLogRecPtrIsInvalid(MyReplicationSlot->data.restart_lsn))
11431143
ereport(ERROR,

src/include/replication/slot.h

+1-9
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ typedef enum ReplicationSlotPersistency
3737
RS_TEMPORARY
3838
} ReplicationSlotPersistency;
3939

40-
/* For ReplicationSlotAcquire, q.v. */
41-
typedef enum SlotAcquireBehavior
42-
{
43-
SAB_Error,
44-
SAB_Block,
45-
SAB_Inquire
46-
} SlotAcquireBehavior;
47-
4840
/*
4941
* On-Disk data of a replication slot, preserved across restarts.
5042
*/
@@ -208,7 +200,7 @@ extern void ReplicationSlotCreate(const char *name, bool db_specific,
208200
extern void ReplicationSlotPersist(void);
209201
extern void ReplicationSlotDrop(const char *name, bool nowait);
210202

211-
extern int ReplicationSlotAcquire(const char *name, SlotAcquireBehavior behavior);
203+
extern void ReplicationSlotAcquire(const char *name, bool nowait);
212204
extern void ReplicationSlotRelease(void);
213205
extern void ReplicationSlotCleanup(void);
214206
extern void ReplicationSlotSave(void);

0 commit comments

Comments
 (0)