PostgreSQL Source Code git master
Loading...
Searching...
No Matches
logicallauncher.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void ApplyLauncherRegister (void)
 
void ApplyLauncherMain (Datum main_arg)
 
Size ApplyLauncherShmemSize (void)
 
void ApplyLauncherShmemInit (void)
 
void ApplyLauncherForgetWorkerStartTime (Oid subid)
 
void ApplyLauncherWakeupAtCommit (void)
 
void ApplyLauncherWakeup (void)
 
void AtEOXact_ApplyLauncher (bool isCommit)
 
void CreateConflictDetectionSlot (void)
 
bool IsLogicalLauncher (void)
 
pid_t GetLeaderApplyWorkerPid (pid_t pid)
 

Variables

PGDLLIMPORT int max_logical_replication_workers
 
PGDLLIMPORT int max_sync_workers_per_subscription
 
PGDLLIMPORT int max_parallel_apply_workers_per_subscription
 

Function Documentation

◆ ApplyLauncherForgetWorkerStartTime()

void ApplyLauncherForgetWorkerStartTime ( Oid  subid)
extern

Definition at line 1154 of file launcher.c.

1155{
1157
1159}
bool dshash_delete_key(dshash_table *hash_table, const void *key)
Definition dshash.c:505
static dshash_table * last_start_times
Definition launcher.c:91
static void logicalrep_launcher_attach_dshmem(void)
Definition launcher.c:1068
static int fb(int x)

References dshash_delete_key(), fb(), last_start_times, and logicalrep_launcher_attach_dshmem().

Referenced by apply_worker_exit(), DisableSubscriptionAndExit(), DropSubscription(), InitializeLogRepWorker(), maybe_reread_subscription(), and ProcessSyncingTablesForApply().

◆ ApplyLauncherMain()

void ApplyLauncherMain ( Datum  main_arg)
extern

Definition at line 1204 of file launcher.c.

1205{
1207 (errmsg_internal("logical replication launcher started")));
1208
1210
1213
1214 /* Establish signal handlers. */
1217
1218 /*
1219 * Establish connection to nailed catalogs (we only ever access
1220 * pg_subscription).
1221 */
1223
1224 /*
1225 * Acquire the conflict detection slot at startup to ensure it can be
1226 * dropped if no longer needed after a restart.
1227 */
1229
1230 /* Enter main loop */
1231 for (;;)
1232 {
1233 int rc;
1234 List *sublist;
1235 ListCell *lc;
1239 bool can_update_xmin = true;
1240 bool retain_dead_tuples = false;
1242
1244
1245 /* Use temporary context to avoid leaking memory across cycles. */
1247 "Logical Replication Launcher sublist",
1250
1251 /*
1252 * Start any missing workers for enabled subscriptions.
1253 *
1254 * Also, during the iteration through all subscriptions, we compute
1255 * the minimum XID required to protect deleted tuples for conflict
1256 * detection if one of the subscription enables retain_dead_tuples
1257 * option.
1258 */
1260 foreach(lc, sublist)
1261 {
1262 Subscription *sub = (Subscription *) lfirst(lc);
1266 long elapsed;
1267
1268 if (sub->retaindeadtuples)
1269 {
1270 retain_dead_tuples = true;
1271
1272 /*
1273 * Create a replication slot to retain information necessary
1274 * for conflict detection such as dead tuples, commit
1275 * timestamps, and origins.
1276 *
1277 * The slot is created before starting the apply worker to
1278 * prevent it from unnecessarily maintaining its
1279 * oldest_nonremovable_xid.
1280 *
1281 * The slot is created even for a disabled subscription to
1282 * ensure that conflict-related information is available when
1283 * applying remote changes that occurred before the
1284 * subscription was enabled.
1285 */
1287
1288 if (sub->retentionactive)
1289 {
1290 /*
1291 * Can't advance xmin of the slot unless all the
1292 * subscriptions actively retaining dead tuples are
1293 * enabled. This is required to ensure that we don't
1294 * advance the xmin of CONFLICT_DETECTION_SLOT if one of
1295 * the subscriptions is not enabled. Otherwise, we won't
1296 * be able to detect conflicts reliably for such a
1297 * subscription even though it has set the
1298 * retain_dead_tuples option.
1299 */
1300 can_update_xmin &= sub->enabled;
1301
1302 /*
1303 * Initialize the slot once the subscription activates
1304 * retention.
1305 */
1308 }
1309 }
1310
1311 if (!sub->enabled)
1312 continue;
1313
1316 false);
1317
1318 if (w != NULL)
1319 {
1320 /*
1321 * Compute the minimum xmin required to protect dead tuples
1322 * required for conflict detection among all running apply
1323 * workers. This computation is performed while holding
1324 * LogicalRepWorkerLock to prevent accessing invalid worker
1325 * data, in scenarios where a worker might exit and reset its
1326 * state concurrently.
1327 */
1328 if (sub->retaindeadtuples &&
1329 sub->retentionactive &&
1332
1334
1335 /* worker is running already */
1336 continue;
1337 }
1338
1340
1341 /*
1342 * Can't advance xmin of the slot unless all the workers
1343 * corresponding to subscriptions actively retaining dead tuples
1344 * are running, disabling the further computation of the minimum
1345 * nonremovable xid.
1346 */
1347 if (sub->retaindeadtuples && sub->retentionactive)
1348 can_update_xmin = false;
1349
1350 /*
1351 * If the worker is eligible to start now, launch it. Otherwise,
1352 * adjust wait_time so that we'll wake up as soon as it can be
1353 * started.
1354 *
1355 * Each subscription's apply worker can only be restarted once per
1356 * wal_retrieve_retry_interval, so that errors do not cause us to
1357 * repeatedly restart the worker as fast as possible. In cases
1358 * where a restart is expected (e.g., subscription parameter
1359 * changes), another process should remove the last-start entry
1360 * for the subscription so that the worker can be restarted
1361 * without waiting for wal_retrieve_retry_interval to elapse.
1362 */
1365 if (last_start == 0 ||
1367 {
1370 sub->dbid, sub->oid, sub->name,
1371 sub->owner, InvalidOid,
1373 sub->retaindeadtuples &&
1374 sub->retentionactive))
1375 {
1376 /*
1377 * We get here either if we failed to launch a worker
1378 * (perhaps for resource-exhaustion reasons) or if we
1379 * launched one but it immediately quit. Either way, it
1380 * seems appropriate to try again after
1381 * wal_retrieve_retry_interval.
1382 */
1385 }
1386 }
1387 else
1388 {
1391 }
1392 }
1393
1394 /*
1395 * Drop the CONFLICT_DETECTION_SLOT slot if there is no subscription
1396 * that requires us to retain dead tuples. Otherwise, if required,
1397 * advance the slot's xmin to protect dead tuples required for the
1398 * conflict detection.
1399 *
1400 * Additionally, if all apply workers for subscriptions with
1401 * retain_dead_tuples enabled have requested to stop retention, the
1402 * slot's xmin will be set to InvalidTransactionId allowing the
1403 * removal of dead tuples.
1404 */
1406 {
1407 if (!retain_dead_tuples)
1409 else if (can_update_xmin)
1411 }
1412
1413 /* Switch back to original memory context. */
1415 /* Clean the temporary memory. */
1417
1418 /* Wait for more work. */
1419 rc = WaitLatch(MyLatch,
1421 wait_time,
1423
1424 if (rc & WL_LATCH_SET)
1425 {
1428 }
1429
1431 {
1432 ConfigReloadPending = false;
1434 }
1435 }
1436
1437 /* Not reachable */
1438}
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition timestamp.c:1757
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1645
Datum now(PG_FUNCTION_ARGS)
Definition timestamp.c:1609
void BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
Definition bgworker.c:859
void BackgroundWorkerUnblockSignals(void)
Definition bgworker.c:933
#define Min(x, y)
Definition c.h:1019
#define Assert(condition)
Definition c.h:885
uint32 TransactionId
Definition c.h:678
int64 TimestampTz
Definition timestamp.h:39
#define DSM_HANDLE_INVALID
Definition dsm_impl.h:58
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define DEBUG1
Definition elog.h:30
#define ereport(elevel,...)
Definition elog.h:150
int MyProcPid
Definition globals.c:47
struct Latch * MyLatch
Definition globals.c:63
void ProcessConfigFile(GucContext context)
Definition guc-file.l:120
@ PGC_SIGHUP
Definition guc.h:75
volatile sig_atomic_t ConfigReloadPending
Definition interrupt.c:27
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition interrupt.c:61
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:344
void ResetLatch(Latch *latch)
Definition latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition latch.c:172
#define DEFAULT_NAPTIME_PER_CYCLE
Definition launcher.c:49
bool logicalrep_worker_launch(LogicalRepWorkerType wtype, Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid, dsm_handle subworker_dsm, bool retain_dead_tuples)
Definition launcher.c:324
static void ApplyLauncherSetWorkerStartTime(Oid subid, TimestampTz start_time)
Definition launcher.c:1112
LogicalRepWorker * logicalrep_worker_find(LogicalRepWorkerType wtype, Oid subid, Oid relid, bool only_running)
Definition launcher.c:258
static void update_conflict_slot_xmin(TransactionId new_xmin)
Definition launcher.c:1498
static void compute_min_nonremovable_xid(LogicalRepWorker *worker, TransactionId *xmin)
Definition launcher.c:1446
static void logicalrep_launcher_onexit(int code, Datum arg)
Definition launcher.c:859
void CreateConflictDetectionSlot(void)
Definition launcher.c:1567
static void init_conflict_slot_xmin(void)
Definition launcher.c:1534
static TimestampTz ApplyLauncherGetWorkerStartTime(Oid subid)
Definition launcher.c:1128
static LogicalRepCtxStruct * LogicalRepCtx
Definition launcher.c:71
static bool acquire_conflict_slot_if_exists(void)
Definition launcher.c:1484
static List * get_subscription_list(void)
Definition launcher.c:117
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1176
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1793
@ LW_SHARED
Definition lwlock.h:113
MemoryContext TopMemoryContext
Definition mcxt.c:166
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:472
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
#define lfirst(lc)
Definition pg_list.h:172
#define pqsignal
Definition port.h:547
uint64_t Datum
Definition postgres.h:70
#define InvalidOid
void ReplicationSlotDropAcquired(void)
Definition slot.c:1034
ReplicationSlot * MyReplicationSlot
Definition slot.c:148
Definition pg_list.h:54
ReplicationSlotPersistentData data
Definition slot.h:213
#define InvalidTransactionId
Definition transam.h:31
#define TransactionIdIsValid(xid)
Definition transam.h:41
#define WL_TIMEOUT
#define WL_EXIT_ON_PM_DEATH
#define WL_LATCH_SET
#define SIGHUP
Definition win32_port.h:158
@ WORKERTYPE_APPLY
int wal_retrieve_retry_interval
Definition xlog.c:137

References acquire_conflict_slot_if_exists(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, ApplyLauncherGetWorkerStartTime(), ApplyLauncherSetWorkerStartTime(), Assert, BackgroundWorkerInitializeConnection(), BackgroundWorkerUnblockSignals(), before_shmem_exit(), CHECK_FOR_INTERRUPTS, compute_min_nonremovable_xid(), ConfigReloadPending, CreateConflictDetectionSlot(), ReplicationSlot::data, Subscription::dbid, DEBUG1, DEFAULT_NAPTIME_PER_CYCLE, DSM_HANDLE_INVALID, Subscription::enabled, ereport, errmsg_internal(), fb(), get_subscription_list(), GetCurrentTimestamp(), init_conflict_slot_xmin(), InvalidOid, InvalidTransactionId, LogicalRepCtxStruct::launcher_pid, lfirst, logicalrep_launcher_onexit(), logicalrep_worker_find(), logicalrep_worker_launch(), LogicalRepCtx, LW_SHARED, LWLockAcquire(), LWLockRelease(), MemoryContextDelete(), MemoryContextSwitchTo(), Min, MyLatch, MyProcPid, MyReplicationSlot, Subscription::name, now(), Subscription::oid, Subscription::owner, PGC_SIGHUP, pqsignal, ProcessConfigFile(), ReplicationSlotDropAcquired(), ResetLatch(), Subscription::retaindeadtuples, Subscription::retentionactive, SIGHUP, SignalHandlerForConfigReload(), TimestampDifferenceMilliseconds(), TopMemoryContext, TransactionIdIsValid, update_conflict_slot_xmin(), WaitLatch(), wal_retrieve_retry_interval, WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_TIMEOUT, WORKERTYPE_APPLY, and ReplicationSlotPersistentData::xmin.

◆ ApplyLauncherRegister()

void ApplyLauncherRegister ( void  )
extern

Definition at line 997 of file launcher.c.

998{
1000
1001 /*
1002 * The logical replication launcher is disabled during binary upgrades, to
1003 * prevent logical replication workers from running on the source cluster.
1004 * That could cause replication origins to move forward after having been
1005 * copied to the target cluster, potentially creating conflicts with the
1006 * copied data files.
1007 */
1009 return;
1010
1011 memset(&bgw, 0, sizeof(bgw));
1012 bgw.bgw_flags = BGWORKER_SHMEM_ACCESS |
1014 bgw.bgw_start_time = BgWorkerStart_RecoveryFinished;
1015 snprintf(bgw.bgw_library_name, MAXPGPATH, "postgres");
1016 snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
1017 snprintf(bgw.bgw_name, BGW_MAXLEN,
1018 "logical replication launcher");
1019 snprintf(bgw.bgw_type, BGW_MAXLEN,
1020 "logical replication launcher");
1021 bgw.bgw_restart_time = 5;
1022 bgw.bgw_notify_pid = 0;
1023 bgw.bgw_main_arg = (Datum) 0;
1024
1026}
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition bgworker.c:946
@ BgWorkerStart_RecoveryFinished
Definition bgworker.h:88
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition bgworker.h:60
#define BGWORKER_SHMEM_ACCESS
Definition bgworker.h:53
#define BGW_MAXLEN
Definition bgworker.h:93
bool IsBinaryUpgrade
Definition globals.c:121
int max_logical_replication_workers
Definition launcher.c:52
#define MAXPGPATH
#define snprintf
Definition port.h:260

References BGW_MAXLEN, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_SHMEM_ACCESS, BgWorkerStart_RecoveryFinished, fb(), IsBinaryUpgrade, max_logical_replication_workers, MAXPGPATH, RegisterBackgroundWorker(), and snprintf.

Referenced by PostmasterMain().

◆ ApplyLauncherShmemInit()

void ApplyLauncherShmemInit ( void  )
extern

Definition at line 1033 of file launcher.c.

1034{
1035 bool found;
1036
1038 ShmemInitStruct("Logical Replication Launcher Data",
1040 &found);
1041
1042 if (!found)
1043 {
1044 int slot;
1045
1047
1050
1051 /* Initialize memory and spin locks for each worker slot. */
1052 for (slot = 0; slot < max_logical_replication_workers; slot++)
1053 {
1054 LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
1055
1056 memset(worker, 0, sizeof(LogicalRepWorker));
1057 SpinLockInit(&worker->relmutex);
1058 }
1059 }
1060}
#define DSA_HANDLE_INVALID
Definition dsa.h:139
#define DSHASH_HANDLE_INVALID
Definition dshash.h:27
Size ApplyLauncherShmemSize(void)
Definition launcher.c:978
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition shmem.c:378
static void SpinLockInit(volatile slock_t *lock)
Definition spin.h:50
dsa_handle last_start_dsa
Definition launcher.c:64
dshash_table_handle last_start_dsh
Definition launcher.c:65
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition launcher.c:68

References ApplyLauncherShmemSize(), DSA_HANDLE_INVALID, DSHASH_HANDLE_INVALID, fb(), LogicalRepCtxStruct::last_start_dsa, LogicalRepCtxStruct::last_start_dsh, LogicalRepCtx, max_logical_replication_workers, LogicalRepWorker::relmutex, ShmemInitStruct(), SpinLockInit(), and LogicalRepCtxStruct::workers.

Referenced by CreateOrAttachShmemStructs().

◆ ApplyLauncherShmemSize()

Size ApplyLauncherShmemSize ( void  )
extern

Definition at line 978 of file launcher.c.

979{
980 Size size;
981
982 /*
983 * Need the fixed struct and the array of LogicalRepWorker.
984 */
985 size = sizeof(LogicalRepCtxStruct);
986 size = MAXALIGN(size);
988 sizeof(LogicalRepWorker)));
989 return size;
990}
#define MAXALIGN(LEN)
Definition c.h:838
size_t Size
Definition c.h:631
Size add_size(Size s1, Size s2)
Definition shmem.c:482
Size mul_size(Size s1, Size s2)
Definition shmem.c:497

References add_size(), max_logical_replication_workers, MAXALIGN, and mul_size().

Referenced by ApplyLauncherShmemInit(), and CalculateShmemSize().

◆ ApplyLauncherWakeup()

void ApplyLauncherWakeup ( void  )
extern

Definition at line 1194 of file launcher.c.

1195{
1196 if (LogicalRepCtx->launcher_pid != 0)
1198}
#define kill(pid, sig)
Definition win32_port.h:490
#define SIGUSR1
Definition win32_port.h:170

References kill, LogicalRepCtxStruct::launcher_pid, LogicalRepCtx, and SIGUSR1.

Referenced by AtEOXact_ApplyLauncher(), logicalrep_worker_onexit(), update_retention_status(), and wait_for_local_flush().

◆ ApplyLauncherWakeupAtCommit()

void ApplyLauncherWakeupAtCommit ( void  )
extern

Definition at line 1184 of file launcher.c.

1185{
1188}
static bool on_commit_launcher_wakeup
Definition launcher.c:93

References on_commit_launcher_wakeup.

Referenced by AlterSubscription(), AlterSubscriptionOwner_internal(), and CreateSubscription().

◆ AtEOXact_ApplyLauncher()

void AtEOXact_ApplyLauncher ( bool  isCommit)
extern

Definition at line 1165 of file launcher.c.

1166{
1167 if (isCommit)
1168 {
1171 }
1172
1174}
void ApplyLauncherWakeup(void)
Definition launcher.c:1194

References ApplyLauncherWakeup(), fb(), and on_commit_launcher_wakeup.

Referenced by AbortTransaction(), CommitTransaction(), and PrepareTransaction().

◆ CreateConflictDetectionSlot()

void CreateConflictDetectionSlot ( void  )
extern

Definition at line 1567 of file launcher.c.

1568{
1569 /* Exit early, if the replication slot is already created and acquired */
1571 return;
1572
1573 ereport(LOG,
1574 errmsg("creating replication conflict detection slot"));
1575
1577 false, false);
1578
1580}
int errmsg(const char *fmt,...)
Definition elog.c:1093
#define LOG
Definition elog.h:31
void ReplicationSlotCreate(const char *name, bool db_specific, ReplicationSlotPersistency persistency, bool two_phase, bool failover, bool synced)
Definition slot.c:379
#define CONFLICT_DETECTION_SLOT
Definition slot.h:28
@ RS_PERSISTENT
Definition slot.h:45

References CONFLICT_DETECTION_SLOT, ereport, errmsg(), init_conflict_slot_xmin(), LOG, MyReplicationSlot, ReplicationSlotCreate(), and RS_PERSISTENT.

Referenced by ApplyLauncherMain(), and binary_upgrade_create_conflict_detection_slot().

◆ GetLeaderApplyWorkerPid()

pid_t GetLeaderApplyWorkerPid ( pid_t  pid)
extern

Definition at line 1596 of file launcher.c.

1597{
1598 int leader_pid = InvalidPid;
1599 int i;
1600
1602
1603 for (i = 0; i < max_logical_replication_workers; i++)
1604 {
1606
1607 if (isParallelApplyWorker(w) && w->proc && pid == w->proc->pid)
1608 {
1609 leader_pid = w->leader_pid;
1610 break;
1611 }
1612 }
1613
1615
1616 return leader_pid;
1617}
int i
Definition isn.c:77
#define InvalidPid
Definition miscadmin.h:32
int pid
Definition proc.h:189
#define isParallelApplyWorker(worker)

References fb(), i, InvalidPid, isParallelApplyWorker, LogicalRepWorker::leader_pid, LogicalRepCtx, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_logical_replication_workers, PGPROC::pid, LogicalRepWorker::proc, and LogicalRepCtxStruct::workers.

Referenced by pg_stat_get_activity().

◆ IsLogicalLauncher()

bool IsLogicalLauncher ( void  )
extern

Variable Documentation

◆ max_logical_replication_workers

◆ max_parallel_apply_workers_per_subscription

PGDLLIMPORT int max_parallel_apply_workers_per_subscription
extern

Definition at line 54 of file launcher.c.

Referenced by logicalrep_worker_launch(), and pa_free_worker().

◆ max_sync_workers_per_subscription

PGDLLIMPORT int max_sync_workers_per_subscription
extern

Definition at line 53 of file launcher.c.

Referenced by launch_sync_worker(), and logicalrep_worker_launch().