PostgreSQL Source Code  git master
origin.c File Reference
#include "postgres.h"
#include <unistd.h>
#include <sys/stat.h>
#include "access/genam.h"
#include "access/htup_details.h"
#include "access/table.h"
#include "access/xact.h"
#include "catalog/catalog.h"
#include "catalog/indexing.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/execnodes.h"
#include "pgstat.h"
#include "replication/logical.h"
#include "replication/origin.h"
#include "storage/condition_variable.h"
#include "storage/copydir.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/pg_lsn.h"
#include "utils/rel.h"
#include "utils/snapmgr.h"
#include "utils/syscache.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 180 of file origin.c.

Referenced by CheckPointReplicationOrigin(), and StartupReplicationOrigin().

Typedef Documentation

◆ ReplicationState

◆ ReplicationStateCtl

◆ ReplicationStateOnDisk

Function Documentation

◆ CheckPointReplicationOrigin()

void CheckPointReplicationOrigin ( void  )

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

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

◆ pg_replication_origin_advance()

Datum pg_replication_origin_advance ( PG_FUNCTION_ARGS  )

Definition at line 1406 of file origin.c.

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

1407 {
1408  text *name = PG_GETARG_TEXT_PP(0);
1409  XLogRecPtr remote_commit = PG_GETARG_LSN(1);
1410  RepOriginId node;
1411 
1412  replorigin_check_prerequisites(true, false);
1413 
1414  /* lock to prevent the replication origin from vanishing */
1415  LockRelationOid(ReplicationOriginRelationId, RowExclusiveLock);
1416 
1417  node = replorigin_by_name(text_to_cstring(name), false);
1418 
1419  /*
1420  * Can't sensibly pass a local commit to be flushed at checkpoint - this
1421  * xact hasn't committed yet. This is why this function should be used to
1422  * set up the initial replication state, but not for replay.
1423  */
1424  replorigin_advance(node, remote_commit, InvalidXLogRecPtr,
1425  true /* go backward */ , true /* WAL log */ );
1426 
1427  UnlockRelationOid(ReplicationOriginRelationId, RowExclusiveLock);
1428 
1429  PG_RETURN_VOID();
1430 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:183
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:199
uint16 RepOriginId
Definition: xlogdefs.h:58
void replorigin_advance(RepOriginId node, XLogRecPtr remote_commit, XLogRecPtr local_commit, bool go_backward, bool wal_log)
Definition: origin.c:856
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:209
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define RowExclusiveLock
Definition: lockdefs.h:38
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
#define PG_RETURN_VOID()
Definition: fmgr.h:349
uint64 XLogRecPtr
Definition: xlogdefs.h:21
const char * name
Definition: encode.c:561
char * text_to_cstring(const text *t)
Definition: varlena.c:221
Definition: c.h:617
void LockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:108

◆ pg_replication_origin_create()

Datum pg_replication_origin_create ( PG_FUNCTION_ARGS  )

Definition at line 1219 of file origin.c.

References DatumGetPointer, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, IsReservedName(), name, pfree(), PG_GETARG_DATUM, PG_RETURN_OID, replorigin_check_prerequisites(), replorigin_create(), ReplicationState::roident, text_to_cstring(), and WARNING.

1220 {
1221  char *name;
1222  RepOriginId roident;
1223 
1224  replorigin_check_prerequisites(false, false);
1225 
1227 
1228  /* Replication origins "pg_xxx" are reserved for internal use */
1229  if (IsReservedName(name))
1230  ereport(ERROR,
1231  (errcode(ERRCODE_RESERVED_NAME),
1232  errmsg("replication origin name \"%s\" is reserved",
1233  name),
1234  errdetail("Origin names starting with \"pg_\" are reserved.")));
1235 
1236  /*
1237  * If built with appropriate switch, whine when regression-testing
1238  * conventions for replication origin names are violated.
1239  */
1240 #ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
1241  if (strncmp(name, "regress_", 8) != 0)
1242  elog(WARNING, "replication origins created by regression test cases should have names starting with \"regress_\"");
1243 #endif
1244 
1245  roident = replorigin_create(name);
1246 
1247  pfree(name);
1248 
1249  PG_RETURN_OID(roident);
1250 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:183
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
uint16 RepOriginId
Definition: xlogdefs.h:58
int errcode(int sqlerrcode)
Definition: elog.c:691
bool IsReservedName(const char *name)
Definition: catalog.c:210
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:1035
RepOriginId replorigin_create(char *roname)
Definition: origin.c:240
#define WARNING
Definition: elog.h:40
#define ereport(elevel,...)
Definition: elog.h:155
const char * name
Definition: encode.c:561
#define DatumGetPointer(X)
Definition: postgres.h:549
char * text_to_cstring(const text *t)
Definition: varlena.c:221
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define elog(elevel,...)
Definition: elog.h:228
Definition: c.h:617
#define PG_RETURN_OID(x)
Definition: fmgr.h:360

◆ pg_replication_origin_drop()

Datum pg_replication_origin_drop ( PG_FUNCTION_ARGS  )

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

1257 {
1258  char *name;
1259  RepOriginId roident;
1260 
1261  replorigin_check_prerequisites(false, false);
1262 
1264 
1265  roident = replorigin_by_name(name, false);
1266  Assert(OidIsValid(roident));
1267 
1268  replorigin_drop(roident, true);
1269 
1270  pfree(name);
1271 
1272  PG_RETURN_VOID();
1273 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:183
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
void replorigin_drop(RepOriginId roident, bool nowait)
Definition: origin.c:332
uint16 RepOriginId
Definition: xlogdefs.h:58
#define OidIsValid(objectId)
Definition: c.h:706
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:209
void pfree(void *pointer)
Definition: mcxt.c:1057
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define Assert(condition)
Definition: c.h:800
const char * name
Definition: encode.c:561
#define DatumGetPointer(X)
Definition: postgres.h:549
char * text_to_cstring(const text *t)
Definition: varlena.c:221
Definition: c.h:617

◆ pg_replication_origin_oid()

Datum pg_replication_origin_oid ( PG_FUNCTION_ARGS  )

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

1280 {
1281  char *name;
1282  RepOriginId roident;
1283 
1284  replorigin_check_prerequisites(false, false);
1285 
1287  roident = replorigin_by_name(name, true);
1288 
1289  pfree(name);
1290 
1291  if (OidIsValid(roident))
1292  PG_RETURN_OID(roident);
1293  PG_RETURN_NULL();
1294 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:183
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
uint16 RepOriginId
Definition: xlogdefs.h:58
#define OidIsValid(objectId)
Definition: c.h:706
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:209
void pfree(void *pointer)
Definition: mcxt.c:1057
const char * name
Definition: encode.c:561
#define DatumGetPointer(X)
Definition: postgres.h:549
char * text_to_cstring(const text *t)
Definition: varlena.c:221
Definition: c.h:617
#define PG_RETURN_OID(x)
Definition: fmgr.h:360
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_replication_origin_progress()

Datum pg_replication_origin_progress ( PG_FUNCTION_ARGS  )

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

1442 {
1443  char *name;
1444  bool flush;
1445  RepOriginId roident;
1446  XLogRecPtr remote_lsn = InvalidXLogRecPtr;
1447 
1448  replorigin_check_prerequisites(true, true);
1449 
1451  flush = PG_GETARG_BOOL(1);
1452 
1453  roident = replorigin_by_name(name, false);
1454  Assert(OidIsValid(roident));
1455 
1456  remote_lsn = replorigin_get_progress(roident, flush);
1457 
1458  if (remote_lsn == InvalidXLogRecPtr)
1459  PG_RETURN_NULL();
1460 
1461  PG_RETURN_LSN(remote_lsn);
1462 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:183
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
uint16 RepOriginId
Definition: xlogdefs.h:58
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define OidIsValid(objectId)
Definition: c.h:706
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:209
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:800
const char * name
Definition: encode.c:561
#define DatumGetPointer(X)
Definition: postgres.h:549
char * text_to_cstring(const text *t)
Definition: varlena.c:221
Definition: c.h:617
XLogRecPtr replorigin_get_progress(RepOriginId node, bool flush)
Definition: origin.c:979
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_replication_origin_session_is_setup()

Datum pg_replication_origin_session_is_setup ( PG_FUNCTION_ARGS  )

Definition at line 1339 of file origin.c.

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

1340 {
1341  replorigin_check_prerequisites(false, false);
1342 
1344 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:183
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
RepOriginId replorigin_session_origin
Definition: origin.c:154
#define InvalidRepOriginId
Definition: origin.h:33

◆ pg_replication_origin_session_progress()

Datum pg_replication_origin_session_progress ( PG_FUNCTION_ARGS  )

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

1356 {
1357  XLogRecPtr remote_lsn = InvalidXLogRecPtr;
1358  bool flush = PG_GETARG_BOOL(0);
1359 
1360  replorigin_check_prerequisites(true, false);
1361 
1362  if (session_replication_state == NULL)
1363  ereport(ERROR,
1364  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1365  errmsg("no replication origin is configured")));
1366 
1367  remote_lsn = replorigin_session_get_progress(flush);
1368 
1369  if (remote_lsn == InvalidXLogRecPtr)
1370  PG_RETURN_NULL();
1371 
1372  PG_RETURN_LSN(remote_lsn);
1373 }
static ReplicationState * session_replication_state
Definition: origin.c:177
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:183
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr replorigin_session_get_progress(bool flush)
Definition: origin.c:1187
int errcode(int sqlerrcode)
Definition: elog.c:691
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define PG_RETURN_LSN(x)
Definition: pg_lsn.h:25
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:155
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define PG_RETURN_NULL()
Definition: fmgr.h:345

◆ pg_replication_origin_session_reset()

Datum pg_replication_origin_session_reset ( PG_FUNCTION_ARGS  )

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

1323 {
1324  replorigin_check_prerequisites(true, false);
1325 
1327 
1331 
1332  PG_RETURN_VOID();
1333 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:183
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void replorigin_session_reset(void)
Definition: origin.c:1140
XLogRecPtr replorigin_session_origin_lsn
Definition: origin.c:155
TimestampTz replorigin_session_origin_timestamp
Definition: origin.c:156
#define PG_RETURN_VOID()
Definition: fmgr.h:349
RepOriginId replorigin_session_origin
Definition: origin.c:154
#define InvalidRepOriginId
Definition: origin.h:33

◆ pg_replication_origin_session_setup()

Datum pg_replication_origin_session_setup ( PG_FUNCTION_ARGS  )

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

1301 {
1302  char *name;
1303  RepOriginId origin;
1304 
1305  replorigin_check_prerequisites(true, false);
1306 
1308  origin = replorigin_by_name(name, false);
1309  replorigin_session_setup(origin);
1310 
1311  replorigin_session_origin = origin;
1312 
1313  pfree(name);
1314 
1315  PG_RETURN_VOID();
1316 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:183
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
uint16 RepOriginId
Definition: xlogdefs.h:58
void replorigin_session_setup(RepOriginId node)
Definition: origin.c:1052
RepOriginId replorigin_by_name(char *roname, bool missing_ok)
Definition: origin.c:209
void pfree(void *pointer)
Definition: mcxt.c:1057
#define PG_RETURN_VOID()
Definition: fmgr.h:349
RepOriginId replorigin_session_origin
Definition: origin.c:154
const char * name
Definition: encode.c:561
#define DatumGetPointer(X)
Definition: postgres.h:549
char * text_to_cstring(const text *t)
Definition: varlena.c:221
Definition: c.h:617

◆ pg_replication_origin_xact_reset()

Datum pg_replication_origin_xact_reset ( PG_FUNCTION_ARGS  )

Definition at line 1394 of file origin.c.

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

1395 {
1396  replorigin_check_prerequisites(true, false);
1397 
1400 
1401  PG_RETURN_VOID();
1402 }
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:183
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr replorigin_session_origin_lsn
Definition: origin.c:155
TimestampTz replorigin_session_origin_timestamp
Definition: origin.c:156
#define PG_RETURN_VOID()
Definition: fmgr.h:349

◆ pg_replication_origin_xact_setup()

Datum pg_replication_origin_xact_setup ( PG_FUNCTION_ARGS  )

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

1377 {
1378  XLogRecPtr location = PG_GETARG_LSN(0);
1379 
1380  replorigin_check_prerequisites(true, false);
1381 
1382  if (session_replication_state == NULL)
1383  ereport(ERROR,
1384  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1385  errmsg("no replication origin is configured")));
1386 
1387  replorigin_session_origin_lsn = location;
1389 
1390  PG_RETURN_VOID();
1391 }
static ReplicationState * session_replication_state
Definition: origin.c:177
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
Definition: origin.c:183
int errcode(int sqlerrcode)
Definition: elog.c:691
#define ERROR
Definition: elog.h:43
XLogRecPtr replorigin_session_origin_lsn
Definition: origin.c:155
TimestampTz replorigin_session_origin_timestamp
Definition: origin.c:156
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:24
#define ereport(elevel,...)
Definition: elog.h:155
#define PG_RETURN_VOID()
Definition: fmgr.h:349
uint64 XLogRecPtr
Definition: xlogdefs.h:21
int errmsg(const char *fmt,...)
Definition: elog.c:902
#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 1466 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.

1467 {
1468  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1469  TupleDesc tupdesc;
1470  Tuplestorestate *tupstore;
1471  MemoryContext per_query_ctx;
1472  MemoryContext oldcontext;
1473  int i;
1475 
1476  /* we want to return 0 rows if slot is set to zero */
1477  replorigin_check_prerequisites(false, true);
1478 
1479  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
1480  ereport(ERROR,
1481  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1482  errmsg("set-valued function called in context that cannot accept a set")));
1483  if (!(rsinfo->allowedModes & SFRM_Materialize))
1484  ereport(ERROR,
1485  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1486  errmsg("materialize mode required, but it is not allowed in this context")));
1487  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
1488  elog(ERROR, "return type must be a row type");
1489 
1490  if (tupdesc->natts != REPLICATION_ORIGIN_PROGRESS_COLS)
1491  elog(ERROR, "wrong function definition");
1492 
1493  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
1494  oldcontext = MemoryContextSwitchTo(per_query_ctx);
1495 
1496  tupstore = tuplestore_begin_heap(true, false, work_mem);
1497  rsinfo->returnMode = SFRM_Materialize;
1498  rsinfo->setResult = tupstore;
1499  rsinfo->setDesc = tupdesc;
1500 
1501  MemoryContextSwitchTo(oldcontext);
1502 
1503 
1504  /* prevent slots from being concurrently dropped */
1505  LWLockAcquire(ReplicationOriginLock, LW_SHARED);
1506 
1507  /*
1508  * Iterate through all possible replication_states, display if they are
1509  * filled. Note that we do not take any locks, so slightly corrupted/out
1510  * of date values are a possibility.
1511  */
1512  for (i = 0; i < max_replication_slots; i++)
1513  {
1517  char *roname;
1518 
1519  state = &replication_states[i];
1520 
1521  /* unused slot, nothing to display */
1522  if (state->roident == InvalidRepOriginId)
1523  continue;
1524 
1525  memset(values, 0, sizeof(values));
1526  memset(nulls, 1, sizeof(nulls));
1527 
1528  values[0] = ObjectIdGetDatum(state->roident);
1529  nulls[0] = false;
1530 
1531  /*
1532  * We're not preventing the origin to be dropped concurrently, so
1533  * silently accept that it might be gone.
1534  */
1535  if (replorigin_by_oid(state->roident, true,
1536  &roname))
1537  {
1538  values[1] = CStringGetTextDatum(roname);
1539  nulls[1] = false;
1540  }
1541 
1542  LWLockAcquire(&state->lock, LW_SHARED);
1543 
1544  values[2] = LSNGetDatum(state->remote_lsn);
1545  nulls[2] = false;
1546 
1547  values[3] = LSNGetDatum(state->local_lsn);
1548  nulls[3] = false;
1549 
1550  LWLockRelease(&state->lock);
1551 
1552  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
1553  }
1554 
1555  tuplestore_donestoring(tupstore);
1556 
1557  LWLockRelease(ReplicationOriginLock);
1558 
1559 #undef REPLICATION_ORIGIN_PROGRESS_COLS
1560 
1561  return (Datum) 0;
1562 }
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:183
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:205
XLogRecPtr local_lsn
Definition: origin.c:117
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:691
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
bool replorigin_by_oid(RepOriginId roident, bool missing_ok, char **roname)
Definition: origin.c:432
RepOriginId roident
Definition: origin.c:105
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1811
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
LWLock lock
Definition: origin.c:132
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
uintptr_t Datum
Definition: postgres.h:367
int work_mem
Definition: globals.c:121
#define ereport(elevel,...)
Definition: elog.h:155
int allowedModes
Definition: execnodes.h:304
SetFunctionReturnMode returnMode
Definition: execnodes.h:306
int max_replication_slots
Definition: slot.c:99
XLogRecPtr remote_lsn
Definition: origin.c:110
Definition: regguts.h:298
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1207
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:232
#define InvalidRepOriginId
Definition: origin.h:33
Tuplestorestate * setResult
Definition: execnodes.h:309
static Datum values[MAXATTR]
Definition: bootstrap.c:165
ExprContext * econtext
Definition: execnodes.h:302
TupleDesc setDesc
Definition: execnodes.h:310
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define elog(elevel,...)
Definition: elog.h:228
int i
static ReplicationState * replication_states
Definition: origin.c:165
#define CStringGetTextDatum(s)
Definition: builtins.h:86
#define REPLICATION_ORIGIN_PROGRESS_COLS

◆ ReplicationOriginExitCleanup()

static void ReplicationOriginExitCleanup ( int  code,
Datum  arg 
)
static

Definition at line 1020 of file origin.c.

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

Referenced by replorigin_session_setup().

1021 {
1022  ConditionVariable *cv = NULL;
1023 
1024  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
1025 
1026  if (session_replication_state != NULL &&
1028  {
1030 
1033  }
1034 
1035  LWLockRelease(ReplicationOriginLock);
1036 
1037  if (cv)
1039 }
static ReplicationState * session_replication_state
Definition: origin.c:177
int MyProcPid
Definition: globals.c:40
void ConditionVariableBroadcast(ConditionVariable *cv)
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1811
ConditionVariable origin_cv
Definition: origin.c:127
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1207

◆ ReplicationOriginShmemInit()

void ReplicationOriginShmemInit ( void  )

Definition at line 493 of file origin.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

494 {
495  bool found;
496 
497  if (max_replication_slots == 0)
498  return;
499 
501  ShmemInitStruct("ReplicationOriginState",
503  &found);
505 
506  if (!found)
507  {
508  int i;
509 
511 
513 
514  for (i = 0; i < max_replication_slots; i++)
515  {
519  }
520  }
521 }
#define MemSet(start, val, len)
Definition: c.h:1004
void ConditionVariableInit(ConditionVariable *cv)
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:392
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:744
Size ReplicationOriginShmemSize(void)
Definition: origin.c:473
static ReplicationStateCtl * replication_states_ctl
Definition: origin.c:170
int max_replication_slots
Definition: slot.c:99
ReplicationState states[FLEXIBLE_ARRAY_MEMBER]
Definition: origin.c:150
int i
static ReplicationState * replication_states
Definition: origin.c:165

◆ ReplicationOriginShmemSize()

Size ReplicationOriginShmemSize ( void  )

Definition at line 473 of file origin.c.

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

Referenced by CreateSharedMemoryAndSemaphores(), and ReplicationOriginShmemInit().

474 {
475  Size size = 0;
476 
477  /*
478  * XXX: max_replication_slots is arguably the wrong thing to use, as here
479  * we keep the replay state of *remote* transactions. But for now it seems
480  * sufficient to reuse it, rather than introduce a separate GUC.
481  */
482  if (max_replication_slots == 0)
483  return size;
484 
485  size = add_size(size, offsetof(ReplicationStateCtl, states));
486 
487  size = add_size(size,
489  return size;
490 }
Size mul_size(Size s1, Size s2)
Definition: shmem.c:515
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
int max_replication_slots
Definition: slot.c:99
size_t Size
Definition: c.h:528
#define offsetof(type, field)
Definition: c.h:723

◆ replorigin_advance()

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

Definition at line 856 of file origin.c.

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

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

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

◆ replorigin_by_name()

RepOriginId replorigin_by_name ( char *  roname,
bool  missing_ok 
)

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

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

◆ replorigin_by_oid()

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

Definition at line 432 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(), pgoutput_begin_txn(), and pgoutput_stream_start().

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

◆ replorigin_check_prerequisites()

static void replorigin_check_prerequisites ( bool  check_slots,
bool  recoveryOK 
)
static

Definition at line 183 of file origin.c.

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

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

184 {
185  if (check_slots && max_replication_slots == 0)
186  ereport(ERROR,
187  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
188  errmsg("cannot query or manipulate replication origin when max_replication_slots = 0")));
189 
190  if (!recoveryOK && RecoveryInProgress())
191  ereport(ERROR,
192  (errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
193  errmsg("cannot manipulate replication origins during recovery")));
194 
195 }
int errcode(int sqlerrcode)
Definition: elog.c:691
bool RecoveryInProgress(void)
Definition: xlog.c:8071
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:155
int max_replication_slots
Definition: slot.c:99
int errmsg(const char *fmt,...)
Definition: elog.c:902

◆ replorigin_create()

RepOriginId replorigin_create ( char *  roname)

Definition at line 240 of file origin.c.

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

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

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

◆ replorigin_drop()

void replorigin_drop ( RepOriginId  roident,
bool  nowait 
)

Definition at line 332 of file origin.c.

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

Referenced by DropSubscription(), and pg_replication_origin_drop().

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

◆ replorigin_get_progress()

XLogRecPtr replorigin_get_progress ( RepOriginId  node,
bool  flush 
)

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

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

◆ replorigin_redo()

void replorigin_redo ( XLogReaderState record)

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

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

◆ replorigin_session_advance()

void replorigin_session_advance ( XLogRecPtr  remote_commit,
XLogRecPtr  local_commit 
)

Definition at line 1169 of file origin.c.

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

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

1170 {
1173 
1175  if (session_replication_state->local_lsn < local_commit)
1176  session_replication_state->local_lsn = local_commit;
1177  if (session_replication_state->remote_lsn < remote_commit)
1178  session_replication_state->remote_lsn = remote_commit;
1180 }
static ReplicationState * session_replication_state
Definition: origin.c:177
XLogRecPtr local_lsn
Definition: origin.c:117
RepOriginId roident
Definition: origin.c:105
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1811
LWLock lock
Definition: origin.c:132
XLogRecPtr remote_lsn
Definition: origin.c:110
#define Assert(condition)
Definition: c.h:800
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1207
#define InvalidRepOriginId
Definition: origin.h:33

◆ replorigin_session_get_progress()

XLogRecPtr replorigin_session_get_progress ( bool  flush)

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

1188 {
1189  XLogRecPtr remote_lsn;
1190  XLogRecPtr local_lsn;
1191 
1193 
1195  remote_lsn = session_replication_state->remote_lsn;
1196  local_lsn = session_replication_state->local_lsn;
1198 
1199  if (flush && local_lsn != InvalidXLogRecPtr)
1200  XLogFlush(local_lsn);
1201 
1202  return remote_lsn;
1203 }
static ReplicationState * session_replication_state
Definition: origin.c:177
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XLogRecPtr local_lsn
Definition: origin.c:117
void XLogFlush(XLogRecPtr record)
Definition: xlog.c:2848
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1811
LWLock lock
Definition: origin.c:132
XLogRecPtr remote_lsn
Definition: origin.c:110
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:800
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1207

◆ replorigin_session_reset()

void replorigin_session_reset ( void  )

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

1141 {
1142  ConditionVariable *cv;
1143 
1145 
1146  if (session_replication_state == NULL)
1147  ereport(ERROR,
1148  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1149  errmsg("no replication origin is configured")));
1150 
1151  LWLockAcquire(ReplicationOriginLock, LW_EXCLUSIVE);
1152 
1156 
1157  LWLockRelease(ReplicationOriginLock);
1158 
1160 }
static ReplicationState * session_replication_state
Definition: origin.c:177
void ConditionVariableBroadcast(ConditionVariable *cv)
int errcode(int sqlerrcode)
Definition: elog.c:691
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1811
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:155
int max_replication_slots
Definition: slot.c:99
#define Assert(condition)
Definition: c.h:800
ConditionVariable origin_cv
Definition: origin.c:127
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1207
int errmsg(const char *fmt,...)
Definition: elog.c:902

◆ replorigin_session_setup()

void replorigin_session_setup ( RepOriginId  node)

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

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

◆ StartupReplicationOrigin()

void StartupReplicationOrigin ( void  )

Definition at line 666 of file origin.c.

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

Referenced by StartupXLOG().

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

Variable Documentation

◆ replication_states

ReplicationState* replication_states
static

Definition at line 165 of file origin.c.

◆ replication_states_ctl

ReplicationStateCtl* replication_states_ctl
static

Definition at line 170 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 177 of file origin.c.