PostgreSQL Source Code git master
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/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)
 
static bool synchronize_one_slot (RemoteSlot *remote_slot, Oid remote_dbid)
 
static bool synchronize_slots (WalReceiverConn *wrconn)
 
static void validate_remote_info (WalReceiverConn *wrconn)
 
char * CheckAndGetDbnameFromConninfo (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 worker_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)
 
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 114 of file slotsync.c.

◆ MIN_SLOTSYNC_WORKER_NAPTIME_MS

#define MIN_SLOTSYNC_WORKER_NAPTIME_MS   200

Definition at line 113 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 119 of file slotsync.c.

Typedef Documentation

◆ RemoteSlot

typedef struct RemoteSlot RemoteSlot

◆ SlotSyncCtxStruct

Function Documentation

◆ check_and_set_sync_info()

static void check_and_set_sync_info ( pid_t  worker_pid)
static

Definition at line 1369 of file slotsync.c.

1370{
1372
1373 /* The worker pid must not be already assigned in SlotSyncCtx */
1374 Assert(worker_pid == InvalidPid || SlotSyncCtx->pid == InvalidPid);
1375
1376 /*
1377 * Emit an error if startup process signaled the slot sync machinery to
1378 * stop. See comments atop SlotSyncCtxStruct.
1379 */
1381 {
1383 ereport(ERROR,
1384 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1385 errmsg("cannot synchronize replication slots when standby promotion is ongoing"));
1386 }
1387
1388 if (SlotSyncCtx->syncing)
1389 {
1391 ereport(ERROR,
1392 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1393 errmsg("cannot synchronize replication slots concurrently"));
1394 }
1395
1396 SlotSyncCtx->syncing = true;
1397
1398 /*
1399 * Advertise the required PID so that the startup process can kill the
1400 * slot sync worker on promotion.
1401 */
1402 SlotSyncCtx->pid = worker_pid;
1403
1405
1406 syncing_slots = true;
1407}
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
Assert(PointerIsAligned(start, uint64))
#define InvalidPid
Definition: miscadmin.h:32
static SlotSyncCtxStruct * SlotSyncCtx
Definition: slotsync.c:103
static bool syncing_slots
Definition: slotsync.c:126
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59

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

Referenced by ReplSlotSyncWorkerMain(), and SyncReplicationSlots().

◆ 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}
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().

◆ drop_local_obsolete_slots()

static void drop_local_obsolete_slots ( List remote_slot_list)
static

Definition at line 475 of file slotsync.c.

476{
477 List *local_slots = get_local_synced_slots();
478
479 foreach_ptr(ReplicationSlot, local_slot, local_slots)
480 {
481 /* Drop the local slot if it is not required to be retained. */
482 if (!local_sync_slot_required(local_slot, remote_slot_list))
483 {
484 bool synced_slot;
485
486 /*
487 * Use shared lock to prevent a conflict with
488 * ReplicationSlotsDropDBSlots(), trying to drop the same slot
489 * during a drop-database operation.
490 */
491 LockSharedObject(DatabaseRelationId, local_slot->data.database,
492 0, AccessShareLock);
493
494 /*
495 * In the small window between getting the slot to drop and
496 * locking the database, there is a possibility of a parallel
497 * database drop by the startup process and the creation of a new
498 * slot by the user. This new user-created slot may end up using
499 * the same shared memory as that of 'local_slot'. Thus check if
500 * local_slot is still the synced one before performing actual
501 * drop.
502 */
503 SpinLockAcquire(&local_slot->mutex);
504 synced_slot = local_slot->in_use && local_slot->data.synced;
505 SpinLockRelease(&local_slot->mutex);
506
507 if (synced_slot)
508 {
509 ReplicationSlotAcquire(NameStr(local_slot->data.name), true, false);
511 }
512
513 UnlockSharedObject(DatabaseRelationId, local_slot->data.database,
514 0, AccessShareLock);
515
516 ereport(LOG,
517 errmsg("dropped replication slot \"%s\" of database with OID %u",
518 NameStr(local_slot->data.name),
519 local_slot->data.database));
520 }
521 }
522}
#define NameStr(name)
Definition: c.h:754
#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:626
void ReplicationSlotDropAcquired(void)
Definition: slot.c:997
static List * get_local_synced_slots(void)
Definition: slotsync.c:391
static bool local_sync_slot_required(ReplicationSlot *local_slot, List *remote_slots)
Definition: slotsync.c:422
Definition: pg_list.h:54

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

Referenced by synchronize_slots().

◆ get_local_synced_slots()

static List * get_local_synced_slots ( void  )
static

Definition at line 391 of file slotsync.c.

392{
393 List *local_slots = NIL;
394
395 LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
396
397 for (int i = 0; i < max_replication_slots; i++)
398 {
400
401 /* Check if it is a synchronized slot */
402 if (s->in_use && s->data.synced)
403 {
405 local_slots = lappend(local_slots, s);
406 }
407 }
408
409 LWLockRelease(ReplicationSlotControlLock);
410
411 return local_slots;
412}
int i
Definition: isn.c:77
List * lappend(List *list, void *datum)
Definition: list.c:339
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1174
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1894
@ LW_SHARED
Definition: lwlock.h:113
#define NIL
Definition: pg_list.h:68
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, 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 1754 of file slotsync.c.

1755{
1756 return syncing_slots;
1757}

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

423{
424 bool remote_exists = false;
425 bool locally_invalidated = false;
426
427 foreach_ptr(RemoteSlot, remote_slot, remote_slots)
428 {
429 if (strcmp(remote_slot->name, NameStr(local_slot->data.name)) == 0)
430 {
431 remote_exists = true;
432
433 /*
434 * If remote slot is not invalidated but local slot is marked as
435 * invalidated, then set locally_invalidated flag.
436 */
437 SpinLockAcquire(&local_slot->mutex);
438 locally_invalidated =
439 (remote_slot->invalidated == RS_INVAL_NONE) &&
440 (local_slot->data.invalidated != RS_INVAL_NONE);
441 SpinLockRelease(&local_slot->mutex);
442
443 break;
444 }
445 }
446
447 return (remote_exists && !locally_invalidated);
448}
@ RS_INVAL_NONE
Definition: slot.h:60
ReplicationSlotInvalidationCause invalidated
Definition: slot.h:128
slock_t mutex
Definition: slot.h:183

References ReplicationSlot::data, foreach_ptr, ReplicationSlotPersistentData::invalidated, ReplicationSlot::mutex, ReplicationSlotPersistentData::name, NameStr, RS_INVAL_NONE, SpinLockAcquire, and SpinLockRelease.

Referenced by drop_local_obsolete_slots().

◆ ProcessSlotSyncInterrupts()

static void ProcessSlotSyncInterrupts ( void  )
static

Definition at line 1253 of file slotsync.c.

1254{
1256
1258 {
1259 ereport(LOG,
1260 errmsg("replication slot synchronization worker is shutting down on receiving SIGINT"));
1261
1262 proc_exit(0);
1263 }
1264
1267}
volatile sig_atomic_t ShutdownRequestPending
Definition: interrupt.c:28
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:27
void proc_exit(int code)
Definition: ipc.c:104
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
static void slotsync_reread_config(void)
Definition: slotsync.c:1204

References CHECK_FOR_INTERRUPTS, ConfigReloadPending, ereport, errmsg(), LOG, proc_exit(), ShutdownRequestPending, and slotsync_reread_config().

Referenced by ReplSlotSyncWorkerMain().

◆ ReplSlotSyncWorkerMain()

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
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 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 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
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.

◆ reserve_wal_for_local_slot()

static void reserve_wal_for_local_slot ( XLogRecPtr  restart_lsn)
static

Definition at line 532 of file slotsync.c.

533{
534 XLogSegNo oldest_segno;
535 XLogSegNo segno;
537
538 Assert(slot != NULL);
540
541 while (true)
542 {
543 SpinLockAcquire(&slot->mutex);
544 slot->data.restart_lsn = restart_lsn;
545 SpinLockRelease(&slot->mutex);
546
547 /* Prevent WAL removal as fast as possible */
549
551
552 /*
553 * Find the oldest existing WAL segment file.
554 *
555 * Normally, we can determine it by using the last removed segment
556 * number. However, if no WAL segment files have been removed by a
557 * checkpoint since startup, we need to search for the oldest segment
558 * file from the current timeline existing in XLOGDIR.
559 *
560 * XXX: Currently, we are searching for the oldest segment in the
561 * current timeline as there is less chance of the slot's restart_lsn
562 * from being some prior timeline, and even if it happens, in the
563 * worst case, we will wait to sync till the slot's restart_lsn moved
564 * to the current timeline.
565 */
566 oldest_segno = XLogGetLastRemovedSegno() + 1;
567
568 if (oldest_segno == 1)
569 {
570 TimeLineID cur_timeline;
571
572 GetWalRcvFlushRecPtr(NULL, &cur_timeline);
573 oldest_segno = XLogGetOldestSegno(cur_timeline);
574 }
575
576 elog(DEBUG1, "segno: " UINT64_FORMAT " of purposed restart_lsn for the synced slot, oldest_segno: " UINT64_FORMAT " available",
577 segno, oldest_segno);
578
579 /*
580 * If all required WAL is still there, great, otherwise retry. The
581 * slot should prevent further removal of WAL, unless there's a
582 * concurrent ReplicationSlotsComputeRequiredLSN() after we've written
583 * the new restart_lsn above, so normally we should never need to loop
584 * more than twice.
585 */
586 if (segno >= oldest_segno)
587 break;
588
589 /* Retry using the location of the oldest wal segment */
590 XLogSegNoOffsetToRecPtr(oldest_segno, 0, wal_segment_size, restart_lsn);
591 }
592}
#define UINT64_FORMAT
Definition: c.h:560
#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:1234
XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
XLogSegNo XLogGetLastRemovedSegno(void)
Definition: xlog.c:3777
int wal_segment_size
Definition: xlog.c:145
XLogSegNo XLogGetOldestSegno(TimeLineID tli)
Definition: xlog.c:3793
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
#define XLogRecPtrIsValid(r)
Definition: xlogdefs.h:29
uint32 TimeLineID
Definition: xlogdefs.h:63
uint64 XLogSegNo
Definition: xlogdefs.h:52

References Assert(), ReplicationSlot::data, DEBUG1, elog, 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()

static void reset_syncing_flag ( void  )
static

◆ 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
static void update_synced_slots_inactive_since(void)
Definition: slotsync.c:1610
#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().

◆ slotsync_failure_callback()

static void slotsync_failure_callback ( int  code,
Datum  arg 
)
static

Definition at line 1792 of file slotsync.c.

1793{
1795
1796 /*
1797 * We need to do slots cleanup here just like WalSndErrorCleanup() does.
1798 *
1799 * The startup process during promotion invokes ShutDownSlotSync() which
1800 * waits for slot sync to finish and it does that by checking the
1801 * 'syncing' flag. Thus the SQL function must be done with slots' release
1802 * and cleanup to avoid any dangling temporary slots or active slots
1803 * before it marks itself as finished syncing.
1804 */
1805
1806 /* Make sure active replication slots are released */
1807 if (MyReplicationSlot != NULL)
1809
1810 /* Also cleanup the synced temporary slots. */
1812
1813 /*
1814 * The set syncing_slots indicates that the process errored out without
1815 * resetting the flag. So, we need to clean up shared memory and reset the
1816 * flag here.
1817 */
1818 if (syncing_slots)
1820
1822}
void * arg
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:322
void ReplicationSlotRelease(void)
Definition: slot.c:764
void ReplicationSlotCleanup(bool synced_only)
Definition: slot.c:853
static void reset_syncing_flag(void)
Definition: slotsync.c:1413
#define walrcv_disconnect(conn)
Definition: walreceiver.h:467

References arg, DatumGetPointer(), 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 1204 of file slotsync.c.

1205{
1206 char *old_primary_conninfo = pstrdup(PrimaryConnInfo);
1207 char *old_primary_slotname = pstrdup(PrimarySlotName);
1208 bool old_sync_replication_slots = sync_replication_slots;
1209 bool old_hot_standby_feedback = hot_standby_feedback;
1210 bool conninfo_changed;
1211 bool primary_slotname_changed;
1212
1214
1215 ConfigReloadPending = false;
1217
1218 conninfo_changed = strcmp(old_primary_conninfo, PrimaryConnInfo) != 0;
1219 primary_slotname_changed = strcmp(old_primary_slotname, PrimarySlotName) != 0;
1220 pfree(old_primary_conninfo);
1221 pfree(old_primary_slotname);
1222
1223 if (old_sync_replication_slots != sync_replication_slots)
1224 {
1225 ereport(LOG,
1226 /* translator: %s is a GUC variable name */
1227 errmsg("replication slot synchronization worker will shut down because \"%s\" is disabled", "sync_replication_slots"));
1228 proc_exit(0);
1229 }
1230
1231 if (conninfo_changed ||
1232 primary_slotname_changed ||
1233 (old_hot_standby_feedback != hot_standby_feedback))
1234 {
1235 ereport(LOG,
1236 errmsg("replication slot synchronization worker will restart because of a parameter change"));
1237
1238 /*
1239 * Reset the last-start time for this worker so that the postmaster
1240 * can restart it without waiting for SLOTSYNC_RESTART_INTERVAL_SEC.
1241 */
1243
1244 proc_exit(0);
1245 }
1246
1247}
void ProcessConfigFile(GucContext context)
Definition: guc-file.l:120
@ PGC_SIGHUP
Definition: guc.h:75
char * pstrdup(const char *in)
Definition: mcxt.c:1759
bool sync_replication_slots
Definition: slotsync.c:106
time_t last_start_time
Definition: slotsync.c:99
bool hot_standby_feedback
Definition: walreceiver.c:90
char * PrimarySlotName
Definition: xlogrecovery.c:100

References Assert(), ConfigReloadPending, ereport, errmsg(), 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 1275 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 1288 of file slotsync.c.

1289{
1290 /*
1291 * We need to do slots cleanup here just like WalSndErrorCleanup() does.
1292 *
1293 * The startup process during promotion invokes ShutDownSlotSync() which
1294 * waits for slot sync to finish and it does that by checking the
1295 * 'syncing' flag. Thus the slot sync worker must be done with slots'
1296 * release and cleanup to avoid any dangling temporary slots or active
1297 * slots before it marks itself as finished syncing.
1298 */
1299
1300 /* Make sure active replication slots are released */
1301 if (MyReplicationSlot != NULL)
1303
1304 /* Also cleanup the temporary slots. */
1306
1308
1310
1311 /*
1312 * If syncing_slots is true, it indicates that the process errored out
1313 * without resetting the flag. So, we need to clean up shared memory and
1314 * reset the flag here.
1315 */
1316 if (syncing_slots)
1317 {
1318 SlotSyncCtx->syncing = false;
1319 syncing_slots = false;
1320 }
1321
1323}

References 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 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

References 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 
)
static

Definition at line 668 of file slotsync.c.

669{
670 ReplicationSlot *slot;
671 XLogRecPtr latestFlushPtr = GetStandbyFlushRecPtr(NULL);
672 bool slot_updated = false;
673
674 /* Search for the named slot */
675 if ((slot = SearchNamedReplicationSlot(remote_slot->name, true)))
676 {
677 bool synced;
678
679 SpinLockAcquire(&slot->mutex);
680 synced = slot->data.synced;
681 SpinLockRelease(&slot->mutex);
682
683 /* User-created slot with the same name exists, raise ERROR. */
684 if (!synced)
686 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
687 errmsg("exiting from slot synchronization because same"
688 " name slot \"%s\" already exists on the standby",
689 remote_slot->name));
690
691 /*
692 * The slot has been synchronized before.
693 *
694 * It is important to acquire the slot here before checking
695 * invalidation. If we don't acquire the slot first, there could be a
696 * race condition that the local slot could be invalidated just after
697 * checking the 'invalidated' flag here and we could end up
698 * overwriting 'invalidated' flag to remote_slot's value. See
699 * InvalidatePossiblyObsoleteSlot() where it invalidates slot directly
700 * if the slot is not acquired by other processes.
701 *
702 * XXX: If it ever turns out that slot acquire/release is costly for
703 * cases when none of the slot properties is changed then we can do a
704 * pre-check to ensure that at least one of the slot properties is
705 * changed before acquiring the slot.
706 */
707 ReplicationSlotAcquire(remote_slot->name, true, false);
708
709 Assert(slot == MyReplicationSlot);
710
711 /*
712 * Copy the invalidation cause from remote only if local slot is not
713 * invalidated locally, we don't want to overwrite existing one.
714 */
715 if (slot->data.invalidated == RS_INVAL_NONE &&
716 remote_slot->invalidated != RS_INVAL_NONE)
717 {
718 SpinLockAcquire(&slot->mutex);
719 slot->data.invalidated = remote_slot->invalidated;
720 SpinLockRelease(&slot->mutex);
721
722 /* Make sure the invalidated state persists across server restart */
725
726 slot_updated = true;
727 }
728
729 /* Skip the sync of an invalidated slot */
730 if (slot->data.invalidated != RS_INVAL_NONE)
731 {
733
735 return slot_updated;
736 }
737
738 /*
739 * Make sure that concerned WAL is received and flushed before syncing
740 * slot to target lsn received from the primary server.
741 *
742 * Report statistics only after the slot has been acquired, ensuring
743 * it cannot be dropped during the reporting process.
744 */
745 if (remote_slot->confirmed_lsn > latestFlushPtr)
746 {
748
749 /*
750 * Can get here only if GUC 'synchronized_standby_slots' on the
751 * primary server was not configured correctly.
752 */
754 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
755 errmsg("skipping slot synchronization because the received slot sync"
756 " LSN %X/%08X for slot \"%s\" is ahead of the standby position %X/%08X",
757 LSN_FORMAT_ARGS(remote_slot->confirmed_lsn),
758 remote_slot->name,
759 LSN_FORMAT_ARGS(latestFlushPtr)));
760
762
763 return slot_updated;
764 }
765
766 /* Slot not ready yet, let's attempt to make it sync-ready now. */
767 if (slot->data.persistency == RS_TEMPORARY)
768 {
769 slot_updated = update_and_persist_local_synced_slot(remote_slot,
770 remote_dbid);
771 }
772
773 /* Slot ready for sync, so sync it. */
774 else
775 {
776 /*
777 * Sanity check: As long as the invalidations are handled
778 * appropriately as above, this should never happen.
779 *
780 * We don't need to check restart_lsn here. See the comments in
781 * update_local_synced_slot() for details.
782 */
783 if (remote_slot->confirmed_lsn < slot->data.confirmed_flush)
785 errmsg_internal("cannot synchronize local slot \"%s\"",
786 remote_slot->name),
787 errdetail_internal("Local slot's start streaming location LSN(%X/%08X) is ahead of remote slot's LSN(%X/%08X).",
789 LSN_FORMAT_ARGS(remote_slot->confirmed_lsn)));
790
791 slot_updated = update_local_synced_slot(remote_slot, remote_dbid,
792 NULL, NULL);
793 }
794 }
795 /* Otherwise create the slot first. */
796 else
797 {
798 NameData plugin_name;
799 TransactionId xmin_horizon = InvalidTransactionId;
800
801 /* Skip creating the local slot if remote_slot is invalidated already */
802 if (remote_slot->invalidated != RS_INVAL_NONE)
803 return false;
804
805 /*
806 * We create temporary slots instead of ephemeral slots here because
807 * we want the slots to survive after releasing them. This is done to
808 * avoid dropping and re-creating the slots in each synchronization
809 * cycle if the restart_lsn or catalog_xmin of the remote slot has not
810 * caught up.
811 */
812 ReplicationSlotCreate(remote_slot->name, true, RS_TEMPORARY,
813 remote_slot->two_phase,
814 remote_slot->failover,
815 true);
816
817 /* For shorter lines. */
818 slot = MyReplicationSlot;
819
820 /* Avoid expensive operations while holding a spinlock. */
821 namestrcpy(&plugin_name, remote_slot->plugin);
822
823 SpinLockAcquire(&slot->mutex);
824 slot->data.database = remote_dbid;
825 slot->data.plugin = plugin_name;
826 SpinLockRelease(&slot->mutex);
827
829
830 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
831 xmin_horizon = GetOldestSafeDecodingTransactionId(true);
832 SpinLockAcquire(&slot->mutex);
833 slot->effective_catalog_xmin = xmin_horizon;
834 slot->data.catalog_xmin = xmin_horizon;
835 SpinLockRelease(&slot->mutex);
837 LWLockRelease(ProcArrayLock);
838
839 /*
840 * Make sure that concerned WAL is received and flushed before syncing
841 * slot to target lsn received from the primary server.
842 *
843 * Report statistics only after the slot has been acquired, ensuring
844 * it cannot be dropped during the reporting process.
845 */
846 if (remote_slot->confirmed_lsn > latestFlushPtr)
847 {
849
850 /*
851 * Can get here only if GUC 'synchronized_standby_slots' on the
852 * primary server was not configured correctly.
853 */
855 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
856 errmsg("skipping slot synchronization because the received slot sync"
857 " LSN %X/%08X for slot \"%s\" is ahead of the standby position %X/%08X",
858 LSN_FORMAT_ARGS(remote_slot->confirmed_lsn),
859 remote_slot->name,
860 LSN_FORMAT_ARGS(latestFlushPtr)));
861
863
864 return false;
865 }
866
867 update_and_persist_local_synced_slot(remote_slot, remote_dbid);
868
869 slot_updated = true;
870 }
871
873
874 return slot_updated;
875}
uint32 TransactionId
Definition: c.h:660
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
#define AmLogicalSlotSyncWorkerProcess()
Definition: miscadmin.h:386
void namestrcpy(Name name, const char *str)
Definition: name.c:233
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2907
void ReplicationSlotCreate(const char *name, bool db_specific, ReplicationSlotPersistency persistency, bool two_phase, bool failover, bool synced)
Definition: slot.c:384
void ReplicationSlotMarkDirty(void)
Definition: slot.c:1139
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:1178
void ReplicationSlotSave(void)
Definition: slot.c:1121
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition: slot.c:546
@ 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:532
static void update_slotsync_skip_stats(SlotSyncSkipReason skip_reason)
Definition: slotsync.c:156
static bool update_and_persist_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid)
Definition: slotsync.c:603
static bool update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid, bool *found_consistent_snapshot, bool *remote_slot_precedes)
Definition: slotsync.c:195
bool two_phase
Definition: slotsync.c:137
char * plugin
Definition: slotsync.c:135
char * name
Definition: slotsync.c:134
bool failover
Definition: slotsync.c:138
ReplicationSlotInvalidationCause invalidated
Definition: slotsync.c:145
XLogRecPtr confirmed_lsn
Definition: slotsync.c:140
XLogRecPtr restart_lsn
Definition: slotsync.c:139
TransactionId catalog_xmin
Definition: slot.h:122
XLogRecPtr confirmed_flush
Definition: slot.h:136
ReplicationSlotPersistency persistency
Definition: slot.h:106
TransactionId effective_catalog_xmin
Definition: slot.h:207
Definition: c.h:749
#define InvalidTransactionId
Definition: transam.h:31
XLogRecPtr GetStandbyFlushRecPtr(TimeLineID *tli)
Definition: walsender.c:3631
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:47
uint64 XLogRecPtr
Definition: xlogdefs.h:21

References AmLogicalSlotSyncWorkerProcess, Assert(), ReplicationSlotPersistentData::catalog_xmin, ReplicationSlotPersistentData::confirmed_flush, RemoteSlot::confirmed_lsn, ReplicationSlot::data, ReplicationSlotPersistentData::database, ReplicationSlot::effective_catalog_xmin, ereport, errcode(), errdetail_internal(), errmsg(), errmsg_internal(), ERROR, RemoteSlot::failover, GetOldestSafeDecodingTransactionId(), GetStandbyFlushRecPtr(), RemoteSlot::invalidated, ReplicationSlotPersistentData::invalidated, InvalidTransactionId, LOG, LSN_FORMAT_ARGS, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), ReplicationSlot::mutex, MyReplicationSlot, RemoteSlot::name, namestrcpy(), ReplicationSlotPersistentData::persistency, RemoteSlot::plugin, ReplicationSlotPersistentData::plugin, ReplicationSlotAcquire(), ReplicationSlotCreate(), ReplicationSlotMarkDirty(), ReplicationSlotRelease(), ReplicationSlotSave(), ReplicationSlotsComputeRequiredXmin(), reserve_wal_for_local_slot(), RemoteSlot::restart_lsn, RS_INVAL_NONE, RS_TEMPORARY, SearchNamedReplicationSlot(), SpinLockAcquire, SpinLockRelease, SS_SKIP_INVALID, SS_SKIP_WAL_NOT_FLUSHED, ReplicationSlotPersistentData::synced, RemoteSlot::two_phase, 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)
static

Definition at line 886 of file slotsync.c.

887{
888#define SLOTSYNC_COLUMN_COUNT 10
889 Oid slotRow[SLOTSYNC_COLUMN_COUNT] = {TEXTOID, TEXTOID, LSNOID,
890 LSNOID, XIDOID, BOOLOID, LSNOID, BOOLOID, TEXTOID, TEXTOID};
891
892 WalRcvExecResult *res;
893 TupleTableSlot *tupslot;
894 List *remote_slot_list = NIL;
895 bool some_slot_updated = false;
896 bool started_tx = false;
897 const char *query = "SELECT slot_name, plugin, confirmed_flush_lsn,"
898 " restart_lsn, catalog_xmin, two_phase, two_phase_at, failover,"
899 " database, invalidation_reason"
900 " FROM pg_catalog.pg_replication_slots"
901 " WHERE failover and NOT temporary";
902
903 /* The syscache access in walrcv_exec() needs a transaction env. */
904 if (!IsTransactionState())
905 {
907 started_tx = true;
908 }
909
910 /* Execute the query */
911 res = walrcv_exec(wrconn, query, SLOTSYNC_COLUMN_COUNT, slotRow);
912 if (res->status != WALRCV_OK_TUPLES)
914 errmsg("could not fetch failover logical slots info from the primary server: %s",
915 res->err));
916
917 /* Construct the remote_slot tuple and synchronize each slot locally */
919 while (tuplestore_gettupleslot(res->tuplestore, true, false, tupslot))
920 {
921 bool isnull;
922 RemoteSlot *remote_slot = palloc0(sizeof(RemoteSlot));
923 Datum d;
924 int col = 0;
925
926 remote_slot->name = TextDatumGetCString(slot_getattr(tupslot, ++col,
927 &isnull));
928 Assert(!isnull);
929
930 remote_slot->plugin = TextDatumGetCString(slot_getattr(tupslot, ++col,
931 &isnull));
932 Assert(!isnull);
933
934 /*
935 * It is possible to get null values for LSN and Xmin if slot is
936 * invalidated on the primary server, so handle accordingly.
937 */
938 d = slot_getattr(tupslot, ++col, &isnull);
939 remote_slot->confirmed_lsn = isnull ? InvalidXLogRecPtr :
940 DatumGetLSN(d);
941
942 d = slot_getattr(tupslot, ++col, &isnull);
943 remote_slot->restart_lsn = isnull ? InvalidXLogRecPtr : DatumGetLSN(d);
944
945 d = slot_getattr(tupslot, ++col, &isnull);
946 remote_slot->catalog_xmin = isnull ? InvalidTransactionId :
948
949 remote_slot->two_phase = DatumGetBool(slot_getattr(tupslot, ++col,
950 &isnull));
951 Assert(!isnull);
952
953 d = slot_getattr(tupslot, ++col, &isnull);
954 remote_slot->two_phase_at = isnull ? InvalidXLogRecPtr : DatumGetLSN(d);
955
956 remote_slot->failover = DatumGetBool(slot_getattr(tupslot, ++col,
957 &isnull));
958 Assert(!isnull);
959
960 remote_slot->database = TextDatumGetCString(slot_getattr(tupslot,
961 ++col, &isnull));
962 Assert(!isnull);
963
964 d = slot_getattr(tupslot, ++col, &isnull);
965 remote_slot->invalidated = isnull ? RS_INVAL_NONE :
967
968 /* Sanity check */
970
971 /*
972 * If restart_lsn, confirmed_lsn or catalog_xmin is invalid but the
973 * slot is valid, that means we have fetched the remote_slot in its
974 * RS_EPHEMERAL state. In such a case, don't sync it; we can always
975 * sync it in the next sync cycle when the remote_slot is persisted
976 * and has valid lsn(s) and xmin values.
977 *
978 * XXX: In future, if we plan to expose 'slot->data.persistency' in
979 * pg_replication_slots view, then we can avoid fetching RS_EPHEMERAL
980 * slots in the first place.
981 */
982 if ((!XLogRecPtrIsValid(remote_slot->restart_lsn) ||
983 !XLogRecPtrIsValid(remote_slot->confirmed_lsn) ||
984 !TransactionIdIsValid(remote_slot->catalog_xmin)) &&
985 remote_slot->invalidated == RS_INVAL_NONE)
986 pfree(remote_slot);
987 else
988 /* Create list of remote slots */
989 remote_slot_list = lappend(remote_slot_list, remote_slot);
990
991 ExecClearTuple(tupslot);
992 }
993
994 /* Drop local slots that no longer need to be synced. */
995 drop_local_obsolete_slots(remote_slot_list);
996
997 /* Now sync the slots locally */
998 foreach_ptr(RemoteSlot, remote_slot, remote_slot_list)
999 {
1000 Oid remote_dbid = get_database_oid(remote_slot->database, false);
1001
1002 /*
1003 * Use shared lock to prevent a conflict with
1004 * ReplicationSlotsDropDBSlots(), trying to drop the same slot during
1005 * a drop-database operation.
1006 */
1007 LockSharedObject(DatabaseRelationId, remote_dbid, 0, AccessShareLock);
1008
1009 some_slot_updated |= synchronize_one_slot(remote_slot, remote_dbid);
1010
1011 UnlockSharedObject(DatabaseRelationId, remote_dbid, 0, AccessShareLock);
1012 }
1013
1014 /* We are done, free remote_slot_list elements */
1015 list_free_deep(remote_slot_list);
1016
1018
1019 if (started_tx)
1021
1022 return some_slot_updated;
1023}
#define TextDatumGetCString(d)
Definition: builtins.h:98
Oid get_database_oid(const char *dbname, bool missing_ok)
Definition: dbcommands.c:3167
TupleTableSlot * MakeSingleTupleTableSlot(TupleDesc tupdesc, const TupleTableSlotOps *tts_ops)
Definition: execTuples.c:1427
const TupleTableSlotOps TTSOpsMinimalTuple
Definition: execTuples.c:86
void list_free_deep(List *list)
Definition: list.c:1560
void * palloc0(Size size)
Definition: mcxt.c:1395
static XLogRecPtr DatumGetLSN(Datum X)
Definition: pg_lsn.h:25
static bool DatumGetBool(Datum X)
Definition: postgres.h:100
static TransactionId DatumGetTransactionId(Datum X)
Definition: postgres.h:272
unsigned int Oid
Definition: postgres_ext.h:32
ReplicationSlotInvalidationCause GetSlotInvalidationCause(const char *cause_name)
Definition: slot.c:2725
static void drop_local_obsolete_slots(List *remote_slot_list)
Definition: slotsync.c:475
#define SLOTSYNC_COLUMN_COUNT
static bool synchronize_one_slot(RemoteSlot *remote_slot, Oid remote_dbid)
Definition: slotsync.c:668
char * database
Definition: slotsync.c:136
XLogRecPtr two_phase_at
Definition: slotsync.c:141
TransactionId catalog_xmin
Definition: slotsync.c:142
Tuplestorestate * tuplestore
Definition: walreceiver.h:223
TupleDesc tupledesc
Definition: walreceiver.h:224
WalRcvExecStatus status
Definition: walreceiver.h:220
#define TransactionIdIsValid(xid)
Definition: transam.h:41
bool tuplestore_gettupleslot(Tuplestorestate *state, bool forward, bool copy, TupleTableSlot *slot)
Definition: tuplestore.c:1130
static Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
Definition: tuptable.h:398
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:457
@ WALRCV_OK_TUPLES
Definition: walreceiver.h:207
static void walrcv_clear_result(WalRcvExecResult *walres)
Definition: walreceiver.h:471
#define walrcv_exec(conn, exec, nRetTypes, retTypes)
Definition: walreceiver.h:465
bool IsTransactionState(void)
Definition: xact.c:388
void StartTransactionCommand(void)
Definition: xact.c:3077
void CommitTransactionCommand(void)
Definition: xact.c:3175
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

References AccessShareLock, Assert(), RemoteSlot::catalog_xmin, CommitTransactionCommand(), RemoteSlot::confirmed_lsn, RemoteSlot::database, DatumGetBool(), DatumGetLSN(), DatumGetTransactionId(), drop_local_obsolete_slots(), ereport, WalRcvExecResult::err, errmsg(), ERROR, ExecClearTuple(), RemoteSlot::failover, foreach_ptr, get_database_oid(), GetSlotInvalidationCause(), RemoteSlot::invalidated, InvalidTransactionId, InvalidXLogRecPtr, IsTransactionState(), lappend(), list_free_deep(), LockSharedObject(), MakeSingleTupleTableSlot(), RemoteSlot::name, NIL, palloc0(), pfree(), RemoteSlot::plugin, RemoteSlot::restart_lsn, RS_INVAL_NONE, slot_getattr(), SLOTSYNC_COLUMN_COUNT, StartTransactionCommand(), WalRcvExecResult::status, synchronize_one_slot(), TextDatumGetCString, TransactionIdIsValid, TTSOpsMinimalTuple, WalRcvExecResult::tupledesc, WalRcvExecResult::tuplestore, tuplestore_gettupleslot(), RemoteSlot::two_phase, RemoteSlot::two_phase_at, UnlockSharedObject(), walrcv_clear_result(), walrcv_exec, WALRCV_OK_TUPLES, wrconn, and XLogRecPtrIsValid.

Referenced by ReplSlotSyncWorkerMain(), and SyncReplicationSlots().

◆ 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
static void slotsync_failure_callback(int code, Datum arg)
Definition: slotsync.c:1792

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().

◆ update_and_persist_local_synced_slot()

static bool update_and_persist_local_synced_slot ( RemoteSlot remote_slot,
Oid  remote_dbid 
)
static

Definition at line 603 of file slotsync.c.

604{
606 bool found_consistent_snapshot = false;
607 bool remote_slot_precedes = false;
608
609 /* Slotsync skip stats are handled in function update_local_synced_slot() */
610 (void) update_local_synced_slot(remote_slot, remote_dbid,
611 &found_consistent_snapshot,
612 &remote_slot_precedes);
613
614 /*
615 * Check if the primary server has caught up. Refer to the comment atop
616 * the file for details on this check.
617 */
618 if (remote_slot_precedes)
619 {
620 /*
621 * The remote slot didn't catch up to locally reserved position.
622 *
623 * We do not drop the slot because the restart_lsn can be ahead of the
624 * current location when recreating the slot in the next cycle. It may
625 * take more time to create such a slot. Therefore, we keep this slot
626 * and attempt the synchronization in the next cycle.
627 */
628 return false;
629 }
630
631 /*
632 * Don't persist the slot if it cannot reach the consistent point from the
633 * restart_lsn. See comments atop this file.
634 */
635 if (!found_consistent_snapshot)
636 {
637 ereport(LOG,
638 errmsg("could not synchronize replication slot \"%s\"", remote_slot->name),
639 errdetail("Synchronization could lead to data loss, because the standby could not build a consistent snapshot to decode WALs at LSN %X/%08X.",
641
642 return false;
643 }
644
646
647 ereport(LOG,
648 errmsg("newly created replication slot \"%s\" is sync-ready now",
649 remote_slot->name));
650
651 return true;
652}
int errdetail(const char *fmt,...)
Definition: elog.c:1216
void ReplicationSlotPersist(void)
Definition: slot.c:1156

References ReplicationSlot::data, ereport, errdetail(), errmsg(), LOG, LSN_FORMAT_ARGS, MyReplicationSlot, RemoteSlot::name, 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 195 of file slotsync.c.

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

References Assert(), RemoteSlot::catalog_xmin, ReplicationSlotPersistentData::catalog_xmin, ReplicationSlotPersistentData::confirmed_flush, RemoteSlot::confirmed_lsn, ReplicationSlot::data, ReplicationSlotPersistentData::database, DEBUG1, ReplicationSlot::effective_catalog_xmin, ereport, errdetail(), errdetail_internal(), errmsg(), errmsg_internal(), ERROR, RemoteSlot::failover, ReplicationSlotPersistentData::failover, ReplicationSlotPersistentData::invalidated, LOG, LogicalSlotAdvanceAndCheckSnapState(), LSN_FORMAT_ARGS, ReplicationSlot::mutex, MyReplicationSlot, RemoteSlot::name, NameStr, namestrcpy(), ReplicationSlotPersistentData::persistency, RemoteSlot::plugin, ReplicationSlotPersistentData::plugin, ReplicationSlotMarkDirty(), ReplicationSlotSave(), ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), RemoteSlot::restart_lsn, 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(), RemoteSlot::two_phase, ReplicationSlotPersistentData::two_phase, RemoteSlot::two_phase_at, 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 156 of file slotsync.c.

157{
158 ReplicationSlot *slot;
159
161
162 slot = MyReplicationSlot;
163
164 /*
165 * Update the slot sync related stats in pg_stat_replication_slot when a
166 * slot sync is skipped
167 */
168 if (skip_reason != SS_SKIP_NONE)
170
171 /* Update the slot sync skip reason */
172 if (slot->slotsync_skip_reason != skip_reason)
173 {
174 SpinLockAcquire(&slot->mutex);
175 slot->slotsync_skip_reason = skip_reason;
176 SpinLockRelease(&slot->mutex);
177 }
178}
void pgstat_report_replslotsync(ReplicationSlot *slot)
SlotSyncSkipReason slotsync_skip_reason
Definition: slot.h:281

References Assert(), 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 1610 of file slotsync.c.

1611{
1612 TimestampTz now = 0;
1613
1614 /*
1615 * We need to update inactive_since only when we are promoting standby to
1616 * correctly interpret the inactive_since if the standby gets promoted
1617 * without a restart. We don't want the slots to appear inactive for a
1618 * long time after promotion if they haven't been synchronized recently.
1619 * Whoever acquires the slot, i.e., makes the slot active, will reset it.
1620 */
1621 if (!StandbyMode)
1622 return;
1623
1624 /* The slot sync worker or SQL function mustn't be running by now */
1626
1627 LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1628
1629 for (int i = 0; i < max_replication_slots; i++)
1630 {
1632
1633 /* Check if it is a synchronized slot */
1634 if (s->in_use && s->data.synced)
1635 {
1637
1638 /* The slot must not be acquired by any process */
1639 Assert(s->active_pid == 0);
1640
1641 /* Use the same inactive_since time for all the slots. */
1642 if (now == 0)
1644
1646 }
1647 }
1648
1649 LWLockRelease(ReplicationSlotControlLock);
1650}
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
Definition: xlogrecovery.c:150

References ReplicationSlot::active_pid, Assert(), ReplicationSlot::data, 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 1032 of file slotsync.c.

1033{
1034#define PRIMARY_INFO_OUTPUT_COL_COUNT 2
1035 WalRcvExecResult *res;
1036 Oid slotRow[PRIMARY_INFO_OUTPUT_COL_COUNT] = {BOOLOID, BOOLOID};
1037 StringInfoData cmd;
1038 bool isnull;
1039 TupleTableSlot *tupslot;
1040 bool remote_in_recovery;
1041 bool primary_slot_valid;
1042 bool started_tx = false;
1043
1044 initStringInfo(&cmd);
1045 appendStringInfo(&cmd,
1046 "SELECT pg_is_in_recovery(), count(*) = 1"
1047 " FROM pg_catalog.pg_replication_slots"
1048 " WHERE slot_type='physical' AND slot_name=%s",
1050
1051 /* The syscache access in walrcv_exec() needs a transaction env. */
1052 if (!IsTransactionState())
1053 {
1055 started_tx = true;
1056 }
1057
1059 pfree(cmd.data);
1060
1061 if (res->status != WALRCV_OK_TUPLES)
1062 ereport(ERROR,
1063 errmsg("could not fetch primary slot name \"%s\" info from the primary server: %s",
1064 PrimarySlotName, res->err),
1065 errhint("Check if \"primary_slot_name\" is configured correctly."));
1066
1068 if (!tuplestore_gettupleslot(res->tuplestore, true, false, tupslot))
1069 elog(ERROR,
1070 "failed to fetch tuple for the primary server slot specified by \"primary_slot_name\"");
1071
1072 remote_in_recovery = DatumGetBool(slot_getattr(tupslot, 1, &isnull));
1073 Assert(!isnull);
1074
1075 /*
1076 * Slot sync is currently not supported on a cascading standby. This is
1077 * because if we allow it, the primary server needs to wait for all the
1078 * cascading standbys, otherwise, logical subscribers can still be ahead
1079 * of one of the cascading standbys which we plan to promote. Thus, to
1080 * avoid this additional complexity, we restrict it for the time being.
1081 */
1082 if (remote_in_recovery)
1083 ereport(ERROR,
1084 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1085 errmsg("cannot synchronize replication slots from a standby server"));
1086
1087 primary_slot_valid = DatumGetBool(slot_getattr(tupslot, 2, &isnull));
1088 Assert(!isnull);
1089
1090 if (!primary_slot_valid)
1091 ereport(ERROR,
1092 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1093 /* translator: second %s is a GUC variable name */
1094 errmsg("replication slot \"%s\" specified by \"%s\" does not exist on primary server",
1095 PrimarySlotName, "primary_slot_name"));
1096
1097 ExecClearTuple(tupslot);
1099
1100 if (started_tx)
1102}
int errhint(const char *fmt,...)
Definition: elog.c:1330
char * quote_literal_cstr(const char *rawstr)
Definition: quote.c:103
#define PRIMARY_INFO_OUTPUT_COL_COUNT

References appendStringInfo(), Assert(), CommitTransactionCommand(), StringInfoData::data, DatumGetBool(), elog, ereport, WalRcvExecResult::err, errcode(), errhint(), errmsg(), ERROR, ExecClearTuple(), 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 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}
int wal_level
Definition: xlog.c:133
@ WAL_LEVEL_LOGICAL
Definition: xlog.h:76

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

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

1335{
1336 int rc;
1337
1338 if (!some_slot_updated)
1339 {
1340 /*
1341 * No slots were updated, so double the sleep time, but not beyond the
1342 * maximum allowable value.
1343 */
1345 }
1346 else
1347 {
1348 /*
1349 * Some slots were updated since the last sleep, so reset the sleep
1350 * time.
1351 */
1353 }
1354
1355 rc = WaitLatch(MyLatch,
1357 sleep_ms,
1358 WAIT_EVENT_REPLICATION_SLOTSYNC_MAIN);
1359
1360 if (rc & WL_LATCH_SET)
1362}
#define Min(x, y)
Definition: c.h:1006
#define MIN_SLOTSYNC_WORKER_NAPTIME_MS
Definition: slotsync.c:113
static long sleep_ms
Definition: slotsync.c:116
#define MAX_SLOTSYNC_WORKER_NAPTIME_MS
Definition: slotsync.c:114

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

Variable Documentation

◆ sleep_ms

long sleep_ms = MIN_SLOTSYNC_WORKER_NAPTIME_MS
static

Definition at line 116 of file slotsync.c.

Referenced by do_watch(), and wait_for_slot_activity().

◆ SlotSyncCtx

◆ sync_replication_slots

bool sync_replication_slots = false

◆ syncing_slots