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

Go to the source code of this file.

Data Structures

struct  ReplicationSlotPersistentData
 
struct  ReplicationSlot
 
struct  ReplicationSlotCtlData
 

Macros

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

Typedefs

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

Enumerations

enum  ReplicationSlotPersistency { RS_PERSISTENT, RS_EPHEMERAL, RS_TEMPORARY }
 
enum  SlotAcquireBehavior { SAB_Error, SAB_Block, SAB_Inquire }
 

Functions

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

Variables

PGDLLIMPORT ReplicationSlotCtlDataReplicationSlotCtl
 
PGDLLIMPORT ReplicationSlotMyReplicationSlot
 
PGDLLIMPORT int max_replication_slots
 

Macro Definition Documentation

◆ SlotIsLogical

◆ SlotIsPhysical

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

Typedef Documentation

◆ ReplicationSlot

◆ ReplicationSlotCtlData

◆ ReplicationSlotPersistency

◆ ReplicationSlotPersistentData

◆ SlotAcquireBehavior

Enumeration Type Documentation

◆ ReplicationSlotPersistency

Enumerator
RS_PERSISTENT 
RS_EPHEMERAL 
RS_TEMPORARY 

Definition at line 33 of file slot.h.

34 {
ReplicationSlotPersistency
Definition: slot.h:33

◆ SlotAcquireBehavior

Enumerator
SAB_Error 
SAB_Block 
SAB_Inquire 

Definition at line 41 of file slot.h.

42 {
43  SAB_Error,
44  SAB_Block,
Definition: slot.h:44
SlotAcquireBehavior
Definition: slot.h:41
Definition: slot.h:43

Function Documentation

◆ CheckPointReplicationSlots()

void CheckPointReplicationSlots ( void  )

Definition at line 1285 of file slot.c.

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

Referenced by CheckPointGuts().

1286 {
1287  int i;
1288 
1289  elog(DEBUG1, "performing replication slot checkpoint");
1290 
1291  /*
1292  * Prevent any slot from being created/dropped while we're active. As we
1293  * explicitly do *not* want to block iterating over replication_slots or
1294  * acquiring a slot we cannot take the control lock - but that's OK,
1295  * because holding ReplicationSlotAllocationLock is strictly stronger, and
1296  * enough to guarantee that nobody can change the in_use bits on us.
1297  */
1298  LWLockAcquire(ReplicationSlotAllocationLock, LW_SHARED);
1299 
1300  for (i = 0; i < max_replication_slots; i++)
1301  {
1303  char path[MAXPGPATH];
1304 
1305  if (!s->in_use)
1306  continue;
1307 
1308  /* save the slot to disk, locking is handled in SaveSlotToPath() */
1309  sprintf(path, "pg_replslot/%s", NameStr(s->data.name));
1310  SaveSlotToPath(s, path, LOG);
1311  }
1312  LWLockRelease(ReplicationSlotAllocationLock);
1313 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
#define DEBUG1
Definition: elog.h:25
ReplicationSlotPersistentData data
Definition: slot.h:156
#define LOG
Definition: elog.h:26
static void SaveSlotToPath(ReplicationSlot *slot, const char *path, int elevel)
Definition: slot.c:1441
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
#define sprintf
Definition: port.h:218
#define MAXPGPATH
bool in_use
Definition: slot.h:132
int max_replication_slots
Definition: slot.c:99
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
#define elog(elevel,...)
Definition: elog.h:232
int i
#define NameStr(name)
Definition: c.h:681
ReplicationSlot replication_slots[1]
Definition: slot.h:189

◆ CheckSlotRequirements()

void CheckSlotRequirements ( void  )

Definition at line 1068 of file slot.c.

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

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

1069 {
1070  /*
1071  * NB: Adding a new requirement likely means that RestoreSlotFromDisk()
1072  * needs the same check.
1073  */
1074 
1075  if (max_replication_slots == 0)
1076  ereport(ERROR,
1077  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1078  errmsg("replication slots can only be used if max_replication_slots > 0")));
1079 
1081  ereport(ERROR,
1082  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1083  errmsg("replication slots can only be used if wal_level >= replica")));
1084 }
int wal_level
Definition: xlog.c:109
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
int max_replication_slots
Definition: slot.c:99
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ InvalidateObsoleteReplicationSlots()

void InvalidateObsoleteReplicationSlots ( XLogSegNo  oldestSegno)

Definition at line 1170 of file slot.c.

References ReplicationSlot::active_cv, CHECK_FOR_INTERRUPTS, ConditionVariableCancelSleep(), ConditionVariablePrepareToSleep(), ConditionVariableTimedSleep(), ReplicationSlot::data, ereport, errmsg(), i, ReplicationSlot::in_use, ReplicationSlotPersistentData::invalidated_at, InvalidXLogRecPtr, kill, LOG, LSN_FORMAT_ARGS, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, ReplicationSlot::mutex, ReplicationSlotPersistentData::name, NameStr, ReplicationSlotCtlData::replication_slots, ReplicationSlotAcquireInternal(), ReplicationSlotMarkDirty(), ReplicationSlotRelease(), ReplicationSlotSave(), ReplicationSlotPersistentData::restart_lsn, SAB_Inquire, SpinLockAcquire, SpinLockRelease, WAIT_EVENT_REPLICATION_SLOT_DROP, wal_segment_size, XLogRecPtrIsInvalid, and XLogSegNoOffsetToRecPtr.

Referenced by CreateCheckPoint(), and CreateRestartPoint().

1171 {
1172  XLogRecPtr oldestLSN;
1173 
1174  XLogSegNoOffsetToRecPtr(oldestSegno, 0, wal_segment_size, oldestLSN);
1175 
1176 restart:
1177  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1178  for (int i = 0; i < max_replication_slots; i++)
1179  {
1181  XLogRecPtr restart_lsn = InvalidXLogRecPtr;
1182  NameData slotname;
1183  int wspid;
1184  int last_signaled_pid = 0;
1185 
1186  if (!s->in_use)
1187  continue;
1188 
1189  SpinLockAcquire(&s->mutex);
1190  slotname = s->data.name;
1191  restart_lsn = s->data.restart_lsn;
1192  SpinLockRelease(&s->mutex);
1193 
1194  if (XLogRecPtrIsInvalid(restart_lsn) || restart_lsn >= oldestLSN)
1195  continue;
1196  LWLockRelease(ReplicationSlotControlLock);
1198 
1199  /* Get ready to sleep on the slot in case it is active */
1201 
1202  for (;;)
1203  {
1204  /*
1205  * Try to mark this slot as used by this process.
1206  *
1207  * Note that ReplicationSlotAcquireInternal(SAB_Inquire)
1208  * should not cancel the prepared condition variable
1209  * if this slot is active in other process. Because in this case
1210  * we have to wait on that CV for the process owning
1211  * the slot to be terminated, later.
1212  */
1213  wspid = ReplicationSlotAcquireInternal(s, NULL, SAB_Inquire);
1214 
1215  /*
1216  * Exit the loop if we successfully acquired the slot or
1217  * the slot was dropped during waiting for the owning process
1218  * to be terminated. For example, the latter case is likely to
1219  * happen when the slot is temporary because it's automatically
1220  * dropped by the termination of the owning process.
1221  */
1222  if (wspid <= 0)
1223  break;
1224 
1225  /*
1226  * Signal to terminate the process that owns the slot.
1227  *
1228  * There is the race condition where other process may own
1229  * the slot after the process using it was terminated and before
1230  * this process owns it. To handle this case, we signal again
1231  * if the PID of the owning process is changed than the last.
1232  *
1233  * XXX This logic assumes that the same PID is not reused
1234  * very quickly.
1235  */
1236  if (last_signaled_pid != wspid)
1237  {
1238  ereport(LOG,
1239  (errmsg("terminating process %d because replication slot \"%s\" is too far behind",
1240  wspid, NameStr(slotname))));
1241  (void) kill(wspid, SIGTERM);
1242  last_signaled_pid = wspid;
1243  }
1244 
1247  }
1249 
1250  /*
1251  * Do nothing here and start from scratch if the slot has
1252  * already been dropped.
1253  */
1254  if (wspid == -1)
1255  goto restart;
1256 
1257  ereport(LOG,
1258  (errmsg("invalidating slot \"%s\" because its restart_lsn %X/%X exceeds max_slot_wal_keep_size",
1259  NameStr(slotname),
1260  LSN_FORMAT_ARGS(restart_lsn))));
1261 
1262  SpinLockAcquire(&s->mutex);
1265  SpinLockRelease(&s->mutex);
1266 
1267  /* Make sure the invalidated state persists across server restart */
1271 
1272  /* if we did anything, start from scratch */
1273  goto restart;
1274  }
1275  LWLockRelease(ReplicationSlotControlLock);
1276 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
int wal_segment_size
Definition: xlog.c:121
#define kill(pid, sig)
Definition: win32_port.h:454
void ReplicationSlotSave(void)
Definition: slot.c:734
ReplicationSlotPersistentData data
Definition: slot.h:156
#define LOG
Definition: elog.h:26
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
void ConditionVariableCancelSleep(void)
bool ConditionVariableTimedSleep(ConditionVariable *cv, long timeout, uint32 wait_event_info)
Definition: c.h:675
void ReplicationSlotRelease(void)
Definition: slot.c:493
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
bool in_use
Definition: slot.h:132
#define SpinLockRelease(lock)
Definition: spin.h:64
#define ereport(elevel,...)
Definition: elog.h:157
int max_replication_slots
Definition: slot.c:99
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr restart_lsn
Definition: slot.h:81
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
static int ReplicationSlotAcquireInternal(ReplicationSlot *slot, const char *name, SlotAcquireBehavior behavior)
Definition: slot.c:398
ConditionVariable active_cv
Definition: slot.h:162
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i
#define NameStr(name)
Definition: c.h:681
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:102
ReplicationSlot replication_slots[1]
Definition: slot.h:189
XLogRecPtr invalidated_at
Definition: slot.h:84
slock_t mutex
Definition: slot.h:129
void ReplicationSlotMarkDirty(void)
Definition: slot.c:752

◆ ReplicationSlotAcquire()

int ReplicationSlotAcquire ( const char *  name,
SlotAcquireBehavior  behavior 
)

Definition at line 384 of file slot.c.

References ReplicationSlotAcquireInternal().

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

385 {
386  return ReplicationSlotAcquireInternal(NULL, name, behavior);
387 }
static int ReplicationSlotAcquireInternal(ReplicationSlot *slot, const char *name, SlotAcquireBehavior behavior)
Definition: slot.c:398
const char * name
Definition: encode.c:515

◆ ReplicationSlotCleanup()

void ReplicationSlotCleanup ( void  )

Definition at line 549 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().

550 {
551  int i;
552 
553  Assert(MyReplicationSlot == NULL);
554 
555 restart:
556  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
557  for (i = 0; i < max_replication_slots; i++)
558  {
560 
561  if (!s->in_use)
562  continue;
563 
564  SpinLockAcquire(&s->mutex);
565  if (s->active_pid == MyProcPid)
566  {
568  SpinLockRelease(&s->mutex);
569  LWLockRelease(ReplicationSlotControlLock); /* avoid deadlock */
570 
572 
574  goto restart;
575  }
576  else
577  SpinLockRelease(&s->mutex);
578  }
579 
580  LWLockRelease(ReplicationSlotControlLock);
581 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
int MyProcPid
Definition: globals.c:43
ReplicationSlotPersistency persistency
Definition: slot.h:62
void ConditionVariableBroadcast(ConditionVariable *cv)
static void ReplicationSlotDropPtr(ReplicationSlot *slot)
Definition: slot.c:617
ReplicationSlotPersistentData data
Definition: slot.h:156
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
#define SpinLockAcquire(lock)
Definition: spin.h:62
bool in_use
Definition: slot.h:132
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
int max_replication_slots
Definition: slot.c:99
#define Assert(condition)
Definition: c.h:804
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
ConditionVariable active_cv
Definition: slot.h:162
pid_t active_pid
Definition: slot.h:135
int i
ReplicationSlot replication_slots[1]
Definition: slot.h:189
slock_t mutex
Definition: slot.h:129

◆ ReplicationSlotCreate()

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

Definition at line 228 of file slot.c.

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

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

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

◆ ReplicationSlotDrop()

void ReplicationSlotDrop ( const char *  name,
bool  nowait 
)

Definition at line 587 of file slot.c.

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

Referenced by DropReplicationSlot(), and pg_drop_replication_slot().

588 {
589  Assert(MyReplicationSlot == NULL);
590 
591  (void) ReplicationSlotAcquire(name, nowait ? SAB_Error : SAB_Block);
592 
594 }
static void ReplicationSlotDropAcquired(void)
Definition: slot.c:600
Definition: slot.h:44
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:804
const char * name
Definition: encode.c:515
int ReplicationSlotAcquire(const char *name, SlotAcquireBehavior behavior)
Definition: slot.c:384
Definition: slot.h:43

◆ ReplicationSlotDropAtPubNode()

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

Definition at line 1320 of file subscriptioncmds.c.

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

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

1321 {
1322  StringInfoData cmd;
1323 
1324  Assert(wrconn);
1325 
1326  load_file("libpqwalreceiver", false);
1327 
1328  initStringInfo(&cmd);
1329  appendStringInfo(&cmd, "DROP_REPLICATION_SLOT %s WAIT", quote_identifier(slotname));
1330 
1331  PG_TRY();
1332  {
1333  WalRcvExecResult *res;
1334 
1335  res = walrcv_exec(wrconn, cmd.data, 0, NULL);
1336 
1337  if (res->status == WALRCV_OK_COMMAND)
1338  {
1339  /* NOTICE. Success. */
1340  ereport(NOTICE,
1341  (errmsg("dropped replication slot \"%s\" on publisher",
1342  slotname)));
1343  }
1344  else if (res->status == WALRCV_ERROR &&
1345  missing_ok &&
1346  res->sqlstate == ERRCODE_UNDEFINED_OBJECT)
1347  {
1348  /* LOG. Error, but missing_ok = true. */
1349  ereport(LOG,
1350  (errmsg("could not drop replication slot \"%s\" on publisher: %s",
1351  slotname, res->err)));
1352  }
1353  else
1354  {
1355  /* ERROR. */
1356  ereport(ERROR,
1357  (errmsg("could not drop replication slot \"%s\" on publisher: %s",
1358  slotname, res->err)));
1359  }
1360 
1361  walrcv_clear_result(res);
1362  }
1363  PG_FINALLY();
1364  {
1365  pfree(cmd.data);
1366  }
1367  PG_END_TRY();
1368 }
const char * quote_identifier(const char *ident)
Definition: ruleutils.c:11323
#define LOG
Definition: elog.h:26
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:433
void pfree(void *pointer)
Definition: mcxt.c:1169
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define ERROR
Definition: elog.h:46
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define PG_FINALLY()
Definition: elog.h:330
#define ereport(elevel,...)
Definition: elog.h:157
#define NOTICE
Definition: elog.h:37
#define Assert(condition)
Definition: c.h:804
WalRcvExecStatus status
Definition: walreceiver.h:214
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:146
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define PG_TRY()
Definition: elog.h:313
#define PG_END_TRY()
Definition: elog.h:338
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:427

◆ ReplicationSlotMarkDirty()

◆ ReplicationSlotNameForTablesync()

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

Definition at line 885 of file tablesync.c.

References GetSystemIdentifier(), snprintf, and UINT64_FORMAT.

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

887 {
888  snprintf(syncslotname, szslot, "pg_%u_sync_%u_" UINT64_FORMAT, suboid,
889  relid, GetSystemIdentifier());
890 }
uint64 GetSystemIdentifier(void)
Definition: xlog.c:4965
#define snprintf
Definition: port.h:216
#define UINT64_FORMAT
Definition: c.h:484

◆ ReplicationSlotPersist()

void ReplicationSlotPersist ( void  )

Definition at line 769 of file slot.c.

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

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

770 {
772 
773  Assert(slot != NULL);
775 
776  SpinLockAcquire(&slot->mutex);
778  SpinLockRelease(&slot->mutex);
779 
782 }
ReplicationSlotPersistency persistency
Definition: slot.h:62
void ReplicationSlotSave(void)
Definition: slot.c:734
ReplicationSlotPersistentData data
Definition: slot.h:156
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:804
slock_t mutex
Definition: slot.h:129
void ReplicationSlotMarkDirty(void)
Definition: slot.c:752

◆ ReplicationSlotRelease()

void ReplicationSlotRelease ( void  )

Definition at line 493 of file slot.c.

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

Referenced by copy_replication_slot(), CreateReplicationSlot(), InvalidateObsoleteReplicationSlots(), 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().

494 {
496 
497  Assert(slot != NULL && slot->active_pid != 0);
498 
499  if (slot->data.persistency == RS_EPHEMERAL)
500  {
501  /*
502  * Delete the slot. There is no !PANIC case where this is allowed to
503  * fail, all that may happen is an incomplete cleanup of the on-disk
504  * data.
505  */
507  }
508 
509  /*
510  * If slot needed to temporarily restrain both data and catalog xmin to
511  * create the catalog snapshot, remove that temporary constraint.
512  * Snapshots can only be exported while the initial snapshot is still
513  * acquired.
514  */
515  if (!TransactionIdIsValid(slot->data.xmin) &&
517  {
518  SpinLockAcquire(&slot->mutex);
520  SpinLockRelease(&slot->mutex);
522  }
523 
524  if (slot->data.persistency == RS_PERSISTENT)
525  {
526  /*
527  * Mark persistent slot inactive. We're not freeing it, just
528  * disconnecting, but wake up others that may be waiting for it.
529  */
530  SpinLockAcquire(&slot->mutex);
531  slot->active_pid = 0;
532  SpinLockRelease(&slot->mutex);
534  }
535 
536  MyReplicationSlot = NULL;
537 
538  /* might not have been set when we've been a plain slot */
539  LWLockAcquire(ProcArrayLock, LW_SHARED);
542  LWLockRelease(ProcArrayLock);
543 }
#define PROC_IN_LOGICAL_DECODING
Definition: proc.h:61
PGPROC * MyProc
Definition: proc.c:68
ReplicationSlotPersistency persistency
Definition: slot.h:62
void ConditionVariableBroadcast(ConditionVariable *cv)
PROC_HDR * ProcGlobal
Definition: proc.c:80
uint8 statusFlags
Definition: proc.h:189
ReplicationSlotPersistentData data
Definition: slot.h:156
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
#define SpinLockAcquire(lock)
Definition: spin.h:62
static void ReplicationSlotDropAcquired(void)
Definition: slot.c:600
TransactionId effective_xmin
Definition: slot.h:152
#define InvalidTransactionId
Definition: transam.h:31
TransactionId xmin
Definition: slot.h:70
#define SpinLockRelease(lock)
Definition: spin.h:64
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:804
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
ConditionVariable active_cv
Definition: slot.h:162
pid_t active_pid
Definition: slot.h:135
int pgxactoff
Definition: proc.h:148
uint8 * statusFlags
Definition: proc.h:333
slock_t mutex
Definition: slot.h:129
#define TransactionIdIsValid(xid)
Definition: transam.h:41
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:791

◆ ReplicationSlotReserveWal()

void ReplicationSlotReserveWal ( void  )

Definition at line 1093 of file slot.c.

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

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

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

◆ ReplicationSlotSave()

void ReplicationSlotSave ( void  )

Definition at line 734 of file slot.c.

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

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

735 {
736  char path[MAXPGPATH];
737 
738  Assert(MyReplicationSlot != NULL);
739 
740  sprintf(path, "pg_replslot/%s", NameStr(MyReplicationSlot->data.name));
742 }
ReplicationSlotPersistentData data
Definition: slot.h:156
static void SaveSlotToPath(ReplicationSlot *slot, const char *path, int elevel)
Definition: slot.c:1441
#define sprintf
Definition: port.h:218
#define ERROR
Definition: elog.h:46
#define MAXPGPATH
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:804
#define NameStr(name)
Definition: c.h:681

◆ ReplicationSlotsComputeLogicalRestartLSN()

XLogRecPtr ReplicationSlotsComputeLogicalRestartLSN ( void  )

Definition at line 884 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().

885 {
886  XLogRecPtr result = InvalidXLogRecPtr;
887  int i;
888 
889  if (max_replication_slots <= 0)
890  return InvalidXLogRecPtr;
891 
892  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
893 
894  for (i = 0; i < max_replication_slots; i++)
895  {
896  ReplicationSlot *s;
897  XLogRecPtr restart_lsn;
898 
900 
901  /* cannot change while ReplicationSlotCtlLock is held */
902  if (!s->in_use)
903  continue;
904 
905  /* we're only interested in logical slots */
906  if (!SlotIsLogical(s))
907  continue;
908 
909  /* read once, it's ok if it increases while we're checking */
910  SpinLockAcquire(&s->mutex);
911  restart_lsn = s->data.restart_lsn;
912  SpinLockRelease(&s->mutex);
913 
914  if (restart_lsn == InvalidXLogRecPtr)
915  continue;
916 
917  if (result == InvalidXLogRecPtr ||
918  restart_lsn < result)
919  result = restart_lsn;
920  }
921 
922  LWLockRelease(ReplicationSlotControlLock);
923 
924  return result;
925 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:156
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SlotIsLogical(slot)
Definition: slot.h:178
bool in_use
Definition: slot.h:132
#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:81
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
int i
ReplicationSlot replication_slots[1]
Definition: slot.h:189
slock_t mutex
Definition: slot.h:129

◆ ReplicationSlotsComputeRequiredLSN()

void ReplicationSlotsComputeRequiredLSN ( void  )

Definition at line 841 of file slot.c.

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

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

842 {
843  int i;
844  XLogRecPtr min_required = InvalidXLogRecPtr;
845 
846  Assert(ReplicationSlotCtl != NULL);
847 
848  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
849  for (i = 0; i < max_replication_slots; i++)
850  {
852  XLogRecPtr restart_lsn;
853 
854  if (!s->in_use)
855  continue;
856 
857  SpinLockAcquire(&s->mutex);
858  restart_lsn = s->data.restart_lsn;
859  SpinLockRelease(&s->mutex);
860 
861  if (restart_lsn != InvalidXLogRecPtr &&
862  (min_required == InvalidXLogRecPtr ||
863  restart_lsn < min_required))
864  min_required = restart_lsn;
865  }
866  LWLockRelease(ReplicationSlotControlLock);
867 
868  XLogSetReplicationSlotMinimumLSN(min_required);
869 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
ReplicationSlotPersistentData data
Definition: slot.h:156
void XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
Definition: xlog.c:2763
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
#define SpinLockAcquire(lock)
Definition: spin.h:62
bool in_use
Definition: slot.h:132
#define SpinLockRelease(lock)
Definition: spin.h:64
int max_replication_slots
Definition: slot.c:99
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
XLogRecPtr restart_lsn
Definition: slot.h:81
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
int i
ReplicationSlot replication_slots[1]
Definition: slot.h:189
slock_t mutex
Definition: slot.h:129

◆ ReplicationSlotsComputeRequiredXmin()

void ReplicationSlotsComputeRequiredXmin ( bool  already_locked)

Definition at line 791 of file slot.c.

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

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

792 {
793  int i;
795  TransactionId agg_catalog_xmin = InvalidTransactionId;
796 
797  Assert(ReplicationSlotCtl != NULL);
798 
799  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
800 
801  for (i = 0; i < max_replication_slots; i++)
802  {
804  TransactionId effective_xmin;
805  TransactionId effective_catalog_xmin;
806 
807  if (!s->in_use)
808  continue;
809 
810  SpinLockAcquire(&s->mutex);
811  effective_xmin = s->effective_xmin;
812  effective_catalog_xmin = s->effective_catalog_xmin;
813  SpinLockRelease(&s->mutex);
814 
815  /* check the data xmin */
816  if (TransactionIdIsValid(effective_xmin) &&
817  (!TransactionIdIsValid(agg_xmin) ||
818  TransactionIdPrecedes(effective_xmin, agg_xmin)))
819  agg_xmin = effective_xmin;
820 
821  /* check the catalog xmin */
822  if (TransactionIdIsValid(effective_catalog_xmin) &&
823  (!TransactionIdIsValid(agg_catalog_xmin) ||
824  TransactionIdPrecedes(effective_catalog_xmin, agg_catalog_xmin)))
825  agg_catalog_xmin = effective_catalog_xmin;
826  }
827 
828  LWLockRelease(ReplicationSlotControlLock);
829 
830  ProcArraySetReplicationSlotXmin(agg_xmin, agg_catalog_xmin, already_locked);
831 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
uint32 TransactionId
Definition: c.h:587
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
#define SpinLockAcquire(lock)
Definition: spin.h:62
TransactionId effective_xmin
Definition: slot.h:152
#define InvalidTransactionId
Definition: transam.h:31
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
bool in_use
Definition: slot.h:132
#define SpinLockRelease(lock)
Definition: spin.h:64
TransactionId effective_catalog_xmin
Definition: slot.h:153
int max_replication_slots
Definition: slot.c:99
#define Assert(condition)
Definition: c.h:804
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
int i
void ProcArraySetReplicationSlotXmin(TransactionId xmin, TransactionId catalog_xmin, bool already_locked)
Definition: procarray.c:3802
ReplicationSlot replication_slots[1]
Definition: slot.h:189
slock_t mutex
Definition: slot.h:129
#define TransactionIdIsValid(xid)
Definition: transam.h:41

◆ ReplicationSlotsCountDBSlots()

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

Definition at line 936 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().

937 {
938  int i;
939 
940  *nslots = *nactive = 0;
941 
942  if (max_replication_slots <= 0)
943  return false;
944 
945  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
946  for (i = 0; i < max_replication_slots; i++)
947  {
948  ReplicationSlot *s;
949 
951 
952  /* cannot change while ReplicationSlotCtlLock is held */
953  if (!s->in_use)
954  continue;
955 
956  /* only logical slots are database specific, skip */
957  if (!SlotIsLogical(s))
958  continue;
959 
960  /* not our database, skip */
961  if (s->data.database != dboid)
962  continue;
963 
964  /* count slots with spinlock held */
965  SpinLockAcquire(&s->mutex);
966  (*nslots)++;
967  if (s->active_pid != 0)
968  (*nactive)++;
969  SpinLockRelease(&s->mutex);
970  }
971  LWLockRelease(ReplicationSlotControlLock);
972 
973  if (*nslots > 0)
974  return true;
975  return false;
976 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
ReplicationSlotPersistentData data
Definition: slot.h:156
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SlotIsLogical(slot)
Definition: slot.h:178
bool in_use
Definition: slot.h:132
#define SpinLockRelease(lock)
Definition: spin.h:64
int max_replication_slots
Definition: slot.c:99
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
pid_t active_pid
Definition: slot.h:135
int i
ReplicationSlot replication_slots[1]
Definition: slot.h:189
slock_t mutex
Definition: slot.h:129

◆ ReplicationSlotsDropDBSlots()

void ReplicationSlotsDropDBSlots ( Oid  dboid)

Definition at line 992 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().

993 {
994  int i;
995 
996  if (max_replication_slots <= 0)
997  return;
998 
999 restart:
1000  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1001  for (i = 0; i < max_replication_slots; i++)
1002  {
1003  ReplicationSlot *s;
1004  char *slotname;
1005  int active_pid;
1006 
1008 
1009  /* cannot change while ReplicationSlotCtlLock is held */
1010  if (!s->in_use)
1011  continue;
1012 
1013  /* only logical slots are database specific, skip */
1014  if (!SlotIsLogical(s))
1015  continue;
1016 
1017  /* not our database, skip */
1018  if (s->data.database != dboid)
1019  continue;
1020 
1021  /* acquire slot, so ReplicationSlotDropAcquired can be reused */
1022  SpinLockAcquire(&s->mutex);
1023  /* can't change while ReplicationSlotControlLock is held */
1024  slotname = NameStr(s->data.name);
1025  active_pid = s->active_pid;
1026  if (active_pid == 0)
1027  {
1028  MyReplicationSlot = s;
1029  s->active_pid = MyProcPid;
1030  }
1031  SpinLockRelease(&s->mutex);
1032 
1033  /*
1034  * Even though we hold an exclusive lock on the database object a
1035  * logical slot for that DB can still be active, e.g. if it's
1036  * concurrently being dropped by a backend connected to another DB.
1037  *
1038  * That's fairly unlikely in practice, so we'll just bail out.
1039  */
1040  if (active_pid)
1041  ereport(ERROR,
1042  (errcode(ERRCODE_OBJECT_IN_USE),
1043  errmsg("replication slot \"%s\" is active for PID %d",
1044  slotname, active_pid)));
1045 
1046  /*
1047  * To avoid duplicating ReplicationSlotDropAcquired() and to avoid
1048  * holding ReplicationSlotControlLock over filesystem operations,
1049  * release ReplicationSlotControlLock and use
1050  * ReplicationSlotDropAcquired.
1051  *
1052  * As that means the set of slots could change, restart scan from the
1053  * beginning each time we release the lock.
1054  */
1055  LWLockRelease(ReplicationSlotControlLock);
1057  goto restart;
1058  }
1059  LWLockRelease(ReplicationSlotControlLock);
1060 }
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:93
int MyProcPid
Definition: globals.c:43
int errcode(int sqlerrcode)
Definition: elog.c:698
ReplicationSlotPersistentData data
Definition: slot.h:156
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1816
#define SpinLockAcquire(lock)
Definition: spin.h:62
static void ReplicationSlotDropAcquired(void)
Definition: slot.c:600
#define ERROR
Definition: elog.h:46
#define SlotIsLogical(slot)
Definition: slot.h:178
bool in_use
Definition: slot.h:132
#define SpinLockRelease(lock)
Definition: spin.h:64
#define ereport(elevel,...)
Definition: elog.h:157
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
int max_replication_slots
Definition: slot.c:99
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1203
int errmsg(const char *fmt,...)
Definition: elog.c:909
pid_t active_pid
Definition: slot.h:135
int i
#define NameStr(name)
Definition: c.h:681
ReplicationSlot replication_slots[1]
Definition: slot.h:189
slock_t mutex
Definition: slot.h:129

◆ ReplicationSlotsShmemInit()

void ReplicationSlotsShmemInit ( void  )

Definition at line 134 of file slot.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

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

◆ ReplicationSlotsShmemSize()

Size ReplicationSlotsShmemSize ( void  )

Definition at line 116 of file slot.c.

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

Referenced by CreateSharedMemoryAndSemaphores(), and ReplicationSlotsShmemInit().

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

◆ ReplicationSlotValidateName()

bool ReplicationSlotValidateName ( const char *  name,
int  elevel 
)

Definition at line 174 of file slot.c.

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

Referenced by check_primary_slot_name(), 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:1156
int errcode(int sqlerrcode)
Definition: elog.c:698
#define NAMEDATALEN
static int elevel
Definition: vacuumlazy.c:400
#define ereport(elevel,...)
Definition: elog.h:157
const char * name
Definition: encode.c:515
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ SearchNamedReplicationSlot()

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

Definition at line 349 of file slot.c.

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

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

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

◆ StartupReplicationSlots()

void StartupReplicationSlots ( void  )

Definition at line 1320 of file slot.c.

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

Referenced by StartupXLOG().

1321 {
1322  DIR *replication_dir;
1323  struct dirent *replication_de;
1324 
1325  elog(DEBUG1, "starting up replication slots");
1326 
1327  /* restore all slots by iterating over all on-disk entries */
1328  replication_dir = AllocateDir("pg_replslot");
1329  while ((replication_de = ReadDir(replication_dir, "pg_replslot")) != NULL)
1330  {
1331  struct stat statbuf;
1332  char path[MAXPGPATH + 12];
1333 
1334  if (strcmp(replication_de->d_name, ".") == 0 ||
1335  strcmp(replication_de->d_name, "..") == 0)
1336  continue;
1337 
1338  snprintf(path, sizeof(path), "pg_replslot/%s", replication_de->d_name);
1339 
1340  /* we're only creating directories here, skip if it's not our's */
1341  if (lstat(path, &statbuf) == 0 && !S_ISDIR(statbuf.st_mode))
1342  continue;
1343 
1344  /* we crashed while a slot was being setup or deleted, clean up */
1345  if (pg_str_endswith(replication_de->d_name, ".tmp"))
1346  {
1347  if (!rmtree(path, true))
1348  {
1349  ereport(WARNING,
1350  (errmsg("could not remove directory \"%s\"",
1351  path)));
1352  continue;
1353  }
1354  fsync_fname("pg_replslot", true);
1355  continue;
1356  }
1357 
1358  /* looks like a slot in a normal state, restore */
1359  RestoreSlotFromDisk(replication_de->d_name);
1360  }
1361  FreeDir(replication_dir);
1362 
1363  /* currently no slots exist, we're done. */
1364  if (max_replication_slots <= 0)
1365  return;
1366 
1367  /* Now that we have recovered all the data, compute replication xmin */
1370 }
static void RestoreSlotFromDisk(const char *name)
Definition: slot.c:1595
#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:666
Definition: dirent.h:9
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:841
Definition: dirent.c:25
#define MAXPGPATH
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2634
#define WARNING
Definition: elog.h:40
bool rmtree(const char *path, bool rmtopdir)
Definition: rmtree.c:42
#define ereport(elevel,...)
Definition: elog.h:157
int max_replication_slots
Definition: slot.c:99
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2700
#define S_ISDIR(m)
Definition: win32_port.h:316
#define lstat(path, sb)
Definition: win32_port.h:276
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
char d_name[MAX_PATH]
Definition: dirent.h:15
#define snprintf
Definition: port.h:216
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:791
int FreeDir(DIR *dir)
Definition: fd.c:2752

Variable Documentation

◆ max_replication_slots

◆ MyReplicationSlot

◆ ReplicationSlotCtl

PGDLLIMPORT ReplicationSlotCtlData* ReplicationSlotCtl

Definition at line 93 of file slot.c.

Referenced by copy_replication_slot(), and pg_get_replication_slots().