PostgreSQL Source Code  git master
launcher.c File Reference
#include "postgres.h"
#include "access/heapam.h"
#include "access/htup.h"
#include "access/htup_details.h"
#include "access/tableam.h"
#include "access/xact.h"
#include "catalog/pg_subscription.h"
#include "catalog/pg_subscription_rel.h"
#include "funcapi.h"
#include "lib/dshash.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/bgworker.h"
#include "postmaster/interrupt.h"
#include "replication/logicallauncher.h"
#include "replication/slot.h"
#include "replication/walreceiver.h"
#include "replication/worker_internal.h"
#include "storage/ipc.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#include "utils/pg_lsn.h"
#include "utils/snapmgr.h"
Include dependency graph for launcher.c:

Go to the source code of this file.

Data Structures

struct  LogicalRepCtxStruct
 
struct  LauncherLastStartTimesEntry
 

Macros

#define DEFAULT_NAPTIME_PER_CYCLE   180000L
 
#define PG_STAT_GET_SUBSCRIPTION_COLS   10
 

Typedefs

typedef struct LogicalRepCtxStruct LogicalRepCtxStruct
 
typedef struct LauncherLastStartTimesEntry LauncherLastStartTimesEntry
 

Functions

static void ApplyLauncherWakeup (void)
 
static void logicalrep_launcher_onexit (int code, Datum arg)
 
static void logicalrep_worker_onexit (int code, Datum arg)
 
static void logicalrep_worker_detach (void)
 
static void logicalrep_worker_cleanup (LogicalRepWorker *worker)
 
static int logicalrep_pa_worker_count (Oid subid)
 
static void logicalrep_launcher_attach_dshmem (void)
 
static void ApplyLauncherSetWorkerStartTime (Oid subid, TimestampTz start_time)
 
static TimestampTz ApplyLauncherGetWorkerStartTime (Oid subid)
 
static Listget_subscription_list (void)
 
static bool WaitForReplicationWorkerAttach (LogicalRepWorker *worker, uint16 generation, BackgroundWorkerHandle *handle)
 
LogicalRepWorkerlogicalrep_worker_find (Oid subid, Oid relid, bool only_running)
 
Listlogicalrep_workers_find (Oid subid, bool only_running, bool acquire_lock)
 
bool logicalrep_worker_launch (LogicalRepWorkerType wtype, Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid, dsm_handle subworker_dsm)
 
static void logicalrep_worker_stop_internal (LogicalRepWorker *worker, int signo)
 
void logicalrep_worker_stop (Oid subid, Oid relid)
 
void logicalrep_pa_worker_stop (ParallelApplyWorkerInfo *winfo)
 
void logicalrep_worker_wakeup (Oid subid, Oid relid)
 
void logicalrep_worker_wakeup_ptr (LogicalRepWorker *worker)
 
void logicalrep_worker_attach (int slot)
 
int logicalrep_sync_worker_count (Oid subid)
 
Size ApplyLauncherShmemSize (void)
 
void ApplyLauncherRegister (void)
 
void ApplyLauncherShmemInit (void)
 
void ApplyLauncherForgetWorkerStartTime (Oid subid)
 
void AtEOXact_ApplyLauncher (bool isCommit)
 
void ApplyLauncherWakeupAtCommit (void)
 
void ApplyLauncherMain (Datum main_arg)
 
bool IsLogicalLauncher (void)
 
pid_t GetLeaderApplyWorkerPid (pid_t pid)
 
Datum pg_stat_get_subscription (PG_FUNCTION_ARGS)
 

Variables

int max_logical_replication_workers = 4
 
int max_sync_workers_per_subscription = 2
 
int max_parallel_apply_workers_per_subscription = 2
 
LogicalRepWorkerMyLogicalRepWorker = NULL
 
static LogicalRepCtxStructLogicalRepCtx
 
static const dshash_parameters dsh_params
 
static dsa_arealast_start_times_dsa = NULL
 
static dshash_tablelast_start_times = NULL
 
static bool on_commit_launcher_wakeup = false
 

Macro Definition Documentation

◆ DEFAULT_NAPTIME_PER_CYCLE

#define DEFAULT_NAPTIME_PER_CYCLE   180000L

Definition at line 47 of file launcher.c.

◆ PG_STAT_GET_SUBSCRIPTION_COLS

#define PG_STAT_GET_SUBSCRIPTION_COLS   10

Typedef Documentation

◆ LauncherLastStartTimesEntry

◆ LogicalRepCtxStruct

Function Documentation

◆ ApplyLauncherForgetWorkerStartTime()

void ApplyLauncherForgetWorkerStartTime ( Oid  subid)

Definition at line 1081 of file launcher.c.

1082 {
1084 
1085  (void) dshash_delete_key(last_start_times, &subid);
1086 }
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:995

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

◆ ApplyLauncherGetWorkerStartTime()

static TimestampTz ApplyLauncherGetWorkerStartTime ( Oid  subid)
static

Definition at line 1055 of file launcher.c.

1056 {
1058  TimestampTz ret;
1059 
1061 
1062  entry = dshash_find(last_start_times, &subid, false);
1063  if (entry == NULL)
1064  return 0;
1065 
1066  ret = entry->last_start_time;
1068 
1069  return ret;
1070 }
int64 TimestampTz
Definition: timestamp.h:39
void dshash_release_lock(dshash_table *hash_table, void *entry)
Definition: dshash.c:558
void * dshash_find(dshash_table *hash_table, const void *key, bool exclusive)
Definition: dshash.c:390
TimestampTz last_start_time
Definition: launcher.c:75

References dshash_find(), dshash_release_lock(), LauncherLastStartTimesEntry::last_start_time, last_start_times, and logicalrep_launcher_attach_dshmem().

Referenced by ApplyLauncherMain().

◆ ApplyLauncherMain()

void ApplyLauncherMain ( Datum  main_arg)

Definition at line 1128 of file launcher.c.

1129 {
1130  ereport(DEBUG1,
1131  (errmsg_internal("logical replication launcher started")));
1132 
1134 
1137 
1138  /* Establish signal handlers. */
1140  pqsignal(SIGTERM, die);
1142 
1143  /*
1144  * Establish connection to nailed catalogs (we only ever access
1145  * pg_subscription).
1146  */
1147  BackgroundWorkerInitializeConnection(NULL, NULL, 0);
1148 
1149  /* Enter main loop */
1150  for (;;)
1151  {
1152  int rc;
1153  List *sublist;
1154  ListCell *lc;
1155  MemoryContext subctx;
1156  MemoryContext oldctx;
1157  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
1158 
1160 
1161  /* Use temporary context to avoid leaking memory across cycles. */
1163  "Logical Replication Launcher sublist",
1165  oldctx = MemoryContextSwitchTo(subctx);
1166 
1167  /* Start any missing workers for enabled subscriptions. */
1168  sublist = get_subscription_list();
1169  foreach(lc, sublist)
1170  {
1171  Subscription *sub = (Subscription *) lfirst(lc);
1172  LogicalRepWorker *w;
1173  TimestampTz last_start;
1174  TimestampTz now;
1175  long elapsed;
1176 
1177  if (!sub->enabled)
1178  continue;
1179 
1180  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1181  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
1182  LWLockRelease(LogicalRepWorkerLock);
1183 
1184  if (w != NULL)
1185  continue; /* worker is running already */
1186 
1187  /*
1188  * If the worker is eligible to start now, launch it. Otherwise,
1189  * adjust wait_time so that we'll wake up as soon as it can be
1190  * started.
1191  *
1192  * Each subscription's apply worker can only be restarted once per
1193  * wal_retrieve_retry_interval, so that errors do not cause us to
1194  * repeatedly restart the worker as fast as possible. In cases
1195  * where a restart is expected (e.g., subscription parameter
1196  * changes), another process should remove the last-start entry
1197  * for the subscription so that the worker can be restarted
1198  * without waiting for wal_retrieve_retry_interval to elapse.
1199  */
1200  last_start = ApplyLauncherGetWorkerStartTime(sub->oid);
1202  if (last_start == 0 ||
1204  {
1207  sub->dbid, sub->oid, sub->name,
1208  sub->owner, InvalidOid,
1210  }
1211  else
1212  {
1213  wait_time = Min(wait_time,
1214  wal_retrieve_retry_interval - elapsed);
1215  }
1216  }
1217 
1218  /* Switch back to original memory context. */
1219  MemoryContextSwitchTo(oldctx);
1220  /* Clean the temporary memory. */
1221  MemoryContextDelete(subctx);
1222 
1223  /* Wait for more work. */
1224  rc = WaitLatch(MyLatch,
1226  wait_time,
1227  WAIT_EVENT_LOGICAL_LAUNCHER_MAIN);
1228 
1229  if (rc & WL_LATCH_SET)
1230  {
1233  }
1234 
1235  if (ConfigReloadPending)
1236  {
1237  ConfigReloadPending = false;
1239  }
1240  }
1241 
1242  /* Not reachable */
1243 }
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition: timestamp.c:1756
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1644
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1608
void BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
Definition: bgworker.c:852
void BackgroundWorkerUnblockSignals(void)
Definition: bgworker.c:926
#define Min(x, y)
Definition: c.h:995
#define Assert(condition)
Definition: c.h:849
#define DSM_HANDLE_INVALID
Definition: dsm_impl.h:58
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
#define DEBUG1
Definition: elog.h:30
#define ereport(elevel,...)
Definition: elog.h:149
int MyProcPid
Definition: globals.c:46
struct Latch * MyLatch
Definition: globals.c:62
@ 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:306
#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:1039
static List * get_subscription_list(void)
Definition: launcher.c:112
static void logicalrep_launcher_onexit(int code, Datum arg)
Definition: launcher.c:813
static TimestampTz ApplyLauncherGetWorkerStartTime(Oid subid)
Definition: launcher.c:1055
static LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:69
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1168
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1781
@ 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
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:133

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 
928  /*
929  * The logical replication launcher is disabled during binary upgrades, to
930  * prevent logical replication workers from running on the source cluster.
931  * That could cause replication origins to move forward after having been
932  * copied to the target cluster, potentially creating conflicts with the
933  * copied data files.
934  */
936  return;
937 
938  memset(&bgw, 0, sizeof(bgw));
942  snprintf(bgw.bgw_library_name, MAXPGPATH, "postgres");
943  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
945  "logical replication launcher");
947  "logical replication launcher");
948  bgw.bgw_restart_time = 5;
949  bgw.bgw_notify_pid = 0;
950  bgw.bgw_main_arg = (Datum) 0;
951 
953 }
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:939
@ 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:120
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().

◆ ApplyLauncherSetWorkerStartTime()

static void ApplyLauncherSetWorkerStartTime ( Oid  subid,
TimestampTz  start_time 
)
static

Definition at line 1039 of file launcher.c.

1040 {
1042  bool found;
1043 
1045 
1046  entry = dshash_find_or_insert(last_start_times, &subid, &found);
1047  entry->last_start_time = start_time;
1049 }
void * dshash_find_or_insert(dshash_table *hash_table, const void *key, bool *found)
Definition: dshash.c:433
static time_t start_time
Definition: pg_ctl.c:95

References dshash_find_or_insert(), dshash_release_lock(), LauncherLastStartTimesEntry::last_start_time, last_start_times, logicalrep_launcher_attach_dshmem(), and start_time.

Referenced by ApplyLauncherMain().

◆ ApplyLauncherShmemInit()

void ApplyLauncherShmemInit ( void  )

Definition at line 960 of file launcher.c.

961 {
962  bool found;
963 
965  ShmemInitStruct("Logical Replication Launcher Data",
967  &found);
968 
969  if (!found)
970  {
971  int slot;
972 
974 
977 
978  /* Initialize memory and spin locks for each worker slot. */
979  for (slot = 0; slot < max_logical_replication_workers; slot++)
980  {
981  LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
982 
983  memset(worker, 0, sizeof(LogicalRepWorker));
984  SpinLockInit(&worker->relmutex);
985  }
986  }
987 }
#define DSA_HANDLE_INVALID
Definition: dsa.h:139
#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:387
#define SpinLockInit(lock)
Definition: spin.h:57
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 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:802
size_t Size
Definition: c.h:596
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().

◆ ApplyLauncherWakeup()

static void ApplyLauncherWakeup ( void  )
static

Definition at line 1118 of file launcher.c.

1119 {
1120  if (LogicalRepCtx->launcher_pid != 0)
1122 }
#define kill(pid, sig)
Definition: win32_port.h:503
#define SIGUSR1
Definition: win32_port.h:180

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

Referenced by AtEOXact_ApplyLauncher(), and logicalrep_worker_onexit().

◆ ApplyLauncherWakeupAtCommit()

void ApplyLauncherWakeupAtCommit ( void  )

Definition at line 1111 of file launcher.c.

1112 {
1115 }
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 1092 of file launcher.c.

1093 {
1094  if (isCommit)
1095  {
1098  }
1099 
1100  on_commit_launcher_wakeup = false;
1101 }
static void ApplyLauncherWakeup(void)
Definition: launcher.c:1118

References ApplyLauncherWakeup(), and on_commit_launcher_wakeup.

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

◆ get_subscription_list()

static List* get_subscription_list ( void  )
static

Definition at line 112 of file launcher.c.

113 {
114  List *res = NIL;
115  Relation rel;
116  TableScanDesc scan;
117  HeapTuple tup;
118  MemoryContext resultcxt;
119 
120  /* This is the context that we will allocate our output data in */
121  resultcxt = CurrentMemoryContext;
122 
123  /*
124  * Start a transaction so we can access pg_database, and get a snapshot.
125  * We don't have a use for the snapshot itself, but we're interested in
126  * the secondary effect that it sets RecentGlobalXmin. (This is critical
127  * for anything that reads heap pages, because HOT may decide to prune
128  * them even if the process doesn't attempt to modify any tuples.)
129  *
130  * FIXME: This comment is inaccurate / the code buggy. A snapshot that is
131  * not pushed/active does not reliably prevent HOT pruning (->xmin could
132  * e.g. be cleared when cache invalidations are processed).
133  */
135  (void) GetTransactionSnapshot();
136 
137  rel = table_open(SubscriptionRelationId, AccessShareLock);
138  scan = table_beginscan_catalog(rel, 0, NULL);
139 
141  {
143  Subscription *sub;
144  MemoryContext oldcxt;
145 
146  /*
147  * Allocate our results in the caller's context, not the
148  * transaction's. We do this inside the loop, and restore the original
149  * context at the end, so that leaky things like heap_getnext() are
150  * not called in a potentially long-lived context.
151  */
152  oldcxt = MemoryContextSwitchTo(resultcxt);
153 
154  sub = (Subscription *) palloc0(sizeof(Subscription));
155  sub->oid = subform->oid;
156  sub->dbid = subform->subdbid;
157  sub->owner = subform->subowner;
158  sub->enabled = subform->subenabled;
159  sub->name = pstrdup(NameStr(subform->subname));
160  /* We don't fill fields we are not interested in. */
161 
162  res = lappend(res, sub);
163  MemoryContextSwitchTo(oldcxt);
164  }
165 
166  table_endscan(scan);
168 
170 
171  return res;
172 }
#define NameStr(name)
Definition: c.h:737
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1243
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
List * lappend(List *list, void *datum)
Definition: list.c:339
#define AccessShareLock
Definition: lockdefs.h:36
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void * palloc0(Size size)
Definition: mcxt.c:1347
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
#define NIL
Definition: pg_list.h:68
FormData_pg_subscription * Form_pg_subscription
@ ForwardScanDirection
Definition: sdir.h:28
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:216
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:126
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:40
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:112
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:1019
void StartTransactionCommand(void)
Definition: xact.c:3039
void CommitTransactionCommand(void)
Definition: xact.c:3137

References AccessShareLock, CommitTransactionCommand(), CurrentMemoryContext, Subscription::dbid, Subscription::enabled, ForwardScanDirection, GETSTRUCT, GetTransactionSnapshot(), heap_getnext(), HeapTupleIsValid, lappend(), MemoryContextSwitchTo(), Subscription::name, NameStr, NIL, Subscription::oid, Subscription::owner, palloc0(), pstrdup(), res, StartTransactionCommand(), table_beginscan_catalog(), table_close(), table_endscan(), and table_open().

Referenced by ApplyLauncherMain().

◆ GetLeaderApplyWorkerPid()

pid_t GetLeaderApplyWorkerPid ( pid_t  pid)

Definition at line 1259 of file launcher.c.

1260 {
1261  int leader_pid = InvalidPid;
1262  int i;
1263 
1264  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1265 
1266  for (i = 0; i < max_logical_replication_workers; i++)
1267  {
1269 
1270  if (isParallelApplyWorker(w) && w->proc && pid == w->proc->pid)
1271  {
1272  leader_pid = w->leader_pid;
1273  break;
1274  }
1275  }
1276 
1277  LWLockRelease(LogicalRepWorkerLock);
1278 
1279  return leader_pid;
1280 }
int i
Definition: isn.c:73
#define InvalidPid
Definition: miscadmin.h:32
int pid
Definition: proc.h:182
#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 1249 of file launcher.c.

1250 {
1252 }

References LogicalRepCtxStruct::launcher_pid, LogicalRepCtx, and MyProcPid.

Referenced by ProcessInterrupts().

◆ logicalrep_launcher_attach_dshmem()

static void logicalrep_launcher_attach_dshmem ( void  )
static

Definition at line 995 of file launcher.c.

996 {
997  MemoryContext oldcontext;
998 
999  /* Quick exit if we already did this. */
1001  last_start_times != NULL)
1002  return;
1003 
1004  /* Otherwise, use a lock to ensure only one process creates the table. */
1005  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
1006 
1007  /* Be sure any local memory allocated by DSA routines is persistent. */
1009 
1011  {
1012  /* Initialize dynamic shared hash table for last-start times. */
1017 
1018  /* Store handles in shared memory for other backends to use. */
1021  }
1022  else if (!last_start_times)
1023  {
1024  /* Attach to existing dynamic shared hash table. */
1029  }
1030 
1031  MemoryContextSwitchTo(oldcontext);
1032  LWLockRelease(LogicalRepWorkerLock);
1033 }
dsa_area * dsa_attach(dsa_handle handle)
Definition: dsa.c:510
void dsa_pin_mapping(dsa_area *area)
Definition: dsa.c:635
dsa_handle dsa_get_handle(dsa_area *area)
Definition: dsa.c:498
void dsa_pin(dsa_area *area)
Definition: dsa.c:975
#define dsa_create(tranch_id)
Definition: dsa.h:117
dshash_table_handle dshash_get_hash_table_handle(dshash_table *hash_table)
Definition: dshash.c:367
dshash_table * dshash_attach(dsa_area *area, const dshash_parameters *params, dshash_table_handle handle, void *arg)
Definition: dshash.c:270
dshash_table * dshash_create(dsa_area *area, const dshash_parameters *params, void *arg)
Definition: dshash.c:206
static dsa_area * last_start_times_dsa
Definition: launcher.c:88
static const dshash_parameters dsh_params
Definition: launcher.c:79
@ LWTRANCHE_LAUNCHER_DSA
Definition: lwlock.h:206
@ LW_EXCLUSIVE
Definition: lwlock.h:114

References dsa_attach(), dsa_create, dsa_get_handle(), dsa_pin(), dsa_pin_mapping(), dsh_params, dshash_attach(), dshash_create(), dshash_get_hash_table_handle(), DSHASH_HANDLE_INVALID, LogicalRepCtxStruct::last_start_dsa, LogicalRepCtxStruct::last_start_dsh, last_start_times, last_start_times_dsa, LogicalRepCtx, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), LWTRANCHE_LAUNCHER_DSA, MemoryContextSwitchTo(), and TopMemoryContext.

Referenced by ApplyLauncherForgetWorkerStartTime(), ApplyLauncherGetWorkerStartTime(), and ApplyLauncherSetWorkerStartTime().

◆ logicalrep_launcher_onexit()

static void logicalrep_launcher_onexit ( int  code,
Datum  arg 
)
static

Definition at line 813 of file launcher.c.

814 {
816 }

References LogicalRepCtxStruct::launcher_pid, and LogicalRepCtx.

Referenced by ApplyLauncherMain().

◆ logicalrep_pa_worker_count()

static int logicalrep_pa_worker_count ( Oid  subid)
static

Definition at line 878 of file launcher.c.

879 {
880  int i;
881  int res = 0;
882 
883  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
884 
885  /*
886  * Scan all attached parallel apply workers, only counting those which
887  * have the given subscription id.
888  */
889  for (i = 0; i < max_logical_replication_workers; i++)
890  {
892 
893  if (isParallelApplyWorker(w) && w->subid == subid)
894  res++;
895  }
896 
897  return res;
898 }
bool LWLockHeldByMe(LWLock *lock)
Definition: lwlock.c:1893

References Assert, i, isParallelApplyWorker, LogicalRepCtx, LWLockHeldByMe(), max_logical_replication_workers, res, LogicalRepWorker::subid, and LogicalRepCtxStruct::workers.

Referenced by logicalrep_worker_launch().

◆ logicalrep_pa_worker_stop()

void logicalrep_pa_worker_stop ( ParallelApplyWorkerInfo winfo)

Definition at line 639 of file launcher.c.

640 {
641  int slot_no;
642  uint16 generation;
643  LogicalRepWorker *worker;
644 
645  SpinLockAcquire(&winfo->shared->mutex);
646  generation = winfo->shared->logicalrep_worker_generation;
647  slot_no = winfo->shared->logicalrep_worker_slot_no;
648  SpinLockRelease(&winfo->shared->mutex);
649 
650  Assert(slot_no >= 0 && slot_no < max_logical_replication_workers);
651 
652  /*
653  * Detach from the error_mq_handle for the parallel apply worker before
654  * stopping it. This prevents the leader apply worker from trying to
655  * receive the message from the error queue that might already be detached
656  * by the parallel apply worker.
657  */
658  if (winfo->error_mq_handle)
659  {
661  winfo->error_mq_handle = NULL;
662  }
663 
664  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
665 
666  worker = &LogicalRepCtx->workers[slot_no];
667  Assert(isParallelApplyWorker(worker));
668 
669  /*
670  * Only stop the worker if the generation matches and the worker is alive.
671  */
672  if (worker->generation == generation && worker->proc)
673  logicalrep_worker_stop_internal(worker, SIGINT);
674 
675  LWLockRelease(LogicalRepWorkerLock);
676 }
unsigned short uint16
Definition: c.h:505
static void logicalrep_worker_stop_internal(LogicalRepWorker *worker, int signo)
Definition: launcher.c:533
void shm_mq_detach(shm_mq_handle *mqh)
Definition: shm_mq.c:843
#define SpinLockRelease(lock)
Definition: spin.h:61
#define SpinLockAcquire(lock)
Definition: spin.h:59
shm_mq_handle * error_mq_handle
ParallelApplyWorkerShared * shared

References Assert, ParallelApplyWorkerInfo::error_mq_handle, LogicalRepWorker::generation, isParallelApplyWorker, ParallelApplyWorkerShared::logicalrep_worker_generation, ParallelApplyWorkerShared::logicalrep_worker_slot_no, logicalrep_worker_stop_internal(), LogicalRepCtx, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_logical_replication_workers, ParallelApplyWorkerShared::mutex, LogicalRepWorker::proc, ParallelApplyWorkerInfo::shared, shm_mq_detach(), SpinLockAcquire, SpinLockRelease, and LogicalRepCtxStruct::workers.

Referenced by pa_free_worker().

◆ logicalrep_sync_worker_count()

int logicalrep_sync_worker_count ( Oid  subid)

Definition at line 854 of file launcher.c.

855 {
856  int i;
857  int res = 0;
858 
859  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
860 
861  /* Search for attached worker for a given subscription id. */
862  for (i = 0; i < max_logical_replication_workers; i++)
863  {
865 
866  if (isTablesyncWorker(w) && w->subid == subid)
867  res++;
868  }
869 
870  return res;
871 }
#define isTablesyncWorker(worker)

References Assert, i, isTablesyncWorker, LogicalRepCtx, LWLockHeldByMe(), max_logical_replication_workers, res, LogicalRepWorker::subid, and LogicalRepCtxStruct::workers.

Referenced by logicalrep_worker_launch(), and process_syncing_tables_for_apply().

◆ logicalrep_worker_attach()

void logicalrep_worker_attach ( int  slot)

Definition at line 713 of file launcher.c.

714 {
715  /* Block concurrent access. */
716  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
717 
718  Assert(slot >= 0 && slot < max_logical_replication_workers);
720 
722  {
723  LWLockRelease(LogicalRepWorkerLock);
724  ereport(ERROR,
725  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
726  errmsg("logical replication worker slot %d is empty, cannot attach",
727  slot)));
728  }
729 
731  {
732  LWLockRelease(LogicalRepWorkerLock);
733  ereport(ERROR,
734  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
735  errmsg("logical replication worker slot %d is already used by "
736  "another worker, cannot attach", slot)));
737  }
738 
741 
742  LWLockRelease(LogicalRepWorkerLock);
743 }
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:54
static void logicalrep_worker_onexit(int code, Datum arg)
Definition: launcher.c:824
PGPROC * MyProc
Definition: proc.c:67

References Assert, before_shmem_exit(), ereport, errcode(), errmsg(), ERROR, LogicalRepWorker::in_use, logicalrep_worker_onexit(), LogicalRepCtx, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), max_logical_replication_workers, MyLogicalRepWorker, MyProc, LogicalRepWorker::proc, and LogicalRepCtxStruct::workers.

Referenced by ParallelApplyWorkerMain(), and SetupApplyOrSyncWorker().

◆ logicalrep_worker_cleanup()

static void logicalrep_worker_cleanup ( LogicalRepWorker worker)
static

Definition at line 792 of file launcher.c.

793 {
794  Assert(LWLockHeldByMeInMode(LogicalRepWorkerLock, LW_EXCLUSIVE));
795 
796  worker->type = WORKERTYPE_UNKNOWN;
797  worker->in_use = false;
798  worker->proc = NULL;
799  worker->dbid = InvalidOid;
800  worker->userid = InvalidOid;
801  worker->subid = InvalidOid;
802  worker->relid = InvalidOid;
803  worker->leader_pid = InvalidPid;
804  worker->parallel_apply = false;
805 }
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1937
LogicalRepWorkerType type
@ WORKERTYPE_UNKNOWN

References Assert, LogicalRepWorker::dbid, LogicalRepWorker::in_use, InvalidOid, InvalidPid, LogicalRepWorker::leader_pid, LW_EXCLUSIVE, LWLockHeldByMeInMode(), LogicalRepWorker::parallel_apply, LogicalRepWorker::proc, LogicalRepWorker::relid, LogicalRepWorker::subid, LogicalRepWorker::type, LogicalRepWorker::userid, and WORKERTYPE_UNKNOWN.

Referenced by logicalrep_worker_detach(), logicalrep_worker_launch(), and WaitForReplicationWorkerAttach().

◆ logicalrep_worker_detach()

static void logicalrep_worker_detach ( void  )
static

Definition at line 750 of file launcher.c.

751 {
752  /* Stop the parallel apply workers. */
754  {
755  List *workers;
756  ListCell *lc;
757 
758  /*
759  * Detach from the error_mq_handle for all parallel apply workers
760  * before terminating them. This prevents the leader apply worker from
761  * receiving the worker termination message and sending it to logs
762  * when the same is already done by the parallel worker.
763  */
765 
766  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
767 
768  workers = logicalrep_workers_find(MyLogicalRepWorker->subid, true, false);
769  foreach(lc, workers)
770  {
772 
773  if (isParallelApplyWorker(w))
775  }
776 
777  LWLockRelease(LogicalRepWorkerLock);
778  }
779 
780  /* Block concurrent access. */
781  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
782 
784 
785  LWLockRelease(LogicalRepWorkerLock);
786 }
void pa_detach_all_error_mq(void)
List * logicalrep_workers_find(Oid subid, bool only_running, bool acquire_lock)
Definition: launcher.c:275
static void logicalrep_worker_cleanup(LogicalRepWorker *worker)
Definition: launcher.c:792
static bool am_leader_apply_worker(void)

References am_leader_apply_worker(), isParallelApplyWorker, lfirst, logicalrep_worker_cleanup(), logicalrep_worker_stop_internal(), logicalrep_workers_find(), LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), MyLogicalRepWorker, pa_detach_all_error_mq(), and LogicalRepWorker::subid.

Referenced by logicalrep_worker_onexit().

◆ logicalrep_worker_find()

LogicalRepWorker* logicalrep_worker_find ( Oid  subid,
Oid  relid,
bool  only_running 
)

Definition at line 243 of file launcher.c.

244 {
245  int i;
246  LogicalRepWorker *res = NULL;
247 
248  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
249 
250  /* Search for attached worker for a given subscription id. */
251  for (i = 0; i < max_logical_replication_workers; i++)
252  {
254 
255  /* Skip parallel apply workers. */
256  if (isParallelApplyWorker(w))
257  continue;
258 
259  if (w->in_use && w->subid == subid && w->relid == relid &&
260  (!only_running || w->proc))
261  {
262  res = w;
263  break;
264  }
265  }
266 
267  return res;
268 }

References Assert, i, LogicalRepWorker::in_use, isParallelApplyWorker, LogicalRepCtx, LWLockHeldByMe(), max_logical_replication_workers, LogicalRepWorker::proc, LogicalRepWorker::relid, res, LogicalRepWorker::subid, and LogicalRepCtxStruct::workers.

Referenced by ApplyLauncherMain(), logicalrep_worker_stop(), logicalrep_worker_wakeup(), process_syncing_tables_for_apply(), wait_for_relation_state_change(), and wait_for_worker_state_change().

◆ logicalrep_worker_launch()

bool logicalrep_worker_launch ( LogicalRepWorkerType  wtype,
Oid  dbid,
Oid  subid,
const char *  subname,
Oid  userid,
Oid  relid,
dsm_handle  subworker_dsm 
)

Definition at line 306 of file launcher.c.

309 {
310  BackgroundWorker bgw;
311  BackgroundWorkerHandle *bgw_handle;
312  uint16 generation;
313  int i;
314  int slot = 0;
315  LogicalRepWorker *worker = NULL;
316  int nsyncworkers;
317  int nparallelapplyworkers;
319  bool is_tablesync_worker = (wtype == WORKERTYPE_TABLESYNC);
320  bool is_parallel_apply_worker = (wtype == WORKERTYPE_PARALLEL_APPLY);
321 
322  /*----------
323  * Sanity checks:
324  * - must be valid worker type
325  * - tablesync workers are only ones to have relid
326  * - parallel apply worker is the only kind of subworker
327  */
328  Assert(wtype != WORKERTYPE_UNKNOWN);
329  Assert(is_tablesync_worker == OidIsValid(relid));
330  Assert(is_parallel_apply_worker == (subworker_dsm != DSM_HANDLE_INVALID));
331 
332  ereport(DEBUG1,
333  (errmsg_internal("starting logical replication worker for subscription \"%s\"",
334  subname)));
335 
336  /* Report this after the initial starting message for consistency. */
337  if (max_replication_slots == 0)
338  ereport(ERROR,
339  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
340  errmsg("cannot start logical replication workers when \"max_replication_slots\"=0")));
341 
342  /*
343  * We need to do the modification of the shared memory under lock so that
344  * we have consistent view.
345  */
346  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
347 
348 retry:
349  /* Find unused worker slot. */
350  for (i = 0; i < max_logical_replication_workers; i++)
351  {
353 
354  if (!w->in_use)
355  {
356  worker = w;
357  slot = i;
358  break;
359  }
360  }
361 
362  nsyncworkers = logicalrep_sync_worker_count(subid);
363 
365 
366  /*
367  * If we didn't find a free slot, try to do garbage collection. The
368  * reason we do this is because if some worker failed to start up and its
369  * parent has crashed while waiting, the in_use state was never cleared.
370  */
371  if (worker == NULL || nsyncworkers >= max_sync_workers_per_subscription)
372  {
373  bool did_cleanup = false;
374 
375  for (i = 0; i < max_logical_replication_workers; i++)
376  {
378 
379  /*
380  * If the worker was marked in use but didn't manage to attach in
381  * time, clean it up.
382  */
383  if (w->in_use && !w->proc &&
386  {
387  elog(WARNING,
388  "logical replication worker for subscription %u took too long to start; canceled",
389  w->subid);
390 
392  did_cleanup = true;
393  }
394  }
395 
396  if (did_cleanup)
397  goto retry;
398  }
399 
400  /*
401  * We don't allow to invoke more sync workers once we have reached the
402  * sync worker limit per subscription. So, just return silently as we
403  * might get here because of an otherwise harmless race condition.
404  */
405  if (is_tablesync_worker && nsyncworkers >= max_sync_workers_per_subscription)
406  {
407  LWLockRelease(LogicalRepWorkerLock);
408  return false;
409  }
410 
411  nparallelapplyworkers = logicalrep_pa_worker_count(subid);
412 
413  /*
414  * Return false if the number of parallel apply workers reached the limit
415  * per subscription.
416  */
417  if (is_parallel_apply_worker &&
418  nparallelapplyworkers >= max_parallel_apply_workers_per_subscription)
419  {
420  LWLockRelease(LogicalRepWorkerLock);
421  return false;
422  }
423 
424  /*
425  * However if there are no more free worker slots, inform user about it
426  * before exiting.
427  */
428  if (worker == NULL)
429  {
430  LWLockRelease(LogicalRepWorkerLock);
432  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
433  errmsg("out of logical replication worker slots"),
434  errhint("You might need to increase \"%s\".", "max_logical_replication_workers")));
435  return false;
436  }
437 
438  /* Prepare the worker slot. */
439  worker->type = wtype;
440  worker->launch_time = now;
441  worker->in_use = true;
442  worker->generation++;
443  worker->proc = NULL;
444  worker->dbid = dbid;
445  worker->userid = userid;
446  worker->subid = subid;
447  worker->relid = relid;
448  worker->relstate = SUBREL_STATE_UNKNOWN;
450  worker->stream_fileset = NULL;
451  worker->leader_pid = is_parallel_apply_worker ? MyProcPid : InvalidPid;
452  worker->parallel_apply = is_parallel_apply_worker;
453  worker->last_lsn = InvalidXLogRecPtr;
456  worker->reply_lsn = InvalidXLogRecPtr;
457  TIMESTAMP_NOBEGIN(worker->reply_time);
458 
459  /* Before releasing lock, remember generation for future identification. */
460  generation = worker->generation;
461 
462  LWLockRelease(LogicalRepWorkerLock);
463 
464  /* Register the new dynamic worker. */
465  memset(&bgw, 0, sizeof(bgw));
469  snprintf(bgw.bgw_library_name, MAXPGPATH, "postgres");
470 
471  switch (worker->type)
472  {
473  case WORKERTYPE_APPLY:
474  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyWorkerMain");
476  "logical replication apply worker for subscription %u",
477  subid);
478  snprintf(bgw.bgw_type, BGW_MAXLEN, "logical replication apply worker");
479  break;
480 
482  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ParallelApplyWorkerMain");
484  "logical replication parallel apply worker for subscription %u",
485  subid);
486  snprintf(bgw.bgw_type, BGW_MAXLEN, "logical replication parallel worker");
487 
488  memcpy(bgw.bgw_extra, &subworker_dsm, sizeof(dsm_handle));
489  break;
490 
492  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "TablesyncWorkerMain");
494  "logical replication tablesync worker for subscription %u sync %u",
495  subid,
496  relid);
497  snprintf(bgw.bgw_type, BGW_MAXLEN, "logical replication tablesync worker");
498  break;
499 
500  case WORKERTYPE_UNKNOWN:
501  /* Should never happen. */
502  elog(ERROR, "unknown worker type");
503  }
504 
507  bgw.bgw_main_arg = Int32GetDatum(slot);
508 
509  if (!RegisterDynamicBackgroundWorker(&bgw, &bgw_handle))
510  {
511  /* Failed to start worker, so clean up the worker slot. */
512  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
513  Assert(generation == worker->generation);
515  LWLockRelease(LogicalRepWorkerLock);
516 
518  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
519  errmsg("out of background worker slots"),
520  errhint("You might need to increase \"%s\".", "max_worker_processes")));
521  return false;
522  }
523 
524  /* Now wait until it attaches. */
525  return WaitForReplicationWorkerAttach(worker, generation, bgw_handle);
526 }
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1780
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition: bgworker.c:1045
#define BGW_NEVER_RESTART
Definition: bgworker.h:85
#define OidIsValid(objectId)
Definition: c.h:766
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:159
uint32 dsm_handle
Definition: dsm_impl.h:55
int errhint(const char *fmt,...)
Definition: elog.c:1317
#define WARNING
Definition: elog.h:36
#define elog(elevel,...)
Definition: elog.h:225
static int logicalrep_pa_worker_count(Oid subid)
Definition: launcher.c:878
int max_sync_workers_per_subscription
Definition: launcher.c:51
static bool WaitForReplicationWorkerAttach(LogicalRepWorker *worker, uint16 generation, BackgroundWorkerHandle *handle)
Definition: launcher.c:183
int logicalrep_sync_worker_count(Oid subid)
Definition: launcher.c:854
int max_parallel_apply_workers_per_subscription
Definition: launcher.c:52
NameData subname
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
int max_replication_slots
Definition: slot.c:141
char bgw_extra[BGW_EXTRALEN]
Definition: bgworker.h:99
XLogRecPtr relstate_lsn
TimestampTz last_recv_time
TimestampTz launch_time
TimestampTz reply_time
FileSet * stream_fileset
XLogRecPtr reply_lsn
XLogRecPtr last_lsn
TimestampTz last_send_time
int wal_receiver_timeout
Definition: walreceiver.c:88
@ WORKERTYPE_TABLESYNC
@ WORKERTYPE_PARALLEL_APPLY
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

References Assert, BackgroundWorker::bgw_extra, BackgroundWorker::bgw_flags, BackgroundWorker::bgw_function_name, BackgroundWorker::bgw_library_name, BackgroundWorker::bgw_main_arg, BGW_MAXLEN, BackgroundWorker::bgw_name, BGW_NEVER_RESTART, BackgroundWorker::bgw_notify_pid, BackgroundWorker::bgw_restart_time, BackgroundWorker::bgw_start_time, BackgroundWorker::bgw_type, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_SHMEM_ACCESS, BgWorkerStart_RecoveryFinished, LogicalRepWorker::dbid, DEBUG1, DSM_HANDLE_INVALID, elog, ereport, errcode(), errhint(), errmsg(), errmsg_internal(), ERROR, LogicalRepWorker::generation, GetCurrentTimestamp(), i, LogicalRepWorker::in_use, Int32GetDatum(), InvalidPid, InvalidXLogRecPtr, LogicalRepWorker::last_lsn, LogicalRepWorker::last_recv_time, LogicalRepWorker::last_send_time, LogicalRepWorker::launch_time, LogicalRepWorker::leader_pid, logicalrep_pa_worker_count(), logicalrep_sync_worker_count(), logicalrep_worker_cleanup(), LogicalRepCtx, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), max_logical_replication_workers, max_parallel_apply_workers_per_subscription, max_replication_slots, max_sync_workers_per_subscription, MAXPGPATH, MyProcPid, now(), OidIsValid, LogicalRepWorker::parallel_apply, LogicalRepWorker::proc, RegisterDynamicBackgroundWorker(), LogicalRepWorker::relid, LogicalRepWorker::relstate, LogicalRepWorker::relstate_lsn, LogicalRepWorker::reply_lsn, LogicalRepWorker::reply_time, snprintf, LogicalRepWorker::stream_fileset, LogicalRepWorker::subid, subname, TIMESTAMP_NOBEGIN, TimestampDifferenceExceeds(), LogicalRepWorker::type, LogicalRepWorker::userid, WaitForReplicationWorkerAttach(), wal_receiver_timeout, WARNING, LogicalRepCtxStruct::workers, WORKERTYPE_APPLY, WORKERTYPE_PARALLEL_APPLY, WORKERTYPE_TABLESYNC, and WORKERTYPE_UNKNOWN.

Referenced by ApplyLauncherMain(), pa_launch_parallel_worker(), and process_syncing_tables_for_apply().

◆ logicalrep_worker_onexit()

static void logicalrep_worker_onexit ( int  code,
Datum  arg 
)
static

Definition at line 824 of file launcher.c.

825 {
826  /* Disconnect gracefully from the remote side. */
829 
831 
832  /* Cleanup fileset used for streaming transactions. */
833  if (MyLogicalRepWorker->stream_fileset != NULL)
835 
836  /*
837  * Session level locks may be acquired outside of a transaction in
838  * parallel apply mode and will not be released when the worker
839  * terminates, so manually release all locks before the worker exits.
840  *
841  * The locks will be acquired once the worker is initialized.
842  */
845 
847 }
bool InitializingApplyWorker
Definition: worker.c:319
WalReceiverConn * LogRepWorkerWalRcvConn
Definition: worker.c:297
void FileSetDeleteAll(FileSet *fileset)
Definition: fileset.c:150
static void logicalrep_worker_detach(void)
Definition: launcher.c:750
void LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks)
Definition: lock.c:2218
#define DEFAULT_LOCKMETHOD
Definition: lock.h:125
#define walrcv_disconnect(conn)
Definition: walreceiver.h:466

References ApplyLauncherWakeup(), DEFAULT_LOCKMETHOD, FileSetDeleteAll(), InitializingApplyWorker, LockReleaseAll(), logicalrep_worker_detach(), LogRepWorkerWalRcvConn, MyLogicalRepWorker, LogicalRepWorker::stream_fileset, and walrcv_disconnect.

Referenced by logicalrep_worker_attach().

◆ logicalrep_worker_stop()

void logicalrep_worker_stop ( Oid  subid,
Oid  relid 
)

Definition at line 615 of file launcher.c.

616 {
617  LogicalRepWorker *worker;
618 
619  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
620 
621  worker = logicalrep_worker_find(subid, relid, false);
622 
623  if (worker)
624  {
625  Assert(!isParallelApplyWorker(worker));
626  logicalrep_worker_stop_internal(worker, SIGTERM);
627  }
628 
629  LWLockRelease(LogicalRepWorkerLock);
630 }

References Assert, isParallelApplyWorker, logicalrep_worker_find(), logicalrep_worker_stop_internal(), LW_SHARED, LWLockAcquire(), and LWLockRelease().

Referenced by AlterSubscription_refresh(), and DropSubscription().

◆ logicalrep_worker_stop_internal()

static void logicalrep_worker_stop_internal ( LogicalRepWorker worker,
int  signo 
)
static

Definition at line 533 of file launcher.c.

534 {
535  uint16 generation;
536 
537  Assert(LWLockHeldByMeInMode(LogicalRepWorkerLock, LW_SHARED));
538 
539  /*
540  * Remember which generation was our worker so we can check if what we see
541  * is still the same one.
542  */
543  generation = worker->generation;
544 
545  /*
546  * If we found a worker but it does not have proc set then it is still
547  * starting up; wait for it to finish starting and then kill it.
548  */
549  while (worker->in_use && !worker->proc)
550  {
551  int rc;
552 
553  LWLockRelease(LogicalRepWorkerLock);
554 
555  /* Wait a bit --- we don't expect to have to wait long. */
556  rc = WaitLatch(MyLatch,
558  10L, WAIT_EVENT_BGWORKER_STARTUP);
559 
560  if (rc & WL_LATCH_SET)
561  {
564  }
565 
566  /* Recheck worker status. */
567  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
568 
569  /*
570  * Check whether the worker slot is no longer used, which would mean
571  * that the worker has exited, or whether the worker generation is
572  * different, meaning that a different worker has taken the slot.
573  */
574  if (!worker->in_use || worker->generation != generation)
575  return;
576 
577  /* Worker has assigned proc, so it has started. */
578  if (worker->proc)
579  break;
580  }
581 
582  /* Now terminate the worker ... */
583  kill(worker->proc->pid, signo);
584 
585  /* ... and wait for it to die. */
586  for (;;)
587  {
588  int rc;
589 
590  /* is it gone? */
591  if (!worker->proc || worker->generation != generation)
592  break;
593 
594  LWLockRelease(LogicalRepWorkerLock);
595 
596  /* Wait a bit --- we don't expect to have to wait long. */
597  rc = WaitLatch(MyLatch,
599  10L, WAIT_EVENT_BGWORKER_SHUTDOWN);
600 
601  if (rc & WL_LATCH_SET)
602  {
605  }
606 
607  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
608  }
609 }

References Assert, CHECK_FOR_INTERRUPTS, LogicalRepWorker::generation, LogicalRepWorker::in_use, kill, LW_SHARED, LWLockAcquire(), LWLockHeldByMeInMode(), LWLockRelease(), MyLatch, PGPROC::pid, LogicalRepWorker::proc, ResetLatch(), WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and WL_TIMEOUT.

Referenced by logicalrep_pa_worker_stop(), logicalrep_worker_detach(), and logicalrep_worker_stop().

◆ logicalrep_worker_wakeup()

void logicalrep_worker_wakeup ( Oid  subid,
Oid  relid 
)

Definition at line 682 of file launcher.c.

683 {
684  LogicalRepWorker *worker;
685 
686  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
687 
688  worker = logicalrep_worker_find(subid, relid, true);
689 
690  if (worker)
692 
693  LWLockRelease(LogicalRepWorkerLock);
694 }
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition: launcher.c:702

References logicalrep_worker_find(), logicalrep_worker_wakeup_ptr(), LW_SHARED, LWLockAcquire(), and LWLockRelease().

Referenced by apply_handle_stream_start(), and pg_attribute_noreturn().

◆ logicalrep_worker_wakeup_ptr()

void logicalrep_worker_wakeup_ptr ( LogicalRepWorker worker)

Definition at line 702 of file launcher.c.

703 {
704  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
705 
706  SetLatch(&worker->proc->procLatch);
707 }
void SetLatch(Latch *latch)
Definition: latch.c:632
Latch procLatch
Definition: proc.h:169

References Assert, LWLockHeldByMe(), LogicalRepWorker::proc, PGPROC::procLatch, and SetLatch().

Referenced by AtEOXact_LogicalRepWorkers(), logicalrep_worker_wakeup(), process_syncing_tables_for_apply(), and wait_for_worker_state_change().

◆ logicalrep_workers_find()

List* logicalrep_workers_find ( Oid  subid,
bool  only_running,
bool  acquire_lock 
)

Definition at line 275 of file launcher.c.

276 {
277  int i;
278  List *res = NIL;
279 
280  if (acquire_lock)
281  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
282 
283  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
284 
285  /* Search for attached worker for a given subscription id. */
286  for (i = 0; i < max_logical_replication_workers; i++)
287  {
289 
290  if (w->in_use && w->subid == subid && (!only_running || w->proc))
291  res = lappend(res, w);
292  }
293 
294  if (acquire_lock)
295  LWLockRelease(LogicalRepWorkerLock);
296 
297  return res;
298 }

References Assert, i, LogicalRepWorker::in_use, lappend(), LogicalRepCtx, LW_SHARED, LWLockAcquire(), LWLockHeldByMe(), LWLockRelease(), max_logical_replication_workers, NIL, LogicalRepWorker::proc, res, LogicalRepWorker::subid, and LogicalRepCtxStruct::workers.

Referenced by AlterSubscription(), AtEOXact_LogicalRepWorkers(), DropSubscription(), and logicalrep_worker_detach().

◆ pg_stat_get_subscription()

Datum pg_stat_get_subscription ( PG_FUNCTION_ARGS  )

Definition at line 1286 of file launcher.c.

1287 {
1288 #define PG_STAT_GET_SUBSCRIPTION_COLS 10
1289  Oid subid = PG_ARGISNULL(0) ? InvalidOid : PG_GETARG_OID(0);
1290  int i;
1291  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
1292 
1293  InitMaterializedSRF(fcinfo, 0);
1294 
1295  /* Make sure we get consistent view of the workers. */
1296  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1297 
1298  for (i = 0; i < max_logical_replication_workers; i++)
1299  {
1300  /* for each row */
1302  bool nulls[PG_STAT_GET_SUBSCRIPTION_COLS] = {0};
1303  int worker_pid;
1304  LogicalRepWorker worker;
1305 
1306  memcpy(&worker, &LogicalRepCtx->workers[i],
1307  sizeof(LogicalRepWorker));
1308  if (!worker.proc || !IsBackendPid(worker.proc->pid))
1309  continue;
1310 
1311  if (OidIsValid(subid) && worker.subid != subid)
1312  continue;
1313 
1314  worker_pid = worker.proc->pid;
1315 
1316  values[0] = ObjectIdGetDatum(worker.subid);
1317  if (isTablesyncWorker(&worker))
1318  values[1] = ObjectIdGetDatum(worker.relid);
1319  else
1320  nulls[1] = true;
1321  values[2] = Int32GetDatum(worker_pid);
1322 
1323  if (isParallelApplyWorker(&worker))
1324  values[3] = Int32GetDatum(worker.leader_pid);
1325  else
1326  nulls[3] = true;
1327 
1328  if (XLogRecPtrIsInvalid(worker.last_lsn))
1329  nulls[4] = true;
1330  else
1331  values[4] = LSNGetDatum(worker.last_lsn);
1332  if (worker.last_send_time == 0)
1333  nulls[5] = true;
1334  else
1336  if (worker.last_recv_time == 0)
1337  nulls[6] = true;
1338  else
1340  if (XLogRecPtrIsInvalid(worker.reply_lsn))
1341  nulls[7] = true;
1342  else
1343  values[7] = LSNGetDatum(worker.reply_lsn);
1344  if (worker.reply_time == 0)
1345  nulls[8] = true;
1346  else
1347  values[8] = TimestampTzGetDatum(worker.reply_time);
1348 
1349  switch (worker.type)
1350  {
1351  case WORKERTYPE_APPLY:
1352  values[9] = CStringGetTextDatum("apply");
1353  break;
1355  values[9] = CStringGetTextDatum("parallel apply");
1356  break;
1357  case WORKERTYPE_TABLESYNC:
1358  values[9] = CStringGetTextDatum("table synchronization");
1359  break;
1360  case WORKERTYPE_UNKNOWN:
1361  /* Should never happen. */
1362  elog(ERROR, "unknown worker type");
1363  }
1364 
1365  tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
1366  values, nulls);
1367 
1368  /*
1369  * If only a single subscription was requested, and we found it,
1370  * break.
1371  */
1372  if (OidIsValid(subid))
1373  break;
1374  }
1375 
1376  LWLockRelease(LogicalRepWorkerLock);
1377 
1378  return (Datum) 0;
1379 }
static Datum values[MAXATTR]
Definition: bootstrap.c:150
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
#define PG_STAT_GET_SUBSCRIPTION_COLS
static Datum LSNGetDatum(XLogRecPtr X)
Definition: pg_lsn.h:28
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
unsigned int Oid
Definition: postgres_ext.h:31
bool IsBackendPid(int pid)
Definition: procarray.c:3295
TupleDesc setDesc
Definition: execnodes.h:343
Tuplestorestate * setResult
Definition: execnodes.h:342
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
Definition: tuplestore.c:784
static Datum TimestampTzGetDatum(TimestampTz X)
Definition: timestamp.h:52
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29

References CStringGetTextDatum, elog, ERROR, i, InitMaterializedSRF(), Int32GetDatum(), InvalidOid, IsBackendPid(), isParallelApplyWorker, isTablesyncWorker, LogicalRepWorker::last_lsn, LogicalRepWorker::last_recv_time, LogicalRepWorker::last_send_time, LogicalRepWorker::leader_pid, LogicalRepCtx, LSNGetDatum(), LW_SHARED, LWLockAcquire(), LWLockRelease(), max_logical_replication_workers, ObjectIdGetDatum(), OidIsValid, PG_ARGISNULL, PG_GETARG_OID, PG_STAT_GET_SUBSCRIPTION_COLS, PGPROC::pid, LogicalRepWorker::proc, LogicalRepWorker::relid, LogicalRepWorker::reply_lsn, LogicalRepWorker::reply_time, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, LogicalRepWorker::subid, TimestampTzGetDatum(), tuplestore_putvalues(), LogicalRepWorker::type, values, LogicalRepCtxStruct::workers, WORKERTYPE_APPLY, WORKERTYPE_PARALLEL_APPLY, WORKERTYPE_TABLESYNC, WORKERTYPE_UNKNOWN, and XLogRecPtrIsInvalid.

◆ WaitForReplicationWorkerAttach()

static bool WaitForReplicationWorkerAttach ( LogicalRepWorker worker,
uint16  generation,
BackgroundWorkerHandle handle 
)
static

Definition at line 183 of file launcher.c.

186 {
187  BgwHandleStatus status;
188  int rc;
189 
190  for (;;)
191  {
192  pid_t pid;
193 
195 
196  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
197 
198  /* Worker either died or has started. Return false if died. */
199  if (!worker->in_use || worker->proc)
200  {
201  LWLockRelease(LogicalRepWorkerLock);
202  return worker->in_use;
203  }
204 
205  LWLockRelease(LogicalRepWorkerLock);
206 
207  /* Check if worker has died before attaching, and clean up after it. */
208  status = GetBackgroundWorkerPid(handle, &pid);
209 
210  if (status == BGWH_STOPPED)
211  {
212  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
213  /* Ensure that this was indeed the worker we waited for. */
214  if (generation == worker->generation)
216  LWLockRelease(LogicalRepWorkerLock);
217  return false;
218  }
219 
220  /*
221  * We need timeout because we generally don't get notified via latch
222  * about the worker attach. But we don't expect to have to wait long.
223  */
224  rc = WaitLatch(MyLatch,
226  10L, WAIT_EVENT_BGWORKER_STARTUP);
227 
228  if (rc & WL_LATCH_SET)
229  {
232  }
233  }
234 }
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1157
BgwHandleStatus
Definition: bgworker.h:104
@ BGWH_STOPPED
Definition: bgworker.h:107

References BGWH_STOPPED, CHECK_FOR_INTERRUPTS, LogicalRepWorker::generation, GetBackgroundWorkerPid(), LogicalRepWorker::in_use, logicalrep_worker_cleanup(), LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), MyLatch, LogicalRepWorker::proc, ResetLatch(), WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and WL_TIMEOUT.

Referenced by logicalrep_worker_launch().

Variable Documentation

◆ dsh_params

const dshash_parameters dsh_params
static
Initial value:
= {
sizeof(Oid),
}
void dshash_memcpy(void *dest, const void *src, size_t size, void *arg)
Definition: dshash.c:590
dshash_hash dshash_memhash(const void *v, size_t size, void *arg)
Definition: dshash.c:581
int dshash_memcmp(const void *a, const void *b, size_t size, void *arg)
Definition: dshash.c:572
@ LWTRANCHE_LAUNCHER_HASH
Definition: lwlock.h:207

Definition at line 79 of file launcher.c.

Referenced by logicalrep_launcher_attach_dshmem().

◆ last_start_times

◆ last_start_times_dsa

dsa_area* last_start_times_dsa = NULL
static

Definition at line 88 of file launcher.c.

Referenced by logicalrep_launcher_attach_dshmem().

◆ LogicalRepCtx

◆ max_logical_replication_workers

◆ max_parallel_apply_workers_per_subscription

int max_parallel_apply_workers_per_subscription = 2

Definition at line 52 of file launcher.c.

Referenced by logicalrep_worker_launch(), and pa_free_worker().

◆ max_sync_workers_per_subscription

int max_sync_workers_per_subscription = 2

Definition at line 51 of file launcher.c.

Referenced by logicalrep_worker_launch(), and process_syncing_tables_for_apply().

◆ MyLogicalRepWorker

◆ on_commit_launcher_wakeup

bool on_commit_launcher_wakeup = false
static

Definition at line 91 of file launcher.c.

Referenced by ApplyLauncherWakeupAtCommit(), and AtEOXact_ApplyLauncher().