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

Go to the source code of this file.

Data Structures

struct  xl_replorigin_set
 
struct  xl_replorigin_drop
 

Macros

#define XLOG_REPLORIGIN_SET   0x00
 
#define XLOG_REPLORIGIN_DROP   0x10
 
#define InvalidRepOriginId   0
 
#define DoNotReplicateId   PG_UINT16_MAX
 

Typedefs

typedef struct xl_replorigin_set xl_replorigin_set
 
typedef struct xl_replorigin_drop xl_replorigin_drop
 

Functions

RepOriginId replorigin_by_name (char *name, bool missing_ok)
 
RepOriginId replorigin_create (char *name)
 
void replorigin_drop (RepOriginId roident, bool nowait)
 
bool replorigin_by_oid (RepOriginId roident, bool missing_ok, char **roname)
 
void replorigin_advance (RepOriginId node, XLogRecPtr remote_commit, XLogRecPtr local_commit, bool go_backward, bool wal_log)
 
XLogRecPtr replorigin_get_progress (RepOriginId node, bool flush)
 
void replorigin_session_advance (XLogRecPtr remote_commit, XLogRecPtr local_commit)
 
void replorigin_session_setup (RepOriginId node)
 
void replorigin_session_reset (void)
 
XLogRecPtr replorigin_session_get_progress (bool flush)
 
void CheckPointReplicationOrigin (void)
 
void StartupReplicationOrigin (void)
 
void replorigin_redo (XLogReaderState *record)
 
void replorigin_desc (StringInfo buf, XLogReaderState *record)
 
const char * replorigin_identify (uint8 info)
 
Size ReplicationOriginShmemSize (void)
 
void ReplicationOriginShmemInit (void)
 

Variables

PGDLLIMPORT RepOriginId replorigin_session_origin
 
PGDLLIMPORT XLogRecPtr replorigin_session_origin_lsn
 
PGDLLIMPORT TimestampTz replorigin_session_origin_timestamp
 

Macro Definition Documentation

◆ DoNotReplicateId

#define DoNotReplicateId   PG_UINT16_MAX

◆ InvalidRepOriginId

◆ XLOG_REPLORIGIN_DROP

#define XLOG_REPLORIGIN_DROP   0x10

Definition at line 31 of file origin.h.

Referenced by replorigin_desc(), replorigin_drop(), replorigin_identify(), and replorigin_redo().

◆ XLOG_REPLORIGIN_SET

#define XLOG_REPLORIGIN_SET   0x00

Definition at line 30 of file origin.h.

Referenced by replorigin_advance(), replorigin_desc(), replorigin_identify(), and replorigin_redo().

Typedef Documentation

◆ xl_replorigin_drop

◆ xl_replorigin_set

Function Documentation

◆ CheckPointReplicationOrigin()

void CheckPointReplicationOrigin ( void  )

Definition at line 542 of file origin.c.

References CloseTransientFile(), COMP_CRC32C, durable_rename(), ereport, errcode_for_file_access(), errmsg(), FIN_CRC32C, i, INIT_CRC32C, InvalidRepOriginId, ReplicationState::local_lsn, ReplicationState::lock, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, OpenTransientFile(), PANIC, PG_BINARY, ReplicationState::remote_lsn, ReplicationStateOnDisk::remote_lsn, REPLICATION_STATE_MAGIC, ReplicationState::roident, ReplicationStateOnDisk::roident, write, and XLogFlush().

Referenced by CheckPointGuts().

543 {
544  const char *tmppath = "pg_logical/replorigin_checkpoint.tmp";
545  const char *path = "pg_logical/replorigin_checkpoint";
546  int tmpfd;
547  int i;
549  pg_crc32c crc;
550 
551  if (max_replication_slots == 0)
552  return;
553 
554  INIT_CRC32C(crc);
555 
556  /* make sure no old temp file is remaining */
557  if (unlink(tmppath) < 0 && errno != ENOENT)
558  ereport(PANIC,
560  errmsg("could not remove file \"%s\": %m",
561  tmppath)));
562 
563  /*
564  * no other backend can perform this at the same time, we're protected by
565  * CheckpointLock.
566  */
567  tmpfd = OpenTransientFile(tmppath,
568  O_CREAT | O_EXCL | O_WRONLY | PG_BINARY);
569  if (tmpfd < 0)
570  ereport(PANIC,
572  errmsg("could not create file \"%s\": %m",
573  tmppath)));
574 
575  /* write magic */
576  errno = 0;
577  if ((write(tmpfd, &magic, sizeof(magic))) != sizeof(magic))
578  {
579  /* if write didn't set errno, assume problem is no disk space */
580  if (errno == 0)
581  errno = ENOSPC;
582  ereport(PANIC,
584  errmsg("could not write to file \"%s\": %m",
585  tmppath)));
586  }
587  COMP_CRC32C(crc, &magic, sizeof(magic));
588 
589  /* prevent concurrent creations/drops */
590  LWLockAcquire(ReplicationOriginLock, LW_SHARED);
591 
592  /* write actual data */
593  for (i = 0; i < max_replication_slots; i++)
594  {
595  ReplicationStateOnDisk disk_state;
596  ReplicationState *curstate = &replication_states[i];
597  XLogRecPtr local_lsn;
598 
599  if (curstate->roident == InvalidRepOriginId)
600  continue;
601 
602  /* zero, to avoid uninitialized padding bytes */
603  memset(&disk_state, 0, sizeof(disk_state));
604 
605  LWLockAcquire(&curstate->lock, LW_SHARED);
606 
607  disk_state.roident = curstate->roident;
608 
609  disk_state.remote_lsn = curstate->remote_lsn;
610  local_lsn = curstate->local_lsn;
611 
612  LWLockRelease(&curstate->lock);
613 
614  /* make sure we only write out a commit that's persistent */
615  XLogFlush(local_lsn);
616 
617  errno = 0;
618  if ((write(tmpfd, &disk_state, sizeof(disk_state))) !=
619  sizeof(disk_state))
620  {
621  /* if write didn't set errno, assume problem is no disk space */
622  if (errno == 0)
623  errno = ENOSPC;
624  ereport(PANIC,
626  errmsg("could not write to file \"%s\": %m",
627  tmppath)));
628  }
629 
630  COMP_CRC32C(crc, &disk_state, sizeof(disk_state));
631  }
632 
633  LWLockRelease(ReplicationOriginLock);
634 
635  /* write out the CRC */
636  FIN_CRC32C(crc);
637  errno = 0;
638  if ((write(tmpfd, &crc, sizeof(crc))) != sizeof(crc))
639  {
640  /* if write didn't set errno, assume problem is no disk space */
641  if (errno == 0)
642  errno = ENOSPC;
643  ereport(PANIC,
645  errmsg("could not write to file \"%s\": %m",
646  tmppath)));
647  }
648 
649  if (CloseTransientFile(tmpfd) != 0)
650  ereport(PANIC,
652  errmsg("could not close file \"%s\": %m",
653  tmppath)));
654 
655  /* fsync, rename to permanent file, fsync file and directory */
656  durable_rename(tmppath, path, PANIC);
657 }
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
XLogRecPtr local_lsn
Definition: origin.c:117
#define write(a, b, c)
Definition: win32.h:14
XLogRecPtr remote_lsn
Definition: origin.c:141
uint32 pg_crc32c
Definition: pg_crc32c.h:38
RepOriginId roident
Definition: origin.c:140
#define PANIC
Definition: elog.h:53
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2805
#define PG_BINARY
Definition: c.h:1222
RepOriginId roident
Definition: origin.c:105
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2292
LWLock lock
Definition: origin.c:132
int errcode_for_file_access(void)
Definition: elog.c:631
unsigned int uint32
Definition: c.h:359
#define ereport(elevel, rest)
Definition: elog.h:141
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:643
int CloseTransientFile(int fd)
Definition: fd.c:2469
#define REPLICATION_STATE_MAGIC
Definition: origin.c:174
int max_replication_slots
Definition: slot.c:99
XLogRecPtr remote_lsn
Definition: origin.c:110
uint64 XLogRecPtr
Definition: xlogdefs.h:21
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
#define InvalidRepOriginId
Definition: origin.h:33
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
static ReplicationState * replication_states
Definition: origin.c:163
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:89
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:94

◆ ReplicationOriginShmemInit()

void ReplicationOriginShmemInit ( void  )

Definition at line 492 of file origin.c.

References ConditionVariableInit(), i, ReplicationState::lock, LWLockInitialize(), LWLockRegisterTranche(), LWTRANCHE_REPLICATION_ORIGIN, max_replication_slots, MemSet, ReplicationState::origin_cv, ReplicationOriginShmemSize(), ShmemInitStruct(), ReplicationStateCtl::states, and ReplicationStateCtl::tranche_id.

Referenced by CreateSharedMemoryAndSemaphores().

493 {
494  bool found;
495 
496  if (max_replication_slots == 0)
497  return;
498 
500  ShmemInitStruct("ReplicationOriginState",
502  &found);
504 
505  if (!found)
506  {
507  int i;
508 
510 
512 
513  for (i = 0; i < max_replication_slots; i++)
514  {
518  }
519  }
520 
522  "replication_origin");
523 }
#define MemSet(start, val, len)
Definition: c.h:962
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
Definition: lwlock.c:603
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:678
Size ReplicationOriginShmemSize(void)
Definition: origin.c:472
static ReplicationStateCtl * replication_states_ctl
Definition: origin.c:164
int max_replication_slots
Definition: slot.c:99
ReplicationState states[FLEXIBLE_ARRAY_MEMBER]
Definition: origin.c:148
int i
static ReplicationState * replication_states
Definition: origin.c:163

◆ ReplicationOriginShmemSize()

Size ReplicationOriginShmemSize ( void  )

Definition at line 472 of file origin.c.

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

Referenced by CreateSharedMemoryAndSemaphores(), and ReplicationOriginShmemInit().

473 {
474  Size size = 0;
475 
476  /*
477  * XXX: max_replication_slots is arguably the wrong thing to use, as here
478  * we keep the replay state of *remote* transactions. But for now it seems
479  * sufficient to reuse it, lest we introduce a separate GUC.
480  */
481  if (max_replication_slots == 0)
482  return size;
483 
484  size = add_size(size, offsetof(ReplicationStateCtl, states));
485 
486  size = add_size(size,
488  return size;
489 }
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:467
#define offsetof(type, field)
Definition: c.h:662

◆ replorigin_advance()

void replorigin_advance ( RepOriginId  node,
XLogRecPtr  remote_commit,
XLogRecPtr  local_commit,
bool  go_backward,
bool  wal_log 
)

Definition at line 857 of file origin.c.

References ReplicationState::acquired_by, Assert, DoNotReplicateId, ereport, errcode(), errhint(), errmsg(), ERROR, xl_replorigin_set::force, i, InvalidRepOriginId, InvalidXLogRecPtr, ReplicationState::local_lsn, ReplicationState::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), max_replication_slots, xl_replorigin_set::node_id, xl_replorigin_set::remote_lsn, ReplicationState::remote_lsn, ReplicationState::roident, XLOG_REPLORIGIN_SET, XLogBeginInsert(), XLogInsert(), and XLogRegisterData().

Referenced by pg_replication_origin_advance(), PrepareRedoAdd(), replorigin_redo(), and xact_redo_commit().

860 {
861  int i;
862  ReplicationState *replication_state = NULL;
863  ReplicationState *free_state = NULL;
864 
865  Assert(node != InvalidRepOriginId);
866 
867  /* we don't track DoNotReplicateId */
868  if (node == DoNotReplicateId)
869  return;
870 
871  /*
872  * XXX: For the case where this is called by WAL replay, it'd be more
873  * efficient to restore into a backend local hashtable and only dump into
874  * shmem after recovery is finished. Let's wait with implementing that
875  * till it's shown to be a measurable expense
876  */
877 
878  /* Lock exclusively, as we may have to create a new table entry. */
879  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
880 
881  /*
882  * Search for either an existing slot for the origin, or a free one we can
883  * use.
884  */
885  for (i = 0; i < max_replication_slots; i++)
886  {
887  ReplicationState *curstate = &replication_states[i];
888 
889  /* remember where to insert if necessary */
890  if (curstate->roident == InvalidRepOriginId &&
891  free_state == NULL)
892  {
893  free_state = curstate;
894  continue;
895  }
896 
897  /* not our slot */
898  if (curstate->roident != node)
899  {
900  continue;
901  }
902 
903  /* ok, found slot */
904  replication_state = curstate;
905 
906  LWLockAcquire(&replication_state->lock, LW_EXCLUSIVE);
907 
908  /* Make sure it's not used by somebody else */
909  if (replication_state->acquired_by != 0)
910  {
911  ereport(ERROR,
912  (errcode(ERRCODE_OBJECT_IN_USE),
913  errmsg("replication origin with OID %d is already active for PID %d",
914  replication_state->roident,
915  replication_state->acquired_by)));
916  }
917 
918  break;
919  }
920 
921  if (replication_state == NULL && free_state == NULL)
922  ereport(ERROR,
923  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
924  errmsg("could not find free replication state slot for replication origin with OID %u",
925  node),
926  errhint("Increase max_replication_slots and try again.")));
927 
928  if (replication_state == NULL)
929  {
930  /* initialize new slot */
931  LWLockAcquire(&free_state->lock, LW_EXCLUSIVE);
932  replication_state = free_state;
933  Assert(replication_state->remote_lsn == InvalidXLogRecPtr);
934  Assert(replication_state->local_lsn == InvalidXLogRecPtr);
935  replication_state->roident = node;
936  }
937 
938  Assert(replication_state->roident != InvalidRepOriginId);
939 
940  /*
941  * If somebody "forcefully" sets this slot, WAL log it, so it's durable
942  * and the standby gets the message. Primarily this will be called during
943  * WAL replay (of commit records) where no WAL logging is necessary.
944  */
945  if (wal_log)
946  {
947  xl_replorigin_set xlrec;
948 
949  xlrec.remote_lsn = remote_commit;
950  xlrec.node_id = node;
951  xlrec.force = go_backward;
952 
953  XLogBeginInsert();
954  XLogRegisterData((char *) (&xlrec), sizeof(xlrec));
955 
956  XLogInsert(RM_REPLORIGIN_ID, XLOG_REPLORIGIN_SET);
957  }
958 
959  /*
960  * Due to - harmless - race conditions during a checkpoint we could see
961  * values here that are older than the ones we already have in memory.
962  * Don't overwrite those.
963  */
964  if (go_backward || replication_state->remote_lsn < remote_commit)
965  replication_state->remote_lsn = remote_commit;
966  if (local_commit != InvalidXLogRecPtr &&
967  (go_backward || replication_state->local_lsn < local_commit))
968  replication_state->local_lsn = local_commit;
969  LWLockRelease(&replication_state->lock);
970 
971  /*
972  * Release *after* changing the LSNs, slot isn't acquired and thus could
973  * otherwise be dropped anytime.
974  */
975  LWLockRelease(ReplicationOriginLock);
976 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
int errhint(const char *fmt,...)
Definition: elog.c:1069
XLogRecPtr local_lsn
Definition: origin.c:117
#define DoNotReplicateId
Definition: origin.h:34
int errcode(int sqlerrcode)
Definition: elog.c:608
RepOriginId roident
Definition: origin.c:105
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define ERROR
Definition: elog.h:43
LWLock lock
Definition: origin.c:132
#define XLOG_REPLORIGIN_SET
Definition: origin.h:30
#define ereport(elevel, rest)
Definition: elog.h:141
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
int max_replication_slots
Definition: slot.c:99
XLogRecPtr remote_lsn
Definition: origin.c:110
#define Assert(condition)
Definition: c.h:739
RepOriginId node_id
Definition: origin.h:21
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
XLogRecPtr remote_lsn
Definition: origin.h:20
#define InvalidRepOriginId
Definition: origin.h:33
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
static ReplicationState * replication_states
Definition: origin.c:163
void XLogBeginInsert(void)
Definition: xloginsert.c:120

◆ replorigin_by_name()

RepOriginId replorigin_by_name ( char *  name,
bool  missing_ok 
)

Definition at line 208 of file origin.c.

References CStringGetTextDatum, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, HeapTupleIsValid, InvalidOid, ReleaseSysCache(), REPLORIGNAME, ReplicationState::roident, and SearchSysCache1().

Referenced by ApplyWorkerMain(), DropSubscription(), pg_replication_origin_advance(), pg_replication_origin_drop(), pg_replication_origin_oid(), pg_replication_origin_progress(), and pg_replication_origin_session_setup().

209 {
211  Oid roident = InvalidOid;
212  HeapTuple tuple;
213  Datum roname_d;
214 
215  roname_d = CStringGetTextDatum(roname);
216 
217  tuple = SearchSysCache1(REPLORIGNAME, roname_d);
218  if (HeapTupleIsValid(tuple))
219  {
220  ident = (Form_pg_replication_origin) GETSTRUCT(tuple);
221  roident = ident->roident;
222  ReleaseSysCache(tuple);
223  }
224  else if (!missing_ok)
225  ereport(ERROR,
226  (errcode(ERRCODE_UNDEFINED_OBJECT),
227  errmsg("replication origin \"%s\" does not exist",
228  roname)));
229 
230  return roident;
231 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
FormData_pg_replication_origin * Form_pg_replication_origin
#define ereport(elevel, rest)
Definition: elog.h:141
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define CStringGetTextDatum(s)
Definition: builtins.h:83

◆ replorigin_by_oid()

bool replorigin_by_oid ( RepOriginId  roident,
bool  missing_ok,
char **  roname 
)

Definition at line 431 of file origin.c.

References Assert, DoNotReplicateId, ereport, errcode(), errmsg(), ERROR, GETSTRUCT, HeapTupleIsValid, InvalidRepOriginId, ObjectIdGetDatum, OidIsValid, ReleaseSysCache(), REPLORIGIDENT, SearchSysCache1(), and text_to_cstring().

Referenced by pg_show_replication_origin_status(), and pgoutput_begin_txn().

432 {
433  HeapTuple tuple;
435 
436  Assert(OidIsValid((Oid) roident));
437  Assert(roident != InvalidRepOriginId);
438  Assert(roident != DoNotReplicateId);
439 
441  ObjectIdGetDatum((Oid) roident));
442 
443  if (HeapTupleIsValid(tuple))
444  {
445  ric = (Form_pg_replication_origin) GETSTRUCT(tuple);
446  *roname = text_to_cstring(&ric->roname);
447  ReleaseSysCache(tuple);
448 
449  return true;
450  }
451  else
452  {
453  *roname = NULL;
454 
455  if (!missing_ok)
456  ereport(ERROR,
457  (errcode(ERRCODE_UNDEFINED_OBJECT),
458  errmsg("replication origin with OID %u does not exist",
459  roident)));
460 
461  return false;
462  }
463 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
#define DoNotReplicateId
Definition: origin.h:34
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:645
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
FormData_pg_replication_origin * Form_pg_replication_origin
#define ereport(elevel, rest)
Definition: elog.h:141
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:739
#define InvalidRepOriginId
Definition: origin.h:33
char * text_to_cstring(const text *t)
Definition: varlena.c:204
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ replorigin_create()

RepOriginId replorigin_create ( char *  name)

Definition at line 239 of file origin.c.

References Assert, BTEqualStrategyNumber, CatalogTupleInsert(), CHECK_FOR_INTERRUPTS, CommandCounterIncrement(), CStringGetTextDatum, ereport, errcode(), errmsg(), ERROR, ExclusiveLock, heap_form_tuple(), heap_freetuple(), HeapTupleIsValid, InitDirtySnapshot, InvalidOid, IsTransactionState(), sort-test::key, ObjectIdGetDatum, PG_UINT16_MAX, RelationGetDescr, ReplicationOriginIdentIndex, ReplicationState::roident, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), table_close(), table_open(), and values.

Referenced by ApplyWorkerMain(), CreateSubscription(), and pg_replication_origin_create().

240 {
241  Oid roident;
242  HeapTuple tuple = NULL;
243  Relation rel;
244  Datum roname_d;
245  SnapshotData SnapshotDirty;
246  SysScanDesc scan;
248 
249  roname_d = CStringGetTextDatum(roname);
250 
252 
253  /*
254  * We need the numeric replication origin to be 16bit wide, so we cannot
255  * rely on the normal oid allocation. Instead we simply scan
256  * pg_replication_origin for the first unused id. That's not particularly
257  * efficient, but this should be a fairly infrequent operation - we can
258  * easily spend a bit more code on this when it turns out it needs to be
259  * faster.
260  *
261  * We handle concurrency by taking an exclusive lock (allowing reads!)
262  * over the table for the duration of the search. Because we use a "dirty
263  * snapshot" we can read rows that other in-progress sessions have
264  * written, even though they would be invisible with normal snapshots. Due
265  * to the exclusive lock there's no danger that new rows can appear while
266  * we're checking.
267  */
268  InitDirtySnapshot(SnapshotDirty);
269 
270  rel = table_open(ReplicationOriginRelationId, ExclusiveLock);
271 
272  for (roident = InvalidOid + 1; roident < PG_UINT16_MAX; roident++)
273  {
274  bool nulls[Natts_pg_replication_origin];
275  Datum values[Natts_pg_replication_origin];
276  bool collides;
277 
279 
280  ScanKeyInit(&key,
281  Anum_pg_replication_origin_roident,
282  BTEqualStrategyNumber, F_OIDEQ,
283  ObjectIdGetDatum(roident));
284 
286  true /* indexOK */ ,
287  &SnapshotDirty,
288  1, &key);
289 
290  collides = HeapTupleIsValid(systable_getnext(scan));
291 
292  systable_endscan(scan);
293 
294  if (!collides)
295  {
296  /*
297  * Ok, found an unused roident, insert the new row and do a CCI,
298  * so our callers can look it up if they want to.
299  */
300  memset(&nulls, 0, sizeof(nulls));
301 
302  values[Anum_pg_replication_origin_roident - 1] = ObjectIdGetDatum(roident);
303  values[Anum_pg_replication_origin_roname - 1] = roname_d;
304 
305  tuple = heap_form_tuple(RelationGetDescr(rel), values, nulls);
306  CatalogTupleInsert(rel, tuple);
308  break;
309  }
310  }
311 
312  /* now release lock again, */
314 
315  if (tuple == NULL)
316  ereport(ERROR,
317  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
318  errmsg("could not find free replication origin OID")));
319 
320  heap_freetuple(tuple);
321  return roident;
322 }
#define InitDirtySnapshot(snapshotdata)
Definition: snapmgr.h:76
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:525
#define RelationGetDescr(relation)
Definition: rel.h:448
#define ExclusiveLock
Definition: lockdefs.h:44
int errcode(int sqlerrcode)
Definition: elog.c:608
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1338
unsigned int Oid
Definition: postgres_ext.h:31
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:352
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:444
#define ReplicationOriginIdentIndex
Definition: indexing.h:340
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
#define PG_UINT16_MAX
Definition: c.h:440
#define ereport(elevel, rest)
Definition: elog.h:141
uintptr_t Datum
Definition: postgres.h:367
void CommandCounterIncrement(void)
Definition: xact.c:1005
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define Assert(condition)
Definition: c.h:739
bool IsTransactionState(void)
Definition: xact.c:355
static Datum values[MAXATTR]
Definition: bootstrap.c:167
int errmsg(const char *fmt,...)
Definition: elog.c:822
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define CStringGetTextDatum(s)
Definition: builtins.h:83
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:183
#define BTEqualStrategyNumber
Definition: stratnum.h:31

◆ replorigin_desc()

void replorigin_desc ( StringInfo  buf,
XLogReaderState record 
)

Definition at line 19 of file replorigindesc.c.

References appendStringInfo(), xl_replorigin_set::force, xl_replorigin_set::node_id, xl_replorigin_drop::node_id, xl_replorigin_set::remote_lsn, XLOG_REPLORIGIN_DROP, XLOG_REPLORIGIN_SET, XLogRecGetData, XLogRecGetInfo, and XLR_INFO_MASK.

20 {
21  char *rec = XLogRecGetData(record);
22  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
23 
24  switch (info)
25  {
27  {
28  xl_replorigin_set *xlrec;
29 
30  xlrec = (xl_replorigin_set *) rec;
31 
32  appendStringInfo(buf, "set %u; lsn %X/%X; force: %d",
33  xlrec->node_id,
34  (uint32) (xlrec->remote_lsn >> 32),
35  (uint32) xlrec->remote_lsn,
36  xlrec->force);
37  break;
38  }
40  {
41  xl_replorigin_drop *xlrec;
42 
43  xlrec = (xl_replorigin_drop *) rec;
44 
45  appendStringInfo(buf, "drop %u", xlrec->node_id);
46  break;
47  }
48  }
49 }
unsigned char uint8
Definition: c.h:357
#define XLogRecGetData(decoder)
Definition: xlogreader.h:283
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
#define XLOG_REPLORIGIN_SET
Definition: origin.h:30
unsigned int uint32
Definition: c.h:359
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:279
#define XLOG_REPLORIGIN_DROP
Definition: origin.h:31
RepOriginId node_id
Definition: origin.h:27
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
RepOriginId node_id
Definition: origin.h:21
XLogRecPtr remote_lsn
Definition: origin.h:20

◆ replorigin_drop()

void replorigin_drop ( RepOriginId  roident,
bool  nowait 
)

Definition at line 331 of file origin.c.

References ReplicationState::acquired_by, Assert, CatalogTupleDelete(), CommandCounterIncrement(), ConditionVariableCancelSleep(), ConditionVariableSleep(), elog, ereport, errcode(), errmsg(), ERROR, ExclusiveLock, HeapTupleIsValid, i, InvalidRepOriginId, InvalidXLogRecPtr, IsTransactionState(), ReplicationState::local_lsn, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), max_replication_slots, xl_replorigin_drop::node_id, ObjectIdGetDatum, ReplicationState::origin_cv, ReleaseSysCache(), ReplicationState::remote_lsn, REPLORIGIDENT, ReplicationState::roident, SearchSysCache1(), HeapTupleData::t_self, table_close(), table_open(), WAIT_EVENT_REPLICATION_ORIGIN_DROP, XLOG_REPLORIGIN_DROP, XLogBeginInsert(), XLogInsert(), and XLogRegisterData().

Referenced by DropSubscription(), and pg_replication_origin_drop().

332 {
333  HeapTuple tuple;
334  Relation rel;
335  int i;
336 
338 
339  /*
340  * To interlock against concurrent drops, we hold ExclusiveLock on
341  * pg_replication_origin throughout this function.
342  */
343  rel = table_open(ReplicationOriginRelationId, ExclusiveLock);
344 
345  /*
346  * First, clean up the slot state info, if there is any matching slot.
347  */
348 restart:
349  tuple = NULL;
350  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
351 
352  for (i = 0; i < max_replication_slots; i++)
353  {
355 
356  if (state->roident == roident)
357  {
358  /* found our slot, is it busy? */
359  if (state->acquired_by != 0)
360  {
361  ConditionVariable *cv;
362 
363  if (nowait)
364  ereport(ERROR,
365  (errcode(ERRCODE_OBJECT_IN_USE),
366  errmsg("could not drop replication origin with OID %d, in use by PID %d",
367  state->roident,
368  state->acquired_by)));
369 
370  /*
371  * We must wait and then retry. Since we don't know which CV
372  * to wait on until here, we can't readily use
373  * ConditionVariablePrepareToSleep (calling it here would be
374  * wrong, since we could miss the signal if we did so); just
375  * use ConditionVariableSleep directly.
376  */
377  cv = &state->origin_cv;
378 
379  LWLockRelease(ReplicationOriginLock);
380 
382  goto restart;
383  }
384 
385  /* first make a WAL log entry */
386  {
387  xl_replorigin_drop xlrec;
388 
389  xlrec.node_id = roident;
390  XLogBeginInsert();
391  XLogRegisterData((char *) (&xlrec), sizeof(xlrec));
392  XLogInsert(RM_REPLORIGIN_ID, XLOG_REPLORIGIN_DROP);
393  }
394 
395  /* then clear the in-memory slot */
396  state->roident = InvalidRepOriginId;
397  state->remote_lsn = InvalidXLogRecPtr;
398  state->local_lsn = InvalidXLogRecPtr;
399  break;
400  }
401  }
402  LWLockRelease(ReplicationOriginLock);
404 
405  /*
406  * Now, we can delete the catalog entry.
407  */
409  if (!HeapTupleIsValid(tuple))
410  elog(ERROR, "cache lookup failed for replication origin with oid %u",
411  roident);
412 
413  CatalogTupleDelete(rel, &tuple->t_self);
414  ReleaseSysCache(tuple);
415 
417 
418  /* now release lock again */
420 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:133
XLogRecPtr local_lsn
Definition: origin.c:117
#define ExclusiveLock
Definition: lockdefs.h:44
int errcode(int sqlerrcode)
Definition: elog.c:608
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:269
RepOriginId roident
Definition: origin.c:105
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
void ConditionVariableCancelSleep(void)
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define ereport(elevel, rest)
Definition: elog.h:141
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1116
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:323
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:415
#define XLOG_REPLORIGIN_DROP
Definition: origin.h:31
void CommandCounterIncrement(void)
Definition: xact.c:1005
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1164
int max_replication_slots
Definition: slot.c:99
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
XLogRecPtr remote_lsn
Definition: origin.c:110
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
RepOriginId node_id
Definition: origin.h:27
#define Assert(condition)
Definition: c.h:739
Definition: regguts.h:298
ConditionVariable origin_cv
Definition: origin.c:127
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
bool IsTransactionState(void)
Definition: xact.c:355
#define InvalidRepOriginId
Definition: origin.h:33
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
int i
static ReplicationState * replication_states
Definition: origin.c:163
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
void XLogBeginInsert(void)
Definition: xloginsert.c:120

◆ replorigin_get_progress()

XLogRecPtr replorigin_get_progress ( RepOriginId  node,
bool  flush 
)

Definition at line 980 of file origin.c.

References i, InvalidXLogRecPtr, ReplicationState::local_lsn, ReplicationState::lock, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, ReplicationState::remote_lsn, ReplicationState::roident, and XLogFlush().

Referenced by pg_replication_origin_progress().

981 {
982  int i;
983  XLogRecPtr local_lsn = InvalidXLogRecPtr;
984  XLogRecPtr remote_lsn = InvalidXLogRecPtr;
985 
986  /* prevent slots from being concurrently dropped */
987  LWLockAcquire(ReplicationOriginLock, LW_SHARED);
988 
989  for (i = 0; i < max_replication_slots; i++)
990  {
992 
993  state = &replication_states[i];
994 
995  if (state->roident == node)
996  {
997  LWLockAcquire(&state->lock, LW_SHARED);
998 
999  remote_lsn = state->remote_lsn;
1000  local_lsn = state->local_lsn;
1001 
1002  LWLockRelease(&state->lock);
1003 
1004  break;
1005  }
1006  }
1007 
1008  LWLockRelease(ReplicationOriginLock);
1009 
1010  if (flush && local_lsn != InvalidXLogRecPtr)
1011  XLogFlush(local_lsn);
1012 
1013  return remote_lsn;
1014 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr local_lsn
Definition: origin.c:117
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2805
RepOriginId roident
Definition: origin.c:105
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
LWLock lock
Definition: origin.c:132
int max_replication_slots
Definition: slot.c:99
XLogRecPtr remote_lsn
Definition: origin.c:110
uint64 XLogRecPtr
Definition: xlogdefs.h:21
Definition: regguts.h:298
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
int i
static ReplicationState * replication_states
Definition: origin.c:163

◆ replorigin_identify()

const char* replorigin_identify ( uint8  info)

Definition at line 52 of file replorigindesc.c.

References XLOG_REPLORIGIN_DROP, and XLOG_REPLORIGIN_SET.

53 {
54  switch (info)
55  {
57  return "SET";
59  return "DROP";
60  default:
61  return NULL;
62  }
63 }
#define XLOG_REPLORIGIN_SET
Definition: origin.h:30
#define XLOG_REPLORIGIN_DROP
Definition: origin.h:31

◆ replorigin_redo()

void replorigin_redo ( XLogReaderState record)

Definition at line 796 of file origin.c.

References elog, XLogReaderState::EndRecPtr, xl_replorigin_set::force, i, InvalidRepOriginId, InvalidXLogRecPtr, ReplicationState::local_lsn, max_replication_slots, xl_replorigin_set::node_id, xl_replorigin_drop::node_id, PANIC, xl_replorigin_set::remote_lsn, ReplicationState::remote_lsn, replorigin_advance(), ReplicationState::roident, XLOG_REPLORIGIN_DROP, XLOG_REPLORIGIN_SET, XLogRecGetData, XLogRecGetInfo, and XLR_INFO_MASK.

797 {
798  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
799 
800  switch (info)
801  {
802  case XLOG_REPLORIGIN_SET:
803  {
804  xl_replorigin_set *xlrec =
805  (xl_replorigin_set *) XLogRecGetData(record);
806 
808  xlrec->remote_lsn, record->EndRecPtr,
809  xlrec->force /* backward */ ,
810  false /* WAL log */ );
811  break;
812  }
814  {
815  xl_replorigin_drop *xlrec;
816  int i;
817 
818  xlrec = (xl_replorigin_drop *) XLogRecGetData(record);
819 
820  for (i = 0; i < max_replication_slots; i++)
821  {
823 
824  /* found our slot */
825  if (state->roident == xlrec->node_id)
826  {
827  /* reset entry */
828  state->roident = InvalidRepOriginId;
829  state->remote_lsn = InvalidXLogRecPtr;
830  state->local_lsn = InvalidXLogRecPtr;
831  break;
832  }
833  }
834  break;
835  }
836  default:
837  elog(PANIC, "replorigin_redo: unknown op code %u", info);
838  }
839 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr local_lsn
Definition: origin.c:117
unsigned char uint8
Definition: c.h:357
void replorigin_advance(RepOriginId node, XLogRecPtr remote_commit, XLogRecPtr local_commit, bool go_backward, bool wal_log)
Definition: origin.c:857
#define PANIC
Definition: elog.h:53
RepOriginId roident
Definition: origin.c:105
XLogRecPtr EndRecPtr
Definition: xlogreader.h:132
#define XLogRecGetData(decoder)
Definition: xlogreader.h:283
#define XLOG_REPLORIGIN_SET
Definition: origin.h:30
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:279
#define XLOG_REPLORIGIN_DROP
Definition: origin.h:31
int max_replication_slots
Definition: slot.c:99
XLogRecPtr remote_lsn
Definition: origin.c:110
RepOriginId node_id
Definition: origin.h:27
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
Definition: regguts.h:298
RepOriginId node_id
Definition: origin.h:21
XLogRecPtr remote_lsn
Definition: origin.h:20
#define InvalidRepOriginId
Definition: origin.h:33
#define elog(elevel,...)
Definition: elog.h:228
int i
static ReplicationState * replication_states
Definition: origin.c:163

◆ replorigin_session_advance()

void replorigin_session_advance ( XLogRecPtr  remote_commit,
XLogRecPtr  local_commit 
)

Definition at line 1170 of file origin.c.

References Assert, InvalidRepOriginId, ReplicationState::local_lsn, ReplicationState::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), ReplicationState::remote_lsn, and ReplicationState::roident.

Referenced by EndPrepare(), RecordTransactionCommit(), and RecordTransactionCommitPrepared().

1171 {
1174 
1176  if (session_replication_state->local_lsn < local_commit)
1177  session_replication_state->local_lsn = local_commit;
1178  if (session_replication_state->remote_lsn < remote_commit)
1179  session_replication_state->remote_lsn = remote_commit;
1181 }
static ReplicationState * session_replication_state
Definition: origin.c:171
XLogRecPtr local_lsn
Definition: origin.c:117
RepOriginId roident
Definition: origin.c:105
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
LWLock lock
Definition: origin.c:132
XLogRecPtr remote_lsn
Definition: origin.c:110
#define Assert(condition)
Definition: c.h:739
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
#define InvalidRepOriginId
Definition: origin.h:33

◆ replorigin_session_get_progress()

XLogRecPtr replorigin_session_get_progress ( bool  flush)

Definition at line 1188 of file origin.c.

References Assert, InvalidXLogRecPtr, ReplicationState::local_lsn, ReplicationState::lock, LW_SHARED, LWLockAcquire(), LWLockRelease(), ReplicationState::remote_lsn, and XLogFlush().

Referenced by ApplyWorkerMain(), and pg_replication_origin_session_progress().

1189 {
1190  XLogRecPtr remote_lsn;
1191  XLogRecPtr local_lsn;
1192 
1194 
1196  remote_lsn = session_replication_state->remote_lsn;
1197  local_lsn = session_replication_state->local_lsn;
1199 
1200  if (flush && local_lsn != InvalidXLogRecPtr)
1201  XLogFlush(local_lsn);
1202 
1203  return remote_lsn;
1204 }
static ReplicationState * session_replication_state
Definition: origin.c:171
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr local_lsn
Definition: origin.c:117
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2805
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
LWLock lock
Definition: origin.c:132
XLogRecPtr remote_lsn
Definition: origin.c:110
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:739
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122

◆ replorigin_session_reset()

void replorigin_session_reset ( void  )

Definition at line 1141 of file origin.c.

References ReplicationState::acquired_by, Assert, ConditionVariableBroadcast(), ereport, errcode(), errmsg(), ERROR, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), max_replication_slots, and ReplicationState::origin_cv.

Referenced by pg_replication_origin_session_reset().

1142 {
1143  ConditionVariable *cv;
1144 
1146 
1147  if (session_replication_state == NULL)
1148  ereport(ERROR,
1149  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1150  errmsg("no replication origin is configured")));
1151 
1152  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
1153 
1157 
1158  LWLockRelease(ReplicationOriginLock);
1159 
1161 }
static ReplicationState * session_replication_state
Definition: origin.c:171
void ConditionVariableBroadcast(ConditionVariable *cv)
int errcode(int sqlerrcode)
Definition: elog.c:608
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
int max_replication_slots
Definition: slot.c:99
#define Assert(condition)
Definition: c.h:739
ConditionVariable origin_cv
Definition: origin.c:127
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
int errmsg(const char *fmt,...)
Definition: elog.c:822

◆ replorigin_session_setup()

void replorigin_session_setup ( RepOriginId  node)

Definition at line 1053 of file origin.c.

References ReplicationState::acquired_by, Assert, ConditionVariableBroadcast(), ereport, errcode(), errhint(), errmsg(), ERROR, i, InvalidRepOriginId, InvalidXLogRecPtr, ReplicationState::local_lsn, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), max_replication_slots, MyProcPid, on_shmem_exit(), ReplicationState::origin_cv, ReplicationState::remote_lsn, ReplicationOriginExitCleanup(), and ReplicationState::roident.

Referenced by ApplyWorkerMain(), and pg_replication_origin_session_setup().

1054 {
1055  static bool registered_cleanup;
1056  int i;
1057  int free_slot = -1;
1058 
1059  if (!registered_cleanup)
1060  {
1062  registered_cleanup = true;
1063  }
1064 
1066 
1067  if (session_replication_state != NULL)
1068  ereport(ERROR,
1069  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1070  errmsg("cannot setup replication origin when one is already setup")));
1071 
1072  /* Lock exclusively, as we may have to create a new table entry. */
1073  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
1074 
1075  /*
1076  * Search for either an existing slot for the origin, or a free one we can
1077  * use.
1078  */
1079  for (i = 0; i < max_replication_slots; i++)
1080  {
1081  ReplicationState *curstate = &replication_states[i];
1082 
1083  /* remember where to insert if necessary */
1084  if (curstate->roident == InvalidRepOriginId &&
1085  free_slot == -1)
1086  {
1087  free_slot = i;
1088  continue;
1089  }
1090 
1091  /* not our slot */
1092  if (curstate->roident != node)
1093  continue;
1094 
1095  else if (curstate->acquired_by != 0)
1096  {
1097  ereport(ERROR,
1098  (errcode(ERRCODE_OBJECT_IN_USE),
1099  errmsg("replication origin with OID %d is already active for PID %d",
1100  curstate->roident, curstate->acquired_by)));
1101  }
1102 
1103  /* ok, found slot */
1104  session_replication_state = curstate;
1105  }
1106 
1107 
1108  if (session_replication_state == NULL && free_slot == -1)
1109  ereport(ERROR,
1110  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
1111  errmsg("could not find free replication state slot for replication origin with OID %u",
1112  node),
1113  errhint("Increase max_replication_slots and try again.")));
1114  else if (session_replication_state == NULL)
1115  {
1116  /* initialize new slot */
1121  }
1122 
1123 
1125 
1127 
1128  LWLockRelease(ReplicationOriginLock);
1129 
1130  /* probably this one is pointless */
1132 }
static ReplicationState * session_replication_state
Definition: origin.c:171
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
int MyProcPid
Definition: globals.c:40
int errhint(const char *fmt,...)
Definition: elog.c:1069
XLogRecPtr local_lsn
Definition: origin.c:117
static void ReplicationOriginExitCleanup(int code, Datum arg)
Definition: origin.c:1021
void ConditionVariableBroadcast(ConditionVariable *cv)
int errcode(int sqlerrcode)
Definition: elog.c:608
RepOriginId roident
Definition: origin.c:105
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define ERROR
Definition: elog.h:43
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
#define ereport(elevel, rest)
Definition: elog.h:141
int max_replication_slots
Definition: slot.c:99
XLogRecPtr remote_lsn
Definition: origin.c:110
#define Assert(condition)
Definition: c.h:739
ConditionVariable origin_cv
Definition: origin.c:127
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
#define InvalidRepOriginId
Definition: origin.h:33
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
static ReplicationState * replication_states
Definition: origin.c:163

◆ StartupReplicationOrigin()

void StartupReplicationOrigin ( void  )

Definition at line 668 of file origin.c.

References Assert, CloseTransientFile(), COMP_CRC32C, DEBUG2, elog, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errcode_for_file_access(), errmsg(), fd(), FIN_CRC32C, INIT_CRC32C, LOG, max_replication_slots, OpenTransientFile(), PANIC, PG_BINARY, read, ReplicationState::remote_lsn, ReplicationStateOnDisk::remote_lsn, REPLICATION_STATE_MAGIC, ReplicationState::roident, and ReplicationStateOnDisk::roident.

Referenced by StartupXLOG().

669 {
670  const char *path = "pg_logical/replorigin_checkpoint";
671  int fd;
672  int readBytes;
674  int last_state = 0;
675  pg_crc32c file_crc;
676  pg_crc32c crc;
677 
678  /* don't want to overwrite already existing state */
679 #ifdef USE_ASSERT_CHECKING
680  static bool already_started = false;
681 
682  Assert(!already_started);
683  already_started = true;
684 #endif
685 
686  if (max_replication_slots == 0)
687  return;
688 
689  INIT_CRC32C(crc);
690 
691  elog(DEBUG2, "starting up replication origin progress state");
692 
693  fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
694 
695  /*
696  * might have had max_replication_slots == 0 last run, or we just brought
697  * up a standby.
698  */
699  if (fd < 0 && errno == ENOENT)
700  return;
701  else if (fd < 0)
702  ereport(PANIC,
704  errmsg("could not open file \"%s\": %m",
705  path)));
706 
707  /* verify magic, that is written even if nothing was active */
708  readBytes = read(fd, &magic, sizeof(magic));
709  if (readBytes != sizeof(magic))
710  {
711  if (readBytes < 0)
712  ereport(PANIC,
714  errmsg("could not read file \"%s\": %m",
715  path)));
716  else
717  ereport(PANIC,
719  errmsg("could not read file \"%s\": read %d of %zu",
720  path, readBytes, sizeof(magic))));
721  }
722  COMP_CRC32C(crc, &magic, sizeof(magic));
723 
724  if (magic != REPLICATION_STATE_MAGIC)
725  ereport(PANIC,
726  (errmsg("replication checkpoint has wrong magic %u instead of %u",
727  magic, REPLICATION_STATE_MAGIC)));
728 
729  /* we can skip locking here, no other access is possible */
730 
731  /* recover individual states, until there are no more to be found */
732  while (true)
733  {
734  ReplicationStateOnDisk disk_state;
735 
736  readBytes = read(fd, &disk_state, sizeof(disk_state));
737 
738  /* no further data */
739  if (readBytes == sizeof(crc))
740  {
741  /* not pretty, but simple ... */
742  file_crc = *(pg_crc32c *) &disk_state;
743  break;
744  }
745 
746  if (readBytes < 0)
747  {
748  ereport(PANIC,
750  errmsg("could not read file \"%s\": %m",
751  path)));
752  }
753 
754  if (readBytes != sizeof(disk_state))
755  {
756  ereport(PANIC,
758  errmsg("could not read file \"%s\": read %d of %zu",
759  path, readBytes, sizeof(disk_state))));
760  }
761 
762  COMP_CRC32C(crc, &disk_state, sizeof(disk_state));
763 
764  if (last_state == max_replication_slots)
765  ereport(PANIC,
766  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
767  errmsg("could not find free replication state, increase max_replication_slots")));
768 
769  /* copy data to shared memory */
770  replication_states[last_state].roident = disk_state.roident;
771  replication_states[last_state].remote_lsn = disk_state.remote_lsn;
772  last_state++;
773 
774  elog(LOG, "recovered replication state of node %u to %X/%X",
775  disk_state.roident,
776  (uint32) (disk_state.remote_lsn >> 32),
777  (uint32) disk_state.remote_lsn);
778  }
779 
780  /* now check checksum */
781  FIN_CRC32C(crc);
782  if (file_crc != crc)
783  ereport(PANIC,
784  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
785  errmsg("replication slot checkpoint has wrong checksum %u, expected %u",
786  crc, file_crc)));
787 
788  if (CloseTransientFile(fd) != 0)
789  ereport(PANIC,
791  errmsg("could not close file \"%s\": %m",
792  path)));
793 }
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
XLogRecPtr remote_lsn
Definition: origin.c:141
uint32 pg_crc32c
Definition: pg_crc32c.h:38
RepOriginId roident
Definition: origin.c:140
int errcode(int sqlerrcode)
Definition: elog.c:608
#define LOG
Definition: elog.h:26
#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:1222
RepOriginId roident
Definition: origin.c:105
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2292
#define DEBUG2
Definition: elog.h:24
int errcode_for_file_access(void)
Definition: elog.c:631
unsigned int uint32
Definition: c.h:359
#define ereport(elevel, rest)
Definition: elog.h:141
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:45
int CloseTransientFile(int fd)
Definition: fd.c:2469
#define REPLICATION_STATE_MAGIC
Definition: origin.c:174
int max_replication_slots
Definition: slot.c:99
XLogRecPtr remote_lsn
Definition: origin.c:110
#define Assert(condition)
Definition: c.h:739
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
static ReplicationState * replication_states
Definition: origin.c:163
#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

Variable Documentation

◆ replorigin_session_origin

◆ replorigin_session_origin_lsn

◆ replorigin_session_origin_timestamp