92 #include "utils/fmgroids.h"
99 #define PG_REPLORIGIN_CHECKPOINT_FILENAME PG_LOGICAL_DIR "/replorigin_checkpoint"
100 #define PG_REPLORIGIN_CHECKPOINT_TMPFILE PG_REPLORIGIN_CHECKPOINT_FILENAME ".tmp"
186 #define REPLICATION_STATE_MAGIC ((uint32) 0x1257DADE)
193 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
194 errmsg(
"cannot query or manipulate replication origin when \"max_replication_slots\" is 0")));
198 (
errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
199 errmsg(
"cannot manipulate replication origins during recovery")));
238 roident =
ident->roident;
241 else if (!missing_ok)
243 (
errcode(ERRCODE_UNDEFINED_OBJECT),
244 errmsg(
"replication origin \"%s\" does not exist",
291 bool nulls[Natts_pg_replication_origin];
298 Anum_pg_replication_origin_roident,
317 memset(&nulls, 0,
sizeof(nulls));
320 values[Anum_pg_replication_origin_roname - 1] = roname_d;
334 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
335 errmsg(
"could not find free replication origin ID")));
359 if (
state->roident == roident)
362 if (
state->acquired_by != 0)
368 (
errcode(ERRCODE_OBJECT_IN_USE),
369 errmsg(
"could not drop replication origin with ID %d, in use by PID %d",
371 state->acquired_by)));
380 cv = &
state->origin_cv;
435 elog(
ERROR,
"cache lookup failed for replication origin with ID %d",
495 (
errcode(ERRCODE_UNDEFINED_OBJECT),
496 errmsg(
"replication origin with ID %d does not exist",
592 if (unlink(tmppath) < 0 && errno != ENOENT)
595 errmsg(
"could not remove file \"%s\": %m",
603 O_CREAT | O_EXCL | O_WRONLY |
PG_BINARY);
607 errmsg(
"could not create file \"%s\": %m",
612 if ((
write(tmpfd, &magic,
sizeof(magic))) !=
sizeof(magic))
619 errmsg(
"could not write to file \"%s\": %m",
638 memset(&disk_state, 0,
sizeof(disk_state));
653 if ((
write(tmpfd, &disk_state,
sizeof(disk_state))) !=
661 errmsg(
"could not write to file \"%s\": %m",
680 errmsg(
"could not write to file \"%s\": %m",
687 errmsg(
"could not close file \"%s\": %m",
714 #ifdef USE_ASSERT_CHECKING
715 static bool already_started =
false;
718 already_started =
true;
726 elog(
DEBUG2,
"starting up replication origin progress state");
734 if (
fd < 0 && errno == ENOENT)
739 errmsg(
"could not open file \"%s\": %m",
743 readBytes =
read(
fd, &magic,
sizeof(magic));
744 if (readBytes !=
sizeof(magic))
749 errmsg(
"could not read file \"%s\": %m",
754 errmsg(
"could not read file \"%s\": read %d of %zu",
755 path, readBytes,
sizeof(magic))));
761 (
errmsg(
"replication checkpoint has wrong magic %u instead of %u",
771 readBytes =
read(
fd, &disk_state,
sizeof(disk_state));
774 if (readBytes ==
sizeof(
crc))
785 errmsg(
"could not read file \"%s\": %m",
789 if (readBytes !=
sizeof(disk_state))
793 errmsg(
"could not read file \"%s\": read %d of %zu",
794 path, readBytes,
sizeof(disk_state))));
801 (
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
802 errmsg(
"could not find free replication state, increase \"max_replication_slots\"")));
810 (
errmsg(
"recovered replication state of node %d to %X/%X",
820 errmsg(
"replication slot checkpoint has wrong checksum %u, expected %u",
826 errmsg(
"could not close file \"%s\": %m",
872 elog(
PANIC,
"replorigin_redo: unknown op code %u", info);
894 bool go_backward,
bool wal_log)
928 free_state = curstate;
939 replication_state = curstate;
947 (
errcode(ERRCODE_OBJECT_IN_USE),
948 errmsg(
"replication origin with ID %d is already active for PID %d",
956 if (replication_state == NULL && free_state == NULL)
958 (
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
959 errmsg(
"could not find free replication state slot for replication origin with ID %d",
961 errhint(
"Increase \"max_replication_slots\" and try again.")));
963 if (replication_state == NULL)
967 replication_state = free_state;
970 replication_state->
roident = node;
986 xlrec.
force = go_backward;
1002 if (go_backward || replication_state->
remote_lsn < remote_commit)
1003 replication_state->
remote_lsn = remote_commit;
1005 (go_backward || replication_state->
local_lsn < local_commit))
1006 replication_state->
local_lsn = local_commit;
1033 if (
state->roident == node)
1037 remote_lsn =
state->remote_lsn;
1038 local_lsn =
state->local_lsn;
1103 static bool registered_cleanup;
1107 if (!registered_cleanup)
1110 registered_cleanup =
true;
1117 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1118 errmsg(
"cannot setup replication origin when one is already setup")));
1140 if (curstate->
roident != node)
1143 else if (curstate->
acquired_by != 0 && acquired_by == 0)
1146 (
errcode(ERRCODE_OBJECT_IN_USE),
1147 errmsg(
"replication origin with ID %d is already active for PID %d",
1159 (
errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
1160 errmsg(
"could not find free replication state slot for replication origin with ID %d",
1162 errhint(
"Increase \"max_replication_slots\" and try again.")));
1175 if (acquired_by == 0)
1178 elog(
ERROR,
"could not find replication state slot for replication origin with OID %u which was acquired by %d",
1202 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1203 errmsg(
"no replication origin is configured")));
1288 (
errcode(ERRCODE_RESERVED_NAME),
1289 errmsg(
"replication origin name \"%s\" is reserved",
1291 errdetail(
"Origin names \"%s\", \"%s\", and names starting with \"pg_\" are reserved.",
1292 LOGICALREP_ORIGIN_ANY, LOGICALREP_ORIGIN_NONE)));
1298 #ifdef ENFORCE_REGRESSION_TEST_NAME_RESTRICTIONS
1299 if (strncmp(
name,
"regress_", 8) != 0)
1300 elog(
WARNING,
"replication origins created by regression test cases should have names starting with \"regress_\"");
1418 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1419 errmsg(
"no replication origin is configured")));
1438 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1439 errmsg(
"no replication origin is configured")));
1553 memset(nulls, 1,
sizeof(nulls));
1585 #undef REPLICATION_ORIGIN_PROGRESS_COLS
static Datum values[MAXATTR]
#define CStringGetTextDatum(s)
#define Assert(condition)
#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)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
void heap_freetuple(HeapTuple htup)
#define HeapTupleIsValid(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)
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)
static void replorigin_check_prerequisites(bool check_slots, bool recoveryOK)
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 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)
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Size mul_size(Size s1, Size s2)
static pg_noinline void Size size
int max_replication_slots
#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 char *data, uint32 len)
void XLogBeginInsert(void)
#define XLogRecGetInfo(decoder)
#define XLogRecGetData(decoder)