92#include "utils/fmgroids.h"
100#define PG_REPLORIGIN_CHECKPOINT_FILENAME PG_LOGICAL_DIR "/replorigin_checkpoint"
101#define PG_REPLORIGIN_CHECKPOINT_TMPFILE PG_REPLORIGIN_CHECKPOINT_FILENAME ".tmp"
187#define REPLICATION_STATE_MAGIC ((uint32) 0x1257DADE)
194 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
195 errmsg(
"cannot query or manipulate replication origin when \"max_active_replication_origins\" is 0")));
199 (
errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
200 errmsg(
"cannot manipulate replication origins during recovery")));
239 roident =
ident->roident;
242 else if (!missing_ok)
244 (
errcode(ERRCODE_UNDEFINED_OBJECT),
245 errmsg(
"replication origin \"%s\" does not exist",
274 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
275 errmsg(
"replication origin name is too long"),
276 errdetail(
"Replication origin names must be no longer than %d bytes.",
315 bool nulls[Natts_pg_replication_origin];
322 Anum_pg_replication_origin_roident,
341 memset(&nulls, 0,
sizeof(nulls));
344 values[Anum_pg_replication_origin_roname - 1] = roname_d;
358 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
359 errmsg(
"could not find free replication origin ID")));
383 if (
state->roident == roident)
386 if (
state->acquired_by != 0)
392 (
errcode(ERRCODE_OBJECT_IN_USE),
393 errmsg(
"could not drop replication origin with ID %d, in use by PID %d",
395 state->acquired_by)));
404 cv = &
state->origin_cv;
459 elog(
ERROR,
"cache lookup failed for replication origin with ID %d",
519 (
errcode(ERRCODE_UNDEFINED_OBJECT),
520 errmsg(
"replication origin with ID %d does not exist",
611 if (unlink(tmppath) < 0 && errno != ENOENT)
614 errmsg(
"could not remove file \"%s\": %m",
622 O_CREAT | O_EXCL | O_WRONLY |
PG_BINARY);
626 errmsg(
"could not create file \"%s\": %m",
631 if ((
write(tmpfd, &magic,
sizeof(magic))) !=
sizeof(magic))
638 errmsg(
"could not write to file \"%s\": %m",
657 memset(&disk_state, 0,
sizeof(disk_state));
672 if ((
write(tmpfd, &disk_state,
sizeof(disk_state))) !=
680 errmsg(
"could not write to file \"%s\": %m",
699 errmsg(
"could not write to file \"%s\": %m",
706 errmsg(
"could not close file \"%s\": %m",
733#ifdef USE_ASSERT_CHECKING
734 static bool already_started =
false;
737 already_started =
true;
745 elog(
DEBUG2,
"starting up replication origin progress state");
753 if (
fd < 0 && errno == ENOENT)
758 errmsg(
"could not open file \"%s\": %m",
762 readBytes =
read(
fd, &magic,
sizeof(magic));
763 if (readBytes !=
sizeof(magic))
768 errmsg(
"could not read file \"%s\": %m",
773 errmsg(
"could not read file \"%s\": read %d of %zu",
774 path, readBytes,
sizeof(magic))));
780 (
errmsg(
"replication checkpoint has wrong magic %u instead of %u",
790 readBytes =
read(
fd, &disk_state,
sizeof(disk_state));
793 if (readBytes ==
sizeof(
crc))
804 errmsg(
"could not read file \"%s\": %m",
808 if (readBytes !=
sizeof(disk_state))
812 errmsg(
"could not read file \"%s\": read %d of %zu",
813 path, readBytes,
sizeof(disk_state))));
820 (
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
821 errmsg(
"could not find free replication state, increase \"max_active_replication_origins\"")));
829 (
errmsg(
"recovered replication state of node %d to %X/%X",
839 errmsg(
"replication slot checkpoint has wrong checksum %u, expected %u",
845 errmsg(
"could not close file \"%s\": %m",
891 elog(
PANIC,
"replorigin_redo: unknown op code %u", info);
913 bool go_backward,
bool wal_log)
947 free_state = curstate;
958 replication_state = curstate;
966 (
errcode(ERRCODE_OBJECT_IN_USE),
967 errmsg(
"replication origin with ID %d is already active for PID %d",
975 if (replication_state == NULL && free_state == NULL)
977 (
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
978 errmsg(
"could not find free replication state slot for replication origin with ID %d",
980 errhint(
"Increase \"max_active_replication_origins\" and try again.")));
982 if (replication_state == NULL)
986 replication_state = free_state;
989 replication_state->
roident = node;
1005 xlrec.
force = go_backward;
1021 if (go_backward || replication_state->
remote_lsn < remote_commit)
1022 replication_state->
remote_lsn = remote_commit;
1024 (go_backward || replication_state->
local_lsn < local_commit))
1025 replication_state->
local_lsn = local_commit;
1052 if (
state->roident == node)
1056 remote_lsn =
state->remote_lsn;
1057 local_lsn =
state->local_lsn;
1122 static bool registered_cleanup;
1126 if (!registered_cleanup)
1129 registered_cleanup =
true;
1136 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1137 errmsg(
"cannot setup replication origin when one is already setup")));
1159 if (curstate->
roident != node)
1162 else if (curstate->
acquired_by != 0 && acquired_by == 0)
1165 (
errcode(ERRCODE_OBJECT_IN_USE),
1166 errmsg(
"replication origin with ID %d is already active for PID %d",
1178 (
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
1179 errmsg(
"could not find free replication state slot for replication origin with ID %d",
1181 errhint(
"Increase \"max_active_replication_origins\" and try again.")));
1194 if (acquired_by == 0)
1197 elog(
ERROR,
"could not find replication state slot for replication origin with OID %u which was acquired by %d",
1221 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1222 errmsg(
"no replication origin is configured")));
1307 (
errcode(ERRCODE_RESERVED_NAME),
1308 errmsg(
"replication origin name \"%s\" is reserved",
1310 errdetail(
"Origin names \"%s\", \"%s\", and names starting with \"pg_\" are reserved.",
1311 LOGICALREP_ORIGIN_ANY, LOGICALREP_ORIGIN_NONE)));
1317#ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
1318 if (strncmp(
name,
"regress_", 8) != 0)
1319 elog(
WARNING,
"replication origins created by regression test cases should have names starting with \"regress_\"");
1437 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1438 errmsg(
"no replication origin is configured")));
1457 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1458 errmsg(
"no replication origin is configured")));
1572 memset(nulls, 1,
sizeof(nulls));
1604#undef REPLICATION_ORIGIN_PROGRESS_COLS
static Datum values[MAXATTR]
#define CStringGetTextDatum(s)
#define FLEXIBLE_ARRAY_MEMBER
#define MemSet(start, val, len)
#define OidIsValid(objectId)
bool IsReservedName(const char *name)
bool ConditionVariableCancelSleep(void)
void ConditionVariableBroadcast(ConditionVariable *cv)
void ConditionVariableInit(ConditionVariable *cv)
void ConditionVariableSleep(ConditionVariable *cv, uint32 wait_event_info)
int errcode_for_file_access(void)
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
int durable_rename(const char *oldfile, const char *newfile, int elevel)
int CloseTransientFile(int fd)
int OpenTransientFile(const char *fileName, int fileFlags)
#define PG_GETARG_TEXT_PP(n)
#define PG_GETARG_DATUM(n)
#define PG_GETARG_BOOL(n)
#define PG_RETURN_BOOL(x)
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
void systable_endscan(SysScanDesc sysscan)
HeapTuple systable_getnext(SysScanDesc sysscan)
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Assert(PointerIsAligned(start, uint64))
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
void heap_freetuple(HeapTuple htup)
#define HeapTupleIsValid(tuple)
static void * GETSTRUCT(const HeapTupleData *tuple)
void CatalogTupleInsert(Relation heapRel, HeapTuple tup)
void CatalogTupleDelete(Relation heapRel, ItemPointer tid)
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
void UnlockRelationOid(Oid relid, LOCKMODE lockmode)
void LockRelationOid(Oid relid, LOCKMODE lockmode)
void UnlockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
#define AccessExclusiveLock
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
void LWLockRelease(LWLock *lock)
void LWLockInitialize(LWLock *lock, int tranche_id)
@ LWTRANCHE_REPLICATION_ORIGIN_STATE
void pfree(void *pointer)
#define CHECK_FOR_INTERRUPTS()
TimestampTz replorigin_session_origin_timestamp
static ReplicationStateCtl * replication_states_ctl
RepOriginId replorigin_by_name(const char *roname, bool missing_ok)
Size ReplicationOriginShmemSize(void)
RepOriginId replorigin_create(const char *roname)
Datum pg_replication_origin_progress(PG_FUNCTION_ARGS)
void replorigin_session_reset(void)
struct ReplicationState ReplicationState
static bool IsReservedOriginName(const char *name)
void replorigin_session_advance(XLogRecPtr remote_commit, XLogRecPtr local_commit)
bool replorigin_by_oid(RepOriginId roident, bool missing_ok, char **roname)
int max_active_replication_origins
Datum pg_replication_origin_advance(PG_FUNCTION_ARGS)
XLogRecPtr replorigin_get_progress(RepOriginId node, bool flush)
#define PG_REPLORIGIN_CHECKPOINT_TMPFILE
Datum pg_replication_origin_session_progress(PG_FUNCTION_ARGS)
static ReplicationState * replication_states
#define PG_REPLORIGIN_CHECKPOINT_FILENAME
Datum pg_replication_origin_session_reset(PG_FUNCTION_ARGS)
Datum pg_replication_origin_xact_setup(PG_FUNCTION_ARGS)
Datum pg_replication_origin_session_is_setup(PG_FUNCTION_ARGS)
Datum pg_replication_origin_oid(PG_FUNCTION_ARGS)
Datum pg_replication_origin_session_setup(PG_FUNCTION_ARGS)
static void ReplicationOriginExitCleanup(int code, Datum arg)
void StartupReplicationOrigin(void)
void replorigin_drop_by_name(const char *name, bool missing_ok, bool nowait)
RepOriginId replorigin_session_origin
void replorigin_advance(RepOriginId node, XLogRecPtr remote_commit, XLogRecPtr local_commit, bool go_backward, bool wal_log)
static void replorigin_state_clear(RepOriginId roident, bool nowait)
void replorigin_session_setup(RepOriginId node, int acquired_by)
void CheckPointReplicationOrigin(void)
static void replorigin_check_prerequisites(bool check_origins, bool recoveryOK)
static ReplicationState * session_replication_state
Datum pg_replication_origin_drop(PG_FUNCTION_ARGS)
#define REPLICATION_ORIGIN_PROGRESS_COLS
XLogRecPtr replorigin_session_get_progress(bool flush)
void ReplicationOriginShmemInit(void)
Datum pg_show_replication_origin_status(PG_FUNCTION_ARGS)
#define REPLICATION_STATE_MAGIC
XLogRecPtr replorigin_session_origin_lsn
Datum pg_replication_origin_create(PG_FUNCTION_ARGS)
Datum pg_replication_origin_xact_reset(PG_FUNCTION_ARGS)
void replorigin_redo(XLogReaderState *record)
struct ReplicationStateCtl ReplicationStateCtl
struct ReplicationStateOnDisk ReplicationStateOnDisk
#define InvalidRepOriginId
#define XLOG_REPLORIGIN_DROP
#define XLOG_REPLORIGIN_SET
#define ERRCODE_DATA_CORRUPTED
#define COMP_CRC32C(crc, data, len)
static Datum LSNGetDatum(XLogRecPtr X)
FormData_pg_replication_origin * Form_pg_replication_origin
int pg_strcasecmp(const char *s1, const char *s2)
static Datum ObjectIdGetDatum(Oid X)
static Pointer DatumGetPointer(Datum X)
static int fd(const char *x, int i)
#define RelationGetDescr(relation)
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
Size add_size(Size s1, Size s2)
Size mul_size(Size s1, Size s2)
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
#define InitDirtySnapshot(snapshotdata)
#define BTEqualStrategyNumber
ReplicationState states[FLEXIBLE_ARRAY_MEMBER]
ConditionVariable origin_cv
Tuplestorestate * setResult
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
#define PG_GETARG_TIMESTAMPTZ(n)
char * text_to_cstring(const text *t)
bool IsTransactionState(void)
void CommandCounterIncrement(void)
bool RecoveryInProgress(void)
void XLogFlush(XLogRecPtr record)
#define LSN_FORMAT_ARGS(lsn)
#define InvalidXLogRecPtr
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
void XLogRegisterData(const void *data, uint32 len)
void XLogBeginInsert(void)
#define XLogRecGetInfo(decoder)
#define XLogRecGetData(decoder)