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

1076 {
1078 
1079  (void) dshash_delete_key(last_start_times, &subid);
1080 }
bool dshash_delete_key(dshash_table *hash_table, const void *key)
Definition: dshash.c:503
static dshash_table * last_start_times
Definition: launcher.c:89
static void logicalrep_launcher_attach_dshmem(void)
Definition: launcher.c:989

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

1123 {
1124  ereport(DEBUG1,
1125  (errmsg_internal("logical replication launcher started")));
1126 
1128 
1131 
1132  /* Establish signal handlers. */
1134  pqsignal(SIGTERM, die);
1136 
1137  /*
1138  * Establish connection to nailed catalogs (we only ever access
1139  * pg_subscription).
1140  */
1141  BackgroundWorkerInitializeConnection(NULL, NULL, 0);
1142 
1143  /* Enter main loop */
1144  for (;;)
1145  {
1146  int rc;
1147  List *sublist;
1148  ListCell *lc;
1149  MemoryContext subctx;
1150  MemoryContext oldctx;
1151  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
1152 
1154 
1155  /* Use temporary context to avoid leaking memory across cycles. */
1157  "Logical Replication Launcher sublist",
1159  oldctx = MemoryContextSwitchTo(subctx);
1160 
1161  /* Start any missing workers for enabled subscriptions. */
1162  sublist = get_subscription_list();
1163  foreach(lc, sublist)
1164  {
1165  Subscription *sub = (Subscription *) lfirst(lc);
1166  LogicalRepWorker *w;
1167  TimestampTz last_start;
1168  TimestampTz now;
1169  long elapsed;
1170 
1171  if (!sub->enabled)
1172  continue;
1173 
1174  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1175  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
1176  LWLockRelease(LogicalRepWorkerLock);
1177 
1178  if (w != NULL)
1179  continue; /* worker is running already */
1180 
1181  /*
1182  * If the worker is eligible to start now, launch it. Otherwise,
1183  * adjust wait_time so that we'll wake up as soon as it can be
1184  * started.
1185  *
1186  * Each subscription's apply worker can only be restarted once per
1187  * wal_retrieve_retry_interval, so that errors do not cause us to
1188  * repeatedly restart the worker as fast as possible. In cases
1189  * where a restart is expected (e.g., subscription parameter
1190  * changes), another process should remove the last-start entry
1191  * for the subscription so that the worker can be restarted
1192  * without waiting for wal_retrieve_retry_interval to elapse.
1193  */
1194  last_start = ApplyLauncherGetWorkerStartTime(sub->oid);
1196  if (last_start == 0 ||
1198  {
1201  sub->dbid, sub->oid, sub->name,
1202  sub->owner, InvalidOid,
1204  }
1205  else
1206  {
1207  wait_time = Min(wait_time,
1208  wal_retrieve_retry_interval - elapsed);
1209  }
1210  }
1211 
1212  /* Switch back to original memory context. */
1213  MemoryContextSwitchTo(oldctx);
1214  /* Clean the temporary memory. */
1215  MemoryContextDelete(subctx);
1216 
1217  /* Wait for more work. */
1218  rc = WaitLatch(MyLatch,
1220  wait_time,
1221  WAIT_EVENT_LOGICAL_LAUNCHER_MAIN);
1222 
1223  if (rc & WL_LATCH_SET)
1224  {
1227  }
1228 
1229  if (ConfigReloadPending)
1230  {
1231  ConfigReloadPending = false;
1233  }
1234  }
1235 
1236  /* Not reachable */
1237 }
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition: timestamp.c:1766
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1654
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1618
#define Min(x, y)
Definition: c.h:1004
#define Assert(condition)
Definition: c.h:858
int64 TimestampTz
Definition: timestamp.h:39
#define DSM_HANDLE_INVALID
Definition: dsm_impl.h:58
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1159
#define DEBUG1
Definition: elog.h:30
#define ereport(elevel,...)
Definition: elog.h:149
int MyProcPid
Definition: globals.c:45
struct Latch * MyLatch
Definition: globals.c:60
@ 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:724
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:517
#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:300
#define DEFAULT_NAPTIME_PER_CYCLE
Definition: launcher.c:47
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:243
static void ApplyLauncherSetWorkerStartTime(Oid subid, TimestampTz start_time)
Definition: launcher.c:1033
static List * get_subscription_list(void)
Definition: launcher.c:112
static void logicalrep_launcher_onexit(int code, Datum arg)
Definition: launcher.c:807
static TimestampTz ApplyLauncherGetWorkerStartTime(Oid subid)
Definition: launcher.c:1049
static LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:69
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1170
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1783
@ LW_SHARED
Definition: lwlock.h:115
MemoryContext TopMemoryContext
Definition: mcxt.c:149
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
#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:4155
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:4229
MemoryContextSwitchTo(old_ctx)
Definition: pg_list.h:54
#define SIGHUP
Definition: win32_port.h:168
@ WORKERTYPE_APPLY
int wal_retrieve_retry_interval
Definition: xlog.c:134

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

919 {
920  BackgroundWorker bgw;
921 
922  /*
923  * The logical replication launcher is disabled during binary upgrades, to
924  * prevent logical replication workers from running on the source cluster.
925  * That could cause replication origins to move forward after having been
926  * copied to the target cluster, potentially creating conflicts with the
927  * copied data files.
928  */
930  return;
931 
932  memset(&bgw, 0, sizeof(bgw));
936  snprintf(bgw.bgw_library_name, MAXPGPATH, "postgres");
937  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
939  "logical replication launcher");
941  "logical replication launcher");
942  bgw.bgw_restart_time = 5;
943  bgw.bgw_notify_pid = 0;
944  bgw.bgw_main_arg = (Datum) 0;
945 
947 }
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:862
@ 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
bool IsBinaryUpgrade
Definition: globals.c:118
int max_logical_replication_workers
Definition: launcher.c:50
#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, IsBinaryUpgrade, max_logical_replication_workers, MAXPGPATH, RegisterBackgroundWorker(), and snprintf.

Referenced by PostmasterMain().

◆ ApplyLauncherShmemInit()

void ApplyLauncherShmemInit ( void  )

Definition at line 954 of file launcher.c.

955 {
956  bool found;
957 
959  ShmemInitStruct("Logical Replication Launcher Data",
961  &found);
962 
963  if (!found)
964  {
965  int slot;
966 
968 
971 
972  /* Initialize memory and spin locks for each worker slot. */
973  for (slot = 0; slot < max_logical_replication_workers; slot++)
974  {
975  LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
976 
977  memset(worker, 0, sizeof(LogicalRepWorker));
978  SpinLockInit(&worker->relmutex);
979  }
980  }
981 }
#define DSA_HANDLE_INVALID
Definition: dsa.h:139
#define DSHASH_HANDLE_INVALID
Definition: dshash.h:27
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:899
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:387
#define SpinLockInit(lock)
Definition: spin.h:60
dsa_handle last_start_dsa
Definition: launcher.c:62
dshash_table_handle last_start_dsh
Definition: launcher.c:63
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:66

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

900 {
901  Size size;
902 
903  /*
904  * Need the fixed struct and the array of LogicalRepWorker.
905  */
906  size = sizeof(LogicalRepCtxStruct);
907  size = MAXALIGN(size);
909  sizeof(LogicalRepWorker)));
910  return size;
911 }
#define MAXALIGN(LEN)
Definition: c.h:811
size_t Size
Definition: c.h:605
struct LogicalRepCtxStruct LogicalRepCtxStruct
Size add_size(Size s1, Size s2)
Definition: shmem.c:493
Size mul_size(Size s1, Size s2)
Definition: shmem.c:510
static pg_noinline void Size size
Definition: slab.c:607

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

Referenced by ApplyLauncherShmemInit(), and CalculateShmemSize().

◆ ApplyLauncherWakeupAtCommit()

void ApplyLauncherWakeupAtCommit ( void  )

Definition at line 1105 of file launcher.c.

1106 {
1109 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:91

References on_commit_launcher_wakeup.

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

◆ AtEOXact_ApplyLauncher()

void AtEOXact_ApplyLauncher ( bool  isCommit)

Definition at line 1086 of file launcher.c.

1087 {
1088  if (isCommit)
1089  {
1092  }
1093 
1094  on_commit_launcher_wakeup = false;
1095 }
static void ApplyLauncherWakeup(void)
Definition: launcher.c:1112

References ApplyLauncherWakeup(), and on_commit_launcher_wakeup.

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

◆ GetLeaderApplyWorkerPid()

pid_t GetLeaderApplyWorkerPid ( pid_t  pid)

Definition at line 1253 of file launcher.c.

1254 {
1255  int leader_pid = InvalidPid;
1256  int i;
1257 
1258  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1259 
1260  for (i = 0; i < max_logical_replication_workers; i++)
1261  {
1263 
1264  if (isParallelApplyWorker(w) && w->proc && pid == w->proc->pid)
1265  {
1266  leader_pid = w->leader_pid;
1267  break;
1268  }
1269  }
1270 
1271  LWLockRelease(LogicalRepWorkerLock);
1272 
1273  return leader_pid;
1274 }
int i
Definition: isn.c:73
#define InvalidPid
Definition: miscadmin.h:32
int pid
Definition: proc.h:178
#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 1243 of file launcher.c.

1244 {
1246 }

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

Referenced by logicalrep_worker_launch(), and process_syncing_tables_for_apply().