PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
slotsync.h File Reference
Include dependency graph for slotsync.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

char * CheckAndGetDbnameFromConninfo (void)
 
bool ValidateSlotSyncParams (int elevel)
 
pg_noreturn void ReplSlotSyncWorkerMain (const void *startup_data, size_t startup_data_len)
 
void ShutDownSlotSync (void)
 
bool SlotSyncWorkerCanRestart (void)
 
bool IsSyncingReplicationSlots (void)
 
Size SlotSyncShmemSize (void)
 
void SlotSyncShmemInit (void)
 
void SyncReplicationSlots (WalReceiverConn *wrconn)
 

Variables

PGDLLIMPORT bool sync_replication_slots
 
PGDLLIMPORT char * PrimaryConnInfo
 
PGDLLIMPORT char * PrimarySlotName
 

Function Documentation

◆ CheckAndGetDbnameFromConninfo()

char * CheckAndGetDbnameFromConninfo ( void  )

Definition at line 1016 of file slotsync.c.

1017{
1018 char *dbname;
1019
1020 /*
1021 * The slot synchronization needs a database connection for walrcv_exec to
1022 * work.
1023 */
1025 if (dbname == NULL)
1026 ereport(ERROR,
1027 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1028
1029 /*
1030 * translator: first %s is a connection option; second %s is a GUC
1031 * variable name
1032 */
1033 errmsg("replication slot synchronization requires \"%s\" to be specified in \"%s\"",
1034 "dbname", "primary_conninfo"));
1035 return dbname;
1036}
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
char * dbname
Definition: streamutil.c:49
#define walrcv_get_dbname_from_conninfo(conninfo)
Definition: walreceiver.h:445
char * PrimaryConnInfo
Definition: xlogrecovery.c:97

References dbname, ereport, errcode(), errmsg(), ERROR, PrimaryConnInfo, and walrcv_get_dbname_from_conninfo.

Referenced by pg_sync_replication_slots(), and ReplSlotSyncWorkerMain().

◆ IsSyncingReplicationSlots()

bool IsSyncingReplicationSlots ( void  )

Definition at line 1653 of file slotsync.c.

1654{
1655 return syncing_slots;
1656}
static bool syncing_slots
Definition: slotsync.c:127

References syncing_slots.

Referenced by CreateDecodingContext(), GetStandbyFlushRecPtr(), and ReplicationSlotCreate().

◆ ReplSlotSyncWorkerMain()

pg_noreturn void ReplSlotSyncWorkerMain ( const void *  startup_data,
size_t  startup_data_len 
)

Definition at line 1335 of file slotsync.c.

1336{
1337 WalReceiverConn *wrconn = NULL;
1338 char *dbname;
1339 char *err;
1340 sigjmp_buf local_sigjmp_buf;
1341 StringInfoData app_name;
1342
1343 Assert(startup_data_len == 0);
1344
1346
1347 init_ps_display(NULL);
1348
1350
1351 /*
1352 * Create a per-backend PGPROC struct in shared memory. We must do this
1353 * before we access any shared memory.
1354 */
1355 InitProcess();
1356
1357 /*
1358 * Early initialization.
1359 */
1360 BaseInit();
1361
1362 Assert(SlotSyncCtx != NULL);
1363
1364 /*
1365 * If an exception is encountered, processing resumes here.
1366 *
1367 * We just need to clean up, report the error, and go away.
1368 *
1369 * If we do not have this handling here, then since this worker process
1370 * operates at the bottom of the exception stack, ERRORs turn into FATALs.
1371 * Therefore, we create our own exception handler to catch ERRORs.
1372 */
1373 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
1374 {
1375 /* since not using PG_TRY, must reset error stack by hand */
1376 error_context_stack = NULL;
1377
1378 /* Prevents interrupts while cleaning up */
1380
1381 /* Report the error to the server log */
1383
1384 /*
1385 * We can now go away. Note that because we called InitProcess, a
1386 * callback was registered to do ProcKill, which will clean up
1387 * necessary state.
1388 */
1389 proc_exit(0);
1390 }
1391
1392 /* We can now handle ereport(ERROR) */
1393 PG_exception_stack = &local_sigjmp_buf;
1394
1395 /* Setup signal handling */
1398 pqsignal(SIGTERM, die);
1401 pqsignal(SIGUSR2, SIG_IGN);
1402 pqsignal(SIGPIPE, SIG_IGN);
1403 pqsignal(SIGCHLD, SIG_DFL);
1404
1406
1407 ereport(LOG, errmsg("slot sync worker started"));
1408
1409 /* Register it as soon as SlotSyncCtx->pid is initialized. */
1411
1412 /*
1413 * Establishes SIGALRM handler and initialize timeout module. It is needed
1414 * by InitPostgres to register different timeouts.
1415 */
1417
1418 /* Load the libpq-specific functions */
1419 load_file("libpqwalreceiver", false);
1420
1421 /*
1422 * Unblock signals (they were blocked when the postmaster forked us)
1423 */
1424 sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
1425
1426 /*
1427 * Set always-secure search path, so malicious users can't redirect user
1428 * code (e.g. operators).
1429 *
1430 * It's not strictly necessary since we won't be scanning or writing to
1431 * any user table locally, but it's good to retain it here for added
1432 * precaution.
1433 */
1434 SetConfigOption("search_path", "", PGC_SUSET, PGC_S_OVERRIDE);
1435
1437
1438 /*
1439 * Connect to the database specified by the user in primary_conninfo. We
1440 * need a database connection for walrcv_exec to work which we use to
1441 * fetch slot information from the remote node. See comments atop
1442 * libpqrcv_exec.
1443 *
1444 * We do not specify a specific user here since the slot sync worker will
1445 * operate as a superuser. This is safe because the slot sync worker does
1446 * not interact with user tables, eliminating the risk of executing
1447 * arbitrary code within triggers.
1448 */
1449 InitPostgres(dbname, InvalidOid, NULL, InvalidOid, 0, NULL);
1450
1452
1453 initStringInfo(&app_name);
1454 if (cluster_name[0])
1455 appendStringInfo(&app_name, "%s_%s", cluster_name, "slotsync worker");
1456 else
1457 appendStringInfoString(&app_name, "slotsync worker");
1458
1459 /*
1460 * Establish the connection to the primary server for slot
1461 * synchronization.
1462 */
1463 wrconn = walrcv_connect(PrimaryConnInfo, false, false, false,
1464 app_name.data, &err);
1465 pfree(app_name.data);
1466
1467 if (!wrconn)
1468 ereport(ERROR,
1469 errcode(ERRCODE_CONNECTION_FAILURE),
1470 errmsg("synchronization worker \"%s\" could not connect to the primary server: %s",
1471 app_name.data, err));
1472
1473 /*
1474 * Register the disconnection callback.
1475 *
1476 * XXX: This can be combined with previous cleanup registration of
1477 * slotsync_worker_onexit() but that will need the connection to be made
1478 * global and we want to avoid introducing global for this purpose.
1479 */
1481
1482 /*
1483 * Using the specified primary server connection, check that we are not a
1484 * cascading standby and slot configured in 'primary_slot_name' exists on
1485 * the primary server.
1486 */
1488
1489 /* Main loop to synchronize slots */
1490 for (;;)
1491 {
1492 bool some_slot_updated = false;
1493
1495
1496 some_slot_updated = synchronize_slots(wrconn);
1497
1498 wait_for_slot_activity(some_slot_updated);
1499 }
1500
1501 /*
1502 * The slot sync worker can't get here because it will only stop when it
1503 * receives a SIGINT from the startup process, or when there is an error.
1504 */
1505 Assert(false);
1506}
sigset_t UnBlockSig
Definition: pqsignal.c:22
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:134
void EmitErrorReport(void)
Definition: elog.c:1709
ErrorContextCallback * error_context_stack
Definition: elog.c:95
sigjmp_buf * PG_exception_stack
Definition: elog.c:97
#define LOG
Definition: elog.h:31
void err(int eval, const char *fmt,...)
Definition: err.c:43
int MyProcPid
Definition: globals.c:48
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4332
@ PGC_S_OVERRIDE
Definition: guc.h:123
@ PGC_SUSET
Definition: guc.h:78
char * cluster_name
Definition: guc_tables.c:554
Assert(PointerIsAligned(start, uint64))
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:109
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:65
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:337
void proc_exit(int code)
Definition: ipc.c:104
void pfree(void *pointer)
Definition: mcxt.c:2147
@ NormalProcessing
Definition: miscadmin.h:472
@ InitProcessing
Definition: miscadmin.h:471
#define GetProcessingMode()
Definition: miscadmin.h:481
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:134
#define SetProcessingMode(mode)
Definition: miscadmin.h:483
@ B_SLOTSYNC_WORKER
Definition: miscadmin.h:348
BackendType MyBackendType
Definition: miscinit.c:64
#define die(msg)
#define pqsignal
Definition: port.h:531
void FloatExceptionHandler(SIGNAL_ARGS)
Definition: postgres.c:3075
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
uintptr_t Datum
Definition: postgres.h:69
#define InvalidOid
Definition: postgres_ext.h:35
void BaseInit(void)
Definition: postinit.c:616
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, bits32 flags, char *out_dbname)
Definition: postinit.c:723
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:673
void init_ps_display(const char *fixed_part)
Definition: ps_status.c:269
static void slotsync_worker_disconnect(int code, Datum arg)
Definition: slotsync.c:1181
static SlotSyncCtxStruct * SlotSyncCtx
Definition: slotsync.c:104
char * CheckAndGetDbnameFromConninfo(void)
Definition: slotsync.c:1016
static bool synchronize_slots(WalReceiverConn *wrconn)
Definition: slotsync.c:792
static void wait_for_slot_activity(bool some_slot_updated)
Definition: slotsync.c:1240
static void slotsync_worker_onexit(int code, Datum arg)
Definition: slotsync.c:1194
static void check_and_set_sync_info(pid_t worker_pid)
Definition: slotsync.c:1275
static void validate_remote_info(WalReceiverConn *wrconn)
Definition: slotsync.c:938
static void ProcessSlotSyncInterrupts(WalReceiverConn *wrconn)
Definition: slotsync.c:1159
void InitProcess(void)
Definition: proc.c:391
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
void appendStringInfoString(StringInfo str, const char *s)
Definition: stringinfo.c:230
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
void InitializeTimeouts(void)
Definition: timeout.c:470
static WalReceiverConn * wrconn
Definition: walreceiver.c:93
#define walrcv_connect(conninfo, replication, logical, must_use_password, appname, err)
Definition: walreceiver.h:435
#define SIGCHLD
Definition: win32_port.h:168
#define SIGHUP
Definition: win32_port.h:158
#define SIGPIPE
Definition: win32_port.h:163
#define SIGUSR1
Definition: win32_port.h:170
#define SIGUSR2
Definition: win32_port.h:171

References appendStringInfo(), appendStringInfoString(), Assert(), B_SLOTSYNC_WORKER, BaseInit(), before_shmem_exit(), check_and_set_sync_info(), CheckAndGetDbnameFromConninfo(), cluster_name, StringInfoData::data, dbname, die, EmitErrorReport(), ereport, err(), errcode(), errmsg(), ERROR, error_context_stack, FloatExceptionHandler(), GetProcessingMode, HOLD_INTERRUPTS, init_ps_display(), InitializeTimeouts(), InitPostgres(), InitProcess(), InitProcessing, initStringInfo(), InvalidOid, load_file(), LOG, MyBackendType, MyProcPid, NormalProcessing, pfree(), PG_exception_stack, PGC_S_OVERRIDE, PGC_SUSET, PointerGetDatum(), pqsignal, PrimaryConnInfo, proc_exit(), ProcessSlotSyncInterrupts(), procsignal_sigusr1_handler(), SetConfigOption(), SetProcessingMode, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), SignalHandlerForShutdownRequest(), SIGPIPE, SIGUSR1, SIGUSR2, slotsync_worker_disconnect(), slotsync_worker_onexit(), SlotSyncCtx, synchronize_slots(), UnBlockSig, validate_remote_info(), wait_for_slot_activity(), walrcv_connect, and wrconn.

◆ ShutDownSlotSync()

void ShutDownSlotSync ( void  )

Definition at line 1565 of file slotsync.c.

1566{
1567 pid_t worker_pid;
1568
1570
1571 SlotSyncCtx->stopSignaled = true;
1572
1573 /*
1574 * Return if neither the slot sync worker is running nor the function
1575 * pg_sync_replication_slots() is executing.
1576 */
1577 if (!SlotSyncCtx->syncing)
1578 {
1581 return;
1582 }
1583
1584 worker_pid = SlotSyncCtx->pid;
1585
1587
1588 if (worker_pid != InvalidPid)
1589 kill(worker_pid, SIGINT);
1590
1591 /* Wait for slot sync to end */
1592 for (;;)
1593 {
1594 int rc;
1595
1596 /* Wait a bit, we don't expect to have to wait long */
1597 rc = WaitLatch(MyLatch,
1599 10L, WAIT_EVENT_REPLICATION_SLOTSYNC_SHUTDOWN);
1600
1601 if (rc & WL_LATCH_SET)
1602 {
1605 }
1606
1608
1609 /* Ensure that no process is syncing the slots. */
1610 if (!SlotSyncCtx->syncing)
1611 break;
1612
1614 }
1615
1617
1619}
struct Latch * MyLatch
Definition: globals.c:64
void ResetLatch(Latch *latch)
Definition: latch.c:372
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:172
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
#define InvalidPid
Definition: miscadmin.h:32
static void update_synced_slots_inactive_since(void)
Definition: slotsync.c:1515
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
#define WL_TIMEOUT
Definition: waiteventset.h:37
#define WL_EXIT_ON_PM_DEATH
Definition: waiteventset.h:39
#define WL_LATCH_SET
Definition: waiteventset.h:34
#define kill(pid, sig)
Definition: win32_port.h:493

References CHECK_FOR_INTERRUPTS, InvalidPid, kill, SlotSyncCtxStruct::mutex, MyLatch, SlotSyncCtxStruct::pid, ResetLatch(), SlotSyncCtx, SpinLockAcquire, SpinLockRelease, SlotSyncCtxStruct::stopSignaled, SlotSyncCtxStruct::syncing, update_synced_slots_inactive_since(), WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and WL_TIMEOUT.

Referenced by FinishWalRecovery().

◆ SlotSyncShmemInit()

void SlotSyncShmemInit ( void  )

Definition at line 1671 of file slotsync.c.

1672{
1673 Size size = SlotSyncShmemSize();
1674 bool found;
1675
1677 ShmemInitStruct("Slot Sync Data", size, &found);
1678
1679 if (!found)
1680 {
1681 memset(SlotSyncCtx, 0, size);
1684 }
1685}
size_t Size
Definition: c.h:576
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387
Size SlotSyncShmemSize(void)
Definition: slotsync.c:1662
#define SpinLockInit(lock)
Definition: spin.h:57

References InvalidPid, SlotSyncCtxStruct::mutex, SlotSyncCtxStruct::pid, ShmemInitStruct(), SlotSyncCtx, SlotSyncShmemSize(), and SpinLockInit.

Referenced by CreateOrAttachShmemStructs().

◆ SlotSyncShmemSize()

Size SlotSyncShmemSize ( void  )

Definition at line 1662 of file slotsync.c.

1663{
1664 return sizeof(SlotSyncCtxStruct);
1665}
struct SlotSyncCtxStruct SlotSyncCtxStruct

Referenced by CalculateShmemSize(), and SlotSyncShmemInit().

◆ SlotSyncWorkerCanRestart()

bool SlotSyncWorkerCanRestart ( void  )

Definition at line 1633 of file slotsync.c.

1634{
1635 time_t curtime = time(NULL);
1636
1637 /* Return false if too soon since last start. */
1638 if ((unsigned int) (curtime - SlotSyncCtx->last_start_time) <
1639 (unsigned int) SLOTSYNC_RESTART_INTERVAL_SEC)
1640 return false;
1641
1642 SlotSyncCtx->last_start_time = curtime;
1643
1644 return true;
1645}
#define SLOTSYNC_RESTART_INTERVAL_SEC
Definition: slotsync.c:120
time_t last_start_time
Definition: slotsync.c:100

References SlotSyncCtxStruct::last_start_time, SLOTSYNC_RESTART_INTERVAL_SEC, and SlotSyncCtx.

Referenced by LaunchMissingBackgroundProcesses().

◆ SyncReplicationSlots()

void SyncReplicationSlots ( WalReceiverConn wrconn)

Definition at line 1728 of file slotsync.c.

1729{
1731 {
1733
1735
1737
1738 /* Cleanup the synced temporary slots */
1740
1741 /* We are done with sync, so reset sync flag */
1743 }
1745}
#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:47
#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition: ipc.h:52
void ReplicationSlotCleanup(bool synced_only)
Definition: slot.c:775
static void slotsync_failure_callback(int code, Datum arg)
Definition: slotsync.c:1691
static void reset_syncing_flag()
Definition: slotsync.c:1319

References check_and_set_sync_info(), InvalidPid, PG_END_ENSURE_ERROR_CLEANUP, PG_ENSURE_ERROR_CLEANUP, PointerGetDatum(), ReplicationSlotCleanup(), reset_syncing_flag(), slotsync_failure_callback(), synchronize_slots(), validate_remote_info(), and wrconn.

Referenced by pg_sync_replication_slots().

◆ ValidateSlotSyncParams()

bool ValidateSlotSyncParams ( int  elevel)

Definition at line 1043 of file slotsync.c.

1044{
1045 /*
1046 * Logical slot sync/creation requires wal_level >= logical.
1047 *
1048 * Since altering the wal_level requires a server restart, so error out in
1049 * this case regardless of elevel provided by caller.
1050 */
1052 ereport(ERROR,
1053 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1054 errmsg("replication slot synchronization requires \"wal_level\" >= \"logical\""));
1055
1056 /*
1057 * A physical replication slot(primary_slot_name) is required on the
1058 * primary to ensure that the rows needed by the standby are not removed
1059 * after restarting, so that the synchronized slot on the standby will not
1060 * be invalidated.
1061 */
1062 if (PrimarySlotName == NULL || *PrimarySlotName == '\0')
1063 {
1064 ereport(elevel,
1065 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1066 /* translator: %s is a GUC variable name */
1067 errmsg("replication slot synchronization requires \"%s\" to be set", "primary_slot_name"));
1068 return false;
1069 }
1070
1071 /*
1072 * hot_standby_feedback must be enabled to cooperate with the physical
1073 * replication slot, which allows informing the primary about the xmin and
1074 * catalog_xmin values on the standby.
1075 */
1077 {
1078 ereport(elevel,
1079 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1080 /* translator: %s is a GUC variable name */
1081 errmsg("replication slot synchronization requires \"%s\" to be enabled",
1082 "hot_standby_feedback"));
1083 return false;
1084 }
1085
1086 /*
1087 * The primary_conninfo is required to make connection to primary for
1088 * getting slots information.
1089 */
1090 if (PrimaryConnInfo == NULL || *PrimaryConnInfo == '\0')
1091 {
1092 ereport(elevel,
1093 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1094 /* translator: %s is a GUC variable name */
1095 errmsg("replication slot synchronization requires \"%s\" to be set",
1096 "primary_conninfo"));
1097 return false;
1098 }
1099
1100 return true;
1101}
bool hot_standby_feedback
Definition: walreceiver.c:90
int wal_level
Definition: xlog.c:131
@ WAL_LEVEL_LOGICAL
Definition: xlog.h:76
char * PrimarySlotName
Definition: xlogrecovery.c:98

References ereport, errcode(), errmsg(), ERROR, hot_standby_feedback, PrimaryConnInfo, PrimarySlotName, wal_level, and WAL_LEVEL_LOGICAL.

Referenced by LaunchMissingBackgroundProcesses(), and pg_sync_replication_slots().

Variable Documentation

◆ PrimaryConnInfo

◆ PrimarySlotName

◆ sync_replication_slots

PGDLLIMPORT bool sync_replication_slots
extern

Definition at line 107 of file slotsync.c.

Referenced by LaunchMissingBackgroundProcesses(), and slotsync_reread_config().