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

1114{
1115 char *dbname;
1116
1117 /*
1118 * The slot synchronization needs a database connection for walrcv_exec to
1119 * work.
1120 */
1122 if (dbname == NULL)
1123 ereport(ERROR,
1124 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1125
1126 /*
1127 * translator: first %s is a connection option; second %s is a GUC
1128 * variable name
1129 */
1130 errmsg("replication slot synchronization requires \"%s\" to be specified in \"%s\"",
1131 "dbname", "primary_conninfo"));
1132 return dbname;
1133}
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 1797 of file slotsync.c.

1798{
1799 return syncing_slots;
1800}
static bool syncing_slots
Definition: slotsync.c:129

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

1466{
1467 WalReceiverConn *wrconn = NULL;
1468 char *dbname;
1469 char *err;
1470 sigjmp_buf local_sigjmp_buf;
1471 StringInfoData app_name;
1472
1473 Assert(startup_data_len == 0);
1474
1476
1477 init_ps_display(NULL);
1478
1480
1481 /*
1482 * Create a per-backend PGPROC struct in shared memory. We must do this
1483 * before we access any shared memory.
1484 */
1485 InitProcess();
1486
1487 /*
1488 * Early initialization.
1489 */
1490 BaseInit();
1491
1492 Assert(SlotSyncCtx != NULL);
1493
1494 /*
1495 * If an exception is encountered, processing resumes here.
1496 *
1497 * We just need to clean up, report the error, and go away.
1498 *
1499 * If we do not have this handling here, then since this worker process
1500 * operates at the bottom of the exception stack, ERRORs turn into FATALs.
1501 * Therefore, we create our own exception handler to catch ERRORs.
1502 */
1503 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
1504 {
1505 /* since not using PG_TRY, must reset error stack by hand */
1506 error_context_stack = NULL;
1507
1508 /* Prevents interrupts while cleaning up */
1510
1511 /* Report the error to the server log */
1513
1514 /*
1515 * We can now go away. Note that because we called InitProcess, a
1516 * callback was registered to do ProcKill, which will clean up
1517 * necessary state.
1518 */
1519 proc_exit(0);
1520 }
1521
1522 /* We can now handle ereport(ERROR) */
1523 PG_exception_stack = &local_sigjmp_buf;
1524
1525 /* Setup signal handling */
1528 pqsignal(SIGTERM, die);
1531 pqsignal(SIGUSR2, SIG_IGN);
1532 pqsignal(SIGPIPE, SIG_IGN);
1533 pqsignal(SIGCHLD, SIG_DFL);
1534
1536
1537 ereport(LOG, errmsg("slot sync worker started"));
1538
1539 /* Register it as soon as SlotSyncCtx->pid is initialized. */
1541
1542 /*
1543 * Establishes SIGALRM handler and initialize timeout module. It is needed
1544 * by InitPostgres to register different timeouts.
1545 */
1547
1548 /* Load the libpq-specific functions */
1549 load_file("libpqwalreceiver", false);
1550
1551 /*
1552 * Unblock signals (they were blocked when the postmaster forked us)
1553 */
1554 sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
1555
1556 /*
1557 * Set always-secure search path, so malicious users can't redirect user
1558 * code (e.g. operators).
1559 *
1560 * It's not strictly necessary since we won't be scanning or writing to
1561 * any user table locally, but it's good to retain it here for added
1562 * precaution.
1563 */
1564 SetConfigOption("search_path", "", PGC_SUSET, PGC_S_OVERRIDE);
1565
1567
1568 /*
1569 * Connect to the database specified by the user in primary_conninfo. We
1570 * need a database connection for walrcv_exec to work which we use to
1571 * fetch slot information from the remote node. See comments atop
1572 * libpqrcv_exec.
1573 *
1574 * We do not specify a specific user here since the slot sync worker will
1575 * operate as a superuser. This is safe because the slot sync worker does
1576 * not interact with user tables, eliminating the risk of executing
1577 * arbitrary code within triggers.
1578 */
1579 InitPostgres(dbname, InvalidOid, NULL, InvalidOid, 0, NULL);
1580
1582
1583 initStringInfo(&app_name);
1584 if (cluster_name[0])
1585 appendStringInfo(&app_name, "%s_%s", cluster_name, "slotsync worker");
1586 else
1587 appendStringInfoString(&app_name, "slotsync worker");
1588
1589 /*
1590 * Establish the connection to the primary server for slot
1591 * synchronization.
1592 */
1593 wrconn = walrcv_connect(PrimaryConnInfo, false, false, false,
1594 app_name.data, &err);
1595
1596 if (!wrconn)
1597 ereport(ERROR,
1598 errcode(ERRCODE_CONNECTION_FAILURE),
1599 errmsg("synchronization worker \"%s\" could not connect to the primary server: %s",
1600 app_name.data, err));
1601
1602 pfree(app_name.data);
1603
1604 /*
1605 * Register the disconnection callback.
1606 *
1607 * XXX: This can be combined with previous cleanup registration of
1608 * slotsync_worker_onexit() but that will need the connection to be made
1609 * global and we want to avoid introducing global for this purpose.
1610 */
1612
1613 /*
1614 * Using the specified primary server connection, check that we are not a
1615 * cascading standby and slot configured in 'primary_slot_name' exists on
1616 * the primary server.
1617 */
1619
1620 /* Main loop to synchronize slots */
1621 for (;;)
1622 {
1623 bool some_slot_updated = false;
1624
1626
1627 some_slot_updated = synchronize_slots(wrconn);
1628
1629 wait_for_slot_activity(some_slot_updated);
1630 }
1631
1632 /*
1633 * The slot sync worker can't get here because it will only stop when it
1634 * receives a stop request from the startup process, or when there is an
1635 * error.
1636 */
1637 Assert(false);
1638}
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 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)
Definition: pg_test_fsync.c:99
#define pqsignal
Definition: port.h:551
void FloatExceptionHandler(SIGNAL_ARGS)
Definition: postgres.c:3079
void StatementCancelHandler(SIGNAL_ARGS)
Definition: postgres.c:3062
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:1322
static SlotSyncCtxStruct * SlotSyncCtx
Definition: slotsync.c:106
char * CheckAndGetDbnameFromConninfo(void)
Definition: slotsync.c:1113
static void ProcessSlotSyncInterrupts(void)
Definition: slotsync.c:1287
static bool synchronize_slots(WalReceiverConn *wrconn)
Definition: slotsync.c:889
static void wait_for_slot_activity(bool some_slot_updated)
Definition: slotsync.c:1381
static void slotsync_worker_onexit(int code, Datum arg)
Definition: slotsync.c:1335
static void validate_remote_info(WalReceiverConn *wrconn)
Definition: slotsync.c:1035
static void check_and_set_sync_info(pid_t sync_process_pid)
Definition: slotsync.c:1416
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(), SIGPIPE, SIGUSR1, SIGUSR2, slotsync_worker_disconnect(), slotsync_worker_onexit(), SlotSyncCtx, StatementCancelHandler(), synchronize_slots(), UnBlockSig, validate_remote_info(), wait_for_slot_activity(), walrcv_connect, and wrconn.

◆ ShutDownSlotSync()

void ShutDownSlotSync ( void  )

Definition at line 1699 of file slotsync.c.

1700{
1701 pid_t sync_process_pid;
1702
1704
1705 SlotSyncCtx->stopSignaled = true;
1706
1707 /*
1708 * Return if neither the slot sync worker is running nor the function
1709 * pg_sync_replication_slots() is executing.
1710 */
1711 if (!SlotSyncCtx->syncing)
1712 {
1715 return;
1716 }
1717
1718 sync_process_pid = SlotSyncCtx->pid;
1719
1721
1722 /*
1723 * Signal process doing slotsync, if any. The process will stop upon
1724 * detecting that the stopSignaled flag is set to true.
1725 */
1726 if (sync_process_pid != InvalidPid)
1727 kill(sync_process_pid, SIGUSR1);
1728
1729 /* Wait for slot sync to end */
1730 for (;;)
1731 {
1732 int rc;
1733
1734 /* Wait a bit, we don't expect to have to wait long */
1735 rc = WaitLatch(MyLatch,
1737 10L, WAIT_EVENT_REPLICATION_SLOTSYNC_SHUTDOWN);
1738
1739 if (rc & WL_LATCH_SET)
1740 {
1743 }
1744
1746
1747 /* Ensure that no process is syncing the slots. */
1748 if (!SlotSyncCtx->syncing)
1749 break;
1750
1752 }
1753
1755
1757}
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:1647
#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:490

References CHECK_FOR_INTERRUPTS, InvalidPid, kill, SlotSyncCtxStruct::mutex, MyLatch, SlotSyncCtxStruct::pid, ResetLatch(), SIGUSR1, 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 1815 of file slotsync.c.

1816{
1817 Size size = SlotSyncShmemSize();
1818 bool found;
1819
1821 ShmemInitStruct("Slot Sync Data", size, &found);
1822
1823 if (!found)
1824 {
1825 memset(SlotSyncCtx, 0, size);
1828 }
1829}
size_t Size
Definition: c.h:624
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:389
Size SlotSyncShmemSize(void)
Definition: slotsync.c:1806
#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 1806 of file slotsync.c.

1807{
1808 return sizeof(SlotSyncCtxStruct);
1809}
struct SlotSyncCtxStruct SlotSyncCtxStruct

Referenced by CalculateShmemSize(), and SlotSyncShmemInit().

◆ SlotSyncWorkerCanRestart()

bool SlotSyncWorkerCanRestart ( void  )

Definition at line 1772 of file slotsync.c.

1773{
1774 time_t curtime = time(NULL);
1775
1776 /*
1777 * If first time through, or time somehow went backwards, always update
1778 * last_start_time to match the current clock and allow worker start.
1779 * Otherwise allow it only once enough time has elapsed.
1780 */
1781 if (SlotSyncCtx->last_start_time == 0 ||
1782 curtime < SlotSyncCtx->last_start_time ||
1784 {
1785 SlotSyncCtx->last_start_time = curtime;
1786 return true;
1787 }
1788 return false;
1789}
#define SLOTSYNC_RESTART_INTERVAL_SEC
Definition: slotsync.c:122
time_t last_start_time
Definition: slotsync.c:102

References SlotSyncCtxStruct::last_start_time, SLOTSYNC_RESTART_INTERVAL_SEC, and SlotSyncCtx.

Referenced by LaunchMissingBackgroundProcesses().

◆ SyncReplicationSlots()

void SyncReplicationSlots ( WalReceiverConn wrconn)

Definition at line 1872 of file slotsync.c.

1873{
1875 {
1877
1878 /* Check for interrupts and config changes */
1880
1882
1884
1885 /* Cleanup the synced temporary slots */
1887
1888 /* We are done with sync, so reset sync flag */
1890 }
1892}
#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:1835
static void reset_syncing_flag(void)
Definition: slotsync.c:1448

References check_and_set_sync_info(), MyProcPid, PG_END_ENSURE_ERROR_CLEANUP, PG_ENSURE_ERROR_CLEANUP, PointerGetDatum(), ProcessSlotSyncInterrupts(), 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 1140 of file slotsync.c.

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