PostgreSQL Source Code  git master
origin.c File Reference
#include "postgres.h"
#include <unistd.h>
#include <sys/stat.h>
#include "funcapi.h"
#include "miscadmin.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/htup_details.h"
#include "access/xact.h"
#include "catalog/indexing.h"
#include "nodes/execnodes.h"
#include "replication/origin.h"
#include "replication/logical.h"
#include "pgstat.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/condition_variable.h"
#include "storage/copydir.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/pg_lsn.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/tqual.h"
Include dependency graph for origin.c:

Go to the source code of this file.

Data Structures

struct  ReplicationState
 
struct  ReplicationStateOnDisk
 
struct  ReplicationStateCtl
 

Macros

#define REPLICATION_STATE_MAGIC   ((uint32) 0x1257DADE)
 
#define REPLICATION_ORIGIN_PROGRESS_COLS   4
 

Typedefs

typedef struct ReplicationState ReplicationState
 
typedef struct ReplicationStateOnDisk ReplicationStateOnDisk
 
typedef struct ReplicationStateCtl ReplicationStateCtl
 

Functions

static void replorigin_check_prerequisites (bool check_slots, bool recoveryOK)
 
RepOriginId replorigin_by_name (char *roname, bool missing_ok)
 
RepOriginId replorigin_create (char *roname)
 
void replorigin_drop (RepOriginId roident, bool nowait)
 
bool replorigin_by_oid (RepOriginId roident, bool missing_ok, char **roname)
 
Size ReplicationOriginShmemSize (void)
 
void ReplicationOriginShmemInit (void)
 
void CheckPointReplicationOrigin (void)
 
void StartupReplicationOrigin (void)
 
void replorigin_redo (XLogReaderState *record)
 
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)
 
static void ReplicationOriginExitCleanup (int code, Datum arg)
 
void replorigin_session_setup (RepOriginId node)
 
void replorigin_session_reset (void)
 
void replorigin_session_advance (XLogRecPtr remote_commit, XLogRecPtr local_commit)
 
XLogRecPtr replorigin_session_get_progress (bool flush)
 
Datum pg_replication_origin_create (PG_FUNCTION_ARGS)
 
Datum pg_replication_origin_drop (PG_FUNCTION_ARGS)
 
Datum pg_replication_origin_oid (PG_FUNCTION_ARGS)
 
Datum pg_replication_origin_session_setup (PG_FUNCTION_ARGS)
 
Datum pg_replication_origin_session_reset (PG_FUNCTION_ARGS)
 
Datum pg_replication_origin_session_is_setup (PG_FUNCTION_ARGS)
 
Datum pg_replication_origin_session_progress (PG_FUNCTION_ARGS)
 
Datum pg_replication_origin_xact_setup (PG_FUNCTION_ARGS)
 
Datum pg_replication_origin_xact_reset (PG_FUNCTION_ARGS)
 
Datum pg_replication_origin_advance (PG_FUNCTION_ARGS)
 
Datum pg_replication_origin_progress (PG_FUNCTION_ARGS)
 
Datum pg_show_replication_origin_status (PG_FUNCTION_ARGS)
 

Variables

RepOriginId replorigin_session_origin = InvalidRepOriginId
 
XLogRecPtr replorigin_session_origin_lsn = InvalidXLogRecPtr
 
TimestampTz replorigin_session_origin_timestamp = 0
 
static ReplicationStatereplication_states
 
static ReplicationStateCtlreplication_states_ctl
 
static ReplicationStatesession_replication_state = NULL
 

Macro Definition Documentation

◆ REPLICATION_ORIGIN_PROGRESS_COLS

#define REPLICATION_ORIGIN_PROGRESS_COLS   4

◆ REPLICATION_STATE_MAGIC

#define REPLICATION_STATE_MAGIC   ((uint32) 0x1257DADE)

Definition at line 177 of file origin.c.

Referenced by CheckPointReplicationOrigin(), and StartupReplicationOrigin().

Typedef Documentation

◆ ReplicationState

◆ ReplicationStateCtl

◆ ReplicationStateOnDisk

Function Documentation

◆ CheckPointReplicationOrigin()

void CheckPointReplicationOrigin ( void  )

Definition at line 528 of file origin.c.

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

Referenced by CheckPointGuts().

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

◆ pg_replication_origin_advance()

Datum pg_replication_origin_advance ( PG_FUNCTION_ARGS  )

Definition at line 1349 of file origin.c.

References InvalidXLogRecPtr, LockRelationOid(), name, PG_GETARG_LSN, PG_GETARG_TEXT_PP, PG_RETURN_VOID, ReplicationOriginRelationId, replorigin_advance(), replorigin_by_name(), replorigin_check_prerequisites(), RowExclusiveLock, text_to_cstring(), and UnlockRelationOid().

1350 {
1351  text *name = PG_GETARG_TEXT_PP(0);
1352  XLogRecPtr remote_commit = PG_GETARG_LSN(1);
1353  RepOriginId node;
1354 
1355  replorigin_check_prerequisites(true, false);
1356 
1357  /* lock to prevent the replication origin from vanishing */
1359 
1360  node = replorigin_by_name(text_to_cstring(name), false);
1361 
1362  /*
1363  * Can't sensibly pass a local commit to be flushed at checkpoint - this
1364  * xact hasn't committed yet. This is why this function should be used to
1365  * set up the initial replication state, but not for replay.
1366  */
1367  replorigin_advance(node, remote_commit, InvalidXLogRecPtr,
1368  true /* go backward */ , true /* WAL log */ );
1369 
1371 
1372  PG_RETURN_VOID();
1373 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:180
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:182
uint16 RepOriginId
Definition: xlogdefs.h:51
void replorigin_advance(RepOriginId node, XLogRecPtr remote_commit, XLogRecPtr local_commit, bool go_backward, bool wal_log)
Definition: origin.c:817
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:211
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define RowExclusiveLock
Definition: lockdefs.h:38
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
#define PG_RETURN_VOID()
Definition: fmgr.h:309
uint64 XLogRecPtr
Definition: xlogdefs.h:21
const char * name
Definition: encode.c:521
char * text_to_cstring(const text *t)
Definition: varlena.c:182
#define ReplicationOriginRelationId
Definition: c.h:487
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:105

◆ pg_replication_origin_create()

Datum pg_replication_origin_create ( PG_FUNCTION_ARGS  )

Definition at line 1180 of file origin.c.

References DatumGetPointer, name, pfree(), PG_GETARG_DATUM, PG_RETURN_OID, replorigin_check_prerequisites(), replorigin_create(), ReplicationState::roident, and text_to_cstring().

1181 {
1182  char *name;
1183  RepOriginId roident;
1184 
1185  replorigin_check_prerequisites(false, false);
1186 
1188  roident = replorigin_create(name);
1189 
1190  pfree(name);
1191 
1192  PG_RETURN_OID(roident);
1193 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:180
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
uint16 RepOriginId
Definition: xlogdefs.h:51
void pfree(void *pointer)
Definition: mcxt.c:949
RepOriginId replorigin_create(char *roname)
Definition: origin.c:242
const char * name
Definition: encode.c:521
#define DatumGetPointer(X)
Definition: postgres.h:555
char * text_to_cstring(const text *t)
Definition: varlena.c:182
Definition: c.h:487
#define PG_RETURN_OID(x)
Definition: fmgr.h:320

◆ pg_replication_origin_drop()

Datum pg_replication_origin_drop ( PG_FUNCTION_ARGS  )

Definition at line 1199 of file origin.c.

References Assert, DatumGetPointer, name, OidIsValid, pfree(), PG_GETARG_DATUM, PG_RETURN_VOID, replorigin_by_name(), replorigin_check_prerequisites(), replorigin_drop(), ReplicationState::roident, and text_to_cstring().

1200 {
1201  char *name;
1202  RepOriginId roident;
1203 
1204  replorigin_check_prerequisites(false, false);
1205 
1207 
1208  roident = replorigin_by_name(name, false);
1209  Assert(OidIsValid(roident));
1210 
1211  replorigin_drop(roident, true);
1212 
1213  pfree(name);
1214 
1215  PG_RETURN_VOID();
1216 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:180
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
void replorigin_drop(RepOriginId roident, bool nowait)
Definition: origin.c:334
uint16 RepOriginId
Definition: xlogdefs.h:51
#define OidIsValid(objectId)
Definition: c.h:576
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:211
void pfree(void *pointer)
Definition: mcxt.c:949
#define PG_RETURN_VOID()
Definition: fmgr.h:309
#define Assert(condition)
Definition: c.h:670
const char * name
Definition: encode.c:521
#define DatumGetPointer(X)
Definition: postgres.h:555
char * text_to_cstring(const text *t)
Definition: varlena.c:182
Definition: c.h:487

◆ pg_replication_origin_oid()

Datum pg_replication_origin_oid ( PG_FUNCTION_ARGS  )

Definition at line 1222 of file origin.c.

References DatumGetPointer, name, OidIsValid, pfree(), PG_GETARG_DATUM, PG_RETURN_NULL, PG_RETURN_OID, replorigin_by_name(), replorigin_check_prerequisites(), ReplicationState::roident, and text_to_cstring().

1223 {
1224  char *name;
1225  RepOriginId roident;
1226 
1227  replorigin_check_prerequisites(false, false);
1228 
1230  roident = replorigin_by_name(name, true);
1231 
1232  pfree(name);
1233 
1234  if (OidIsValid(roident))
1235  PG_RETURN_OID(roident);
1236  PG_RETURN_NULL();
1237 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:180
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
uint16 RepOriginId
Definition: xlogdefs.h:51
#define OidIsValid(objectId)
Definition: c.h:576
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:211
void pfree(void *pointer)
Definition: mcxt.c:949
const char * name
Definition: encode.c:521
#define DatumGetPointer(X)
Definition: postgres.h:555
char * text_to_cstring(const text *t)
Definition: varlena.c:182
Definition: c.h:487
#define PG_RETURN_OID(x)
Definition: fmgr.h:320
#define PG_RETURN_NULL()
Definition: fmgr.h:305

◆ pg_replication_origin_progress()

Datum pg_replication_origin_progress ( PG_FUNCTION_ARGS  )

Definition at line 1384 of file origin.c.

References Assert, DatumGetPointer, InvalidXLogRecPtr, name, OidIsValid, PG_GETARG_BOOL, PG_GETARG_DATUM, PG_RETURN_LSN, PG_RETURN_NULL, ReplicationState::remote_lsn, replorigin_by_name(), replorigin_check_prerequisites(), replorigin_get_progress(), ReplicationState::roident, and text_to_cstring().

1385 {
1386  char *name;
1387  bool flush;
1388  RepOriginId roident;
1389  XLogRecPtr remote_lsn = InvalidXLogRecPtr;
1390 
1391  replorigin_check_prerequisites(true, true);
1392 
1394  flush = PG_GETARG_BOOL(1);
1395 
1396  roident = replorigin_by_name(name, false);
1397  Assert(OidIsValid(roident));
1398 
1399  remote_lsn = replorigin_get_progress(roident, flush);
1400 
1401  if (remote_lsn == InvalidXLogRecPtr)
1402  PG_RETURN_NULL();
1403 
1404  PG_RETURN_LSN(remote_lsn);
1405 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:180
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
uint16 RepOriginId
Definition: xlogdefs.h:51
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define OidIsValid(objectId)
Definition: c.h:576
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:211
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:670
const char * name
Definition: encode.c:521
#define DatumGetPointer(X)
Definition: postgres.h:555
char * text_to_cstring(const text *t)
Definition: varlena.c:182
Definition: c.h:487
XLogRecPtr replorigin_get_progress(RepOriginId node, bool flush)
Definition: origin.c:940
#define PG_RETURN_NULL()
Definition: fmgr.h:305

◆ pg_replication_origin_session_is_setup()

Datum pg_replication_origin_session_is_setup ( PG_FUNCTION_ARGS  )

Definition at line 1282 of file origin.c.

References InvalidRepOriginId, PG_RETURN_BOOL, replorigin_check_prerequisites(), and replorigin_session_origin.

1283 {
1284  replorigin_check_prerequisites(false, false);
1285 
1287 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:180
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:319
RepOriginId replorigin_session_origin
Definition: origin.c:155
#define InvalidRepOriginId
Definition: origin.h:34

◆ pg_replication_origin_session_progress()

Datum pg_replication_origin_session_progress ( PG_FUNCTION_ARGS  )

Definition at line 1298 of file origin.c.

References ereport, errcode(), errmsg(), ERROR, InvalidXLogRecPtr, PG_GETARG_BOOL, PG_RETURN_LSN, PG_RETURN_NULL, ReplicationState::remote_lsn, replorigin_check_prerequisites(), and replorigin_session_get_progress().

1299 {
1300  XLogRecPtr remote_lsn = InvalidXLogRecPtr;
1301  bool flush = PG_GETARG_BOOL(0);
1302 
1303  replorigin_check_prerequisites(true, false);
1304 
1305  if (session_replication_state == NULL)
1306  ereport(ERROR,
1307  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1308  errmsg("no replication origin is configured")));
1309 
1310  remote_lsn = replorigin_session_get_progress(flush);
1311 
1312  if (remote_lsn == InvalidXLogRecPtr)
1313  PG_RETURN_NULL();
1314 
1315  PG_RETURN_LSN(remote_lsn);
1316 }
static ReplicationState * session_replication_state
Definition: origin.c:174
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:180
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr replorigin_session_get_progress(bool flush)
Definition: origin.c:1148
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_RETURN_NULL()
Definition: fmgr.h:305

◆ pg_replication_origin_session_reset()

Datum pg_replication_origin_session_reset ( PG_FUNCTION_ARGS  )

Definition at line 1265 of file origin.c.

References InvalidRepOriginId, InvalidXLogRecPtr, PG_RETURN_VOID, replorigin_check_prerequisites(), replorigin_session_origin, replorigin_session_origin_lsn, replorigin_session_origin_timestamp, and replorigin_session_reset().

1266 {
1267  replorigin_check_prerequisites(true, false);
1268 
1270 
1274 
1275  PG_RETURN_VOID();
1276 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:180
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void replorigin_session_reset(void)
Definition: origin.c:1101
XLogRecPtr replorigin_session_origin_lsn
Definition: origin.c:156
TimestampTz replorigin_session_origin_timestamp
Definition: origin.c:157
#define PG_RETURN_VOID()
Definition: fmgr.h:309
RepOriginId replorigin_session_origin
Definition: origin.c:155
#define InvalidRepOriginId
Definition: origin.h:34

◆ pg_replication_origin_session_setup()

Datum pg_replication_origin_session_setup ( PG_FUNCTION_ARGS  )

Definition at line 1243 of file origin.c.

References DatumGetPointer, name, pfree(), PG_GETARG_DATUM, PG_RETURN_VOID, replorigin_by_name(), replorigin_check_prerequisites(), replorigin_session_origin, replorigin_session_setup(), and text_to_cstring().

1244 {
1245  char *name;
1246  RepOriginId origin;
1247 
1248  replorigin_check_prerequisites(true, false);
1249 
1251  origin = replorigin_by_name(name, false);
1252  replorigin_session_setup(origin);
1253 
1254  replorigin_session_origin = origin;
1255 
1256  pfree(name);
1257 
1258  PG_RETURN_VOID();
1259 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:180
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:233
uint16 RepOriginId
Definition: xlogdefs.h:51
void replorigin_session_setup(RepOriginId node)
Definition: origin.c:1013
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:211
void pfree(void *pointer)
Definition: mcxt.c:949
#define PG_RETURN_VOID()
Definition: fmgr.h:309
RepOriginId replorigin_session_origin
Definition: origin.c:155
const char * name
Definition: encode.c:521
#define DatumGetPointer(X)
Definition: postgres.h:555
char * text_to_cstring(const text *t)
Definition: varlena.c:182
Definition: c.h:487

◆ pg_replication_origin_xact_reset()

Datum pg_replication_origin_xact_reset ( PG_FUNCTION_ARGS  )

Definition at line 1337 of file origin.c.

References InvalidXLogRecPtr, PG_RETURN_VOID, replorigin_check_prerequisites(), replorigin_session_origin_lsn, and replorigin_session_origin_timestamp.

1338 {
1339  replorigin_check_prerequisites(true, false);
1340 
1343 
1344  PG_RETURN_VOID();
1345 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:180
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr replorigin_session_origin_lsn
Definition: origin.c:156
TimestampTz replorigin_session_origin_timestamp
Definition: origin.c:157
#define PG_RETURN_VOID()
Definition: fmgr.h:309

◆ pg_replication_origin_xact_setup()

Datum pg_replication_origin_xact_setup ( PG_FUNCTION_ARGS  )

Definition at line 1319 of file origin.c.

References ereport, errcode(), errmsg(), ERROR, PG_GETARG_LSN, PG_GETARG_TIMESTAMPTZ, PG_RETURN_VOID, replorigin_check_prerequisites(), replorigin_session_origin_lsn, and replorigin_session_origin_timestamp.

1320 {
1321  XLogRecPtr location = PG_GETARG_LSN(0);
1322 
1323  replorigin_check_prerequisites(true, false);
1324 
1325  if (session_replication_state == NULL)
1326  ereport(ERROR,
1327  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1328  errmsg("no replication origin is configured")));
1329 
1330  replorigin_session_origin_lsn = location;
1332 
1333  PG_RETURN_VOID();
1334 }
static ReplicationState * session_replication_state
Definition: origin.c:174
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:180
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
XLogRecPtr replorigin_session_origin_lsn
Definition: origin.c:156
TimestampTz replorigin_session_origin_timestamp
Definition: origin.c:157
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
#define PG_RETURN_VOID()
Definition: fmgr.h:309
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PG_GETARG_TIMESTAMPTZ(n)
Definition: timestamp.h:36

◆ pg_show_replication_origin_status()

Datum pg_show_replication_origin_status ( PG_FUNCTION_ARGS  )

Definition at line 1409 of file origin.c.

References ReturnSetInfo::allowedModes, CStringGetTextDatum, ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), i, InvalidRepOriginId, IsA, ReplicationState::local_lsn, ReplicationState::lock, LSNGetDatum, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, MemoryContextSwitchTo(), ObjectIdGetDatum, ReplicationState::remote_lsn, REPLICATION_ORIGIN_PROGRESS_COLS, replorigin_by_oid(), replorigin_check_prerequisites(), ReturnSetInfo::returnMode, ReplicationState::roident, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, tuplestore_begin_heap(), tuplestore_donestoring, tuplestore_putvalues(), TYPEFUNC_COMPOSITE, values, and work_mem.

1410 {
1411  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1412  TupleDesc tupdesc;
1413  Tuplestorestate *tupstore;
1414  MemoryContext per_query_ctx;
1415  MemoryContext oldcontext;
1416  int i;
1418 
1419  /* we we want to return 0 rows if slot is set to zero */
1420  replorigin_check_prerequisites(false, true);
1421 
1422  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1423  ereport(ERROR,
1424  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1425  errmsg("set-valued function called in context that cannot accept a set")));
1426  if (!(rsinfo->allowedModes & SFRM_Materialize))
1427  ereport(ERROR,
1428  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1429  errmsg("materialize mode required, but it is not allowed in this context")));
1430  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1431  elog(ERROR, "return type must be a row type");
1432 
1433  if (tupdesc->natts != REPLICATION_ORIGIN_PROGRESS_COLS)
1434  elog(ERROR, "wrong function definition");
1435 
1436  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1437  oldcontext = MemoryContextSwitchTo(per_query_ctx);
1438 
1439  tupstore = tuplestore_begin_heap(true, false, work_mem);
1440  rsinfo->returnMode = SFRM_Materialize;
1441  rsinfo->setResult = tupstore;
1442  rsinfo->setDesc = tupdesc;
1443 
1444  MemoryContextSwitchTo(oldcontext);
1445 
1446 
1447  /* prevent slots from being concurrently dropped */
1448  LWLockAcquire(ReplicationOriginLock, LW_SHARED);
1449 
1450  /*
1451  * Iterate through all possible replication_states, display if they are
1452  * filled. Note that we do not take any locks, so slightly corrupted/out
1453  * of date values are a possibility.
1454  */
1455  for (i = 0; i < max_replication_slots; i++)
1456  {
1460  char *roname;
1461 
1462  state = &replication_states[i];
1463 
1464  /* unused slot, nothing to display */
1465  if (state->roident == InvalidRepOriginId)
1466  continue;
1467 
1468  memset(values, 0, sizeof(values));
1469  memset(nulls, 1, sizeof(nulls));
1470 
1471  values[0] = ObjectIdGetDatum(state->roident);
1472  nulls[0] = false;
1473 
1474  /*
1475  * We're not preventing the origin to be dropped concurrently, so
1476  * silently accept that it might be gone.
1477  */
1478  if (replorigin_by_oid(state->roident, true,
1479  &roname))
1480  {
1481  values[1] = CStringGetTextDatum(roname);
1482  nulls[1] = false;
1483  }
1484 
1485  LWLockAcquire(&state->lock, LW_SHARED);
1486 
1487  values[2] = LSNGetDatum(state->remote_lsn);
1488  nulls[2] = false;
1489 
1490  values[3] = LSNGetDatum(state->local_lsn);
1491  nulls[3] = false;
1492 
1493  LWLockRelease(&state->lock);
1494 
1495  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
1496  }
1497 
1498  tuplestore_donestoring(tupstore);
1499 
1500  LWLockRelease(ReplicationOriginLock);
1501 
1502 #undef REPLICATION_ORIGIN_PROGRESS_COLS
1503 
1504  return (Datum) 0;
1505 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:180
#define IsA(nodeptr, _type_)
Definition: nodes.h:562
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
XLogRecPtr local_lsn
Definition: origin.c:120
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
bool replorigin_by_oid(RepOriginId roident, bool missing_ok, char **roname)
Definition: origin.c:417
RepOriginId roident
Definition: origin.c:108
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
LWLock lock
Definition: origin.c:135
#define ereport(elevel, rest)
Definition: elog.h:122
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
uintptr_t Datum
Definition: postgres.h:372
int work_mem
Definition: globals.c:113
int allowedModes
Definition: execnodes.h:268
SetFunctionReturnMode returnMode
Definition: execnodes.h:270
int max_replication_slots
Definition: slot.c:99
XLogRecPtr remote_lsn
Definition: origin.c:113
Definition: regguts.h:298
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:202
#define InvalidRepOriginId
Definition: origin.h:34
Tuplestorestate * setResult
Definition: execnodes.h:273
static Datum values[MAXATTR]
Definition: bootstrap.c:164
ExprContext * econtext
Definition: execnodes.h:266
TupleDesc setDesc
Definition: execnodes.h:274
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
static ReplicationState * replication_states
Definition: origin.c:166
#define CStringGetTextDatum(s)
Definition: builtins.h:91
#define elog
Definition: elog.h:219
#define REPLICATION_ORIGIN_PROGRESS_COLS

◆ ReplicationOriginExitCleanup()

static void ReplicationOriginExitCleanup ( int  code,
Datum  arg 
)
static

Definition at line 981 of file origin.c.

References ReplicationState::acquired_by, ConditionVariableBroadcast(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyProcPid, and ReplicationState::origin_cv.

Referenced by replorigin_session_setup().

982 {
983  ConditionVariable *cv = NULL;
984 
985  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
986 
987  if (session_replication_state != NULL &&
989  {
991 
994  }
995 
996  LWLockRelease(ReplicationOriginLock);
997 
998  if (cv)
1000 }
static ReplicationState * session_replication_state
Definition: origin.c:174
int MyProcPid
Definition: globals.c:39
int ConditionVariableBroadcast(ConditionVariable *cv)
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1721
ConditionVariable origin_cv
Definition: origin.c:130
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117

◆ ReplicationOriginShmemInit()

void ReplicationOriginShmemInit ( void  )

Definition at line 478 of file origin.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

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

◆ ReplicationOriginShmemSize()

Size ReplicationOriginShmemSize ( void  )

Definition at line 458 of file origin.c.

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

Referenced by CreateSharedMemoryAndSemaphores(), and ReplicationOriginShmemInit().

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

◆ replorigin_advance()

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

Definition at line 817 of file origin.c.

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

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

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

◆ replorigin_by_name()

RepOriginId replorigin_by_name ( char *  roname,
bool  missing_ok 
)

Definition at line 211 of file origin.c.

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

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

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

◆ replorigin_by_oid()

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

Definition at line 417 of file origin.c.

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

Referenced by pg_show_replication_origin_status(), and pgoutput_begin_txn().

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

◆ replorigin_check_prerequisites()

static void replorigin_check_prerequisites ( bool  check_slots,
bool  recoveryOK 
)
static

Definition at line 180 of file origin.c.

References ereport, errcode(), errmsg(), ERROR, max_replication_slots, RecoveryInProgress(), and superuser().

Referenced by pg_replication_origin_advance(), pg_replication_origin_create(), pg_replication_origin_drop(), pg_replication_origin_oid(), pg_replication_origin_progress(), pg_replication_origin_session_is_setup(), pg_replication_origin_session_progress(), pg_replication_origin_session_reset(), pg_replication_origin_session_setup(), pg_replication_origin_xact_reset(), pg_replication_origin_xact_setup(), and pg_show_replication_origin_status().

181 {
182  if (!superuser())
183  ereport(ERROR,
184  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
185  errmsg("only superusers can query or manipulate replication origins")));
186 
187  if (check_slots && max_replication_slots == 0)
188  ereport(ERROR,
189  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
190  errmsg("cannot query or manipulate replication origin when max_replication_slots = 0")));
191 
192  if (!recoveryOK && RecoveryInProgress())
193  ereport(ERROR,
194  (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
195  errmsg("cannot manipulate replication origins during recovery")));
196 
197 }
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
bool RecoveryInProgress(void)
Definition: xlog.c:7929
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
int max_replication_slots
Definition: slot.c:99
int errmsg(const char *fmt,...)
Definition: elog.c:797

◆ replorigin_create()

RepOriginId replorigin_create ( char *  roname)

Definition at line 242 of file origin.c.

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

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

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

◆ replorigin_drop()

void replorigin_drop ( RepOriginId  roident,
bool  nowait 
)

Definition at line 334 of file origin.c.

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

Referenced by DropSubscription(), and pg_replication_origin_drop().

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

◆ replorigin_get_progress()

XLogRecPtr replorigin_get_progress ( RepOriginId  node,
bool  flush 
)

Definition at line 940 of file origin.c.

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

Referenced by pg_replication_origin_progress().

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

◆ replorigin_redo()

void replorigin_redo ( XLogReaderState record)

Definition at line 756 of file origin.c.

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

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

◆ replorigin_session_advance()

void replorigin_session_advance ( XLogRecPtr  remote_commit,
XLogRecPtr  local_commit 
)

Definition at line 1130 of file origin.c.

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

Referenced by RecordTransactionCommit(), and RecordTransactionCommitPrepared().

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

◆ replorigin_session_get_progress()

XLogRecPtr replorigin_session_get_progress ( bool  flush)

Definition at line 1148 of file origin.c.

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

Referenced by ApplyWorkerMain(), and pg_replication_origin_session_progress().

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

◆ replorigin_session_reset()

void replorigin_session_reset ( void  )

Definition at line 1101 of file origin.c.

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

Referenced by pg_replication_origin_session_reset().

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

◆ replorigin_session_setup()

void replorigin_session_setup ( RepOriginId  node)

Definition at line 1013 of file origin.c.

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

Referenced by ApplyWorkerMain(), and pg_replication_origin_session_setup().

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

◆ StartupReplicationOrigin()

void StartupReplicationOrigin ( void  )

Definition at line 641 of file origin.c.

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

Referenced by StartupXLOG().

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

Variable Documentation

◆ replication_states

ReplicationState* replication_states
static

Definition at line 166 of file origin.c.

◆ replication_states_ctl

ReplicationStateCtl* replication_states_ctl
static

Definition at line 167 of file origin.c.

◆ replorigin_session_origin

◆ replorigin_session_origin_lsn

◆ replorigin_session_origin_timestamp

◆ session_replication_state

ReplicationState* session_replication_state = NULL
static

Definition at line 174 of file origin.c.