PostgreSQL Source Code  git master
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 AtEOXact_ApplyLauncher (bool isCommit)
 
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)

Definition at line 1031 of file launcher.c.

1032 {
1034 
1035  (void) dshash_delete_key(last_start_times, &subid);
1036 }
bool dshash_delete_key(dshash_table *hash_table, const void *key)
Definition: dshash.c:503
static dshash_table * last_start_times
Definition: launcher.c:95
static void logicalrep_launcher_attach_dshmem(void)
Definition: launcher.c:945

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

Referenced by apply_worker_exit(), DisableSubscriptionAndExit(), DropSubscription(), InitializeApplyWorker(), maybe_reread_subscription(), and process_syncing_tables_for_apply().

◆ ApplyLauncherMain()

void ApplyLauncherMain ( Datum  main_arg)

Definition at line 1078 of file launcher.c.

1079 {
1080  ereport(DEBUG1,
1081  (errmsg_internal("logical replication launcher started")));
1082 
1084 
1087 
1088  /* Establish signal handlers. */
1090  pqsignal(SIGTERM, die);
1092 
1093  /*
1094  * Establish connection to nailed catalogs (we only ever access
1095  * pg_subscription).
1096  */
1097  BackgroundWorkerInitializeConnection(NULL, NULL, 0);
1098 
1099  /* Enter main loop */
1100  for (;;)
1101  {
1102  int rc;
1103  List *sublist;
1104  ListCell *lc;
1105  MemoryContext subctx;
1106  MemoryContext oldctx;
1107  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
1108 
1110 
1111  /* Use temporary context to avoid leaking memory across cycles. */
1113  "Logical Replication Launcher sublist",
1115  oldctx = MemoryContextSwitchTo(subctx);
1116 
1117  /* Start any missing workers for enabled subscriptions. */
1118  sublist = get_subscription_list();
1119  foreach(lc, sublist)
1120  {
1121  Subscription *sub = (Subscription *) lfirst(lc);
1122  LogicalRepWorker *w;
1123  TimestampTz last_start;
1124  TimestampTz now;
1125  long elapsed;
1126 
1127  if (!sub->enabled)
1128  continue;
1129 
1130  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1131  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
1132  LWLockRelease(LogicalRepWorkerLock);
1133 
1134  if (w != NULL)
1135  continue; /* worker is running already */
1136 
1137  /*
1138  * If the worker is eligible to start now, launch it. Otherwise,
1139  * adjust wait_time so that we'll wake up as soon as it can be
1140  * started.
1141  *
1142  * Each subscription's apply worker can only be restarted once per
1143  * wal_retrieve_retry_interval, so that errors do not cause us to
1144  * repeatedly restart the worker as fast as possible. In cases
1145  * where a restart is expected (e.g., subscription parameter
1146  * changes), another process should remove the last-start entry
1147  * for the subscription so that the worker can be restarted
1148  * without waiting for wal_retrieve_retry_interval to elapse.
1149  */
1150  last_start = ApplyLauncherGetWorkerStartTime(sub->oid);
1152  if (last_start == 0 ||
1154  {
1156  logicalrep_worker_launch(sub->dbid, sub->oid, sub->name,
1157  sub->owner, InvalidOid,
1159  }
1160  else
1161  {
1162  wait_time = Min(wait_time,
1163  wal_retrieve_retry_interval - elapsed);
1164  }
1165  }
1166 
1167  /* Switch back to original memory context. */
1168  MemoryContextSwitchTo(oldctx);
1169  /* Clean the temporary memory. */
1170  MemoryContextDelete(subctx);
1171 
1172  /* Wait for more work. */
1173  rc = WaitLatch(MyLatch,
1175  wait_time,
1177 
1178  if (rc & WL_LATCH_SET)
1179  {
1182  }
1183 
1184  if (ConfigReloadPending)
1185  {
1186  ConfigReloadPending = false;
1188  }
1189  }
1190 
1191  /* Not reachable */
1192 }
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition: timestamp.c:1703
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1582
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1546
#define Min(x, y)
Definition: c.h:988
int64 TimestampTz
Definition: timestamp.h:39
#define DSM_HANDLE_INVALID
Definition: dsm_impl.h:58
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1156
#define DEBUG1
Definition: elog.h:30
#define ereport(elevel,...)
Definition: elog.h:149
int MyProcPid
Definition: globals.c:44
struct Latch * MyLatch
Definition: globals.c:58
@ PGC_SIGHUP
Definition: guc.h:71
void ProcessConfigFile(GucContext context)
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:333
void ResetLatch(Latch *latch)
Definition: latch.c:699
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:492
#define WL_TIMEOUT
Definition: latch.h:128
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130
#define WL_LATCH_SET
Definition: latch.h:125
#define DEFAULT_NAPTIME_PER_CYCLE
Definition: launcher.c:54
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:249
static void ApplyLauncherSetWorkerStartTime(Oid subid, TimestampTz start_time)
Definition: launcher.c:989
static List * get_subscription_list(void)
Definition: launcher.c:118
bool logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid, dsm_handle subworker_dsm)
Definition: launcher.c:306
static void logicalrep_launcher_onexit(int code, Datum arg)
Definition: launcher.c:773
static TimestampTz ApplyLauncherGetWorkerStartTime(Oid subid)
Definition: launcher.c:1005
static LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:76
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1195
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
@ LW_SHARED
Definition: lwlock.h:116
MemoryContext TopMemoryContext
Definition: mcxt.c:141
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:387
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
#define lfirst(lc)
Definition: pg_list.h:172
#define die(msg)
Definition: pg_test_fsync.c:95
pqsigfunc pqsignal(int signo, pqsigfunc func)
uintptr_t Datum
Definition: postgres.h:64
#define InvalidOid
Definition: postgres_ext.h:36
void BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
Definition: postmaster.c:5600
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5660
Definition: pg_list.h:54
@ WAIT_EVENT_LOGICAL_LAUNCHER_MAIN
Definition: wait_event.h:44
#define SIGHUP
Definition: win32_port.h:176
int wal_retrieve_retry_interval
Definition: xlog.c:137

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, ApplyLauncherGetWorkerStartTime(), ApplyLauncherSetWorkerStartTime(), Assert(), BackgroundWorkerInitializeConnection(), BackgroundWorkerUnblockSignals(), before_shmem_exit(), CHECK_FOR_INTERRUPTS, ConfigReloadPending, Subscription::dbid, DEBUG1, DEFAULT_NAPTIME_PER_CYCLE, die, DSM_HANDLE_INVALID, Subscription::enabled, ereport, errmsg_internal(), get_subscription_list(), GetCurrentTimestamp(), InvalidOid, LogicalRepCtxStruct::launcher_pid, lfirst, logicalrep_launcher_onexit(), logicalrep_worker_find(), logicalrep_worker_launch(), LogicalRepCtx, LW_SHARED, LWLockAcquire(), LWLockRelease(), MemoryContextDelete(), MemoryContextSwitchTo(), Min, MyLatch, MyProcPid, Subscription::name, now(), Subscription::oid, Subscription::owner, PGC_SIGHUP, pqsignal(), ProcessConfigFile(), ResetLatch(), SIGHUP, SignalHandlerForConfigReload(), TimestampDifferenceMilliseconds(), TopMemoryContext, WAIT_EVENT_LOGICAL_LAUNCHER_MAIN, WaitLatch(), wal_retrieve_retry_interval, WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and WL_TIMEOUT.

◆ ApplyLauncherRegister()

void ApplyLauncherRegister ( void  )

Definition at line 881 of file launcher.c.

882 {
883  BackgroundWorker bgw;
884 
886  return;
887 
888  memset(&bgw, 0, sizeof(bgw));
892  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
893  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
895  "logical replication launcher");
897  "logical replication launcher");
898  bgw.bgw_restart_time = 5;
899  bgw.bgw_notify_pid = 0;
900  bgw.bgw_main_arg = (Datum) 0;
901 
903 }
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:875
@ BgWorkerStart_RecoveryFinished
Definition: bgworker.h:81
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:60
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:53
#define BGW_MAXLEN
Definition: bgworker.h:86
int max_logical_replication_workers
Definition: launcher.c:57
#define snprintf
Definition: port.h:238
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:97
Datum bgw_main_arg
Definition: bgworker.h:98
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:91
int bgw_restart_time
Definition: bgworker.h:95
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:92
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:94
pid_t bgw_notify_pid
Definition: bgworker.h:100
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:96

References BackgroundWorker::bgw_flags, BackgroundWorker::bgw_function_name, BackgroundWorker::bgw_library_name, BackgroundWorker::bgw_main_arg, BGW_MAXLEN, BackgroundWorker::bgw_name, BackgroundWorker::bgw_notify_pid, BackgroundWorker::bgw_restart_time, BackgroundWorker::bgw_start_time, BackgroundWorker::bgw_type, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_SHMEM_ACCESS, BgWorkerStart_RecoveryFinished, max_logical_replication_workers, RegisterBackgroundWorker(), and snprintf.

Referenced by PostmasterMain().

◆ ApplyLauncherShmemInit()

void ApplyLauncherShmemInit ( void  )

Definition at line 910 of file launcher.c.

911 {
912  bool found;
913 
915  ShmemInitStruct("Logical Replication Launcher Data",
917  &found);
918 
919  if (!found)
920  {
921  int slot;
922 
924 
927 
928  /* Initialize memory and spin locks for each worker slot. */
929  for (slot = 0; slot < max_logical_replication_workers; slot++)
930  {
931  LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
932 
933  memset(worker, 0, sizeof(LogicalRepWorker));
934  SpinLockInit(&worker->relmutex);
935  }
936  }
937 }
#define DSA_HANDLE_INVALID
Definition: dsa.h:103
#define DSHASH_HANDLE_INVALID
Definition: dshash.h:27
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:862
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
#define SpinLockInit(lock)
Definition: spin.h:60
dsa_handle last_start_dsa
Definition: launcher.c:69
dshash_table_handle last_start_dsh
Definition: launcher.c:70
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:73

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

Referenced by CreateSharedMemoryAndSemaphores().

◆ ApplyLauncherShmemSize()

Size ApplyLauncherShmemSize ( void  )

Definition at line 862 of file launcher.c.

863 {
864  Size size;
865 
866  /*
867  * Need the fixed struct and the array of LogicalRepWorker.
868  */
869  size = sizeof(LogicalRepCtxStruct);
870  size = MAXALIGN(size);
872  sizeof(LogicalRepWorker)));
873  return size;
874 }
#define MAXALIGN(LEN)
Definition: c.h:795
size_t Size
Definition: c.h:589
struct LogicalRepCtxStruct LogicalRepCtxStruct
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519

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

Referenced by ApplyLauncherShmemInit(), and CalculateShmemSize().

◆ ApplyLauncherWakeupAtCommit()

void ApplyLauncherWakeupAtCommit ( void  )

Definition at line 1061 of file launcher.c.

1062 {
1065 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:97

References on_commit_launcher_wakeup.

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

◆ AtEOXact_ApplyLauncher()

void AtEOXact_ApplyLauncher ( bool  isCommit)

Definition at line 1042 of file launcher.c.

1043 {
1044  if (isCommit)
1045  {
1048  }
1049 
1050  on_commit_launcher_wakeup = false;
1051 }
static void ApplyLauncherWakeup(void)
Definition: launcher.c:1068

References ApplyLauncherWakeup(), and on_commit_launcher_wakeup.

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

◆ GetLeaderApplyWorkerPid()

pid_t GetLeaderApplyWorkerPid ( pid_t  pid)

Definition at line 1208 of file launcher.c.

1209 {
1210  int leader_pid = InvalidPid;
1211  int i;
1212 
1213  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1214 
1215  for (i = 0; i < max_logical_replication_workers; i++)
1216  {
1218 
1219  if (isParallelApplyWorker(w) && w->proc && pid == w->proc->pid)
1220  {
1221  leader_pid = w->leader_pid;
1222  break;
1223  }
1224  }
1225 
1226  LWLockRelease(LogicalRepWorkerLock);
1227 
1228  return leader_pid;
1229 }
int i
Definition: isn.c:73
#define InvalidPid
Definition: miscadmin.h:32
int pid
Definition: proc.h:186
#define isParallelApplyWorker(worker)

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

Definition at line 1198 of file launcher.c.

1199 {
1201 }

References LogicalRepCtxStruct::launcher_pid, LogicalRepCtx, and MyProcPid.

Referenced by ProcessInterrupts().

Variable Documentation

◆ max_logical_replication_workers

◆ max_parallel_apply_workers_per_subscription

PGDLLIMPORT int max_parallel_apply_workers_per_subscription
extern

Definition at line 59 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 58 of file launcher.c.

Referenced by logicalrep_worker_launch(), and process_syncing_tables_for_apply().