PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 "commands/dbcommands.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 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 (WalReceiverConn *wrconn)
 
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 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 115 of file slotsync.c.

◆ MIN_SLOTSYNC_WORKER_NAPTIME_MS

#define MIN_SLOTSYNC_WORKER_NAPTIME_MS   200

Definition at line 114 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 120 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 1275 of file slotsync.c.

1276{
1278
1279 /* The worker pid must not be already assigned in SlotSyncCtx */
1280 Assert(worker_pid == InvalidPid || SlotSyncCtx->pid == InvalidPid);
1281
1282 /*
1283 * Emit an error if startup process signaled the slot sync machinery to
1284 * stop. See comments atop SlotSyncCtxStruct.
1285 */
1287 {
1289 ereport(ERROR,
1290 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1291 errmsg("cannot synchronize replication slots when standby promotion is ongoing"));
1292 }
1293
1294 if (SlotSyncCtx->syncing)
1295 {
1297 ereport(ERROR,
1298 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1299 errmsg("cannot synchronize replication slots concurrently"));
1300 }
1301
1302 SlotSyncCtx->syncing = true;
1303
1304 /*
1305 * Advertise the required PID so that the startup process can kill the
1306 * slot sync worker on promotion.
1307 */
1308 SlotSyncCtx->pid = worker_pid;
1309
1311
1312 syncing_slots = true;
1313}
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
Assert(PointerIsAligned(start, uint64))
#define InvalidPid
Definition: miscadmin.h:32
static SlotSyncCtxStruct * SlotSyncCtx
Definition: slotsync.c:104
static bool syncing_slots
Definition: slotsync.c:127
#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 1016 of file slotsync.c.

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

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

Referenced by pg_sync_replication_slots(), and ReplSlotSyncWorkerMain().

◆ drop_local_obsolete_slots()

static void drop_local_obsolete_slots ( List remote_slot_list)
static

Definition at line 418 of file slotsync.c.

419{
420 List *local_slots = get_local_synced_slots();
421
422 foreach_ptr(ReplicationSlot, local_slot, local_slots)
423 {
424 /* Drop the local slot if it is not required to be retained. */
425 if (!local_sync_slot_required(local_slot, remote_slot_list))
426 {
427 bool synced_slot;
428
429 /*
430 * Use shared lock to prevent a conflict with
431 * ReplicationSlotsDropDBSlots(), trying to drop the same slot
432 * during a drop-database operation.
433 */
434 LockSharedObject(DatabaseRelationId, local_slot->data.database,
435 0, AccessShareLock);
436
437 /*
438 * In the small window between getting the slot to drop and
439 * locking the database, there is a possibility of a parallel
440 * database drop by the startup process and the creation of a new
441 * slot by the user. This new user-created slot may end up using
442 * the same shared memory as that of 'local_slot'. Thus check if
443 * local_slot is still the synced one before performing actual
444 * drop.
445 */
446 SpinLockAcquire(&local_slot->mutex);
447 synced_slot = local_slot->in_use && local_slot->data.synced;
448 SpinLockRelease(&local_slot->mutex);
449
450 if (synced_slot)
451 {
452 ReplicationSlotAcquire(NameStr(local_slot->data.name), true, false);
454 }
455
456 UnlockSharedObject(DatabaseRelationId, local_slot->data.database,
457 0, AccessShareLock);
458
459 ereport(LOG,
460 errmsg("dropped replication slot \"%s\" of database with OID %u",
461 NameStr(local_slot->data.name),
462 local_slot->data.database));
463 }
464 }
465}
#define NameStr(name)
Definition: c.h:717
#define LOG
Definition: elog.h:31
void LockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1082
void UnlockSharedObject(Oid classid, Oid objid, uint16 objsubid, LOCKMODE lockmode)
Definition: lmgr.c:1142
#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:559
void ReplicationSlotDropAcquired(void)
Definition: slot.c:919
static List * get_local_synced_slots(void)
Definition: slotsync.c:334
static bool local_sync_slot_required(ReplicationSlot *local_slot, List *remote_slots)
Definition: slotsync.c:365
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 334 of file slotsync.c.

335{
336 List *local_slots = NIL;
337
338 LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
339
340 for (int i = 0; i < max_replication_slots; i++)
341 {
343
344 /* Check if it is a synchronized slot */
345 if (s->in_use && s->data.synced)
346 {
348 local_slots = lappend(local_slots, s);
349 }
350 }
351
352 LWLockRelease(ReplicationSlotControlLock);
353
354 return local_slots;
355}
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:1182
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1902
@ LW_SHARED
Definition: lwlock.h:115
#define NIL
Definition: pg_list.h:68
int max_replication_slots
Definition: slot.c:150
ReplicationSlotCtlData * ReplicationSlotCtl
Definition: slot.c:144
#define SlotIsLogical(slot)
Definition: slot.h:221
ReplicationSlot replication_slots[1]
Definition: slot.h:232
bool in_use
Definition: slot.h:161
ReplicationSlotPersistentData data
Definition: slot.h:185

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

1654{
1655 return syncing_slots;
1656}

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

366{
367 bool remote_exists = false;
368 bool locally_invalidated = false;
369
370 foreach_ptr(RemoteSlot, remote_slot, remote_slots)
371 {
372 if (strcmp(remote_slot->name, NameStr(local_slot->data.name)) == 0)
373 {
374 remote_exists = true;
375
376 /*
377 * If remote slot is not invalidated but local slot is marked as
378 * invalidated, then set locally_invalidated flag.
379 */
380 SpinLockAcquire(&local_slot->mutex);
381 locally_invalidated =
382 (remote_slot->invalidated == RS_INVAL_NONE) &&
383 (local_slot->data.invalidated != RS_INVAL_NONE);
384 SpinLockRelease(&local_slot->mutex);
385
386 break;
387 }
388 }
389
390 return (remote_exists && !locally_invalidated);
391}
@ RS_INVAL_NONE
Definition: slot.h:53
ReplicationSlotInvalidationCause invalidated
Definition: slot.h:103
slock_t mutex
Definition: slot.h:158

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 ( WalReceiverConn wrconn)
static

Definition at line 1159 of file slotsync.c.

1160{
1162
1164 {
1165 ereport(LOG,
1166 errmsg("replication slot synchronization worker is shutting down on receiving SIGINT"));
1167
1168 proc_exit(0);
1169 }
1170
1173}
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:1110

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

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

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

◆ reserve_wal_for_local_slot()

static void reserve_wal_for_local_slot ( XLogRecPtr  restart_lsn)
static

Definition at line 475 of file slotsync.c.

476{
477 XLogSegNo oldest_segno;
478 XLogSegNo segno;
480
481 Assert(slot != NULL);
483
484 while (true)
485 {
486 SpinLockAcquire(&slot->mutex);
487 slot->data.restart_lsn = restart_lsn;
488 SpinLockRelease(&slot->mutex);
489
490 /* Prevent WAL removal as fast as possible */
492
494
495 /*
496 * Find the oldest existing WAL segment file.
497 *
498 * Normally, we can determine it by using the last removed segment
499 * number. However, if no WAL segment files have been removed by a
500 * checkpoint since startup, we need to search for the oldest segment
501 * file from the current timeline existing in XLOGDIR.
502 *
503 * XXX: Currently, we are searching for the oldest segment in the
504 * current timeline as there is less chance of the slot's restart_lsn
505 * from being some prior timeline, and even if it happens, in the
506 * worst case, we will wait to sync till the slot's restart_lsn moved
507 * to the current timeline.
508 */
509 oldest_segno = XLogGetLastRemovedSegno() + 1;
510
511 if (oldest_segno == 1)
512 {
513 TimeLineID cur_timeline;
514
515 GetWalRcvFlushRecPtr(NULL, &cur_timeline);
516 oldest_segno = XLogGetOldestSegno(cur_timeline);
517 }
518
519 elog(DEBUG1, "segno: " UINT64_FORMAT " of purposed restart_lsn for the synced slot, oldest_segno: " UINT64_FORMAT " available",
520 segno, oldest_segno);
521
522 /*
523 * If all required WAL is still there, great, otherwise retry. The
524 * slot should prevent further removal of WAL, unless there's a
525 * concurrent ReplicationSlotsComputeRequiredLSN() after we've written
526 * the new restart_lsn above, so normally we should never need to loop
527 * more than twice.
528 */
529 if (segno >= oldest_segno)
530 break;
531
532 /* Retry using the location of the oldest wal segment */
533 XLogSegNoOffsetToRecPtr(oldest_segno, 0, wal_segment_size, restart_lsn);
534 }
535}
#define UINT64_FORMAT
Definition: c.h:521
#define DEBUG1
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:226
ReplicationSlot * MyReplicationSlot
Definition: slot.c:147
void ReplicationSlotsComputeRequiredLSN(void)
Definition: slot.c:1156
XLogRecPtr GetWalRcvFlushRecPtr(XLogRecPtr *latestChunkStart, TimeLineID *receiveTLI)
XLogSegNo XLogGetLastRemovedSegno(void)
Definition: xlog.c:3897
int wal_segment_size
Definition: xlog.c:143
XLogSegNo XLogGetOldestSegno(TimeLineID tli)
Definition: xlog.c:3913
#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest)
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
uint32 TimeLineID
Definition: xlogdefs.h:59
uint64 XLogSegNo
Definition: xlogdefs.h:48

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

Referenced by synchronize_one_slot().

◆ reset_syncing_flag()

static void reset_syncing_flag ( )
static

◆ ShutDownSlotSync()

void ShutDownSlotSync ( void  )

Definition at line 1565 of file slotsync.c.

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

1692{
1694
1695 /*
1696 * We need to do slots cleanup here just like WalSndErrorCleanup() does.
1697 *
1698 * The startup process during promotion invokes ShutDownSlotSync() which
1699 * waits for slot sync to finish and it does that by checking the
1700 * 'syncing' flag. Thus the SQL function must be done with slots' release
1701 * and cleanup to avoid any dangling temporary slots or active slots
1702 * before it marks itself as finished syncing.
1703 */
1704
1705 /* Make sure active replication slots are released */
1706 if (MyReplicationSlot != NULL)
1708
1709 /* Also cleanup the synced temporary slots. */
1711
1712 /*
1713 * The set syncing_slots indicates that the process errored out without
1714 * resetting the flag. So, we need to clean up shared memory and reset the
1715 * flag here.
1716 */
1717 if (syncing_slots)
1719
1721}
void * arg
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
void ReplicationSlotRelease(void)
Definition: slot.c:686
void ReplicationSlotCleanup(bool synced_only)
Definition: slot.c:775
static void reset_syncing_flag()
Definition: slotsync.c:1319
#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 1110 of file slotsync.c.

1111{
1112 char *old_primary_conninfo = pstrdup(PrimaryConnInfo);
1113 char *old_primary_slotname = pstrdup(PrimarySlotName);
1114 bool old_sync_replication_slots = sync_replication_slots;
1115 bool old_hot_standby_feedback = hot_standby_feedback;
1116 bool conninfo_changed;
1117 bool primary_slotname_changed;
1118
1120
1121 ConfigReloadPending = false;
1123
1124 conninfo_changed = strcmp(old_primary_conninfo, PrimaryConnInfo) != 0;
1125 primary_slotname_changed = strcmp(old_primary_slotname, PrimarySlotName) != 0;
1126 pfree(old_primary_conninfo);
1127 pfree(old_primary_slotname);
1128
1129 if (old_sync_replication_slots != sync_replication_slots)
1130 {
1131 ereport(LOG,
1132 /* translator: %s is a GUC variable name */
1133 errmsg("replication slot synchronization worker will shut down because \"%s\" is disabled", "sync_replication_slots"));
1134 proc_exit(0);
1135 }
1136
1137 if (conninfo_changed ||
1138 primary_slotname_changed ||
1139 (old_hot_standby_feedback != hot_standby_feedback))
1140 {
1141 ereport(LOG,
1142 errmsg("replication slot synchronization worker will restart because of a parameter change"));
1143
1144 /*
1145 * Reset the last-start time for this worker so that the postmaster
1146 * can restart it without waiting for SLOTSYNC_RESTART_INTERVAL_SEC.
1147 */
1149
1150 proc_exit(0);
1151 }
1152
1153}
void ProcessConfigFile(GucContext context)
Definition: guc-file.l:120
@ PGC_SIGHUP
Definition: guc.h:75
char * pstrdup(const char *in)
Definition: mcxt.c:2322
bool sync_replication_slots
Definition: slotsync.c:107
time_t last_start_time
Definition: slotsync.c:100
bool hot_standby_feedback
Definition: walreceiver.c:90
char * PrimarySlotName
Definition: xlogrecovery.c:98

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

1195{
1196 /*
1197 * We need to do slots cleanup here just like WalSndErrorCleanup() does.
1198 *
1199 * The startup process during promotion invokes ShutDownSlotSync() which
1200 * waits for slot sync to finish and it does that by checking the
1201 * 'syncing' flag. Thus the slot sync worker must be done with slots'
1202 * release and cleanup to avoid any dangling temporary slots or active
1203 * slots before it marks itself as finished syncing.
1204 */
1205
1206 /* Make sure active replication slots are released */
1207 if (MyReplicationSlot != NULL)
1209
1210 /* Also cleanup the temporary slots. */
1212
1214
1216
1217 /*
1218 * If syncing_slots is true, it indicates that the process errored out
1219 * without resetting the flag. So, we need to clean up shared memory and
1220 * reset the flag here.
1221 */
1222 if (syncing_slots)
1223 {
1224 SlotSyncCtx->syncing = false;
1225 syncing_slots = false;
1226 }
1227
1229}

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

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

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

Referenced by CreateOrAttachShmemStructs().

◆ SlotSyncShmemSize()

Size SlotSyncShmemSize ( void  )

Definition at line 1662 of file slotsync.c.

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

Referenced by CalculateShmemSize(), and SlotSyncShmemInit().

◆ SlotSyncWorkerCanRestart()

bool SlotSyncWorkerCanRestart ( void  )

Definition at line 1633 of file slotsync.c.

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

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

611{
612 ReplicationSlot *slot;
613 XLogRecPtr latestFlushPtr;
614 bool slot_updated = false;
615
616 /*
617 * Make sure that concerned WAL is received and flushed before syncing
618 * slot to target lsn received from the primary server.
619 */
620 latestFlushPtr = GetStandbyFlushRecPtr(NULL);
621 if (remote_slot->confirmed_lsn > latestFlushPtr)
622 {
623 /*
624 * Can get here only if GUC 'synchronized_standby_slots' on the
625 * primary server was not configured correctly.
626 */
628 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
629 errmsg("skipping slot synchronization because the received slot sync"
630 " LSN %X/%X for slot \"%s\" is ahead of the standby position %X/%X",
631 LSN_FORMAT_ARGS(remote_slot->confirmed_lsn),
632 remote_slot->name,
633 LSN_FORMAT_ARGS(latestFlushPtr)));
634
635 return false;
636 }
637
638 /* Search for the named slot */
639 if ((slot = SearchNamedReplicationSlot(remote_slot->name, true)))
640 {
641 bool synced;
642
643 SpinLockAcquire(&slot->mutex);
644 synced = slot->data.synced;
645 SpinLockRelease(&slot->mutex);
646
647 /* User-created slot with the same name exists, raise ERROR. */
648 if (!synced)
650 errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
651 errmsg("exiting from slot synchronization because same"
652 " name slot \"%s\" already exists on the standby",
653 remote_slot->name));
654
655 /*
656 * The slot has been synchronized before.
657 *
658 * It is important to acquire the slot here before checking
659 * invalidation. If we don't acquire the slot first, there could be a
660 * race condition that the local slot could be invalidated just after
661 * checking the 'invalidated' flag here and we could end up
662 * overwriting 'invalidated' flag to remote_slot's value. See
663 * InvalidatePossiblyObsoleteSlot() where it invalidates slot directly
664 * if the slot is not acquired by other processes.
665 *
666 * XXX: If it ever turns out that slot acquire/release is costly for
667 * cases when none of the slot properties is changed then we can do a
668 * pre-check to ensure that at least one of the slot properties is
669 * changed before acquiring the slot.
670 */
671 ReplicationSlotAcquire(remote_slot->name, true, false);
672
673 Assert(slot == MyReplicationSlot);
674
675 /*
676 * Copy the invalidation cause from remote only if local slot is not
677 * invalidated locally, we don't want to overwrite existing one.
678 */
679 if (slot->data.invalidated == RS_INVAL_NONE &&
680 remote_slot->invalidated != RS_INVAL_NONE)
681 {
682 SpinLockAcquire(&slot->mutex);
683 slot->data.invalidated = remote_slot->invalidated;
684 SpinLockRelease(&slot->mutex);
685
686 /* Make sure the invalidated state persists across server restart */
689
690 slot_updated = true;
691 }
692
693 /* Skip the sync of an invalidated slot */
694 if (slot->data.invalidated != RS_INVAL_NONE)
695 {
697 return slot_updated;
698 }
699
700 /* Slot not ready yet, let's attempt to make it sync-ready now. */
701 if (slot->data.persistency == RS_TEMPORARY)
702 {
703 slot_updated = update_and_persist_local_synced_slot(remote_slot,
704 remote_dbid);
705 }
706
707 /* Slot ready for sync, so sync it. */
708 else
709 {
710 /*
711 * Sanity check: As long as the invalidations are handled
712 * appropriately as above, this should never happen.
713 *
714 * We don't need to check restart_lsn here. See the comments in
715 * update_local_synced_slot() for details.
716 */
717 if (remote_slot->confirmed_lsn < slot->data.confirmed_flush)
719 errmsg_internal("cannot synchronize local slot \"%s\"",
720 remote_slot->name),
721 errdetail_internal("Local slot's start streaming location LSN(%X/%X) is ahead of remote slot's LSN(%X/%X).",
723 LSN_FORMAT_ARGS(remote_slot->confirmed_lsn)));
724
725 slot_updated = update_local_synced_slot(remote_slot, remote_dbid,
726 NULL, NULL);
727 }
728 }
729 /* Otherwise create the slot first. */
730 else
731 {
732 NameData plugin_name;
733 TransactionId xmin_horizon = InvalidTransactionId;
734
735 /* Skip creating the local slot if remote_slot is invalidated already */
736 if (remote_slot->invalidated != RS_INVAL_NONE)
737 return false;
738
739 /*
740 * We create temporary slots instead of ephemeral slots here because
741 * we want the slots to survive after releasing them. This is done to
742 * avoid dropping and re-creating the slots in each synchronization
743 * cycle if the restart_lsn or catalog_xmin of the remote slot has not
744 * caught up.
745 */
746 ReplicationSlotCreate(remote_slot->name, true, RS_TEMPORARY,
747 remote_slot->two_phase,
748 remote_slot->failover,
749 true);
750
751 /* For shorter lines. */
752 slot = MyReplicationSlot;
753
754 /* Avoid expensive operations while holding a spinlock. */
755 namestrcpy(&plugin_name, remote_slot->plugin);
756
757 SpinLockAcquire(&slot->mutex);
758 slot->data.database = remote_dbid;
759 slot->data.plugin = plugin_name;
760 SpinLockRelease(&slot->mutex);
761
763
764 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
765 xmin_horizon = GetOldestSafeDecodingTransactionId(true);
766 SpinLockAcquire(&slot->mutex);
767 slot->effective_catalog_xmin = xmin_horizon;
768 slot->data.catalog_xmin = xmin_horizon;
769 SpinLockRelease(&slot->mutex);
771 LWLockRelease(ProcArrayLock);
772
773 update_and_persist_local_synced_slot(remote_slot, remote_dbid);
774
775 slot_updated = true;
776 }
777
779
780 return slot_updated;
781}
uint32 TransactionId
Definition: c.h:623
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1158
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1231
@ LW_EXCLUSIVE
Definition: lwlock.h:114
#define AmLogicalSlotSyncWorkerProcess()
Definition: miscadmin.h:386
void namestrcpy(Name name, const char *str)
Definition: name.c:233
TransactionId GetOldestSafeDecodingTransactionId(bool catalogOnly)
Definition: procarray.c:2945
void ReplicationSlotCreate(const char *name, bool db_specific, ReplicationSlotPersistency persistency, bool two_phase, bool failover, bool synced)
Definition: slot.c:324
void ReplicationSlotMarkDirty(void)
Definition: slot.c:1061
void ReplicationSlotsComputeRequiredXmin(bool already_locked)
Definition: slot.c:1100
void ReplicationSlotSave(void)
Definition: slot.c:1043
ReplicationSlot * SearchNamedReplicationSlot(const char *name, bool need_lock)
Definition: slot.c:479
@ RS_TEMPORARY
Definition: slot.h:40
static void reserve_wal_for_local_slot(XLogRecPtr restart_lsn)
Definition: slotsync.c:475
static bool update_and_persist_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid)
Definition: slotsync.c:546
static bool update_local_synced_slot(RemoteSlot *remote_slot, Oid remote_dbid, bool *found_consistent_snapshot, bool *remote_slot_precedes)
Definition: slotsync.c:167
bool two_phase
Definition: slotsync.c:138
char * plugin
Definition: slotsync.c:136
char * name
Definition: slotsync.c:135
bool failover
Definition: slotsync.c:139
ReplicationSlotInvalidationCause invalidated
Definition: slotsync.c:146
XLogRecPtr confirmed_lsn
Definition: slotsync.c:141
XLogRecPtr restart_lsn
Definition: slotsync.c:140
TransactionId catalog_xmin
Definition: slot.h:97
XLogRecPtr confirmed_flush
Definition: slot.h:111
ReplicationSlotPersistency persistency
Definition: slot.h:81
TransactionId effective_catalog_xmin
Definition: slot.h:182
Definition: c.h:712
#define InvalidTransactionId
Definition: transam.h:31
XLogRecPtr GetStandbyFlushRecPtr(TimeLineID *tli)
Definition: walsender.c:3509
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
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, ReplicationSlotPersistentData::synced, RemoteSlot::two_phase, update_and_persist_local_synced_slot(), and update_local_synced_slot().

Referenced by synchronize_slots().

◆ synchronize_slots()

static bool synchronize_slots ( WalReceiverConn wrconn)
static

Definition at line 792 of file slotsync.c.

793{
794#define SLOTSYNC_COLUMN_COUNT 10
795 Oid slotRow[SLOTSYNC_COLUMN_COUNT] = {TEXTOID, TEXTOID, LSNOID,
796 LSNOID, XIDOID, BOOLOID, LSNOID, BOOLOID, TEXTOID, TEXTOID};
797
798 WalRcvExecResult *res;
799 TupleTableSlot *tupslot;
800 List *remote_slot_list = NIL;
801 bool some_slot_updated = false;
802 bool started_tx = false;
803 const char *query = "SELECT slot_name, plugin, confirmed_flush_lsn,"
804 " restart_lsn, catalog_xmin, two_phase, two_phase_at, failover,"
805 " database, invalidation_reason"
806 " FROM pg_catalog.pg_replication_slots"
807 " WHERE failover and NOT temporary";
808
809 /* The syscache access in walrcv_exec() needs a transaction env. */
810 if (!IsTransactionState())
811 {
813 started_tx = true;
814 }
815
816 /* Execute the query */
817 res = walrcv_exec(wrconn, query, SLOTSYNC_COLUMN_COUNT, slotRow);
818 if (res->status != WALRCV_OK_TUPLES)
820 errmsg("could not fetch failover logical slots info from the primary server: %s",
821 res->err));
822
823 /* Construct the remote_slot tuple and synchronize each slot locally */
825 while (tuplestore_gettupleslot(res->tuplestore, true, false, tupslot))
826 {
827 bool isnull;
828 RemoteSlot *remote_slot = palloc0(sizeof(RemoteSlot));
829 Datum d;
830 int col = 0;
831
832 remote_slot->name = TextDatumGetCString(slot_getattr(tupslot, ++col,
833 &isnull));
834 Assert(!isnull);
835
836 remote_slot->plugin = TextDatumGetCString(slot_getattr(tupslot, ++col,
837 &isnull));
838 Assert(!isnull);
839
840 /*
841 * It is possible to get null values for LSN and Xmin if slot is
842 * invalidated on the primary server, so handle accordingly.
843 */
844 d = slot_getattr(tupslot, ++col, &isnull);
845 remote_slot->confirmed_lsn = isnull ? InvalidXLogRecPtr :
846 DatumGetLSN(d);
847
848 d = slot_getattr(tupslot, ++col, &isnull);
849 remote_slot->restart_lsn = isnull ? InvalidXLogRecPtr : DatumGetLSN(d);
850
851 d = slot_getattr(tupslot, ++col, &isnull);
852 remote_slot->catalog_xmin = isnull ? InvalidTransactionId :
854
855 remote_slot->two_phase = DatumGetBool(slot_getattr(tupslot, ++col,
856 &isnull));
857 Assert(!isnull);
858
859 d = slot_getattr(tupslot, ++col, &isnull);
860 remote_slot->two_phase_at = isnull ? InvalidXLogRecPtr : DatumGetLSN(d);
861
862 remote_slot->failover = DatumGetBool(slot_getattr(tupslot, ++col,
863 &isnull));
864 Assert(!isnull);
865
866 remote_slot->database = TextDatumGetCString(slot_getattr(tupslot,
867 ++col, &isnull));
868 Assert(!isnull);
869
870 d = slot_getattr(tupslot, ++col, &isnull);
871 remote_slot->invalidated = isnull ? RS_INVAL_NONE :
873
874 /* Sanity check */
876
877 /*
878 * If restart_lsn, confirmed_lsn or catalog_xmin is invalid but the
879 * slot is valid, that means we have fetched the remote_slot in its
880 * RS_EPHEMERAL state. In such a case, don't sync it; we can always
881 * sync it in the next sync cycle when the remote_slot is persisted
882 * and has valid lsn(s) and xmin values.
883 *
884 * XXX: In future, if we plan to expose 'slot->data.persistency' in
885 * pg_replication_slots view, then we can avoid fetching RS_EPHEMERAL
886 * slots in the first place.
887 */
888 if ((XLogRecPtrIsInvalid(remote_slot->restart_lsn) ||
889 XLogRecPtrIsInvalid(remote_slot->confirmed_lsn) ||
890 !TransactionIdIsValid(remote_slot->catalog_xmin)) &&
891 remote_slot->invalidated == RS_INVAL_NONE)
892 pfree(remote_slot);
893 else
894 /* Create list of remote slots */
895 remote_slot_list = lappend(remote_slot_list, remote_slot);
896
897 ExecClearTuple(tupslot);
898 }
899
900 /* Drop local slots that no longer need to be synced. */
901 drop_local_obsolete_slots(remote_slot_list);
902
903 /* Now sync the slots locally */
904 foreach_ptr(RemoteSlot, remote_slot, remote_slot_list)
905 {
906 Oid remote_dbid = get_database_oid(remote_slot->database, false);
907
908 /*
909 * Use shared lock to prevent a conflict with
910 * ReplicationSlotsDropDBSlots(), trying to drop the same slot during
911 * a drop-database operation.
912 */
913 LockSharedObject(DatabaseRelationId, remote_dbid, 0, AccessShareLock);
914
915 some_slot_updated |= synchronize_one_slot(remote_slot, remote_dbid);
916
917 UnlockSharedObject(DatabaseRelationId, remote_dbid, 0, AccessShareLock);
918 }
919
920 /* We are done, free remote_slot_list elements */
921 list_free_deep(remote_slot_list);
922
924
925 if (started_tx)
927
928 return some_slot_updated;
929}
#define TextDatumGetCString(d)
Definition: builtins.h:98
Oid get_database_oid(const char *dbname, bool missing_ok)
Definition: dbcommands.c:3141
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:1970
static XLogRecPtr DatumGetLSN(Datum X)
Definition: pg_lsn.h:22
static bool DatumGetBool(Datum X)
Definition: postgres.h:95
static TransactionId DatumGetTransactionId(Datum X)
Definition: postgres.h:267
unsigned int Oid
Definition: postgres_ext.h:30
ReplicationSlotInvalidationCause GetSlotInvalidationCause(const char *cause_name)
Definition: slot.c:2607
static void drop_local_obsolete_slots(List *remote_slot_list)
Definition: slotsync.c:418
#define SLOTSYNC_COLUMN_COUNT
static bool synchronize_one_slot(RemoteSlot *remote_slot, Oid remote_dbid)
Definition: slotsync.c:610
char * database
Definition: slotsync.c:137
XLogRecPtr two_phase_at
Definition: slotsync.c:142
TransactionId catalog_xmin
Definition: slotsync.c:143
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:399
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:458
@ 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:387
void StartTransactionCommand(void)
Definition: xact.c:3059
void CommitTransactionCommand(void)
Definition: xact.c:3157
#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 XLogRecPtrIsInvalid.

Referenced by ReplSlotSyncWorkerMain(), and SyncReplicationSlots().

◆ SyncReplicationSlots()

void SyncReplicationSlots ( WalReceiverConn wrconn)

Definition at line 1728 of file slotsync.c.

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

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

547{
549 bool found_consistent_snapshot = false;
550 bool remote_slot_precedes = false;
551
552 (void) update_local_synced_slot(remote_slot, remote_dbid,
553 &found_consistent_snapshot,
554 &remote_slot_precedes);
555
556 /*
557 * Check if the primary server has caught up. Refer to the comment atop
558 * the file for details on this check.
559 */
560 if (remote_slot_precedes)
561 {
562 /*
563 * The remote slot didn't catch up to locally reserved position.
564 *
565 * We do not drop the slot because the restart_lsn can be ahead of the
566 * current location when recreating the slot in the next cycle. It may
567 * take more time to create such a slot. Therefore, we keep this slot
568 * and attempt the synchronization in the next cycle.
569 */
570 return false;
571 }
572
573 /*
574 * Don't persist the slot if it cannot reach the consistent point from the
575 * restart_lsn. See comments atop this file.
576 */
577 if (!found_consistent_snapshot)
578 {
579 ereport(LOG,
580 errmsg("could not synchronize replication slot \"%s\"", remote_slot->name),
581 errdetail("Logical decoding could not find consistent point from local slot's LSN %X/%X.",
583
584 return false;
585 }
586
588
589 ereport(LOG,
590 errmsg("newly created replication slot \"%s\" is sync-ready now",
591 remote_slot->name));
592
593 return true;
594}
int errdetail(const char *fmt,...)
Definition: elog.c:1204
void ReplicationSlotPersist(void)
Definition: slot.c:1078

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

170{
172 bool updated_xmin_or_lsn = false;
173 bool updated_config = false;
174
176
177 if (found_consistent_snapshot)
178 *found_consistent_snapshot = false;
179
180 if (remote_slot_precedes)
181 *remote_slot_precedes = false;
182
183 /*
184 * Don't overwrite if we already have a newer catalog_xmin and
185 * restart_lsn.
186 */
187 if (remote_slot->restart_lsn < slot->data.restart_lsn ||
189 slot->data.catalog_xmin))
190 {
191 /*
192 * This can happen in following situations:
193 *
194 * If the slot is temporary, it means either the initial WAL location
195 * reserved for the local slot is ahead of the remote slot's
196 * restart_lsn or the initial xmin_horizon computed for the local slot
197 * is ahead of the remote slot.
198 *
199 * If the slot is persistent, restart_lsn of the synced slot could
200 * still be ahead of the remote slot. Since we use slot advance
201 * functionality to keep snapbuild/slot updated, it is possible that
202 * the restart_lsn is advanced to a later position than it has on the
203 * primary. This can happen when slot advancing machinery finds
204 * running xacts record after reaching the consistent state at a later
205 * point than the primary where it serializes the snapshot and updates
206 * the restart_lsn.
207 *
208 * We LOG the message if the slot is temporary as it can help the user
209 * to understand why the slot is not sync-ready. In the case of a
210 * persistent slot, it would be a more common case and won't directly
211 * impact the users, so we used DEBUG1 level to log the message.
212 */
214 errmsg("could not synchronize replication slot \"%s\" because remote slot precedes local slot",
215 remote_slot->name),
216 errdetail("The remote slot has LSN %X/%X and catalog xmin %u, but the local slot has LSN %X/%X and catalog xmin %u.",
217 LSN_FORMAT_ARGS(remote_slot->restart_lsn),
218 remote_slot->catalog_xmin,
220 slot->data.catalog_xmin));
221
222 if (remote_slot_precedes)
223 *remote_slot_precedes = true;
224 }
225
226 /*
227 * Attempt to sync LSNs and xmins only if remote slot is ahead of local
228 * slot.
229 */
230 else if (remote_slot->confirmed_lsn > slot->data.confirmed_flush ||
231 remote_slot->restart_lsn > slot->data.restart_lsn ||
233 slot->data.catalog_xmin))
234 {
235 /*
236 * We can't directly copy the remote slot's LSN or xmin unless there
237 * exists a consistent snapshot at that point. Otherwise, after
238 * promotion, the slots may not reach a consistent point before the
239 * confirmed_flush_lsn which can lead to a data loss. To avoid data
240 * loss, we let slot machinery advance the slot which ensures that
241 * snapbuilder/slot statuses are updated properly.
242 */
243 if (SnapBuildSnapshotExists(remote_slot->restart_lsn))
244 {
245 /*
246 * Update the slot info directly if there is a serialized snapshot
247 * at the restart_lsn, as the slot can quickly reach consistency
248 * at restart_lsn by restoring the snapshot.
249 */
250 SpinLockAcquire(&slot->mutex);
251 slot->data.restart_lsn = remote_slot->restart_lsn;
252 slot->data.confirmed_flush = remote_slot->confirmed_lsn;
253 slot->data.catalog_xmin = remote_slot->catalog_xmin;
254 SpinLockRelease(&slot->mutex);
255
256 if (found_consistent_snapshot)
257 *found_consistent_snapshot = true;
258 }
259 else
260 {
262 found_consistent_snapshot);
263
264 /* Sanity check */
265 if (slot->data.confirmed_flush != remote_slot->confirmed_lsn)
267 errmsg_internal("synchronized confirmed_flush for slot \"%s\" differs from remote slot",
268 remote_slot->name),
269 errdetail_internal("Remote slot has LSN %X/%X but local slot has LSN %X/%X.",
270 LSN_FORMAT_ARGS(remote_slot->confirmed_lsn),
272 }
273
274 updated_xmin_or_lsn = true;
275 }
276
277 if (remote_dbid != slot->data.database ||
278 remote_slot->two_phase != slot->data.two_phase ||
279 remote_slot->failover != slot->data.failover ||
280 strcmp(remote_slot->plugin, NameStr(slot->data.plugin)) != 0 ||
281 remote_slot->two_phase_at != slot->data.two_phase_at)
282 {
283 NameData plugin_name;
284
285 /* Avoid expensive operations while holding a spinlock. */
286 namestrcpy(&plugin_name, remote_slot->plugin);
287
288 SpinLockAcquire(&slot->mutex);
289 slot->data.plugin = plugin_name;
290 slot->data.database = remote_dbid;
291 slot->data.two_phase = remote_slot->two_phase;
292 slot->data.two_phase_at = remote_slot->two_phase_at;
293 slot->data.failover = remote_slot->failover;
294 SpinLockRelease(&slot->mutex);
295
296 updated_config = true;
297 }
298
299 /*
300 * We have to write the changed xmin to disk *before* we change the
301 * in-memory value, otherwise after a crash we wouldn't know that some
302 * catalog tuples might have been removed already.
303 */
304 if (updated_config || updated_xmin_or_lsn)
305 {
308 }
309
310 /*
311 * Now the new xmin is safely on disk, we can let the global value
312 * advance. We do not take ProcArrayLock or similar since we only advance
313 * xmin here and there's not much harm done by a concurrent computation
314 * missing that.
315 */
316 if (updated_xmin_or_lsn)
317 {
318 SpinLockAcquire(&slot->mutex);
319 slot->effective_catalog_xmin = remote_slot->catalog_xmin;
320 SpinLockRelease(&slot->mutex);
321
324 }
325
326 return updated_config || updated_xmin_or_lsn;
327}
XLogRecPtr LogicalSlotAdvanceAndCheckSnapState(XLogRecPtr moveto, bool *found_consistent_snapshot)
Definition: logical.c:2044
bool SnapBuildSnapshotExists(XLogRecPtr lsn)
Definition: snapbuild.c:2050
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280
bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition: transam.c:314

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, TransactionIdFollows(), TransactionIdPrecedes(), RemoteSlot::two_phase, ReplicationSlotPersistentData::two_phase, RemoteSlot::two_phase_at, and ReplicationSlotPersistentData::two_phase_at.

Referenced by synchronize_one_slot(), and update_and_persist_local_synced_slot().

◆ update_synced_slots_inactive_since()

static void update_synced_slots_inactive_since ( void  )
static

Definition at line 1515 of file slotsync.c.

1516{
1517 TimestampTz now = 0;
1518
1519 /*
1520 * We need to update inactive_since only when we are promoting standby to
1521 * correctly interpret the inactive_since if the standby gets promoted
1522 * without a restart. We don't want the slots to appear inactive for a
1523 * long time after promotion if they haven't been synchronized recently.
1524 * Whoever acquires the slot, i.e., makes the slot active, will reset it.
1525 */
1526 if (!StandbyMode)
1527 return;
1528
1529 /* The slot sync worker or SQL function mustn't be running by now */
1531
1532 LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1533
1534 for (int i = 0; i < max_replication_slots; i++)
1535 {
1537
1538 /* Check if it is a synchronized slot */
1539 if (s->in_use && s->data.synced)
1540 {
1542
1543 /* The slot must not be acquired by any process */
1544 Assert(s->active_pid == 0);
1545
1546 /* Use the same inactive_since time for all the slots. */
1547 if (now == 0)
1549
1551 }
1552 }
1553
1554 LWLockRelease(ReplicationSlotControlLock);
1555}
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:239
pid_t active_pid
Definition: slot.h:164
bool StandbyMode
Definition: xlogrecovery.c:148

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

939{
940#define PRIMARY_INFO_OUTPUT_COL_COUNT 2
941 WalRcvExecResult *res;
942 Oid slotRow[PRIMARY_INFO_OUTPUT_COL_COUNT] = {BOOLOID, BOOLOID};
943 StringInfoData cmd;
944 bool isnull;
945 TupleTableSlot *tupslot;
946 bool remote_in_recovery;
947 bool primary_slot_valid;
948 bool started_tx = false;
949
950 initStringInfo(&cmd);
951 appendStringInfo(&cmd,
952 "SELECT pg_is_in_recovery(), count(*) = 1"
953 " FROM pg_catalog.pg_replication_slots"
954 " WHERE slot_type='physical' AND slot_name=%s",
956
957 /* The syscache access in walrcv_exec() needs a transaction env. */
958 if (!IsTransactionState())
959 {
961 started_tx = true;
962 }
963
965 pfree(cmd.data);
966
967 if (res->status != WALRCV_OK_TUPLES)
969 errmsg("could not fetch primary slot name \"%s\" info from the primary server: %s",
970 PrimarySlotName, res->err),
971 errhint("Check if \"primary_slot_name\" is configured correctly."));
972
974 if (!tuplestore_gettupleslot(res->tuplestore, true, false, tupslot))
975 elog(ERROR,
976 "failed to fetch tuple for the primary server slot specified by \"primary_slot_name\"");
977
978 remote_in_recovery = DatumGetBool(slot_getattr(tupslot, 1, &isnull));
979 Assert(!isnull);
980
981 /*
982 * Slot sync is currently not supported on a cascading standby. This is
983 * because if we allow it, the primary server needs to wait for all the
984 * cascading standbys, otherwise, logical subscribers can still be ahead
985 * of one of the cascading standbys which we plan to promote. Thus, to
986 * avoid this additional complexity, we restrict it for the time being.
987 */
988 if (remote_in_recovery)
990 errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
991 errmsg("cannot synchronize replication slots from a standby server"));
992
993 primary_slot_valid = DatumGetBool(slot_getattr(tupslot, 2, &isnull));
994 Assert(!isnull);
995
996 if (!primary_slot_valid)
998 errcode(ERRCODE_INVALID_PARAMETER_VALUE),
999 /* translator: second %s is a GUC variable name */
1000 errmsg("replication slot \"%s\" specified by \"%s\" does not exist on primary server",
1001 PrimarySlotName, "primary_slot_name"));
1002
1003 ExecClearTuple(tupslot);
1005
1006 if (started_tx)
1008}
int errhint(const char *fmt,...)
Definition: elog.c:1318
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 1043 of file slotsync.c.

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

References ereport, errcode(), errmsg(), ERROR, 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 1240 of file slotsync.c.

1241{
1242 int rc;
1243
1244 if (!some_slot_updated)
1245 {
1246 /*
1247 * No slots were updated, so double the sleep time, but not beyond the
1248 * maximum allowable value.
1249 */
1251 }
1252 else
1253 {
1254 /*
1255 * Some slots were updated since the last sleep, so reset the sleep
1256 * time.
1257 */
1259 }
1260
1261 rc = WaitLatch(MyLatch,
1263 sleep_ms,
1264 WAIT_EVENT_REPLICATION_SLOTSYNC_MAIN);
1265
1266 if (rc & WL_LATCH_SET)
1268}
#define Min(x, y)
Definition: c.h:975
#define MIN_SLOTSYNC_WORKER_NAPTIME_MS
Definition: slotsync.c:114
static long sleep_ms
Definition: slotsync.c:117
#define MAX_SLOTSYNC_WORKER_NAPTIME_MS
Definition: slotsync.c:115

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

Referenced by do_watch(), and wait_for_slot_activity().

◆ SlotSyncCtx

◆ sync_replication_slots

bool sync_replication_slots = false

Definition at line 107 of file slotsync.c.

Referenced by LaunchMissingBackgroundProcesses(), and slotsync_reread_config().

◆ syncing_slots