PostgreSQL Source Code  git master
origin.h File Reference
#include "fmgr.h"
#include "access/xlog.h"
#include "access/xlogdefs.h"
#include "access/xlogreader.h"
#include "catalog/pg_replication_origin.h"
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 32 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 31 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 528 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().

529 {
530  const char *tmppath = "pg_logical/replorigin_checkpoint.tmp";
531  const char *path = "pg_logical/replorigin_checkpoint";
532  int tmpfd;
533  int i;
535  pg_crc32c crc;
536 
537  if (max_replication_slots == 0)
538  return;
539 
540  INIT_CRC32C(crc);
541 
542  /* make sure no old temp file is remaining */
543  if (unlink(tmppath) < 0 && errno != ENOENT)
544  ereport(PANIC,
546  errmsg("could not remove file \"%s\": %m",
547  tmppath)));
548 
549  /*
550  * no other backend can perform this at the same time, we're protected by
551  * CheckpointLock.
552  */
553  tmpfd = OpenTransientFile(tmppath,
554  O_CREAT | O_EXCL | O_WRONLY | PG_BINARY);
555  if (tmpfd < 0)
556  ereport(PANIC,
558  errmsg("could not create file \"%s\": %m",
559  tmppath)));
560 
561  /* write magic */
562  if ((write(tmpfd, &magic, sizeof(magic))) != sizeof(magic))
563  {
564  CloseTransientFile(tmpfd);
565  ereport(PANIC,
567  errmsg("could not write to file \"%s\": %m",
568  tmppath)));
569  }
570  COMP_CRC32C(crc, &magic, sizeof(magic));
571 
572  /* prevent concurrent creations/drops */
573  LWLockAcquire(ReplicationOriginLock, LW_SHARED);
574 
575  /* write actual data */
576  for (i = 0; i < max_replication_slots; i++)
577  {
578  ReplicationStateOnDisk disk_state;
579  ReplicationState *curstate = &replication_states[i];
580  XLogRecPtr local_lsn;
581 
582  if (curstate->roident == InvalidRepOriginId)
583  continue;
584 
585  /* zero, to avoid uninitialized padding bytes */
586  memset(&disk_state, 0, sizeof(disk_state));
587 
588  LWLockAcquire(&curstate->lock, LW_SHARED);
589 
590  disk_state.roident = curstate->roident;
591 
592  disk_state.remote_lsn = curstate->remote_lsn;
593  local_lsn = curstate->local_lsn;
594 
595  LWLockRelease(&curstate->lock);
596 
597  /* make sure we only write out a commit that's persistent */
598  XLogFlush(local_lsn);
599 
600  if ((write(tmpfd, &disk_state, sizeof(disk_state))) !=
601  sizeof(disk_state))
602  {
603  CloseTransientFile(tmpfd);
604  ereport(PANIC,
606  errmsg("could not write to file \"%s\": %m",
607  tmppath)));
608  }
609 
610  COMP_CRC32C(crc, &disk_state, sizeof(disk_state));
611  }
612 
613  LWLockRelease(ReplicationOriginLock);
614 
615  /* write out the CRC */
616  FIN_CRC32C(crc);
617  if ((write(tmpfd, &crc, sizeof(crc))) != sizeof(crc))
618  {
619  CloseTransientFile(tmpfd);
620  ereport(PANIC,
622  errmsg("could not write to file \"%s\": %m",
623  tmppath)));
624  }
625 
626  CloseTransientFile(tmpfd);
627 
628  /* fsync, rename to permanent file, fsync file and directory */
629  durable_rename(tmppath, path, PANIC);
630 }
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
XLogRecPtr local_lsn
Definition: origin.c:120
#define write(a, b, c)
Definition: win32.h:14
XLogRecPtr remote_lsn
Definition: origin.c:144
uint32 pg_crc32c
Definition: pg_crc32c.h:38
RepOriginId roident
Definition: origin.c:143
#define PANIC
Definition: elog.h:53
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2763
#define PG_BINARY
Definition: c.h:1025
RepOriginId roident
Definition: origin.c:108
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1722
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2392
LWLock lock
Definition: origin.c:135
int errcode_for_file_access(void)
Definition: elog.c:598
unsigned int uint32
Definition: c.h:296
#define ereport(elevel, rest)
Definition: elog.h:122
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:607
int CloseTransientFile(int fd)
Definition: fd.c:2562
#define REPLICATION_STATE_MAGIC
Definition: origin.c:177
int max_replication_slots
Definition: slot.c:99
XLogRecPtr remote_lsn
Definition: origin.c:113
uint64 XLogRecPtr
Definition: xlogdefs.h:21
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1118
#define InvalidRepOriginId
Definition: origin.h:34
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
static ReplicationState * replication_states
Definition: origin.c:166
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:73
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:78

◆ ReplicationOriginShmemInit()

void ReplicationOriginShmemInit ( void  )

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

479 {
480  bool found;
481 
482  if (max_replication_slots == 0)
483  return;
484 
486  ShmemInitStruct("ReplicationOriginState",
488  &found);
490 
491  if (!found)
492  {
493  int i;
494 
496 
498 
499  for (i = 0; i < max_replication_slots; i++)
500  {
504  }
505  }
506 
508  "replication_origin");
509 }
#define MemSet(start, val, len)
Definition: c.h:853
void LWLockRegisterTranche(int tranche_id, const char *tranche_name)
Definition: lwlock.c:599
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:674
Size ReplicationOriginShmemSize(void)
Definition: origin.c:458
static ReplicationStateCtl * replication_states_ctl
Definition: origin.c:167
int max_replication_slots
Definition: slot.c:99
ReplicationState states[FLEXIBLE_ARRAY_MEMBER]
Definition: origin.c:151
int i
static ReplicationState * replication_states
Definition: origin.c:166

◆ ReplicationOriginShmemSize()

Size ReplicationOriginShmemSize ( void  )

Definition at line 458 of file origin.c.

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

Referenced by CreateSharedMemoryAndSemaphores(), and ReplicationOriginShmemInit().

459 {
460  Size size = 0;
461 
462  /*
463  * XXX: max_replication_slots is arguably the wrong thing to use, as here
464  * we keep the replay state of *remote* transactions. But for now it seems
465  * sufficient to reuse it, lest we introduce a separate GUC.
466  */
467  if (max_replication_slots == 0)
468  return size;
469 
470  size = add_size(size, offsetof(ReplicationStateCtl, states));
471 
472  size = add_size(size,
474  return size;
475 }
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:404
#define offsetof(type, field)
Definition: c.h:593

◆ replorigin_advance()

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

Definition at line 817 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(), replorigin_redo(), and xact_redo_commit().

820 {
821  int i;
822  ReplicationState *replication_state = NULL;
823  ReplicationState *free_state = NULL;
824 
825  Assert(node != InvalidRepOriginId);
826 
827  /* we don't track DoNotReplicateId */
828  if (node == DoNotReplicateId)
829  return;
830 
831  /*
832  * XXX: For the case where this is called by WAL replay, it'd be more
833  * efficient to restore into a backend local hashtable and only dump into
834  * shmem after recovery is finished. Let's wait with implementing that
835  * till it's shown to be a measurable expense
836  */
837 
838  /* Lock exclusively, as we may have to create a new table entry. */
839  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
840 
841  /*
842  * Search for either an existing slot for the origin, or a free one we can
843  * use.
844  */
845  for (i = 0; i < max_replication_slots; i++)
846  {
847  ReplicationState *curstate = &replication_states[i];
848 
849  /* remember where to insert if necessary */
850  if (curstate->roident == InvalidRepOriginId &&
851  free_state == NULL)
852  {
853  free_state = curstate;
854  continue;
855  }
856 
857  /* not our slot */
858  if (curstate->roident != node)
859  {
860  continue;
861  }
862 
863  /* ok, found slot */
864  replication_state = curstate;
865 
866  LWLockAcquire(&replication_state->lock, LW_EXCLUSIVE);
867 
868  /* Make sure it's not used by somebody else */
869  if (replication_state->acquired_by != 0)
870  {
871  ereport(ERROR,
872  (errcode(ERRCODE_OBJECT_IN_USE),
873  errmsg("replication origin with OID %d is already active for PID %d",
874  replication_state->roident,
875  replication_state->acquired_by)));
876  }
877 
878  break;
879  }
880 
881  if (replication_state == NULL && free_state == NULL)
882  ereport(ERROR,
883  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
884  errmsg("could not find free replication state slot for replication origin with OID %u",
885  node),
886  errhint("Increase max_replication_slots and try again.")));
887 
888  if (replication_state == NULL)
889  {
890  /* initialize new slot */
891  LWLockAcquire(&free_state->lock, LW_EXCLUSIVE);
892  replication_state = free_state;
893  Assert(replication_state->remote_lsn == InvalidXLogRecPtr);
894  Assert(replication_state->local_lsn == InvalidXLogRecPtr);
895  replication_state->roident = node;
896  }
897 
898  Assert(replication_state->roident != InvalidRepOriginId);
899 
900  /*
901  * If somebody "forcefully" sets this slot, WAL log it, so it's durable
902  * and the standby gets the message. Primarily this will be called during
903  * WAL replay (of commit records) where no WAL logging is necessary.
904  */
905  if (wal_log)
906  {
907  xl_replorigin_set xlrec;
908 
909  xlrec.remote_lsn = remote_commit;
910  xlrec.node_id = node;
911  xlrec.force = go_backward;
912 
913  XLogBeginInsert();
914  XLogRegisterData((char *) (&xlrec), sizeof(xlrec));
915 
916  XLogInsert(RM_REPLORIGIN_ID, XLOG_REPLORIGIN_SET);
917  }
918 
919  /*
920  * Due to - harmless - race conditions during a checkpoint we could see
921  * values here that are older than the ones we already have in memory.
922  * Don't overwrite those.
923  */
924  if (go_backward || replication_state->remote_lsn < remote_commit)
925  replication_state->remote_lsn = remote_commit;
926  if (local_commit != InvalidXLogRecPtr &&
927  (go_backward || replication_state->local_lsn < local_commit))
928  replication_state->local_lsn = local_commit;
929  LWLockRelease(&replication_state->lock);
930 
931  /*
932  * Release *after* changing the LSNs, slot isn't acquired and thus could
933  * otherwise be dropped anytime.
934  */
935  LWLockRelease(ReplicationOriginLock);
936 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
int errhint(const char *fmt,...)
Definition: elog.c:987
XLogRecPtr local_lsn
Definition: origin.c:120
#define DoNotReplicateId
Definition: origin.h:35
int errcode(int sqlerrcode)
Definition: elog.c:575
RepOriginId roident
Definition: origin.c:108
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1722
#define ERROR
Definition: elog.h:43
LWLock lock
Definition: origin.c:135
#define XLOG_REPLORIGIN_SET
Definition: origin.h:31
#define ereport(elevel, rest)
Definition: elog.h:122
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:113
#define Assert(condition)
Definition: c.h:670
RepOriginId node_id
Definition: origin.h:22
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1118
XLogRecPtr remote_lsn
Definition: origin.h:21
#define InvalidRepOriginId
Definition: origin.h:34
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
static ReplicationState * replication_states
Definition: origin.c:166
void XLogBeginInsert(void)
Definition: xloginsert.c:120

◆ replorigin_by_name()

RepOriginId replorigin_by_name ( char *  name,
bool  missing_ok 
)

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

212 {
214  Oid roident = InvalidOid;
215  HeapTuple tuple;
216  Datum roname_d;
217 
218  roname_d = CStringGetTextDatum(roname);
219 
220  tuple = SearchSysCache1(REPLORIGNAME, roname_d);
221  if (HeapTupleIsValid(tuple))
222  {
223  ident = (Form_pg_replication_origin) GETSTRUCT(tuple);
224  roident = ident->roident;
225  ReleaseSysCache(tuple);
226  }
227  else if (!missing_ok)
228  ereport(ERROR,
229  (errcode(ERRCODE_UNDEFINED_OBJECT),
230  errmsg("replication origin \"%s\" does not exist",
231  roname)));
232 
233  return roident;
234 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
int errcode(int sqlerrcode)
Definition: elog.c:575
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:122
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CStringGetTextDatum(s)
Definition: builtins.h:91

◆ replorigin_by_oid()

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

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

418 {
419  HeapTuple tuple;
421 
422  Assert(OidIsValid((Oid) roident));
423  Assert(roident != InvalidRepOriginId);
424  Assert(roident != DoNotReplicateId);
425 
427  ObjectIdGetDatum((Oid) roident));
428 
429  if (HeapTupleIsValid(tuple))
430  {
431  ric = (Form_pg_replication_origin) GETSTRUCT(tuple);
432  *roname = text_to_cstring(&ric->roname);
433  ReleaseSysCache(tuple);
434 
435  return true;
436  }
437  else
438  {
439  *roname = NULL;
440 
441  if (!missing_ok)
442  ereport(ERROR,
443  (errcode(ERRCODE_UNDEFINED_OBJECT),
444  errmsg("replication origin with OID %u does not exist",
445  roident)));
446 
447  return false;
448  }
449 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
#define DoNotReplicateId
Definition: origin.h:35
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:576
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
FormData_pg_replication_origin * Form_pg_replication_origin
#define ereport(elevel, rest)
Definition: elog.h:122
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:670
#define InvalidRepOriginId
Definition: origin.h:34
char * text_to_cstring(const text *t)
Definition: varlena.c:182
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ replorigin_create()

RepOriginId replorigin_create ( char *  name)

Definition at line 242 of file origin.c.

References Anum_pg_replication_origin_roident, Anum_pg_replication_origin_roname, Assert, BTEqualStrategyNumber, CatalogTupleInsert(), CHECK_FOR_INTERRUPTS, CommandCounterIncrement(), CStringGetTextDatum, ereport, errcode(), errmsg(), ERROR, ExclusiveLock, heap_close, heap_form_tuple(), heap_freetuple(), heap_open(), HeapTupleIsValid, InitDirtySnapshot, InvalidOid, IsTransactionState(), Natts_pg_replication_origin, ObjectIdGetDatum, PG_UINT16_MAX, RelationGetDescr, ReplicationOriginIdentIndex, ReplicationOriginRelationId, ReplicationState::roident, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), and values.

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

243 {
244  Oid roident;
245  HeapTuple tuple = NULL;
246  Relation rel;
247  Datum roname_d;
248  SnapshotData SnapshotDirty;
249  SysScanDesc scan;
250  ScanKeyData key;
251 
252  roname_d = CStringGetTextDatum(roname);
253 
255 
256  /*
257  * We need the numeric replication origin to be 16bit wide, so we cannot
258  * rely on the normal oid allocation. Instead we simply scan
259  * pg_replication_origin for the first unused id. That's not particularly
260  * efficient, but this should be a fairly infrequent operation - we can
261  * easily spend a bit more code on this when it turns out it needs to be
262  * faster.
263  *
264  * We handle concurrency by taking an exclusive lock (allowing reads!)
265  * over the table for the duration of the search. Because we use a "dirty
266  * snapshot" we can read rows that other in-progress sessions have
267  * written, even though they would be invisible with normal snapshots. Due
268  * to the exclusive lock there's no danger that new rows can appear while
269  * we're checking.
270  */
271  InitDirtySnapshot(SnapshotDirty);
272 
274 
275  for (roident = InvalidOid + 1; roident < PG_UINT16_MAX; roident++)
276  {
277  bool nulls[Natts_pg_replication_origin];
279  bool collides;
280 
282 
283  ScanKeyInit(&key,
285  BTEqualStrategyNumber, F_OIDEQ,
286  ObjectIdGetDatum(roident));
287 
289  true /* indexOK */ ,
290  &SnapshotDirty,
291  1, &key);
292 
293  collides = HeapTupleIsValid(systable_getnext(scan));
294 
295  systable_endscan(scan);
296 
297  if (!collides)
298  {
299  /*
300  * Ok, found an unused roident, insert the new row and do a CCI,
301  * so our callers can look it up if they want to.
302  */
303  memset(&nulls, 0, sizeof(nulls));
304 
306  values[Anum_pg_replication_origin_roname - 1] = roname_d;
307 
308  tuple = heap_form_tuple(RelationGetDescr(rel), values, nulls);
309  CatalogTupleInsert(rel, tuple);
311  break;
312  }
313  }
314 
315  /* now release lock again, */
317 
318  if (tuple == NULL)
319  ereport(ERROR,
320  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
321  errmsg("could not find free replication origin OID")));
322 
323  heap_freetuple(tuple);
324  return roident;
325 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define Anum_pg_replication_origin_roident
#define RelationGetDescr(relation)
Definition: rel.h:437
#define ExclusiveLock
Definition: lockdefs.h:44
int errcode(int sqlerrcode)
Definition: elog.c:575
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1373
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:328
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:416
#define Anum_pg_replication_origin_roname
#define ReplicationOriginIdentIndex
Definition: indexing.h:333
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define PG_UINT16_MAX
Definition: c.h:377
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:162
#define InitDirtySnapshot(snapshotdata)
Definition: tqual.h:103
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:372
void CommandCounterIncrement(void)
Definition: xact.c:915
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:670
bool IsTransactionState(void)
Definition: xact.c:351
#define Natts_pg_replication_origin
static Datum values[MAXATTR]
Definition: bootstrap.c:164
#define ReplicationOriginRelationId
int errmsg(const char *fmt,...)
Definition: elog.c:797
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Definition: scankey.c:76
#define CStringGetTextDatum(s)
Definition: builtins.h:91
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#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:294
#define XLogRecGetData(decoder)
Definition: xlogreader.h:226
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:78
#define XLOG_REPLORIGIN_SET
Definition: origin.h:31
unsigned int uint32
Definition: c.h:296
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:222
#define XLOG_REPLORIGIN_DROP
Definition: origin.h:32
RepOriginId node_id
Definition: origin.h:28
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
RepOriginId node_id
Definition: origin.h:22
XLogRecPtr remote_lsn
Definition: origin.h:21

◆ replorigin_drop()

void replorigin_drop ( RepOriginId  roident,
bool  nowait 
)

Definition at line 334 of file origin.c.

References ReplicationState::acquired_by, Assert, CatalogTupleDelete(), CommandCounterIncrement(), ConditionVariableCancelSleep(), ConditionVariablePrepareToSleep(), ConditionVariableSleep(), elog, ereport, errcode(), errmsg(), ERROR, ExclusiveLock, heap_close, heap_open(), 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, ReplicationOriginRelationId, REPLORIGIDENT, ReplicationState::roident, SearchSysCache1(), HeapTupleData::t_self, WAIT_EVENT_REPLICATION_ORIGIN_DROP, XLOG_REPLORIGIN_DROP, XLogBeginInsert(), XLogInsert(), and XLogRegisterData().

Referenced by DropSubscription(), and pg_replication_origin_drop().

335 {
336  HeapTuple tuple;
337  Relation rel;
338  int i;
339 
341 
343 
344 restart:
345  tuple = NULL;
346  /* cleanup the slot state info */
347  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
348 
349  for (i = 0; i < max_replication_slots; i++)
350  {
352 
353  /* found our slot */
354  if (state->roident == roident)
355  {
356  if (state->acquired_by != 0)
357  {
358  ConditionVariable *cv;
359 
360  if (nowait)
361  ereport(ERROR,
362  (errcode(ERRCODE_OBJECT_IN_USE),
363  errmsg("could not drop replication origin with OID %d, in use by PID %d",
364  state->roident,
365  state->acquired_by)));
366  cv = &state->origin_cv;
367 
368  LWLockRelease(ReplicationOriginLock);
372  goto restart;
373  }
374 
375  /* first WAL log */
376  {
377  xl_replorigin_drop xlrec;
378 
379  xlrec.node_id = roident;
380  XLogBeginInsert();
381  XLogRegisterData((char *) (&xlrec), sizeof(xlrec));
382  XLogInsert(RM_REPLORIGIN_ID, XLOG_REPLORIGIN_DROP);
383  }
384 
385  /* then reset the in-memory entry */
386  state->roident = InvalidRepOriginId;
387  state->remote_lsn = InvalidXLogRecPtr;
388  state->local_lsn = InvalidXLogRecPtr;
389  break;
390  }
391  }
392  LWLockRelease(ReplicationOriginLock);
393 
395  if (!HeapTupleIsValid(tuple))
396  elog(ERROR, "cache lookup failed for replication origin with oid %u",
397  roident);
398 
399  CatalogTupleDelete(rel, &tuple->t_self);
400  ReleaseSysCache(tuple);
401 
403 
404  /* now release lock again */
406 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr local_lsn
Definition: origin.c:120
#define ExclusiveLock
Definition: lockdefs.h:44
int errcode(int sqlerrcode)
Definition: elog.c:575
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
Definition: indexing.c:255
#define heap_close(r, l)
Definition: heapam.h:97
RepOriginId roident
Definition: origin.c:108
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1722
void ConditionVariablePrepareToSleep(ConditionVariable *cv)
void ConditionVariableCancelSleep(void)
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
ItemPointerData t_self
Definition: htup.h:65
#define ereport(elevel, rest)
Definition: elog.h:122
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
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:32
void CommandCounterIncrement(void)
Definition: xact.c:915
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1290
int max_replication_slots
Definition: slot.c:99
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
XLogRecPtr remote_lsn
Definition: origin.c:113
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
RepOriginId node_id
Definition: origin.h:28
#define Assert(condition)
Definition: c.h:670
Definition: regguts.h:298
ConditionVariable origin_cv
Definition: origin.c:130
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1118
bool IsTransactionState(void)
Definition: xact.c:351
#define InvalidRepOriginId
Definition: origin.h:34
#define ReplicationOriginRelationId
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
static ReplicationState * replication_states
Definition: origin.c:166
#define elog
Definition: elog.h:219
void XLogBeginInsert(void)
Definition: xloginsert.c:120

◆ replorigin_get_progress()

XLogRecPtr replorigin_get_progress ( RepOriginId  node,
bool  flush 
)

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

941 {
942  int i;
943  XLogRecPtr local_lsn = InvalidXLogRecPtr;
944  XLogRecPtr remote_lsn = InvalidXLogRecPtr;
945 
946  /* prevent slots from being concurrently dropped */
947  LWLockAcquire(ReplicationOriginLock, LW_SHARED);
948 
949  for (i = 0; i < max_replication_slots; i++)
950  {
952 
953  state = &replication_states[i];
954 
955  if (state->roident == node)
956  {
957  LWLockAcquire(&state->lock, LW_SHARED);
958 
959  remote_lsn = state->remote_lsn;
960  local_lsn = state->local_lsn;
961 
962  LWLockRelease(&state->lock);
963 
964  break;
965  }
966  }
967 
968  LWLockRelease(ReplicationOriginLock);
969 
970  if (flush && local_lsn != InvalidXLogRecPtr)
971  XLogFlush(local_lsn);
972 
973  return remote_lsn;
974 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr local_lsn
Definition: origin.c:120
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2763
RepOriginId roident
Definition: origin.c:108
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1722
LWLock lock
Definition: origin.c:135
int max_replication_slots
Definition: slot.c:99
XLogRecPtr remote_lsn
Definition: origin.c:113
uint64 XLogRecPtr
Definition: xlogdefs.h:21
Definition: regguts.h:298
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1118
int i
static ReplicationState * replication_states
Definition: origin.c:166

◆ 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:31
#define XLOG_REPLORIGIN_DROP
Definition: origin.h:32

◆ replorigin_redo()

void replorigin_redo ( XLogReaderState record)

Definition at line 756 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.

757 {
758  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
759 
760  switch (info)
761  {
762  case XLOG_REPLORIGIN_SET:
763  {
764  xl_replorigin_set *xlrec =
765  (xl_replorigin_set *) XLogRecGetData(record);
766 
768  xlrec->remote_lsn, record->EndRecPtr,
769  xlrec->force /* backward */ ,
770  false /* WAL log */ );
771  break;
772  }
774  {
775  xl_replorigin_drop *xlrec;
776  int i;
777 
778  xlrec = (xl_replorigin_drop *) XLogRecGetData(record);
779 
780  for (i = 0; i < max_replication_slots; i++)
781  {
783 
784  /* found our slot */
785  if (state->roident == xlrec->node_id)
786  {
787  /* reset entry */
788  state->roident = InvalidRepOriginId;
789  state->remote_lsn = InvalidXLogRecPtr;
790  state->local_lsn = InvalidXLogRecPtr;
791  break;
792  }
793  }
794  break;
795  }
796  default:
797  elog(PANIC, "replorigin_redo: unknown op code %u", info);
798  }
799 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr local_lsn
Definition: origin.c:120
unsigned char uint8
Definition: c.h:294
void replorigin_advance(RepOriginId node, XLogRecPtr remote_commit, XLogRecPtr local_commit, bool go_backward, bool wal_log)
Definition: origin.c:817
#define PANIC
Definition: elog.h:53
RepOriginId roident
Definition: origin.c:108
XLogRecPtr EndRecPtr
Definition: xlogreader.h:120
#define XLogRecGetData(decoder)
Definition: xlogreader.h:226
#define XLOG_REPLORIGIN_SET
Definition: origin.h:31
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:222
#define XLOG_REPLORIGIN_DROP
Definition: origin.h:32
int max_replication_slots
Definition: slot.c:99
XLogRecPtr remote_lsn
Definition: origin.c:113
RepOriginId node_id
Definition: origin.h:28
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
Definition: regguts.h:298
RepOriginId node_id
Definition: origin.h:22
XLogRecPtr remote_lsn
Definition: origin.h:21
#define InvalidRepOriginId
Definition: origin.h:34
int i
static ReplicationState * replication_states
Definition: origin.c:166
#define elog
Definition: elog.h:219

◆ replorigin_session_advance()

void replorigin_session_advance ( XLogRecPtr  remote_commit,
XLogRecPtr  local_commit 
)

Definition at line 1130 of file origin.c.

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

Referenced by RecordTransactionCommit(), and RecordTransactionCommitPrepared().

1131 {
1134 
1136  if (session_replication_state->local_lsn < local_commit)
1137  session_replication_state->local_lsn = local_commit;
1138  if (session_replication_state->remote_lsn < remote_commit)
1139  session_replication_state->remote_lsn = remote_commit;
1141 }
static ReplicationState * session_replication_state
Definition: origin.c:174
XLogRecPtr local_lsn
Definition: origin.c:120
RepOriginId roident
Definition: origin.c:108
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1722
LWLock lock
Definition: origin.c:135
XLogRecPtr remote_lsn
Definition: origin.c:113
#define Assert(condition)
Definition: c.h:670
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1118
#define InvalidRepOriginId
Definition: origin.h:34

◆ replorigin_session_get_progress()

XLogRecPtr replorigin_session_get_progress ( bool  flush)

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

1149 {
1150  XLogRecPtr remote_lsn;
1151  XLogRecPtr local_lsn;
1152 
1154 
1156  remote_lsn = session_replication_state->remote_lsn;
1157  local_lsn = session_replication_state->local_lsn;
1159 
1160  if (flush && local_lsn != InvalidXLogRecPtr)
1161  XLogFlush(local_lsn);
1162 
1163  return remote_lsn;
1164 }
static ReplicationState * session_replication_state
Definition: origin.c:174
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr local_lsn
Definition: origin.c:120
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2763
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1722
LWLock lock
Definition: origin.c:135
XLogRecPtr remote_lsn
Definition: origin.c:113
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:670
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1118

◆ replorigin_session_reset()

void replorigin_session_reset ( void  )

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

1102 {
1103  ConditionVariable *cv;
1104 
1106 
1107  if (session_replication_state == NULL)
1108  ereport(ERROR,
1109  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1110  errmsg("no replication origin is configured")));
1111 
1112  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
1113 
1117 
1118  LWLockRelease(ReplicationOriginLock);
1119 
1121 }
static ReplicationState * session_replication_state
Definition: origin.c:174
int ConditionVariableBroadcast(ConditionVariable *cv)
int errcode(int sqlerrcode)
Definition: elog.c:575
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1722
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int max_replication_slots
Definition: slot.c:99
#define Assert(condition)
Definition: c.h:670
ConditionVariable origin_cv
Definition: origin.c:130
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1118
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ replorigin_session_setup()

void replorigin_session_setup ( RepOriginId  node)

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

1014 {
1015  static bool registered_cleanup;
1016  int i;
1017  int free_slot = -1;
1018 
1019  if (!registered_cleanup)
1020  {
1022  registered_cleanup = true;
1023  }
1024 
1026 
1027  if (session_replication_state != NULL)
1028  ereport(ERROR,
1029  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1030  errmsg("cannot setup replication origin when one is already setup")));
1031 
1032  /* Lock exclusively, as we may have to create a new table entry. */
1033  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
1034 
1035  /*
1036  * Search for either an existing slot for the origin, or a free one we can
1037  * use.
1038  */
1039  for (i = 0; i < max_replication_slots; i++)
1040  {
1041  ReplicationState *curstate = &replication_states[i];
1042 
1043  /* remember where to insert if necessary */
1044  if (curstate->roident == InvalidRepOriginId &&
1045  free_slot == -1)
1046  {
1047  free_slot = i;
1048  continue;
1049  }
1050 
1051  /* not our slot */
1052  if (curstate->roident != node)
1053  continue;
1054 
1055  else if (curstate->acquired_by != 0)
1056  {
1057  ereport(ERROR,
1058  (errcode(ERRCODE_OBJECT_IN_USE),
1059  errmsg("replication identifier %d is already active for PID %d",
1060  curstate->roident, curstate->acquired_by)));
1061  }
1062 
1063  /* ok, found slot */
1064  session_replication_state = curstate;
1065  }
1066 
1067 
1068  if (session_replication_state == NULL && free_slot == -1)
1069  ereport(ERROR,
1070  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
1071  errmsg("could not find free replication state slot for replication origin with OID %u",
1072  node),
1073  errhint("Increase max_replication_slots and try again.")));
1074  else if (session_replication_state == NULL)
1075  {
1076  /* initialize new slot */
1081  }
1082 
1083 
1085 
1087 
1088  LWLockRelease(ReplicationOriginLock);
1089 
1090  /* probably this one is pointless */
1092 }
static ReplicationState * session_replication_state
Definition: origin.c:174
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
int MyProcPid
Definition: globals.c:39
int errhint(const char *fmt,...)
Definition: elog.c:987
XLogRecPtr local_lsn
Definition: origin.c:120
static void ReplicationOriginExitCleanup(int code, Datum arg)
Definition: origin.c:981
int ConditionVariableBroadcast(ConditionVariable *cv)
int errcode(int sqlerrcode)
Definition: elog.c:575
RepOriginId roident
Definition: origin.c:108
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1722
#define ERROR
Definition: elog.h:43
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:348
#define ereport(elevel, rest)
Definition: elog.h:122
int max_replication_slots
Definition: slot.c:99
XLogRecPtr remote_lsn
Definition: origin.c:113
#define Assert(condition)
Definition: c.h:670
ConditionVariable origin_cv
Definition: origin.c:130
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1118
#define InvalidRepOriginId
Definition: origin.h:34
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
static ReplicationState * replication_states
Definition: origin.c:166

◆ StartupReplicationOrigin()

void StartupReplicationOrigin ( void  )

Definition at line 641 of file origin.c.

References Assert, CloseTransientFile(), COMP_CRC32C, DEBUG2, elog, ereport, errcode(), 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().

642 {
643  const char *path = "pg_logical/replorigin_checkpoint";
644  int fd;
645  int readBytes;
647  int last_state = 0;
648  pg_crc32c file_crc;
649  pg_crc32c crc;
650 
651  /* don't want to overwrite already existing state */
652 #ifdef USE_ASSERT_CHECKING
653  static bool already_started = false;
654 
655  Assert(!already_started);
656  already_started = true;
657 #endif
658 
659  if (max_replication_slots == 0)
660  return;
661 
662  INIT_CRC32C(crc);
663 
664  elog(DEBUG2, "starting up replication origin progress state");
665 
666  fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
667 
668  /*
669  * might have had max_replication_slots == 0 last run, or we just brought
670  * up a standby.
671  */
672  if (fd < 0 && errno == ENOENT)
673  return;
674  else if (fd < 0)
675  ereport(PANIC,
677  errmsg("could not open file \"%s\": %m",
678  path)));
679 
680  /* verify magic, that is written even if nothing was active */
681  readBytes = read(fd, &magic, sizeof(magic));
682  if (readBytes != sizeof(magic))
683  ereport(PANIC,
684  (errmsg("could not read file \"%s\": %m",
685  path)));
686  COMP_CRC32C(crc, &magic, sizeof(magic));
687 
688  if (magic != REPLICATION_STATE_MAGIC)
689  ereport(PANIC,
690  (errmsg("replication checkpoint has wrong magic %u instead of %u",
691  magic, REPLICATION_STATE_MAGIC)));
692 
693  /* we can skip locking here, no other access is possible */
694 
695  /* recover individual states, until there are no more to be found */
696  while (true)
697  {
698  ReplicationStateOnDisk disk_state;
699 
700  readBytes = read(fd, &disk_state, sizeof(disk_state));
701 
702  /* no further data */
703  if (readBytes == sizeof(crc))
704  {
705  /* not pretty, but simple ... */
706  file_crc = *(pg_crc32c *) &disk_state;
707  break;
708  }
709 
710  if (readBytes < 0)
711  {
712  ereport(PANIC,
714  errmsg("could not read file \"%s\": %m",
715  path)));
716  }
717 
718  if (readBytes != sizeof(disk_state))
719  {
720  ereport(PANIC,
722  errmsg("could not read file \"%s\": read %d of %zu",
723  path, readBytes, sizeof(disk_state))));
724  }
725 
726  COMP_CRC32C(crc, &disk_state, sizeof(disk_state));
727 
728  if (last_state == max_replication_slots)
729  ereport(PANIC,
730  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
731  errmsg("could not find free replication state, increase max_replication_slots")));
732 
733  /* copy data to shared memory */
734  replication_states[last_state].roident = disk_state.roident;
735  replication_states[last_state].remote_lsn = disk_state.remote_lsn;
736  last_state++;
737 
738  elog(LOG, "recovered replication state of node %u to %X/%X",
739  disk_state.roident,
740  (uint32) (disk_state.remote_lsn >> 32),
741  (uint32) disk_state.remote_lsn);
742  }
743 
744  /* now check checksum */
745  FIN_CRC32C(crc);
746  if (file_crc != crc)
747  ereport(PANIC,
748  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
749  errmsg("replication slot checkpoint has wrong checksum %u, expected %u",
750  crc, file_crc)));
751 
752  CloseTransientFile(fd);
753 }
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
XLogRecPtr remote_lsn
Definition: origin.c:144
uint32 pg_crc32c
Definition: pg_crc32c.h:38
RepOriginId roident
Definition: origin.c:143
int errcode(int sqlerrcode)
Definition: elog.c:575
#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:1025
RepOriginId roident
Definition: origin.c:108
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2392
#define DEBUG2
Definition: elog.h:24
int errcode_for_file_access(void)
Definition: elog.c:598
unsigned int uint32
Definition: c.h:296
#define ereport(elevel, rest)
Definition: elog.h:122
int CloseTransientFile(int fd)
Definition: fd.c:2562
#define REPLICATION_STATE_MAGIC
Definition: origin.c:177
int max_replication_slots
Definition: slot.c:99
XLogRecPtr remote_lsn
Definition: origin.c:113
#define Assert(condition)
Definition: c.h:670
int errmsg(const char *fmt,...)
Definition: elog.c:797
static ReplicationState * replication_states
Definition: origin.c:166
#define elog
Definition: elog.h:219
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:73
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:78
#define read(a, b, c)
Definition: win32.h:13

Variable Documentation

◆ replorigin_session_origin

◆ replorigin_session_origin_lsn

◆ replorigin_session_origin_timestamp