PostgreSQL Source Code  git master
launcher.c File Reference
Include dependency graph for launcher.c:

Go to the source code of this file.

Data Structures

struct  LogicalRepCtxStruct
 

Macros

#define DEFAULT_NAPTIME_PER_CYCLE   180000L
 
#define PG_STAT_GET_SUBSCRIPTION_COLS   8
 

Typedefs

typedef struct LogicalRepCtxStruct LogicalRepCtxStruct
 

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 Listget_subscription_list (void)
 
static void 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)
 
void logicalrep_worker_launch (Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid)
 
void logicalrep_worker_stop (Oid subid, Oid relid)
 
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 AtEOXact_ApplyLauncher (bool isCommit)
 
void ApplyLauncherWakeupAtCommit (void)
 
void ApplyLauncherMain (Datum main_arg)
 
bool IsLogicalLauncher (void)
 
Datum pg_stat_get_subscription (PG_FUNCTION_ARGS)
 

Variables

int max_logical_replication_workers = 4
 
int max_sync_workers_per_subscription = 2
 
LogicalRepWorkerMyLogicalRepWorker = NULL
 
static LogicalRepCtxStructLogicalRepCtx
 
static bool on_commit_launcher_wakeup = false
 

Macro Definition Documentation

◆ DEFAULT_NAPTIME_PER_CYCLE

#define DEFAULT_NAPTIME_PER_CYCLE   180000L

Definition at line 53 of file launcher.c.

◆ PG_STAT_GET_SUBSCRIPTION_COLS

#define PG_STAT_GET_SUBSCRIPTION_COLS   8

Typedef Documentation

◆ LogicalRepCtxStruct

Function Documentation

◆ ApplyLauncherMain()

void ApplyLauncherMain ( Datum  main_arg)

Definition at line 801 of file launcher.c.

802 {
803  TimestampTz last_start_time = 0;
804 
805  ereport(DEBUG1,
806  (errmsg_internal("logical replication launcher started")));
807 
809 
812 
813  /* Establish signal handlers. */
815  pqsignal(SIGTERM, die);
817 
818  /*
819  * Establish connection to nailed catalogs (we only ever access
820  * pg_subscription).
821  */
823 
824  /* Enter main loop */
825  for (;;)
826  {
827  int rc;
828  List *sublist;
829  ListCell *lc;
830  MemoryContext subctx;
831  MemoryContext oldctx;
833  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
834 
836 
838 
839  /* Limit the start retry to once a wal_retrieve_retry_interval */
840  if (TimestampDifferenceExceeds(last_start_time, now,
842  {
843  /* Use temporary context for the database list and worker info. */
845  "Logical Replication Launcher sublist",
847  oldctx = MemoryContextSwitchTo(subctx);
848 
849  /* search for subscriptions to start or stop. */
850  sublist = get_subscription_list();
851 
852  /* Start the missing workers for enabled subscriptions. */
853  foreach(lc, sublist)
854  {
855  Subscription *sub = (Subscription *) lfirst(lc);
856  LogicalRepWorker *w;
857 
858  if (!sub->enabled)
859  continue;
860 
861  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
862  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
863  LWLockRelease(LogicalRepWorkerLock);
864 
865  if (w == NULL)
866  {
867  last_start_time = now;
868  wait_time = wal_retrieve_retry_interval;
869 
870  logicalrep_worker_launch(sub->dbid, sub->oid, sub->name,
871  sub->owner, InvalidOid);
872  }
873  }
874 
875  /* Switch back to original memory context. */
876  MemoryContextSwitchTo(oldctx);
877  /* Clean the temporary memory. */
878  MemoryContextDelete(subctx);
879  }
880  else
881  {
882  /*
883  * The wait in previous cycle was interrupted in less than
884  * wal_retrieve_retry_interval since last worker was started, this
885  * usually means crash of the worker, so we should retry in
886  * wal_retrieve_retry_interval again.
887  */
888  wait_time = wal_retrieve_retry_interval;
889  }
890 
891  /* Wait for more work. */
892  rc = WaitLatch(MyLatch,
894  wait_time,
896 
897  if (rc & WL_LATCH_SET)
898  {
901  }
902 
904  {
905  ConfigReloadPending = false;
907  }
908  }
909 
910  /* Not reachable */
911 }
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1705
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1574
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1538
int64 TimestampTz
Definition: timestamp.h:39
int errmsg_internal(const char *fmt,...)
Definition: elog.c:991
#define DEBUG1
Definition: elog.h:24
#define ereport(elevel,...)
Definition: elog.h:143
int MyProcPid
Definition: globals.c:44
struct Latch * MyLatch
Definition: globals.c:58
@ PGC_SIGHUP
Definition: guc.h:72
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:658
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:451
#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:53
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:214
static List * get_subscription_list(void)
Definition: launcher.c:87
static void logicalrep_launcher_onexit(int code, Datum arg)
Definition: launcher.c:632
void logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid)
Definition: launcher.c:265
static LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:69
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1196
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1800
@ LW_SHARED
Definition: lwlock.h:105
MemoryContext TopMemoryContext
Definition: mcxt.c:48
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define AllocSetContextCreate
Definition: memutils.h:173
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:197
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define lfirst(lc)
Definition: pg_list.h:169
#define die(msg)
Definition: pg_test_fsync.c:95
uintptr_t Datum
Definition: postgres.h:411
#define InvalidOid
Definition: postgres_ext.h:36
void BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
Definition: postmaster.c:5661
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5713
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:180
Definition: pg_list.h:51
@ WAIT_EVENT_LOGICAL_LAUNCHER_MAIN
Definition: wait_event.h:44
#define SIGHUP
Definition: win32_port.h:167
int wal_retrieve_retry_interval
Definition: xlog.c:135

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert(), BackgroundWorkerInitializeConnection(), BackgroundWorkerUnblockSignals(), before_shmem_exit(), CHECK_FOR_INTERRUPTS, ConfigReloadPending, Subscription::dbid, DEBUG1, DEFAULT_NAPTIME_PER_CYCLE, die, 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(), MyLatch, MyProcPid, Subscription::name, now(), Subscription::oid, Subscription::owner, PGC_SIGHUP, pqsignal(), ProcessConfigFile(), ResetLatch(), SIGHUP, SignalHandlerForConfigReload(), TimestampDifferenceExceeds(), 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 706 of file launcher.c.

707 {
708  BackgroundWorker bgw;
709 
711  return;
712 
713  memset(&bgw, 0, sizeof(bgw));
717  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
718  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
720  "logical replication launcher");
722  "logical replication launcher");
723  bgw.bgw_restart_time = 5;
724  bgw.bgw_notify_pid = 0;
725  bgw.bgw_main_arg = (Datum) 0;
726 
728 }
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:872
@ 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:55
#define snprintf
Definition: port.h:225
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 735 of file launcher.c.

736 {
737  bool found;
738 
740  ShmemInitStruct("Logical Replication Launcher Data",
742  &found);
743 
744  if (!found)
745  {
746  int slot;
747 
749 
750  /* Initialize memory and spin locks for each worker slot. */
751  for (slot = 0; slot < max_logical_replication_workers; slot++)
752  {
753  LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
754 
755  memset(worker, 0, sizeof(LogicalRepWorker));
756  SpinLockInit(&worker->relmutex);
757  }
758  }
759 }
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:687
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:396
#define SpinLockInit(lock)
Definition: spin.h:60
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:66

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

Referenced by CreateSharedMemoryAndSemaphores().

◆ ApplyLauncherShmemSize()

Size ApplyLauncherShmemSize ( void  )

Definition at line 687 of file launcher.c.

688 {
689  Size size;
690 
691  /*
692  * Need the fixed struct and the array of LogicalRepWorker.
693  */
694  size = sizeof(LogicalRepCtxStruct);
695  size = MAXALIGN(size);
697  sizeof(LogicalRepWorker)));
698  return size;
699 }
#define MAXALIGN(LEN)
Definition: c.h:757
size_t Size
Definition: c.h:540
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().

◆ ApplyLauncherWakeup()

static void ApplyLauncherWakeup ( void  )
static

Definition at line 791 of file launcher.c.

792 {
793  if (LogicalRepCtx->launcher_pid != 0)
795 }
#define kill(pid, sig)
Definition: win32_port.h:464
#define SIGUSR1
Definition: win32_port.h:179

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

Referenced by AtEOXact_ApplyLauncher(), and logicalrep_worker_onexit().

◆ ApplyLauncherWakeupAtCommit()

void ApplyLauncherWakeupAtCommit ( void  )

Definition at line 784 of file launcher.c.

785 {
788 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:77

References on_commit_launcher_wakeup.

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

◆ AtEOXact_ApplyLauncher()

void AtEOXact_ApplyLauncher ( bool  isCommit)

Definition at line 765 of file launcher.c.

766 {
767  if (isCommit)
768  {
771  }
772 
774 }
static void ApplyLauncherWakeup(void)
Definition: launcher.c:791

References ApplyLauncherWakeup(), and on_commit_launcher_wakeup.

Referenced by AbortTransaction(), and CommitTransaction().

◆ get_subscription_list()

static List* get_subscription_list ( void  )
static

Definition at line 87 of file launcher.c.

88 {
89  List *res = NIL;
90  Relation rel;
91  TableScanDesc scan;
92  HeapTuple tup;
93  MemoryContext resultcxt;
94 
95  /* This is the context that we will allocate our output data in */
96  resultcxt = CurrentMemoryContext;
97 
98  /*
99  * Start a transaction so we can access pg_database, and get a snapshot.
100  * We don't have a use for the snapshot itself, but we're interested in
101  * the secondary effect that it sets RecentGlobalXmin. (This is critical
102  * for anything that reads heap pages, because HOT may decide to prune
103  * them even if the process doesn't attempt to modify any tuples.)
104  *
105  * FIXME: This comment is inaccurate / the code buggy. A snapshot that is
106  * not pushed/active does not reliably prevent HOT pruning (->xmin could
107  * e.g. be cleared when cache invalidations are processed).
108  */
110  (void) GetTransactionSnapshot();
111 
112  rel = table_open(SubscriptionRelationId, AccessShareLock);
113  scan = table_beginscan_catalog(rel, 0, NULL);
114 
116  {
118  Subscription *sub;
119  MemoryContext oldcxt;
120 
121  /*
122  * Allocate our results in the caller's context, not the
123  * transaction's. We do this inside the loop, and restore the original
124  * context at the end, so that leaky things like heap_getnext() are
125  * not called in a potentially long-lived context.
126  */
127  oldcxt = MemoryContextSwitchTo(resultcxt);
128 
129  sub = (Subscription *) palloc0(sizeof(Subscription));
130  sub->oid = subform->oid;
131  sub->dbid = subform->subdbid;
132  sub->owner = subform->subowner;
133  sub->enabled = subform->subenabled;
134  sub->name = pstrdup(NameStr(subform->subname));
135  /* We don't fill fields we are not interested in. */
136 
137  res = lappend(res, sub);
138  MemoryContextSwitchTo(oldcxt);
139  }
140 
141  table_endscan(scan);
143 
145 
146  return res;
147 }
#define NameStr(name)
Definition: c.h:681
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition: heapam.c:1296
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:649
List * lappend(List *list, void *datum)
Definition: list.c:336
#define AccessShareLock
Definition: lockdefs.h:36
char * pstrdup(const char *in)
Definition: mcxt.c:1305
void * palloc0(Size size)
Definition: mcxt.c:1099
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
#define NIL
Definition: pg_list.h:65
FormData_pg_subscription * Form_pg_subscription
@ ForwardScanDirection
Definition: sdir.h:26
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:250
void table_close(Relation relation, LOCKMODE lockmode)
Definition: table.c:167
Relation table_open(Oid relationId, LOCKMODE lockmode)
Definition: table.c:39
TableScanDesc table_beginscan_catalog(Relation relation, int nkeys, struct ScanKeyData *key)
Definition: tableam.c:112
static void table_endscan(TableScanDesc scan)
Definition: tableam.h:993
void StartTransactionCommand(void)
Definition: xact.c:2925
void CommitTransactionCommand(void)
Definition: xact.c:3022

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

◆ IsLogicalLauncher()

bool IsLogicalLauncher ( void  )

Definition at line 917 of file launcher.c.

918 {
920 }

References LogicalRepCtxStruct::launcher_pid, LogicalRepCtx, and MyProcPid.

Referenced by ProcessInterrupts().

◆ logicalrep_launcher_onexit()

static void logicalrep_launcher_onexit ( int  code,
Datum  arg 
)
static

Definition at line 632 of file launcher.c.

633 {
635 }

References LogicalRepCtxStruct::launcher_pid, and LogicalRepCtx.

Referenced by ApplyLauncherMain().

◆ logicalrep_sync_worker_count()

int logicalrep_sync_worker_count ( Oid  subid)

Definition at line 663 of file launcher.c.

664 {
665  int i;
666  int res = 0;
667 
668  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
669 
670  /* Search for attached worker for a given subscription id. */
671  for (i = 0; i < max_logical_replication_workers; i++)
672  {
674 
675  if (w->subid == subid && OidIsValid(w->relid))
676  res++;
677  }
678 
679  return res;
680 }
#define OidIsValid(objectId)
Definition: c.h:710
int i
Definition: isn.c:73
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1916

References Assert(), i, LogicalRepCtx, LWLockHeldByMe(), max_logical_replication_workers, OidIsValid, LogicalRepWorker::relid, 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 564 of file launcher.c.

565 {
566  /* Block concurrent access. */
567  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
568 
569  Assert(slot >= 0 && slot < max_logical_replication_workers);
571 
573  {
574  LWLockRelease(LogicalRepWorkerLock);
575  ereport(ERROR,
576  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
577  errmsg("logical replication worker slot %d is empty, cannot attach",
578  slot)));
579  }
580 
582  {
583  LWLockRelease(LogicalRepWorkerLock);
584  ereport(ERROR,
585  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
586  errmsg("logical replication worker slot %d is already used by "
587  "another worker, cannot attach", slot)));
588  }
589 
592 
593  LWLockRelease(LogicalRepWorkerLock);
594 }
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:58
static void logicalrep_worker_onexit(int code, Datum arg)
Definition: launcher.c:643
@ LW_EXCLUSIVE
Definition: lwlock.h:104
PGPROC * MyProc
Definition: proc.c:68

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

◆ logicalrep_worker_cleanup()

static void logicalrep_worker_cleanup ( LogicalRepWorker worker)
static

Definition at line 614 of file launcher.c.

615 {
616  Assert(LWLockHeldByMeInMode(LogicalRepWorkerLock, LW_EXCLUSIVE));
617 
618  worker->in_use = false;
619  worker->proc = NULL;
620  worker->dbid = InvalidOid;
621  worker->userid = InvalidOid;
622  worker->subid = InvalidOid;
623  worker->relid = InvalidOid;
624 }
bool LWLockHeldByMeInMode(LWLock *l, LWLockMode mode)
Definition: lwlock.c:1934

References Assert(), LogicalRepWorker::dbid, LogicalRepWorker::in_use, InvalidOid, LW_EXCLUSIVE, LWLockHeldByMeInMode(), LogicalRepWorker::proc, LogicalRepWorker::relid, LogicalRepWorker::subid, and LogicalRepWorker::userid.

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

◆ logicalrep_worker_detach()

static void logicalrep_worker_detach ( void  )
static

Definition at line 600 of file launcher.c.

601 {
602  /* Block concurrent access. */
603  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
604 
606 
607  LWLockRelease(LogicalRepWorkerLock);
608 }
static void logicalrep_worker_cleanup(LogicalRepWorker *worker)
Definition: launcher.c:614

References logicalrep_worker_cleanup(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), and MyLogicalRepWorker.

Referenced by logicalrep_worker_onexit().

◆ logicalrep_worker_find()

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

Definition at line 214 of file launcher.c.

215 {
216  int i;
217  LogicalRepWorker *res = NULL;
218 
219  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
220 
221  /* Search for attached worker for a given subscription id. */
222  for (i = 0; i < max_logical_replication_workers; i++)
223  {
225 
226  if (w->in_use && w->subid == subid && w->relid == relid &&
227  (!only_running || w->proc))
228  {
229  res = w;
230  break;
231  }
232  }
233 
234  return res;
235 }

References Assert(), i, LogicalRepWorker::in_use, 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()

void logicalrep_worker_launch ( Oid  dbid,
Oid  subid,
const char *  subname,
Oid  userid,
Oid  relid 
)

Definition at line 265 of file launcher.c.

267 {
268  BackgroundWorker bgw;
269  BackgroundWorkerHandle *bgw_handle;
270  uint16 generation;
271  int i;
272  int slot = 0;
273  LogicalRepWorker *worker = NULL;
274  int nsyncworkers;
276 
277  ereport(DEBUG1,
278  (errmsg_internal("starting logical replication worker for subscription \"%s\"",
279  subname)));
280 
281  /* Report this after the initial starting message for consistency. */
282  if (max_replication_slots == 0)
283  ereport(ERROR,
284  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
285  errmsg("cannot start logical replication workers when max_replication_slots = 0")));
286 
287  /*
288  * We need to do the modification of the shared memory under lock so that
289  * we have consistent view.
290  */
291  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
292 
293 retry:
294  /* Find unused worker slot. */
295  for (i = 0; i < max_logical_replication_workers; i++)
296  {
298 
299  if (!w->in_use)
300  {
301  worker = w;
302  slot = i;
303  break;
304  }
305  }
306 
307  nsyncworkers = logicalrep_sync_worker_count(subid);
308 
310 
311  /*
312  * If we didn't find a free slot, try to do garbage collection. The
313  * reason we do this is because if some worker failed to start up and its
314  * parent has crashed while waiting, the in_use state was never cleared.
315  */
316  if (worker == NULL || nsyncworkers >= max_sync_workers_per_subscription)
317  {
318  bool did_cleanup = false;
319 
320  for (i = 0; i < max_logical_replication_workers; i++)
321  {
323 
324  /*
325  * If the worker was marked in use but didn't manage to attach in
326  * time, clean it up.
327  */
328  if (w->in_use && !w->proc &&
331  {
332  elog(WARNING,
333  "logical replication worker for subscription %u took too long to start; canceled",
334  w->subid);
335 
337  did_cleanup = true;
338  }
339  }
340 
341  if (did_cleanup)
342  goto retry;
343  }
344 
345  /*
346  * We don't allow to invoke more sync workers once we have reached the
347  * sync worker limit per subscription. So, just return silently as we
348  * might get here because of an otherwise harmless race condition.
349  */
350  if (OidIsValid(relid) && nsyncworkers >= max_sync_workers_per_subscription)
351  {
352  LWLockRelease(LogicalRepWorkerLock);
353  return;
354  }
355 
356  /*
357  * However if there are no more free worker slots, inform user about it
358  * before exiting.
359  */
360  if (worker == NULL)
361  {
362  LWLockRelease(LogicalRepWorkerLock);
364  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
365  errmsg("out of logical replication worker slots"),
366  errhint("You might need to increase max_logical_replication_workers.")));
367  return;
368  }
369 
370  /* Prepare the worker slot. */
371  worker->launch_time = now;
372  worker->in_use = true;
373  worker->generation++;
374  worker->proc = NULL;
375  worker->dbid = dbid;
376  worker->userid = userid;
377  worker->subid = subid;
378  worker->relid = relid;
379  worker->relstate = SUBREL_STATE_UNKNOWN;
381  worker->stream_fileset = NULL;
382  worker->last_lsn = InvalidXLogRecPtr;
385  worker->reply_lsn = InvalidXLogRecPtr;
386  TIMESTAMP_NOBEGIN(worker->reply_time);
387 
388  /* Before releasing lock, remember generation for future identification. */
389  generation = worker->generation;
390 
391  LWLockRelease(LogicalRepWorkerLock);
392 
393  /* Register the new dynamic worker. */
394  memset(&bgw, 0, sizeof(bgw));
398  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
399  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyWorkerMain");
400  if (OidIsValid(relid))
402  "logical replication worker for subscription %u sync %u", subid, relid);
403  else
405  "logical replication worker for subscription %u", subid);
406  snprintf(bgw.bgw_type, BGW_MAXLEN, "logical replication worker");
407 
410  bgw.bgw_main_arg = Int32GetDatum(slot);
411 
412  if (!RegisterDynamicBackgroundWorker(&bgw, &bgw_handle))
413  {
414  /* Failed to start worker, so clean up the worker slot. */
415  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
416  Assert(generation == worker->generation);
418  LWLockRelease(LogicalRepWorkerLock);
419 
421  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
422  errmsg("out of background worker slots"),
423  errhint("You might need to increase max_worker_processes.")));
424  return;
425  }
426 
427  /* Now wait until it attaches. */
428  WaitForReplicationWorkerAttach(worker, generation, bgw_handle);
429 }
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition: bgworker.c:956
#define BGW_NEVER_RESTART
Definition: bgworker.h:85
unsigned short uint16
Definition: c.h:440
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:151
int errhint(const char *fmt,...)
Definition: elog.c:1151
#define WARNING
Definition: elog.h:30
#define elog(elevel,...)
Definition: elog.h:218
static void WaitForReplicationWorkerAttach(LogicalRepWorker *worker, uint16 generation, BackgroundWorkerHandle *handle)
Definition: launcher.c:156
int max_sync_workers_per_subscription
Definition: launcher.c:56
int logicalrep_sync_worker_count(Oid subid)
Definition: launcher.c:663
NameData subname
#define Int32GetDatum(X)
Definition: postgres.h:523
int max_replication_slots
Definition: slot.c:100
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:91
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

References Assert(), 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, elog, ereport, errcode(), errhint(), errmsg(), errmsg_internal(), ERROR, LogicalRepWorker::generation, GetCurrentTimestamp(), i, LogicalRepWorker::in_use, Int32GetDatum, InvalidXLogRecPtr, LogicalRepWorker::last_lsn, LogicalRepWorker::last_recv_time, LogicalRepWorker::last_send_time, LogicalRepWorker::launch_time, logicalrep_sync_worker_count(), logicalrep_worker_cleanup(), LogicalRepCtx, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), max_logical_replication_workers, max_replication_slots, max_sync_workers_per_subscription, MyProcPid, now(), OidIsValid, 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::userid, WaitForReplicationWorkerAttach(), wal_receiver_timeout, WARNING, and LogicalRepCtxStruct::workers.

Referenced by ApplyLauncherMain(), and process_syncing_tables_for_apply().

◆ logicalrep_worker_onexit()

static void logicalrep_worker_onexit ( int  code,
Datum  arg 
)
static

Definition at line 643 of file launcher.c.

644 {
645  /* Disconnect gracefully from the remote side. */
648 
650 
651  /* Cleanup fileset used for streaming transactions. */
652  if (MyLogicalRepWorker->stream_fileset != NULL)
654 
656 }
WalReceiverConn * LogRepWorkerWalRcvConn
Definition: worker.c:251
void FileSetDeleteAll(FileSet *fileset)
Definition: fileset.c:152
static void logicalrep_worker_detach(void)
Definition: launcher.c:600
#define walrcv_disconnect(conn)
Definition: walreceiver.h:432

References ApplyLauncherWakeup(), FileSetDeleteAll(), 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 436 of file launcher.c.

437 {
438  LogicalRepWorker *worker;
439  uint16 generation;
440 
441  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
442 
443  worker = logicalrep_worker_find(subid, relid, false);
444 
445  /* No worker, nothing to do. */
446  if (!worker)
447  {
448  LWLockRelease(LogicalRepWorkerLock);
449  return;
450  }
451 
452  /*
453  * Remember which generation was our worker so we can check if what we see
454  * is still the same one.
455  */
456  generation = worker->generation;
457 
458  /*
459  * If we found a worker but it does not have proc set then it is still
460  * starting up; wait for it to finish starting and then kill it.
461  */
462  while (worker->in_use && !worker->proc)
463  {
464  int rc;
465 
466  LWLockRelease(LogicalRepWorkerLock);
467 
468  /* Wait a bit --- we don't expect to have to wait long. */
469  rc = WaitLatch(MyLatch,
472 
473  if (rc & WL_LATCH_SET)
474  {
477  }
478 
479  /* Recheck worker status. */
480  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
481 
482  /*
483  * Check whether the worker slot is no longer used, which would mean
484  * that the worker has exited, or whether the worker generation is
485  * different, meaning that a different worker has taken the slot.
486  */
487  if (!worker->in_use || worker->generation != generation)
488  {
489  LWLockRelease(LogicalRepWorkerLock);
490  return;
491  }
492 
493  /* Worker has assigned proc, so it has started. */
494  if (worker->proc)
495  break;
496  }
497 
498  /* Now terminate the worker ... */
499  kill(worker->proc->pid, SIGTERM);
500 
501  /* ... and wait for it to die. */
502  for (;;)
503  {
504  int rc;
505 
506  /* is it gone? */
507  if (!worker->proc || worker->generation != generation)
508  break;
509 
510  LWLockRelease(LogicalRepWorkerLock);
511 
512  /* Wait a bit --- we don't expect to have to wait long. */
513  rc = WaitLatch(MyLatch,
516 
517  if (rc & WL_LATCH_SET)
518  {
521  }
522 
523  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
524  }
525 
526  LWLockRelease(LogicalRepWorkerLock);
527 }
int pid
Definition: proc.h:184
@ WAIT_EVENT_BGWORKER_STARTUP
Definition: wait_event.h:87
@ WAIT_EVENT_BGWORKER_SHUTDOWN
Definition: wait_event.h:86

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

Referenced by AlterSubscription_refresh(), and DropSubscription().

◆ logicalrep_worker_wakeup()

void logicalrep_worker_wakeup ( Oid  subid,
Oid  relid 
)

Definition at line 533 of file launcher.c.

534 {
535  LogicalRepWorker *worker;
536 
537  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
538 
539  worker = logicalrep_worker_find(subid, relid, true);
540 
541  if (worker)
543 
544  LWLockRelease(LogicalRepWorkerLock);
545 }
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition: launcher.c:553

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

Referenced by pg_attribute_noreturn().

◆ logicalrep_worker_wakeup_ptr()

void logicalrep_worker_wakeup_ptr ( LogicalRepWorker worker)

Definition at line 553 of file launcher.c.

554 {
555  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
556 
557  SetLatch(&worker->proc->procLatch);
558 }
void SetLatch(Latch *latch)
Definition: latch.c:566
Latch procLatch
Definition: proc.h:168

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

Referenced by 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 
)

Definition at line 242 of file launcher.c.

243 {
244  int i;
245  List *res = NIL;
246 
247  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
248 
249  /* Search for attached worker for a given subscription id. */
250  for (i = 0; i < max_logical_replication_workers; i++)
251  {
253 
254  if (w->in_use && w->subid == subid && (!only_running || w->proc))
255  res = lappend(res, w);
256  }
257 
258  return res;
259 }

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

Referenced by DropSubscription().

◆ pg_stat_get_subscription()

Datum pg_stat_get_subscription ( PG_FUNCTION_ARGS  )

Definition at line 926 of file launcher.c.

927 {
928 #define PG_STAT_GET_SUBSCRIPTION_COLS 8
929  Oid subid = PG_ARGISNULL(0) ? InvalidOid : PG_GETARG_OID(0);
930  int i;
931  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
932 
933  SetSingleFuncCall(fcinfo, 0);
934 
935  /* Make sure we get consistent view of the workers. */
936  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
937 
938  for (i = 0; i <= max_logical_replication_workers; i++)
939  {
940  /* for each row */
942  bool nulls[PG_STAT_GET_SUBSCRIPTION_COLS];
943  int worker_pid;
944  LogicalRepWorker worker;
945 
946  memcpy(&worker, &LogicalRepCtx->workers[i],
947  sizeof(LogicalRepWorker));
948  if (!worker.proc || !IsBackendPid(worker.proc->pid))
949  continue;
950 
951  if (OidIsValid(subid) && worker.subid != subid)
952  continue;
953 
954  worker_pid = worker.proc->pid;
955 
956  MemSet(values, 0, sizeof(values));
957  MemSet(nulls, 0, sizeof(nulls));
958 
959  values[0] = ObjectIdGetDatum(worker.subid);
960  if (OidIsValid(worker.relid))
961  values[1] = ObjectIdGetDatum(worker.relid);
962  else
963  nulls[1] = true;
964  values[2] = Int32GetDatum(worker_pid);
965  if (XLogRecPtrIsInvalid(worker.last_lsn))
966  nulls[3] = true;
967  else
968  values[3] = LSNGetDatum(worker.last_lsn);
969  if (worker.last_send_time == 0)
970  nulls[4] = true;
971  else
973  if (worker.last_recv_time == 0)
974  nulls[5] = true;
975  else
977  if (XLogRecPtrIsInvalid(worker.reply_lsn))
978  nulls[6] = true;
979  else
980  values[6] = LSNGetDatum(worker.reply_lsn);
981  if (worker.reply_time == 0)
982  nulls[7] = true;
983  else
985 
986  tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
987  values, nulls);
988 
989  /*
990  * If only a single subscription was requested, and we found it,
991  * break.
992  */
993  if (OidIsValid(subid))
994  break;
995  }
996 
997  LWLockRelease(LogicalRepWorkerLock);
998 
999  return (Datum) 0;
1000 }
static Datum values[MAXATTR]
Definition: bootstrap.c:156
#define MemSet(start, val, len)
Definition: c.h:1008
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
void SetSingleFuncCall(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
#define PG_STAT_GET_SUBSCRIPTION_COLS
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
unsigned int Oid
Definition: postgres_ext.h:31
bool IsBackendPid(int pid)
Definition: procarray.c:3265
TupleDesc setDesc
Definition: execnodes.h:317
Tuplestorestate * setResult
Definition: execnodes.h:316
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29

References i, Int32GetDatum, InvalidOid, IsBackendPid(), LogicalRepWorker::last_lsn, LogicalRepWorker::last_recv_time, LogicalRepWorker::last_send_time, LogicalRepCtx, LSNGetDatum, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_logical_replication_workers, MemSet, 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, SetSingleFuncCall(), LogicalRepWorker::subid, TimestampTzGetDatum, tuplestore_putvalues(), values, LogicalRepCtxStruct::workers, and XLogRecPtrIsInvalid.

◆ WaitForReplicationWorkerAttach()

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

Definition at line 156 of file launcher.c.

159 {
161  int rc;
162 
163  for (;;)
164  {
165  pid_t pid;
166 
168 
169  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
170 
171  /* Worker either died or has started; no need to do anything. */
172  if (!worker->in_use || worker->proc)
173  {
174  LWLockRelease(LogicalRepWorkerLock);
175  return;
176  }
177 
178  LWLockRelease(LogicalRepWorkerLock);
179 
180  /* Check if worker has died before attaching, and clean up after it. */
181  status = GetBackgroundWorkerPid(handle, &pid);
182 
183  if (status == BGWH_STOPPED)
184  {
185  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
186  /* Ensure that this was indeed the worker we waited for. */
187  if (generation == worker->generation)
189  LWLockRelease(LogicalRepWorkerLock);
190  return;
191  }
192 
193  /*
194  * We need timeout because we generally don't get notified via latch
195  * about the worker attach. But we don't expect to have to wait long.
196  */
197  rc = WaitLatch(MyLatch,
200 
201  if (rc & WL_LATCH_SET)
202  {
205  }
206  }
207 }
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1068
BgwHandleStatus
Definition: bgworker.h:104
@ BGWH_STOPPED
Definition: bgworker.h:107
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:229

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(), status(), WAIT_EVENT_BGWORKER_STARTUP, WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and WL_TIMEOUT.

Referenced by logicalrep_worker_launch().

Variable Documentation

◆ LogicalRepCtx

◆ max_logical_replication_workers

◆ max_sync_workers_per_subscription

int max_sync_workers_per_subscription = 2

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

Referenced by ApplyLauncherWakeupAtCommit(), and AtEOXact_ApplyLauncher().