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 1074 of file launcher.c.

1075 {
1077 
1078  (void) dshash_delete_key(last_start_times, &subid);
1079 }
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:988

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

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

◆ ApplyLauncherMain()

void ApplyLauncherMain ( Datum  main_arg)

Definition at line 1121 of file launcher.c.

1122 {
1123  ereport(DEBUG1,
1124  (errmsg_internal("logical replication launcher started")));
1125 
1127 
1130 
1131  /* Establish signal handlers. */
1133  pqsignal(SIGTERM, die);
1135 
1136  /*
1137  * Establish connection to nailed catalogs (we only ever access
1138  * pg_subscription).
1139  */
1140  BackgroundWorkerInitializeConnection(NULL, NULL, 0);
1141 
1142  /* Enter main loop */
1143  for (;;)
1144  {
1145  int rc;
1146  List *sublist;
1147  ListCell *lc;
1148  MemoryContext subctx;
1149  MemoryContext oldctx;
1150  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
1151 
1153 
1154  /* Use temporary context to avoid leaking memory across cycles. */
1156  "Logical Replication Launcher sublist",
1158  oldctx = MemoryContextSwitchTo(subctx);
1159 
1160  /* Start any missing workers for enabled subscriptions. */
1161  sublist = get_subscription_list();
1162  foreach(lc, sublist)
1163  {
1164  Subscription *sub = (Subscription *) lfirst(lc);
1165  LogicalRepWorker *w;
1166  TimestampTz last_start;
1167  TimestampTz now;
1168  long elapsed;
1169 
1170  if (!sub->enabled)
1171  continue;
1172 
1173  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1174  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
1175  LWLockRelease(LogicalRepWorkerLock);
1176 
1177  if (w != NULL)
1178  continue; /* worker is running already */
1179 
1180  /*
1181  * If the worker is eligible to start now, launch it. Otherwise,
1182  * adjust wait_time so that we'll wake up as soon as it can be
1183  * started.
1184  *
1185  * Each subscription's apply worker can only be restarted once per
1186  * wal_retrieve_retry_interval, so that errors do not cause us to
1187  * repeatedly restart the worker as fast as possible. In cases
1188  * where a restart is expected (e.g., subscription parameter
1189  * changes), another process should remove the last-start entry
1190  * for the subscription so that the worker can be restarted
1191  * without waiting for wal_retrieve_retry_interval to elapse.
1192  */
1193  last_start = ApplyLauncherGetWorkerStartTime(sub->oid);
1195  if (last_start == 0 ||
1197  {
1200  sub->dbid, sub->oid, sub->name,
1201  sub->owner, InvalidOid,
1203  }
1204  else
1205  {
1206  wait_time = Min(wait_time,
1207  wal_retrieve_retry_interval - elapsed);
1208  }
1209  }
1210 
1211  /* Switch back to original memory context. */
1212  MemoryContextSwitchTo(oldctx);
1213  /* Clean the temporary memory. */
1214  MemoryContextDelete(subctx);
1215 
1216  /* Wait for more work. */
1217  rc = WaitLatch(MyLatch,
1219  wait_time,
1220  WAIT_EVENT_LOGICAL_LAUNCHER_MAIN);
1221 
1222  if (rc & WL_LATCH_SET)
1223  {
1226  }
1227 
1228  if (ConfigReloadPending)
1229  {
1230  ConfigReloadPending = false;
1232  }
1233  }
1234 
1235  /* Not reachable */
1236 }
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition: timestamp.c:1761
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1649
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1613
#define Min(x, y)
Definition: c.h:993
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:337
void ResetLatch(Latch *latch)
Definition: latch.c:725
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:518
#define WL_TIMEOUT
Definition: latch.h:130
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:132
#define WL_LATCH_SET
Definition: latch.h:127
bool logicalrep_worker_launch(LogicalRepWorkerType wtype, Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid, dsm_handle subworker_dsm)
Definition: launcher.c:306
#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:1032
static List * get_subscription_list(void)
Definition: launcher.c:118
static void logicalrep_launcher_onexit(int code, Datum arg)
Definition: launcher.c:813
static TimestampTz ApplyLauncherGetWorkerStartTime(Oid subid)
Definition: launcher.c:1048
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:1808
@ LW_SHARED
Definition: lwlock.h:117
MemoryContext TopMemoryContext
Definition: mcxt.c:141
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:403
#define AllocSetContextCreate
Definition: memutils.h:126
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:150
#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)
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:5527
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5601
Definition: pg_list.h:54
#define SIGHUP
Definition: win32_port.h:168
@ WORKERTYPE_APPLY
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, WaitLatch(), wal_retrieve_retry_interval, WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_TIMEOUT, and WORKERTYPE_APPLY.

◆ ApplyLauncherRegister()

void ApplyLauncherRegister ( void  )

Definition at line 924 of file launcher.c.

925 {
926  BackgroundWorker bgw;
927 
929  return;
930 
931  memset(&bgw, 0, sizeof(bgw));
935  snprintf(bgw.bgw_library_name, MAXPGPATH, "postgres");
936  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
938  "logical replication launcher");
940  "logical replication launcher");
941  bgw.bgw_restart_time = 5;
942  bgw.bgw_notify_pid = 0;
943  bgw.bgw_main_arg = (Datum) 0;
944 
946 }
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:853
@ 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 MAXPGPATH
#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[MAXPGPATH]
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, MAXPGPATH, RegisterBackgroundWorker(), and snprintf.

Referenced by PostmasterMain().

◆ ApplyLauncherShmemInit()

void ApplyLauncherShmemInit ( void  )

Definition at line 953 of file launcher.c.

954 {
955  bool found;
956 
958  ShmemInitStruct("Logical Replication Launcher Data",
960  &found);
961 
962  if (!found)
963  {
964  int slot;
965 
967 
970 
971  /* Initialize memory and spin locks for each worker slot. */
972  for (slot = 0; slot < max_logical_replication_workers; slot++)
973  {
974  LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
975 
976  memset(worker, 0, sizeof(LogicalRepWorker));
977  SpinLockInit(&worker->relmutex);
978  }
979  }
980 }
#define DSA_HANDLE_INVALID
Definition: dsa.h:103
#define DSHASH_HANDLE_INVALID
Definition: dshash.h:27
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:905
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 CreateOrAttachShmemStructs().

◆ ApplyLauncherShmemSize()

Size ApplyLauncherShmemSize ( void  )

Definition at line 905 of file launcher.c.

906 {
907  Size size;
908 
909  /*
910  * Need the fixed struct and the array of LogicalRepWorker.
911  */
912  size = sizeof(LogicalRepCtxStruct);
913  size = MAXALIGN(size);
915  sizeof(LogicalRepWorker)));
916  return size;
917 }
#define MAXALIGN(LEN)
Definition: c.h:800
size_t Size
Definition: c.h:594
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 1104 of file launcher.c.

1105 {
1108 }
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 1085 of file launcher.c.

1086 {
1087  if (isCommit)
1088  {
1091  }
1092 
1093  on_commit_launcher_wakeup = false;
1094 }
static void ApplyLauncherWakeup(void)
Definition: launcher.c:1111

References ApplyLauncherWakeup(), and on_commit_launcher_wakeup.

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

◆ GetLeaderApplyWorkerPid()

pid_t GetLeaderApplyWorkerPid ( pid_t  pid)

Definition at line 1252 of file launcher.c.

1253 {
1254  int leader_pid = InvalidPid;
1255  int i;
1256 
1257  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1258 
1259  for (i = 0; i < max_logical_replication_workers; i++)
1260  {
1262 
1263  if (isParallelApplyWorker(w) && w->proc && pid == w->proc->pid)
1264  {
1265  leader_pid = w->leader_pid;
1266  break;
1267  }
1268  }
1269 
1270  LWLockRelease(LogicalRepWorkerLock);
1271 
1272  return leader_pid;
1273 }
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 1242 of file launcher.c.

1243 {
1245 }

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