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)
 
void ReplicationSlotInitialize (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)
 
int ReplicationSlotIndex (ReplicationSlot *slot)
 
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

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

Definition at line 169 of file slot.h.

◆ SlotIsPhysical

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

Definition at line 168 of file slot.h.

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:34
@ RS_PERSISTENT
Definition: slot.h:35
@ RS_EPHEMERAL
Definition: slot.h:36
@ RS_TEMPORARY
Definition: slot.h:37

Function Documentation

◆ CheckPointReplicationSlots()

void CheckPointReplicationSlots ( void  )

Definition at line 1399 of file slot.c.

1400 {
1401  int i;
1402 
1403  elog(DEBUG1, "performing replication slot checkpoint");
1404 
1405  /*
1406  * Prevent any slot from being created/dropped while we're active. As we
1407  * explicitly do *not* want to block iterating over replication_slots or
1408  * acquiring a slot we cannot take the control lock - but that's OK,
1409  * because holding ReplicationSlotAllocationLock is strictly stronger, and
1410  * enough to guarantee that nobody can change the in_use bits on us.
1411  */
1412  LWLockAcquire(ReplicationSlotAllocationLock, LW_SHARED);
1413 
1414  for (i = 0; i < max_replication_slots; i++)
1415  {
1417  char path[MAXPGPATH];
1418 
1419  if (!s->in_use)
1420  continue;
1421 
1422  /* save the slot to disk, locking is handled in SaveSlotToPath() */
1423  sprintf(path, "pg_replslot/%s", NameStr(s->data.name));
1424  SaveSlotToPath(s, path, LOG);
1425  }
1426  LWLockRelease(ReplicationSlotAllocationLock);
1427 }
#define NameStr(name)
Definition: c.h:692
#define LOG
Definition: elog.h:25
#define DEBUG1
Definition: elog.h:24
int i
Definition: isn.c:73
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1196
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1800
@ LW_SHARED
Definition: lwlock.h:105
#define MAXPGPATH
#define sprintf
Definition: port.h:227
int max_replication_slots
Definition: slot.c:100
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:94
static void SaveSlotToPath(ReplicationSlot *slot, const char *path, int elevel)
Definition: slot.c:1555
ReplicationSlot replication_slots[1]
Definition: slot.h:180
bool in_use
Definition: slot.h:123
ReplicationSlotPersistentData data
Definition: slot.h:147

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

Referenced by CheckPointGuts().

◆ CheckSlotPermissions()

void CheckSlotPermissions ( void  )

Definition at line 1105 of file slot.c.

1106 {
1107  if (!superuser() && !has_rolreplication(GetUserId()))
1108  ereport(ERROR,
1109  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1110  errmsg("must be superuser or replication role to use replication slots")));
1111 }
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
Oid GetUserId(void)
Definition: miscinit.c:491
bool has_rolreplication(Oid roleid)
Definition: miscinit.c:678
bool superuser(void)
Definition: superuser.c:46

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().

◆ CheckSlotRequirements()

void CheckSlotRequirements ( void  )

Definition at line 1083 of file slot.c.

1084 {
1085  /*
1086  * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
1087  * needs the same check.
1088  */
1089 
1090  if (max_replication_slots == 0)
1091  ereport(ERROR,
1092  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1093  errmsg("replication slots can only be used if max_replication_slots > 0")));
1094 
1096  ereport(ERROR,
1097  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1098  errmsg("replication slots can only be used if wal_level >= replica")));
1099 }
int wal_level
Definition: xlog.c:132
@ WAL_LEVEL_REPLICA
Definition: xlog.h:70

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().

◆ InvalidateObsoleteReplicationSlots()

bool InvalidateObsoleteReplicationSlots ( XLogSegNo  oldestSegno)

Definition at line 1356 of file slot.c.

1357 {
1358  XLogRecPtr oldestLSN;
1359  bool invalidated = false;
1360 
1361  XLogSegNoOffsetToRecPtr(oldestSegno, 0, wal_segment_size, oldestLSN);
1362 
1363 restart:
1364  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1365  for (int i = 0; i < max_replication_slots; i++)
1366  {
1368 
1369  if (!s->in_use)
1370  continue;
1371 
1372  if (InvalidatePossiblyObsoleteSlot(s, oldestLSN, &invalidated))
1373  {
1374  /* if the lock was released, start from scratch */
1375  goto restart;
1376  }
1377  }
1378  LWLockRelease(ReplicationSlotControlLock);
1379 
1380  /*
1381  * If any slots have been invalidated, recalculate the resource limits.
1382  */
1383  if (invalidated)
1384  {
1387  }
1388 
1389  return invalidated;
1390 }
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:806
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:856
static bool InvalidatePossiblyObsoleteSlot(ReplicationSlot *s, XLogRecPtr oldestLSN, bool *invalidated)
Definition: slot.c:1203
int wal_segment_size
Definition: xlog.c:144
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
uint64 XLogRecPtr
Definition: xlogdefs.h:21

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

Referenced by CreateCheckPoint(), and CreateRestartPoint().

◆ ReplicationSlotAcquire()

void ReplicationSlotAcquire ( const char *  name,
bool  nowait 
)

Definition at line 421 of file slot.c.

422 {
423  ReplicationSlot *s;
424  int active_pid;
425 
426  AssertArg(name != NULL);
427 
428 retry:
429  Assert(MyReplicationSlot == NULL);
430 
431  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
432 
433  /*
434  * Search for the slot with the specified name if the slot to acquire is
435  * not given. If the slot is not found, we either return -1 or error out.
436  */
437  s = SearchNamedReplicationSlot(name, false);
438  if (s == NULL || !s->in_use)
439  {
440  LWLockRelease(ReplicationSlotControlLock);
441 
442  ereport(ERROR,
443  (errcode(ERRCODE_UNDEFINED_OBJECT),
444  errmsg("replication slot \"%s\" does not exist",
445  name)));
446  }
447 
448  /*
449  * This is the slot we want; check if it's active under some other
450  * process. In single user mode, we don't need this check.
451  */
452  if (IsUnderPostmaster)
453  {
454  /*
455  * Get ready to sleep on the slot in case it is active. (We may end
456  * up not sleeping, but we don't want to do this while holding the
457  * spinlock.)
458  */
459  if (!nowait)
461 
462  SpinLockAcquire(&s->mutex);
463  if (s->active_pid == 0)
464  s->active_pid = MyProcPid;
465  active_pid = s->active_pid;
466  SpinLockRelease(&s->mutex);
467  }
468  else
469  active_pid = MyProcPid;
470  LWLockRelease(ReplicationSlotControlLock);
471 
472  /*
473  * If we found the slot but it's already active in another process, we
474  * wait until the owning process signals us that it's been released, or
475  * error out.
476  */
477  if (active_pid != MyProcPid)
478  {
479  if (!nowait)
480  {
481  /* Wait here until we get signaled, and then restart */
485  goto retry;
486  }
487 
488  ereport(ERROR,
489  (errcode(ERRCODE_OBJECT_IN_USE),
490  errmsg("replication slot \"%s\" is active for PID %d",
491  NameStr(s->data.name), active_pid)));
492  }
493  else if (!nowait)
494  ConditionVariableCancelSleep(); /* no sleep needed after all */
495 
496  /* Let everybody know we've modified this slot */
498 
499  /* We made this slot active, so it's ours now. */
500  MyReplicationSlot = s;
501 
502  /*
503  * The call to pgstat_acquire_replslot() protects against stats for a
504  * different slot, from before a restart or such, being present during
505  * pgstat_report_replslot().
506  */
507  if (SlotIsLogical(s))
509 }
#define AssertArg(condition)
Definition: c.h:817
void ConditionVariableBroadcast(ConditionVariable *cv)
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
void ConditionVariableCancelSleep(void)
const char * name
Definition: encode.c:561
int MyProcPid
Definition: globals.c:44
bool IsUnderPostmaster
Definition: globals.c:113
Assert(fmt[strlen(fmt) - 1] !='\n')
void pgstat_acquire_replslot(ReplicationSlot *slot)
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition: slot.c:373
ReplicationSlot * MyReplicationSlot
Definition: slot.c:97
#define SlotIsLogical(slot)
Definition: slot.h:169
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62
slock_t mutex
Definition: slot.h:120
pid_t active_pid
Definition: slot.h:126
ConditionVariable active_cv
Definition: slot.h:153
@ WAIT_EVENT_REPLICATION_SLOT_DROP
Definition: wait_event.h:125

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, MyReplicationSlot, name, ReplicationSlotPersistentData::name, NameStr, pgstat_acquire_replslot(), SearchNamedReplicationSlot(), SlotIsLogical, SpinLockAcquire, SpinLockRelease, and WAIT_EVENT_REPLICATION_SLOT_DROP.

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

◆ ReplicationSlotCleanup()

void ReplicationSlotCleanup ( void  )

Definition at line 574 of file slot.c.

575 {
576  int i;
577 
578  Assert(MyReplicationSlot == NULL);
579 
580 restart:
581  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
582  for (i = 0; i < max_replication_slots; i++)
583  {
585 
586  if (!s->in_use)
587  continue;
588 
589  SpinLockAcquire(&s->mutex);
590  if (s->active_pid == MyProcPid)
591  {
593  SpinLockRelease(&s->mutex);
594  LWLockRelease(ReplicationSlotControlLock); /* avoid deadlock */
595 
597 
599  goto restart;
600  }
601  else
602  SpinLockRelease(&s->mutex);
603  }
604 
605  LWLockRelease(ReplicationSlotControlLock);
606 }
static void ReplicationSlotDropPtr(ReplicationSlot *slot)
Definition: slot.c:642
ReplicationSlotPersistency persistency
Definition: slot.h:54

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, MyReplicationSlot, ReplicationSlotPersistentData::persistency, ReplicationSlotCtlData::replication_slots, ReplicationSlotCtl, ReplicationSlotDropPtr(), RS_TEMPORARY, SpinLockAcquire, and SpinLockRelease.

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

◆ ReplicationSlotCreate()

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

Definition at line 251 of file slot.c.

253 {
254  ReplicationSlot *slot = NULL;
255  int i;
256 
257  Assert(MyReplicationSlot == NULL);
258 
260 
261  /*
262  * If some other backend ran this code concurrently with us, we'd likely
263  * both allocate the same slot, and that would be bad. We'd also be at
264  * risk of missing a name collision. Also, we don't want to try to create
265  * a new slot while somebody's busy cleaning up an old one, because we
266  * might both be monkeying with the same directory.
267  */
268  LWLockAcquire(ReplicationSlotAllocationLock, LW_EXCLUSIVE);
269 
270  /*
271  * Check for name collision, and identify an allocatable slot. We need to
272  * hold ReplicationSlotControlLock in shared mode for this, so that nobody
273  * else can change the in_use flags while we're looking at them.
274  */
275  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
276  for (i = 0; i < max_replication_slots; i++)
277  {
279 
280  if (s->in_use && strcmp(name, NameStr(s->data.name)) == 0)
281  ereport(ERROR,
283  errmsg("replication slot \"%s\" already exists", name)));
284  if (!s->in_use && slot == NULL)
285  slot = s;
286  }
287  LWLockRelease(ReplicationSlotControlLock);
288 
289  /* If all slots are in use, we're out of luck. */
290  if (slot == NULL)
291  ereport(ERROR,
292  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
293  errmsg("all replication slots are in use"),
294  errhint("Free one or increase max_replication_slots.")));
295 
296  /*
297  * Since this slot is not in use, nobody should be looking at any part of
298  * it other than the in_use field unless they're trying to allocate it.
299  * And since we hold ReplicationSlotAllocationLock, nobody except us can
300  * be doing that. So it's safe to initialize the slot.
301  */
302  Assert(!slot->in_use);
303  Assert(slot->active_pid == 0);
304 
305  /* first initialize persistent data */
306  memset(&slot->data, 0, sizeof(ReplicationSlotPersistentData));
307  namestrcpy(&slot->data.name, name);
308  slot->data.database = db_specific ? MyDatabaseId : InvalidOid;
309  slot->data.persistency = persistency;
310  slot->data.two_phase = two_phase;
312 
313  /* and then data only present in shared memory */
314  slot->just_dirtied = false;
315  slot->dirty = false;
322 
323  /*
324  * Create the slot on disk. We haven't actually marked the slot allocated
325  * yet, so no special cleanup is required if this errors out.
326  */
327  CreateSlotOnDisk(slot);
328 
329  /*
330  * We need to briefly prevent any other backend from iterating over the
331  * slots while we flip the in_use flag. We also need to set the active
332  * flag while holding the ControlLock as otherwise a concurrent
333  * ReplicationSlotAcquire() could acquire the slot as well.
334  */
335  LWLockAcquire(ReplicationSlotControlLock, LW_EXCLUSIVE);
336 
337  slot->in_use = true;
338 
339  /* We can now mark the slot active, and that makes it our slot. */
340  SpinLockAcquire(&slot->mutex);
341  Assert(slot->active_pid == 0);
342  slot->active_pid = MyProcPid;
343  SpinLockRelease(&slot->mutex);
344  MyReplicationSlot = slot;
345 
346  LWLockRelease(ReplicationSlotControlLock);
347 
348  /*
349  * Create statistics entry for the new logical slot. We don't collect any
350  * stats for physical slots, so no need to create an entry for the same.
351  * See ReplicationSlotDropPtr for why we need to do this before releasing
352  * ReplicationSlotAllocationLock.
353  */
354  if (SlotIsLogical(slot))
356 
357  /*
358  * Now that the slot has been marked as in_use and active, it's safe to
359  * let somebody else try to allocate a slot.
360  */
361  LWLockRelease(ReplicationSlotAllocationLock);
362 
363  /* Let everybody know we've modified this slot */
365 }
int errhint(const char *fmt,...)
Definition: elog.c:1151
Oid MyDatabaseId
Definition: globals.c:89
@ LW_EXCLUSIVE
Definition: lwlock.h:104
void namestrcpy(Name name, const char *str)
Definition: name.c:233
static bool two_phase
void pgstat_create_replslot(ReplicationSlot *slot)
#define InvalidOid
Definition: postgres_ext.h:36
static void CreateSlotOnDisk(ReplicationSlot *slot)
Definition: slot.c:1494
bool ReplicationSlotValidateName(const char *name, int elevel)
Definition: slot.c:197
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
XLogRecPtr two_phase_at
Definition: slot.h:90
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:163
TransactionId effective_catalog_xmin
Definition: slot.h:144
XLogRecPtr candidate_restart_valid
Definition: slot.h:164
TransactionId effective_xmin
Definition: slot.h:143
bool just_dirtied
Definition: slot.h:129
XLogRecPtr candidate_restart_lsn
Definition: slot.h:165
TransactionId candidate_catalog_xmin
Definition: slot.h:162
bool dirty
Definition: slot.h:130
#define InvalidTransactionId
Definition: transam.h:31
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

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, MyReplicationSlot, name, ReplicationSlotPersistentData::name, NameStr, namestrcpy(), ReplicationSlotPersistentData::persistency, pgstat_create_replslot(), ReplicationSlotCtlData::replication_slots, ReplicationSlotCtl, 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().

◆ ReplicationSlotDrop()

void ReplicationSlotDrop ( const char *  name,
bool  nowait 
)

Definition at line 612 of file slot.c.

613 {
614  Assert(MyReplicationSlot == NULL);
615 
616  ReplicationSlotAcquire(name, nowait);
617 
619 }
void ReplicationSlotAcquire(const char *name, bool nowait)
Definition: slot.c:421
static void ReplicationSlotDropAcquired(void)
Definition: slot.c:625

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

Referenced by DropReplicationSlot(), and pg_drop_replication_slot().

◆ ReplicationSlotDropAtPubNode()

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

Definition at line 1602 of file subscriptioncmds.c.

1603 {
1604  StringInfoData cmd;
1605 
1606  Assert(wrconn);
1607 
1608  load_file("libpqwalreceiver", false);
1609 
1610  initStringInfo(&cmd);
1611  appendStringInfo(&cmd, "DROP_REPLICATION_SLOT %s WAIT", quote_identifier(slotname));
1612 
1613  PG_TRY();
1614  {
1616 
1617  res = walrcv_exec(wrconn, cmd.data, 0, NULL);
1618 
1619  if (res->status == WALRCV_OK_COMMAND)
1620  {
1621  /* NOTICE. Success. */
1622  ereport(NOTICE,
1623  (errmsg("dropped replication slot \"%s\" on publisher",
1624  slotname)));
1625  }
1626  else if (res->status == WALRCV_ERROR &&
1627  missing_ok &&
1628  res->sqlstate == ERRCODE_UNDEFINED_OBJECT)
1629  {
1630  /* LOG. Error, but missing_ok = true. */
1631  ereport(LOG,
1632  (errmsg("could not drop replication slot \"%s\" on publisher: %s",
1633  slotname, res->err)));
1634  }
1635  else
1636  {
1637  /* ERROR. */
1638  ereport(ERROR,
1639  (errcode(ERRCODE_CONNECTION_FAILURE),
1640  errmsg("could not drop replication slot \"%s\" on publisher: %s",
1641  slotname, res->err)));
1642  }
1643 
1645  }
1646  PG_FINALLY();
1647  {
1648  pfree(cmd.data);
1649  }
1650  PG_END_TRY();
1651 }
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:144
#define PG_END_TRY()
Definition: elog.h:324
#define PG_TRY()
Definition: elog.h:299
#define PG_FINALLY()
Definition: elog.h:316
#define NOTICE
Definition: elog.h:29
void pfree(void *pointer)
Definition: mcxt.c:1175
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:12189
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
static WalReceiverConn * wrconn
Definition: walreceiver.c:95
@ WALRCV_OK_COMMAND
Definition: walreceiver.h:201
@ WALRCV_ERROR
Definition: walreceiver.h:200
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:436
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:430

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

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

◆ ReplicationSlotIndex()

int ReplicationSlotIndex ( ReplicationSlot slot)

◆ ReplicationSlotInitialize()

void ReplicationSlotInitialize ( void  )

Definition at line 169 of file slot.c.

170 {
172 }
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:333
static void ReplicationSlotShmemExit(int code, Datum arg)
Definition: slot.c:178

References before_shmem_exit(), and ReplicationSlotShmemExit().

Referenced by BaseInit().

◆ ReplicationSlotMarkDirty()

◆ ReplicationSlotNameForTablesync()

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

Definition at line 1155 of file tablesync.c.

1157 {
1158  snprintf(syncslotname, szslot, "pg_%u_sync_%u_" UINT64_FORMAT, suboid,
1159  relid, GetSystemIdentifier());
1160 }
#define UINT64_FORMAT
Definition: c.h:495
#define snprintf
Definition: port.h:225
uint64 GetSystemIdentifier(void)
Definition: xlog.c:4203

References GetSystemIdentifier(), snprintf, and UINT64_FORMAT.

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

◆ ReplicationSlotPersist()

void ReplicationSlotPersist ( void  )

◆ ReplicationSlotRelease()

void ReplicationSlotRelease ( void  )

Definition at line 518 of file slot.c.

519 {
521 
522  Assert(slot != NULL && slot->active_pid != 0);
523 
524  if (slot->data.persistency == RS_EPHEMERAL)
525  {
526  /*
527  * Delete the slot. There is no !PANIC case where this is allowed to
528  * fail, all that may happen is an incomplete cleanup of the on-disk
529  * data.
530  */
532  }
533 
534  /*
535  * If slot needed to temporarily restrain both data and catalog xmin to
536  * create the catalog snapshot, remove that temporary constraint.
537  * Snapshots can only be exported while the initial snapshot is still
538  * acquired.
539  */
540  if (!TransactionIdIsValid(slot->data.xmin) &&
542  {
543  SpinLockAcquire(&slot->mutex);
545  SpinLockRelease(&slot->mutex);
547  }
548 
549  if (slot->data.persistency == RS_PERSISTENT)
550  {
551  /*
552  * Mark persistent slot inactive. We're not freeing it, just
553  * disconnecting, but wake up others that may be waiting for it.
554  */
555  SpinLockAcquire(&slot->mutex);
556  slot->active_pid = 0;
557  SpinLockRelease(&slot->mutex);
559  }
560 
561  MyReplicationSlot = NULL;
562 
563  /* might not have been set when we've been a plain slot */
564  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
567  LWLockRelease(ProcArrayLock);
568 }
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:58
PGPROC * MyProc
Definition: proc.c:68
PROC_HDR * ProcGlobal
Definition: proc.c:80
uint8 statusFlags
Definition: proc.h:227
int pgxactoff
Definition: proc.h:186
uint8 * statusFlags
Definition: proc.h:371
TransactionId xmin
Definition: slot.h:62
#define TransactionIdIsValid(xid)
Definition: transam.h:41

References ReplicationSlot::active_cv, ReplicationSlot::active_pid, Assert(), ConditionVariableBroadcast(), ReplicationSlot::data, ReplicationSlot::effective_xmin, InvalidTransactionId, LW_EXCLUSIVE, 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(), ReplicationSlotShmemExit(), StartLogicalReplication(), StartReplication(), and WalSndErrorCleanup().

◆ ReplicationSlotReserveWal()

void ReplicationSlotReserveWal ( void  )

Definition at line 1120 of file slot.c.

1121 {
1123 
1124  Assert(slot != NULL);
1126 
1127  /*
1128  * The replication slot mechanism is used to prevent removal of required
1129  * WAL. As there is no interlock between this routine and checkpoints, WAL
1130  * segments could concurrently be removed when a now stale return value of
1131  * ReplicationSlotsComputeRequiredLSN() is used. In the unlikely case that
1132  * this happens we'll just retry.
1133  */
1134  while (true)
1135  {
1136  XLogSegNo segno;
1137  XLogRecPtr restart_lsn;
1138 
1139  /*
1140  * For logical slots log a standby snapshot and start logical decoding
1141  * at exactly that position. That allows the slot to start up more
1142  * quickly.
1143  *
1144  * That's not needed (or indeed helpful) for physical slots as they'll
1145  * start replay at the last logged checkpoint anyway. Instead return
1146  * the location of the last redo LSN. While that slightly increases
1147  * the chance that we have to retry, it's where a base backup has to
1148  * start replay at.
1149  */
1150  if (!RecoveryInProgress() && SlotIsLogical(slot))
1151  {
1152  XLogRecPtr flushptr;
1153 
1154  /* start at current insert position */
1155  restart_lsn = GetXLogInsertRecPtr();
1156  SpinLockAcquire(&slot->mutex);
1157  slot->data.restart_lsn = restart_lsn;
1158  SpinLockRelease(&slot->mutex);
1159 
1160  /* make sure we have enough information to start */
1161  flushptr = LogStandbySnapshot();
1162 
1163  /* and make sure it's fsynced to disk */
1164  XLogFlush(flushptr);
1165  }
1166  else
1167  {
1168  restart_lsn = GetRedoRecPtr();
1169  SpinLockAcquire(&slot->mutex);
1170  slot->data.restart_lsn = restart_lsn;
1171  SpinLockRelease(&slot->mutex);
1172  }
1173 
1174  /* prevent WAL removal as fast as possible */
1176 
1177  /*
1178  * If all required WAL is still there, great, otherwise retry. The
1179  * slot should prevent further removal of WAL, unless there's a
1180  * concurrent ReplicationSlotsComputeRequiredLSN() after we've written
1181  * the new restart_lsn above, so normally we should never need to loop
1182  * more than twice.
1183  */
1185  if (XLogGetLastRemovedSegno() < segno)
1186  break;
1187  }
1188 }
XLogRecPtr LogStandbySnapshot(void)
Definition: standby.c:1222
XLogRecPtr restart_lsn
Definition: slot.h:73
bool RecoveryInProgress(void)
Definition: xlog.c:5759
XLogSegNo XLogGetLastRemovedSegno(void)
Definition: xlog.c:3482
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:5862
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:8809
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2509
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
uint64 XLogSegNo
Definition: xlogdefs.h:48

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().

◆ ReplicationSlotSave()

◆ ReplicationSlotsComputeLogicalRestartLSN()

XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN ( void  )

Definition at line 899 of file slot.c.

900 {
901  XLogRecPtr result = InvalidXLogRecPtr;
902  int i;
903 
904  if (max_replication_slots <= 0)
905  return InvalidXLogRecPtr;
906 
907  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
908 
909  for (i = 0; i < max_replication_slots; i++)
910  {
911  ReplicationSlot *s;
912  XLogRecPtr restart_lsn;
913 
915 
916  /* cannot change while ReplicationSlotCtlLock is held */
917  if (!s->in_use)
918  continue;
919 
920  /* we're only interested in logical slots */
921  if (!SlotIsLogical(s))
922  continue;
923 
924  /* read once, it's ok if it increases while we're checking */
925  SpinLockAcquire(&s->mutex);
926  restart_lsn = s->data.restart_lsn;
927  SpinLockRelease(&s->mutex);
928 
929  if (restart_lsn == InvalidXLogRecPtr)
930  continue;
931 
932  if (result == InvalidXLogRecPtr ||
933  restart_lsn < result)
934  result = restart_lsn;
935  }
936 
937  LWLockRelease(ReplicationSlotControlLock);
938 
939  return result;
940 }

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

Referenced by CheckPointLogicalRewriteHeap(), and CheckPointSnapBuild().

◆ ReplicationSlotsComputeRequiredLSN()

void ReplicationSlotsComputeRequiredLSN ( void  )

Definition at line 856 of file slot.c.

857 {
858  int i;
859  XLogRecPtr min_required = InvalidXLogRecPtr;
860 
861  Assert(ReplicationSlotCtl != NULL);
862 
863  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
864  for (i = 0; i < max_replication_slots; i++)
865  {
867  XLogRecPtr restart_lsn;
868 
869  if (!s->in_use)
870  continue;
871 
872  SpinLockAcquire(&s->mutex);
873  restart_lsn = s->data.restart_lsn;
874  SpinLockRelease(&s->mutex);
875 
876  if (restart_lsn != InvalidXLogRecPtr &&
877  (min_required == InvalidXLogRecPtr ||
878  restart_lsn < min_required))
879  min_required = restart_lsn;
880  }
881  LWLockRelease(ReplicationSlotControlLock);
882 
883  XLogSetReplicationSlotMinimumLSN(min_required);
884 }
void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
Definition: xlog.c:2395

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

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

◆ ReplicationSlotsComputeRequiredXmin()

void ReplicationSlotsComputeRequiredXmin ( bool  already_locked)

Definition at line 806 of file slot.c.

807 {
808  int i;
810  TransactionId agg_catalog_xmin = InvalidTransactionId;
811 
812  Assert(ReplicationSlotCtl != NULL);
813 
814  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
815 
816  for (i = 0; i < max_replication_slots; i++)
817  {
819  TransactionId effective_xmin;
820  TransactionId effective_catalog_xmin;
821 
822  if (!s->in_use)
823  continue;
824 
825  SpinLockAcquire(&s->mutex);
826  effective_xmin = s->effective_xmin;
827  effective_catalog_xmin = s->effective_catalog_xmin;
828  SpinLockRelease(&s->mutex);
829 
830  /* check the data xmin */
831  if (TransactionIdIsValid(effective_xmin) &&
832  (!TransactionIdIsValid(agg_xmin) ||
833  TransactionIdPrecedes(effective_xmin, agg_xmin)))
834  agg_xmin = effective_xmin;
835 
836  /* check the catalog xmin */
837  if (TransactionIdIsValid(effective_catalog_xmin) &&
838  (!TransactionIdIsValid(agg_catalog_xmin) ||
839  TransactionIdPrecedes(effective_catalog_xmin, agg_catalog_xmin)))
840  agg_catalog_xmin = effective_catalog_xmin;
841  }
842 
843  LWLockRelease(ReplicationSlotControlLock);
844 
845  ProcArraySetReplicationSlotXmin(agg_xmin, agg_catalog_xmin, already_locked);
846 }
uint32 TransactionId
Definition: c.h:598
void ProcArraySetReplicationSlotXmin(TransactionId xmin, TransactionId catalog_xmin, bool already_locked)
Definition: procarray.c:3890
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:273

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, ReplicationSlotCtl, SpinLockAcquire, SpinLockRelease, TransactionIdIsValid, and TransactionIdPrecedes().

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

◆ ReplicationSlotsCountDBSlots()

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

Definition at line 951 of file slot.c.

952 {
953  int i;
954 
955  *nslots = *nactive = 0;
956 
957  if (max_replication_slots <= 0)
958  return false;
959 
960  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
961  for (i = 0; i < max_replication_slots; i++)
962  {
963  ReplicationSlot *s;
964 
966 
967  /* cannot change while ReplicationSlotCtlLock is held */
968  if (!s->in_use)
969  continue;
970 
971  /* only logical slots are database specific, skip */
972  if (!SlotIsLogical(s))
973  continue;
974 
975  /* not our database, skip */
976  if (s->data.database != dboid)
977  continue;
978 
979  /* count slots with spinlock held */
980  SpinLockAcquire(&s->mutex);
981  (*nslots)++;
982  if (s->active_pid != 0)
983  (*nactive)++;
984  SpinLockRelease(&s->mutex);
985  }
986  LWLockRelease(ReplicationSlotControlLock);
987 
988  if (*nslots > 0)
989  return true;
990  return false;
991 }

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

Referenced by dropdb().

◆ ReplicationSlotsDropDBSlots()

void ReplicationSlotsDropDBSlots ( Oid  dboid)

Definition at line 1007 of file slot.c.

1008 {
1009  int i;
1010 
1011  if (max_replication_slots <= 0)
1012  return;
1013 
1014 restart:
1015  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1016  for (i = 0; i < max_replication_slots; i++)
1017  {
1018  ReplicationSlot *s;
1019  char *slotname;
1020  int active_pid;
1021 
1023 
1024  /* cannot change while ReplicationSlotCtlLock is held */
1025  if (!s->in_use)
1026  continue;
1027 
1028  /* only logical slots are database specific, skip */
1029  if (!SlotIsLogical(s))
1030  continue;
1031 
1032  /* not our database, skip */
1033  if (s->data.database != dboid)
1034  continue;
1035 
1036  /* acquire slot, so ReplicationSlotDropAcquired can be reused */
1037  SpinLockAcquire(&s->mutex);
1038  /* can't change while ReplicationSlotControlLock is held */
1039  slotname = NameStr(s->data.name);
1040  active_pid = s->active_pid;
1041  if (active_pid == 0)
1042  {
1043  MyReplicationSlot = s;
1044  s->active_pid = MyProcPid;
1045  }
1046  SpinLockRelease(&s->mutex);
1047 
1048  /*
1049  * Even though we hold an exclusive lock on the database object a
1050  * logical slot for that DB can still be active, e.g. if it's
1051  * concurrently being dropped by a backend connected to another DB.
1052  *
1053  * That's fairly unlikely in practice, so we'll just bail out.
1054  */
1055  if (active_pid)
1056  ereport(ERROR,
1057  (errcode(ERRCODE_OBJECT_IN_USE),
1058  errmsg("replication slot \"%s\" is active for PID %d",
1059  slotname, active_pid)));
1060 
1061  /*
1062  * To avoid duplicating ReplicationSlotDropAcquired() and to avoid
1063  * holding ReplicationSlotControlLock over filesystem operations,
1064  * release ReplicationSlotControlLock and use
1065  * ReplicationSlotDropAcquired.
1066  *
1067  * As that means the set of slots could change, restart scan from the
1068  * beginning each time we release the lock.
1069  */
1070  LWLockRelease(ReplicationSlotControlLock);
1072  goto restart;
1073  }
1074  LWLockRelease(ReplicationSlotControlLock);
1075 }

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, MyReplicationSlot, ReplicationSlotPersistentData::name, NameStr, ReplicationSlotCtlData::replication_slots, ReplicationSlotCtl, ReplicationSlotDropAcquired(), SlotIsLogical, SpinLockAcquire, and SpinLockRelease.

Referenced by dbase_redo(), and dropdb().

◆ ReplicationSlotsShmemInit()

void ReplicationSlotsShmemInit ( void  )

Definition at line 134 of file slot.c.

135 {
136  bool found;
137 
138  if (max_replication_slots == 0)
139  return;
140 
142  ShmemInitStruct("ReplicationSlot Ctl", ReplicationSlotsShmemSize(),
143  &found);
144 
145  if (!found)
146  {
147  int i;
148 
149  /* First time through, so initialize */
151 
152  for (i = 0; i < max_replication_slots; i++)
153  {
155 
156  /* everything else is zeroed by the memset above */
157  SpinLockInit(&slot->mutex);
161  }
162  }
163 }
#define MemSet(start, val, len)
Definition: c.h:1019
void ConditionVariableInit(ConditionVariable *cv)
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:734
@ LWTRANCHE_REPLICATION_SLOT_IO
Definition: lwlock.h:178
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:116
#define SpinLockInit(lock)
Definition: spin.h:60
LWLock io_in_progress_lock
Definition: slot.h:150

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, ReplicationSlotCtl, ReplicationSlotsShmemSize(), ShmemInitStruct(), and SpinLockInit.

Referenced by CreateSharedMemoryAndSemaphores().

◆ ReplicationSlotsShmemSize()

Size ReplicationSlotsShmemSize ( void  )

Definition at line 116 of file slot.c.

117 {
118  Size size = 0;
119 
120  if (max_replication_slots == 0)
121  return size;
122 
123  size = offsetof(ReplicationSlotCtlData, replication_slots);
124  size = add_size(size,
126 
127  return size;
128 }
#define offsetof(type, field)
Definition: c.h:738
size_t Size
Definition: c.h:551
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519

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

Referenced by CalculateShmemSize(), and ReplicationSlotsShmemInit().

◆ ReplicationSlotValidateName()

bool ReplicationSlotValidateName ( const char *  name,
int  elevel 
)

Definition at line 197 of file slot.c.

198 {
199  const char *cp;
200 
201  if (strlen(name) == 0)
202  {
203  ereport(elevel,
204  (errcode(ERRCODE_INVALID_NAME),
205  errmsg("replication slot name \"%s\" is too short",
206  name)));
207  return false;
208  }
209 
210  if (strlen(name) >= NAMEDATALEN)
211  {
212  ereport(elevel,
213  (errcode(ERRCODE_NAME_TOO_LONG),
214  errmsg("replication slot name \"%s\" is too long",
215  name)));
216  return false;
217  }
218 
219  for (cp = name; *cp; cp++)
220  {
221  if (!((*cp >= 'a' && *cp <= 'z')
222  || (*cp >= '0' && *cp <= '9')
223  || (*cp == '_')))
224  {
225  ereport(elevel,
226  (errcode(ERRCODE_INVALID_NAME),
227  errmsg("replication slot name \"%s\" contains invalid character",
228  name),
229  errhint("Replication slot names may only contain lower case letters, numbers, and the underscore character.")));
230  return false;
231  }
232  }
233  return true;
234 }
#define NAMEDATALEN

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

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

◆ SearchNamedReplicationSlot()

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

Definition at line 373 of file slot.c.

374 {
375  int i;
376  ReplicationSlot *slot = NULL;
377 
378  if (need_lock)
379  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
380 
381  for (i = 0; i < max_replication_slots; i++)
382  {
384 
385  if (s->in_use && strcmp(name, NameStr(s->data.name)) == 0)
386  {
387  slot = s;
388  break;
389  }
390  }
391 
392  if (need_lock)
393  LWLockRelease(ReplicationSlotControlLock);
394 
395  return slot;
396 }

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

Referenced by get_replslot_index(), pg_ls_replslotdir(), pgstat_reset_replslot(), ReadReplicationSlot(), and ReplicationSlotAcquire().

◆ StartupReplicationSlots()

void StartupReplicationSlots ( void  )

Definition at line 1434 of file slot.c.

1435 {
1436  DIR *replication_dir;
1437  struct dirent *replication_de;
1438 
1439  elog(DEBUG1, "starting up replication slots");
1440 
1441  /* restore all slots by iterating over all on-disk entries */
1442  replication_dir = AllocateDir("pg_replslot");
1443  while ((replication_de = ReadDir(replication_dir, "pg_replslot")) != NULL)
1444  {
1445  struct stat statbuf;
1446  char path[MAXPGPATH + 12];
1447 
1448  if (strcmp(replication_de->d_name, ".") == 0 ||
1449  strcmp(replication_de->d_name, "..") == 0)
1450  continue;
1451 
1452  snprintf(path, sizeof(path), "pg_replslot/%s", replication_de->d_name);
1453 
1454  /* we're only creating directories here, skip if it's not our's */
1455  if (lstat(path, &statbuf) == 0 && !S_ISDIR(statbuf.st_mode))
1456  continue;
1457 
1458  /* we crashed while a slot was being setup or deleted, clean up */
1459  if (pg_str_endswith(replication_de->d_name, ".tmp"))
1460  {
1461  if (!rmtree(path, true))
1462  {
1463  ereport(WARNING,
1464  (errmsg("could not remove directory \"%s\"",
1465  path)));
1466  continue;
1467  }
1468  fsync_fname("pg_replslot", true);
1469  continue;
1470  }
1471 
1472  /* looks like a slot in a normal state, restore */
1473  RestoreSlotFromDisk(replication_de->d_name);
1474  }
1475  FreeDir(replication_dir);
1476 
1477  /* currently no slots exist, we're done. */
1478  if (max_replication_slots <= 0)
1479  return;
1480 
1481  /* Now that we have recovered all the data, compute replication xmin */
1484 }
#define WARNING
Definition: elog.h:30
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2725
int FreeDir(DIR *dir)
Definition: fd.c:2777
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:673
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2659
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:42
static void RestoreSlotFromDisk(const char *name)
Definition: slot.c:1709
bool pg_str_endswith(const char *str, const char *end)
Definition: string.c:31
Definition: dirent.c:26
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15
#define lstat(path, sb)
Definition: win32_port.h:284
#define S_ISDIR(m)
Definition: win32_port.h:324

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().

Variable Documentation

◆ max_replication_slots

◆ MyReplicationSlot

◆ ReplicationSlotCtl