PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

#define DoNotReplicateId   PG_UINT16_MAX
#define XLOG_REPLORIGIN_DROP   0x10

Definition at line 32 of file origin.h.

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

#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

Function Documentation

void CheckPointReplicationOrigin ( void  )

Definition at line 524 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, unlink(), write, and XLogFlush().

Referenced by CheckPointGuts().

525 {
526  const char *tmppath = "pg_logical/replorigin_checkpoint.tmp";
527  const char *path = "pg_logical/replorigin_checkpoint";
528  int tmpfd;
529  int i;
531  pg_crc32c crc;
532 
533  if (max_replication_slots == 0)
534  return;
535 
536  INIT_CRC32C(crc);
537 
538  /* make sure no old temp file is remaining */
539  if (unlink(tmppath) < 0 && errno != ENOENT)
540  ereport(PANIC,
542  errmsg("could not remove file \"%s\": %m",
543  tmppath)));
544 
545  /*
546  * no other backend can perform this at the same time, we're protected by
547  * CheckpointLock.
548  */
549  tmpfd = OpenTransientFile((char *) tmppath,
550  O_CREAT | O_EXCL | O_WRONLY | PG_BINARY,
551  S_IRUSR | S_IWUSR);
552  if (tmpfd < 0)
553  ereport(PANIC,
555  errmsg("could not create file \"%s\": %m",
556  tmppath)));
557 
558  /* write magic */
559  if ((write(tmpfd, &magic, sizeof(magic))) != sizeof(magic))
560  {
561  CloseTransientFile(tmpfd);
562  ereport(PANIC,
564  errmsg("could not write to file \"%s\": %m",
565  tmppath)));
566  }
567  COMP_CRC32C(crc, &magic, sizeof(magic));
568 
569  /* prevent concurrent creations/drops */
570  LWLockAcquire(ReplicationOriginLock, LW_SHARED);
571 
572  /* write actual data */
573  for (i = 0; i < max_replication_slots; i++)
574  {
575  ReplicationStateOnDisk disk_state;
576  ReplicationState *curstate = &replication_states[i];
577  XLogRecPtr local_lsn;
578 
579  if (curstate->roident == InvalidRepOriginId)
580  continue;
581 
582  /* zero, to avoid uninitialized padding bytes */
583  memset(&disk_state, 0, sizeof(disk_state));
584 
585  LWLockAcquire(&curstate->lock, LW_SHARED);
586 
587  disk_state.roident = curstate->roident;
588 
589  disk_state.remote_lsn = curstate->remote_lsn;
590  local_lsn = curstate->local_lsn;
591 
592  LWLockRelease(&curstate->lock);
593 
594  /* make sure we only write out a commit that's persistent */
595  XLogFlush(local_lsn);
596 
597  if ((write(tmpfd, &disk_state, sizeof(disk_state))) !=
598  sizeof(disk_state))
599  {
600  CloseTransientFile(tmpfd);
601  ereport(PANIC,
603  errmsg("could not write to file \"%s\": %m",
604  tmppath)));
605  }
606 
607  COMP_CRC32C(crc, &disk_state, sizeof(disk_state));
608  }
609 
610  LWLockRelease(ReplicationOriginLock);
611 
612  /* write out the CRC */
613  FIN_CRC32C(crc);
614  if ((write(tmpfd, &crc, sizeof(crc))) != sizeof(crc))
615  {
616  CloseTransientFile(tmpfd);
617  ereport(PANIC,
619  errmsg("could not write to file \"%s\": %m",
620  tmppath)));
621  }
622 
623  CloseTransientFile(tmpfd);
624 
625  /* fsync, rename to permanent file, fsync file and directory */
626  durable_rename(tmppath, path, PANIC);
627 }
#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:2757
#define PG_BINARY
Definition: c.h:1039
RepOriginId roident
Definition: origin.c:108
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
LWLock lock
Definition: origin.c:135
int OpenTransientFile(FileName fileName, int fileFlags, int fileMode)
Definition: fd.c:2144
int errcode_for_file_access(void)
Definition: elog.c:598
unsigned int uint32
Definition: c.h:268
int unlink(const char *filename)
#define ereport(elevel, rest)
Definition: elog.h:122
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:593
int CloseTransientFile(int fd)
Definition: fd.c:2305
#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:1111
#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
void ReplicationOriginShmemInit ( void  )

Definition at line 474 of file origin.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

475 {
476  bool found;
477 
478  if (max_replication_slots == 0)
479  return;
480 
482  ShmemInitStruct("ReplicationOriginState",
484  &found);
486 
487  if (!found)
488  {
489  int i;
490 
492 
494 
495  for (i = 0; i < max_replication_slots; i++)
496  {
500  }
501  }
502 
504  "replication_origin");
505 }
#define MemSet(start, val, len)
Definition: c.h:858
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:667
Size ReplicationOriginShmemSize(void)
Definition: origin.c:454
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
void LWLockRegisterTranche(int tranche_id, char *tranche_name)
Definition: lwlock.c:592
int i
static ReplicationState * replication_states
Definition: origin.c:166
Size ReplicationOriginShmemSize ( void  )

Definition at line 454 of file origin.c.

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

Referenced by CreateSharedMemoryAndSemaphores(), and ReplicationOriginShmemInit().

455 {
456  Size size = 0;
457 
458  /*
459  * XXX: max_replication_slots is arguably the wrong thing to use, as here
460  * we keep the replay state of *remote* transactions. But for now it seems
461  * sufficient to reuse it, lest we introduce a separate GUC.
462  */
463  if (max_replication_slots == 0)
464  return size;
465 
466  size = add_size(size, offsetof(ReplicationStateCtl, states));
467 
468  size = add_size(size,
470  return size;
471 }
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:356
#define offsetof(type, field)
Definition: c.h:555
void replorigin_advance ( RepOriginId  node,
XLogRecPtr  remote_commit,
XLogRecPtr  local_commit,
bool  go_backward,
bool  wal_log 
)

Definition at line 814 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, NULL, 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().

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

Definition at line 211 of file origin.c.

References CStringGetTextDatum, elog, ERROR, GETSTRUCT, HeapTupleIsValid, InvalidOid, ReleaseSysCache(), REPLORIGNAME, 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  elog(ERROR, "cache lookup failed for replication origin '%s'",
229  roname);
230 
231  return roident;
232 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:156
#define ERROR
Definition: elog.h:43
FormData_pg_replication_origin * Form_pg_replication_origin
uintptr_t Datum
Definition: postgres.h:372
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define InvalidOid
Definition: postgres_ext.h:36
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define CStringGetTextDatum(s)
Definition: builtins.h:91
#define elog
Definition: elog.h:219
bool replorigin_by_oid ( RepOriginId  roident,
bool  missing_ok,
char **  roname 
)

Definition at line 415 of file origin.c.

References Assert, DoNotReplicateId, elog, ERROR, GETSTRUCT, HeapTupleIsValid, InvalidRepOriginId, NULL, ObjectIdGetDatum, OidIsValid, ReleaseSysCache(), REPLORIGIDENT, SearchSysCache1, and text_to_cstring().

Referenced by pg_show_replication_origin_status(), and pgoutput_begin_txn().

416 {
417  HeapTuple tuple;
419 
420  Assert(OidIsValid((Oid) roident));
421  Assert(roident != InvalidRepOriginId);
422  Assert(roident != DoNotReplicateId);
423 
425  ObjectIdGetDatum((Oid) roident));
426 
427  if (HeapTupleIsValid(tuple))
428  {
429  ric = (Form_pg_replication_origin) GETSTRUCT(tuple);
430  *roname = text_to_cstring(&ric->roname);
431  ReleaseSysCache(tuple);
432 
433  return true;
434  }
435  else
436  {
437  *roname = NULL;
438 
439  if (!missing_ok)
440  elog(ERROR, "cache lookup failed for replication origin with oid %u",
441  roident);
442 
443  return false;
444  }
445 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
#define DoNotReplicateId
Definition: origin.h:35
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:156
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
FormData_pg_replication_origin * Form_pg_replication_origin
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
#define InvalidRepOriginId
Definition: origin.h:34
char * text_to_cstring(const text *t)
Definition: varlena.c:182
#define elog
Definition: elog.h:219
RepOriginId replorigin_create ( char *  name)

Definition at line 240 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, NULL, ObjectIdGetDatum, PG_UINT16_MAX, RelationGetDescr, ReplicationOriginIdentIndex, ReplicationOriginRelationId, ScanKeyInit(), systable_beginscan(), systable_endscan(), systable_getnext(), and values.

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

241 {
242  Oid roident;
243  HeapTuple tuple = NULL;
244  Relation rel;
245  Datum roname_d;
246  SnapshotData SnapshotDirty;
247  SysScanDesc scan;
248  ScanKeyData key;
249 
250  roname_d = CStringGetTextDatum(roname);
251 
253 
254  /*
255  * We need the numeric replication origin to be 16bit wide, so we cannot
256  * rely on the normal oid allocation. Instead we simply scan
257  * pg_replication_origin for the first unused id. That's not particularly
258  * efficient, but this should be a fairly infrequent operation - we can
259  * easily spend a bit more code on this when it turns out it needs to be
260  * faster.
261  *
262  * We handle concurrency by taking an exclusive lock (allowing reads!)
263  * over the table for the duration of the search. Because we use a "dirty
264  * snapshot" we can read rows that other in-progress sessions have
265  * written, even though they would be invisible with normal snapshots. Due
266  * to the exclusive lock there's no danger that new rows can appear while
267  * we're checking.
268  */
269  InitDirtySnapshot(SnapshotDirty);
270 
272 
273  for (roident = InvalidOid + 1; roident < PG_UINT16_MAX; roident++)
274  {
275  bool nulls[Natts_pg_replication_origin];
277  bool collides;
278 
280 
281  ScanKeyInit(&key,
283  BTEqualStrategyNumber, F_OIDEQ,
284  ObjectIdGetDatum(roident));
285 
287  true /* indexOK */ ,
288  &SnapshotDirty,
289  1, &key);
290 
291  collides = HeapTupleIsValid(systable_getnext(scan));
292 
293  systable_endscan(scan);
294 
295  if (!collides)
296  {
297  /*
298  * Ok, found an unused roident, insert the new row and do a CCI,
299  * so our callers can look it up if they want to.
300  */
301  memset(&nulls, 0, sizeof(nulls));
302 
304  values[Anum_pg_replication_origin_roname - 1] = roname_d;
305 
306  tuple = heap_form_tuple(RelationGetDescr(rel), values, nulls);
307  CatalogTupleInsert(rel, tuple);
309  break;
310  }
311  }
312 
313  /* now release lock again, */
315 
316  if (tuple == NULL)
317  ereport(ERROR,
318  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
319  errmsg("could not find free replication origin OID")));
320 
321  heap_freetuple(tuple);
322  return roident;
323 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define Anum_pg_replication_origin_roident
#define RelationGetDescr(relation)
Definition: rel.h:428
#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:338
Oid CatalogTupleInsert(Relation heapRel, HeapTuple tup)
Definition: indexing.c:162
#define InitDirtySnapshot(snapshotdata)
Definition: tqual.h:100
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:372
void CommandCounterIncrement(void)
Definition: xact.c:922
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 NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
bool IsTransactionState(void)
Definition: xact.c:350
#define Natts_pg_replication_origin
static Datum values[MAXATTR]
Definition: bootstrap.c:163
#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
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:266
#define XLogRecGetData(decoder)
Definition: xlogreader.h:220
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:268
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:216
#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
void replorigin_drop ( RepOriginId  roident,
bool  nowait 
)

Definition at line 332 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, NULL, 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().

333 {
334  HeapTuple tuple;
335  Relation rel;
336  int i;
337 
339 
341 
342 restart:
343  tuple = NULL;
344  /* cleanup the slot state info */
345  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
346 
347  for (i = 0; i < max_replication_slots; i++)
348  {
350 
351  /* found our slot */
352  if (state->roident == roident)
353  {
354  if (state->acquired_by != 0)
355  {
356  ConditionVariable *cv;
357 
358  if (nowait)
359  ereport(ERROR,
360  (errcode(ERRCODE_OBJECT_IN_USE),
361  errmsg("could not drop replication origin with OID %d, in use by PID %d",
362  state->roident,
363  state->acquired_by)));
364  cv = &state->origin_cv;
365 
366  LWLockRelease(ReplicationOriginLock);
370  goto restart;
371  }
372 
373  /* first WAL log */
374  {
375  xl_replorigin_drop xlrec;
376 
377  xlrec.node_id = roident;
378  XLogBeginInsert();
379  XLogRegisterData((char *) (&xlrec), sizeof(xlrec));
380  XLogInsert(RM_REPLORIGIN_ID, XLOG_REPLORIGIN_DROP);
381  }
382 
383  /* then reset the in-memory entry */
384  state->roident = InvalidRepOriginId;
385  state->remote_lsn = InvalidXLogRecPtr;
386  state->local_lsn = InvalidXLogRecPtr;
387  break;
388  }
389  }
390  LWLockRelease(ReplicationOriginLock);
391 
393  if (!HeapTupleIsValid(tuple))
394  elog(ERROR, "cache lookup failed for replication origin with oid %u",
395  roident);
396 
397  CatalogTupleDelete(rel, &tuple->t_self);
398  ReleaseSysCache(tuple);
399 
401 
402  /* now release lock again */
404 }
#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
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:156
RepOriginId roident
Definition: origin.c:108
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
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
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:922
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1117
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 NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
Definition: regguts.h:298
ConditionVariable origin_cv
Definition: origin.c:130
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
bool IsTransactionState(void)
Definition: xact.c:350
#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
XLogRecPtr replorigin_get_progress ( RepOriginId  node,
bool  flush 
)

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

938 {
939  int i;
940  XLogRecPtr local_lsn = InvalidXLogRecPtr;
941  XLogRecPtr remote_lsn = InvalidXLogRecPtr;
942 
943  /* prevent slots from being concurrently dropped */
944  LWLockAcquire(ReplicationOriginLock, LW_SHARED);
945 
946  for (i = 0; i < max_replication_slots; i++)
947  {
949 
950  state = &replication_states[i];
951 
952  if (state->roident == node)
953  {
954  LWLockAcquire(&state->lock, LW_SHARED);
955 
956  remote_lsn = state->remote_lsn;
957  local_lsn = state->local_lsn;
958 
959  LWLockRelease(&state->lock);
960 
961  break;
962  }
963  }
964 
965  LWLockRelease(ReplicationOriginLock);
966 
967  if (flush && local_lsn != InvalidXLogRecPtr)
968  XLogFlush(local_lsn);
969 
970  return remote_lsn;
971 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr local_lsn
Definition: origin.c:120
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2757
RepOriginId roident
Definition: origin.c:108
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
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:1111
int i
static ReplicationState * replication_states
Definition: origin.c:166
const char* replorigin_identify ( uint8  info)

Definition at line 52 of file replorigindesc.c.

References NULL, 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
#define NULL
Definition: c.h:229
void replorigin_redo ( XLogReaderState record)

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

754 {
755  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
756 
757  switch (info)
758  {
759  case XLOG_REPLORIGIN_SET:
760  {
761  xl_replorigin_set *xlrec =
762  (xl_replorigin_set *) XLogRecGetData(record);
763 
765  xlrec->remote_lsn, record->EndRecPtr,
766  xlrec->force /* backward */ ,
767  false /* WAL log */ );
768  break;
769  }
771  {
772  xl_replorigin_drop *xlrec;
773  int i;
774 
775  xlrec = (xl_replorigin_drop *) XLogRecGetData(record);
776 
777  for (i = 0; i < max_replication_slots; i++)
778  {
780 
781  /* found our slot */
782  if (state->roident == xlrec->node_id)
783  {
784  /* reset entry */
785  state->roident = InvalidRepOriginId;
786  state->remote_lsn = InvalidXLogRecPtr;
787  state->local_lsn = InvalidXLogRecPtr;
788  break;
789  }
790  }
791  break;
792  }
793  default:
794  elog(PANIC, "replorigin_redo: unknown op code %u", info);
795  }
796 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr local_lsn
Definition: origin.c:120
unsigned char uint8
Definition: c.h:266
void replorigin_advance(RepOriginId node, XLogRecPtr remote_commit, XLogRecPtr local_commit, bool go_backward, bool wal_log)
Definition: origin.c:814
#define PANIC
Definition: elog.h:53
RepOriginId roident
Definition: origin.c:108
XLogRecPtr EndRecPtr
Definition: xlogreader.h:115
#define XLogRecGetData(decoder)
Definition: xlogreader.h:220
#define XLOG_REPLORIGIN_SET
Definition: origin.h:31
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:216
#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
void replorigin_session_advance ( XLogRecPtr  remote_commit,
XLogRecPtr  local_commit 
)

Definition at line 1127 of file origin.c.

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

Referenced by RecordTransactionCommit(), and RecordTransactionCommitPrepared().

1128 {
1131 
1133  if (session_replication_state->local_lsn < local_commit)
1134  session_replication_state->local_lsn = local_commit;
1135  if (session_replication_state->remote_lsn < remote_commit)
1136  session_replication_state->remote_lsn = remote_commit;
1138 }
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:1715
LWLock lock
Definition: origin.c:135
XLogRecPtr remote_lsn
Definition: origin.c:113
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
#define InvalidRepOriginId
Definition: origin.h:34
XLogRecPtr replorigin_session_get_progress ( bool  flush)

Definition at line 1145 of file origin.c.

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

Referenced by ApplyWorkerMain(), and pg_replication_origin_session_progress().

1146 {
1147  XLogRecPtr remote_lsn;
1148  XLogRecPtr local_lsn;
1149 
1151 
1153  remote_lsn = session_replication_state->remote_lsn;
1154  local_lsn = session_replication_state->local_lsn;
1156 
1157  if (flush && local_lsn != InvalidXLogRecPtr)
1158  XLogFlush(local_lsn);
1159 
1160  return remote_lsn;
1161 }
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:2757
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
LWLock lock
Definition: origin.c:135
XLogRecPtr remote_lsn
Definition: origin.c:113
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:676
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
void replorigin_session_reset ( void  )

Definition at line 1098 of file origin.c.

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

Referenced by pg_replication_origin_session_reset().

1099 {
1100  ConditionVariable *cv;
1101 
1103 
1105  ereport(ERROR,
1106  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1107  errmsg("no replication origin is configured")));
1108 
1109  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
1110 
1114 
1115  LWLockRelease(ReplicationOriginLock);
1116 
1118 }
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:1715
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int max_replication_slots
Definition: slot.c:99
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
ConditionVariable origin_cv
Definition: origin.c:130
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int errmsg(const char *fmt,...)
Definition: elog.c:797
void replorigin_session_setup ( RepOriginId  node)

Definition at line 1010 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, NULL, on_shmem_exit(), ReplicationState::origin_cv, ReplicationState::remote_lsn, ReplicationOriginExitCleanup(), and ReplicationState::roident.

Referenced by ApplyWorkerMain(), and pg_replication_origin_session_setup().

1011 {
1012  static bool registered_cleanup;
1013  int i;
1014  int free_slot = -1;
1015 
1016  if (!registered_cleanup)
1017  {
1019  registered_cleanup = true;
1020  }
1021 
1023 
1025  ereport(ERROR,
1026  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1027  errmsg("cannot setup replication origin when one is already setup")));
1028 
1029  /* Lock exclusively, as we may have to create a new table entry. */
1030  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
1031 
1032  /*
1033  * Search for either an existing slot for the origin, or a free one we can
1034  * use.
1035  */
1036  for (i = 0; i < max_replication_slots; i++)
1037  {
1038  ReplicationState *curstate = &replication_states[i];
1039 
1040  /* remember where to insert if necessary */
1041  if (curstate->roident == InvalidRepOriginId &&
1042  free_slot == -1)
1043  {
1044  free_slot = i;
1045  continue;
1046  }
1047 
1048  /* not our slot */
1049  if (curstate->roident != node)
1050  continue;
1051 
1052  else if (curstate->acquired_by != 0)
1053  {
1054  ereport(ERROR,
1055  (errcode(ERRCODE_OBJECT_IN_USE),
1056  errmsg("replication identifier %d is already active for PID %d",
1057  curstate->roident, curstate->acquired_by)));
1058  }
1059 
1060  /* ok, found slot */
1061  session_replication_state = curstate;
1062  }
1063 
1064 
1065  if (session_replication_state == NULL && free_slot == -1)
1066  ereport(ERROR,
1067  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
1068  errmsg("could not find free replication state slot for replication origin with OID %u",
1069  node),
1070  errhint("Increase max_replication_slots and try again.")));
1071  else if (session_replication_state == NULL)
1072  {
1073  /* initialize new slot */
1078  }
1079 
1080 
1082 
1084 
1085  LWLockRelease(ReplicationOriginLock);
1086 
1087  /* probably this one is pointless */
1089 }
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:978
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:1715
#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 NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
ConditionVariable origin_cv
Definition: origin.c:130
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
#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 StartupReplicationOrigin ( void  )

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

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