PostgreSQL Source Code git master
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 1110 of file slotsync.c.

1111{
1112 char *dbname;
1113
1114 /*
1115 * The slot synchronization needs a database connection for walrcv_exec to
1116 * work.
1117 */
1119 if (dbname == NULL)
1120 ereport(ERROR,
1121 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1122
1123 /*
1124 * translator: first %s is a connection option; second %s is a GUC
1125 * variable name
1126 */
1127 errmsg("replication slot synchronization requires \"%s\" to be specified in \"%s\"",
1128 "dbname", "primary_conninfo"));
1129 return dbname;
1130}
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
char * dbname
Definition: streamutil.c:49
#define walrcv_get_dbname_from_conninfo(conninfo)
Definition: walreceiver.h:445
char * PrimaryConnInfo
Definition: xlogrecovery.c:99

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 1754 of file slotsync.c.

1755{
1756 return syncing_slots;
1757}
static bool syncing_slots
Definition: slotsync.c:126

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 1429 of file slotsync.c.

1430{
1431 WalReceiverConn *wrconn = NULL;
1432 char *dbname;
1433 char *err;
1434 sigjmp_buf local_sigjmp_buf;
1435 StringInfoData app_name;
1436
1437 Assert(startup_data_len == 0);
1438
1440
1441 init_ps_display(NULL);
1442
1444
1445 /*
1446 * Create a per-backend PGPROC struct in shared memory. We must do this
1447 * before we access any shared memory.
1448 */
1449 InitProcess();
1450
1451 /*
1452 * Early initialization.
1453 */
1454 BaseInit();
1455
1456 Assert(SlotSyncCtx != NULL);
1457
1458 /*
1459 * If an exception is encountered, processing resumes here.
1460 *
1461 * We just need to clean up, report the error, and go away.
1462 *
1463 * If we do not have this handling here, then since this worker process
1464 * operates at the bottom of the exception stack, ERRORs turn into FATALs.
1465 * Therefore, we create our own exception handler to catch ERRORs.
1466 */
1467 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
1468 {
1469 /* since not using PG_TRY, must reset error stack by hand */
1470 error_context_stack = NULL;
1471
1472 /* Prevents interrupts while cleaning up */
1474
1475 /* Report the error to the server log */
1477
1478 /*
1479 * We can now go away. Note that because we called InitProcess, a
1480 * callback was registered to do ProcKill, which will clean up
1481 * necessary state.
1482 */
1483 proc_exit(0);
1484 }
1485
1486 /* We can now handle ereport(ERROR) */
1487 PG_exception_stack = &local_sigjmp_buf;
1488
1489 /* Setup signal handling */
1492 pqsignal(SIGTERM, die);
1495 pqsignal(SIGUSR2, SIG_IGN);
1496 pqsignal(SIGPIPE, SIG_IGN);
1497 pqsignal(SIGCHLD, SIG_DFL);
1498
1500
1501 ereport(LOG, errmsg("slot sync worker started"));
1502
1503 /* Register it as soon as SlotSyncCtx->pid is initialized. */
1505
1506 /*
1507 * Establishes SIGALRM handler and initialize timeout module. It is needed
1508 * by InitPostgres to register different timeouts.
1509 */
1511
1512 /* Load the libpq-specific functions */
1513 load_file("libpqwalreceiver", false);
1514
1515 /*
1516 * Unblock signals (they were blocked when the postmaster forked us)
1517 */
1518 sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
1519
1520 /*
1521 * Set always-secure search path, so malicious users can't redirect user
1522 * code (e.g. operators).
1523 *
1524 * It's not strictly necessary since we won't be scanning or writing to
1525 * any user table locally, but it's good to retain it here for added
1526 * precaution.
1527 */
1528 SetConfigOption("search_path", "", PGC_SUSET, PGC_S_OVERRIDE);
1529
1531
1532 /*
1533 * Connect to the database specified by the user in primary_conninfo. We
1534 * need a database connection for walrcv_exec to work which we use to
1535 * fetch slot information from the remote node. See comments atop
1536 * libpqrcv_exec.
1537 *
1538 * We do not specify a specific user here since the slot sync worker will
1539 * operate as a superuser. This is safe because the slot sync worker does
1540 * not interact with user tables, eliminating the risk of executing
1541 * arbitrary code within triggers.
1542 */
1543 InitPostgres(dbname, InvalidOid, NULL, InvalidOid, 0, NULL);
1544
1546
1547 initStringInfo(&app_name);
1548 if (cluster_name[0])
1549 appendStringInfo(&app_name, "%s_%s", cluster_name, "slotsync worker");
1550 else
1551 appendStringInfoString(&app_name, "slotsync worker");
1552
1553 /*
1554 * Establish the connection to the primary server for slot
1555 * synchronization.
1556 */
1557 wrconn = walrcv_connect(PrimaryConnInfo, false, false, false,
1558 app_name.data, &err);
1559
1560 if (!wrconn)
1561 ereport(ERROR,
1562 errcode(ERRCODE_CONNECTION_FAILURE),
1563 errmsg("synchronization worker \"%s\" could not connect to the primary server: %s",
1564 app_name.data, err));
1565
1566 pfree(app_name.data);
1567
1568 /*
1569 * Register the disconnection callback.
1570 *
1571 * XXX: This can be combined with previous cleanup registration of
1572 * slotsync_worker_onexit() but that will need the connection to be made
1573 * global and we want to avoid introducing global for this purpose.
1574 */
1576
1577 /*
1578 * Using the specified primary server connection, check that we are not a
1579 * cascading standby and slot configured in 'primary_slot_name' exists on
1580 * the primary server.
1581 */
1583
1584 /* Main loop to synchronize slots */
1585 for (;;)
1586 {
1587 bool some_slot_updated = false;
1588
1590
1591 some_slot_updated = synchronize_slots(wrconn);
1592
1593 wait_for_slot_activity(some_slot_updated);
1594 }
1595
1596 /*
1597 * The slot sync worker can't get here because it will only stop when it
1598 * receives a SIGINT from the startup process, or when there is an error.
1599 */
1600 Assert(false);
1601}
sigset_t UnBlockSig
Definition: pqsignal.c:22
void load_file(const char *filename, bool restricted)
Definition: dfmgr.c:149
void EmitErrorReport(void)
Definition: elog.c:1704
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:47
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:4196
@ PGC_S_OVERRIDE
Definition: guc.h:123
@ PGC_SUSET
Definition: guc.h:78
char * cluster_name
Definition: guc_tables.c:555
Assert(PointerIsAligned(start, uint64))
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:104
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:61
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:1594
@ 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:552
void FloatExceptionHandler(SIGNAL_ARGS)
Definition: postgres.c:3079
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
uint64_t Datum
Definition: postgres.h:70
#define InvalidOid
Definition: postgres_ext.h:37
void BaseInit(void)
Definition: postinit.c:607
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, bits32 flags, char *out_dbname)
Definition: postinit.c:707
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:674
void init_ps_display(const char *fixed_part)
Definition: ps_status.c:285
static void slotsync_worker_disconnect(int code, Datum arg)
Definition: slotsync.c:1275
static SlotSyncCtxStruct * SlotSyncCtx
Definition: slotsync.c:103
char * CheckAndGetDbnameFromConninfo(void)
Definition: slotsync.c:1110
static void ProcessSlotSyncInterrupts(void)
Definition: slotsync.c:1253
static bool synchronize_slots(WalReceiverConn *wrconn)
Definition: slotsync.c:886
static void wait_for_slot_activity(bool some_slot_updated)
Definition: slotsync.c:1334
static void slotsync_worker_onexit(int code, Datum arg)
Definition: slotsync.c:1288
static void check_and_set_sync_info(pid_t worker_pid)
Definition: slotsync.c:1369
static void validate_remote_info(WalReceiverConn *wrconn)
Definition: slotsync.c:1032
void InitProcess(void)
Definition: proc.c:395
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 1660 of file slotsync.c.

1661{
1662 pid_t worker_pid;
1663
1665
1666 SlotSyncCtx->stopSignaled = true;
1667
1668 /*
1669 * Return if neither the slot sync worker is running nor the function
1670 * pg_sync_replication_slots() is executing.
1671 */
1672 if (!SlotSyncCtx->syncing)
1673 {
1676 return;
1677 }
1678
1679 worker_pid = SlotSyncCtx->pid;
1680
1682
1683 if (worker_pid != InvalidPid)
1684 kill(worker_pid, SIGINT);
1685
1686 /* Wait for slot sync to end */
1687 for (;;)
1688 {
1689 int rc;
1690
1691 /* Wait a bit, we don't expect to have to wait long */
1692 rc = WaitLatch(MyLatch,
1694 10L, WAIT_EVENT_REPLICATION_SLOTSYNC_SHUTDOWN);
1695
1696 if (rc & WL_LATCH_SET)
1697 {
1700 }
1701
1703
1704 /* Ensure that no process is syncing the slots. */
1705 if (!SlotSyncCtx->syncing)
1706 break;
1707
1709 }
1710
1712
1714}
struct Latch * MyLatch
Definition: globals.c:63
void ResetLatch(Latch *latch)
Definition: latch.c:374
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:1610
#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 1772 of file slotsync.c.

1773{
1774 Size size = SlotSyncShmemSize();
1775 bool found;
1776
1778 ShmemInitStruct("Slot Sync Data", size, &found);
1779
1780 if (!found)
1781 {
1782 memset(SlotSyncCtx, 0, size);
1785 }
1786}
size_t Size
Definition: c.h:613
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:389
Size SlotSyncShmemSize(void)
Definition: slotsync.c:1763
#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 1763 of file slotsync.c.

1764{
1765 return sizeof(SlotSyncCtxStruct);
1766}
struct SlotSyncCtxStruct SlotSyncCtxStruct

Referenced by CalculateShmemSize(), and SlotSyncShmemInit().

◆ SlotSyncWorkerCanRestart()

bool SlotSyncWorkerCanRestart ( void  )

Definition at line 1729 of file slotsync.c.

1730{
1731 time_t curtime = time(NULL);
1732
1733 /*
1734 * If first time through, or time somehow went backwards, always update
1735 * last_start_time to match the current clock and allow worker start.
1736 * Otherwise allow it only once enough time has elapsed.
1737 */
1738 if (SlotSyncCtx->last_start_time == 0 ||
1739 curtime < SlotSyncCtx->last_start_time ||
1741 {
1742 SlotSyncCtx->last_start_time = curtime;
1743 return true;
1744 }
1745 return false;
1746}
#define SLOTSYNC_RESTART_INTERVAL_SEC
Definition: slotsync.c:119
time_t last_start_time
Definition: slotsync.c:99

References SlotSyncCtxStruct::last_start_time, SLOTSYNC_RESTART_INTERVAL_SEC, and SlotSyncCtx.

Referenced by LaunchMissingBackgroundProcesses().

◆ SyncReplicationSlots()

void SyncReplicationSlots ( WalReceiverConn wrconn)

Definition at line 1829 of file slotsync.c.

1830{
1832 {
1834
1836
1838
1839 /* Cleanup the synced temporary slots */
1841
1842 /* We are done with sync, so reset sync flag */
1844 }
1846}
#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:853
static void slotsync_failure_callback(int code, Datum arg)
Definition: slotsync.c:1792
static void reset_syncing_flag(void)
Definition: slotsync.c:1413

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 1137 of file slotsync.c.

1138{
1139 /*
1140 * Logical slot sync/creation requires wal_level >= logical.
1141 */
1143 {
1144 ereport(elevel,
1145 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1146 errmsg("replication slot synchronization requires \"wal_level\" >= \"logical\""));
1147 return false;
1148 }
1149
1150 /*
1151 * A physical replication slot(primary_slot_name) is required on the
1152 * primary to ensure that the rows needed by the standby are not removed
1153 * after restarting, so that the synchronized slot on the standby will not
1154 * be invalidated.
1155 */
1156 if (PrimarySlotName == NULL || *PrimarySlotName == '\0')
1157 {
1158 ereport(elevel,
1159 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1160 /* translator: %s is a GUC variable name */
1161 errmsg("replication slot synchronization requires \"%s\" to be set", "primary_slot_name"));
1162 return false;
1163 }
1164
1165 /*
1166 * hot_standby_feedback must be enabled to cooperate with the physical
1167 * replication slot, which allows informing the primary about the xmin and
1168 * catalog_xmin values on the standby.
1169 */
1171 {
1172 ereport(elevel,
1173 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1174 /* translator: %s is a GUC variable name */
1175 errmsg("replication slot synchronization requires \"%s\" to be enabled",
1176 "hot_standby_feedback"));
1177 return false;
1178 }
1179
1180 /*
1181 * The primary_conninfo is required to make connection to primary for
1182 * getting slots information.
1183 */
1184 if (PrimaryConnInfo == NULL || *PrimaryConnInfo == '\0')
1185 {
1186 ereport(elevel,
1187 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1188 /* translator: %s is a GUC variable name */
1189 errmsg("replication slot synchronization requires \"%s\" to be set",
1190 "primary_conninfo"));
1191 return false;
1192 }
1193
1194 return true;
1195}
bool hot_standby_feedback
Definition: walreceiver.c:90
int wal_level
Definition: xlog.c:133
@ WAL_LEVEL_LOGICAL
Definition: xlog.h:76
char * PrimarySlotName
Definition: xlogrecovery.c:100

References ereport, errcode(), errmsg(), 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