PostgreSQL Source Code  git master
slot.c File Reference
#include "postgres.h"
#include <unistd.h>
#include <sys/stat.h>
#include "access/transam.h"
#include "access/xlog_internal.h"
#include "common/file_utils.h"
#include "common/string.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/slot.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "utils/builtins.h"
Include dependency graph for slot.c:

Go to the source code of this file.

Data Structures

struct  ReplicationSlotOnDisk
 

Macros

#define ReplicationSlotOnDiskConstantSize    offsetof(ReplicationSlotOnDisk, slotdata)
 
#define ReplicationSlotOnDiskNotChecksummedSize    offsetof(ReplicationSlotOnDisk, version)
 
#define ReplicationSlotOnDiskChecksummedSize    sizeof(ReplicationSlotOnDisk) - ReplicationSlotOnDiskNotChecksummedSize
 
#define ReplicationSlotOnDiskV2Size    sizeof(ReplicationSlotOnDisk) - ReplicationSlotOnDiskConstantSize
 
#define SLOT_MAGIC   0x1051CA1 /* format identifier */
 
#define SLOT_VERSION   2 /* version for new files */
 

Typedefs

typedef struct ReplicationSlotOnDisk ReplicationSlotOnDisk
 

Functions

static void ReplicationSlotShmemExit (int code, Datum arg)
 
static void ReplicationSlotDropAcquired (void)
 
static void ReplicationSlotDropPtr (ReplicationSlot *slot)
 
static void RestoreSlotFromDisk (const char *name)
 
static void CreateSlotOnDisk (ReplicationSlot *slot)
 
static void SaveSlotToPath (ReplicationSlot *slot, const char *dir, int elevel)
 
Size ReplicationSlotsShmemSize (void)
 
void ReplicationSlotsShmemInit (void)
 
void ReplicationSlotInitialize (void)
 
bool ReplicationSlotValidateName (const char *name, int elevel)
 
void ReplicationSlotCreate (const char *name, bool db_specific, ReplicationSlotPersistency persistency, bool two_phase)
 
ReplicationSlotSearchNamedReplicationSlot (const char *name, bool need_lock)
 
int ReplicationSlotIndex (ReplicationSlot *slot)
 
bool ReplicationSlotName (int index, Name name)
 
void ReplicationSlotAcquire (const char *name, bool nowait)
 
void ReplicationSlotRelease (void)
 
void ReplicationSlotCleanup (void)
 
void ReplicationSlotDrop (const char *name, bool nowait)
 
void ReplicationSlotSave (void)
 
void ReplicationSlotMarkDirty (void)
 
void ReplicationSlotPersist (void)
 
void ReplicationSlotsComputeRequiredXmin (bool already_locked)
 
void ReplicationSlotsComputeRequiredLSN (void)
 
XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN (void)
 
bool ReplicationSlotsCountDBSlots (Oid dboid, int *nslots, int *nactive)
 
void ReplicationSlotsDropDBSlots (Oid dboid)
 
void CheckSlotRequirements (void)
 
void CheckSlotPermissions (void)
 
void ReplicationSlotReserveWal (void)
 
static bool InvalidatePossiblyObsoleteSlot (ReplicationSlot *s, XLogRecPtr oldestLSN, bool *invalidated)
 
bool InvalidateObsoleteReplicationSlots (XLogSegNo oldestSegno)
 
void CheckPointReplicationSlots (void)
 
void StartupReplicationSlots (void)
 

Variables

ReplicationSlotCtlDataReplicationSlotCtl = NULL
 
ReplicationSlotMyReplicationSlot = NULL
 
int max_replication_slots = 10
 

Macro Definition Documentation

◆ ReplicationSlotOnDiskChecksummedSize

#define ReplicationSlotOnDiskChecksummedSize    sizeof(ReplicationSlotOnDisk) - ReplicationSlotOnDiskNotChecksummedSize

Definition at line 85 of file slot.c.

◆ ReplicationSlotOnDiskConstantSize

#define ReplicationSlotOnDiskConstantSize    offsetof(ReplicationSlotOnDisk, slotdata)

Definition at line 79 of file slot.c.

◆ ReplicationSlotOnDiskNotChecksummedSize

#define ReplicationSlotOnDiskNotChecksummedSize    offsetof(ReplicationSlotOnDisk, version)

Definition at line 82 of file slot.c.

◆ ReplicationSlotOnDiskV2Size

#define ReplicationSlotOnDiskV2Size    sizeof(ReplicationSlotOnDisk) - ReplicationSlotOnDiskConstantSize

Definition at line 88 of file slot.c.

◆ SLOT_MAGIC

#define SLOT_MAGIC   0x1051CA1 /* format identifier */

Definition at line 91 of file slot.c.

◆ SLOT_VERSION

#define SLOT_VERSION   2 /* version for new files */

Definition at line 92 of file slot.c.

Typedef Documentation

◆ ReplicationSlotOnDisk

Function Documentation

◆ CheckPointReplicationSlots()

void CheckPointReplicationSlots ( void  )

Definition at line 1442 of file slot.c.

1443 {
1444  int i;
1445 
1446  elog(DEBUG1, "performing replication slot checkpoint");
1447 
1448  /*
1449  * Prevent any slot from being created/dropped while we're active. As we
1450  * explicitly do *not* want to block iterating over replication_slots or
1451  * acquiring a slot we cannot take the control lock - but that's OK,
1452  * because holding ReplicationSlotAllocationLock is strictly stronger, and
1453  * enough to guarantee that nobody can change the in_use bits on us.
1454  */
1455  LWLockAcquire(ReplicationSlotAllocationLock, LW_SHARED);
1456 
1457  for (i = 0; i < max_replication_slots; i++)
1458  {
1460  char path[MAXPGPATH];
1461 
1462  if (!s->in_use)
1463  continue;
1464 
1465  /* save the slot to disk, locking is handled in SaveSlotToPath() */
1466  sprintf(path, "pg_replslot/%s", NameStr(s->data.name));
1467  SaveSlotToPath(s, path, LOG);
1468  }
1469  LWLockRelease(ReplicationSlotAllocationLock);
1470 }
#define NameStr(name)
Definition: c.h:682
#define LOG
Definition: elog.h:27
#define DEBUG1
Definition: elog.h:26
int i
Definition: isn.c:73
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1194
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1802
@ LW_SHARED
Definition: lwlock.h:113
#define MAXPGPATH
#define sprintf
Definition: port.h:240
static void SaveSlotToPath(ReplicationSlot *slot, const char *dir, int elevel)
Definition: slot.c:1599
int max_replication_slots
Definition: slot.c:101
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:95
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 1141 of file slot.c.

1142 {
1143  if (!superuser() && !has_rolreplication(GetUserId()))
1144  ereport(ERROR,
1145  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1146  errmsg("must be superuser or replication role to use replication slots")));
1147 }
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
Oid GetUserId(void)
Definition: miscinit.c:497
bool has_rolreplication(Oid roleid)
Definition: miscinit.c:694
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 1119 of file slot.c.

1120 {
1121  /*
1122  * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
1123  * needs the same check.
1124  */
1125 
1126  if (max_replication_slots == 0)
1127  ereport(ERROR,
1128  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1129  errmsg("replication slots can only be used if max_replication_slots > 0")));
1130 
1132  ereport(ERROR,
1133  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1134  errmsg("replication slots can only be used if wal_level >= replica")));
1135 }
int wal_level
Definition: xlog.c:134
@ 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().

◆ CreateSlotOnDisk()

static void CreateSlotOnDisk ( ReplicationSlot slot)
static

Definition at line 1538 of file slot.c.

1539 {
1540  char tmppath[MAXPGPATH];
1541  char path[MAXPGPATH];
1542  struct stat st;
1543 
1544  /*
1545  * No need to take out the io_in_progress_lock, nobody else can see this
1546  * slot yet, so nobody else will write. We're reusing SaveSlotToPath which
1547  * takes out the lock, if we'd take the lock here, we'd deadlock.
1548  */
1549 
1550  sprintf(path, "pg_replslot/%s", NameStr(slot->data.name));
1551  sprintf(tmppath, "pg_replslot/%s.tmp", NameStr(slot->data.name));
1552 
1553  /*
1554  * It's just barely possible that some previous effort to create or drop a
1555  * slot with this name left a temp directory lying around. If that seems
1556  * to be the case, try to remove it. If the rmtree() fails, we'll error
1557  * out at the MakePGDirectory() below, so we don't bother checking
1558  * success.
1559  */
1560  if (stat(tmppath, &st) == 0 && S_ISDIR(st.st_mode))
1561  rmtree(tmppath, true);
1562 
1563  /* Create and fsync the temporary slot directory. */
1564  if (MakePGDirectory(tmppath) < 0)
1565  ereport(ERROR,
1567  errmsg("could not create directory \"%s\": %m",
1568  tmppath)));
1569  fsync_fname(tmppath, true);
1570 
1571  /* Write the actual state file. */
1572  slot->dirty = true; /* signal that we really need to write */
1573  SaveSlotToPath(slot, tmppath, ERROR);
1574 
1575  /* Rename the directory into place. */
1576  if (rename(tmppath, path) != 0)
1577  ereport(ERROR,
1579  errmsg("could not rename file \"%s\" to \"%s\": %m",
1580  tmppath, path)));
1581 
1582  /*
1583  * If we'd now fail - really unlikely - we wouldn't know whether this slot
1584  * would persist after an OS crash or not - so, force a restart. The
1585  * restart would try to fsync this again till it works.
1586  */
1588 
1589  fsync_fname(path, true);
1590  fsync_fname("pg_replslot", true);
1591 
1592  END_CRIT_SECTION();
1593 }
int errcode_for_file_access(void)
Definition: elog.c:718
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3713
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:662
#define START_CRIT_SECTION()
Definition: miscadmin.h:148
#define END_CRIT_SECTION()
Definition: miscadmin.h:150
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:42
bool dirty
Definition: slot.h:130
#define stat
Definition: win32_port.h:286
#define S_ISDIR(m)
Definition: win32_port.h:327

References ReplicationSlot::data, ReplicationSlot::dirty, END_CRIT_SECTION, ereport, errcode_for_file_access(), errmsg(), ERROR, fsync_fname(), MakePGDirectory(), MAXPGPATH, ReplicationSlotPersistentData::name, NameStr, rmtree(), S_ISDIR, SaveSlotToPath(), sprintf, stat::st_mode, START_CRIT_SECTION, and stat.

Referenced by ReplicationSlotCreate().

◆ InvalidateObsoleteReplicationSlots()

bool InvalidateObsoleteReplicationSlots ( XLogSegNo  oldestSegno)

Definition at line 1399 of file slot.c.

1400 {
1401  XLogRecPtr oldestLSN;
1402  bool invalidated = false;
1403 
1404  XLogSegNoOffsetToRecPtr(oldestSegno, 0, wal_segment_size, oldestLSN);
1405 
1406 restart:
1407  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1408  for (int i = 0; i < max_replication_slots; i++)
1409  {
1411 
1412  if (!s->in_use)
1413  continue;
1414 
1415  if (InvalidatePossiblyObsoleteSlot(s, oldestLSN, &invalidated))
1416  {
1417  /* if the lock was released, start from scratch */
1418  goto restart;
1419  }
1420  }
1421  LWLockRelease(ReplicationSlotControlLock);
1422 
1423  /*
1424  * If any slots have been invalidated, recalculate the resource limits.
1425  */
1426  if (invalidated)
1427  {
1430  }
1431 
1432  return invalidated;
1433 }
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:835
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:892
static bool InvalidatePossiblyObsoleteSlot(ReplicationSlot *s, XLogRecPtr oldestLSN, bool *invalidated)
Definition: slot.c:1239
int wal_segment_size
Definition: xlog.c:146
#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().

◆ InvalidatePossiblyObsoleteSlot()

static bool InvalidatePossiblyObsoleteSlot ( ReplicationSlot s,
XLogRecPtr  oldestLSN,
bool invalidated 
)
static

Definition at line 1239 of file slot.c.

1241 {
1242  int last_signaled_pid = 0;
1243  bool released_lock = false;
1244 
1245  for (;;)
1246  {
1247  XLogRecPtr restart_lsn;
1248  NameData slotname;
1249  int active_pid = 0;
1250 
1251  Assert(LWLockHeldByMeInMode(ReplicationSlotControlLock, LW_SHARED));
1252 
1253  if (!s->in_use)
1254  {
1255  if (released_lock)
1256  LWLockRelease(ReplicationSlotControlLock);
1257  break;
1258  }
1259 
1260  /*
1261  * Check if the slot needs to be invalidated. If it needs to be
1262  * invalidated, and is not currently acquired, acquire it and mark it
1263  * as having been invalidated. We do this with the spinlock held to
1264  * avoid race conditions -- for example the restart_lsn could move
1265  * forward, or the slot could be dropped.
1266  */
1267  SpinLockAcquire(&s->mutex);
1268 
1269  restart_lsn = s->data.restart_lsn;
1270 
1271  /*
1272  * If the slot is already invalid or is fresh enough, we don't need to
1273  * do anything.
1274  */
1275  if (XLogRecPtrIsInvalid(restart_lsn) || restart_lsn >= oldestLSN)
1276  {
1277  SpinLockRelease(&s->mutex);
1278  if (released_lock)
1279  LWLockRelease(ReplicationSlotControlLock);
1280  break;
1281  }
1282 
1283  slotname = s->data.name;
1284  active_pid = s->active_pid;
1285 
1286  /*
1287  * If the slot can be acquired, do so and mark it invalidated
1288  * immediately. Otherwise we'll signal the owning process, below, and
1289  * retry.
1290  */
1291  if (active_pid == 0)
1292  {
1293  MyReplicationSlot = s;
1294  s->active_pid = MyProcPid;
1295  s->data.invalidated_at = restart_lsn;
1297 
1298  /* Let caller know */
1299  *invalidated = true;
1300  }
1301 
1302  SpinLockRelease(&s->mutex);
1303 
1304  if (active_pid != 0)
1305  {
1306  /*
1307  * Prepare the sleep on the slot's condition variable before
1308  * releasing the lock, to close a possible race condition if the
1309  * slot is released before the sleep below.
1310  */
1312 
1313  LWLockRelease(ReplicationSlotControlLock);
1314  released_lock = true;
1315 
1316  /*
1317  * Signal to terminate the process that owns the slot, if we
1318  * haven't already signalled it. (Avoidance of repeated
1319  * signalling is the only reason for there to be a loop in this
1320  * routine; otherwise we could rely on caller's restart loop.)
1321  *
1322  * There is the race condition that other process may own the slot
1323  * after its current owner process is terminated and before this
1324  * process owns it. To handle that, we signal only if the PID of
1325  * the owning process has changed from the previous time. (This
1326  * logic assumes that the same PID is not reused very quickly.)
1327  */
1328  if (last_signaled_pid != active_pid)
1329  {
1330  ereport(LOG,
1331  errmsg("terminating process %d to release replication slot \"%s\"",
1332  active_pid, NameStr(slotname)),
1333  errdetail("The slot's restart_lsn %X/%X exceeds the limit by %llu bytes.",
1334  LSN_FORMAT_ARGS(restart_lsn),
1335  (unsigned long long) (oldestLSN - restart_lsn)),
1336  errhint("You might need to increase max_slot_wal_keep_size."));
1337 
1338  (void) kill(active_pid, SIGTERM);
1339  last_signaled_pid = active_pid;
1340  }
1341 
1342  /* Wait until the slot is released. */
1345 
1346  /*
1347  * Re-acquire lock and start over; we expect to invalidate the
1348  * slot next time (unless another process acquires the slot in the
1349  * meantime).
1350  */
1351  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1352  continue;
1353  }
1354  else
1355  {
1356  /*
1357  * We hold the slot now and have already invalidated it; flush it
1358  * to ensure that state persists.
1359  *
1360  * Don't want to hold ReplicationSlotControlLock across file
1361  * system operations, so release it now but be sure to tell caller
1362  * to restart from scratch.
1363  */
1364  LWLockRelease(ReplicationSlotControlLock);
1365  released_lock = true;
1366 
1367  /* Make sure the invalidated state persists across server restart */
1371 
1372  ereport(LOG,
1373  errmsg("invalidating obsolete replication slot \"%s\"",
1374  NameStr(slotname)),
1375  errdetail("The slot's restart_lsn %X/%X exceeds the limit by %llu bytes.",
1376  LSN_FORMAT_ARGS(restart_lsn),
1377  (unsigned long long) (oldestLSN - restart_lsn)),
1378  errhint("You might need to increase max_slot_wal_keep_size."));
1379 
1380  /* done with this slot for now */
1381  break;
1382  }
1383  }
1384 
1385  Assert(released_lock == !LWLockHeldByMe(ReplicationSlotControlLock));
1386 
1387  return released_lock;
1388 }
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
int errdetail(const char *fmt,...)
Definition: elog.c:1039
int errhint(const char *fmt,...)
Definition: elog.c:1153
int MyProcPid
Definition: globals.c:44
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1918
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1962
void ReplicationSlotMarkDirty(void)
Definition: slot.c:796
ReplicationSlot * MyReplicationSlot
Definition: slot.c:98
void ReplicationSlotSave(void)
Definition: slot.c:778
void ReplicationSlotRelease(void)
Definition: slot.c:547
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogRecPtr restart_lsn
Definition: slot.h:73
XLogRecPtr invalidated_at
Definition: slot.h:76
slock_t mutex
Definition: slot.h:120
pid_t active_pid
Definition: slot.h:126
ConditionVariable active_cv
Definition: slot.h:153
Definition: c.h:677
@ WAIT_EVENT_REPLICATION_SLOT_DROP
Definition: wait_event.h:125
#define kill(pid, sig)
Definition: win32_port.h:482
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

References ReplicationSlot::active_cv, ReplicationSlot::active_pid, Assert(), ConditionVariablePrepareToSleep(), ConditionVariableSleep(), ReplicationSlot::data, ereport, errdetail(), errhint(), errmsg(), ReplicationSlot::in_use, ReplicationSlotPersistentData::invalidated_at, InvalidXLogRecPtr, kill, LOG, LSN_FORMAT_ARGS, LW_SHARED, LWLockAcquire(), LWLockHeldByMe(), LWLockHeldByMeInMode(), LWLockRelease(), ReplicationSlot::mutex, MyProcPid, MyReplicationSlot, ReplicationSlotPersistentData::name, NameStr, ReplicationSlotMarkDirty(), ReplicationSlotRelease(), ReplicationSlotSave(), ReplicationSlotPersistentData::restart_lsn, SpinLockAcquire, SpinLockRelease, WAIT_EVENT_REPLICATION_SLOT_DROP, and XLogRecPtrIsInvalid.

Referenced by InvalidateObsoleteReplicationSlots().

◆ ReplicationSlotAcquire()

void ReplicationSlotAcquire ( const char *  name,
bool  nowait 
)

Definition at line 450 of file slot.c.

451 {
452  ReplicationSlot *s;
453  int active_pid;
454 
455  Assert(name != NULL);
456 
457 retry:
458  Assert(MyReplicationSlot == NULL);
459 
460  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
461 
462  /*
463  * Search for the slot with the specified name if the slot to acquire is
464  * not given. If the slot is not found, we either return -1 or error out.
465  */
466  s = SearchNamedReplicationSlot(name, false);
467  if (s == NULL || !s->in_use)
468  {
469  LWLockRelease(ReplicationSlotControlLock);
470 
471  ereport(ERROR,
472  (errcode(ERRCODE_UNDEFINED_OBJECT),
473  errmsg("replication slot \"%s\" does not exist",
474  name)));
475  }
476 
477  /*
478  * This is the slot we want; check if it's active under some other
479  * process. In single user mode, we don't need this check.
480  */
481  if (IsUnderPostmaster)
482  {
483  /*
484  * Get ready to sleep on the slot in case it is active. (We may end
485  * up not sleeping, but we don't want to do this while holding the
486  * spinlock.)
487  */
488  if (!nowait)
490 
491  SpinLockAcquire(&s->mutex);
492  if (s->active_pid == 0)
493  s->active_pid = MyProcPid;
494  active_pid = s->active_pid;
495  SpinLockRelease(&s->mutex);
496  }
497  else
498  active_pid = MyProcPid;
499  LWLockRelease(ReplicationSlotControlLock);
500 
501  /*
502  * If we found the slot but it's already active in another process, we
503  * wait until the owning process signals us that it's been released, or
504  * error out.
505  */
506  if (active_pid != MyProcPid)
507  {
508  if (!nowait)
509  {
510  /* Wait here until we get signaled, and then restart */
514  goto retry;
515  }
516 
517  ereport(ERROR,
518  (errcode(ERRCODE_OBJECT_IN_USE),
519  errmsg("replication slot \"%s\" is active for PID %d",
520  NameStr(s->data.name), active_pid)));
521  }
522  else if (!nowait)
523  ConditionVariableCancelSleep(); /* no sleep needed after all */
524 
525  /* Let everybody know we've modified this slot */
527 
528  /* We made this slot active, so it's ours now. */
529  MyReplicationSlot = s;
530 
531  /*
532  * The call to pgstat_acquire_replslot() protects against stats for a
533  * different slot, from before a restart or such, being present during
534  * pgstat_report_replslot().
535  */
536  if (SlotIsLogical(s))
538 }
void ConditionVariableBroadcast(ConditionVariable *cv)
void ConditionVariableCancelSleep(void)
const char * name
Definition: encode.c:561
bool IsUnderPostmaster
Definition: globals.c:113
void pgstat_acquire_replslot(ReplicationSlot *slot)
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition: slot.c:374
#define SlotIsLogical(slot)
Definition: slot.h:169

References ReplicationSlot::active_cv, ReplicationSlot::active_pid, Assert(), 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 603 of file slot.c.

604 {
605  int i;
606 
607  Assert(MyReplicationSlot == NULL);
608 
609 restart:
610  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
611  for (i = 0; i < max_replication_slots; i++)
612  {
614 
615  if (!s->in_use)
616  continue;
617 
618  SpinLockAcquire(&s->mutex);
619  if (s->active_pid == MyProcPid)
620  {
622  SpinLockRelease(&s->mutex);
623  LWLockRelease(ReplicationSlotControlLock); /* avoid deadlock */
624 
626 
628  goto restart;
629  }
630  else
631  SpinLockRelease(&s->mutex);
632  }
633 
634  LWLockRelease(ReplicationSlotControlLock);
635 }
static void ReplicationSlotDropPtr(ReplicationSlot *slot)
Definition: slot.c:671
@ RS_TEMPORARY
Definition: slot.h:37
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  persistency,
bool  two_phase 
)

Definition at line 252 of file slot.c.

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

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 641 of file slot.c.

642 {
643  Assert(MyReplicationSlot == NULL);
644 
645  ReplicationSlotAcquire(name, nowait);
646 
648 }
void ReplicationSlotAcquire(const char *name, bool nowait)
Definition: slot.c:450
static void ReplicationSlotDropAcquired(void)
Definition: slot.c:654

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

Referenced by DropReplicationSlot(), and pg_drop_replication_slot().

◆ ReplicationSlotDropAcquired()

static void ReplicationSlotDropAcquired ( void  )
static

Definition at line 654 of file slot.c.

655 {
657 
658  Assert(MyReplicationSlot != NULL);
659 
660  /* slot isn't acquired anymore */
661  MyReplicationSlot = NULL;
662 
664 }

References Assert(), MyReplicationSlot, and ReplicationSlotDropPtr().

Referenced by ReplicationSlotDrop(), ReplicationSlotRelease(), and ReplicationSlotsDropDBSlots().

◆ ReplicationSlotDropPtr()

static void ReplicationSlotDropPtr ( ReplicationSlot slot)
static

Definition at line 671 of file slot.c.

672 {
673  char path[MAXPGPATH];
674  char tmppath[MAXPGPATH];
675 
676  /*
677  * If some other backend ran this code concurrently with us, we might try
678  * to delete a slot with a certain name while someone else was trying to
679  * create a slot with the same name.
680  */
681  LWLockAcquire(ReplicationSlotAllocationLock, LW_EXCLUSIVE);
682 
683  /* Generate pathnames. */
684  sprintf(path, "pg_replslot/%s", NameStr(slot->data.name));
685  sprintf(tmppath, "pg_replslot/%s.tmp", NameStr(slot->data.name));
686 
687  /*
688  * Rename the slot directory on disk, so that we'll no longer recognize
689  * this as a valid slot. Note that if this fails, we've got to mark the
690  * slot inactive before bailing out. If we're dropping an ephemeral or a
691  * temporary slot, we better never fail hard as the caller won't expect
692  * the slot to survive and this might get called during error handling.
693  */
694  if (rename(path, tmppath) == 0)
695  {
696  /*
697  * We need to fsync() the directory we just renamed and its parent to
698  * make sure that our changes are on disk in a crash-safe fashion. If
699  * fsync() fails, we can't be sure whether the changes are on disk or
700  * not. For now, we handle that by panicking;
701  * StartupReplicationSlots() will try to straighten it out after
702  * restart.
703  */
705  fsync_fname(tmppath, true);
706  fsync_fname("pg_replslot", true);
708  }
709  else
710  {
711  bool fail_softly = slot->data.persistency != RS_PERSISTENT;
712 
713  SpinLockAcquire(&slot->mutex);
714  slot->active_pid = 0;
715  SpinLockRelease(&slot->mutex);
716 
717  /* wake up anyone waiting on this slot */
719 
720  ereport(fail_softly ? WARNING : ERROR,
722  errmsg("could not rename file \"%s\" to \"%s\": %m",
723  path, tmppath)));
724  }
725 
726  /*
727  * The slot is definitely gone. Lock out concurrent scans of the array
728  * long enough to kill it. It's OK to clear the active PID here without
729  * grabbing the mutex because nobody else can be scanning the array here,
730  * and nobody can be attached to this slot and thus access it without
731  * scanning the array.
732  *
733  * Also wake up processes waiting for it.
734  */
735  LWLockAcquire(ReplicationSlotControlLock, LW_EXCLUSIVE);
736  slot->active_pid = 0;
737  slot->in_use = false;
738  LWLockRelease(ReplicationSlotControlLock);
740 
741  /*
742  * Slot is dead and doesn't prevent resource removal anymore, recompute
743  * limits.
744  */
747 
748  /*
749  * If removing the directory fails, the worst thing that will happen is
750  * that the user won't be able to create a new slot with the same name
751  * until the next server restart. We warn about it, but that's all.
752  */
753  if (!rmtree(tmppath, true))
755  (errmsg("could not remove directory \"%s\"", tmppath)));
756 
757  /*
758  * Drop the statistics entry for the replication slot. Do this while
759  * holding ReplicationSlotAllocationLock so that we don't drop a
760  * statistics entry for another slot with the same name just created in
761  * another session.
762  */
763  if (SlotIsLogical(slot))
764  pgstat_drop_replslot(slot);
765 
766  /*
767  * We release this at the very end, so that nobody starts trying to create
768  * a slot while we're still cleaning up the detritus of the old one.
769  */
770  LWLockRelease(ReplicationSlotAllocationLock);
771 }
#define WARNING
Definition: elog.h:32
void pgstat_drop_replslot(ReplicationSlot *slot)
@ RS_PERSISTENT
Definition: slot.h:35

References ReplicationSlot::active_cv, ReplicationSlot::active_pid, ConditionVariableBroadcast(), ReplicationSlot::data, END_CRIT_SECTION, ereport, errcode_for_file_access(), errmsg(), ERROR, fsync_fname(), ReplicationSlot::in_use, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MAXPGPATH, ReplicationSlot::mutex, ReplicationSlotPersistentData::name, NameStr, ReplicationSlotPersistentData::persistency, pgstat_drop_replslot(), ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), rmtree(), RS_PERSISTENT, SlotIsLogical, SpinLockAcquire, SpinLockRelease, sprintf, START_CRIT_SECTION, and WARNING.

Referenced by ReplicationSlotCleanup(), and ReplicationSlotDropAcquired().

◆ ReplicationSlotIndex()

int ReplicationSlotIndex ( ReplicationSlot slot)

◆ ReplicationSlotInitialize()

void ReplicationSlotInitialize ( void  )

Definition at line 170 of file slot.c.

171 {
173 }
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:179

References before_shmem_exit(), and ReplicationSlotShmemExit().

Referenced by BaseInit().

◆ ReplicationSlotMarkDirty()

◆ ReplicationSlotName()

bool ReplicationSlotName ( int  index,
Name  name 
)

Definition at line 423 of file slot.c.

424 {
425  ReplicationSlot *slot;
426  bool found;
427 
429 
430  /*
431  * Ensure that the slot cannot be dropped while we copy the name. Don't
432  * need the spinlock as the name of an existing slot cannot change.
433  */
434  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
435  found = slot->in_use;
436  if (slot->in_use)
437  namestrcpy(name, NameStr(slot->data.name));
438  LWLockRelease(ReplicationSlotControlLock);
439 
440  return found;
441 }
Definition: type.h:95

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

Referenced by pgstat_replslot_to_serialized_name_cb().

◆ ReplicationSlotPersist()

◆ ReplicationSlotRelease()

void ReplicationSlotRelease ( void  )

Definition at line 547 of file slot.c.

548 {
550 
551  Assert(slot != NULL && slot->active_pid != 0);
552 
553  if (slot->data.persistency == RS_EPHEMERAL)
554  {
555  /*
556  * Delete the slot. There is no !PANIC case where this is allowed to
557  * fail, all that may happen is an incomplete cleanup of the on-disk
558  * data.
559  */
561  }
562 
563  /*
564  * If slot needed to temporarily restrain both data and catalog xmin to
565  * create the catalog snapshot, remove that temporary constraint.
566  * Snapshots can only be exported while the initial snapshot is still
567  * acquired.
568  */
569  if (!TransactionIdIsValid(slot->data.xmin) &&
571  {
572  SpinLockAcquire(&slot->mutex);
574  SpinLockRelease(&slot->mutex);
576  }
577 
578  if (slot->data.persistency == RS_PERSISTENT)
579  {
580  /*
581  * Mark persistent slot inactive. We're not freeing it, just
582  * disconnecting, but wake up others that may be waiting for it.
583  */
584  SpinLockAcquire(&slot->mutex);
585  slot->active_pid = 0;
586  SpinLockRelease(&slot->mutex);
588  }
589 
590  MyReplicationSlot = NULL;
591 
592  /* might not have been set when we've been a plain slot */
593  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
596  LWLockRelease(ProcArrayLock);
597 }
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:60
@ RS_EPHEMERAL
Definition: slot.h:36
PGPROC * MyProc
Definition: proc.c:68
PROC_HDR * ProcGlobal
Definition: proc.c:80
uint8 statusFlags
Definition: proc.h:233
int pgxactoff
Definition: proc.h:188
uint8 * statusFlags
Definition: proc.h:377
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 1156 of file slot.c.

1157 {
1159 
1160  Assert(slot != NULL);
1162 
1163  /*
1164  * The replication slot mechanism is used to prevent removal of required
1165  * WAL. As there is no interlock between this routine and checkpoints, WAL
1166  * segments could concurrently be removed when a now stale return value of
1167  * ReplicationSlotsComputeRequiredLSN() is used. In the unlikely case that
1168  * this happens we'll just retry.
1169  */
1170  while (true)
1171  {
1172  XLogSegNo segno;
1173  XLogRecPtr restart_lsn;
1174 
1175  /*
1176  * For logical slots log a standby snapshot and start logical decoding
1177  * at exactly that position. That allows the slot to start up more
1178  * quickly.
1179  *
1180  * That's not needed (or indeed helpful) for physical slots as they'll
1181  * start replay at the last logged checkpoint anyway. Instead return
1182  * the location of the last redo LSN. While that slightly increases
1183  * the chance that we have to retry, it's where a base backup has to
1184  * start replay at.
1185  */
1186  if (!RecoveryInProgress() && SlotIsLogical(slot))
1187  {
1188  XLogRecPtr flushptr;
1189 
1190  /* start at current insert position */
1191  restart_lsn = GetXLogInsertRecPtr();
1192  SpinLockAcquire(&slot->mutex);
1193  slot->data.restart_lsn = restart_lsn;
1194  SpinLockRelease(&slot->mutex);
1195 
1196  /* make sure we have enough information to start */
1197  flushptr = LogStandbySnapshot();
1198 
1199  /* and make sure it's fsynced to disk */
1200  XLogFlush(flushptr);
1201  }
1202  else
1203  {
1204  restart_lsn = GetRedoRecPtr();
1205  SpinLockAcquire(&slot->mutex);
1206  slot->data.restart_lsn = restart_lsn;
1207  SpinLockRelease(&slot->mutex);
1208  }
1209 
1210  /* prevent WAL removal as fast as possible */
1212 
1213  /*
1214  * If all required WAL is still there, great, otherwise retry. The
1215  * slot should prevent further removal of WAL, unless there's a
1216  * concurrent ReplicationSlotsComputeRequiredLSN() after we've written
1217  * the new restart_lsn above, so normally we should never need to loop
1218  * more than twice.
1219  */
1221  if (XLogGetLastRemovedSegno() < segno)
1222  break;
1223  }
1224 }
XLogRecPtr LogStandbySnapshot(void)
Definition: standby.c:1272
bool RecoveryInProgress(void)
Definition: xlog.c:5912
XLogSegNo XLogGetLastRemovedSegno(void)
Definition: xlog.c:3461
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:6015
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:8858
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2512
#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 935 of file slot.c.

936 {
937  XLogRecPtr result = InvalidXLogRecPtr;
938  int i;
939 
940  if (max_replication_slots <= 0)
941  return InvalidXLogRecPtr;
942 
943  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
944 
945  for (i = 0; i < max_replication_slots; i++)
946  {
947  ReplicationSlot *s;
948  XLogRecPtr restart_lsn;
949 
951 
952  /* cannot change while ReplicationSlotCtlLock is held */
953  if (!s->in_use)
954  continue;
955 
956  /* we're only interested in logical slots */
957  if (!SlotIsLogical(s))
958  continue;
959 
960  /* read once, it's ok if it increases while we're checking */
961  SpinLockAcquire(&s->mutex);
962  restart_lsn = s->data.restart_lsn;
963  SpinLockRelease(&s->mutex);
964 
965  if (restart_lsn == InvalidXLogRecPtr)
966  continue;
967 
968  if (result == InvalidXLogRecPtr ||
969  restart_lsn < result)
970  result = restart_lsn;
971  }
972 
973  LWLockRelease(ReplicationSlotControlLock);
974 
975  return result;
976 }

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 892 of file slot.c.

893 {
894  int i;
895  XLogRecPtr min_required = InvalidXLogRecPtr;
896 
897  Assert(ReplicationSlotCtl != NULL);
898 
899  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
900  for (i = 0; i < max_replication_slots; i++)
901  {
903  XLogRecPtr restart_lsn;
904 
905  if (!s->in_use)
906  continue;
907 
908  SpinLockAcquire(&s->mutex);
909  restart_lsn = s->data.restart_lsn;
910  SpinLockRelease(&s->mutex);
911 
912  if (restart_lsn != InvalidXLogRecPtr &&
913  (min_required == InvalidXLogRecPtr ||
914  restart_lsn < min_required))
915  min_required = restart_lsn;
916  }
917  LWLockRelease(ReplicationSlotControlLock);
918 
919  XLogSetReplicationSlotMinimumLSN(min_required);
920 }
void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
Definition: xlog.c:2398

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 835 of file slot.c.

836 {
837  int i;
839  TransactionId agg_catalog_xmin = InvalidTransactionId;
840 
841  Assert(ReplicationSlotCtl != NULL);
842 
843  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
844 
845  for (i = 0; i < max_replication_slots; i++)
846  {
848  TransactionId effective_xmin;
849  TransactionId effective_catalog_xmin;
850  bool invalidated;
851 
852  if (!s->in_use)
853  continue;
854 
855  SpinLockAcquire(&s->mutex);
856  effective_xmin = s->effective_xmin;
857  effective_catalog_xmin = s->effective_catalog_xmin;
858  invalidated = (!XLogRecPtrIsInvalid(s->data.invalidated_at) &&
860  SpinLockRelease(&s->mutex);
861 
862  /* invalidated slots need not apply */
863  if (invalidated)
864  continue;
865 
866  /* check the data xmin */
867  if (TransactionIdIsValid(effective_xmin) &&
868  (!TransactionIdIsValid(agg_xmin) ||
869  TransactionIdPrecedes(effective_xmin, agg_xmin)))
870  agg_xmin = effective_xmin;
871 
872  /* check the catalog xmin */
873  if (TransactionIdIsValid(effective_catalog_xmin) &&
874  (!TransactionIdIsValid(agg_catalog_xmin) ||
875  TransactionIdPrecedes(effective_catalog_xmin, agg_catalog_xmin)))
876  agg_catalog_xmin = effective_catalog_xmin;
877  }
878 
879  LWLockRelease(ReplicationSlotControlLock);
880 
881  ProcArraySetReplicationSlotXmin(agg_xmin, agg_catalog_xmin, already_locked);
882 }
uint32 TransactionId
Definition: c.h:588
void ProcArraySetReplicationSlotXmin(TransactionId xmin, TransactionId catalog_xmin, bool already_locked)
Definition: procarray.c:3891
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:273

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

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 987 of file slot.c.

988 {
989  int i;
990 
991  *nslots = *nactive = 0;
992 
993  if (max_replication_slots <= 0)
994  return false;
995 
996  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
997  for (i = 0; i < max_replication_slots; i++)
998  {
999  ReplicationSlot *s;
1000 
1002 
1003  /* cannot change while ReplicationSlotCtlLock is held */
1004  if (!s->in_use)
1005  continue;
1006 
1007  /* only logical slots are database specific, skip */
1008  if (!SlotIsLogical(s))
1009  continue;
1010 
1011  /* not our database, skip */
1012  if (s->data.database != dboid)
1013  continue;
1014 
1015  /* count slots with spinlock held */
1016  SpinLockAcquire(&s->mutex);
1017  (*nslots)++;
1018  if (s->active_pid != 0)
1019  (*nactive)++;
1020  SpinLockRelease(&s->mutex);
1021  }
1022  LWLockRelease(ReplicationSlotControlLock);
1023 
1024  if (*nslots > 0)
1025  return true;
1026  return false;
1027 }

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 1043 of file slot.c.

1044 {
1045  int i;
1046 
1047  if (max_replication_slots <= 0)
1048  return;
1049 
1050 restart:
1051  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1052  for (i = 0; i < max_replication_slots; i++)
1053  {
1054  ReplicationSlot *s;
1055  char *slotname;
1056  int active_pid;
1057 
1059 
1060  /* cannot change while ReplicationSlotCtlLock is held */
1061  if (!s->in_use)
1062  continue;
1063 
1064  /* only logical slots are database specific, skip */
1065  if (!SlotIsLogical(s))
1066  continue;
1067 
1068  /* not our database, skip */
1069  if (s->data.database != dboid)
1070  continue;
1071 
1072  /* acquire slot, so ReplicationSlotDropAcquired can be reused */
1073  SpinLockAcquire(&s->mutex);
1074  /* can't change while ReplicationSlotControlLock is held */
1075  slotname = NameStr(s->data.name);
1076  active_pid = s->active_pid;
1077  if (active_pid == 0)
1078  {
1079  MyReplicationSlot = s;
1080  s->active_pid = MyProcPid;
1081  }
1082  SpinLockRelease(&s->mutex);
1083 
1084  /*
1085  * Even though we hold an exclusive lock on the database object a
1086  * logical slot for that DB can still be active, e.g. if it's
1087  * concurrently being dropped by a backend connected to another DB.
1088  *
1089  * That's fairly unlikely in practice, so we'll just bail out.
1090  */
1091  if (active_pid)
1092  ereport(ERROR,
1093  (errcode(ERRCODE_OBJECT_IN_USE),
1094  errmsg("replication slot \"%s\" is active for PID %d",
1095  slotname, active_pid)));
1096 
1097  /*
1098  * To avoid duplicating ReplicationSlotDropAcquired() and to avoid
1099  * holding ReplicationSlotControlLock over filesystem operations,
1100  * release ReplicationSlotControlLock and use
1101  * ReplicationSlotDropAcquired.
1102  *
1103  * As that means the set of slots could change, restart scan from the
1104  * beginning each time we release the lock.
1105  */
1106  LWLockRelease(ReplicationSlotControlLock);
1108  goto restart;
1109  }
1110  LWLockRelease(ReplicationSlotControlLock);
1111 }

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

◆ ReplicationSlotShmemExit()

static void ReplicationSlotShmemExit ( int  code,
Datum  arg 
)
static

Definition at line 179 of file slot.c.

180 {
181  /* Make sure active replication slots are released */
182  if (MyReplicationSlot != NULL)
184 
185  /* Also cleanup all the temporary slots. */
187 }
void ReplicationSlotCleanup(void)
Definition: slot.c:603

References MyReplicationSlot, ReplicationSlotCleanup(), and ReplicationSlotRelease().

Referenced by ReplicationSlotInitialize().

◆ ReplicationSlotsShmemInit()

void ReplicationSlotsShmemInit ( void  )

Definition at line 135 of file slot.c.

136 {
137  bool found;
138 
139  if (max_replication_slots == 0)
140  return;
141 
143  ShmemInitStruct("ReplicationSlot Ctl", ReplicationSlotsShmemSize(),
144  &found);
145 
146  if (!found)
147  {
148  int i;
149 
150  /* First time through, so initialize */
152 
153  for (i = 0; i < max_replication_slots; i++)
154  {
156 
157  /* everything else is zeroed by the memset above */
158  SpinLockInit(&slot->mutex);
162  }
163  }
164 }
#define MemSet(start, val, len)
Definition: c.h:953
void ConditionVariableInit(ConditionVariable *cv)
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:729
@ LWTRANCHE_REPLICATION_SLOT_IO
Definition: lwlock.h:187
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:117
#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 117 of file slot.c.

118 {
119  Size size = 0;
120 
121  if (max_replication_slots == 0)
122  return size;
123 
124  size = offsetof(ReplicationSlotCtlData, replication_slots);
125  size = add_size(size,
127 
128  return size;
129 }
size_t Size
Definition: c.h:541
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, and mul_size().

Referenced by CalculateShmemSize(), and ReplicationSlotsShmemInit().

◆ ReplicationSlotValidateName()

bool ReplicationSlotValidateName ( const char *  name,
int  elevel 
)

Definition at line 198 of file slot.c.

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

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

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

◆ RestoreSlotFromDisk()

static void RestoreSlotFromDisk ( const char *  name)
static

Definition at line 1753 of file slot.c.

1754 {
1756  int i;
1757  char slotdir[MAXPGPATH + 12];
1758  char path[MAXPGPATH + 22];
1759  int fd;
1760  bool restored = false;
1761  int readBytes;
1762  pg_crc32c checksum;
1763 
1764  /* no need to lock here, no concurrent access allowed yet */
1765 
1766  /* delete temp file if it exists */
1767  sprintf(slotdir, "pg_replslot/%s", name);
1768  sprintf(path, "%s/state.tmp", slotdir);
1769  if (unlink(path) < 0 && errno != ENOENT)
1770  ereport(PANIC,
1772  errmsg("could not remove file \"%s\": %m", path)));
1773 
1774  sprintf(path, "%s/state", slotdir);
1775 
1776  elog(DEBUG1, "restoring replication slot from \"%s\"", path);
1777 
1778  /* on some operating systems fsyncing a file requires O_RDWR */
1779  fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
1780 
1781  /*
1782  * We do not need to handle this as we are rename()ing the directory into
1783  * place only after we fsync()ed the state file.
1784  */
1785  if (fd < 0)
1786  ereport(PANIC,
1788  errmsg("could not open file \"%s\": %m", path)));
1789 
1790  /*
1791  * Sync state file before we're reading from it. We might have crashed
1792  * while it wasn't synced yet and we shouldn't continue on that basis.
1793  */
1795  if (pg_fsync(fd) != 0)
1796  ereport(PANIC,
1798  errmsg("could not fsync file \"%s\": %m",
1799  path)));
1801 
1802  /* Also sync the parent directory */
1804  fsync_fname(slotdir, true);
1805  END_CRIT_SECTION();
1806 
1807  /* read part of statefile that's guaranteed to be version independent */
1809  readBytes = read(fd, &cp, ReplicationSlotOnDiskConstantSize);
1811  if (readBytes != ReplicationSlotOnDiskConstantSize)
1812  {
1813  if (readBytes < 0)
1814  ereport(PANIC,
1816  errmsg("could not read file \"%s\": %m", path)));
1817  else
1818  ereport(PANIC,
1820  errmsg("could not read file \"%s\": read %d of %zu",
1821  path, readBytes,
1823  }
1824 
1825  /* verify magic */
1826  if (cp.magic != SLOT_MAGIC)
1827  ereport(PANIC,
1829  errmsg("replication slot file \"%s\" has wrong magic number: %u instead of %u",
1830  path, cp.magic, SLOT_MAGIC)));
1831 
1832  /* verify version */
1833  if (cp.version != SLOT_VERSION)
1834  ereport(PANIC,
1836  errmsg("replication slot file \"%s\" has unsupported version %u",
1837  path, cp.version)));
1838 
1839  /* boundary check on length */
1841  ereport(PANIC,
1843  errmsg("replication slot file \"%s\" has corrupted length %u",
1844  path, cp.length)));
1845 
1846  /* Now that we know the size, read the entire file */
1848  readBytes = read(fd,
1849  (char *) &cp + ReplicationSlotOnDiskConstantSize,
1850  cp.length);
1852  if (readBytes != cp.length)
1853  {
1854  if (readBytes < 0)
1855  ereport(PANIC,
1857  errmsg("could not read file \"%s\": %m", path)));
1858  else
1859  ereport(PANIC,
1861  errmsg("could not read file \"%s\": read %d of %zu",
1862  path, readBytes, (Size) cp.length)));
1863  }
1864 
1865  if (CloseTransientFile(fd) != 0)
1866  ereport(PANIC,
1868  errmsg("could not close file \"%s\": %m", path)));
1869 
1870  /* now verify the CRC */
1871  INIT_CRC32C(checksum);
1872  COMP_CRC32C(checksum,
1875  FIN_CRC32C(checksum);
1876 
1877  if (!EQ_CRC32C(checksum, cp.checksum))
1878  ereport(PANIC,
1879  (errmsg("checksum mismatch for replication slot file \"%s\": is %u, should be %u",
1880  path, checksum, cp.checksum)));
1881 
1882  /*
1883  * If we crashed with an ephemeral slot active, don't restore but delete
1884  * it.
1885  */
1887  {
1888  if (!rmtree(slotdir, true))
1889  {
1890  ereport(WARNING,
1891  (errmsg("could not remove directory \"%s\"",
1892  slotdir)));
1893  }
1894  fsync_fname("pg_replslot", true);
1895  return;
1896  }
1897 
1898  /*
1899  * Verify that requirements for the specific slot type are met. That's
1900  * important because if these aren't met we're not guaranteed to retain
1901  * all the necessary resources for the slot.
1902  *
1903  * NB: We have to do so *after* the above checks for ephemeral slots,
1904  * because otherwise a slot that shouldn't exist anymore could prevent
1905  * restarts.
1906  *
1907  * NB: Changing the requirements here also requires adapting
1908  * CheckSlotRequirements() and CheckLogicalDecodingRequirements().
1909  */
1911  ereport(FATAL,
1912  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1913  errmsg("logical replication slot \"%s\" exists, but wal_level < logical",
1914  NameStr(cp.slotdata.name)),
1915  errhint("Change wal_level to be logical or higher.")));
1916  else if (wal_level < WAL_LEVEL_REPLICA)
1917  ereport(FATAL,
1918  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1919  errmsg("physical replication slot \"%s\" exists, but wal_level < replica",
1920  NameStr(cp.slotdata.name)),
1921  errhint("Change wal_level to be replica or higher.")));
1922 
1923  /* nothing can be active yet, don't lock anything */
1924  for (i = 0; i < max_replication_slots; i++)
1925  {
1926  ReplicationSlot *slot;
1927 
1929 
1930  if (slot->in_use)
1931  continue;
1932 
1933  /* restore the entire set of persistent data */
1934  memcpy(&slot->data, &cp.slotdata,
1936 
1937  /* initialize in memory state */
1938  slot->effective_xmin = cp.slotdata.xmin;
1940 
1945 
1946  slot->in_use = true;
1947  slot->active_pid = 0;
1948 
1949  restored = true;
1950  break;
1951  }
1952 
1953  if (!restored)
1954  ereport(FATAL,
1955  (errmsg("too many replication slots active before shutdown"),
1956  errhint("Increase max_replication_slots and try again.")));
1957 }
#define PG_BINARY
Definition: c.h:1209
#define FATAL
Definition: elog.h:37
#define PANIC
Definition: elog.h:38
int CloseTransientFile(int fd)
Definition: fd.c:2609
int pg_fsync(int fd)
Definition: fd.c:356
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2433
#define read(a, b, c)
Definition: win32.h:13
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:41
uint32 pg_crc32c
Definition: pg_crc32c.h:38
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:89
#define EQ_CRC32C(c1, c2)
Definition: pg_crc32c.h:42
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:94
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define ReplicationSlotOnDiskChecksummedSize
Definition: slot.c:85
#define ReplicationSlotOnDiskNotChecksummedSize
Definition: slot.c:82
#define ReplicationSlotOnDiskV2Size
Definition: slot.c:88
#define SLOT_VERSION
Definition: slot.c:92
#define SLOT_MAGIC
Definition: slot.c:91
#define ReplicationSlotOnDiskConstantSize
Definition: slot.c:79
uint32 version
Definition: slot.c:67
ReplicationSlotPersistentData slotdata
Definition: slot.c:75
pg_crc32c checksum
Definition: slot.c:64
TransactionId catalog_xmin
Definition: slot.h:70
@ WAIT_EVENT_REPLICATION_SLOT_READ
Definition: wait_event.h:203
@ WAIT_EVENT_REPLICATION_SLOT_RESTORE_SYNC
Definition: wait_event.h:204
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:268
static void pgstat_report_wait_end(void)
Definition: wait_event.h:284
@ WAL_LEVEL_LOGICAL
Definition: xlog.h:71

References ReplicationSlot::active_pid, ReplicationSlot::candidate_catalog_xmin, ReplicationSlot::candidate_restart_lsn, ReplicationSlot::candidate_restart_valid, ReplicationSlot::candidate_xmin_lsn, ReplicationSlotPersistentData::catalog_xmin, ReplicationSlotOnDisk::checksum, CloseTransientFile(), COMP_CRC32C, ReplicationSlot::data, ReplicationSlotPersistentData::database, DEBUG1, ReplicationSlot::effective_catalog_xmin, ReplicationSlot::effective_xmin, elog(), END_CRIT_SECTION, EQ_CRC32C, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errcode_for_file_access(), errhint(), errmsg(), FATAL, fd(), FIN_CRC32C, fsync_fname(), i, ReplicationSlot::in_use, INIT_CRC32C, InvalidOid, InvalidTransactionId, InvalidXLogRecPtr, ReplicationSlotOnDisk::length, ReplicationSlotOnDisk::magic, max_replication_slots, MAXPGPATH, name, ReplicationSlotPersistentData::name, NameStr, OpenTransientFile(), PANIC, ReplicationSlotPersistentData::persistency, PG_BINARY, pg_fsync(), pgstat_report_wait_end(), pgstat_report_wait_start(), read, ReplicationSlotCtlData::replication_slots, ReplicationSlotCtl, ReplicationSlotOnDiskChecksummedSize, ReplicationSlotOnDiskConstantSize, ReplicationSlotOnDiskNotChecksummedSize, ReplicationSlotOnDiskV2Size, rmtree(), RS_PERSISTENT, SLOT_MAGIC, SLOT_VERSION, ReplicationSlotOnDisk::slotdata, sprintf, START_CRIT_SECTION, ReplicationSlotOnDisk::version, WAIT_EVENT_REPLICATION_SLOT_READ, WAIT_EVENT_REPLICATION_SLOT_RESTORE_SYNC, wal_level, WAL_LEVEL_LOGICAL, WAL_LEVEL_REPLICA, WARNING, and ReplicationSlotPersistentData::xmin.

Referenced by StartupReplicationSlots().

◆ SaveSlotToPath()

static void SaveSlotToPath ( ReplicationSlot slot,
const char *  dir,
int  elevel 
)
static

Definition at line 1599 of file slot.c.

1600 {
1601  char tmppath[MAXPGPATH];
1602  char path[MAXPGPATH];
1603  int fd;
1605  bool was_dirty;
1606 
1607  /* first check whether there's something to write out */
1608  SpinLockAcquire(&slot->mutex);
1609  was_dirty = slot->dirty;
1610  slot->just_dirtied = false;
1611  SpinLockRelease(&slot->mutex);
1612 
1613  /* and don't do anything if there's nothing to write */
1614  if (!was_dirty)
1615  return;
1616 
1618 
1619  /* silence valgrind :( */
1620  memset(&cp, 0, sizeof(ReplicationSlotOnDisk));
1621 
1622  sprintf(tmppath, "%s/state.tmp", dir);
1623  sprintf(path, "%s/state", dir);
1624 
1625  fd = OpenTransientFile(tmppath, O_CREAT | O_EXCL | O_WRONLY | PG_BINARY);
1626  if (fd < 0)
1627  {
1628  /*
1629  * If not an ERROR, then release the lock before returning. In case
1630  * of an ERROR, the error recovery path automatically releases the
1631  * lock, but no harm in explicitly releasing even in that case. Note
1632  * that LWLockRelease() could affect errno.
1633  */
1634  int save_errno = errno;
1635 
1637  errno = save_errno;
1638  ereport(elevel,
1640  errmsg("could not create file \"%s\": %m",
1641  tmppath)));
1642  return;
1643  }
1644 
1645  cp.magic = SLOT_MAGIC;
1646  INIT_CRC32C(cp.checksum);
1647  cp.version = SLOT_VERSION;
1649 
1650  SpinLockAcquire(&slot->mutex);
1651 
1652  memcpy(&cp.slotdata, &slot->data, sizeof(ReplicationSlotPersistentData));
1653 
1654  SpinLockRelease(&slot->mutex);
1655 
1656  COMP_CRC32C(cp.checksum,
1657  (char *) (&cp) + ReplicationSlotOnDiskNotChecksummedSize,
1659  FIN_CRC32C(cp.checksum);
1660 
1661  errno = 0;
1663  if ((write(fd, &cp, sizeof(cp))) != sizeof(cp))
1664  {
1665  int save_errno = errno;
1666 
1670 
1671  /* if write didn't set errno, assume problem is no disk space */
1672  errno = save_errno ? save_errno : ENOSPC;
1673  ereport(elevel,
1675  errmsg("could not write to file \"%s\": %m",
1676  tmppath)));
1677  return;
1678  }
1680 
1681  /* fsync the temporary file */
1683  if (pg_fsync(fd) != 0)
1684  {
1685  int save_errno = errno;
1686 
1690  errno = save_errno;
1691  ereport(elevel,
1693  errmsg("could not fsync file \"%s\": %m",
1694  tmppath)));
1695  return;
1696  }
1698 
1699  if (CloseTransientFile(fd) != 0)
1700  {
1701  int save_errno = errno;
1702 
1704  errno = save_errno;
1705  ereport(elevel,
1707  errmsg("could not close file \"%s\": %m",
1708  tmppath)));
1709  return;
1710  }
1711 
1712  /* rename to permanent file, fsync file and directory */
1713  if (rename(tmppath, path) != 0)
1714  {
1715  int save_errno = errno;
1716 
1718  errno = save_errno;
1719  ereport(elevel,
1721  errmsg("could not rename file \"%s\" to \"%s\": %m",
1722  tmppath, path)));
1723  return;
1724  }
1725 
1726  /*
1727  * Check CreateSlotOnDisk() for the reasoning of using a critical section.
1728  */
1730 
1731  fsync_fname(path, false);
1732  fsync_fname(dir, true);
1733  fsync_fname("pg_replslot", true);
1734 
1735  END_CRIT_SECTION();
1736 
1737  /*
1738  * Successfully wrote, unset dirty bit, unless somebody dirtied again
1739  * already.
1740  */
1741  SpinLockAcquire(&slot->mutex);
1742  if (!slot->just_dirtied)
1743  slot->dirty = false;
1744  SpinLockRelease(&slot->mutex);
1745 
1747 }
#define write(a, b, c)
Definition: win32.h:14
@ WAIT_EVENT_REPLICATION_SLOT_WRITE
Definition: wait_event.h:206
@ WAIT_EVENT_REPLICATION_SLOT_SYNC
Definition: wait_event.h:205

References ReplicationSlotOnDisk::checksum, CloseTransientFile(), COMP_CRC32C, ReplicationSlot::data, ReplicationSlot::dirty, END_CRIT_SECTION, ereport, errcode_for_file_access(), errmsg(), fd(), FIN_CRC32C, fsync_fname(), INIT_CRC32C, ReplicationSlot::io_in_progress_lock, ReplicationSlot::just_dirtied, ReplicationSlotOnDisk::length, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), ReplicationSlotOnDisk::magic, MAXPGPATH, ReplicationSlot::mutex, OpenTransientFile(), PG_BINARY, pg_fsync(), pgstat_report_wait_end(), pgstat_report_wait_start(), ReplicationSlotOnDiskChecksummedSize, ReplicationSlotOnDiskNotChecksummedSize, ReplicationSlotOnDiskV2Size, SLOT_MAGIC, SLOT_VERSION, ReplicationSlotOnDisk::slotdata, SpinLockAcquire, SpinLockRelease, sprintf, START_CRIT_SECTION, ReplicationSlotOnDisk::version, WAIT_EVENT_REPLICATION_SLOT_SYNC, WAIT_EVENT_REPLICATION_SLOT_WRITE, and write.

Referenced by CheckPointReplicationSlots(), CreateSlotOnDisk(), and ReplicationSlotSave().

◆ SearchNamedReplicationSlot()

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

Definition at line 374 of file slot.c.

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

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 1477 of file slot.c.

1478 {
1479  DIR *replication_dir;
1480  struct dirent *replication_de;
1481 
1482  elog(DEBUG1, "starting up replication slots");
1483 
1484  /* restore all slots by iterating over all on-disk entries */
1485  replication_dir = AllocateDir("pg_replslot");
1486  while ((replication_de = ReadDir(replication_dir, "pg_replslot")) != NULL)
1487  {
1488  char path[MAXPGPATH + 12];
1489  PGFileType de_type;
1490 
1491  if (strcmp(replication_de->d_name, ".") == 0 ||
1492  strcmp(replication_de->d_name, "..") == 0)
1493  continue;
1494 
1495  snprintf(path, sizeof(path), "pg_replslot/%s", replication_de->d_name);
1496  de_type = get_dirent_type(path, replication_de, false, DEBUG1);
1497 
1498  /* we're only creating directories here, skip if it's not our's */
1499  if (de_type != PGFILETYPE_ERROR && de_type != PGFILETYPE_DIR)
1500  continue;
1501 
1502  /* we crashed while a slot was being setup or deleted, clean up */
1503  if (pg_str_endswith(replication_de->d_name, ".tmp"))
1504  {
1505  if (!rmtree(path, true))
1506  {
1507  ereport(WARNING,
1508  (errmsg("could not remove directory \"%s\"",
1509  path)));
1510  continue;
1511  }
1512  fsync_fname("pg_replslot", true);
1513  continue;
1514  }
1515 
1516  /* looks like a slot in a normal state, restore */
1517  RestoreSlotFromDisk(replication_de->d_name);
1518  }
1519  FreeDir(replication_dir);
1520 
1521  /* currently no slots exist, we're done. */
1522  if (max_replication_slots <= 0)
1523  return;
1524 
1525  /* Now that we have recovered all the data, compute replication xmin */
1528 }
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2709
int FreeDir(DIR *dir)
Definition: fd.c:2761
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2643
PGFileType get_dirent_type(const char *path, const struct dirent *de, bool look_through_symlinks, int elevel)
Definition: file_utils.c:406
PGFileType
Definition: file_utils.h:19
@ PGFILETYPE_DIR
Definition: file_utils.h:23
@ PGFILETYPE_ERROR
Definition: file_utils.h:20
#define snprintf
Definition: port.h:238
static void RestoreSlotFromDisk(const char *name)
Definition: slot.c:1753
bool pg_str_endswith(const char *str, const char *end)
Definition: string.c:32
Definition: dirent.c:26
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15

References AllocateDir(), dirent::d_name, DEBUG1, elog(), ereport, errmsg(), FreeDir(), fsync_fname(), get_dirent_type(), max_replication_slots, MAXPGPATH, pg_str_endswith(), PGFILETYPE_DIR, PGFILETYPE_ERROR, ReadDir(), ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), RestoreSlotFromDisk(), rmtree(), snprintf, and WARNING.

Referenced by StartupXLOG().

Variable Documentation

◆ max_replication_slots

◆ MyReplicationSlot

◆ ReplicationSlotCtl