PostgreSQL Source Code  git master
slot.h File Reference
Include dependency graph for slot.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ReplicationSlotPersistentData
 
struct  ReplicationSlot
 
struct  ReplicationSlotCtlData
 

Macros

#define SlotIsPhysical(slot)   ((slot)->data.database == InvalidOid)
 
#define SlotIsLogical(slot)   ((slot)->data.database != InvalidOid)
 

Typedefs

typedef enum ReplicationSlotPersistency ReplicationSlotPersistency
 
typedef struct ReplicationSlotPersistentData ReplicationSlotPersistentData
 
typedef struct ReplicationSlot ReplicationSlot
 
typedef struct ReplicationSlotCtlData ReplicationSlotCtlData
 

Enumerations

enum  ReplicationSlotPersistency { RS_PERSISTENT, RS_EPHEMERAL, RS_TEMPORARY }
 

Functions

Size ReplicationSlotsShmemSize (void)
 
void ReplicationSlotsShmemInit (void)
 
void ReplicationSlotCreate (const char *name, bool db_specific, ReplicationSlotPersistency p, bool two_phase)
 
void ReplicationSlotPersist (void)
 
void ReplicationSlotDrop (const char *name, bool nowait)
 
void ReplicationSlotAcquire (const char *name, bool nowait)
 
void ReplicationSlotRelease (void)
 
void ReplicationSlotCleanup (void)
 
void ReplicationSlotSave (void)
 
void ReplicationSlotMarkDirty (void)
 
bool ReplicationSlotValidateName (const char *name, int elevel)
 
void ReplicationSlotReserveWal (void)
 
void ReplicationSlotsComputeRequiredXmin (bool already_locked)
 
void ReplicationSlotsComputeRequiredLSN (void)
 
XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN (void)
 
bool ReplicationSlotsCountDBSlots (Oid dboid, int *nslots, int *nactive)
 
void ReplicationSlotsDropDBSlots (Oid dboid)
 
bool InvalidateObsoleteReplicationSlots (XLogSegNo oldestSegno)
 
ReplicationSlotSearchNamedReplicationSlot (const char *name, bool need_lock)
 
void ReplicationSlotNameForTablesync (Oid suboid, Oid relid, char *syncslotname, int szslot)
 
void ReplicationSlotDropAtPubNode (WalReceiverConn *wrconn, char *slotname, bool missing_ok)
 
void StartupReplicationSlots (void)
 
void CheckPointReplicationSlots (void)
 
void CheckSlotRequirements (void)
 
void CheckSlotPermissions (void)
 

Variables

PGDLLIMPORT ReplicationSlotCtlDataReplicationSlotCtl
 
PGDLLIMPORT ReplicationSlotMyReplicationSlot
 
PGDLLIMPORT int max_replication_slots
 

Macro Definition Documentation

◆ SlotIsLogical

◆ SlotIsPhysical

#define SlotIsPhysical (   slot)    ((slot)->data.database == InvalidOid)

Typedef Documentation

◆ ReplicationSlot

◆ ReplicationSlotCtlData

◆ ReplicationSlotPersistency

◆ ReplicationSlotPersistentData

Enumeration Type Documentation

◆ ReplicationSlotPersistency

Enumerator
RS_PERSISTENT 
RS_EPHEMERAL 
RS_TEMPORARY 

Definition at line 33 of file slot.h.

34 {
ReplicationSlotPersistency
Definition: slot.h:33

Function Documentation

◆ CheckPointReplicationSlots()

void CheckPointReplicationSlots ( void  )

Definition at line 1360 of file slot.c.

References ReplicationSlot::data, DEBUG1, elog, i, ReplicationSlot::in_use, LOG, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, MAXPGPATH, ReplicationSlotPersistentData::name, NameStr, ReplicationSlotCtlData::replication_slots, SaveSlotToPath(), and sprintf.

Referenced by CheckPointGuts().

1361 {
1362  int i;
1363 
1364  elog(DEBUG1, "performing replication slot checkpoint");
1365 
1366  /*
1367  * Prevent any slot from being created/dropped while we're active. As we
1368  * explicitly do *not* want to block iterating over replication_slots or
1369  * acquiring a slot we cannot take the control lock - but that's OK,
1370  * because holding ReplicationSlotAllocationLock is strictly stronger, and
1371  * enough to guarantee that nobody can change the in_use bits on us.
1372  */
1373  LWLockAcquire(ReplicationSlotAllocationLock, LW_SHARED);
1374 
1375  for (i = 0; i < max_replication_slots; i++)
1376  {
1378  char path[MAXPGPATH];
1379 
1380  if (!s->in_use)
1381  continue;
1382 
1383  /* save the slot to disk, locking is handled in SaveSlotToPath() */
1384  sprintf(path, "pg_replslot/%s", NameStr(s->data.name));
1385  SaveSlotToPath(s, path, LOG);
1386  }
1387  LWLockRelease(ReplicationSlotAllocationLock);
1388 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
#define DEBUG1
Definition: elog.h:25
ReplicationSlotPersistentData data
Definition: slot.h:147
#define LOG
Definition: elog.h:26
static void SaveSlotToPath(ReplicationSlot *slot, const char *path, int elevel)
Definition: slot.c:1516
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define sprintf
Definition: port.h:218
#define MAXPGPATH
bool in_use
Definition: slot.h:123
int max_replication_slots
Definition: slot.c:99
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
#define elog(elevel,...)
Definition: elog.h:232
int i
#define NameStr(name)
Definition: c.h:681
ReplicationSlot replication_slots[1]
Definition: slot.h:180

◆ CheckSlotPermissions()

void CheckSlotPermissions ( void  )

Definition at line 1066 of file slot.c.

References ereport, errcode(), errmsg(), ERROR, GetUserId(), has_rolreplication(), and superuser().

Referenced by copy_replication_slot(), pg_create_logical_replication_slot(), pg_create_physical_replication_slot(), pg_drop_replication_slot(), pg_logical_slot_get_changes_guts(), and pg_replication_slot_advance().

1067 {
1068  if (!superuser() && !has_rolreplication(GetUserId()))
1069  ereport(ERROR,
1070  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1071  errmsg("must be superuser or replication role to use replication slots")));
1072 }
Oid GetUserId(void)
Definition: miscinit.c:495
int errcode(int sqlerrcode)
Definition: elog.c:698
bool superuser(void)
Definition: superuser.c:46
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
bool has_rolreplication(Oid roleid)
Definition: miscinit.c:676
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ CheckSlotRequirements()

void CheckSlotRequirements ( void  )

Definition at line 1044 of file slot.c.

References ereport, errcode(), errmsg(), ERROR, max_replication_slots, wal_level, and WAL_LEVEL_REPLICA.

Referenced by CheckLogicalDecodingRequirements(), copy_replication_slot(), pg_create_physical_replication_slot(), and pg_drop_replication_slot().

1045 {
1046  /*
1047  * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
1048  * needs the same check.
1049  */
1050 
1051  if (max_replication_slots == 0)
1052  ereport(ERROR,
1053  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1054  errmsg("replication slots can only be used if max_replication_slots > 0")));
1055 
1057  ereport(ERROR,
1058  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1059  errmsg("replication slots can only be used if wal_level >= replica")));
1060 }
int wal_level
Definition: xlog.c:108
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
int max_replication_slots
Definition: slot.c:99
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ InvalidateObsoleteReplicationSlots()

bool InvalidateObsoleteReplicationSlots ( XLogSegNo  oldestSegno)

Definition at line 1317 of file slot.c.

References i, ReplicationSlot::in_use, InvalidatePossiblyObsoleteSlot(), LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, ReplicationSlotCtlData::replication_slots, ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), wal_segment_size, and XLogSegNoOffsetToRecPtr.

Referenced by CreateCheckPoint(), and CreateRestartPoint().

1318 {
1319  XLogRecPtr oldestLSN;
1320  bool invalidated = false;
1321 
1322  XLogSegNoOffsetToRecPtr(oldestSegno, 0, wal_segment_size, oldestLSN);
1323 
1324 restart:
1325  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1326  for (int i = 0; i < max_replication_slots; i++)
1327  {
1329 
1330  if (!s->in_use)
1331  continue;
1332 
1333  if (InvalidatePossiblyObsoleteSlot(s, oldestLSN, &invalidated))
1334  {
1335  /* if the lock was released, start from scratch */
1336  goto restart;
1337  }
1338  }
1339  LWLockRelease(ReplicationSlotControlLock);
1340 
1341  /*
1342  * If any slots have been invalidated, recalculate the resource limits.
1343  */
1344  if (invalidated)
1345  {
1348  }
1349 
1350  return invalidated;
1351 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
int wal_segment_size
Definition: xlog.c:119
static bool InvalidatePossiblyObsoleteSlot(ReplicationSlot *s, XLogRecPtr oldestLSN, bool *invalidated)
Definition: slot.c:1164
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:817
bool in_use
Definition: slot.h:123
int max_replication_slots
Definition: slot.c:99
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
int i
ReplicationSlot replication_slots[1]
Definition: slot.h:180
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:767

◆ ReplicationSlotAcquire()

void ReplicationSlotAcquire ( const char *  name,
bool  nowait 
)

Definition at line 380 of file slot.c.

References ReplicationSlot::active_cv, ReplicationSlot::active_pid, Assert, AssertArg, ConditionVariableBroadcast(), ConditionVariableCancelSleep(), ConditionVariablePrepareToSleep(), ConditionVariableSleep(), ReplicationSlot::data, ereport, errcode(), errmsg(), ERROR, ReplicationSlot::in_use, IsUnderPostmaster, LW_SHARED, LWLockAcquire(), LWLockRelease(), ReplicationSlot::mutex, MyProcPid, ReplicationSlotPersistentData::name, NameStr, SearchNamedReplicationSlot(), SpinLockAcquire, SpinLockRelease, and WAIT_EVENT_REPLICATION_SLOT_DROP.

Referenced by pg_logical_slot_get_changes_guts(), pg_replication_slot_advance(), ReplicationSlotDrop(), StartLogicalReplication(), and StartReplication().

381 {
382  ReplicationSlot *s;
383  int active_pid;
384 
385  AssertArg(name != NULL);
386 
387 retry:
388  Assert(MyReplicationSlot == NULL);
389 
390  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
391 
392  /*
393  * Search for the slot with the specified name if the slot to acquire is
394  * not given. If the slot is not found, we either return -1 or error out.
395  */
396  s = SearchNamedReplicationSlot(name, false);
397  if (s == NULL || !s->in_use)
398  {
399  LWLockRelease(ReplicationSlotControlLock);
400 
401  ereport(ERROR,
402  (errcode(ERRCODE_UNDEFINED_OBJECT),
403  errmsg("replication slot \"%s\" does not exist",
404  name)));
405  }
406 
407  /*
408  * This is the slot we want; check if it's active under some other
409  * process. In single user mode, we don't need this check.
410  */
411  if (IsUnderPostmaster)
412  {
413  /*
414  * Get ready to sleep on the slot in case it is active. (We may end
415  * up not sleeping, but we don't want to do this while holding the
416  * spinlock.)
417  */
418  if (!nowait)
420 
421  SpinLockAcquire(&s->mutex);
422  if (s->active_pid == 0)
423  s->active_pid = MyProcPid;
424  active_pid = s->active_pid;
425  SpinLockRelease(&s->mutex);
426  }
427  else
428  active_pid = MyProcPid;
429  LWLockRelease(ReplicationSlotControlLock);
430 
431  /*
432  * If we found the slot but it's already active in another process, we
433  * wait until the owning process signals us that it's been released, or
434  * error out.
435  */
436  if (active_pid != MyProcPid)
437  {
438  if (!nowait)
439  {
440  /* Wait here until we get signaled, and then restart */
444  goto retry;
445  }
446 
447  ereport(ERROR,
448  (errcode(ERRCODE_OBJECT_IN_USE),
449  errmsg("replication slot \"%s\" is active for PID %d",
450  NameStr(s->data.name), active_pid)));
451  }
452  else if (!nowait)
453  ConditionVariableCancelSleep(); /* no sleep needed after all */
454 
455  /* Let everybody know we've modified this slot */
457 
458  /* We made this slot active, so it's ours now. */
459  MyReplicationSlot = s;
460 }
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition: slot.c:348
int MyProcPid
Definition: globals.c:43
void ConditionVariableBroadcast(ConditionVariable *cv)
int errcode(int sqlerrcode)
Definition: elog.c:698
ReplicationSlotPersistentData data
Definition: slot.h:147
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ConditionVariableCancelSleep(void)
#define ERROR
Definition: elog.h:46
bool IsUnderPostmaster
Definition: globals.c:112
#define AssertArg(condition)
Definition: c.h:806
bool in_use
Definition: slot.h:123
#define SpinLockRelease(lock)
Definition: spin.h:64
#define ereport(elevel,...)
Definition: elog.h:157
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
#define Assert(condition)
Definition: c.h:804
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
ConditionVariable active_cv
Definition: slot.h:153
const char * name
Definition: encode.c:561
int errmsg(const char *fmt,...)
Definition: elog.c:909
pid_t active_pid
Definition: slot.h:126
#define NameStr(name)
Definition: c.h:681
slock_t mutex
Definition: slot.h:120

◆ ReplicationSlotCleanup()

void ReplicationSlotCleanup ( void  )

Definition at line 525 of file slot.c.

References ReplicationSlot::active_cv, ReplicationSlot::active_pid, Assert, ConditionVariableBroadcast(), ReplicationSlot::data, i, ReplicationSlot::in_use, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, ReplicationSlot::mutex, MyProcPid, ReplicationSlotPersistentData::persistency, ReplicationSlotCtlData::replication_slots, ReplicationSlotDropPtr(), RS_TEMPORARY, SpinLockAcquire, and SpinLockRelease.

Referenced by PostgresMain(), ProcKill(), and WalSndErrorCleanup().

526 {
527  int i;
528 
529  Assert(MyReplicationSlot == NULL);
530 
531 restart:
532  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
533  for (i = 0; i < max_replication_slots; i++)
534  {
536 
537  if (!s->in_use)
538  continue;
539 
540  SpinLockAcquire(&s->mutex);
541  if (s->active_pid == MyProcPid)
542  {
544  SpinLockRelease(&s->mutex);
545  LWLockRelease(ReplicationSlotControlLock); /* avoid deadlock */
546 
548 
550  goto restart;
551  }
552  else
553  SpinLockRelease(&s->mutex);
554  }
555 
556  LWLockRelease(ReplicationSlotControlLock);
557 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
int MyProcPid
Definition: globals.c:43
ReplicationSlotPersistency persistency
Definition: slot.h:54
void ConditionVariableBroadcast(ConditionVariable *cv)
static void ReplicationSlotDropPtr(ReplicationSlot *slot)
Definition: slot.c:593
ReplicationSlotPersistentData data
Definition: slot.h:147
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define SpinLockAcquire(lock)
Definition: spin.h:62
bool in_use
Definition: slot.h:123
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
int max_replication_slots
Definition: slot.c:99
#define Assert(condition)
Definition: c.h:804
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
ConditionVariable active_cv
Definition: slot.h:153
pid_t active_pid
Definition: slot.h:126
int i
ReplicationSlot replication_slots[1]
Definition: slot.h:180
slock_t mutex
Definition: slot.h:120

◆ ReplicationSlotCreate()

void ReplicationSlotCreate ( const char *  name,
bool  db_specific,
ReplicationSlotPersistency  p,
bool  two_phase 
)

Definition at line 226 of file slot.c.

References ReplicationSlot::active_cv, ReplicationSlot::active_pid, Assert, ReplicationSlot::candidate_catalog_xmin, ReplicationSlot::candidate_restart_lsn, ReplicationSlot::candidate_restart_valid, ReplicationSlot::candidate_xmin_lsn, ConditionVariableBroadcast(), CreateSlotOnDisk(), ReplicationSlot::data, ReplicationSlotPersistentData::database, ReplicationSlot::dirty, ReplicationSlot::effective_catalog_xmin, ReplicationSlot::effective_xmin, ereport, errcode(), ERRCODE_DUPLICATE_OBJECT, errhint(), errmsg(), ERROR, i, ReplicationSlot::in_use, InvalidOid, InvalidTransactionId, InvalidXLogRecPtr, ReplicationSlot::just_dirtied, LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, ReplicationSlot::mutex, MyDatabaseId, MyProcPid, ReplicationSlotPersistentData::name, NameStr, namestrcpy(), ReplicationSlotPersistentData::persistency, pgstat_report_replslot_create(), ReplicationSlotCtlData::replication_slots, ReplicationSlotValidateName(), SlotIsLogical, SpinLockAcquire, SpinLockRelease, two_phase, ReplicationSlotPersistentData::two_phase, and ReplicationSlotPersistentData::two_phase_at.

Referenced by create_logical_replication_slot(), create_physical_replication_slot(), and CreateReplicationSlot().

228 {
229  ReplicationSlot *slot = NULL;
230  int i;
231 
232  Assert(MyReplicationSlot == NULL);
233 
235 
236  /*
237  * If some other backend ran this code concurrently with us, we'd likely
238  * both allocate the same slot, and that would be bad. We'd also be at
239  * risk of missing a name collision. Also, we don't want to try to create
240  * a new slot while somebody's busy cleaning up an old one, because we
241  * might both be monkeying with the same directory.
242  */
243  LWLockAcquire(ReplicationSlotAllocationLock, LW_EXCLUSIVE);
244 
245  /*
246  * Check for name collision, and identify an allocatable slot. We need to
247  * hold ReplicationSlotControlLock in shared mode for this, so that nobody
248  * else can change the in_use flags while we're looking at them.
249  */
250  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
251  for (i = 0; i < max_replication_slots; i++)
252  {
254 
255  if (s->in_use && strcmp(name, NameStr(s->data.name)) == 0)
256  ereport(ERROR,
258  errmsg("replication slot \"%s\" already exists", name)));
259  if (!s->in_use && slot == NULL)
260  slot = s;
261  }
262  LWLockRelease(ReplicationSlotControlLock);
263 
264  /* If all slots are in use, we're out of luck. */
265  if (slot == NULL)
266  ereport(ERROR,
267  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
268  errmsg("all replication slots are in use"),
269  errhint("Free one or increase max_replication_slots.")));
270 
271  /*
272  * Since this slot is not in use, nobody should be looking at any part of
273  * it other than the in_use field unless they're trying to allocate it.
274  * And since we hold ReplicationSlotAllocationLock, nobody except us can
275  * be doing that. So it's safe to initialize the slot.
276  */
277  Assert(!slot->in_use);
278  Assert(slot->active_pid == 0);
279 
280  /* first initialize persistent data */
281  memset(&slot->data, 0, sizeof(ReplicationSlotPersistentData));
282  namestrcpy(&slot->data.name, name);
283  slot->data.database = db_specific ? MyDatabaseId : InvalidOid;
284  slot->data.persistency = persistency;
285  slot->data.two_phase = two_phase;
287 
288  /* and then data only present in shared memory */
289  slot->just_dirtied = false;
290  slot->dirty = false;
297 
298  /*
299  * Create the slot on disk. We haven't actually marked the slot allocated
300  * yet, so no special cleanup is required if this errors out.
301  */
302  CreateSlotOnDisk(slot);
303 
304  /*
305  * We need to briefly prevent any other backend from iterating over the
306  * slots while we flip the in_use flag. We also need to set the active
307  * flag while holding the ControlLock as otherwise a concurrent
308  * ReplicationSlotAcquire() could acquire the slot as well.
309  */
310  LWLockAcquire(ReplicationSlotControlLock, LW_EXCLUSIVE);
311 
312  slot->in_use = true;
313 
314  /* We can now mark the slot active, and that makes it our slot. */
315  SpinLockAcquire(&slot->mutex);
316  Assert(slot->active_pid == 0);
317  slot->active_pid = MyProcPid;
318  SpinLockRelease(&slot->mutex);
319  MyReplicationSlot = slot;
320 
321  LWLockRelease(ReplicationSlotControlLock);
322 
323  /*
324  * Create statistics entry for the new logical slot. We don't collect any
325  * stats for physical slots, so no need to create an entry for the same.
326  * See ReplicationSlotDropPtr for why we need to do this before releasing
327  * ReplicationSlotAllocationLock.
328  */
329  if (SlotIsLogical(slot))
331 
332  /*
333  * Now that the slot has been marked as in_use and active, it's safe to
334  * let somebody else try to allocate a slot.
335  */
336  LWLockRelease(ReplicationSlotAllocationLock);
337 
338  /* Let everybody know we've modified this slot */
340 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
TransactionId candidate_catalog_xmin
Definition: slot.h:162
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
int MyProcPid
Definition: globals.c:43
int errhint(const char *fmt,...)
Definition: elog.c:1156
void namestrcpy(Name name, const char *str)
Definition: name.c:233
ReplicationSlotPersistency persistency
Definition: slot.h:54
void ConditionVariableBroadcast(ConditionVariable *cv)
int errcode(int sqlerrcode)
Definition: elog.c:698
ReplicationSlotPersistentData data
Definition: slot.h:147
static void CreateSlotOnDisk(ReplicationSlot *slot)
Definition: slot.c:1455
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
bool ReplicationSlotValidateName(const char *name, int elevel)
Definition: slot.c:172
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define ERROR
Definition: elog.h:46
TransactionId effective_xmin
Definition: slot.h:143
XLogRecPtr candidate_restart_valid
Definition: slot.h:164
#define InvalidTransactionId
Definition: transam.h:31
#define SlotIsLogical(slot)
Definition: slot.h:169
bool in_use
Definition: slot.h:123
#define SpinLockRelease(lock)
Definition: spin.h:64
bool just_dirtied
Definition: slot.h:129
TransactionId effective_catalog_xmin
Definition: slot.h:144
Oid MyDatabaseId
Definition: globals.c:88
static bool two_phase
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
int max_replication_slots
Definition: slot.c:99
#define Assert(condition)
Definition: c.h:804
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
ConditionVariable active_cv
Definition: slot.h:153
const char * name
Definition: encode.c:561
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:163
void pgstat_report_replslot_create(const char *slotname)
Definition: pgstat.c:1843
int errmsg(const char *fmt,...)
Definition: elog.c:909
pid_t active_pid
Definition: slot.h:126
int i
#define NameStr(name)
Definition: c.h:681
ReplicationSlot replication_slots[1]
Definition: slot.h:180
slock_t mutex
Definition: slot.h:120
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
bool dirty
Definition: slot.h:130
XLogRecPtr candidate_restart_lsn
Definition: slot.h:165
XLogRecPtr two_phase_at
Definition: slot.h:90

◆ ReplicationSlotDrop()

void ReplicationSlotDrop ( const char *  name,
bool  nowait 
)

Definition at line 563 of file slot.c.

References Assert, ReplicationSlotAcquire(), and ReplicationSlotDropAcquired().

Referenced by DropReplicationSlot(), and pg_drop_replication_slot().

564 {
565  Assert(MyReplicationSlot == NULL);
566 
567  ReplicationSlotAcquire(name, nowait);
568 
570 }
void ReplicationSlotAcquire(const char *name, bool nowait)
Definition: slot.c:380
static void ReplicationSlotDropAcquired(void)
Definition: slot.c:576
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:804
const char * name
Definition: encode.c:561

◆ ReplicationSlotDropAtPubNode()

void ReplicationSlotDropAtPubNode ( WalReceiverConn wrconn,
char *  slotname,
bool  missing_ok 
)

Definition at line 1391 of file subscriptioncmds.c.

References appendStringInfo(), Assert, StringInfoData::data, ereport, WalRcvExecResult::err, errcode(), errmsg(), ERROR, initStringInfo(), load_file(), LOG, NOTICE, pfree(), PG_END_TRY, PG_FINALLY, PG_TRY, quote_identifier(), WalRcvExecResult::sqlstate, WalRcvExecResult::status, walrcv_clear_result(), WALRCV_ERROR, walrcv_exec, and WALRCV_OK_COMMAND.

Referenced by AlterSubscription_refresh(), DropSubscription(), LogicalRepSyncTableStart(), and process_syncing_tables_for_sync().

1392 {
1393  StringInfoData cmd;
1394 
1395  Assert(wrconn);
1396 
1397  load_file("libpqwalreceiver", false);
1398 
1399  initStringInfo(&cmd);
1400  appendStringInfo(&cmd, "DROP_REPLICATION_SLOT %s WAIT", quote_identifier(slotname));
1401 
1402  PG_TRY();
1403  {
1404  WalRcvExecResult *res;
1405 
1406  res = walrcv_exec(wrconn, cmd.data, 0, NULL);
1407 
1408  if (res->status == WALRCV_OK_COMMAND)
1409  {
1410  /* NOTICE. Success. */
1411  ereport(NOTICE,
1412  (errmsg("dropped replication slot \"%s\" on publisher",
1413  slotname)));
1414  }
1415  else if (res->status == WALRCV_ERROR &&
1416  missing_ok &&
1417  res->sqlstate == ERRCODE_UNDEFINED_OBJECT)
1418  {
1419  /* LOG. Error, but missing_ok = true. */
1420  ereport(LOG,
1421  (errmsg("could not drop replication slot \"%s\" on publisher: %s",
1422  slotname, res->err)));
1423  }
1424  else
1425  {
1426  /* ERROR. */
1427  ereport(ERROR,
1428  (errcode(ERRCODE_CONNECTION_FAILURE),
1429  errmsg("could not drop replication slot \"%s\" on publisher: %s",
1430  slotname, res->err)));
1431  }
1432 
1433  walrcv_clear_result(res);
1434  }
1435  PG_FINALLY();
1436  {
1437  pfree(cmd.data);
1438  }
1439  PG_END_TRY();
1440 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11374
int errcode(int sqlerrcode)
Definition: elog.c:698
#define LOG
Definition: elog.h:26
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:436
void pfree(void *pointer)
Definition: mcxt.c:1169
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ERROR
Definition: elog.h:46
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define PG_FINALLY()
Definition: elog.h:330
#define ereport(elevel,...)
Definition: elog.h:157
#define NOTICE
Definition: elog.h:37
#define Assert(condition)
Definition: c.h:804
WalRcvExecStatus status
Definition: walreceiver.h:216
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:146
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define PG_TRY()
Definition: elog.h:313
#define PG_END_TRY()
Definition: elog.h:338
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:430

◆ ReplicationSlotMarkDirty()

◆ ReplicationSlotNameForTablesync()

void ReplicationSlotNameForTablesync ( Oid  suboid,
Oid  relid,
char *  syncslotname,
int  szslot 
)

Definition at line 892 of file tablesync.c.

References GetSystemIdentifier(), snprintf, and UINT64_FORMAT.

Referenced by AlterSubscription_refresh(), DropSubscription(), LogicalRepSyncTableStart(), process_syncing_tables_for_sync(), and ReportSlotConnectionError().

894 {
895  snprintf(syncslotname, szslot, "pg_%u_sync_%u_" UINT64_FORMAT, suboid,
896  relid, GetSystemIdentifier());
897 }
uint64 GetSystemIdentifier(void)
Definition: xlog.c:4958
#define snprintf
Definition: port.h:216
#define UINT64_FORMAT
Definition: c.h:484

◆ ReplicationSlotPersist()

void ReplicationSlotPersist ( void  )

Definition at line 745 of file slot.c.

References Assert, ReplicationSlot::data, ReplicationSlot::mutex, MyReplicationSlot, ReplicationSlotPersistentData::persistency, ReplicationSlotMarkDirty(), ReplicationSlotSave(), RS_PERSISTENT, SpinLockAcquire, and SpinLockRelease.

Referenced by copy_replication_slot(), CreateReplicationSlot(), and pg_create_logical_replication_slot().

746 {
748 
749  Assert(slot != NULL);
751 
752  SpinLockAcquire(&slot->mutex);
754  SpinLockRelease(&slot->mutex);
755 
758 }
ReplicationSlotPersistency persistency
Definition: slot.h:54
void ReplicationSlotSave(void)
Definition: slot.c:710
ReplicationSlotPersistentData data
Definition: slot.h:147
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:804
slock_t mutex
Definition: slot.h:120
void ReplicationSlotMarkDirty(void)
Definition: slot.c:728

◆ ReplicationSlotRelease()

void ReplicationSlotRelease ( void  )

Definition at line 469 of file slot.c.

References ReplicationSlot::active_cv, ReplicationSlot::active_pid, Assert, ConditionVariableBroadcast(), ReplicationSlot::data, ReplicationSlot::effective_xmin, InvalidTransactionId, LW_SHARED, LWLockAcquire(), LWLockRelease(), ReplicationSlot::mutex, MyProc, MyReplicationSlot, ReplicationSlotPersistentData::persistency, PGPROC::pgxactoff, PROC_IN_LOGICAL_DECODING, ProcGlobal, ReplicationSlotDropAcquired(), ReplicationSlotsComputeRequiredXmin(), RS_EPHEMERAL, RS_PERSISTENT, SpinLockAcquire, SpinLockRelease, PGPROC::statusFlags, PROC_HDR::statusFlags, TransactionIdIsValid, and ReplicationSlotPersistentData::xmin.

Referenced by copy_replication_slot(), CreateReplicationSlot(), InvalidatePossiblyObsoleteSlot(), pg_create_logical_replication_slot(), pg_create_physical_replication_slot(), pg_logical_slot_get_changes_guts(), pg_replication_slot_advance(), PostgresMain(), ProcKill(), StartLogicalReplication(), StartReplication(), and WalSndErrorCleanup().

470 {
472 
473  Assert(slot != NULL && slot->active_pid != 0);
474 
475  if (slot->data.persistency == RS_EPHEMERAL)
476  {
477  /*
478  * Delete the slot. There is no !PANIC case where this is allowed to
479  * fail, all that may happen is an incomplete cleanup of the on-disk
480  * data.
481  */
483  }
484 
485  /*
486  * If slot needed to temporarily restrain both data and catalog xmin to
487  * create the catalog snapshot, remove that temporary constraint.
488  * Snapshots can only be exported while the initial snapshot is still
489  * acquired.
490  */
491  if (!TransactionIdIsValid(slot->data.xmin) &&
493  {
494  SpinLockAcquire(&slot->mutex);
496  SpinLockRelease(&slot->mutex);
498  }
499 
500  if (slot->data.persistency == RS_PERSISTENT)
501  {
502  /*
503  * Mark persistent slot inactive. We're not freeing it, just
504  * disconnecting, but wake up others that may be waiting for it.
505  */
506  SpinLockAcquire(&slot->mutex);
507  slot->active_pid = 0;
508  SpinLockRelease(&slot->mutex);
510  }
511 
512  MyReplicationSlot = NULL;
513 
514  /* might not have been set when we've been a plain slot */
515  LWLockAcquire(ProcArrayLock, LW_SHARED);
518  LWLockRelease(ProcArrayLock);
519 }
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:61
PGPROC * MyProc
Definition: proc.c:68
ReplicationSlotPersistency persistency
Definition: slot.h:54
void ConditionVariableBroadcast(ConditionVariable *cv)
PROC_HDR * ProcGlobal
Definition: proc.c:80
uint8 statusFlags
Definition: proc.h:189
ReplicationSlotPersistentData data
Definition: slot.h:147
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define SpinLockAcquire(lock)
Definition: spin.h:62
static void ReplicationSlotDropAcquired(void)
Definition: slot.c:576
TransactionId effective_xmin
Definition: slot.h:143
#define InvalidTransactionId
Definition: transam.h:31
TransactionId xmin
Definition: slot.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:804
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
ConditionVariable active_cv
Definition: slot.h:153
pid_t active_pid
Definition: slot.h:126
int pgxactoff
Definition: proc.h:148
uint8 * statusFlags
Definition: proc.h:333
slock_t mutex
Definition: slot.h:120
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:767

◆ ReplicationSlotReserveWal()

void ReplicationSlotReserveWal ( void  )

Definition at line 1081 of file slot.c.

References Assert, ReplicationSlot::data, GetRedoRecPtr(), GetXLogInsertRecPtr(), InvalidXLogRecPtr, LogStandbySnapshot(), ReplicationSlot::mutex, MyReplicationSlot, RecoveryInProgress(), ReplicationSlotsComputeRequiredLSN(), ReplicationSlotPersistentData::restart_lsn, SlotIsLogical, SpinLockAcquire, SpinLockRelease, wal_segment_size, XLByteToSeg, XLogFlush(), and XLogGetLastRemovedSegno().

Referenced by create_physical_replication_slot(), CreateInitDecodingContext(), and CreateReplicationSlot().

1082 {
1084 
1085  Assert(slot != NULL);
1087 
1088  /*
1089  * The replication slot mechanism is used to prevent removal of required
1090  * WAL. As there is no interlock between this routine and checkpoints, WAL
1091  * segments could concurrently be removed when a now stale return value of
1092  * ReplicationSlotsComputeRequiredLSN() is used. In the unlikely case that
1093  * this happens we'll just retry.
1094  */
1095  while (true)
1096  {
1097  XLogSegNo segno;
1098  XLogRecPtr restart_lsn;
1099 
1100  /*
1101  * For logical slots log a standby snapshot and start logical decoding
1102  * at exactly that position. That allows the slot to start up more
1103  * quickly.
1104  *
1105  * That's not needed (or indeed helpful) for physical slots as they'll
1106  * start replay at the last logged checkpoint anyway. Instead return
1107  * the location of the last redo LSN. While that slightly increases
1108  * the chance that we have to retry, it's where a base backup has to
1109  * start replay at.
1110  */
1111  if (!RecoveryInProgress() && SlotIsLogical(slot))
1112  {
1113  XLogRecPtr flushptr;
1114 
1115  /* start at current insert position */
1116  restart_lsn = GetXLogInsertRecPtr();
1117  SpinLockAcquire(&slot->mutex);
1118  slot->data.restart_lsn = restart_lsn;
1119  SpinLockRelease(&slot->mutex);
1120 
1121  /* make sure we have enough information to start */
1122  flushptr = LogStandbySnapshot();
1123 
1124  /* and make sure it's fsynced to disk */
1125  XLogFlush(flushptr);
1126  }
1127  else
1128  {
1129  restart_lsn = GetRedoRecPtr();
1130  SpinLockAcquire(&slot->mutex);
1131  slot->data.restart_lsn = restart_lsn;
1132  SpinLockRelease(&slot->mutex);
1133  }
1134 
1135  /* prevent WAL removal as fast as possible */
1137 
1138  /*
1139  * If all required WAL is still there, great, otherwise retry. The
1140  * slot should prevent further removal of WAL, unless there's a
1141  * concurrent ReplicationSlotsComputeRequiredLSN() after we've written
1142  * the new restart_lsn above, so normally we should never need to loop
1143  * more than twice.
1144  */
1146  if (XLogGetLastRemovedSegno() < segno)
1147  break;
1148  }
1149 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
int wal_segment_size
Definition: xlog.c:119
ReplicationSlotPersistentData data
Definition: slot.h:147
bool RecoveryInProgress(void)
Definition: xlog.c:8220
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2860
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogSegNo XLogGetLastRemovedSegno(void)
Definition: xlog.c:4003
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:817
XLogRecPtr LogStandbySnapshot(void)
Definition: standby.c:1220
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:11764
uint64 XLogSegNo
Definition: xlogdefs.h:48
#define SlotIsLogical(slot)
Definition: slot.h:169
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
XLogRecPtr restart_lsn
Definition: slot.h:73
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:8512
slock_t mutex
Definition: slot.h:120
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ ReplicationSlotSave()

void ReplicationSlotSave ( void  )

Definition at line 710 of file slot.c.

References Assert, ReplicationSlot::data, ERROR, MAXPGPATH, ReplicationSlotPersistentData::name, NameStr, SaveSlotToPath(), and sprintf.

Referenced by copy_replication_slot(), create_physical_replication_slot(), CreateDecodingContext(), CreateInitDecodingContext(), CreateReplicationSlot(), InvalidatePossiblyObsoleteSlot(), LogicalConfirmReceivedLocation(), and ReplicationSlotPersist().

711 {
712  char path[MAXPGPATH];
713 
714  Assert(MyReplicationSlot != NULL);
715 
716  sprintf(path, "pg_replslot/%s", NameStr(MyReplicationSlot->data.name));
718 }
ReplicationSlotPersistentData data
Definition: slot.h:147
static void SaveSlotToPath(ReplicationSlot *slot, const char *path, int elevel)
Definition: slot.c:1516
#define sprintf
Definition: port.h:218
#define ERROR
Definition: elog.h:46
#define MAXPGPATH
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:804
#define NameStr(name)
Definition: c.h:681

◆ ReplicationSlotsComputeLogicalRestartLSN()

XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN ( void  )

Definition at line 860 of file slot.c.

References ReplicationSlot::data, i, ReplicationSlot::in_use, InvalidXLogRecPtr, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, ReplicationSlot::mutex, ReplicationSlotCtlData::replication_slots, ReplicationSlotPersistentData::restart_lsn, SlotIsLogical, SpinLockAcquire, and SpinLockRelease.

Referenced by CheckPointLogicalRewriteHeap(), and CheckPointSnapBuild().

861 {
862  XLogRecPtr result = InvalidXLogRecPtr;
863  int i;
864 
865  if (max_replication_slots <= 0)
866  return InvalidXLogRecPtr;
867 
868  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
869 
870  for (i = 0; i < max_replication_slots; i++)
871  {
872  ReplicationSlot *s;
873  XLogRecPtr restart_lsn;
874 
876 
877  /* cannot change while ReplicationSlotCtlLock is held */
878  if (!s->in_use)
879  continue;
880 
881  /* we're only interested in logical slots */
882  if (!SlotIsLogical(s))
883  continue;
884 
885  /* read once, it's ok if it increases while we're checking */
886  SpinLockAcquire(&s->mutex);
887  restart_lsn = s->data.restart_lsn;
888  SpinLockRelease(&s->mutex);
889 
890  if (restart_lsn == InvalidXLogRecPtr)
891  continue;
892 
893  if (result == InvalidXLogRecPtr ||
894  restart_lsn < result)
895  result = restart_lsn;
896  }
897 
898  LWLockRelease(ReplicationSlotControlLock);
899 
900  return result;
901 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:147
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SlotIsLogical(slot)
Definition: slot.h:169
bool in_use
Definition: slot.h:123
#define SpinLockRelease(lock)
Definition: spin.h:64
int max_replication_slots
Definition: slot.c:99
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr restart_lsn
Definition: slot.h:73
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
int i
ReplicationSlot replication_slots[1]
Definition: slot.h:180
slock_t mutex
Definition: slot.h:120

◆ ReplicationSlotsComputeRequiredLSN()

void ReplicationSlotsComputeRequiredLSN ( void  )

Definition at line 817 of file slot.c.

References Assert, ReplicationSlot::data, i, ReplicationSlot::in_use, InvalidXLogRecPtr, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, ReplicationSlot::mutex, ReplicationSlotCtlData::replication_slots, ReplicationSlotPersistentData::restart_lsn, SpinLockAcquire, SpinLockRelease, and XLogSetReplicationSlotMinimumLSN().

Referenced by copy_replication_slot(), InvalidateObsoleteReplicationSlots(), LogicalConfirmReceivedLocation(), pg_replication_slot_advance(), PhysicalConfirmReceivedLocation(), ReplicationSlotDropPtr(), ReplicationSlotReserveWal(), and StartupReplicationSlots().

818 {
819  int i;
820  XLogRecPtr min_required = InvalidXLogRecPtr;
821 
822  Assert(ReplicationSlotCtl != NULL);
823 
824  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
825  for (i = 0; i < max_replication_slots; i++)
826  {
828  XLogRecPtr restart_lsn;
829 
830  if (!s->in_use)
831  continue;
832 
833  SpinLockAcquire(&s->mutex);
834  restart_lsn = s->data.restart_lsn;
835  SpinLockRelease(&s->mutex);
836 
837  if (restart_lsn != InvalidXLogRecPtr &&
838  (min_required == InvalidXLogRecPtr ||
839  restart_lsn < min_required))
840  min_required = restart_lsn;
841  }
842  LWLockRelease(ReplicationSlotControlLock);
843 
844  XLogSetReplicationSlotMinimumLSN(min_required);
845 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:147
void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
Definition: xlog.c:2742
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define SpinLockAcquire(lock)
Definition: spin.h:62
bool in_use
Definition: slot.h:123
#define SpinLockRelease(lock)
Definition: spin.h:64
int max_replication_slots
Definition: slot.c:99
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
XLogRecPtr restart_lsn
Definition: slot.h:73
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
int i
ReplicationSlot replication_slots[1]
Definition: slot.h:180
slock_t mutex
Definition: slot.h:120

◆ ReplicationSlotsComputeRequiredXmin()

void ReplicationSlotsComputeRequiredXmin ( bool  already_locked)

Definition at line 767 of file slot.c.

References Assert, ReplicationSlot::effective_catalog_xmin, ReplicationSlot::effective_xmin, i, ReplicationSlot::in_use, InvalidTransactionId, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, ReplicationSlot::mutex, ProcArraySetReplicationSlotXmin(), ReplicationSlotCtlData::replication_slots, SpinLockAcquire, SpinLockRelease, TransactionIdIsValid, and TransactionIdPrecedes().

Referenced by copy_replication_slot(), CreateInitDecodingContext(), InvalidateObsoleteReplicationSlots(), LogicalConfirmReceivedLocation(), pg_replication_slot_advance(), PhysicalReplicationSlotNewXmin(), ReplicationSlotDropPtr(), ReplicationSlotRelease(), and StartupReplicationSlots().

768 {
769  int i;
771  TransactionId agg_catalog_xmin = InvalidTransactionId;
772 
773  Assert(ReplicationSlotCtl != NULL);
774 
775  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
776 
777  for (i = 0; i < max_replication_slots; i++)
778  {
780  TransactionId effective_xmin;
781  TransactionId effective_catalog_xmin;
782 
783  if (!s->in_use)
784  continue;
785 
786  SpinLockAcquire(&s->mutex);
787  effective_xmin = s->effective_xmin;
788  effective_catalog_xmin = s->effective_catalog_xmin;
789  SpinLockRelease(&s->mutex);
790 
791  /* check the data xmin */
792  if (TransactionIdIsValid(effective_xmin) &&
793  (!TransactionIdIsValid(agg_xmin) ||
794  TransactionIdPrecedes(effective_xmin, agg_xmin)))
795  agg_xmin = effective_xmin;
796 
797  /* check the catalog xmin */
798  if (TransactionIdIsValid(effective_catalog_xmin) &&
799  (!TransactionIdIsValid(agg_catalog_xmin) ||
800  TransactionIdPrecedes(effective_catalog_xmin, agg_catalog_xmin)))
801  agg_catalog_xmin = effective_catalog_xmin;
802  }
803 
804  LWLockRelease(ReplicationSlotControlLock);
805 
806  ProcArraySetReplicationSlotXmin(agg_xmin, agg_catalog_xmin, already_locked);
807 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
uint32 TransactionId
Definition: c.h:587
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define SpinLockAcquire(lock)
Definition: spin.h:62
TransactionId effective_xmin
Definition: slot.h:143
#define InvalidTransactionId
Definition: transam.h:31
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
bool in_use
Definition: slot.h:123
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:144
int max_replication_slots
Definition: slot.c:99
#define Assert(condition)
Definition: c.h:804
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
int i
void ProcArraySetReplicationSlotXmin(TransactionId xmin, TransactionId catalog_xmin, bool already_locked)
Definition: procarray.c:3868
ReplicationSlot replication_slots[1]
Definition: slot.h:180
slock_t mutex
Definition: slot.h:120
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ ReplicationSlotsCountDBSlots()

bool ReplicationSlotsCountDBSlots ( Oid  dboid,
int *  nslots,
int *  nactive 
)

Definition at line 912 of file slot.c.

References ReplicationSlot::active_pid, ReplicationSlot::data, ReplicationSlotPersistentData::database, i, ReplicationSlot::in_use, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, ReplicationSlot::mutex, ReplicationSlotCtlData::replication_slots, SlotIsLogical, SpinLockAcquire, and SpinLockRelease.

Referenced by dropdb().

913 {
914  int i;
915 
916  *nslots = *nactive = 0;
917 
918  if (max_replication_slots <= 0)
919  return false;
920 
921  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
922  for (i = 0; i < max_replication_slots; i++)
923  {
924  ReplicationSlot *s;
925 
927 
928  /* cannot change while ReplicationSlotCtlLock is held */
929  if (!s->in_use)
930  continue;
931 
932  /* only logical slots are database specific, skip */
933  if (!SlotIsLogical(s))
934  continue;
935 
936  /* not our database, skip */
937  if (s->data.database != dboid)
938  continue;
939 
940  /* count slots with spinlock held */
941  SpinLockAcquire(&s->mutex);
942  (*nslots)++;
943  if (s->active_pid != 0)
944  (*nactive)++;
945  SpinLockRelease(&s->mutex);
946  }
947  LWLockRelease(ReplicationSlotControlLock);
948 
949  if (*nslots > 0)
950  return true;
951  return false;
952 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
ReplicationSlotPersistentData data
Definition: slot.h:147
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SlotIsLogical(slot)
Definition: slot.h:169
bool in_use
Definition: slot.h:123
#define SpinLockRelease(lock)
Definition: spin.h:64
int max_replication_slots
Definition: slot.c:99
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
pid_t active_pid
Definition: slot.h:126
int i
ReplicationSlot replication_slots[1]
Definition: slot.h:180
slock_t mutex
Definition: slot.h:120

◆ ReplicationSlotsDropDBSlots()

void ReplicationSlotsDropDBSlots ( Oid  dboid)

Definition at line 968 of file slot.c.

References ReplicationSlot::active_pid, ReplicationSlot::data, ReplicationSlotPersistentData::database, ereport, errcode(), errmsg(), ERROR, i, ReplicationSlot::in_use, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, ReplicationSlot::mutex, MyProcPid, ReplicationSlotPersistentData::name, NameStr, ReplicationSlotCtlData::replication_slots, ReplicationSlotDropAcquired(), SlotIsLogical, SpinLockAcquire, and SpinLockRelease.

Referenced by dbase_redo(), and dropdb().

969 {
970  int i;
971 
972  if (max_replication_slots <= 0)
973  return;
974 
975 restart:
976  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
977  for (i = 0; i < max_replication_slots; i++)
978  {
979  ReplicationSlot *s;
980  char *slotname;
981  int active_pid;
982 
984 
985  /* cannot change while ReplicationSlotCtlLock is held */
986  if (!s->in_use)
987  continue;
988 
989  /* only logical slots are database specific, skip */
990  if (!SlotIsLogical(s))
991  continue;
992 
993  /* not our database, skip */
994  if (s->data.database != dboid)
995  continue;
996 
997  /* acquire slot, so ReplicationSlotDropAcquired can be reused */
998  SpinLockAcquire(&s->mutex);
999  /* can't change while ReplicationSlotControlLock is held */
1000  slotname = NameStr(s->data.name);
1001  active_pid = s->active_pid;
1002  if (active_pid == 0)
1003  {
1004  MyReplicationSlot = s;
1005  s->active_pid = MyProcPid;
1006  }
1007  SpinLockRelease(&s->mutex);
1008 
1009  /*
1010  * Even though we hold an exclusive lock on the database object a
1011  * logical slot for that DB can still be active, e.g. if it's
1012  * concurrently being dropped by a backend connected to another DB.
1013  *
1014  * That's fairly unlikely in practice, so we'll just bail out.
1015  */
1016  if (active_pid)
1017  ereport(ERROR,
1018  (errcode(ERRCODE_OBJECT_IN_USE),
1019  errmsg("replication slot \"%s\" is active for PID %d",
1020  slotname, active_pid)));
1021 
1022  /*
1023  * To avoid duplicating ReplicationSlotDropAcquired() and to avoid
1024  * holding ReplicationSlotControlLock over filesystem operations,
1025  * release ReplicationSlotControlLock and use
1026  * ReplicationSlotDropAcquired.
1027  *
1028  * As that means the set of slots could change, restart scan from the
1029  * beginning each time we release the lock.
1030  */
1031  LWLockRelease(ReplicationSlotControlLock);
1033  goto restart;
1034  }
1035  LWLockRelease(ReplicationSlotControlLock);
1036 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
int MyProcPid
Definition: globals.c:43
int errcode(int sqlerrcode)
Definition: elog.c:698
ReplicationSlotPersistentData data
Definition: slot.h:147
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define SpinLockAcquire(lock)
Definition: spin.h:62
static void ReplicationSlotDropAcquired(void)
Definition: slot.c:576
#define ERROR
Definition: elog.h:46
#define SlotIsLogical(slot)
Definition: slot.h:169
bool in_use
Definition: slot.h:123
#define SpinLockRelease(lock)
Definition: spin.h:64
#define ereport(elevel,...)
Definition: elog.h:157
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
int max_replication_slots
Definition: slot.c:99
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
int errmsg(const char *fmt,...)
Definition: elog.c:909
pid_t active_pid
Definition: slot.h:126
int i
#define NameStr(name)
Definition: c.h:681
ReplicationSlot replication_slots[1]
Definition: slot.h:180
slock_t mutex
Definition: slot.h:120

◆ ReplicationSlotsShmemInit()

void ReplicationSlotsShmemInit ( void  )

Definition at line 132 of file slot.c.

References ReplicationSlot::active_cv, ConditionVariableInit(), i, ReplicationSlot::io_in_progress_lock, LWLockInitialize(), LWTRANCHE_REPLICATION_SLOT_IO, max_replication_slots, MemSet, ReplicationSlot::mutex, ReplicationSlotCtlData::replication_slots, ReplicationSlotsShmemSize(), ShmemInitStruct(), and SpinLockInit.

Referenced by CreateSharedMemoryAndSemaphores().

133 {
134  bool found;
135 
136  if (max_replication_slots == 0)
137  return;
138 
140  ShmemInitStruct("ReplicationSlot Ctl", ReplicationSlotsShmemSize(),
141  &found);
142 
143  if (!found)
144  {
145  int i;
146 
147  /* First time through, so initialize */
149 
150  for (i = 0; i < max_replication_slots; i++)
151  {
153 
154  /* everything else is zeroed by the memset above */
155  SpinLockInit(&slot->mutex);
159  }
160  }
161 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:114
#define SpinLockInit(lock)
Definition: spin.h:60
#define MemSet(start, val, len)
Definition: c.h:1008
void ConditionVariableInit(ConditionVariable *cv)
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:736
int max_replication_slots
Definition: slot.c:99
ConditionVariable active_cv
Definition: slot.h:153
int i
ReplicationSlot replication_slots[1]
Definition: slot.h:180
slock_t mutex
Definition: slot.h:120
LWLock io_in_progress_lock
Definition: slot.h:150

◆ ReplicationSlotsShmemSize()

Size ReplicationSlotsShmemSize ( void  )

Definition at line 114 of file slot.c.

References add_size(), max_replication_slots, mul_size(), and offsetof.

Referenced by CalculateShmemSize(), and ReplicationSlotsShmemInit().

115 {
116  Size size = 0;
117 
118  if (max_replication_slots == 0)
119  return size;
120 
121  size = offsetof(ReplicationSlotCtlData, replication_slots);
122  size = add_size(size,
124 
125  return size;
126 }
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
int max_replication_slots
Definition: slot.c:99
size_t Size
Definition: c.h:540
#define offsetof(type, field)
Definition: c.h:727

◆ ReplicationSlotValidateName()

bool ReplicationSlotValidateName ( const char *  name,
int  elevel 
)

Definition at line 172 of file slot.c.

References ereport, errcode(), errhint(), errmsg(), and NAMEDATALEN.

Referenced by check_primary_slot_name(), parse_subscription_options(), ReplicationSlotCreate(), and StartupReorderBuffer().

173 {
174  const char *cp;
175 
176  if (strlen(name) == 0)
177  {
178  ereport(elevel,
179  (errcode(ERRCODE_INVALID_NAME),
180  errmsg("replication slot name \"%s\" is too short",
181  name)));
182  return false;
183  }
184 
185  if (strlen(name) >= NAMEDATALEN)
186  {
187  ereport(elevel,
188  (errcode(ERRCODE_NAME_TOO_LONG),
189  errmsg("replication slot name \"%s\" is too long",
190  name)));
191  return false;
192  }
193 
194  for (cp = name; *cp; cp++)
195  {
196  if (!((*cp >= 'a' && *cp <= 'z')
197  || (*cp >= '0' && *cp <= '9')
198  || (*cp == '_')))
199  {
200  ereport(elevel,
201  (errcode(ERRCODE_INVALID_NAME),
202  errmsg("replication slot name \"%s\" contains invalid character",
203  name),
204  errhint("Replication slot names may only contain lower case letters, numbers, and the underscore character.")));
205  return false;
206  }
207  }
208  return true;
209 }
int errhint(const char *fmt,...)
Definition: elog.c:1156
int errcode(int sqlerrcode)
Definition: elog.c:698
#define NAMEDATALEN
static int elevel
Definition: vacuumlazy.c:401
#define ereport(elevel,...)
Definition: elog.h:157
const char * name
Definition: encode.c:561
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ SearchNamedReplicationSlot()

ReplicationSlot* SearchNamedReplicationSlot ( const char *  name,
bool  need_lock 
)

Definition at line 348 of file slot.c.

References ReplicationSlot::data, i, ReplicationSlot::in_use, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, ReplicationSlotPersistentData::name, NameStr, and ReplicationSlotCtlData::replication_slots.

Referenced by pg_stat_reset_replication_slot(), pgstat_vacuum_stat(), and ReplicationSlotAcquire().

349 {
350  int i;
351  ReplicationSlot *slot = NULL;
352 
353  if (need_lock)
354  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
355 
356  for (i = 0; i < max_replication_slots; i++)
357  {
359 
360  if (s->in_use && strcmp(name, NameStr(s->data.name)) == 0)
361  {
362  slot = s;
363  break;
364  }
365  }
366 
367  if (need_lock)
368  LWLockRelease(ReplicationSlotControlLock);
369 
370  return slot;
371 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
ReplicationSlotPersistentData data
Definition: slot.h:147
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
bool in_use
Definition: slot.h:123
int max_replication_slots
Definition: slot.c:99
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
const char * name
Definition: encode.c:561
int i
#define NameStr(name)
Definition: c.h:681
ReplicationSlot replication_slots[1]
Definition: slot.h:180

◆ StartupReplicationSlots()

void StartupReplicationSlots ( void  )

Definition at line 1395 of file slot.c.

References AllocateDir(), dirent::d_name, DEBUG1, elog, ereport, errmsg(), FreeDir(), fsync_fname(), lstat, max_replication_slots, MAXPGPATH, pg_str_endswith(), ReadDir(), ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), RestoreSlotFromDisk(), rmtree(), S_ISDIR, snprintf, stat::st_mode, and WARNING.

Referenced by StartupXLOG().

1396 {
1397  DIR *replication_dir;
1398  struct dirent *replication_de;
1399 
1400  elog(DEBUG1, "starting up replication slots");
1401 
1402  /* restore all slots by iterating over all on-disk entries */
1403  replication_dir = AllocateDir("pg_replslot");
1404  while ((replication_de = ReadDir(replication_dir, "pg_replslot")) != NULL)
1405  {
1406  struct stat statbuf;
1407  char path[MAXPGPATH + 12];
1408 
1409  if (strcmp(replication_de->d_name, ".") == 0 ||
1410  strcmp(replication_de->d_name, "..") == 0)
1411  continue;
1412 
1413  snprintf(path, sizeof(path), "pg_replslot/%s", replication_de->d_name);
1414 
1415  /* we're only creating directories here, skip if it's not our's */
1416  if (lstat(path, &statbuf) == 0 && !S_ISDIR(statbuf.st_mode))
1417  continue;
1418 
1419  /* we crashed while a slot was being setup or deleted, clean up */
1420  if (pg_str_endswith(replication_de->d_name, ".tmp"))
1421  {
1422  if (!rmtree(path, true))
1423  {
1424  ereport(WARNING,
1425  (errmsg("could not remove directory \"%s\"",
1426  path)));
1427  continue;
1428  }
1429  fsync_fname("pg_replslot", true);
1430  continue;
1431  }
1432 
1433  /* looks like a slot in a normal state, restore */
1434  RestoreSlotFromDisk(replication_de->d_name);
1435  }
1436  FreeDir(replication_dir);
1437 
1438  /* currently no slots exist, we're done. */
1439  if (max_replication_slots <= 0)
1440  return;
1441 
1442  /* Now that we have recovered all the data, compute replication xmin */
1445 }
static void RestoreSlotFromDisk(const char *name)
Definition: slot.c:1670
#define DEBUG1
Definition: elog.h:25
bool pg_str_endswith(const char *str, const char *end)
Definition: string.c:31
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:671
Definition: dirent.h:9
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:817
Definition: dirent.c:25
#define MAXPGPATH
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2720
#define WARNING
Definition: elog.h:40
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:42
#define ereport(elevel,...)
Definition: elog.h:157
int max_replication_slots
Definition: slot.c:99
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2786
#define S_ISDIR(m)
Definition: win32_port.h:316
#define lstat(path, sb)
Definition: win32_port.h:276
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
char d_name[MAX_PATH]
Definition: dirent.h:15
#define snprintf
Definition: port.h:216
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:767
int FreeDir(DIR *dir)
Definition: fd.c:2838

Variable Documentation

◆ max_replication_slots

◆ MyReplicationSlot

◆ ReplicationSlotCtl

PGDLLIMPORT ReplicationSlotCtlData* ReplicationSlotCtl

Definition at line 93 of file slot.c.

Referenced by copy_replication_slot(), and pg_get_replication_slots().