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

1445 {
1446  int i;
1447 
1448  elog(DEBUG1, "performing replication slot checkpoint");
1449 
1450  /*
1451  * Prevent any slot from being created/dropped while we're active. As we
1452  * explicitly do *not* want to block iterating over replication_slots or
1453  * acquiring a slot we cannot take the control lock - but that's OK,
1454  * because holding ReplicationSlotAllocationLock is strictly stronger, and
1455  * enough to guarantee that nobody can change the in_use bits on us.
1456  */
1457  LWLockAcquire(ReplicationSlotAllocationLock, LW_SHARED);
1458 
1459  for (i = 0; i < max_replication_slots; i++)
1460  {
1462  char path[MAXPGPATH];
1463 
1464  if (!s->in_use)
1465  continue;
1466 
1467  /* save the slot to disk, locking is handled in SaveSlotToPath() */
1468  sprintf(path, "pg_replslot/%s", NameStr(s->data.name));
1469  SaveSlotToPath(s, path, LOG);
1470  }
1471  LWLockRelease(ReplicationSlotAllocationLock);
1472 }
#define NameStr(name)
Definition: c.h:730
#define LOG
Definition: elog.h:31
#define DEBUG1
Definition: elog.h:30
int i
Definition: isn.c:73
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1195
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
@ LW_SHARED
Definition: lwlock.h:116
#define MAXPGPATH
#define sprintf
Definition: port.h:240
static void SaveSlotToPath(ReplicationSlot *slot, const char *dir, int elevel)
Definition: slot.c:1601
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 (!has_rolreplication(GetUserId()))
1144  ereport(ERROR,
1145  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1146  errmsg("permission denied to use replication slots"),
1147  errdetail("Only roles with the %s attribute may use replication slots.",
1148  "REPLICATION")));
1149 }
int errdetail(const char *fmt,...)
Definition: elog.c:1202
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
Oid GetUserId(void)
Definition: miscinit.c:510
bool has_rolreplication(Oid roleid)
Definition: miscinit.c:707

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

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

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

1402 {
1403  XLogRecPtr oldestLSN;
1404  bool invalidated = false;
1405 
1406  XLogSegNoOffsetToRecPtr(oldestSegno, 0, wal_segment_size, oldestLSN);
1407 
1408 restart:
1409  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1410  for (int i = 0; i < max_replication_slots; i++)
1411  {
1413 
1414  if (!s->in_use)
1415  continue;
1416 
1417  if (InvalidatePossiblyObsoleteSlot(s, oldestLSN, &invalidated))
1418  {
1419  /* if the lock was released, start from scratch */
1420  goto restart;
1421  }
1422  }
1423  LWLockRelease(ReplicationSlotControlLock);
1424 
1425  /*
1426  * If any slots have been invalidated, recalculate the resource limits.
1427  */
1428  if (invalidated)
1429  {
1432  }
1433 
1434  return invalidated;
1435 }
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:1241
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 1241 of file slot.c.

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

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

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:636
void ProcArraySetReplicationSlotXmin(TransactionId xmin, TransactionId catalog_xmin, bool already_locked)
Definition: procarray.c:3927
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280

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:1004
void ConditionVariableInit(ConditionVariable *cv)
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:730
@ LWTRANCHE_REPLICATION_SLOT_IO
Definition: lwlock.h:190
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:589
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 1755 of file slot.c.

1756 {
1758  int i;
1759  char slotdir[MAXPGPATH + 12];
1760  char path[MAXPGPATH + 22];
1761  int fd;
1762  bool restored = false;
1763  int readBytes;
1764  pg_crc32c checksum;
1765 
1766  /* no need to lock here, no concurrent access allowed yet */
1767 
1768  /* delete temp file if it exists */
1769  sprintf(slotdir, "pg_replslot/%s", name);
1770  sprintf(path, "%s/state.tmp", slotdir);
1771  if (unlink(path) < 0 && errno != ENOENT)
1772  ereport(PANIC,
1774  errmsg("could not remove file \"%s\": %m", path)));
1775 
1776  sprintf(path, "%s/state", slotdir);
1777 
1778  elog(DEBUG1, "restoring replication slot from \"%s\"", path);
1779 
1780  /* on some operating systems fsyncing a file requires O_RDWR */
1781  fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
1782 
1783  /*
1784  * We do not need to handle this as we are rename()ing the directory into
1785  * place only after we fsync()ed the state file.
1786  */
1787  if (fd < 0)
1788  ereport(PANIC,
1790  errmsg("could not open file \"%s\": %m", path)));
1791 
1792  /*
1793  * Sync state file before we're reading from it. We might have crashed
1794  * while it wasn't synced yet and we shouldn't continue on that basis.
1795  */
1797  if (pg_fsync(fd) != 0)
1798  ereport(PANIC,
1800  errmsg("could not fsync file \"%s\": %m",
1801  path)));
1803 
1804  /* Also sync the parent directory */
1806  fsync_fname(slotdir, true);
1807  END_CRIT_SECTION();
1808 
1809  /* read part of statefile that's guaranteed to be version independent */
1811  readBytes = read(fd, &cp, ReplicationSlotOnDiskConstantSize);
1813  if (readBytes != ReplicationSlotOnDiskConstantSize)
1814  {
1815  if (readBytes < 0)
1816  ereport(PANIC,
1818  errmsg("could not read file \"%s\": %m", path)));
1819  else
1820  ereport(PANIC,
1822  errmsg("could not read file \"%s\": read %d of %zu",
1823  path, readBytes,
1825  }
1826 
1827  /* verify magic */
1828  if (cp.magic != SLOT_MAGIC)
1829  ereport(PANIC,
1831  errmsg("replication slot file \"%s\" has wrong magic number: %u instead of %u",
1832  path, cp.magic, SLOT_MAGIC)));
1833 
1834  /* verify version */
1835  if (cp.version != SLOT_VERSION)
1836  ereport(PANIC,
1838  errmsg("replication slot file \"%s\" has unsupported version %u",
1839  path, cp.version)));
1840 
1841  /* boundary check on length */
1843  ereport(PANIC,
1845  errmsg("replication slot file \"%s\" has corrupted length %u",
1846  path, cp.length)));
1847 
1848  /* Now that we know the size, read the entire file */
1850  readBytes = read(fd,
1851  (char *) &cp + ReplicationSlotOnDiskConstantSize,
1852  cp.length);
1854  if (readBytes != cp.length)
1855  {
1856  if (readBytes < 0)
1857  ereport(PANIC,
1859  errmsg("could not read file \"%s\": %m", path)));
1860  else
1861  ereport(PANIC,
1863  errmsg("could not read file \"%s\": read %d of %zu",
1864  path, readBytes, (Size) cp.length)));
1865  }
1866 
1867  if (CloseTransientFile(fd) != 0)
1868  ereport(PANIC,
1870  errmsg("could not close file \"%s\": %m", path)));
1871 
1872  /* now verify the CRC */
1873  INIT_CRC32C(checksum);
1874  COMP_CRC32C(checksum,
1877  FIN_CRC32C(checksum);
1878 
1879  if (!EQ_CRC32C(checksum, cp.checksum))
1880  ereport(PANIC,
1881  (errmsg("checksum mismatch for replication slot file \"%s\": is %u, should be %u",
1882  path, checksum, cp.checksum)));
1883 
1884  /*
1885  * If we crashed with an ephemeral slot active, don't restore but delete
1886  * it.
1887  */
1889  {
1890  if (!rmtree(slotdir, true))
1891  {
1892  ereport(WARNING,
1893  (errmsg("could not remove directory \"%s\"",
1894  slotdir)));
1895  }
1896  fsync_fname("pg_replslot", true);
1897  return;
1898  }
1899 
1900  /*
1901  * Verify that requirements for the specific slot type are met. That's
1902  * important because if these aren't met we're not guaranteed to retain
1903  * all the necessary resources for the slot.
1904  *
1905  * NB: We have to do so *after* the above checks for ephemeral slots,
1906  * because otherwise a slot that shouldn't exist anymore could prevent
1907  * restarts.
1908  *
1909  * NB: Changing the requirements here also requires adapting
1910  * CheckSlotRequirements() and CheckLogicalDecodingRequirements().
1911  */
1913  ereport(FATAL,
1914  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1915  errmsg("logical replication slot \"%s\" exists, but wal_level < logical",
1916  NameStr(cp.slotdata.name)),
1917  errhint("Change wal_level to be logical or higher.")));
1918  else if (wal_level < WAL_LEVEL_REPLICA)
1919  ereport(FATAL,
1920  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1921  errmsg("physical replication slot \"%s\" exists, but wal_level < replica",
1922  NameStr(cp.slotdata.name)),
1923  errhint("Change wal_level to be replica or higher.")));
1924 
1925  /* nothing can be active yet, don't lock anything */
1926  for (i = 0; i < max_replication_slots; i++)
1927  {
1928  ReplicationSlot *slot;
1929 
1931 
1932  if (slot->in_use)
1933  continue;
1934 
1935  /* restore the entire set of persistent data */
1936  memcpy(&slot->data, &cp.slotdata,
1938 
1939  /* initialize in memory state */
1940  slot->effective_xmin = cp.slotdata.xmin;
1942 
1947 
1948  slot->in_use = true;
1949  slot->active_pid = 0;
1950 
1951  restored = true;
1952  break;
1953  }
1954 
1955  if (!restored)
1956  ereport(FATAL,
1957  (errmsg("too many replication slots active before shutdown"),
1958  errhint("Increase max_replication_slots and try again.")));
1959 }
#define PG_BINARY
Definition: c.h:1260
#define FATAL
Definition: elog.h:41
#define PANIC
Definition: elog.h:42
int CloseTransientFile(int fd)
Definition: fd.c:2610
int pg_fsync(int fd)
Definition: fd.c:356
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2434
#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:206
@ WAIT_EVENT_REPLICATION_SLOT_RESTORE_SYNC
Definition: wait_event.h:207
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:271
static void pgstat_report_wait_end(void)
Definition: wait_event.h:287
@ 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 1601 of file slot.c.

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

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

1480 {
1481  DIR *replication_dir;
1482  struct dirent *replication_de;
1483 
1484  elog(DEBUG1, "starting up replication slots");
1485 
1486  /* restore all slots by iterating over all on-disk entries */
1487  replication_dir = AllocateDir("pg_replslot");
1488  while ((replication_de = ReadDir(replication_dir, "pg_replslot")) != NULL)
1489  {
1490  char path[MAXPGPATH + 12];
1491  PGFileType de_type;
1492 
1493  if (strcmp(replication_de->d_name, ".") == 0 ||
1494  strcmp(replication_de->d_name, "..") == 0)
1495  continue;
1496 
1497  snprintf(path, sizeof(path), "pg_replslot/%s", replication_de->d_name);
1498  de_type = get_dirent_type(path, replication_de, false, DEBUG1);
1499 
1500  /* we're only creating directories here, skip if it's not our's */
1501  if (de_type != PGFILETYPE_ERROR && de_type != PGFILETYPE_DIR)
1502  continue;
1503 
1504  /* we crashed while a slot was being setup or deleted, clean up */
1505  if (pg_str_endswith(replication_de->d_name, ".tmp"))
1506  {
1507  if (!rmtree(path, true))
1508  {
1509  ereport(WARNING,
1510  (errmsg("could not remove directory \"%s\"",
1511  path)));
1512  continue;
1513  }
1514  fsync_fname("pg_replslot", true);
1515  continue;
1516  }
1517 
1518  /* looks like a slot in a normal state, restore */
1519  RestoreSlotFromDisk(replication_de->d_name);
1520  }
1521  FreeDir(replication_dir);
1522 
1523  /* currently no slots exist, we're done. */
1524  if (max_replication_slots <= 0)
1525  return;
1526 
1527  /* Now that we have recovered all the data, compute replication xmin */
1530 }
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2710
int FreeDir(DIR *dir)
Definition: fd.c:2762
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2644
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:1755
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