PostgreSQL Source Code git master
Loading...
Searching...
No Matches
slotsync.c File Reference
#include "postgres.h"
#include <time.h>
#include "access/xlog_internal.h"
#include "access/xlogrecovery.h"
#include "catalog/pg_database.h"
#include "libpq/pqsignal.h"
#include "pgstat.h"
#include "postmaster/interrupt.h"
#include "replication/logical.h"
#include "replication/slotsync.h"
#include "replication/snapbuild.h"
#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/pg_lsn.h"
#include "utils/ps_status.h"
#include "utils/timeout.h"
Include dependency graph for slotsync.c:

Go to the source code of this file.

Data Structures

struct  SlotSyncCtxStruct
 
struct  RemoteSlot
 

Macros

#define MIN_SLOTSYNC_WORKER_NAPTIME_MS   200
 
#define MAX_SLOTSYNC_WORKER_NAPTIME_MS   30000 /* 30s */
 
#define SLOTSYNC_RESTART_INTERVAL_SEC   10
 
#define SLOTSYNC_COLUMN_COUNT   10
 
#define PRIMARY_INFO_OUTPUT_COL_COUNT   2
 

Typedefs

typedef struct SlotSyncCtxStruct SlotSyncCtxStruct
 
typedef struct RemoteSlot RemoteSlot
 

Functions

static void slotsync_failure_callback (int code, Datum arg)
 
static void update_synced_slots_inactive_since (void)
 
static void update_slotsync_skip_stats (SlotSyncSkipReason skip_reason)
 
static bool update_local_synced_slot (RemoteSlot *remote_slot, Oid remote_dbid, bool *found_consistent_snapshot, bool *remote_slot_precedes)
 
static Listget_local_synced_slots (void)
 
static bool local_sync_slot_required (ReplicationSlot *local_slot, List *remote_slots)
 
static void drop_local_obsolete_slots (List *remote_slot_list)
 
static void reserve_wal_for_local_slot (XLogRecPtr restart_lsn)
 
static bool update_and_persist_local_synced_slot (RemoteSlot *remote_slot, Oid remote_dbid, bool *slot_persistence_pending)
 
static bool synchronize_one_slot (RemoteSlot *remote_slot, Oid remote_dbid, bool *slot_persistence_pending)
 
static Listfetch_remote_slots (WalReceiverConn *wrconn, List *slot_names)
 
static bool synchronize_slots (WalReceiverConn *wrconn, List *remote_slot_list, bool *slot_persistence_pending)
 
static void validate_remote_info (WalReceiverConn *wrconn)
 
charCheckAndGetDbnameFromConninfo (void)
 
bool ValidateSlotSyncParams (int elevel)
 
static void slotsync_reread_config (void)
 
static void ProcessSlotSyncInterrupts (void)
 
static void slotsync_worker_disconnect (int code, Datum arg)
 
static void slotsync_worker_onexit (int code, Datum arg)
 
static void wait_for_slot_activity (bool some_slot_updated)
 
static void check_and_set_sync_info (pid_t sync_process_pid)
 
static void reset_syncing_flag (void)
 
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)
 
static Listextract_slot_names (List *remote_slots)
 
void SyncReplicationSlots (WalReceiverConn *wrconn)
 

Variables

static SlotSyncCtxStructSlotSyncCtx = NULL
 
bool sync_replication_slots = false
 
static long sleep_ms = MIN_SLOTSYNC_WORKER_NAPTIME_MS
 
static bool syncing_slots = false
 

Macro Definition Documentation

◆ MAX_SLOTSYNC_WORKER_NAPTIME_MS

#define MAX_SLOTSYNC_WORKER_NAPTIME_MS   30000 /* 30s */

Definition at line 125 of file slotsync.c.

◆ MIN_SLOTSYNC_WORKER_NAPTIME_MS

#define MIN_SLOTSYNC_WORKER_NAPTIME_MS   200

Definition at line 124 of file slotsync.c.

◆ PRIMARY_INFO_OUTPUT_COL_COUNT

#define PRIMARY_INFO_OUTPUT_COL_COUNT   2

◆ SLOTSYNC_COLUMN_COUNT

#define SLOTSYNC_COLUMN_COUNT   10

◆ SLOTSYNC_RESTART_INTERVAL_SEC

#define SLOTSYNC_RESTART_INTERVAL_SEC   10

Definition at line 130 of file slotsync.c.

Typedef Documentation

◆ RemoteSlot

◆ SlotSyncCtxStruct

Function Documentation

◆ check_and_set_sync_info()

static void check_and_set_sync_info ( pid_t  sync_process_pid)
static

Definition at line 1481 of file slotsync.c.

1482{
1484
1485 if (SlotSyncCtx->syncing)
1486 {
1488 ereport(ERROR,
1490 errmsg("cannot synchronize replication slots concurrently"));
1491 }
1492
1493 /* The pid must not be already assigned in SlotSyncCtx */
1495
1496 SlotSyncCtx->syncing = true;
1497
1498 /*
1499 * Advertise the required PID so that the startup process can kill the
1500 * slot sync process on promotion.
1501 */
1503
1505
1506 syncing_slots = true;
1507}
#define Assert(condition)
Definition c.h:883
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
#define InvalidPid
Definition miscadmin.h:32
static int fb(int x)
static SlotSyncCtxStruct * SlotSyncCtx
Definition slotsync.c:114
static bool syncing_slots
Definition slotsync.c:137
#define SpinLockRelease(lock)
Definition spin.h:61
#define SpinLockAcquire(lock)
Definition spin.h:59

References Assert, ereport, errcode(), errmsg(), ERROR, fb(), InvalidPid, SlotSyncCtxStruct::mutex, SlotSyncCtxStruct::pid, SlotSyncCtx, SpinLockAcquire, SpinLockRelease, SlotSyncCtxStruct::syncing, and syncing_slots.

Referenced by ReplSlotSyncWorkerMain(), and SyncReplicationSlots().

◆ CheckAndGetDbnameFromConninfo()

char * CheckAndGetDbnameFromConninfo ( void  )

Definition at line 1176 of file slotsync.c.

1177{
1178 char *dbname;
1179
1180 /*
1181 * The slot synchronization needs a database connection for walrcv_exec to
1182 * work.
1183 */
1185 if (dbname == NULL)
1186 ereport(ERROR,
1188
1189 /*
1190 * translator: first %s is a connection option; second %s is a GUC
1191 * variable name
1192 */
1193 errmsg("replication slot synchronization requires \"%s\" to be specified in \"%s\"",
1194 "dbname", "primary_conninfo"));
1195 return dbname;
1196}
char * dbname
Definition streamutil.c:49
#define walrcv_get_dbname_from_conninfo(conninfo)
char * PrimaryConnInfo

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

Referenced by pg_sync_replication_slots(), and ReplSlotSyncWorkerMain().

◆ drop_local_obsolete_slots()

static void drop_local_obsolete_slots ( List remote_slot_list)
static

Definition at line 486 of file slotsync.c.

487{
489
491 {
492 /* Drop the local slot if it is not required to be retained. */
494 {
495 bool synced_slot;
496
497 /*
498 * Use shared lock to prevent a conflict with
499 * ReplicationSlotsDropDBSlots(), trying to drop the same slot
500 * during a drop-database operation.
501 */
503 0, AccessShareLock);
504
505 /*
506 * In the small window between getting the slot to drop and
507 * locking the database, there is a possibility of a parallel
508 * database drop by the startup process and the creation of a new
509 * slot by the user. This new user-created slot may end up using
510 * the same shared memory as that of 'local_slot'. Thus check if
511 * local_slot is still the synced one before performing actual
512 * drop.
513 */
515 synced_slot = local_slot->in_use && local_slot->data.synced;
517
518 if (synced_slot)
519 {
520 ReplicationSlotAcquire(NameStr(local_slot->data.name), true, false);
522 }
523
525 0, AccessShareLock);
526
527 ereport(LOG,
528 errmsg("dropped replication slot \"%s\" of database with OID %u",
529 NameStr(local_slot->data.name),
530 local_slot->data.database));
531 }
532 }
533}
#define NameStr(name)
Definition c.h:775
#define LOG
Definition elog.h:31
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition lmgr.c:1088
void UnlockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition lmgr.c:1148
#define AccessShareLock
Definition lockdefs.h:36
#define foreach_ptr(type, var, lst)
Definition pg_list.h:469
void ReplicationSlotAcquire(const char *name, bool nowait, bool error_if_invalid)
Definition slot.c:620
void ReplicationSlotDropAcquired(void)
Definition slot.c:1031
static List * get_local_synced_slots(void)
Definition slotsync.c:402
static bool local_sync_slot_required(ReplicationSlot *local_slot, List *remote_slots)
Definition slotsync.c:433
Definition pg_list.h:54

References AccessShareLock, ereport, errmsg(), fb(), foreach_ptr, get_local_synced_slots(), local_sync_slot_required(), LockSharedObject(), LOG, NameStr, ReplicationSlotAcquire(), ReplicationSlotDropAcquired(), SpinLockAcquire, SpinLockRelease, and UnlockSharedObject().

Referenced by synchronize_slots().

◆ extract_slot_names()

static List * extract_slot_names ( List remote_slots)
static

Definition at line 1956 of file slotsync.c.

1957{
1958 List *slot_names = NIL;
1959
1961 {
1962 char *slot_name;
1963
1964 slot_name = pstrdup(remote_slot->name);
1965 slot_names = lappend(slot_names, slot_name);
1966 }
1967
1968 return slot_names;
1969}
List * lappend(List *list, void *datum)
Definition list.c:339
char * pstrdup(const char *in)
Definition mcxt.c:1781
#define NIL
Definition pg_list.h:68

References fb(), foreach_ptr, lappend(), NIL, and pstrdup().

Referenced by SyncReplicationSlots().

◆ fetch_remote_slots()

static List * fetch_remote_slots ( WalReceiverConn wrconn,
List slot_names 
)
static

Definition at line 920 of file slotsync.c.

921{
922#define SLOTSYNC_COLUMN_COUNT 10
925
926 WalRcvExecResult *res;
927 TupleTableSlot *tupslot;
929 StringInfoData query;
930
931 initStringInfo(&query);
933 "SELECT slot_name, plugin, confirmed_flush_lsn,"
934 " restart_lsn, catalog_xmin, two_phase,"
935 " two_phase_at, failover,"
936 " database, invalidation_reason"
937 " FROM pg_catalog.pg_replication_slots"
938 " WHERE failover and NOT temporary");
939
940 if (slot_names != NIL)
941 {
942 bool first_slot = true;
943
944 /*
945 * Construct the query to fetch only the specified slots
946 */
947 appendStringInfoString(&query, " AND slot_name IN (");
948
949 foreach_ptr(char, slot_name, slot_names)
950 {
951 if (!first_slot)
952 appendStringInfoString(&query, ", ");
953
954 appendStringInfo(&query, "%s", quote_literal_cstr(slot_name));
955 first_slot = false;
956 }
957 appendStringInfoChar(&query, ')');
958 }
959
960 /* Execute the query */
962 pfree(query.data);
963 if (res->status != WALRCV_OK_TUPLES)
965 errmsg("could not fetch failover logical slots info from the primary server: %s",
966 res->err));
967
969 while (tuplestore_gettupleslot(res->tuplestore, true, false, tupslot))
970 {
971 bool isnull;
973 Datum d;
974 int col = 0;
975
977 &isnull));
978 Assert(!isnull);
979
980 remote_slot->plugin = TextDatumGetCString(slot_getattr(tupslot, ++col,
981 &isnull));
982 Assert(!isnull);
983
984 /*
985 * It is possible to get null values for LSN and Xmin if slot is
986 * invalidated on the primary server, so handle accordingly.
987 */
988 d = slot_getattr(tupslot, ++col, &isnull);
989 remote_slot->confirmed_lsn = isnull ? InvalidXLogRecPtr :
990 DatumGetLSN(d);
991
992 d = slot_getattr(tupslot, ++col, &isnull);
993 remote_slot->restart_lsn = isnull ? InvalidXLogRecPtr : DatumGetLSN(d);
994
995 d = slot_getattr(tupslot, ++col, &isnull);
996 remote_slot->catalog_xmin = isnull ? InvalidTransactionId :
998
999 remote_slot->two_phase = DatumGetBool(slot_getattr(tupslot, ++col,
1000 &isnull));
1001 Assert(!isnull);
1002
1003 d = slot_getattr(tupslot, ++col, &isnull);
1004 remote_slot->two_phase_at = isnull ? InvalidXLogRecPtr : DatumGetLSN(d);
1005
1006 remote_slot->failover = DatumGetBool(slot_getattr(tupslot, ++col,
1007 &isnull));
1008 Assert(!isnull);
1009
1010 remote_slot->database = TextDatumGetCString(slot_getattr(tupslot,
1011 ++col, &isnull));
1012 Assert(!isnull);
1013
1014 d = slot_getattr(tupslot, ++col, &isnull);
1015 remote_slot->invalidated = isnull ? RS_INVAL_NONE :
1017
1018 /* Sanity check */
1020
1021 /*
1022 * If restart_lsn, confirmed_lsn or catalog_xmin is invalid but the
1023 * slot is valid, that means we have fetched the remote_slot in its
1024 * RS_EPHEMERAL state. In such a case, don't sync it; we can always
1025 * sync it in the next sync cycle when the remote_slot is persisted
1026 * and has valid lsn(s) and xmin values.
1027 *
1028 * XXX: In future, if we plan to expose 'slot->data.persistency' in
1029 * pg_replication_slots view, then we can avoid fetching RS_EPHEMERAL
1030 * slots in the first place.
1031 */
1032 if ((!XLogRecPtrIsValid(remote_slot->restart_lsn) ||
1033 !XLogRecPtrIsValid(remote_slot->confirmed_lsn) ||
1034 !TransactionIdIsValid(remote_slot->catalog_xmin)) &&
1035 remote_slot->invalidated == RS_INVAL_NONE)
1037 else
1038 /* Create list of remote slots */
1040
1041 ExecClearTuple(tupslot);
1042 }
1043
1045
1046 return remote_slot_list;
1047}
#define TextDatumGetCString(d)
Definition builtins.h:98
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
const TupleTableSlotOps TTSOpsMinimalTuple
Definition execTuples.c:86
#define palloc0_object(type)
Definition fe_memutils.h:75
void pfree(void *pointer)
Definition mcxt.c:1616
static XLogRecPtr DatumGetLSN(Datum X)
Definition pg_lsn.h:25
static bool DatumGetBool(Datum X)
Definition postgres.h:100
uint64_t Datum
Definition postgres.h:70
static TransactionId DatumGetTransactionId(Datum X)
Definition postgres.h:292
unsigned int Oid
char * quote_literal_cstr(const char *rawstr)
Definition quote.c:101
ReplicationSlotInvalidationCause GetSlotInvalidationCause(const char *cause_name)
Definition slot.c:2906
@ RS_INVAL_NONE
Definition slot.h:60
#define SLOTSYNC_COLUMN_COUNT
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230
void appendStringInfoChar(StringInfo str, char ch)
Definition stringinfo.c:242
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
Tuplestorestate * tuplestore
TupleDesc tupledesc
WalRcvExecStatus status
#define InvalidTransactionId
Definition transam.h:31
#define TransactionIdIsValid(xid)
Definition transam.h:41
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition tuptable.h:398
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition tuptable.h:457
static WalReceiverConn * wrconn
Definition walreceiver.c:94
@ WALRCV_OK_TUPLES
static void walrcv_clear_result(WalRcvExecResult *walres)
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
#define XLogRecPtrIsValid(r)
Definition xlogdefs.h:29
#define InvalidXLogRecPtr
Definition xlogdefs.h:28

References appendStringInfo(), appendStringInfoChar(), appendStringInfoString(), Assert, StringInfoData::data, DatumGetBool(), DatumGetLSN(), DatumGetTransactionId(), ereport, WalRcvExecResult::err, errmsg(), ERROR, ExecClearTuple(), fb(), foreach_ptr, GetSlotInvalidationCause(), initStringInfo(), InvalidTransactionId, InvalidXLogRecPtr, lappend(), MakeSingleTupleTableSlot(), NIL, palloc0_object, pfree(), quote_literal_cstr(), RS_INVAL_NONE, slot_getattr(), SLOTSYNC_COLUMN_COUNT, WalRcvExecResult::status, TextDatumGetCString, TransactionIdIsValid, TTSOpsMinimalTuple, WalRcvExecResult::tupledesc, WalRcvExecResult::tuplestore, tuplestore_gettupleslot(), walrcv_clear_result(), walrcv_exec, WALRCV_OK_TUPLES, wrconn, and XLogRecPtrIsValid.

Referenced by ReplSlotSyncWorkerMain(), and SyncReplicationSlots().

◆ get_local_synced_slots()

static List * get_local_synced_slots ( void  )
static

Definition at line 402 of file slotsync.c.

403{
405
407
408 for (int i = 0; i < max_replication_slots; i++)
409 {
411
412 /* Check if it is a synchronized slot */
413 if (s->in_use && s->data.synced)
414 {
417 }
418 }
419
421
422 return local_slots;
423}
int i
Definition isn.c:77
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1176
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1793
@ LW_SHARED
Definition lwlock.h:113
int max_replication_slots
Definition slot.c:151
ReplicationSlotCtlData * ReplicationSlotCtl
Definition slot.c:145
#define SlotIsLogical(slot)
Definition slot.h:285
ReplicationSlot replication_slots[1]
Definition slot.h:296
bool in_use
Definition slot.h:186
ReplicationSlotPersistentData data
Definition slot.h:210

References Assert, ReplicationSlot::data, fb(), i, ReplicationSlot::in_use, lappend(), LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, NIL, ReplicationSlotCtlData::replication_slots, ReplicationSlotCtl, SlotIsLogical, and ReplicationSlotPersistentData::synced.

Referenced by drop_local_obsolete_slots().

◆ IsSyncingReplicationSlots()

bool IsSyncingReplicationSlots ( void  )

Definition at line 1882 of file slotsync.c.

1883{
1884 return syncing_slots;
1885}

References syncing_slots.

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

◆ local_sync_slot_required()

static bool local_sync_slot_required ( ReplicationSlot local_slot,
List remote_slots 
)
static

Definition at line 433 of file slotsync.c.

434{
435 bool remote_exists = false;
436 bool locally_invalidated = false;
437
439 {
440 if (strcmp(remote_slot->name, NameStr(local_slot->data.name)) == 0)
441 {
442 remote_exists = true;
443
444 /*
445 * If remote slot is not invalidated but local slot is marked as
446 * invalidated, then set locally_invalidated flag.
447 */
450 (remote_slot->invalidated == RS_INVAL_NONE) &&
451 (local_slot->data.invalidated != RS_INVAL_NONE);
453
454 break;
455 }
456 }
457
459}

References fb(), foreach_ptr, NameStr, RS_INVAL_NONE, SpinLockAcquire, and SpinLockRelease.

Referenced by drop_local_obsolete_slots().

◆ ProcessSlotSyncInterrupts()

static void ProcessSlotSyncInterrupts ( void  )
static

Definition at line 1352 of file slotsync.c.

1353{
1355
1357 {
1359 {
1360 ereport(LOG,
1361 errmsg("replication slot synchronization worker will stop because promotion is triggered"));
1362
1363 proc_exit(0);
1364 }
1365 else
1366 {
1367 /*
1368 * For the backend executing SQL function
1369 * pg_sync_replication_slots().
1370 */
1371 ereport(ERROR,
1373 errmsg("replication slot synchronization will stop because promotion is triggered"));
1374 }
1375 }
1376
1379}
volatile sig_atomic_t ConfigReloadPending
Definition interrupt.c:27
void proc_exit(int code)
Definition ipc.c:105
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
#define AmLogicalSlotSyncWorkerProcess()
Definition miscadmin.h:386
static void slotsync_reread_config(void)
Definition slotsync.c:1272

References AmLogicalSlotSyncWorkerProcess, CHECK_FOR_INTERRUPTS, ConfigReloadPending, ereport, errcode(), errmsg(), ERROR, fb(), LOG, proc_exit(), slotsync_reread_config(), SlotSyncCtx, and SlotSyncCtxStruct::stopSignaled.

Referenced by ReplSlotSyncWorkerMain(), and SyncReplicationSlots().

◆ ReplSlotSyncWorkerMain()

void ReplSlotSyncWorkerMain ( const void startup_data,
size_t  startup_data_len 
)

Definition at line 1533 of file slotsync.c.

1534{
1536 char *dbname;
1537 char *err;
1540
1542
1544
1546
1548
1549 /*
1550 * Create a per-backend PGPROC struct in shared memory. We must do this
1551 * before we access any shared memory.
1552 */
1553 InitProcess();
1554
1555 /*
1556 * Early initialization.
1557 */
1558 BaseInit();
1559
1561
1562 /*
1563 * If an exception is encountered, processing resumes here.
1564 *
1565 * We just need to clean up, report the error, and go away.
1566 *
1567 * If we do not have this handling here, then since this worker process
1568 * operates at the bottom of the exception stack, ERRORs turn into FATALs.
1569 * Therefore, we create our own exception handler to catch ERRORs.
1570 */
1571 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
1572 {
1573 /* since not using PG_TRY, must reset error stack by hand */
1575
1576 /* Prevents interrupts while cleaning up */
1578
1579 /* Report the error to the server log */
1581
1582 /*
1583 * We can now go away. Note that because we called InitProcess, a
1584 * callback was registered to do ProcKill, which will clean up
1585 * necessary state.
1586 */
1587 proc_exit(0);
1588 }
1589
1590 /* We can now handle ereport(ERROR) */
1592
1593 /* Setup signal handling */
1602
1604
1605 ereport(LOG, errmsg("slot sync worker started"));
1606
1607 /* Register it as soon as SlotSyncCtx->pid is initialized. */
1609
1610 /*
1611 * Establishes SIGALRM handler and initialize timeout module. It is needed
1612 * by InitPostgres to register different timeouts.
1613 */
1615
1616 /* Load the libpq-specific functions */
1617 load_file("libpqwalreceiver", false);
1618
1619 /*
1620 * Unblock signals (they were blocked when the postmaster forked us)
1621 */
1623
1624 /*
1625 * Set always-secure search path, so malicious users can't redirect user
1626 * code (e.g. operators).
1627 *
1628 * It's not strictly necessary since we won't be scanning or writing to
1629 * any user table locally, but it's good to retain it here for added
1630 * precaution.
1631 */
1632 SetConfigOption("search_path", "", PGC_SUSET, PGC_S_OVERRIDE);
1633
1635
1636 /*
1637 * Connect to the database specified by the user in primary_conninfo. We
1638 * need a database connection for walrcv_exec to work which we use to
1639 * fetch slot information from the remote node. See comments atop
1640 * libpqrcv_exec.
1641 *
1642 * We do not specify a specific user here since the slot sync worker will
1643 * operate as a superuser. This is safe because the slot sync worker does
1644 * not interact with user tables, eliminating the risk of executing
1645 * arbitrary code within triggers.
1646 */
1648
1650
1652 if (cluster_name[0])
1653 appendStringInfo(&app_name, "%s_%s", cluster_name, "slotsync worker");
1654 else
1655 appendStringInfoString(&app_name, "slotsync worker");
1656
1657 /*
1658 * Establish the connection to the primary server for slot
1659 * synchronization.
1660 */
1661 wrconn = walrcv_connect(PrimaryConnInfo, false, false, false,
1662 app_name.data, &err);
1663
1664 if (!wrconn)
1665 ereport(ERROR,
1667 errmsg("synchronization worker \"%s\" could not connect to the primary server: %s",
1668 app_name.data, err));
1669
1670 pfree(app_name.data);
1671
1672 /*
1673 * Register the disconnection callback.
1674 *
1675 * XXX: This can be combined with previous cleanup registration of
1676 * slotsync_worker_onexit() but that will need the connection to be made
1677 * global and we want to avoid introducing global for this purpose.
1678 */
1680
1681 /*
1682 * Using the specified primary server connection, check that we are not a
1683 * cascading standby and slot configured in 'primary_slot_name' exists on
1684 * the primary server.
1685 */
1687
1688 /* Main loop to synchronize slots */
1689 for (;;)
1690 {
1691 bool some_slot_updated = false;
1692 bool started_tx = false;
1694
1696
1697 /*
1698 * The syscache access in fetch_remote_slots() needs a transaction
1699 * env.
1700 */
1701 if (!IsTransactionState())
1702 {
1704 started_tx = true;
1705 }
1706
1710
1711 if (started_tx)
1713
1715 }
1716
1717 /*
1718 * The slot sync worker can't get here because it will only stop when it
1719 * receives a stop request from the startup process, or when there is an
1720 * error.
1721 */
1722 Assert(false);
1723}
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
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
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition interrupt.c:61
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:344
void list_free_deep(List *list)
Definition list.c:1560
@ 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:547
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:352
#define InvalidOid
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:710
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition procsignal.c:677
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:1387
char * CheckAndGetDbnameFromConninfo(void)
Definition slotsync.c:1176
static void ProcessSlotSyncInterrupts(void)
Definition slotsync.c:1352
static void wait_for_slot_activity(bool some_slot_updated)
Definition slotsync.c:1446
static void slotsync_worker_onexit(int code, Datum arg)
Definition slotsync.c:1400
static void validate_remote_info(WalReceiverConn *wrconn)
Definition slotsync.c:1098
static void check_and_set_sync_info(pid_t sync_process_pid)
Definition slotsync.c:1481
static List * fetch_remote_slots(WalReceiverConn *wrconn, List *slot_names)
Definition slotsync.c:920
static bool synchronize_slots(WalReceiverConn *wrconn, List *remote_slot_list, bool *slot_persistence_pending)
Definition slotsync.c:1062
void InitProcess(void)
Definition proc.c:395
void InitializeTimeouts(void)
Definition timeout.c:470
#define walrcv_connect(conninfo, replication, logical, must_use_password, appname, err)
#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
bool IsTransactionState(void)
Definition xact.c:388
void StartTransactionCommand(void)
Definition xact.c:3080
void CommitTransactionCommand(void)
Definition xact.c:3178

References appendStringInfo(), appendStringInfoString(), Assert, B_SLOTSYNC_WORKER, BaseInit(), before_shmem_exit(), check_and_set_sync_info(), CheckAndGetDbnameFromConninfo(), cluster_name, CommitTransactionCommand(), dbname, die, EmitErrorReport(), ereport, err(), errcode(), errmsg(), ERROR, error_context_stack, fb(), fetch_remote_slots(), FloatExceptionHandler(), GetProcessingMode, HOLD_INTERRUPTS, init_ps_display(), InitializeTimeouts(), InitPostgres(), InitProcess(), InitProcessing, initStringInfo(), InvalidOid, IsTransactionState(), list_free_deep(), load_file(), LOG, MyBackendType, MyProcPid, NIL, 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, StartTransactionCommand(), StatementCancelHandler(), synchronize_slots(), UnBlockSig, validate_remote_info(), wait_for_slot_activity(), walrcv_connect, and wrconn.

◆ reserve_wal_for_local_slot()

static void reserve_wal_for_local_slot ( XLogRecPtr  restart_lsn)
static

Definition at line 543 of file slotsync.c.

544{
546 XLogSegNo segno;
548
549 Assert(slot != NULL);
551
552 while (true)
553 {
554 SpinLockAcquire(&slot->mutex);
555 slot->data.restart_lsn = restart_lsn;
556 SpinLockRelease(&slot->mutex);
557
558 /* Prevent WAL removal as fast as possible */
560
562
563 /*
564 * Find the oldest existing WAL segment file.
565 *
566 * Normally, we can determine it by using the last removed segment
567 * number. However, if no WAL segment files have been removed by a
568 * checkpoint since startup, we need to search for the oldest segment
569 * file from the current timeline existing in XLOGDIR.
570 *
571 * XXX: Currently, we are searching for the oldest segment in the
572 * current timeline as there is less chance of the slot's restart_lsn
573 * from being some prior timeline, and even if it happens, in the
574 * worst case, we will wait to sync till the slot's restart_lsn moved
575 * to the current timeline.
576 */
578
579 if (oldest_segno == 1)
580 {
582
585 }
586
587 elog(DEBUG1, "segno: " UINT64_FORMAT " of purposed restart_lsn for the synced slot, oldest_segno: " UINT64_FORMAT " available",
588 segno, oldest_segno);
589
590 /*
591 * If all required WAL is still there, great, otherwise retry. The
592 * slot should prevent further removal of WAL, unless there's a
593 * concurrent ReplicationSlotsComputeRequiredLSN() after we've written
594 * the new restart_lsn above, so normally we should never need to loop
595 * more than twice.
596 */
597 if (segno >= oldest_segno)
598 break;
599
600 /* Retry using the location of the oldest wal segment */
602 }
603}
#define UINT64_FORMAT
Definition c.h:575
#define DEBUG1
Definition elog.h:30
#define elog(elevel,...)
Definition elog.h:226
ReplicationSlot * MyReplicationSlot
Definition slot.c:148
void ReplicationSlotsComputeRequiredLSN(void)
Definition slot.c:1297
slock_t mutex
Definition slot.h:183
XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
XLogSegNo XLogGetLastRemovedSegno(void)
Definition xlog.c:3796
int wal_segment_size
Definition xlog.c:146
XLogSegNo XLogGetOldestSegno(TimeLineID tli)
Definition xlog.c:3812
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
uint32 TimeLineID
Definition xlogdefs.h:63
uint64 XLogSegNo
Definition xlogdefs.h:52

References Assert, ReplicationSlot::data, DEBUG1, elog, fb(), GetWalRcvFlushRecPtr(), ReplicationSlot::mutex, MyReplicationSlot, ReplicationSlotsComputeRequiredLSN(), ReplicationSlotPersistentData::restart_lsn, SpinLockAcquire, SpinLockRelease, UINT64_FORMAT, wal_segment_size, XLByteToSeg, XLogGetLastRemovedSegno(), XLogGetOldestSegno(), XLogRecPtrIsValid, and XLogSegNoOffsetToRecPtr.

Referenced by synchronize_one_slot().

◆ reset_syncing_flag()

◆ ShutDownSlotSync()

void ShutDownSlotSync ( void  )

Definition at line 1784 of file slotsync.c.

1785{
1787
1789
1790 SlotSyncCtx->stopSignaled = true;
1791
1792 /*
1793 * Return if neither the slot sync worker is running nor the function
1794 * pg_sync_replication_slots() is executing.
1795 */
1796 if (!SlotSyncCtx->syncing)
1797 {
1800 return;
1801 }
1802
1804
1806
1807 /*
1808 * Signal process doing slotsync, if any. The process will stop upon
1809 * detecting that the stopSignaled flag is set to true.
1810 */
1813
1814 /* Wait for slot sync to end */
1815 for (;;)
1816 {
1817 int rc;
1818
1819 /* Wait a bit, we don't expect to have to wait long */
1820 rc = WaitLatch(MyLatch,
1823
1824 if (rc & WL_LATCH_SET)
1825 {
1828 }
1829
1831
1832 /* Ensure that no process is syncing the slots. */
1833 if (!SlotSyncCtx->syncing)
1834 break;
1835
1837 }
1838
1840
1842}
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
static void update_synced_slots_inactive_since(void)
Definition slotsync.c:1732
#define WL_TIMEOUT
#define WL_EXIT_ON_PM_DEATH
#define WL_LATCH_SET
#define kill(pid, sig)
Definition win32_port.h:490

References CHECK_FOR_INTERRUPTS, fb(), 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().

◆ slotsync_failure_callback()

static void slotsync_failure_callback ( int  code,
Datum  arg 
)
static

Definition at line 1920 of file slotsync.c.

1921{
1923
1924 /*
1925 * We need to do slots cleanup here just like WalSndErrorCleanup() does.
1926 *
1927 * The startup process during promotion invokes ShutDownSlotSync() which
1928 * waits for slot sync to finish and it does that by checking the
1929 * 'syncing' flag. Thus the SQL function must be done with slots' release
1930 * and cleanup to avoid any dangling temporary slots or active slots
1931 * before it marks itself as finished syncing.
1932 */
1933
1934 /* Make sure active replication slots are released */
1935 if (MyReplicationSlot != NULL)
1937
1938 /* Also cleanup the synced temporary slots. */
1940
1941 /*
1942 * The set syncing_slots indicates that the process errored out without
1943 * resetting the flag. So, we need to clean up shared memory and reset the
1944 * flag here.
1945 */
1946 if (syncing_slots)
1948
1950}
void * arg
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
void ReplicationSlotRelease(void)
Definition slot.c:758
void ReplicationSlotCleanup(bool synced_only)
Definition slot.c:857
static void reset_syncing_flag(void)
Definition slotsync.c:1513
#define walrcv_disconnect(conn)

References arg, DatumGetPointer(), fb(), MyReplicationSlot, ReplicationSlotCleanup(), ReplicationSlotRelease(), reset_syncing_flag(), syncing_slots, walrcv_disconnect, and wrconn.

Referenced by SyncReplicationSlots().

◆ slotsync_reread_config()

static void slotsync_reread_config ( void  )
static

Definition at line 1272 of file slotsync.c.

1273{
1278 bool conninfo_changed;
1281 bool parameter_changed = false;
1282
1285
1286 ConfigReloadPending = false;
1288
1293
1295 {
1297 {
1298 ereport(LOG,
1299 /* translator: %s is a GUC variable name */
1300 errmsg("replication slot synchronization worker will stop because \"%s\" is disabled",
1301 "sync_replication_slots"));
1302
1303 proc_exit(0);
1304 }
1305
1306 parameter_changed = true;
1307 }
1308 else
1309 {
1310 if (conninfo_changed ||
1313 {
1314
1316 {
1317 ereport(LOG,
1318 errmsg("replication slot synchronization worker will restart because of a parameter change"));
1319
1320 /*
1321 * Reset the last-start time for this worker so that the
1322 * postmaster can restart it without waiting for
1323 * SLOTSYNC_RESTART_INTERVAL_SEC.
1324 */
1326
1327 proc_exit(0);
1328 }
1329
1330 parameter_changed = true;
1331 }
1332 }
1333
1334 /*
1335 * If we have reached here with a parameter change, we must be running in
1336 * SQL function, emit error in such a case.
1337 */
1339 {
1341 ereport(ERROR,
1343 errmsg("replication slot synchronization will stop because of a parameter change"));
1344 }
1345
1346}
void ProcessConfigFile(GucContext context)
Definition guc-file.l:120
@ PGC_SIGHUP
Definition guc.h:75
bool sync_replication_slots
Definition slotsync.c:117
time_t last_start_time
Definition slotsync.c:110
bool hot_standby_feedback
Definition walreceiver.c:91
char * PrimarySlotName

References AmLogicalSlotSyncWorkerProcess, Assert, ConfigReloadPending, ereport, errcode(), errmsg(), ERROR, fb(), hot_standby_feedback, SlotSyncCtxStruct::last_start_time, LOG, pfree(), PGC_SIGHUP, PrimaryConnInfo, PrimarySlotName, proc_exit(), ProcessConfigFile(), pstrdup(), SlotSyncCtx, and sync_replication_slots.

Referenced by ProcessSlotSyncInterrupts().

◆ slotsync_worker_disconnect()

static void slotsync_worker_disconnect ( int  code,
Datum  arg 
)
static

Definition at line 1387 of file slotsync.c.

References arg, DatumGetPointer(), walrcv_disconnect, and wrconn.

Referenced by ReplSlotSyncWorkerMain().

◆ slotsync_worker_onexit()

static void slotsync_worker_onexit ( int  code,
Datum  arg 
)
static

Definition at line 1400 of file slotsync.c.

1401{
1402 /*
1403 * We need to do slots cleanup here just like WalSndErrorCleanup() does.
1404 *
1405 * The startup process during promotion invokes ShutDownSlotSync() which
1406 * waits for slot sync to finish and it does that by checking the
1407 * 'syncing' flag. Thus the slot sync worker must be done with slots'
1408 * release and cleanup to avoid any dangling temporary slots or active
1409 * slots before it marks itself as finished syncing.
1410 */
1411
1412 /* Make sure active replication slots are released */
1413 if (MyReplicationSlot != NULL)
1415
1416 /* Also cleanup the temporary slots. */
1418
1420
1422
1423 /*
1424 * If syncing_slots is true, it indicates that the process errored out
1425 * without resetting the flag. So, we need to clean up shared memory and
1426 * reset the flag here.
1427 */
1428 if (syncing_slots)
1429 {
1430 SlotSyncCtx->syncing = false;
1431 syncing_slots = false;
1432 }
1433
1435}

References fb(), InvalidPid, SlotSyncCtxStruct::mutex, MyReplicationSlot, SlotSyncCtxStruct::pid, ReplicationSlotCleanup(), ReplicationSlotRelease(), SlotSyncCtx, SpinLockAcquire, SpinLockRelease, SlotSyncCtxStruct::syncing, and syncing_slots.

Referenced by ReplSlotSyncWorkerMain().

◆ SlotSyncShmemInit()

void SlotSyncShmemInit ( void  )

Definition at line 1900 of file slotsync.c.

1901{
1902 Size size = SlotSyncShmemSize();
1903 bool found;
1904
1906 ShmemInitStruct("Slot Sync Data", size, &found);
1907
1908 if (!found)
1909 {
1910 memset(SlotSyncCtx, 0, size);
1913 }
1914}
size_t Size
Definition c.h:629
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition shmem.c:389
Size SlotSyncShmemSize(void)
Definition slotsync.c:1891
#define SpinLockInit(lock)
Definition spin.h:57

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

Referenced by CreateOrAttachShmemStructs().

◆ SlotSyncShmemSize()

Size SlotSyncShmemSize ( void  )

Definition at line 1891 of file slotsync.c.

1892{
1893 return sizeof(SlotSyncCtxStruct);
1894}

Referenced by CalculateShmemSize(), and SlotSyncShmemInit().

◆ SlotSyncWorkerCanRestart()

bool SlotSyncWorkerCanRestart ( void  )

Definition at line 1857 of file slotsync.c.

1858{
1859 time_t curtime = time(NULL);
1860
1861 /*
1862 * If first time through, or time somehow went backwards, always update
1863 * last_start_time to match the current clock and allow worker start.
1864 * Otherwise allow it only once enough time has elapsed.
1865 */
1866 if (SlotSyncCtx->last_start_time == 0 ||
1867 curtime < SlotSyncCtx->last_start_time ||
1869 {
1871 return true;
1872 }
1873 return false;
1874}
#define SLOTSYNC_RESTART_INTERVAL_SEC
Definition slotsync.c:130

References fb(), SlotSyncCtxStruct::last_start_time, SLOTSYNC_RESTART_INTERVAL_SEC, and SlotSyncCtx.

Referenced by LaunchMissingBackgroundProcesses().

◆ synchronize_one_slot()

static bool synchronize_one_slot ( RemoteSlot remote_slot,
Oid  remote_dbid,
bool slot_persistence_pending 
)
static

Definition at line 696 of file slotsync.c.

698{
699 ReplicationSlot *slot;
701 bool slot_updated = false;
702
703 /* Search for the named slot */
704 if ((slot = SearchNamedReplicationSlot(remote_slot->name, true)))
705 {
706 bool synced;
707
708 SpinLockAcquire(&slot->mutex);
709 synced = slot->data.synced;
710 SpinLockRelease(&slot->mutex);
711
712 /* User-created slot with the same name exists, raise ERROR. */
713 if (!synced)
716 errmsg("exiting from slot synchronization because same"
717 " name slot \"%s\" already exists on the standby",
718 remote_slot->name));
719
720 /*
721 * The slot has been synchronized before.
722 *
723 * It is important to acquire the slot here before checking
724 * invalidation. If we don't acquire the slot first, there could be a
725 * race condition that the local slot could be invalidated just after
726 * checking the 'invalidated' flag here and we could end up
727 * overwriting 'invalidated' flag to remote_slot's value. See
728 * InvalidatePossiblyObsoleteSlot() where it invalidates slot directly
729 * if the slot is not acquired by other processes.
730 *
731 * XXX: If it ever turns out that slot acquire/release is costly for
732 * cases when none of the slot properties is changed then we can do a
733 * pre-check to ensure that at least one of the slot properties is
734 * changed before acquiring the slot.
735 */
736 ReplicationSlotAcquire(remote_slot->name, true, false);
737
738 Assert(slot == MyReplicationSlot);
739
740 /*
741 * Copy the invalidation cause from remote only if local slot is not
742 * invalidated locally, we don't want to overwrite existing one.
743 */
744 if (slot->data.invalidated == RS_INVAL_NONE &&
745 remote_slot->invalidated != RS_INVAL_NONE)
746 {
747 SpinLockAcquire(&slot->mutex);
748 slot->data.invalidated = remote_slot->invalidated;
749 SpinLockRelease(&slot->mutex);
750
751 /* Make sure the invalidated state persists across server restart */
754
755 slot_updated = true;
756 }
757
758 /* Skip the sync of an invalidated slot */
759 if (slot->data.invalidated != RS_INVAL_NONE)
760 {
762
764 return slot_updated;
765 }
766
767 /*
768 * Make sure that concerned WAL is received and flushed before syncing
769 * slot to target lsn received from the primary server.
770 *
771 * Report statistics only after the slot has been acquired, ensuring
772 * it cannot be dropped during the reporting process.
773 */
774 if (remote_slot->confirmed_lsn > latestFlushPtr)
775 {
777
778 /*
779 * Can get here only if GUC 'synchronized_standby_slots' on the
780 * primary server was not configured correctly.
781 */
784 errmsg("skipping slot synchronization because the received slot sync"
785 " LSN %X/%08X for slot \"%s\" is ahead of the standby position %X/%08X",
786 LSN_FORMAT_ARGS(remote_slot->confirmed_lsn),
789
791
792 return slot_updated;
793 }
794
795 /* Slot not ready yet, let's attempt to make it sync-ready now. */
796 if (slot->data.persistency == RS_TEMPORARY)
797 {
801 }
802
803 /* Slot ready for sync, so sync it. */
804 else
805 {
806 /*
807 * Sanity check: As long as the invalidations are handled
808 * appropriately as above, this should never happen.
809 *
810 * We don't need to check restart_lsn here. See the comments in
811 * update_local_synced_slot() for details.
812 */
813 if (remote_slot->confirmed_lsn < slot->data.confirmed_flush)
815 errmsg_internal("cannot synchronize local slot \"%s\"",
816 remote_slot->name),
817 errdetail_internal("Local slot's start streaming location LSN(%X/%08X) is ahead of remote slot's LSN(%X/%08X).",
819 LSN_FORMAT_ARGS(remote_slot->confirmed_lsn)));
820
822 NULL, NULL);
823 }
824 }
825 /* Otherwise create the slot first. */
826 else
827 {
830
831 /* Skip creating the local slot if remote_slot is invalidated already */
832 if (remote_slot->invalidated != RS_INVAL_NONE)
833 return false;
834
835 /*
836 * We create temporary slots instead of ephemeral slots here because
837 * we want the slots to survive after releasing them. This is done to
838 * avoid dropping and re-creating the slots in each synchronization
839 * cycle if the restart_lsn or catalog_xmin of the remote slot has not
840 * caught up.
841 */
843 remote_slot->two_phase,
844 remote_slot->failover,
845 true);
846
847 /* For shorter lines. */
848 slot = MyReplicationSlot;
849
850 /* Avoid expensive operations while holding a spinlock. */
852
853 SpinLockAcquire(&slot->mutex);
854 slot->data.database = remote_dbid;
855 slot->data.plugin = plugin_name;
856 SpinLockRelease(&slot->mutex);
857
859
863 SpinLockAcquire(&slot->mutex);
866 SpinLockRelease(&slot->mutex);
870
871 /*
872 * Make sure that concerned WAL is received and flushed before syncing
873 * slot to target lsn received from the primary server.
874 *
875 * Report statistics only after the slot has been acquired, ensuring
876 * it cannot be dropped during the reporting process.
877 */
878 if (remote_slot->confirmed_lsn > latestFlushPtr)
879 {
881
882 /*
883 * Can get here only if GUC 'synchronized_standby_slots' on the
884 * primary server was not configured correctly.
885 */
888 errmsg("skipping slot synchronization because the received slot sync"
889 " LSN %X/%08X for slot \"%s\" is ahead of the standby position %X/%08X",
890 LSN_FORMAT_ARGS(remote_slot->confirmed_lsn),
893
895
896 return false;
897 }
898
901
902 slot_updated = true;
903 }
904
906
907 return slot_updated;
908}
uint32 TransactionId
Definition c.h:676
int errmsg_internal(const char *fmt,...)
Definition elog.c:1170
int errdetail_internal(const char *fmt,...)
Definition elog.c:1243
@ LW_EXCLUSIVE
Definition lwlock.h:112
void namestrcpy(Name name, const char *str)
Definition name.c:233
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition procarray.c:2909
void ReplicationSlotCreate(const char *name, bool db_specific, ReplicationSlotPersistency persistency, bool two_phase, bool failover, bool synced)
Definition slot.c:378
void ReplicationSlotMarkDirty(void)
Definition slot.c:1173
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition slot.c:1215
void ReplicationSlotSave(void)
Definition slot.c:1155
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition slot.c:540
@ RS_TEMPORARY
Definition slot.h:47
@ SS_SKIP_WAL_NOT_FLUSHED
Definition slot.h:83
@ SS_SKIP_INVALID
Definition slot.h:89
static void reserve_wal_for_local_slot(XLogRecPtr restart_lsn)
Definition slotsync.c:543
static void update_slotsync_skip_stats(SlotSyncSkipReason skip_reason)
Definition slotsync.c:167
static bool update_and_persist_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid, bool *slot_persistence_pending)
Definition slotsync.c:617
static bool update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid, bool *found_consistent_snapshot, bool *remote_slot_precedes)
Definition slotsync.c:206
TransactionId catalog_xmin
Definition slot.h:122
ReplicationSlotPersistency persistency
Definition slot.h:106
ReplicationSlotInvalidationCause invalidated
Definition slot.h:128
TransactionId effective_catalog_xmin
Definition slot.h:207
Definition c.h:770
const char * name
XLogRecPtr GetStandbyFlushRecPtr(TimeLineID *tli)
Definition walsender.c:3638
#define LSN_FORMAT_ARGS(lsn)
Definition xlogdefs.h:47
uint64 XLogRecPtr
Definition xlogdefs.h:21

References AmLogicalSlotSyncWorkerProcess, Assert, ReplicationSlotPersistentData::catalog_xmin, ReplicationSlotPersistentData::confirmed_flush, ReplicationSlot::data, ReplicationSlotPersistentData::database, ReplicationSlot::effective_catalog_xmin, ereport, errcode(), errdetail_internal(), errmsg(), errmsg_internal(), ERROR, fb(), GetOldestSafeDecodingTransactionId(), GetStandbyFlushRecPtr(), ReplicationSlotPersistentData::invalidated, InvalidTransactionId, LOG, LSN_FORMAT_ARGS, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), ReplicationSlot::mutex, MyReplicationSlot, namestrcpy(), ReplicationSlotPersistentData::persistency, ReplicationSlotPersistentData::plugin, ReplicationSlotAcquire(), ReplicationSlotCreate(), ReplicationSlotMarkDirty(), ReplicationSlotRelease(), ReplicationSlotSave(), ReplicationSlotsComputeRequiredXmin(), reserve_wal_for_local_slot(), RS_INVAL_NONE, RS_TEMPORARY, SearchNamedReplicationSlot(), SpinLockAcquire, SpinLockRelease, SS_SKIP_INVALID, SS_SKIP_WAL_NOT_FLUSHED, ReplicationSlotPersistentData::synced, update_and_persist_local_synced_slot(), update_local_synced_slot(), and update_slotsync_skip_stats().

Referenced by synchronize_slots().

◆ synchronize_slots()

static bool synchronize_slots ( WalReceiverConn wrconn,
List remote_slot_list,
bool slot_persistence_pending 
)
static

Definition at line 1062 of file slotsync.c.

1064{
1065 bool some_slot_updated = false;
1066
1067 /* Drop local slots that no longer need to be synced. */
1069
1070 /* Now sync the slots locally */
1072 {
1073 Oid remote_dbid = get_database_oid(remote_slot->database, false);
1074
1075 /*
1076 * Use shared lock to prevent a conflict with
1077 * ReplicationSlotsDropDBSlots(), trying to drop the same slot during
1078 * a drop-database operation.
1079 */
1081
1084
1086 }
1087
1088 return some_slot_updated;
1089}
Oid get_database_oid(const char *dbname, bool missing_ok)
static void drop_local_obsolete_slots(List *remote_slot_list)
Definition slotsync.c:486
static bool synchronize_one_slot(RemoteSlot *remote_slot, Oid remote_dbid, bool *slot_persistence_pending)
Definition slotsync.c:696

References AccessShareLock, drop_local_obsolete_slots(), fb(), foreach_ptr, get_database_oid(), LockSharedObject(), synchronize_one_slot(), and UnlockSharedObject().

Referenced by ReplSlotSyncWorkerMain(), and SyncReplicationSlots().

◆ SyncReplicationSlots()

void SyncReplicationSlots ( WalReceiverConn wrconn)

Definition at line 1982 of file slotsync.c.

1983{
1985 {
1987 List *slot_names = NIL; /* List of slot names to track */
1988
1990
1991 /* Check for interrupts and config changes */
1993
1995
1996 /* Retry until all the slots are sync-ready */
1997 for (;;)
1998 {
1999 bool slot_persistence_pending = false;
2000 bool some_slot_updated = false;
2001
2002 /* Check for interrupts and config changes */
2004
2005 /* We must be in a valid transaction state */
2007
2008 /*
2009 * Fetch remote slot info for the given slot_names. If slot_names
2010 * is NIL, fetch all failover-enabled slots. Note that we reuse
2011 * slot_names from the first iteration; re-fetching all failover
2012 * slots each time could cause an endless loop. Instead of
2013 * reprocessing only the pending slots in each iteration, it's
2014 * better to process all the slots received in the first
2015 * iteration. This ensures that by the time we're done, all slots
2016 * reflect the latest values.
2017 */
2018 remote_slots = fetch_remote_slots(wrconn, slot_names);
2019
2020 /* Attempt to synchronize slots */
2023
2024 /*
2025 * If slot_persistence_pending is true, extract slot names for
2026 * future iterations (only needed if we haven't done it yet)
2027 */
2028 if (slot_names == NIL && slot_persistence_pending)
2029 slot_names = extract_slot_names(remote_slots);
2030
2031 /* Free the current remote_slots list */
2033
2034 /* Done if all slots are persisted i.e are sync-ready */
2036 break;
2037
2038 /* wait before retrying again */
2040 }
2041
2042 if (slot_names)
2043 list_free_deep(slot_names);
2044
2045 /* Cleanup the synced temporary slots */
2047
2048 /* We are done with sync, so reset sync flag */
2050 }
2052}
#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
static void slotsync_failure_callback(int code, Datum arg)
Definition slotsync.c:1920
static List * extract_slot_names(List *remote_slots)
Definition slotsync.c:1956

References Assert, check_and_set_sync_info(), extract_slot_names(), fb(), fetch_remote_slots(), IsTransactionState(), list_free_deep(), MyProcPid, NIL, PG_END_ENSURE_ERROR_CLEANUP, PG_ENSURE_ERROR_CLEANUP, PointerGetDatum(), ProcessSlotSyncInterrupts(), ReplicationSlotCleanup(), reset_syncing_flag(), slotsync_failure_callback(), synchronize_slots(), validate_remote_info(), wait_for_slot_activity(), and wrconn.

Referenced by pg_sync_replication_slots().

◆ update_and_persist_local_synced_slot()

static bool update_and_persist_local_synced_slot ( RemoteSlot remote_slot,
Oid  remote_dbid,
bool slot_persistence_pending 
)
static

Definition at line 617 of file slotsync.c.

619{
621 bool found_consistent_snapshot = false;
622 bool remote_slot_precedes = false;
623
624 /* Slotsync skip stats are handled in function update_local_synced_slot() */
628
629 /*
630 * Check if the primary server has caught up. Refer to the comment atop
631 * the file for details on this check.
632 */
634 {
635 /*
636 * The remote slot didn't catch up to locally reserved position.
637 *
638 * We do not drop the slot because the restart_lsn can be ahead of the
639 * current location when recreating the slot in the next cycle. It may
640 * take more time to create such a slot. Therefore, we keep this slot
641 * and attempt the synchronization in the next cycle.
642 *
643 * We also update the slot_persistence_pending parameter, so the SQL
644 * function can retry.
645 */
648
649 return false;
650 }
651
652 /*
653 * Don't persist the slot if it cannot reach the consistent point from the
654 * restart_lsn. See comments atop this file.
655 */
657 {
658 ereport(LOG,
659 errmsg("could not synchronize replication slot \"%s\"", remote_slot->name),
660 errdetail("Synchronization could lead to data loss, because the standby could not build a consistent snapshot to decode WALs at LSN %X/%08X.",
662
663 /* Set this, so that SQL function can retry */
666
667 return false;
668 }
669
671
672 ereport(LOG,
673 errmsg("newly created replication slot \"%s\" is sync-ready now",
674 remote_slot->name));
675
676 return true;
677}
int errdetail(const char *fmt,...)
Definition elog.c:1216
void ReplicationSlotPersist(void)
Definition slot.c:1190

References ReplicationSlot::data, ereport, errdetail(), errmsg(), fb(), LOG, LSN_FORMAT_ARGS, MyReplicationSlot, ReplicationSlotPersist(), ReplicationSlotPersistentData::restart_lsn, and update_local_synced_slot().

Referenced by synchronize_one_slot().

◆ update_local_synced_slot()

static bool update_local_synced_slot ( RemoteSlot remote_slot,
Oid  remote_dbid,
bool found_consistent_snapshot,
bool remote_slot_precedes 
)
static

Definition at line 206 of file slotsync.c.

209{
211 bool updated_xmin_or_lsn = false;
212 bool updated_config = false;
214
216
219
221 *remote_slot_precedes = false;
222
223 /*
224 * Don't overwrite if we already have a newer catalog_xmin and
225 * restart_lsn.
226 */
227 if (remote_slot->restart_lsn < slot->data.restart_lsn ||
229 slot->data.catalog_xmin))
230 {
231 /* Update slot sync skip stats */
233
234 /*
235 * This can happen in following situations:
236 *
237 * If the slot is temporary, it means either the initial WAL location
238 * reserved for the local slot is ahead of the remote slot's
239 * restart_lsn or the initial xmin_horizon computed for the local slot
240 * is ahead of the remote slot.
241 *
242 * If the slot is persistent, both restart_lsn and catalog_xmin of the
243 * synced slot could still be ahead of the remote slot. Since we use
244 * slot advance functionality to keep snapbuild/slot updated, it is
245 * possible that the restart_lsn and catalog_xmin are advanced to a
246 * later position than it has on the primary. This can happen when
247 * slot advancing machinery finds running xacts record after reaching
248 * the consistent state at a later point than the primary where it
249 * serializes the snapshot and updates the restart_lsn.
250 *
251 * We LOG the message if the slot is temporary as it can help the user
252 * to understand why the slot is not sync-ready. In the case of a
253 * persistent slot, it would be a more common case and won't directly
254 * impact the users, so we used DEBUG1 level to log the message.
255 */
257 errmsg("could not synchronize replication slot \"%s\"",
259 errdetail("Synchronization could lead to data loss, because the remote slot needs WAL at LSN %X/%08X and catalog xmin %u, but the standby has LSN %X/%08X and catalog xmin %u.",
260 LSN_FORMAT_ARGS(remote_slot->restart_lsn),
261 remote_slot->catalog_xmin,
262 LSN_FORMAT_ARGS(slot->data.restart_lsn),
263 slot->data.catalog_xmin));
264
266 *remote_slot_precedes = true;
267
268 /*
269 * Skip updating the configuration. This is required to avoid syncing
270 * two_phase_at without syncing confirmed_lsn. Otherwise, the prepared
271 * transaction between old confirmed_lsn and two_phase_at will
272 * unexpectedly get decoded and sent to the downstream after
273 * promotion. See comments in ReorderBufferFinishPrepared.
274 */
275 return false;
276 }
277
278 /*
279 * Attempt to sync LSNs and xmins only if remote slot is ahead of local
280 * slot.
281 */
282 if (remote_slot->confirmed_lsn > slot->data.confirmed_flush ||
283 remote_slot->restart_lsn > slot->data.restart_lsn ||
284 TransactionIdFollows(remote_slot->catalog_xmin,
285 slot->data.catalog_xmin))
286 {
287 /*
288 * We can't directly copy the remote slot's LSN or xmin unless there
289 * exists a consistent snapshot at that point. Otherwise, after
290 * promotion, the slots may not reach a consistent point before the
291 * confirmed_flush_lsn which can lead to a data loss. To avoid data
292 * loss, we let slot machinery advance the slot which ensures that
293 * snapbuilder/slot statuses are updated properly.
294 */
295 if (SnapBuildSnapshotExists(remote_slot->restart_lsn))
296 {
297 /*
298 * Update the slot info directly if there is a serialized snapshot
299 * at the restart_lsn, as the slot can quickly reach consistency
300 * at restart_lsn by restoring the snapshot.
301 */
302 SpinLockAcquire(&slot->mutex);
303 slot->data.restart_lsn = remote_slot->restart_lsn;
304 slot->data.confirmed_flush = remote_slot->confirmed_lsn;
305 slot->data.catalog_xmin = remote_slot->catalog_xmin;
306 SpinLockRelease(&slot->mutex);
307
310 }
311 else
312 {
315
316 /* Sanity check */
317 if (slot->data.confirmed_flush != remote_slot->confirmed_lsn)
319 errmsg_internal("synchronized confirmed_flush for slot \"%s\" differs from remote slot",
320 remote_slot->name),
321 errdetail_internal("Remote slot has LSN %X/%08X but local slot has LSN %X/%08X.",
322 LSN_FORMAT_ARGS(remote_slot->confirmed_lsn),
324
325 /*
326 * If we can't reach a consistent snapshot, the slot won't be
327 * persisted. See update_and_persist_local_synced_slot().
328 */
331 }
332
333 updated_xmin_or_lsn = true;
334 }
335
336 /* Update slot sync skip stats */
338
339 if (remote_dbid != slot->data.database ||
340 remote_slot->two_phase != slot->data.two_phase ||
341 remote_slot->failover != slot->data.failover ||
342 strcmp(remote_slot->plugin, NameStr(slot->data.plugin)) != 0 ||
343 remote_slot->two_phase_at != slot->data.two_phase_at)
344 {
346
347 /* Avoid expensive operations while holding a spinlock. */
349
350 SpinLockAcquire(&slot->mutex);
351 slot->data.plugin = plugin_name;
352 slot->data.database = remote_dbid;
353 slot->data.two_phase = remote_slot->two_phase;
354 slot->data.two_phase_at = remote_slot->two_phase_at;
355 slot->data.failover = remote_slot->failover;
356 SpinLockRelease(&slot->mutex);
357
358 updated_config = true;
359
360 /*
361 * Ensure that there is no risk of sending prepared transactions
362 * unexpectedly after the promotion.
363 */
365 }
366
367 /*
368 * We have to write the changed xmin to disk *before* we change the
369 * in-memory value, otherwise after a crash we wouldn't know that some
370 * catalog tuples might have been removed already.
371 */
373 {
376 }
377
378 /*
379 * Now the new xmin is safely on disk, we can let the global value
380 * advance. We do not take ProcArrayLock or similar since we only advance
381 * xmin here and there's not much harm done by a concurrent computation
382 * missing that.
383 */
385 {
386 SpinLockAcquire(&slot->mutex);
387 slot->effective_catalog_xmin = remote_slot->catalog_xmin;
388 SpinLockRelease(&slot->mutex);
389
392 }
393
395}
XLogRecPtr LogicalSlotAdvanceAndCheckSnapState(XLogRecPtr moveto, bool *found_consistent_snapshot)
Definition logical.c:2076
const void * data
SlotSyncSkipReason
Definition slot.h:81
@ SS_SKIP_NO_CONSISTENT_SNAPSHOT
Definition slot.h:87
@ SS_SKIP_NONE
Definition slot.h:82
@ SS_SKIP_WAL_OR_ROWS_REMOVED
Definition slot.h:85
bool SnapBuildSnapshotExists(XLogRecPtr lsn)
Definition snapbuild.c:2057
static bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition transam.h:297
static bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition transam.h:263

References Assert, ReplicationSlotPersistentData::catalog_xmin, ReplicationSlotPersistentData::confirmed_flush, ReplicationSlot::data, ReplicationSlotPersistentData::database, DEBUG1, ReplicationSlot::effective_catalog_xmin, ereport, errdetail(), errdetail_internal(), errmsg(), errmsg_internal(), ERROR, ReplicationSlotPersistentData::failover, fb(), ReplicationSlotPersistentData::invalidated, LOG, LogicalSlotAdvanceAndCheckSnapState(), LSN_FORMAT_ARGS, ReplicationSlot::mutex, MyReplicationSlot, NameStr, namestrcpy(), ReplicationSlotPersistentData::persistency, ReplicationSlotPersistentData::plugin, ReplicationSlotMarkDirty(), ReplicationSlotSave(), ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), ReplicationSlotPersistentData::restart_lsn, RS_INVAL_NONE, RS_TEMPORARY, SnapBuildSnapshotExists(), SpinLockAcquire, SpinLockRelease, SS_SKIP_NO_CONSISTENT_SNAPSHOT, SS_SKIP_NONE, SS_SKIP_WAL_OR_ROWS_REMOVED, TransactionIdFollows(), TransactionIdPrecedes(), ReplicationSlotPersistentData::two_phase, ReplicationSlotPersistentData::two_phase_at, and update_slotsync_skip_stats().

Referenced by synchronize_one_slot(), and update_and_persist_local_synced_slot().

◆ update_slotsync_skip_stats()

static void update_slotsync_skip_stats ( SlotSyncSkipReason  skip_reason)
static

Definition at line 167 of file slotsync.c.

168{
169 ReplicationSlot *slot;
170
172
173 slot = MyReplicationSlot;
174
175 /*
176 * Update the slot sync related stats in pg_stat_replication_slots when a
177 * slot sync is skipped
178 */
181
182 /* Update the slot sync skip reason */
184 {
185 SpinLockAcquire(&slot->mutex);
187 SpinLockRelease(&slot->mutex);
188 }
189}
void pgstat_report_replslotsync(ReplicationSlot *slot)
SlotSyncSkipReason slotsync_skip_reason
Definition slot.h:281

References Assert, fb(), ReplicationSlot::mutex, MyReplicationSlot, pgstat_report_replslotsync(), ReplicationSlot::slotsync_skip_reason, SpinLockAcquire, SpinLockRelease, and SS_SKIP_NONE.

Referenced by synchronize_one_slot(), and update_local_synced_slot().

◆ update_synced_slots_inactive_since()

static void update_synced_slots_inactive_since ( void  )
static

Definition at line 1732 of file slotsync.c.

1733{
1734 TimestampTz now = 0;
1735
1736 /*
1737 * We need to update inactive_since only when we are promoting standby to
1738 * correctly interpret the inactive_since if the standby gets promoted
1739 * without a restart. We don't want the slots to appear inactive for a
1740 * long time after promotion if they haven't been synchronized recently.
1741 * Whoever acquires the slot, i.e., makes the slot active, will reset it.
1742 */
1743 if (!StandbyMode)
1744 return;
1745
1746 /* The slot sync worker or the SQL function mustn't be running by now */
1748
1750
1751 for (int i = 0; i < max_replication_slots; i++)
1752 {
1754
1755 /* Check if it is a synchronized slot */
1756 if (s->in_use && s->data.synced)
1757 {
1759
1760 /* The slot must not be acquired by any process */
1761 Assert(s->active_pid == 0);
1762
1763 /* Use the same inactive_since time for all the slots. */
1764 if (now == 0)
1766
1768 }
1769 }
1770
1772}
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1645
Datum now(PG_FUNCTION_ARGS)
Definition timestamp.c:1609
int64 TimestampTz
Definition timestamp.h:39
static void ReplicationSlotSetInactiveSince(ReplicationSlot *s, TimestampTz ts, bool acquire_lock)
Definition slot.h:303
pid_t active_pid
Definition slot.h:189
bool StandbyMode

References ReplicationSlot::active_pid, Assert, ReplicationSlot::data, fb(), GetCurrentTimestamp(), i, ReplicationSlot::in_use, InvalidPid, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_replication_slots, now(), SlotSyncCtxStruct::pid, ReplicationSlotCtlData::replication_slots, ReplicationSlotCtl, ReplicationSlotSetInactiveSince(), SlotIsLogical, SlotSyncCtx, StandbyMode, ReplicationSlotPersistentData::synced, and SlotSyncCtxStruct::syncing.

Referenced by ShutDownSlotSync().

◆ validate_remote_info()

static void validate_remote_info ( WalReceiverConn wrconn)
static

Definition at line 1098 of file slotsync.c.

1099{
1100#define PRIMARY_INFO_OUTPUT_COL_COUNT 2
1101 WalRcvExecResult *res;
1103 StringInfoData cmd;
1104 bool isnull;
1105 TupleTableSlot *tupslot;
1106 bool remote_in_recovery;
1107 bool primary_slot_valid;
1108 bool started_tx = false;
1109
1110 initStringInfo(&cmd);
1111 appendStringInfo(&cmd,
1112 "SELECT pg_is_in_recovery(), count(*) = 1"
1113 " FROM pg_catalog.pg_replication_slots"
1114 " WHERE slot_type='physical' AND slot_name=%s",
1116
1117 /* The syscache access in walrcv_exec() needs a transaction env. */
1118 if (!IsTransactionState())
1119 {
1121 started_tx = true;
1122 }
1123
1125 pfree(cmd.data);
1126
1127 if (res->status != WALRCV_OK_TUPLES)
1128 ereport(ERROR,
1129 errmsg("could not fetch primary slot name \"%s\" info from the primary server: %s",
1130 PrimarySlotName, res->err),
1131 errhint("Check if \"primary_slot_name\" is configured correctly."));
1132
1134 if (!tuplestore_gettupleslot(res->tuplestore, true, false, tupslot))
1135 elog(ERROR,
1136 "failed to fetch tuple for the primary server slot specified by \"primary_slot_name\"");
1137
1138 remote_in_recovery = DatumGetBool(slot_getattr(tupslot, 1, &isnull));
1139 Assert(!isnull);
1140
1141 /*
1142 * Slot sync is currently not supported on a cascading standby. This is
1143 * because if we allow it, the primary server needs to wait for all the
1144 * cascading standbys, otherwise, logical subscribers can still be ahead
1145 * of one of the cascading standbys which we plan to promote. Thus, to
1146 * avoid this additional complexity, we restrict it for the time being.
1147 */
1149 ereport(ERROR,
1151 errmsg("cannot synchronize replication slots from a standby server"));
1152
1153 primary_slot_valid = DatumGetBool(slot_getattr(tupslot, 2, &isnull));
1154 Assert(!isnull);
1155
1156 if (!primary_slot_valid)
1157 ereport(ERROR,
1159 /* translator: second %s is a GUC variable name */
1160 errmsg("replication slot \"%s\" specified by \"%s\" does not exist on primary server",
1161 PrimarySlotName, "primary_slot_name"));
1162
1163 ExecClearTuple(tupslot);
1165
1166 if (started_tx)
1168}
int errhint(const char *fmt,...)
Definition elog.c:1330
#define PRIMARY_INFO_OUTPUT_COL_COUNT

References appendStringInfo(), Assert, CommitTransactionCommand(), StringInfoData::data, DatumGetBool(), elog, ereport, WalRcvExecResult::err, errcode(), errhint(), errmsg(), ERROR, ExecClearTuple(), fb(), initStringInfo(), IsTransactionState(), MakeSingleTupleTableSlot(), pfree(), PRIMARY_INFO_OUTPUT_COL_COUNT, PrimarySlotName, quote_literal_cstr(), slot_getattr(), StartTransactionCommand(), WalRcvExecResult::status, TTSOpsMinimalTuple, WalRcvExecResult::tupledesc, WalRcvExecResult::tuplestore, tuplestore_gettupleslot(), walrcv_clear_result(), walrcv_exec, WALRCV_OK_TUPLES, and wrconn.

Referenced by ReplSlotSyncWorkerMain(), and SyncReplicationSlots().

◆ ValidateSlotSyncParams()

bool ValidateSlotSyncParams ( int  elevel)

Definition at line 1203 of file slotsync.c.

1204{
1205 /*
1206 * Logical slot sync/creation requires logical decoding to be enabled.
1207 */
1209 {
1210 ereport(elevel,
1212 errmsg("replication slot synchronization requires \"effective_wal_level\" >= \"logical\" on the primary"),
1213 errhint("To enable logical decoding on primary, set \"wal_level\" >= \"logical\" or create at least one logical slot when \"wal_level\" = \"replica\"."));
1214
1215 return false;
1216 }
1217
1218 /*
1219 * A physical replication slot(primary_slot_name) is required on the
1220 * primary to ensure that the rows needed by the standby are not removed
1221 * after restarting, so that the synchronized slot on the standby will not
1222 * be invalidated.
1223 */
1224 if (PrimarySlotName == NULL || *PrimarySlotName == '\0')
1225 {
1226 ereport(elevel,
1228 /* translator: %s is a GUC variable name */
1229 errmsg("replication slot synchronization requires \"%s\" to be set", "primary_slot_name"));
1230 return false;
1231 }
1232
1233 /*
1234 * hot_standby_feedback must be enabled to cooperate with the physical
1235 * replication slot, which allows informing the primary about the xmin and
1236 * catalog_xmin values on the standby.
1237 */
1239 {
1240 ereport(elevel,
1242 /* translator: %s is a GUC variable name */
1243 errmsg("replication slot synchronization requires \"%s\" to be enabled",
1244 "hot_standby_feedback"));
1245 return false;
1246 }
1247
1248 /*
1249 * The primary_conninfo is required to make connection to primary for
1250 * getting slots information.
1251 */
1252 if (PrimaryConnInfo == NULL || *PrimaryConnInfo == '\0')
1253 {
1254 ereport(elevel,
1256 /* translator: %s is a GUC variable name */
1257 errmsg("replication slot synchronization requires \"%s\" to be set",
1258 "primary_conninfo"));
1259 return false;
1260 }
1261
1262 return true;
1263}
bool IsLogicalDecodingEnabled(void)
Definition logicalctl.c:204

References ereport, errcode(), errhint(), errmsg(), fb(), hot_standby_feedback, IsLogicalDecodingEnabled(), PrimaryConnInfo, and PrimarySlotName.

Referenced by LaunchMissingBackgroundProcesses(), and pg_sync_replication_slots().

◆ wait_for_slot_activity()

static void wait_for_slot_activity ( bool  some_slot_updated)
static

Definition at line 1446 of file slotsync.c.

1447{
1448 int rc;
1449
1450 if (!some_slot_updated)
1451 {
1452 /*
1453 * No slots were updated, so double the sleep time, but not beyond the
1454 * maximum allowable value.
1455 */
1457 }
1458 else
1459 {
1460 /*
1461 * Some slots were updated since the last sleep, so reset the sleep
1462 * time.
1463 */
1465 }
1466
1467 rc = WaitLatch(MyLatch,
1469 sleep_ms,
1471
1472 if (rc & WL_LATCH_SET)
1474}
#define Min(x, y)
Definition c.h:1007
#define MIN_SLOTSYNC_WORKER_NAPTIME_MS
Definition slotsync.c:124
static long sleep_ms
Definition slotsync.c:127
#define MAX_SLOTSYNC_WORKER_NAPTIME_MS
Definition slotsync.c:125

References fb(), MAX_SLOTSYNC_WORKER_NAPTIME_MS, Min, MIN_SLOTSYNC_WORKER_NAPTIME_MS, MyLatch, ResetLatch(), sleep_ms, WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and WL_TIMEOUT.

Referenced by ReplSlotSyncWorkerMain(), and SyncReplicationSlots().

Variable Documentation

◆ sleep_ms

Definition at line 127 of file slotsync.c.

Referenced by do_watch(), and wait_for_slot_activity().

◆ SlotSyncCtx

◆ sync_replication_slots

bool sync_replication_slots = false

◆ syncing_slots