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 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 504 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().

505 {
506  const char *tmppath = "pg_logical/replorigin_checkpoint.tmp";
507  const char *path = "pg_logical/replorigin_checkpoint";
508  int tmpfd;
509  int i;
511  pg_crc32c crc;
512 
513  if (max_replication_slots == 0)
514  return;
515 
516  INIT_CRC32C(crc);
517 
518  /* make sure no old temp file is remaining */
519  if (unlink(tmppath) < 0 && errno != ENOENT)
520  ereport(PANIC,
522  errmsg("could not remove file \"%s\": %m",
523  tmppath)));
524 
525  /*
526  * no other backend can perform this at the same time, we're protected by
527  * CheckpointLock.
528  */
529  tmpfd = OpenTransientFile((char *) tmppath,
530  O_CREAT | O_EXCL | O_WRONLY | PG_BINARY,
531  S_IRUSR | S_IWUSR);
532  if (tmpfd < 0)
533  ereport(PANIC,
535  errmsg("could not create file \"%s\": %m",
536  tmppath)));
537 
538  /* write magic */
539  if ((write(tmpfd, &magic, sizeof(magic))) != sizeof(magic))
540  {
541  CloseTransientFile(tmpfd);
542  ereport(PANIC,
544  errmsg("could not write to file \"%s\": %m",
545  tmppath)));
546  }
547  COMP_CRC32C(crc, &magic, sizeof(magic));
548 
549  /* prevent concurrent creations/drops */
550  LWLockAcquire(ReplicationOriginLock, LW_SHARED);
551 
552  /* write actual data */
553  for (i = 0; i < max_replication_slots; i++)
554  {
555  ReplicationStateOnDisk disk_state;
556  ReplicationState *curstate = &replication_states[i];
557  XLogRecPtr local_lsn;
558 
559  if (curstate->roident == InvalidRepOriginId)
560  continue;
561 
562  LWLockAcquire(&curstate->lock, LW_SHARED);
563 
564  disk_state.roident = curstate->roident;
565 
566  disk_state.remote_lsn = curstate->remote_lsn;
567  local_lsn = curstate->local_lsn;
568 
569  LWLockRelease(&curstate->lock);
570 
571  /* make sure we only write out a commit that's persistent */
572  XLogFlush(local_lsn);
573 
574  if ((write(tmpfd, &disk_state, sizeof(disk_state))) !=
575  sizeof(disk_state))
576  {
577  CloseTransientFile(tmpfd);
578  ereport(PANIC,
580  errmsg("could not write to file \"%s\": %m",
581  tmppath)));
582  }
583 
584  COMP_CRC32C(crc, &disk_state, sizeof(disk_state));
585  }
586 
587  LWLockRelease(ReplicationOriginLock);
588 
589  /* write out the CRC */
590  FIN_CRC32C(crc);
591  if ((write(tmpfd, &crc, sizeof(crc))) != sizeof(crc))
592  {
593  CloseTransientFile(tmpfd);
594  ereport(PANIC,
596  errmsg("could not write to file \"%s\": %m",
597  tmppath)));
598  }
599 
600  CloseTransientFile(tmpfd);
601 
602  /* fsync, rename to permanent file, fsync file and directory */
603  durable_rename(tmppath, path, PANIC);
604 }
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
XLogRecPtr local_lsn
Definition: origin.c:120
#define write(a, b, c)
Definition: win32.h:19
XLogRecPtr remote_lsn
Definition: origin.c:139
uint32 pg_crc32c
Definition: pg_crc32c.h:38
RepOriginId roident
Definition: origin.c:138
#define PANIC
Definition: elog.h:53
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2754
#define PG_BINARY
Definition: c.h:1038
RepOriginId roident
Definition: origin.c:108
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
LWLock lock
Definition: origin.c:130
int OpenTransientFile(FileName fileName, int fileFlags, int fileMode)
Definition: fd.c:2107
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:2268
#define REPLICATION_STATE_MAGIC
Definition: origin.c:172
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:161
#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 457 of file origin.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

458 {
459  bool found;
460 
461  if (max_replication_slots == 0)
462  return;
463 
465  ShmemInitStruct("ReplicationOriginState",
467  &found);
469 
470  if (!found)
471  {
472  int i;
473 
475 
477 
478  for (i = 0; i < max_replication_slots; i++)
481  }
482 
484  "replication_origin");
485 }
#define MemSet(start, val, len)
Definition: c.h:857
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:437
static ReplicationStateCtl * replication_states_ctl
Definition: origin.c:162
int max_replication_slots
Definition: slot.c:99
ReplicationState states[FLEXIBLE_ARRAY_MEMBER]
Definition: origin.c:146
void LWLockRegisterTranche(int tranche_id, char *tranche_name)
Definition: lwlock.c:592
int i
static ReplicationState * replication_states
Definition: origin.c:161
Size ReplicationOriginShmemSize ( void  )

Definition at line 437 of file origin.c.

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

Referenced by CreateSharedMemoryAndSemaphores(), and ReplicationOriginShmemInit().

438 {
439  Size size = 0;
440 
441  /*
442  * XXX: max_replication_slots is arguably the wrong thing to use, as here
443  * we keep the replay state of *remote* transactions. But for now it seems
444  * sufficient to reuse it, lest we introduce a separate guc.
445  */
446  if (max_replication_slots == 0)
447  return size;
448 
449  size = add_size(size, offsetof(ReplicationStateCtl, states));
450 
451  size = add_size(size,
453  return size;
454 }
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 791 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().

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

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

207 {
209  Oid roident = InvalidOid;
210  HeapTuple tuple;
211  Datum roname_d;
212 
213  roname_d = CStringGetTextDatum(roname);
214 
215  tuple = SearchSysCache1(REPLORIGNAME, roname_d);
216  if (HeapTupleIsValid(tuple))
217  {
218  ident = (Form_pg_replication_origin) GETSTRUCT(tuple);
219  roident = ident->roident;
220  ReleaseSysCache(tuple);
221  }
222  else if (!missing_ok)
223  elog(ERROR, "cache lookup failed for replication origin '%s'",
224  roname);
225 
226  return roident;
227 }
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
unsigned int Oid
Definition: postgres_ext.h:31
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:152
#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:1116
#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 398 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().

399 {
400  HeapTuple tuple;
402 
403  Assert(OidIsValid((Oid) roident));
404  Assert(roident != InvalidRepOriginId);
405  Assert(roident != DoNotReplicateId);
406 
408  ObjectIdGetDatum((Oid) roident));
409 
410  if (HeapTupleIsValid(tuple))
411  {
412  ric = (Form_pg_replication_origin) GETSTRUCT(tuple);
413  *roname = text_to_cstring(&ric->roname);
414  ReleaseSysCache(tuple);
415 
416  return true;
417  }
418  else
419  {
420  *roname = NULL;
421 
422  if (!missing_ok)
423  elog(ERROR, "cache lookup failed for replication origin with oid %u",
424  roident);
425 
426  return false;
427  }
428 }
#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:152
#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:1116
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#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 235 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().

236 {
237  Oid roident;
238  HeapTuple tuple = NULL;
239  Relation rel;
240  Datum roname_d;
241  SnapshotData SnapshotDirty;
242  SysScanDesc scan;
243  ScanKeyData key;
244 
245  roname_d = CStringGetTextDatum(roname);
246 
248 
249  /*
250  * We need the numeric replication origin to be 16bit wide, so we cannot
251  * rely on the normal oid allocation. Instead we simply scan
252  * pg_replication_origin for the first unused id. That's not particularly
253  * efficient, but this should be a fairly infrequent operation - we can
254  * easily spend a bit more code on this when it turns out it needs to be
255  * faster.
256  *
257  * We handle concurrency by taking an exclusive lock (allowing reads!)
258  * over the table for the duration of the search. Because we use a "dirty
259  * snapshot" we can read rows that other in-progress sessions have
260  * written, even though they would be invisible with normal snapshots. Due
261  * to the exclusive lock there's no danger that new rows can appear while
262  * we're checking.
263  */
264  InitDirtySnapshot(SnapshotDirty);
265 
267 
268  for (roident = InvalidOid + 1; roident < PG_UINT16_MAX; roident++)
269  {
270  bool nulls[Natts_pg_replication_origin];
272  bool collides;
273 
275 
276  ScanKeyInit(&key,
278  BTEqualStrategyNumber, F_OIDEQ,
279  ObjectIdGetDatum(roident));
280 
282  true /* indexOK */ ,
283  &SnapshotDirty,
284  1, &key);
285 
286  collides = HeapTupleIsValid(systable_getnext(scan));
287 
288  systable_endscan(scan);
289 
290  if (!collides)
291  {
292  /*
293  * Ok, found an unused roident, insert the new row and do a CCI,
294  * so our callers can look it up if they want to.
295  */
296  memset(&nulls, 0, sizeof(nulls));
297 
299  values[Anum_pg_replication_origin_roname - 1] = roname_d;
300 
301  tuple = heap_form_tuple(RelationGetDescr(rel), values, nulls);
302  CatalogTupleInsert(rel, tuple);
304  break;
305  }
306  }
307 
308  /* now release lock again, */
310 
311  if (tuple == NULL)
312  ereport(ERROR,
313  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
314  errmsg("could not find free replication origin OID")));
315 
316  heap_freetuple(tuple);
317  return roident;
318 }
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:499
#define Anum_pg_replication_origin_roident
#define RelationGetDescr(relation)
Definition: rel.h:429
#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:692
#define heap_close(r, l)
Definition: heapam.h:97
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
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:1287
#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:675
bool IsTransactionState(void)
Definition: xact.c:350
#define Natts_pg_replication_origin
static Datum values[MAXATTR]
Definition: bootstrap.c:162
#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:97
#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:218
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:110
#define XLOG_REPLORIGIN_SET
Definition: origin.h:31
unsigned int uint32
Definition: c.h:268
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:214
#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)

Definition at line 327 of file origin.c.

References ReplicationState::acquired_by, Assert, CatalogTupleDelete(), CommandCounterIncrement(), 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, ReleaseSysCache(), ReplicationState::remote_lsn, ReplicationOriginRelationId, REPLORIGIDENT, ReplicationState::roident, SearchSysCache1, HeapTupleData::t_self, XLOG_REPLORIGIN_DROP, XLogBeginInsert(), XLogInsert(), and XLogRegisterData().

Referenced by DropSubscription(), and pg_replication_origin_drop().

328 {
329  HeapTuple tuple = NULL;
330  Relation rel;
331  int i;
332 
334 
336 
337  /* cleanup the slot state info */
338  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
339 
340  for (i = 0; i < max_replication_slots; i++)
341  {
343 
344  /* found our slot */
345  if (state->roident == roident)
346  {
347  if (state->acquired_by != 0)
348  {
349  ereport(ERROR,
350  (errcode(ERRCODE_OBJECT_IN_USE),
351  errmsg("could not drop replication origin with OID %d, in use by PID %d",
352  state->roident,
353  state->acquired_by)));
354  }
355 
356  /* first WAL log */
357  {
358  xl_replorigin_drop xlrec;
359 
360  xlrec.node_id = roident;
361  XLogBeginInsert();
362  XLogRegisterData((char *) (&xlrec), sizeof(xlrec));
363  XLogInsert(RM_REPLORIGIN_ID, XLOG_REPLORIGIN_DROP);
364  }
365 
366  /* then reset the in-memory entry */
367  state->roident = InvalidRepOriginId;
368  state->remote_lsn = InvalidXLogRecPtr;
369  state->local_lsn = InvalidXLogRecPtr;
370  break;
371  }
372  }
373  LWLockRelease(ReplicationOriginLock);
374 
376  if (!HeapTupleIsValid(tuple))
377  elog(ERROR, "cache lookup failed for replication origin with oid %u",
378  roident);
379 
380  CatalogTupleDelete(rel, &tuple->t_self);
381  ReleaseSysCache(tuple);
382 
384 
385  /* now release lock again, */
387 }
#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:152
RepOriginId roident
Definition: origin.c:108
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#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:1116
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
int max_replication_slots
Definition: slot.c:99
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:675
Definition: regguts.h:298
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:161
#define elog
Definition: elog.h:219
void XLogBeginInsert(void)
Definition: xloginsert.c:120
XLogRecPtr replorigin_get_progress ( RepOriginId  node,
bool  flush 
)

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

915 {
916  int i;
917  XLogRecPtr local_lsn = InvalidXLogRecPtr;
918  XLogRecPtr remote_lsn = InvalidXLogRecPtr;
919 
920  /* prevent slots from being concurrently dropped */
921  LWLockAcquire(ReplicationOriginLock, LW_SHARED);
922 
923  for (i = 0; i < max_replication_slots; i++)
924  {
926 
927  state = &replication_states[i];
928 
929  if (state->roident == node)
930  {
931  LWLockAcquire(&state->lock, LW_SHARED);
932 
933  remote_lsn = state->remote_lsn;
934  local_lsn = state->local_lsn;
935 
936  LWLockRelease(&state->lock);
937 
938  break;
939  }
940  }
941 
942  LWLockRelease(ReplicationOriginLock);
943 
944  if (flush && local_lsn != InvalidXLogRecPtr)
945  XLogFlush(local_lsn);
946 
947  return remote_lsn;
948 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr local_lsn
Definition: origin.c:120
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2754
RepOriginId roident
Definition: origin.c:108
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
LWLock lock
Definition: origin.c:130
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:161
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 730 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.

731 {
732  uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
733 
734  switch (info)
735  {
736  case XLOG_REPLORIGIN_SET:
737  {
738  xl_replorigin_set *xlrec =
739  (xl_replorigin_set *) XLogRecGetData(record);
740 
742  xlrec->remote_lsn, record->EndRecPtr,
743  xlrec->force /* backward */ ,
744  false /* WAL log */ );
745  break;
746  }
748  {
749  xl_replorigin_drop *xlrec;
750  int i;
751 
752  xlrec = (xl_replorigin_drop *) XLogRecGetData(record);
753 
754  for (i = 0; i < max_replication_slots; i++)
755  {
757 
758  /* found our slot */
759  if (state->roident == xlrec->node_id)
760  {
761  /* reset entry */
762  state->roident = InvalidRepOriginId;
763  state->remote_lsn = InvalidXLogRecPtr;
764  state->local_lsn = InvalidXLogRecPtr;
765  break;
766  }
767  }
768  break;
769  }
770  default:
771  elog(PANIC, "replorigin_redo: unknown op code %u", info);
772  }
773 }
#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:791
#define PANIC
Definition: elog.h:53
RepOriginId roident
Definition: origin.c:108
XLogRecPtr EndRecPtr
Definition: xlogreader.h:115
#define XLogRecGetData(decoder)
Definition: xlogreader.h:218
#define XLOG_REPLORIGIN_SET
Definition: origin.h:31
#define XLogRecGetInfo(decoder)
Definition: xlogreader.h:214
#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:161
#define elog
Definition: elog.h:219
void replorigin_session_advance ( XLogRecPtr  remote_commit,
XLogRecPtr  local_commit 
)

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

1090 {
1093 
1095  if (session_replication_state->local_lsn < local_commit)
1096  session_replication_state->local_lsn = local_commit;
1097  if (session_replication_state->remote_lsn < remote_commit)
1098  session_replication_state->remote_lsn = remote_commit;
1100 }
static ReplicationState * session_replication_state
Definition: origin.c:169
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:130
XLogRecPtr remote_lsn
Definition: origin.c:113
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
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 1107 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().

1108 {
1109  XLogRecPtr remote_lsn;
1110  XLogRecPtr local_lsn;
1111 
1113 
1115  remote_lsn = session_replication_state->remote_lsn;
1116  local_lsn = session_replication_state->local_lsn;
1118 
1119  if (flush && local_lsn != InvalidXLogRecPtr)
1120  XLogFlush(local_lsn);
1121 
1122  return remote_lsn;
1123 }
static ReplicationState * session_replication_state
Definition: origin.c:169
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr local_lsn
Definition: origin.c:120
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2754
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
LWLock lock
Definition: origin.c:130
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:675
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
void replorigin_session_reset ( void  )

Definition at line 1065 of file origin.c.

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

Referenced by pg_replication_origin_session_reset().

1066 {
1068 
1070  ereport(ERROR,
1071  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1072  errmsg("no replication origin is configured")));
1073 
1074  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
1075 
1078 
1079  LWLockRelease(ReplicationOriginLock);
1080 }
static ReplicationState * session_replication_state
Definition: origin.c:169
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:675
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 980 of file origin.c.

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

Referenced by ApplyWorkerMain(), and pg_replication_origin_session_setup().

981 {
982  static bool registered_cleanup;
983  int i;
984  int free_slot = -1;
985 
986  if (!registered_cleanup)
987  {
989  registered_cleanup = true;
990  }
991 
993 
995  ereport(ERROR,
996  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
997  errmsg("cannot setup replication origin when one is already setup")));
998 
999  /* Lock exclusively, as we may have to create a new table entry. */
1000  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
1001 
1002  /*
1003  * Search for either an existing slot for the origin, or a free one we can
1004  * use.
1005  */
1006  for (i = 0; i < max_replication_slots; i++)
1007  {
1008  ReplicationState *curstate = &replication_states[i];
1009 
1010  /* remember where to insert if necessary */
1011  if (curstate->roident == InvalidRepOriginId &&
1012  free_slot == -1)
1013  {
1014  free_slot = i;
1015  continue;
1016  }
1017 
1018  /* not our slot */
1019  if (curstate->roident != node)
1020  continue;
1021 
1022  else if (curstate->acquired_by != 0)
1023  {
1024  ereport(ERROR,
1025  (errcode(ERRCODE_OBJECT_IN_USE),
1026  errmsg("replication identifier %d is already active for PID %d",
1027  curstate->roident, curstate->acquired_by)));
1028  }
1029 
1030  /* ok, found slot */
1031  session_replication_state = curstate;
1032  }
1033 
1034 
1035  if (session_replication_state == NULL && free_slot == -1)
1036  ereport(ERROR,
1037  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
1038  errmsg("could not find free replication state slot for replication origin with OID %u",
1039  node),
1040  errhint("Increase max_replication_slots and try again.")));
1041  else if (session_replication_state == NULL)
1042  {
1043  /* initialize new slot */
1048  }
1049 
1050 
1052 
1054 
1055  LWLockRelease(ReplicationOriginLock);
1056 }
static ReplicationState * session_replication_state
Definition: origin.c:169
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
int MyProcPid
Definition: globals.c:38
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:955
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:675
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:161
void StartupReplicationOrigin ( void  )

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

616 {
617  const char *path = "pg_logical/replorigin_checkpoint";
618  int fd;
619  int readBytes;
621  int last_state = 0;
622  pg_crc32c file_crc;
623  pg_crc32c crc;
624 
625  /* don't want to overwrite already existing state */
626 #ifdef USE_ASSERT_CHECKING
627  static bool already_started = false;
628 
629  Assert(!already_started);
630  already_started = true;
631 #endif
632 
633  if (max_replication_slots == 0)
634  return;
635 
636  INIT_CRC32C(crc);
637 
638  elog(DEBUG2, "starting up replication origin progress state");
639 
640  fd = OpenTransientFile((char *) path, O_RDONLY | PG_BINARY, 0);
641 
642  /*
643  * might have had max_replication_slots == 0 last run, or we just brought
644  * up a standby.
645  */
646  if (fd < 0 && errno == ENOENT)
647  return;
648  else if (fd < 0)
649  ereport(PANIC,
651  errmsg("could not open file \"%s\": %m",
652  path)));
653 
654  /* verify magic, that is written even if nothing was active */
655  readBytes = read(fd, &magic, sizeof(magic));
656  if (readBytes != sizeof(magic))
657  ereport(PANIC,
658  (errmsg("could not read file \"%s\": %m",
659  path)));
660  COMP_CRC32C(crc, &magic, sizeof(magic));
661 
662  if (magic != REPLICATION_STATE_MAGIC)
663  ereport(PANIC,
664  (errmsg("replication checkpoint has wrong magic %u instead of %u",
665  magic, REPLICATION_STATE_MAGIC)));
666 
667  /* we can skip locking here, no other access is possible */
668 
669  /* recover individual states, until there are no more to be found */
670  while (true)
671  {
672  ReplicationStateOnDisk disk_state;
673 
674  readBytes = read(fd, &disk_state, sizeof(disk_state));
675 
676  /* no further data */
677  if (readBytes == sizeof(crc))
678  {
679  /* not pretty, but simple ... */
680  file_crc = *(pg_crc32c *) &disk_state;
681  break;
682  }
683 
684  if (readBytes < 0)
685  {
686  ereport(PANIC,
688  errmsg("could not read file \"%s\": %m",
689  path)));
690  }
691 
692  if (readBytes != sizeof(disk_state))
693  {
694  ereport(PANIC,
696  errmsg("could not read file \"%s\": read %d of %zu",
697  path, readBytes, sizeof(disk_state))));
698  }
699 
700  COMP_CRC32C(crc, &disk_state, sizeof(disk_state));
701 
702  if (last_state == max_replication_slots)
703  ereport(PANIC,
704  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
705  errmsg("could not find free replication state, increase max_replication_slots")));
706 
707  /* copy data to shared memory */
708  replication_states[last_state].roident = disk_state.roident;
709  replication_states[last_state].remote_lsn = disk_state.remote_lsn;
710  last_state++;
711 
712  elog(LOG, "recovered replication state of node %u to %X/%X",
713  disk_state.roident,
714  (uint32) (disk_state.remote_lsn >> 32),
715  (uint32) disk_state.remote_lsn);
716  }
717 
718  /* now check checksum */
719  FIN_CRC32C(crc);
720  if (file_crc != crc)
721  ereport(PANIC,
722  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
723  errmsg("replication slot checkpoint has wrong checksum %u, expected %u",
724  crc, file_crc)));
725 
726  CloseTransientFile(fd);
727 }
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
XLogRecPtr remote_lsn
Definition: origin.c:139
uint32 pg_crc32c
Definition: pg_crc32c.h:38
RepOriginId roident
Definition: origin.c:138
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:1038
RepOriginId roident
Definition: origin.c:108
#define DEBUG2
Definition: elog.h:24
int OpenTransientFile(FileName fileName, int fileFlags, int fileMode)
Definition: fd.c:2107
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:2268
#define REPLICATION_STATE_MAGIC
Definition: origin.c:172
int max_replication_slots
Definition: slot.c:99
XLogRecPtr remote_lsn
Definition: origin.c:113
#define Assert(condition)
Definition: c.h:675
int errmsg(const char *fmt,...)
Definition: elog.c:797
static ReplicationState * replication_states
Definition: origin.c:161
#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:18

Variable Documentation