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/string.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "replication/slot.h"
#include "storage/fd.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 SnapBuildOnDiskNotChecksummedSize   offsetof(ReplicationSlotOnDisk, version)
 
#define SnapBuildOnDiskChecksummedSize   sizeof(ReplicationSlotOnDisk) - SnapBuildOnDiskNotChecksummedSize
 
#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 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 *path, int elevel)
 
Size ReplicationSlotsShmemSize (void)
 
void ReplicationSlotsShmemInit (void)
 
bool ReplicationSlotValidateName (const char *name, int elevel)
 
void ReplicationSlotCreate (const char *name, bool db_specific, ReplicationSlotPersistency persistency)
 
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 ReplicationSlotReserveWal (void)
 
void CheckPointReplicationSlots (void)
 
void StartupReplicationSlots (void)
 

Variables

ReplicationSlotCtlDataReplicationSlotCtl = NULL
 
ReplicationSlotMyReplicationSlot = NULL
 
int max_replication_slots = 0
 

Macro Definition Documentation

◆ ReplicationSlotOnDiskConstantSize

#define ReplicationSlotOnDiskConstantSize   offsetof(ReplicationSlotOnDisk, slotdata)

Definition at line 77 of file slot.c.

Referenced by RestoreSlotFromDisk().

◆ ReplicationSlotOnDiskV2Size

#define ReplicationSlotOnDiskV2Size   sizeof(ReplicationSlotOnDisk) - ReplicationSlotOnDiskConstantSize

Definition at line 86 of file slot.c.

Referenced by RestoreSlotFromDisk(), and SaveSlotToPath().

◆ SLOT_MAGIC

#define SLOT_MAGIC   0x1051CA1 /* format identifier */

Definition at line 89 of file slot.c.

Referenced by RestoreSlotFromDisk(), and SaveSlotToPath().

◆ SLOT_VERSION

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

Definition at line 90 of file slot.c.

Referenced by RestoreSlotFromDisk(), and SaveSlotToPath().

◆ SnapBuildOnDiskChecksummedSize

#define SnapBuildOnDiskChecksummedSize   sizeof(ReplicationSlotOnDisk) - SnapBuildOnDiskNotChecksummedSize

Definition at line 83 of file slot.c.

Referenced by RestoreSlotFromDisk(), and SaveSlotToPath().

◆ SnapBuildOnDiskNotChecksummedSize

#define SnapBuildOnDiskNotChecksummedSize   offsetof(ReplicationSlotOnDisk, version)

Definition at line 80 of file slot.c.

Referenced by RestoreSlotFromDisk(), and SaveSlotToPath().

Typedef Documentation

◆ ReplicationSlotOnDisk

Function Documentation

◆ CheckPointReplicationSlots()

void CheckPointReplicationSlots ( void  )

Definition at line 1062 of file slot.c.

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

Referenced by CheckPointGuts().

1063 {
1064  int i;
1065 
1066  elog(DEBUG1, "performing replication slot checkpoint");
1067 
1068  /*
1069  * Prevent any slot from being created/dropped while we're active. As we
1070  * explicitly do *not* want to block iterating over replication_slots or
1071  * acquiring a slot we cannot take the control lock - but that's OK,
1072  * because holding ReplicationSlotAllocationLock is strictly stronger, and
1073  * enough to guarantee that nobody can change the in_use bits on us.
1074  */
1075  LWLockAcquire(ReplicationSlotAllocationLock, LW_SHARED);
1076 
1077  for (i = 0; i < max_replication_slots; i++)
1078  {
1080  char path[MAXPGPATH];
1081 
1082  if (!s->in_use)
1083  continue;
1084 
1085  /* save the slot to disk, locking is handled in SaveSlotToPath() */
1086  sprintf(path, "pg_replslot/%s", NameStr(s->data.name));
1087  SaveSlotToPath(s, path, LOG);
1088  }
1089  LWLockRelease(ReplicationSlotAllocationLock);
1090 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
#define DEBUG1
Definition: elog.h:25
ReplicationSlotPersistentData data
Definition: slot.h:133
#define LOG
Definition: elog.h:26
static void SaveSlotToPath(ReplicationSlot *slot, const char *path, int elevel)
Definition: slot.c:1218
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define MAXPGPATH
bool in_use
Definition: slot.h:109
int max_replication_slots
Definition: slot.c:99
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
int i
#define NameStr(name)
Definition: c.h:576
ReplicationSlot replication_slots[1]
Definition: slot.h:166
#define elog
Definition: elog.h:219

◆ CheckSlotRequirements()

void CheckSlotRequirements ( void  )

Definition at line 965 of file slot.c.

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

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

966 {
967  if (max_replication_slots == 0)
968  ereport(ERROR,
969  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
970  (errmsg("replication slots can only be used if max_replication_slots > 0"))));
971 
973  ereport(ERROR,
974  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
975  errmsg("replication slots can only be used if wal_level >= replica")));
976 }
int wal_level
Definition: xlog.c:104
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int max_replication_slots
Definition: slot.c:99
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ CreateSlotOnDisk()

static void CreateSlotOnDisk ( ReplicationSlot slot)
static

Definition at line 1157 of file slot.c.

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(), START_CRIT_SECTION, and stat.

Referenced by ReplicationSlotCreate().

1158 {
1159  char tmppath[MAXPGPATH];
1160  char path[MAXPGPATH];
1161  struct stat st;
1162 
1163  /*
1164  * No need to take out the io_in_progress_lock, nobody else can see this
1165  * slot yet, so nobody else will write. We're reusing SaveSlotToPath which
1166  * takes out the lock, if we'd take the lock here, we'd deadlock.
1167  */
1168 
1169  sprintf(path, "pg_replslot/%s", NameStr(slot->data.name));
1170  sprintf(tmppath, "pg_replslot/%s.tmp", NameStr(slot->data.name));
1171 
1172  /*
1173  * It's just barely possible that some previous effort to create or drop a
1174  * slot with this name left a temp directory lying around. If that seems
1175  * to be the case, try to remove it. If the rmtree() fails, we'll error
1176  * out at the MakePGDirectory() below, so we don't bother checking
1177  * success.
1178  */
1179  if (stat(tmppath, &st) == 0 && S_ISDIR(st.st_mode))
1180  rmtree(tmppath, true);
1181 
1182  /* Create and fsync the temporary slot directory. */
1183  if (MakePGDirectory(tmppath) < 0)
1184  ereport(ERROR,
1186  errmsg("could not create directory \"%s\": %m",
1187  tmppath)));
1188  fsync_fname(tmppath, true);
1189 
1190  /* Write the actual state file. */
1191  slot->dirty = true; /* signal that we really need to write */
1192  SaveSlotToPath(slot, tmppath, ERROR);
1193 
1194  /* Rename the directory into place. */
1195  if (rename(tmppath, path) != 0)
1196  ereport(ERROR,
1198  errmsg("could not rename file \"%s\" to \"%s\": %m",
1199  tmppath, path)));
1200 
1201  /*
1202  * If we'd now fail - really unlikely - we wouldn't know whether this slot
1203  * would persist after an OS crash or not - so, force a restart. The
1204  * restart would try to fsync this again till it works.
1205  */
1207 
1208  fsync_fname(path, true);
1209  fsync_fname("pg_replslot", true);
1210 
1211  END_CRIT_SECTION();
1212 }
#define END_CRIT_SECTION()
Definition: miscadmin.h:133
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:575
#define START_CRIT_SECTION()
Definition: miscadmin.h:131
ReplicationSlotPersistentData data
Definition: slot.h:133
static void SaveSlotToPath(ReplicationSlot *slot, const char *path, int elevel)
Definition: slot.c:1218
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
#define stat(a, b)
Definition: win32_port.h:266
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:36
int MakePGDirectory(const char *directoryName)
Definition: fd.c:3571
#define S_ISDIR(m)
Definition: win32_port.h:307
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define NameStr(name)
Definition: c.h:576
bool dirty
Definition: slot.h:116

◆ ReplicationSlotAcquire()

void ReplicationSlotAcquire ( const char *  name,
bool  nowait 
)

Definition at line 330 of file slot.c.

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

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

331 {
332  ReplicationSlot *slot;
333  int active_pid;
334  int i;
335 
336 retry:
337  Assert(MyReplicationSlot == NULL);
338 
339  /*
340  * Search for the named slot and mark it active if we find it. If the
341  * slot is already active, we exit the loop with active_pid set to the PID
342  * of the backend that owns it.
343  */
344  active_pid = 0;
345  slot = NULL;
346  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
347  for (i = 0; i < max_replication_slots; i++)
348  {
350 
351  if (s->in_use && strcmp(name, NameStr(s->data.name)) == 0)
352  {
353  /*
354  * This is the slot we want. We don't know yet if it's active, so
355  * get ready to sleep on it in case it is. (We may end up not
356  * sleeping, but we don't want to do this while holding the
357  * spinlock.)
358  */
360 
361  SpinLockAcquire(&s->mutex);
362 
363  active_pid = s->active_pid;
364  if (active_pid == 0)
365  active_pid = s->active_pid = MyProcPid;
366 
367  SpinLockRelease(&s->mutex);
368  slot = s;
369 
370  break;
371  }
372  }
373  LWLockRelease(ReplicationSlotControlLock);
374 
375  /* If we did not find the slot, error out. */
376  if (slot == NULL)
377  ereport(ERROR,
378  (errcode(ERRCODE_UNDEFINED_OBJECT),
379  errmsg("replication slot \"%s\" does not exist", name)));
380 
381  /*
382  * If we found the slot but it's already active in another backend, we
383  * either error out or retry after a short wait, as caller specified.
384  */
385  if (active_pid != MyProcPid)
386  {
387  if (nowait)
388  ereport(ERROR,
389  (errcode(ERRCODE_OBJECT_IN_USE),
390  errmsg("replication slot \"%s\" is active for PID %d",
391  name, active_pid)));
392 
393  /* Wait here until we get signaled, and then restart */
397  goto retry;
398  }
399  else
400  ConditionVariableCancelSleep(); /* no sleep needed after all */
401 
402  /* Let everybody know we've modified this slot */
404 
405  /* We made this slot active, so it's ours now. */
406  MyReplicationSlot = slot;
407 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
int MyProcPid
Definition: globals.c:40
void ConditionVariableBroadcast(ConditionVariable *cv)
int errcode(int sqlerrcode)
Definition: elog.c:575
ReplicationSlotPersistentData data
Definition: slot.h:133
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ConditionVariableCancelSleep(void)
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
bool in_use
Definition: slot.h:109
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
int max_replication_slots
Definition: slot.c:99
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
#define Assert(condition)
Definition: c.h:699
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
ConditionVariable active_cv
Definition: slot.h:139
const char * name
Definition: encode.c:521
int errmsg(const char *fmt,...)
Definition: elog.c:797
pid_t active_pid
Definition: slot.h:112
int i
#define NameStr(name)
Definition: c.h:576
ReplicationSlot replication_slots[1]
Definition: slot.h:166
slock_t mutex
Definition: slot.h:106

◆ ReplicationSlotCleanup()

void ReplicationSlotCleanup ( void  )

Definition at line 471 of file slot.c.

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

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

472 {
473  int i;
474 
475  Assert(MyReplicationSlot == NULL);
476 
477 restart:
478  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
479  for (i = 0; i < max_replication_slots; i++)
480  {
482 
483  if (!s->in_use)
484  continue;
485 
486  SpinLockAcquire(&s->mutex);
487  if (s->active_pid == MyProcPid)
488  {
490  SpinLockRelease(&s->mutex);
491  LWLockRelease(ReplicationSlotControlLock); /* avoid deadlock */
492 
494 
496  goto restart;
497  }
498  else
499  SpinLockRelease(&s->mutex);
500  }
501 
502  LWLockRelease(ReplicationSlotControlLock);
503 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
int MyProcPid
Definition: globals.c:40
ReplicationSlotPersistency persistency
Definition: slot.h:54
void ConditionVariableBroadcast(ConditionVariable *cv)
static void ReplicationSlotDropPtr(ReplicationSlot *slot)
Definition: slot.c:539
ReplicationSlotPersistentData data
Definition: slot.h:133
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define SpinLockAcquire(lock)
Definition: spin.h:62
bool in_use
Definition: slot.h:109
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
int max_replication_slots
Definition: slot.c:99
#define Assert(condition)
Definition: c.h:699
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
ConditionVariable active_cv
Definition: slot.h:139
pid_t active_pid
Definition: slot.h:112
int i
ReplicationSlot replication_slots[1]
Definition: slot.h:166
slock_t mutex
Definition: slot.h:106

◆ ReplicationSlotCreate()

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

Definition at line 221 of file slot.c.

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

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

223 {
224  ReplicationSlot *slot = NULL;
225  int i;
226 
227  Assert(MyReplicationSlot == NULL);
228 
230 
231  /*
232  * If some other backend ran this code concurrently with us, we'd likely
233  * both allocate the same slot, and that would be bad. We'd also be at
234  * risk of missing a name collision. Also, we don't want to try to create
235  * a new slot while somebody's busy cleaning up an old one, because we
236  * might both be monkeying with the same directory.
237  */
238  LWLockAcquire(ReplicationSlotAllocationLock, LW_EXCLUSIVE);
239 
240  /*
241  * Check for name collision, and identify an allocatable slot. We need to
242  * hold ReplicationSlotControlLock in shared mode for this, so that nobody
243  * else can change the in_use flags while we're looking at them.
244  */
245  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
246  for (i = 0; i < max_replication_slots; i++)
247  {
249 
250  if (s->in_use && strcmp(name, NameStr(s->data.name)) == 0)
251  ereport(ERROR,
253  errmsg("replication slot \"%s\" already exists", name)));
254  if (!s->in_use && slot == NULL)
255  slot = s;
256  }
257  LWLockRelease(ReplicationSlotControlLock);
258 
259  /* If all slots are in use, we're out of luck. */
260  if (slot == NULL)
261  ereport(ERROR,
262  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
263  errmsg("all replication slots are in use"),
264  errhint("Free one or increase max_replication_slots.")));
265 
266  /*
267  * Since this slot is not in use, nobody should be looking at any part of
268  * it other than the in_use field unless they're trying to allocate it.
269  * And since we hold ReplicationSlotAllocationLock, nobody except us can
270  * be doing that. So it's safe to initialize the slot.
271  */
272  Assert(!slot->in_use);
273  Assert(slot->active_pid == 0);
274 
275  /* first initialize persistent data */
276  memset(&slot->data, 0, sizeof(ReplicationSlotPersistentData));
278  slot->data.database = db_specific ? MyDatabaseId : InvalidOid;
279  slot->data.persistency = persistency;
280 
281  /* and then data only present in shared memory */
282  slot->just_dirtied = false;
283  slot->dirty = false;
290 
291  /*
292  * Create the slot on disk. We haven't actually marked the slot allocated
293  * yet, so no special cleanup is required if this errors out.
294  */
295  CreateSlotOnDisk(slot);
296 
297  /*
298  * We need to briefly prevent any other backend from iterating over the
299  * slots while we flip the in_use flag. We also need to set the active
300  * flag while holding the ControlLock as otherwise a concurrent
301  * SlotAcquire() could acquire the slot as well.
302  */
303  LWLockAcquire(ReplicationSlotControlLock, LW_EXCLUSIVE);
304 
305  slot->in_use = true;
306 
307  /* We can now mark the slot active, and that makes it our slot. */
308  SpinLockAcquire(&slot->mutex);
309  Assert(slot->active_pid == 0);
310  slot->active_pid = MyProcPid;
311  SpinLockRelease(&slot->mutex);
312  MyReplicationSlot = slot;
313 
314  LWLockRelease(ReplicationSlotControlLock);
315 
316  /*
317  * Now that the slot has been marked as in_use and active, it's safe to
318  * let somebody else try to allocate a slot.
319  */
320  LWLockRelease(ReplicationSlotAllocationLock);
321 
322  /* Let everybody know we've modified this slot */
324 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
TransactionId candidate_catalog_xmin
Definition: slot.h:148
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
int MyProcPid
Definition: globals.c:40
int errhint(const char *fmt,...)
Definition: elog.c:987
ReplicationSlotPersistency persistency
Definition: slot.h:54
void ConditionVariableBroadcast(ConditionVariable *cv)
int errcode(int sqlerrcode)
Definition: elog.c:575
ReplicationSlotPersistentData data
Definition: slot.h:133
static void CreateSlotOnDisk(ReplicationSlot *slot)
Definition: slot.c:1157
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define NAMEDATALEN
bool ReplicationSlotValidateName(const char *name, int elevel)
Definition: slot.c:174
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define ERROR
Definition: elog.h:43
TransactionId effective_xmin
Definition: slot.h:129
XLogRecPtr candidate_restart_valid
Definition: slot.h:150
#define InvalidTransactionId
Definition: transam.h:31
#define ereport(elevel, rest)
Definition: elog.h:122
bool in_use
Definition: slot.h:109
#define SpinLockRelease(lock)
Definition: spin.h:64
bool just_dirtied
Definition: slot.h:115
TransactionId effective_catalog_xmin
Definition: slot.h:130
Oid MyDatabaseId
Definition: globals.c:84
#define InvalidOid
Definition: postgres_ext.h:36
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
int max_replication_slots
Definition: slot.c:99
#define Assert(condition)
Definition: c.h:699
#define StrNCpy(dst, src, len)
Definition: c.h:881
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
ConditionVariable active_cv
Definition: slot.h:139
const char * name
Definition: encode.c:521
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:149
int errmsg(const char *fmt,...)
Definition: elog.c:797
pid_t active_pid
Definition: slot.h:112
int i
#define NameStr(name)
Definition: c.h:576
ReplicationSlot replication_slots[1]
Definition: slot.h:166
slock_t mutex
Definition: slot.h:106
#define ERRCODE_DUPLICATE_OBJECT
Definition: streamutil.c:32
bool dirty
Definition: slot.h:116
XLogRecPtr candidate_restart_lsn
Definition: slot.h:151

◆ ReplicationSlotDrop()

void ReplicationSlotDrop ( const char *  name,
bool  nowait 
)

Definition at line 509 of file slot.c.

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

Referenced by DropReplicationSlot(), and pg_drop_replication_slot().

510 {
511  Assert(MyReplicationSlot == NULL);
512 
513  ReplicationSlotAcquire(name, nowait);
514 
516 }
void ReplicationSlotAcquire(const char *name, bool nowait)
Definition: slot.c:330
static void ReplicationSlotDropAcquired(void)
Definition: slot.c:522
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:699
const char * name
Definition: encode.c:521

◆ ReplicationSlotDropAcquired()

static void ReplicationSlotDropAcquired ( void  )
static

Definition at line 522 of file slot.c.

References Assert, MyReplicationSlot, and ReplicationSlotDropPtr().

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

523 {
525 
526  Assert(MyReplicationSlot != NULL);
527 
528  /* slot isn't acquired anymore */
529  MyReplicationSlot = NULL;
530 
532 }
static void ReplicationSlotDropPtr(ReplicationSlot *slot)
Definition: slot.c:539
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:699

◆ ReplicationSlotDropPtr()

static void ReplicationSlotDropPtr ( ReplicationSlot slot)
static

Definition at line 539 of file slot.c.

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, ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), rmtree(), RS_PERSISTENT, SpinLockAcquire, SpinLockRelease, START_CRIT_SECTION, and WARNING.

Referenced by ReplicationSlotCleanup(), and ReplicationSlotDropAcquired().

540 {
541  char path[MAXPGPATH];
542  char tmppath[MAXPGPATH];
543 
544  /*
545  * If some other backend ran this code concurrently with us, we might try
546  * to delete a slot with a certain name while someone else was trying to
547  * create a slot with the same name.
548  */
549  LWLockAcquire(ReplicationSlotAllocationLock, LW_EXCLUSIVE);
550 
551  /* Generate pathnames. */
552  sprintf(path, "pg_replslot/%s", NameStr(slot->data.name));
553  sprintf(tmppath, "pg_replslot/%s.tmp", NameStr(slot->data.name));
554 
555  /*
556  * Rename the slot directory on disk, so that we'll no longer recognize
557  * this as a valid slot. Note that if this fails, we've got to mark the
558  * slot inactive before bailing out. If we're dropping an ephemeral or a
559  * temporary slot, we better never fail hard as the caller won't expect
560  * the slot to survive and this might get called during error handling.
561  */
562  if (rename(path, tmppath) == 0)
563  {
564  /*
565  * We need to fsync() the directory we just renamed and its parent to
566  * make sure that our changes are on disk in a crash-safe fashion. If
567  * fsync() fails, we can't be sure whether the changes are on disk or
568  * not. For now, we handle that by panicking;
569  * StartupReplicationSlots() will try to straighten it out after
570  * restart.
571  */
573  fsync_fname(tmppath, true);
574  fsync_fname("pg_replslot", true);
576  }
577  else
578  {
579  bool fail_softly = slot->data.persistency != RS_PERSISTENT;
580 
581  SpinLockAcquire(&slot->mutex);
582  slot->active_pid = 0;
583  SpinLockRelease(&slot->mutex);
584 
585  /* wake up anyone waiting on this slot */
587 
588  ereport(fail_softly ? WARNING : ERROR,
590  errmsg("could not rename file \"%s\" to \"%s\": %m",
591  path, tmppath)));
592  }
593 
594  /*
595  * The slot is definitely gone. Lock out concurrent scans of the array
596  * long enough to kill it. It's OK to clear the active PID here without
597  * grabbing the mutex because nobody else can be scanning the array here,
598  * and nobody can be attached to this slot and thus access it without
599  * scanning the array.
600  *
601  * Also wake up processes waiting for it.
602  */
603  LWLockAcquire(ReplicationSlotControlLock, LW_EXCLUSIVE);
604  slot->active_pid = 0;
605  slot->in_use = false;
606  LWLockRelease(ReplicationSlotControlLock);
608 
609  /*
610  * Slot is dead and doesn't prevent resource removal anymore, recompute
611  * limits.
612  */
615 
616  /*
617  * If removing the directory fails, the worst thing that will happen is
618  * that the user won't be able to create a new slot with the same name
619  * until the next server restart. We warn about it, but that's all.
620  */
621  if (!rmtree(tmppath, true))
624  errmsg("could not remove directory \"%s\"", tmppath)));
625 
626  /*
627  * We release this at the very end, so that nobody starts trying to create
628  * a slot while we're still cleaning up the detritus of the old one.
629  */
630  LWLockRelease(ReplicationSlotAllocationLock);
631 }
#define END_CRIT_SECTION()
Definition: miscadmin.h:133
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:575
ReplicationSlotPersistency persistency
Definition: slot.h:54
#define START_CRIT_SECTION()
Definition: miscadmin.h:131
void ConditionVariableBroadcast(ConditionVariable *cv)
ReplicationSlotPersistentData data
Definition: slot.h:133
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define SpinLockAcquire(lock)
Definition: spin.h:62
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:741
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:36
bool in_use
Definition: slot.h:109
#define SpinLockRelease(lock)
Definition: spin.h:64
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
ConditionVariable active_cv
Definition: slot.h:139
int errmsg(const char *fmt,...)
Definition: elog.c:797
pid_t active_pid
Definition: slot.h:112
#define NameStr(name)
Definition: c.h:576
slock_t mutex
Definition: slot.h:106
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:695

◆ ReplicationSlotMarkDirty()

◆ ReplicationSlotPersist()

void ReplicationSlotPersist ( void  )

Definition at line 673 of file slot.c.

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

Referenced by CreateReplicationSlot(), and pg_create_logical_replication_slot().

674 {
676 
677  Assert(slot != NULL);
679 
680  SpinLockAcquire(&slot->mutex);
682  SpinLockRelease(&slot->mutex);
683 
686 }
ReplicationSlotPersistency persistency
Definition: slot.h:54
void ReplicationSlotSave(void)
Definition: slot.c:638
ReplicationSlotPersistentData data
Definition: slot.h:133
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:699
slock_t mutex
Definition: slot.h:106
void ReplicationSlotMarkDirty(void)
Definition: slot.c:656

◆ ReplicationSlotRelease()

void ReplicationSlotRelease ( void  )

Definition at line 416 of file slot.c.

References ReplicationSlot::active_cv, ReplicationSlot::active_pid, Assert, ConditionVariableBroadcast(), ReplicationSlot::data, ReplicationSlot::effective_xmin, InvalidTransactionId, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), ReplicationSlot::mutex, MyPgXact, MyReplicationSlot, ReplicationSlotPersistentData::persistency, PROC_IN_LOGICAL_DECODING, ReplicationSlotDropAcquired(), ReplicationSlotsComputeRequiredXmin(), RS_EPHEMERAL, RS_PERSISTENT, SpinLockAcquire, SpinLockRelease, TransactionIdIsValid, PGXACT::vacuumFlags, and ReplicationSlotPersistentData::xmin.

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

417 {
419 
420  Assert(slot != NULL && slot->active_pid != 0);
421 
422  if (slot->data.persistency == RS_EPHEMERAL)
423  {
424  /*
425  * Delete the slot. There is no !PANIC case where this is allowed to
426  * fail, all that may happen is an incomplete cleanup of the on-disk
427  * data.
428  */
430  }
431 
432  /*
433  * If slot needed to temporarily restrain both data and catalog xmin to
434  * create the catalog snapshot, remove that temporary constraint.
435  * Snapshots can only be exported while the initial snapshot is still
436  * acquired.
437  */
438  if (!TransactionIdIsValid(slot->data.xmin) &&
440  {
441  SpinLockAcquire(&slot->mutex);
443  SpinLockRelease(&slot->mutex);
445  }
446 
447  if (slot->data.persistency == RS_PERSISTENT)
448  {
449  /*
450  * Mark persistent slot inactive. We're not freeing it, just
451  * disconnecting, but wake up others that may be waiting for it.
452  */
453  SpinLockAcquire(&slot->mutex);
454  slot->active_pid = 0;
455  SpinLockRelease(&slot->mutex);
457  }
458 
459  MyReplicationSlot = NULL;
460 
461  /* might not have been set when we've been a plain slot */
462  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
464  LWLockRelease(ProcArrayLock);
465 }
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:57
ReplicationSlotPersistency persistency
Definition: slot.h:54
void ConditionVariableBroadcast(ConditionVariable *cv)
ReplicationSlotPersistentData data
Definition: slot.h:133
PGXACT * MyPgXact
Definition: proc.c:68
uint8 vacuumFlags
Definition: proc.h:230
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define SpinLockAcquire(lock)
Definition: spin.h:62
static void ReplicationSlotDropAcquired(void)
Definition: slot.c:522
TransactionId effective_xmin
Definition: slot.h:129
#define InvalidTransactionId
Definition: transam.h:31
TransactionId xmin
Definition: slot.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:699
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
ConditionVariable active_cv
Definition: slot.h:139
pid_t active_pid
Definition: slot.h:112
slock_t mutex
Definition: slot.h:106
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:695

◆ ReplicationSlotReserveWal()

void ReplicationSlotReserveWal ( void  )

Definition at line 985 of file slot.c.

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

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

986 {
988 
989  Assert(slot != NULL);
991 
992  /*
993  * The replication slot mechanism is used to prevent removal of required
994  * WAL. As there is no interlock between this routine and checkpoints, WAL
995  * segments could concurrently be removed when a now stale return value of
996  * ReplicationSlotsComputeRequiredLSN() is used. In the unlikely case that
997  * this happens we'll just retry.
998  */
999  while (true)
1000  {
1001  XLogSegNo segno;
1002  XLogRecPtr restart_lsn;
1003 
1004  /*
1005  * For logical slots log a standby snapshot and start logical decoding
1006  * at exactly that position. That allows the slot to start up more
1007  * quickly.
1008  *
1009  * That's not needed (or indeed helpful) for physical slots as they'll
1010  * start replay at the last logged checkpoint anyway. Instead return
1011  * the location of the last redo LSN. While that slightly increases
1012  * the chance that we have to retry, it's where a base backup has to
1013  * start replay at.
1014  */
1015  if (!RecoveryInProgress() && SlotIsLogical(slot))
1016  {
1017  XLogRecPtr flushptr;
1018 
1019  /* start at current insert position */
1020  restart_lsn = GetXLogInsertRecPtr();
1021  SpinLockAcquire(&slot->mutex);
1022  slot->data.restart_lsn = restart_lsn;
1023  SpinLockRelease(&slot->mutex);
1024 
1025  /* make sure we have enough information to start */
1026  flushptr = LogStandbySnapshot();
1027 
1028  /* and make sure it's fsynced to disk */
1029  XLogFlush(flushptr);
1030  }
1031  else
1032  {
1033  restart_lsn = GetRedoRecPtr();
1034  SpinLockAcquire(&slot->mutex);
1035  slot->data.restart_lsn = restart_lsn;
1036  SpinLockRelease(&slot->mutex);
1037  }
1038 
1039  /* prevent WAL removal as fast as possible */
1041 
1042  /*
1043  * If all required WAL is still there, great, otherwise retry. The
1044  * slot should prevent further removal of WAL, unless there's a
1045  * concurrent ReplicationSlotsComputeRequiredLSN() after we've written
1046  * the new restart_lsn above, so normally we should never need to loop
1047  * more than twice.
1048  */
1050  if (XLogGetLastRemovedSegno() < segno)
1051  break;
1052  }
1053 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
int wal_segment_size
Definition: xlog.c:113
ReplicationSlotPersistentData data
Definition: slot.h:133
bool RecoveryInProgress(void)
Definition: xlog.c:7949
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2783
#define SpinLockAcquire(lock)
Definition: spin.h:62
XLogSegNo XLogGetLastRemovedSegno(void)
Definition: xlog.c:3825
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:741
XLogRecPtr LogStandbySnapshot(void)
Definition: standby.c:891
XLogRecPtr GetXLogInsertRecPtr(void)
Definition: xlog.c:11213
uint64 XLogSegNo
Definition: xlogdefs.h:34
#define SlotIsLogical(slot)
Definition: slot.h:155
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:699
XLogRecPtr restart_lsn
Definition: slot.h:73
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:8222
slock_t mutex
Definition: slot.h:106
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)

◆ ReplicationSlotSave()

void ReplicationSlotSave ( void  )

Definition at line 638 of file slot.c.

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

Referenced by CreateInitDecodingContext(), CreateReplicationSlot(), LogicalConfirmReceivedLocation(), pg_create_physical_replication_slot(), pg_replication_slot_advance(), and ReplicationSlotPersist().

639 {
640  char path[MAXPGPATH];
641 
642  Assert(MyReplicationSlot != NULL);
643 
644  sprintf(path, "pg_replslot/%s", NameStr(MyReplicationSlot->data.name));
646 }
ReplicationSlotPersistentData data
Definition: slot.h:133
static void SaveSlotToPath(ReplicationSlot *slot, const char *path, int elevel)
Definition: slot.c:1218
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:699
#define NameStr(name)
Definition: c.h:576

◆ ReplicationSlotsComputeLogicalRestartLSN()

XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN ( void  )

Definition at line 784 of file slot.c.

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

Referenced by CheckPointLogicalRewriteHeap(), and CheckPointSnapBuild().

785 {
786  XLogRecPtr result = InvalidXLogRecPtr;
787  int i;
788 
789  if (max_replication_slots <= 0)
790  return InvalidXLogRecPtr;
791 
792  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
793 
794  for (i = 0; i < max_replication_slots; i++)
795  {
796  ReplicationSlot *s;
797  XLogRecPtr restart_lsn;
798 
800 
801  /* cannot change while ReplicationSlotCtlLock is held */
802  if (!s->in_use)
803  continue;
804 
805  /* we're only interested in logical slots */
806  if (!SlotIsLogical(s))
807  continue;
808 
809  /* read once, it's ok if it increases while we're checking */
810  SpinLockAcquire(&s->mutex);
811  restart_lsn = s->data.restart_lsn;
812  SpinLockRelease(&s->mutex);
813 
814  if (result == InvalidXLogRecPtr ||
815  restart_lsn < result)
816  result = restart_lsn;
817  }
818 
819  LWLockRelease(ReplicationSlotControlLock);
820 
821  return result;
822 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:133
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SlotIsLogical(slot)
Definition: slot.h:155
bool in_use
Definition: slot.h:109
#define SpinLockRelease(lock)
Definition: spin.h:64
int max_replication_slots
Definition: slot.c:99
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr restart_lsn
Definition: slot.h:73
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
int i
ReplicationSlot replication_slots[1]
Definition: slot.h:166
slock_t mutex
Definition: slot.h:106

◆ ReplicationSlotsComputeRequiredLSN()

void ReplicationSlotsComputeRequiredLSN ( void  )

Definition at line 741 of file slot.c.

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

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

742 {
743  int i;
744  XLogRecPtr min_required = InvalidXLogRecPtr;
745 
746  Assert(ReplicationSlotCtl != NULL);
747 
748  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
749  for (i = 0; i < max_replication_slots; i++)
750  {
752  XLogRecPtr restart_lsn;
753 
754  if (!s->in_use)
755  continue;
756 
757  SpinLockAcquire(&s->mutex);
758  restart_lsn = s->data.restart_lsn;
759  SpinLockRelease(&s->mutex);
760 
761  if (restart_lsn != InvalidXLogRecPtr &&
762  (min_required == InvalidXLogRecPtr ||
763  restart_lsn < min_required))
764  min_required = restart_lsn;
765  }
766  LWLockRelease(ReplicationSlotControlLock);
767 
768  XLogSetReplicationSlotMinimumLSN(min_required);
769 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:133
void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
Definition: xlog.c:2674
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define SpinLockAcquire(lock)
Definition: spin.h:62
bool in_use
Definition: slot.h:109
#define SpinLockRelease(lock)
Definition: spin.h:64
int max_replication_slots
Definition: slot.c:99
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:699
XLogRecPtr restart_lsn
Definition: slot.h:73
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
int i
ReplicationSlot replication_slots[1]
Definition: slot.h:166
slock_t mutex
Definition: slot.h:106

◆ ReplicationSlotsComputeRequiredXmin()

void ReplicationSlotsComputeRequiredXmin ( bool  already_locked)

Definition at line 695 of file slot.c.

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

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

696 {
697  int i;
699  TransactionId agg_catalog_xmin = InvalidTransactionId;
700 
701  Assert(ReplicationSlotCtl != NULL);
702 
703  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
704 
705  for (i = 0; i < max_replication_slots; i++)
706  {
708  TransactionId effective_xmin;
709  TransactionId effective_catalog_xmin;
710 
711  if (!s->in_use)
712  continue;
713 
714  SpinLockAcquire(&s->mutex);
715  effective_xmin = s->effective_xmin;
716  effective_catalog_xmin = s->effective_catalog_xmin;
717  SpinLockRelease(&s->mutex);
718 
719  /* check the data xmin */
720  if (TransactionIdIsValid(effective_xmin) &&
721  (!TransactionIdIsValid(agg_xmin) ||
722  TransactionIdPrecedes(effective_xmin, agg_xmin)))
723  agg_xmin = effective_xmin;
724 
725  /* check the catalog xmin */
726  if (TransactionIdIsValid(effective_catalog_xmin) &&
727  (!TransactionIdIsValid(agg_catalog_xmin) ||
728  TransactionIdPrecedes(effective_catalog_xmin, agg_catalog_xmin)))
729  agg_catalog_xmin = effective_catalog_xmin;
730  }
731 
732  LWLockRelease(ReplicationSlotControlLock);
733 
734  ProcArraySetReplicationSlotXmin(agg_xmin, agg_catalog_xmin, already_locked);
735 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
uint32 TransactionId
Definition: c.h:474
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define SpinLockAcquire(lock)
Definition: spin.h:62
TransactionId effective_xmin
Definition: slot.h:129
#define InvalidTransactionId
Definition: transam.h:31
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
bool in_use
Definition: slot.h:109
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:130
int max_replication_slots
Definition: slot.c:99
#define Assert(condition)
Definition: c.h:699
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
int i
void ProcArraySetReplicationSlotXmin(TransactionId xmin, TransactionId catalog_xmin, bool already_locked)
Definition: procarray.c:2972
ReplicationSlot replication_slots[1]
Definition: slot.h:166
slock_t mutex
Definition: slot.h:106
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ ReplicationSlotsCountDBSlots()

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

Definition at line 833 of file slot.c.

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

Referenced by dropdb().

834 {
835  int i;
836 
837  *nslots = *nactive = 0;
838 
839  if (max_replication_slots <= 0)
840  return false;
841 
842  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
843  for (i = 0; i < max_replication_slots; i++)
844  {
845  ReplicationSlot *s;
846 
848 
849  /* cannot change while ReplicationSlotCtlLock is held */
850  if (!s->in_use)
851  continue;
852 
853  /* only logical slots are database specific, skip */
854  if (!SlotIsLogical(s))
855  continue;
856 
857  /* not our database, skip */
858  if (s->data.database != dboid)
859  continue;
860 
861  /* count slots with spinlock held */
862  SpinLockAcquire(&s->mutex);
863  (*nslots)++;
864  if (s->active_pid != 0)
865  (*nactive)++;
866  SpinLockRelease(&s->mutex);
867  }
868  LWLockRelease(ReplicationSlotControlLock);
869 
870  if (*nslots > 0)
871  return true;
872  return false;
873 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
ReplicationSlotPersistentData data
Definition: slot.h:133
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SlotIsLogical(slot)
Definition: slot.h:155
bool in_use
Definition: slot.h:109
#define SpinLockRelease(lock)
Definition: spin.h:64
int max_replication_slots
Definition: slot.c:99
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
pid_t active_pid
Definition: slot.h:112
int i
ReplicationSlot replication_slots[1]
Definition: slot.h:166
slock_t mutex
Definition: slot.h:106

◆ ReplicationSlotsDropDBSlots()

void ReplicationSlotsDropDBSlots ( Oid  dboid)

Definition at line 889 of file slot.c.

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

Referenced by dbase_redo(), and dropdb().

890 {
891  int i;
892 
893  if (max_replication_slots <= 0)
894  return;
895 
896 restart:
897  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
898  for (i = 0; i < max_replication_slots; i++)
899  {
900  ReplicationSlot *s;
901  char *slotname;
902  int active_pid;
903 
905 
906  /* cannot change while ReplicationSlotCtlLock is held */
907  if (!s->in_use)
908  continue;
909 
910  /* only logical slots are database specific, skip */
911  if (!SlotIsLogical(s))
912  continue;
913 
914  /* not our database, skip */
915  if (s->data.database != dboid)
916  continue;
917 
918  /* acquire slot, so ReplicationSlotDropAcquired can be reused */
919  SpinLockAcquire(&s->mutex);
920  /* can't change while ReplicationSlotControlLock is held */
921  slotname = NameStr(s->data.name);
922  active_pid = s->active_pid;
923  if (active_pid == 0)
924  {
925  MyReplicationSlot = s;
926  s->active_pid = MyProcPid;
927  }
928  SpinLockRelease(&s->mutex);
929 
930  /*
931  * Even though we hold an exclusive lock on the database object a
932  * logical slot for that DB can still be active, e.g. if it's
933  * concurrently being dropped by a backend connected to another DB.
934  *
935  * That's fairly unlikely in practice, so we'll just bail out.
936  */
937  if (active_pid)
938  ereport(ERROR,
939  (errcode(ERRCODE_OBJECT_IN_USE),
940  errmsg("replication slot \"%s\" is active for PID %d",
941  slotname, active_pid)));
942 
943  /*
944  * To avoid duplicating ReplicationSlotDropAcquired() and to avoid
945  * holding ReplicationSlotControlLock over filesystem operations,
946  * release ReplicationSlotControlLock and use
947  * ReplicationSlotDropAcquired.
948  *
949  * As that means the set of slots could change, restart scan from the
950  * beginning each time we release the lock.
951  */
952  LWLockRelease(ReplicationSlotControlLock);
954  goto restart;
955  }
956  LWLockRelease(ReplicationSlotControlLock);
957 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
int MyProcPid
Definition: globals.c:40
int errcode(int sqlerrcode)
Definition: elog.c:575
ReplicationSlotPersistentData data
Definition: slot.h:133
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define SpinLockAcquire(lock)
Definition: spin.h:62
static void ReplicationSlotDropAcquired(void)
Definition: slot.c:522
#define ERROR
Definition: elog.h:43
#define SlotIsLogical(slot)
Definition: slot.h:155
#define ereport(elevel, rest)
Definition: elog.h:122
bool in_use
Definition: slot.h:109
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
int max_replication_slots
Definition: slot.c:99
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
int errmsg(const char *fmt,...)
Definition: elog.c:797
pid_t active_pid
Definition: slot.h:112
int i
#define NameStr(name)
Definition: c.h:576
ReplicationSlot replication_slots[1]
Definition: slot.h:166
slock_t mutex
Definition: slot.h:106

◆ ReplicationSlotsShmemInit()

void ReplicationSlotsShmemInit ( void  )

Definition at line 132 of file slot.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

133 {
134  bool found;
135 
136  if (max_replication_slots == 0)
137  return;
138 
140  ShmemInitStruct("ReplicationSlot Ctl", ReplicationSlotsShmemSize(),
141  &found);
142 
144  "replication_slot_io");
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);
161  }
162  }
163 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
Size ReplicationSlotsShmemSize(void)
Definition: slot.c:114
#define SpinLockInit(lock)
Definition: spin.h:60
#define MemSet(start, val, len)
Definition: c.h:908
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
Definition: lwlock.c:602
void ConditionVariableInit(ConditionVariable *cv)
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:677
int max_replication_slots
Definition: slot.c:99
ConditionVariable active_cv
Definition: slot.h:139
int i
ReplicationSlot replication_slots[1]
Definition: slot.h:166
slock_t mutex
Definition: slot.h:106
LWLock io_in_progress_lock
Definition: slot.h:136

◆ ReplicationSlotsShmemSize()

Size ReplicationSlotsShmemSize ( void  )

Definition at line 114 of file slot.c.

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

Referenced by CreateSharedMemoryAndSemaphores(), and ReplicationSlotsShmemInit().

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

◆ ReplicationSlotValidateName()

bool ReplicationSlotValidateName ( const char *  name,
int  elevel 
)

Definition at line 174 of file slot.c.

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

Referenced by readRecoveryCommandFile(), ReplicationSlotCreate(), and StartupReorderBuffer().

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

◆ RestoreSlotFromDisk()

static void RestoreSlotFromDisk ( const char *  name)
static

Definition at line 1340 of file slot.c.

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, DEBUG1, ReplicationSlot::effective_catalog_xmin, ReplicationSlot::effective_xmin, elog, END_CRIT_SECTION, EQ_CRC32C, ereport, errcode_for_file_access(), errhint(), errmsg(), fd(), FIN_CRC32C, fsync_fname(), i, ReplicationSlot::in_use, INIT_CRC32C, InvalidTransactionId, InvalidXLogRecPtr, ReplicationSlotOnDisk::length, ReplicationSlotOnDisk::magic, max_replication_slots, MAXPGPATH, OpenTransientFile(), PANIC, ReplicationSlotPersistentData::persistency, PG_BINARY, pg_fsync(), pgstat_report_wait_end(), pgstat_report_wait_start(), read, ReplicationSlotCtlData::replication_slots, ReplicationSlotOnDiskConstantSize, ReplicationSlotOnDiskV2Size, rmtree(), RS_PERSISTENT, SLOT_MAGIC, SLOT_VERSION, ReplicationSlotOnDisk::slotdata, SnapBuildOnDiskChecksummedSize, SnapBuildOnDiskNotChecksummedSize, START_CRIT_SECTION, ReplicationSlotOnDisk::version, WAIT_EVENT_REPLICATION_SLOT_READ, WAIT_EVENT_REPLICATION_SLOT_RESTORE_SYNC, WARNING, and ReplicationSlotPersistentData::xmin.

Referenced by StartupReplicationSlots().

1341 {
1343  int i;
1344  char path[MAXPGPATH + 22];
1345  int fd;
1346  bool restored = false;
1347  int readBytes;
1348  pg_crc32c checksum;
1349 
1350  /* no need to lock here, no concurrent access allowed yet */
1351 
1352  /* delete temp file if it exists */
1353  sprintf(path, "pg_replslot/%s/state.tmp", name);
1354  if (unlink(path) < 0 && errno != ENOENT)
1355  ereport(PANIC,
1357  errmsg("could not remove file \"%s\": %m", path)));
1358 
1359  sprintf(path, "pg_replslot/%s/state", name);
1360 
1361  elog(DEBUG1, "restoring replication slot from \"%s\"", path);
1362 
1363  fd = OpenTransientFile(path, O_RDWR | PG_BINARY);
1364 
1365  /*
1366  * We do not need to handle this as we are rename()ing the directory into
1367  * place only after we fsync()ed the state file.
1368  */
1369  if (fd < 0)
1370  ereport(PANIC,
1372  errmsg("could not open file \"%s\": %m", path)));
1373 
1374  /*
1375  * Sync state file before we're reading from it. We might have crashed
1376  * while it wasn't synced yet and we shouldn't continue on that basis.
1377  */
1379  if (pg_fsync(fd) != 0)
1380  {
1381  CloseTransientFile(fd);
1382  ereport(PANIC,
1384  errmsg("could not fsync file \"%s\": %m",
1385  path)));
1386  }
1388 
1389  /* Also sync the parent directory */
1391  fsync_fname(path, true);
1392  END_CRIT_SECTION();
1393 
1394  /* read part of statefile that's guaranteed to be version independent */
1396  readBytes = read(fd, &cp, ReplicationSlotOnDiskConstantSize);
1398  if (readBytes != ReplicationSlotOnDiskConstantSize)
1399  {
1400  int saved_errno = errno;
1401 
1402  CloseTransientFile(fd);
1403  errno = saved_errno;
1404  ereport(PANIC,
1406  errmsg("could not read file \"%s\", read %d of %u: %m",
1407  path, readBytes,
1409  }
1410 
1411  /* verify magic */
1412  if (cp.magic != SLOT_MAGIC)
1413  ereport(PANIC,
1415  errmsg("replication slot file \"%s\" has wrong magic number: %u instead of %u",
1416  path, cp.magic, SLOT_MAGIC)));
1417 
1418  /* verify version */
1419  if (cp.version != SLOT_VERSION)
1420  ereport(PANIC,
1422  errmsg("replication slot file \"%s\" has unsupported version %u",
1423  path, cp.version)));
1424 
1425  /* boundary check on length */
1427  ereport(PANIC,
1429  errmsg("replication slot file \"%s\" has corrupted length %u",
1430  path, cp.length)));
1431 
1432  /* Now that we know the size, read the entire file */
1434  readBytes = read(fd,
1435  (char *) &cp + ReplicationSlotOnDiskConstantSize,
1436  cp.length);
1438  if (readBytes != cp.length)
1439  {
1440  int saved_errno = errno;
1441 
1442  CloseTransientFile(fd);
1443  errno = saved_errno;
1444  ereport(PANIC,
1446  errmsg("could not read file \"%s\", read %d of %u: %m",
1447  path, readBytes, cp.length)));
1448  }
1449 
1450  CloseTransientFile(fd);
1451 
1452  /* now verify the CRC */
1453  INIT_CRC32C(checksum);
1454  COMP_CRC32C(checksum,
1455  (char *) &cp + SnapBuildOnDiskNotChecksummedSize,
1457  FIN_CRC32C(checksum);
1458 
1459  if (!EQ_CRC32C(checksum, cp.checksum))
1460  ereport(PANIC,
1461  (errmsg("checksum mismatch for replication slot file \"%s\": is %u, should be %u",
1462  path, checksum, cp.checksum)));
1463 
1464  /*
1465  * If we crashed with an ephemeral slot active, don't restore but delete
1466  * it.
1467  */
1469  {
1470  sprintf(path, "pg_replslot/%s", name);
1471 
1472  if (!rmtree(path, true))
1473  {
1474  ereport(WARNING,
1476  errmsg("could not remove directory \"%s\"", path)));
1477  }
1478  fsync_fname("pg_replslot", true);
1479  return;
1480  }
1481 
1482  /* nothing can be active yet, don't lock anything */
1483  for (i = 0; i < max_replication_slots; i++)
1484  {
1485  ReplicationSlot *slot;
1486 
1488 
1489  if (slot->in_use)
1490  continue;
1491 
1492  /* restore the entire set of persistent data */
1493  memcpy(&slot->data, &cp.slotdata,
1495 
1496  /* initialize in memory state */
1497  slot->effective_xmin = cp.slotdata.xmin;
1499 
1504 
1505  slot->in_use = true;
1506  slot->active_pid = 0;
1507 
1508  restored = true;
1509  break;
1510  }
1511 
1512  if (!restored)
1513  ereport(PANIC,
1514  (errmsg("too many replication slots active before shutdown"),
1515  errhint("Increase max_replication_slots and try again.")));
1516 }
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
TransactionId candidate_catalog_xmin
Definition: slot.h:148
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
int errhint(const char *fmt,...)
Definition: elog.c:987
#define SLOT_MAGIC
Definition: slot.c:89
uint32 pg_crc32c
Definition: pg_crc32c.h:38
#define END_CRIT_SECTION()
Definition: miscadmin.h:133
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:575
ReplicationSlotPersistency persistency
Definition: slot.h:54
#define START_CRIT_SECTION()
Definition: miscadmin.h:131
#define SnapBuildOnDiskNotChecksummedSize
Definition: slot.c:80
ReplicationSlotPersistentData data
Definition: slot.h:133
#define PANIC
Definition: elog.h:53
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1080
ReplicationSlotPersistentData slotdata
Definition: slot.c:73
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2386
#define MAXPGPATH
TransactionId effective_xmin
Definition: slot.h:129
XLogRecPtr candidate_restart_valid
Definition: slot.h:150
int errcode_for_file_access(void)
Definition: elog.c:598
TransactionId catalog_xmin
Definition: slot.h:70
#define InvalidTransactionId
Definition: transam.h:31
unsigned int uint32
Definition: c.h:325
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1260
TransactionId xmin
Definition: slot.h:62
#define EQ_CRC32C(c1, c2)
Definition: pg_crc32c.h:42
#define ereport(elevel, rest)
Definition: elog.h:122
pg_crc32c checksum
Definition: slot.c:62
int CloseTransientFile(int fd)
Definition: fd.c:2556
#define WARNING
Definition: elog.h:40
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:36
bool in_use
Definition: slot.h:109
TransactionId effective_catalog_xmin
Definition: slot.h:130
#define SLOT_VERSION
Definition: slot.c:90
int max_replication_slots
Definition: slot.c:99
#define ReplicationSlotOnDiskV2Size
Definition: slot.c:86
#define ReplicationSlotOnDiskConstantSize
Definition: slot.c:77
uint32 version
Definition: slot.c:65
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1236
#define SnapBuildOnDiskChecksummedSize
Definition: slot.c:83
const char * name
Definition: encode.c:521
XLogRecPtr candidate_xmin_lsn
Definition: slot.h:149
int errmsg(const char *fmt,...)
Definition: elog.c:797
pid_t active_pid
Definition: slot.h:112
int i
int pg_fsync(int fd)
Definition: fd.c:341
ReplicationSlot replication_slots[1]
Definition: slot.h:166
#define elog
Definition: elog.h:219
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:89
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:94
#define read(a, b, c)
Definition: win32.h:13
XLogRecPtr candidate_restart_lsn
Definition: slot.h:151

◆ SaveSlotToPath()

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

Definition at line 1218 of file slot.c.

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(), ReplicationSlotOnDiskV2Size, SLOT_MAGIC, SLOT_VERSION, ReplicationSlotOnDisk::slotdata, SnapBuildOnDiskChecksummedSize, SnapBuildOnDiskNotChecksummedSize, SpinLockAcquire, SpinLockRelease, START_CRIT_SECTION, ReplicationSlotOnDisk::version, WAIT_EVENT_REPLICATION_SLOT_SYNC, WAIT_EVENT_REPLICATION_SLOT_WRITE, and write.

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

1219 {
1220  char tmppath[MAXPGPATH];
1221  char path[MAXPGPATH];
1222  int fd;
1224  bool was_dirty;
1225 
1226  /* first check whether there's something to write out */
1227  SpinLockAcquire(&slot->mutex);
1228  was_dirty = slot->dirty;
1229  slot->just_dirtied = false;
1230  SpinLockRelease(&slot->mutex);
1231 
1232  /* and don't do anything if there's nothing to write */
1233  if (!was_dirty)
1234  return;
1235 
1237 
1238  /* silence valgrind :( */
1239  memset(&cp, 0, sizeof(ReplicationSlotOnDisk));
1240 
1241  sprintf(tmppath, "%s/state.tmp", dir);
1242  sprintf(path, "%s/state", dir);
1243 
1244  fd = OpenTransientFile(tmppath, O_CREAT | O_EXCL | O_WRONLY | PG_BINARY);
1245  if (fd < 0)
1246  {
1247  ereport(elevel,
1249  errmsg("could not create file \"%s\": %m",
1250  tmppath)));
1251  return;
1252  }
1253 
1254  cp.magic = SLOT_MAGIC;
1255  INIT_CRC32C(cp.checksum);
1256  cp.version = SLOT_VERSION;
1258 
1259  SpinLockAcquire(&slot->mutex);
1260 
1261  memcpy(&cp.slotdata, &slot->data, sizeof(ReplicationSlotPersistentData));
1262 
1263  SpinLockRelease(&slot->mutex);
1264 
1265  COMP_CRC32C(cp.checksum,
1266  (char *) (&cp) + SnapBuildOnDiskNotChecksummedSize,
1268  FIN_CRC32C(cp.checksum);
1269 
1271  if ((write(fd, &cp, sizeof(cp))) != sizeof(cp))
1272  {
1273  int save_errno = errno;
1274 
1276  CloseTransientFile(fd);
1277  errno = save_errno;
1278  ereport(elevel,
1280  errmsg("could not write to file \"%s\": %m",
1281  tmppath)));
1282  return;
1283  }
1285 
1286  /* fsync the temporary file */
1288  if (pg_fsync(fd) != 0)
1289  {
1290  int save_errno = errno;
1291 
1293  CloseTransientFile(fd);
1294  errno = save_errno;
1295  ereport(elevel,
1297  errmsg("could not fsync file \"%s\": %m",
1298  tmppath)));
1299  return;
1300  }
1302 
1303  CloseTransientFile(fd);
1304 
1305  /* rename to permanent file, fsync file and directory */
1306  if (rename(tmppath, path) != 0)
1307  {
1308  ereport(elevel,
1310  errmsg("could not rename file \"%s\" to \"%s\": %m",
1311  tmppath, path)));
1312  return;
1313  }
1314 
1315  /* Check CreateSlot() for the reasoning of using a crit. section. */
1317 
1318  fsync_fname(path, false);
1319  fsync_fname(dir, true);
1320  fsync_fname("pg_replslot", true);
1321 
1322  END_CRIT_SECTION();
1323 
1324  /*
1325  * Successfully wrote, unset dirty bit, unless somebody dirtied again
1326  * already.
1327  */
1328  SpinLockAcquire(&slot->mutex);
1329  if (!slot->just_dirtied)
1330  slot->dirty = false;
1331  SpinLockRelease(&slot->mutex);
1332 
1334 }
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
#define write(a, b, c)
Definition: win32.h:14
#define SLOT_MAGIC
Definition: slot.c:89
#define END_CRIT_SECTION()
Definition: miscadmin.h:133
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:575
#define START_CRIT_SECTION()
Definition: miscadmin.h:131
#define SnapBuildOnDiskNotChecksummedSize
Definition: slot.c:80
ReplicationSlotPersistentData data
Definition: slot.h:133
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define PG_BINARY
Definition: c.h:1080
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1725
#define SpinLockAcquire(lock)
Definition: spin.h:62
ReplicationSlotPersistentData slotdata
Definition: slot.c:73
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2386
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:598
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1260
#define ereport(elevel, rest)
Definition: elog.h:122
pg_crc32c checksum
Definition: slot.c:62
int CloseTransientFile(int fd)
Definition: fd.c:2556
static int elevel
Definition: vacuumlazy.c:144
#define SpinLockRelease(lock)
Definition: spin.h:64
bool just_dirtied
Definition: slot.h:115
#define SLOT_VERSION
Definition: slot.c:90
#define ReplicationSlotOnDiskV2Size
Definition: slot.c:86
uint32 version
Definition: slot.c:65
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: pgstat.h:1236
#define SnapBuildOnDiskChecksummedSize
Definition: slot.c:83
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1121
int errmsg(const char *fmt,...)
Definition: elog.c:797
int pg_fsync(int fd)
Definition: fd.c:341
slock_t mutex
Definition: slot.h:106
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:89
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:94
bool dirty
Definition: slot.h:116
LWLock io_in_progress_lock
Definition: slot.h:136

◆ StartupReplicationSlots()

void StartupReplicationSlots ( void  )

Definition at line 1097 of file slot.c.

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

Referenced by StartupXLOG().

1098 {
1099  DIR *replication_dir;
1100  struct dirent *replication_de;
1101 
1102  elog(DEBUG1, "starting up replication slots");
1103 
1104  /* restore all slots by iterating over all on-disk entries */
1105  replication_dir = AllocateDir("pg_replslot");
1106  while ((replication_de = ReadDir(replication_dir, "pg_replslot")) != NULL)
1107  {
1108  struct stat statbuf;
1109  char path[MAXPGPATH + 12];
1110 
1111  if (strcmp(replication_de->d_name, ".") == 0 ||
1112  strcmp(replication_de->d_name, "..") == 0)
1113  continue;
1114 
1115  snprintf(path, sizeof(path), "pg_replslot/%s", replication_de->d_name);
1116 
1117  /* we're only creating directories here, skip if it's not our's */
1118  if (lstat(path, &statbuf) == 0 && !S_ISDIR(statbuf.st_mode))
1119  continue;
1120 
1121  /* we crashed while a slot was being setup or deleted, clean up */
1122  if (pg_str_endswith(replication_de->d_name, ".tmp"))
1123  {
1124  if (!rmtree(path, true))
1125  {
1126  ereport(WARNING,
1128  errmsg("could not remove directory \"%s\"", path)));
1129  continue;
1130  }
1131  fsync_fname("pg_replslot", true);
1132  continue;
1133  }
1134 
1135  /* looks like a slot in a normal state, restore */
1136  RestoreSlotFromDisk(replication_de->d_name);
1137  }
1138  FreeDir(replication_dir);
1139 
1140  /* currently no slots exist, we're done. */
1141  if (max_replication_slots <= 0)
1142  return;
1143 
1144  /* Now that we have recovered all the data, compute replication xmin */
1147 }
static void RestoreSlotFromDisk(const char *name)
Definition: slot.c:1340
#define DEBUG1
Definition: elog.h:25
bool pg_str_endswith(const char *str, const char *end)
Definition: string.c:31
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:575
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
Definition: dirent.h:9
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:741
Definition: dirent.c:25
#define MAXPGPATH
int errcode_for_file_access(void)
Definition: elog.c:598
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2590
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
#define stat(a, b)
Definition: win32_port.h:266
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:36
int max_replication_slots
Definition: slot.c:99
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2656
#define S_ISDIR(m)
Definition: win32_port.h:307
#define lstat(path, sb)
Definition: win32_port.h:255
int errmsg(const char *fmt,...)
Definition: elog.c:797
char d_name[MAX_PATH]
Definition: dirent.h:14
#define elog
Definition: elog.h:219
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:695
int FreeDir(DIR *dir)
Definition: fd.c:2708

Variable Documentation

◆ max_replication_slots

◆ MyReplicationSlot

◆ ReplicationSlotCtl

ReplicationSlotCtlData* ReplicationSlotCtl = NULL

Definition at line 93 of file slot.c.

Referenced by pg_get_replication_slots().