PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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)
 
Datum pg_stat_get_subscription (PG_FUNCTION_ARGS)
 
static Listget_subscription_list (void)
 
static void WaitForReplicationWorkerAttach (LogicalRepWorker *worker, BackgroundWorkerHandle *handle)
 
LogicalRepWorkerlogicalrep_worker_find (Oid subid, Oid relid, 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)
 
void logicalrep_worker_sigterm (SIGNAL_ARGS)
 
void logicalrep_worker_sighup (SIGNAL_ARGS)
 
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)
 

Variables

int max_logical_replication_workers = 4
 
int max_sync_workers_per_subscription = 2
 
LogicalRepWorkerMyLogicalRepWorker = NULL
 
LogicalRepCtxStructLogicalRepCtx
 
volatile sig_atomic_t got_SIGHUP = false
 
volatile sig_atomic_t got_SIGTERM = false
 
static bool on_commit_launcher_wakeup = false
 

Macro Definition Documentation

#define DEFAULT_NAPTIME_PER_CYCLE   180000L

Definition at line 58 of file launcher.c.

Referenced by ApplyLauncherMain().

#define PG_STAT_GET_SUBSCRIPTION_COLS   8

Typedef Documentation

Function Documentation

void ApplyLauncherMain ( Datum  main_arg)

Definition at line 786 of file launcher.c.

References ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE, ALLOCSET_DEFAULT_MINSIZE, AllocSetContextCreate(), BackgroundWorkerInitializeConnection(), BackgroundWorkerUnblockSignals(), before_shmem_exit(), BackgroundWorker::bgw_name, Subscription::dbid, DEBUG1, DEFAULT_NAPTIME_PER_CYCLE, Subscription::enabled, ereport, errmsg(), get_subscription_list(), GetCurrentTimestamp(), got_SIGHUP, got_SIGTERM, InvalidOid, LogicalRepCtxStruct::launcher_pid, lfirst, logicalrep_launcher_onexit(), logicalrep_worker_find(), logicalrep_worker_launch(), logicalrep_worker_sighup(), logicalrep_worker_sigterm(), LW_SHARED, LWLockAcquire(), LWLockRelease(), MemoryContextDelete(), MemoryContextSwitchTo(), MyBgworkerEntry, MyProc, MyProcPid, Subscription::name, now(), NULL, Subscription::oid, Subscription::owner, PGC_S_SESSION, PGC_SIGHUP, PGC_USERSET, pqsignal(), proc_exit(), ProcessConfigFile(), PGPROC::procLatch, ResetLatch(), SetConfigOption(), SIGHUP, TimestampDifferenceExceeds(), TopMemoryContext, WAIT_EVENT_LOGICAL_LAUNCHER_MAIN, WaitLatch(), wal_retrieve_retry_interval, WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_TIMEOUT.

787 {
788  TimestampTz last_start_time = 0;
789 
790  ereport(DEBUG1,
791  (errmsg("logical replication launcher started")));
792 
794 
795  /* Establish signal handlers. */
799 
800  /* Make it easy to identify our processes. */
801  SetConfigOption("application_name", MyBgworkerEntry->bgw_name,
803 
805 
806  /*
807  * Establish connection to nailed catalogs (we only ever access
808  * pg_subscription).
809  */
811 
812  /* Enter main loop */
813  while (!got_SIGTERM)
814  {
815  int rc;
816  List *sublist;
817  ListCell *lc;
818  MemoryContext subctx;
819  MemoryContext oldctx;
821  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
822 
823  now = GetCurrentTimestamp();
824 
825  /* Limit the start retry to once a wal_retrieve_retry_interval */
826  if (TimestampDifferenceExceeds(last_start_time, now,
828  {
829  /* Use temporary context for the database list and worker info. */
831  "Logical Replication Launcher sublist",
835  oldctx = MemoryContextSwitchTo(subctx);
836 
837  /* search for subscriptions to start or stop. */
838  sublist = get_subscription_list();
839 
840  /* Start the missing workers for enabled subscriptions. */
841  foreach(lc, sublist)
842  {
843  Subscription *sub = (Subscription *) lfirst(lc);
844  LogicalRepWorker *w;
845 
846  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
847  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
848  LWLockRelease(LogicalRepWorkerLock);
849 
850  if (sub->enabled && w == NULL)
851  {
852  last_start_time = now;
853  wait_time = wal_retrieve_retry_interval;
854 
855  logicalrep_worker_launch(sub->dbid, sub->oid, sub->name,
856  sub->owner, InvalidOid);
857  }
858  }
859 
860  /* Switch back to original memory context. */
861  MemoryContextSwitchTo(oldctx);
862  /* Clean the temporary memory. */
863  MemoryContextDelete(subctx);
864  }
865  else
866  {
867  /*
868  * The wait in previous cycle was interrupted in less than
869  * wal_retrieve_retry_interval since last worker was started, this
870  * usually means crash of the worker, so we should retry in
871  * wal_retrieve_retry_interval again.
872  */
873  wait_time = wal_retrieve_retry_interval;
874  }
875 
876  /* Wait for more work. */
877  rc = WaitLatch(&MyProc->procLatch,
879  wait_time,
881 
882  /* emergency bailout if postmaster has died */
883  if (rc & WL_POSTMASTER_DEATH)
884  proc_exit(1);
885 
886  if (got_SIGHUP)
887  {
888  got_SIGHUP = false;
890  }
891 
893  }
894 
896 
897  /* ... and if it returns, we're done */
898  ereport(DEBUG1,
899  (errmsg("logical replication launcher shutting down")));
900 
901  proc_exit(0);
902 }
volatile sig_atomic_t got_SIGHUP
Definition: launcher.c:83
void logicalrep_worker_sighup(SIGNAL_ARGS)
Definition: launcher.c:636
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
#define DEBUG1
Definition: elog.h:25
int MyProcPid
Definition: globals.c:38
#define WL_TIMEOUT
Definition: latch.h:127
void ProcessConfigFile(GucContext context)
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
PGPROC * MyProc
Definition: proc.c:67
int64 TimestampTz
Definition: timestamp.h:39
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void proc_exit(int code)
Definition: ipc.c:99
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:189
void ResetLatch(volatile Latch *latch)
Definition: latch.c:498
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1649
#define ALLOCSET_DEFAULT_MINSIZE
Definition: memutils.h:162
Latch procLatch
Definition: proc.h:103
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:336
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:6672
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:320
static void logicalrep_launcher_onexit(int code, Datum arg)
Definition: launcher.c:604
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:43
Definition: guc.h:72
int wal_retrieve_retry_interval
Definition: xlog.c:107
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
#define DEFAULT_NAPTIME_PER_CYCLE
Definition: launcher.c:58
uintptr_t Datum
Definition: postgres.h:372
void logicalrep_worker_sigterm(SIGNAL_ARGS)
Definition: launcher.c:622
#define SIGHUP
Definition: win32.h:188
#define InvalidOid
Definition: postgres_ext.h:36
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
static List * get_subscription_list(void)
Definition: launcher.c:98
#define NULL
Definition: c.h:229
volatile sig_atomic_t got_SIGTERM
Definition: launcher.c:84
void logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid)
Definition: launcher.c:252
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define lfirst(lc)
Definition: pg_list.h:106
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:225
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int errmsg(const char *fmt,...)
Definition: elog.c:797
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
#define ALLOCSET_DEFAULT_INITSIZE
Definition: memutils.h:163
#define ALLOCSET_DEFAULT_MAXSIZE
Definition: memutils.h:164
Definition: pg_list.h:45
#define WL_LATCH_SET
Definition: latch.h:124
void BackgroundWorkerInitializeConnection(char *dbname, char *username)
Definition: postmaster.c:5470
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1534
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5522
void ApplyLauncherRegister ( void  )

Definition at line 696 of file launcher.c.

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, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_SHMEM_ACCESS, BgWorkerStart_RecoveryFinished, max_logical_replication_workers, RegisterBackgroundWorker(), and snprintf().

Referenced by PostmasterMain().

697 {
698  BackgroundWorker bgw;
699 
701  return;
702 
703  memset(&bgw, 0, sizeof(bgw));
707  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
708  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
710  "logical replication launcher");
711  bgw.bgw_restart_time = 5;
712  bgw.bgw_notify_pid = 0;
713  bgw.bgw_main_arg = (Datum) 0;
714 
716 }
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:849
int bgw_restart_time
Definition: bgworker.h:93
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:52
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:95
Datum bgw_main_arg
Definition: bgworker.h:96
uintptr_t Datum
Definition: postgres.h:372
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
int max_logical_replication_workers
Definition: launcher.c:60
#define BGW_MAXLEN
Definition: bgworker.h:85
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:92
pid_t bgw_notify_pid
Definition: bgworker.h:98
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:94
void ApplyLauncherShmemInit ( void  )

Definition at line 723 of file launcher.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

724 {
725  bool found;
726 
728  ShmemInitStruct("Logical Replication Launcher Data",
730  &found);
731 
732  if (!found)
733  {
734  int slot;
735 
737 
738  /* Initialize memory and spin locks for each worker slot. */
739  for (slot = 0; slot < max_logical_replication_workers; slot++)
740  {
741  LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
742 
743  memset(worker, 0, sizeof(LogicalRepWorker));
744  SpinLockInit(&worker->relmutex);
745  }
746  }
747 }
#define SpinLockInit(lock)
Definition: spin.h:60
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:71
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:677
int max_logical_replication_workers
Definition: launcher.c:60
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
Size ApplyLauncherShmemSize ( void  )

Definition at line 677 of file launcher.c.

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

Referenced by ApplyLauncherShmemInit(), and CreateSharedMemoryAndSemaphores().

678 {
679  Size size;
680 
681  /*
682  * Need the fixed struct and the array of LogicalRepWorker.
683  */
684  size = sizeof(LogicalRepCtxStruct);
685  size = MAXALIGN(size);
687  sizeof(LogicalRepWorker)));
688  return size;
689 }
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
int max_logical_replication_workers
Definition: launcher.c:60
size_t Size
Definition: c.h:356
#define MAXALIGN(LEN)
Definition: c.h:588
struct LogicalRepCtxStruct LogicalRepCtxStruct
static void ApplyLauncherWakeup ( void  )
static

Definition at line 776 of file launcher.c.

References LogicalRepCtxStruct::launcher_pid, and SIGUSR1.

Referenced by AtEOXact_ApplyLauncher().

777 {
778  if (LogicalRepCtx->launcher_pid != 0)
780 }
#define SIGUSR1
Definition: win32.h:202
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
void ApplyLauncherWakeupAtCommit ( void  )

Definition at line 769 of file launcher.c.

References on_commit_launcher_wakeup.

Referenced by AlterSubscription(), and CreateSubscription().

770 {
773 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:86
void AtEOXact_ApplyLauncher ( bool  isCommit)

Definition at line 753 of file launcher.c.

References ApplyLauncherWakeup(), and on_commit_launcher_wakeup.

Referenced by AbortTransaction(), and CommitTransaction().

754 {
755  if (isCommit && on_commit_launcher_wakeup)
757 
759 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:86
static void ApplyLauncherWakeup(void)
Definition: launcher.c:776
static List* get_subscription_list ( void  )
static

Definition at line 98 of file launcher.c.

References AccessShareLock, CommitTransactionCommand(), CurrentMemoryContext, Subscription::dbid, Subscription::enabled, ForwardScanDirection, GETSTRUCT, GetTransactionSnapshot(), heap_beginscan_catalog(), heap_close, heap_endscan(), heap_getnext(), heap_open(), HeapTupleGetOid, HeapTupleIsValid, lappend(), MemoryContextSwitchTo(), Subscription::name, NameStr, NIL, NULL, Subscription::oid, Subscription::owner, palloc0(), pstrdup(), StartTransactionCommand(), and SubscriptionRelationId.

Referenced by ApplyLauncherMain().

99 {
100  List *res = NIL;
101  Relation rel;
102  HeapScanDesc scan;
103  HeapTuple tup;
104  MemoryContext resultcxt;
105 
106  /* This is the context that we will allocate our output data in */
107  resultcxt = CurrentMemoryContext;
108 
109  /*
110  * Start a transaction so we can access pg_database, and get a snapshot.
111  * We don't have a use for the snapshot itself, but we're interested in
112  * the secondary effect that it sets RecentGlobalXmin. (This is critical
113  * for anything that reads heap pages, because HOT may decide to prune
114  * them even if the process doesn't attempt to modify any tuples.)
115  */
117  (void) GetTransactionSnapshot();
118 
120  scan = heap_beginscan_catalog(rel, 0, NULL);
121 
123  {
125  Subscription *sub;
126  MemoryContext oldcxt;
127 
128  /*
129  * Allocate our results in the caller's context, not the
130  * transaction's. We do this inside the loop, and restore the original
131  * context at the end, so that leaky things like heap_getnext() are
132  * not called in a potentially long-lived context.
133  */
134  oldcxt = MemoryContextSwitchTo(resultcxt);
135 
136  sub = (Subscription *) palloc0(sizeof(Subscription));
137  sub->oid = HeapTupleGetOid(tup);
138  sub->dbid = subform->subdbid;
139  sub->owner = subform->subowner;
140  sub->enabled = subform->subenabled;
141  sub->name = pstrdup(NameStr(subform->subname));
142  /* We don't fill fields we are not interested in. */
143 
144  res = lappend(res, sub);
145  MemoryContextSwitchTo(oldcxt);
146  }
147 
148  heap_endscan(scan);
150 
152 
153  return res;
154 }
#define NIL
Definition: pg_list.h:69
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
void heap_endscan(HeapScanDesc scan)
Definition: heapam.c:1578
char * pstrdup(const char *in)
Definition: mcxt.c:1077
void CommitTransactionCommand(void)
Definition: xact.c:2749
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
#define heap_close(r, l)
Definition: heapam.h:97
FormData_pg_subscription * Form_pg_subscription
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:300
#define SubscriptionRelationId
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
HeapScanDesc heap_beginscan_catalog(Relation relation, int nkeys, ScanKey key)
Definition: heapam.c:1399
List * lappend(List *list, void *datum)
Definition: list.c:128
void * palloc0(Size size)
Definition: mcxt.c:878
HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction)
Definition: heapam.c:1794
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1284
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:229
void StartTransactionCommand(void)
Definition: xact.c:2679
#define NameStr(name)
Definition: c.h:499
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
Definition: pg_list.h:45
static void logicalrep_launcher_onexit ( int  code,
Datum  arg 
)
static

Definition at line 604 of file launcher.c.

References LogicalRepCtxStruct::launcher_pid.

Referenced by ApplyLauncherMain().

605 {
607 }
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
int logicalrep_sync_worker_count ( Oid  subid)

Definition at line 653 of file launcher.c.

References Assert, i, LWLockHeldByMe(), max_logical_replication_workers, OidIsValid, LogicalRepWorker::relid, LogicalRepWorker::subid, and LogicalRepCtxStruct::workers.

Referenced by logicalrep_worker_launch(), and process_syncing_tables_for_apply().

654 {
655  int i;
656  int res = 0;
657 
658  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
659 
660  /* Search for attached worker for a given subscription id. */
661  for (i = 0; i < max_logical_replication_workers; i++)
662  {
664 
665  if (w->subid == subid && OidIsValid(w->relid))
666  res++;
667  }
668 
669  return res;
670 }
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1831
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:71
#define OidIsValid(objectId)
Definition: c.h:538
#define Assert(condition)
Definition: c.h:675
int max_logical_replication_workers
Definition: launcher.c:60
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
int i
void logicalrep_worker_attach ( int  slot)

Definition at line 536 of file launcher.c.

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

Referenced by ApplyWorkerMain().

537 {
538  /* Block concurrent access. */
539  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
540 
541  Assert(slot >= 0 && slot < max_logical_replication_workers);
543 
545  {
546  LWLockRelease(LogicalRepWorkerLock);
547  ereport(ERROR,
548  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
549  errmsg("logical replication worker slot %d is empty, cannot attach",
550  slot)));
551  }
552 
554  {
555  LWLockRelease(LogicalRepWorkerLock);
556  ereport(ERROR,
557  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
558  errmsg("logical replication worker slot %d is already used by "
559  "another worker, cannot attach", slot)));
560  }
561 
564 
565  LWLockRelease(LogicalRepWorkerLock);
566 }
PGPROC * MyProc
Definition: proc.c:67
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:71
int errcode(int sqlerrcode)
Definition: elog.c:575
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:63
#define ERROR
Definition: elog.h:43
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:320
#define ereport(elevel, rest)
Definition: elog.h:122
static void logicalrep_worker_onexit(int code, Datum arg)
Definition: launcher.c:615
uintptr_t Datum
Definition: postgres.h:372
#define Assert(condition)
Definition: c.h:675
int max_logical_replication_workers
Definition: launcher.c:60
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int errmsg(const char *fmt,...)
Definition: elog.c:797
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
static void logicalrep_worker_cleanup ( LogicalRepWorker worker)
static

Definition at line 586 of file launcher.c.

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

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

587 {
588  Assert(LWLockHeldByMeInMode(LogicalRepWorkerLock, LW_EXCLUSIVE));
589 
590  worker->in_use = false;
591  worker->proc = NULL;
592  worker->dbid = InvalidOid;
593  worker->userid = InvalidOid;
594  worker->subid = InvalidOid;
595  worker->relid = InvalidOid;
596 }
bool LWLockHeldByMeInMode(LWLock *l, LWLockMode mode)
Definition: lwlock.c:1849
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
static void logicalrep_worker_detach ( void  )
static

Definition at line 572 of file launcher.c.

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

Referenced by logicalrep_worker_onexit().

573 {
574  /* Block concurrent access. */
575  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
576 
578 
579  LWLockRelease(LogicalRepWorkerLock);
580 }
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:63
static void logicalrep_worker_cleanup(LogicalRepWorker *worker)
Definition: launcher.c:586
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
LogicalRepWorker* logicalrep_worker_find ( Oid  subid,
Oid  relid,
bool  only_running 
)

Definition at line 225 of file launcher.c.

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

Referenced by ApplyLauncherMain(), logicalrep_worker_stop(), logicalrep_worker_wakeup(), process_syncing_tables_for_apply(), and wait_for_sync_status_change().

226 {
227  int i;
228  LogicalRepWorker *res = NULL;
229 
230  Assert(LWLockHeldByMe(LogicalRepWorkerLock));
231 
232  /* Search for attached worker for a given subscription id. */
233  for (i = 0; i < max_logical_replication_workers; i++)
234  {
236 
237  if (w->in_use && w->subid == subid && w->relid == relid &&
238  (!only_running || w->proc))
239  {
240  res = w;
241  break;
242  }
243  }
244 
245  return res;
246 }
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1831
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:71
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
int max_logical_replication_workers
Definition: launcher.c:60
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
int i
void logicalrep_worker_launch ( Oid  dbid,
Oid  subid,
const char *  subname,
Oid  userid,
Oid  relid 
)

Definition at line 252 of file launcher.c.

References 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, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_SHMEM_ACCESS, BgWorkerStart_RecoveryFinished, LogicalRepWorker::dbid, DEBUG1, elog, ereport, errcode(), errhint(), errmsg(), 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(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), max_logical_replication_workers, max_replication_slots, max_sync_workers_per_subscription, MyProcPid, now(), NULL, OidIsValid, LogicalRepWorker::proc, RegisterDynamicBackgroundWorker(), LogicalRepWorker::relid, LogicalRepWorker::relstate, LogicalRepWorker::relstate_lsn, LogicalRepWorker::reply_lsn, LogicalRepWorker::reply_time, snprintf(), LogicalRepWorker::subid, SUBREL_STATE_UNKNOWN, TIMESTAMP_NOBEGIN, TimestampDifferenceExceeds(), LogicalRepWorker::userid, WaitForReplicationWorkerAttach(), wal_receiver_timeout, WARNING, and LogicalRepCtxStruct::workers.

Referenced by ApplyLauncherMain(), and process_syncing_tables_for_apply().

254 {
255  BackgroundWorker bgw;
256  BackgroundWorkerHandle *bgw_handle;
257  int i;
258  int slot = 0;
259  LogicalRepWorker *worker = NULL;
260  int nsyncworkers;
262 
263  ereport(DEBUG1,
264  (errmsg("starting logical replication worker for subscription \"%s\"",
265  subname)));
266 
267  /* Report this after the initial starting message for consistency. */
268  if (max_replication_slots == 0)
269  ereport(ERROR,
270  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
271  errmsg("cannot start logical replication workers when max_replication_slots = 0")));
272 
273  /*
274  * We need to do the modification of the shared memory under lock so that
275  * we have consistent view.
276  */
277  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
278 
279 retry:
280  /* Find unused worker slot. */
281  for (i = 0; i < max_logical_replication_workers; i++)
282  {
284 
285  if (!w->in_use)
286  {
287  worker = w;
288  slot = i;
289  break;
290  }
291  }
292 
293  nsyncworkers = logicalrep_sync_worker_count(subid);
294 
295  now = GetCurrentTimestamp();
296 
297  /*
298  * If we didn't find a free slot, try to do garbage collection. The
299  * reason we do this is because if some worker failed to start up and its
300  * parent has crashed while waiting, the in_use state was never cleared.
301  */
302  if (worker == NULL || nsyncworkers >= max_sync_workers_per_subscription)
303  {
304  bool did_cleanup = false;
305 
306  for (i = 0; i < max_logical_replication_workers; i++)
307  {
309 
310  /*
311  * If the worker was marked in use but didn't manage to attach in
312  * time, clean it up.
313  */
314  if (w->in_use && !w->proc &&
317  {
318  elog(WARNING,
319  "logical replication worker for subscription %u took too long to start; canceled",
320  w->subid);
321 
323  did_cleanup = true;
324  }
325  }
326 
327  if (did_cleanup)
328  goto retry;
329  }
330 
331  /*
332  * If we reached the sync worker limit per subscription, just exit
333  * silently as we might get here because of an otherwise harmless race
334  * condition.
335  */
336  if (nsyncworkers >= max_sync_workers_per_subscription)
337  {
338  LWLockRelease(LogicalRepWorkerLock);
339  return;
340  }
341 
342  /*
343  * However if there are no more free worker slots, inform user about it
344  * before exiting.
345  */
346  if (worker == NULL)
347  {
348  LWLockRelease(LogicalRepWorkerLock);
350  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
351  errmsg("out of logical replication worker slots"),
352  errhint("You might need to increase max_logical_replication_workers.")));
353  return;
354  }
355 
356  /* Prepare the worker slot. */
357  worker->launch_time = now;
358  worker->in_use = true;
359  worker->generation++;
360  worker->proc = NULL;
361  worker->dbid = dbid;
362  worker->userid = userid;
363  worker->subid = subid;
364  worker->relid = relid;
365  worker->relstate = SUBREL_STATE_UNKNOWN;
367  worker->last_lsn = InvalidXLogRecPtr;
370  worker->reply_lsn = InvalidXLogRecPtr;
371  TIMESTAMP_NOBEGIN(worker->reply_time);
372 
373  LWLockRelease(LogicalRepWorkerLock);
374 
375  /* Register the new dynamic worker. */
376  memset(&bgw, 0, sizeof(bgw));
380  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
381  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyWorkerMain");
382  if (OidIsValid(relid))
384  "logical replication worker for subscription %u sync %u", subid, relid);
385  else
387  "logical replication worker for subscription %u", subid);
388 
391  bgw.bgw_main_arg = Int32GetDatum(slot);
392 
393  if (!RegisterDynamicBackgroundWorker(&bgw, &bgw_handle))
394  {
396  (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED),
397  errmsg("out of background worker slots"),
398  errhint("You might need to increase max_worker_processes.")));
399  return;
400  }
401 
402  /* Now wait until it attaches. */
403  WaitForReplicationWorkerAttach(worker, bgw_handle);
404 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define DEBUG1
Definition: elog.h:25
int MyProcPid
Definition: globals.c:38
int errhint(const char *fmt,...)
Definition: elog.c:987
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
int64 TimestampTz
Definition: timestamp.h:39
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:71
TimestampTz last_send_time
XLogRecPtr last_lsn
int bgw_restart_time
Definition: bgworker.h:93
int errcode(int sqlerrcode)
Definition: elog.c:575
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:52
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1649
#define OidIsValid(objectId)
Definition: c.h:538
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:95
int wal_receiver_timeout
Definition: walreceiver.c:75
XLogRecPtr relstate_lsn
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
Datum bgw_main_arg
Definition: bgworker.h:96
#define ERROR
Definition: elog.h:43
int max_sync_workers_per_subscription
Definition: launcher.c:61
XLogRecPtr reply_lsn
static void logicalrep_worker_cleanup(LogicalRepWorker *worker)
Definition: launcher.c:586
#define SUBREL_STATE_UNKNOWN
#define BGW_NEVER_RESTART
Definition: bgworker.h:84
#define TIMESTAMP_NOBEGIN(j)
Definition: timestamp.h:112
#define ereport(elevel, rest)
Definition: elog.h:122
#define WARNING
Definition: elog.h:40
int logicalrep_sync_worker_count(Oid subid)
Definition: launcher.c:653
TimestampTz launch_time
int max_replication_slots
Definition: slot.c:99
TimestampTz last_recv_time
#define NULL
Definition: c.h:229
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:59
int max_logical_replication_workers
Definition: launcher.c:60
#define BGW_MAXLEN
Definition: bgworker.h:85
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:92
bool RegisterDynamicBackgroundWorker(BackgroundWorker *worker, BackgroundWorkerHandle **handle)
Definition: bgworker.c:933
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
static void WaitForReplicationWorkerAttach(LogicalRepWorker *worker, BackgroundWorkerHandle *handle)
Definition: launcher.c:163
#define Int32GetDatum(X)
Definition: postgres.h:485
int errmsg(const char *fmt,...)
Definition: elog.c:797
pid_t bgw_notify_pid
Definition: bgworker.h:98
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
int i
#define elog
Definition: elog.h:219
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:94
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1534
TimestampTz reply_time
static void logicalrep_worker_onexit ( int  code,
Datum  arg 
)
static

Definition at line 615 of file launcher.c.

References logicalrep_worker_detach().

Referenced by logicalrep_worker_attach().

616 {
618 }
static void logicalrep_worker_detach(void)
Definition: launcher.c:572
void logicalrep_worker_sighup ( SIGNAL_ARGS  )

Definition at line 636 of file launcher.c.

References got_SIGHUP, MyLatch, and SetLatch().

Referenced by ApplyLauncherMain(), and ApplyWorkerMain().

637 {
638  int save_errno = errno;
639 
640  got_SIGHUP = true;
641 
642  /* Waken anything waiting on the process latch */
643  SetLatch(MyLatch);
644 
645  errno = save_errno;
646 }
volatile sig_atomic_t got_SIGHUP
Definition: launcher.c:83
void SetLatch(volatile Latch *latch)
Definition: latch.c:415
struct Latch * MyLatch
Definition: globals.c:51
void logicalrep_worker_sigterm ( SIGNAL_ARGS  )

Definition at line 622 of file launcher.c.

References got_SIGTERM, MyLatch, and SetLatch().

Referenced by ApplyLauncherMain(), and ApplyWorkerMain().

623 {
624  int save_errno = errno;
625 
626  got_SIGTERM = true;
627 
628  /* Waken anything waiting on the process latch */
629  SetLatch(MyLatch);
630 
631  errno = save_errno;
632 }
void SetLatch(volatile Latch *latch)
Definition: latch.c:415
volatile sig_atomic_t got_SIGTERM
Definition: launcher.c:84
struct Latch * MyLatch
Definition: globals.c:51
void logicalrep_worker_stop ( Oid  subid,
Oid  relid 
)

Definition at line 411 of file launcher.c.

References CHECK_FOR_INTERRUPTS, LogicalRepWorker::generation, LogicalRepWorker::in_use, logicalrep_worker_find(), LW_SHARED, LWLockAcquire(), LWLockRelease(), MyProc, PGPROC::pid, LogicalRepWorker::proc, proc_exit(), PGPROC::procLatch, ResetLatch(), WAIT_EVENT_BGWORKER_SHUTDOWN, WAIT_EVENT_BGWORKER_STARTUP, WaitLatch(), WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_TIMEOUT.

Referenced by DropSubscription().

412 {
413  LogicalRepWorker *worker;
414  uint16 generation;
415 
416  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
417 
418  worker = logicalrep_worker_find(subid, relid, false);
419 
420  /* No worker, nothing to do. */
421  if (!worker)
422  {
423  LWLockRelease(LogicalRepWorkerLock);
424  return;
425  }
426 
427  /*
428  * Remember which generation was our worker so we can check if what we see
429  * is still the same one.
430  */
431  generation = worker->generation;
432 
433  /*
434  * If we found worker but it does not have proc set it is starting up,
435  * wait for it to finish and then kill it.
436  */
437  while (worker->in_use && !worker->proc)
438  {
439  int rc;
440 
441  LWLockRelease(LogicalRepWorkerLock);
442 
444 
445  /* Wait for signal. */
446  rc = WaitLatch(&MyProc->procLatch,
449 
450  /* emergency bailout if postmaster has died */
451  if (rc & WL_POSTMASTER_DEATH)
452  proc_exit(1);
453 
455 
456  /* Check worker status. */
457  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
458 
459  /*
460  * Check whether the worker slot is no longer used, which would mean
461  * that the worker has exited, or whether the worker generation is
462  * different, meaning that a different worker has taken the slot.
463  */
464  if (!worker->in_use || worker->generation != generation)
465  {
466  LWLockRelease(LogicalRepWorkerLock);
467  return;
468  }
469 
470  /* Worker has assigned proc, so it has started. */
471  if (worker->proc)
472  break;
473  }
474 
475  /* Now terminate the worker ... */
476  kill(worker->proc->pid, SIGTERM);
477  LWLockRelease(LogicalRepWorkerLock);
478 
479  /* ... and wait for it to die. */
480  for (;;)
481  {
482  int rc;
483 
484  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
485  if (!worker->proc || worker->generation != generation)
486  {
487  LWLockRelease(LogicalRepWorkerLock);
488  break;
489  }
490  LWLockRelease(LogicalRepWorkerLock);
491 
493 
494  /* Wait for more work. */
495  rc = WaitLatch(&MyProc->procLatch,
496  WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
498 
499  /* emergency bailout if postmaster has died */
500  if (rc & WL_POSTMASTER_DEATH)
501  proc_exit(1);
502 
504  }
505 }
#define WL_TIMEOUT
Definition: latch.h:127
PGPROC * MyProc
Definition: proc.c:67
void proc_exit(int code)
Definition: ipc.c:99
void ResetLatch(volatile Latch *latch)
Definition: latch.c:498
Latch procLatch
Definition: proc.h:103
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:336
unsigned short uint16
Definition: c.h:267
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:225
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
int pid
Definition: proc.h:108
#define WL_LATCH_SET
Definition: latch.h:124
void logicalrep_worker_wakeup ( Oid  subid,
Oid  relid 
)

Definition at line 511 of file launcher.c.

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

Referenced by pg_attribute_noreturn().

512 {
513  LogicalRepWorker *worker;
514 
515  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
516  worker = logicalrep_worker_find(subid, relid, true);
517  LWLockRelease(LogicalRepWorkerLock);
518 
519  if (worker)
521 }
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition: launcher.c:527
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:225
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
void logicalrep_worker_wakeup_ptr ( LogicalRepWorker worker)

Definition at line 527 of file launcher.c.

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

Referenced by logicalrep_worker_wakeup(), and process_syncing_tables_for_apply().

528 {
529  SetLatch(&worker->proc->procLatch);
530 }
Latch procLatch
Definition: proc.h:103
void SetLatch(volatile Latch *latch)
Definition: latch.c:415
Datum pg_stat_get_subscription ( PG_FUNCTION_ARGS  )

Definition at line 908 of file launcher.c.

References ReturnSetInfo::allowedModes, ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), i, Int32GetDatum, InvalidOid, IsA, IsBackendPid(), LogicalRepWorker::last_lsn, LogicalRepWorker::last_recv_time, LogicalRepWorker::last_send_time, LSNGetDatum, LW_SHARED, LWLockAcquire(), LWLockRelease(), max_logical_replication_workers, MemoryContextSwitchTo(), MemSet, NULL, 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::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, LogicalRepWorker::subid, TimestampTzGetDatum, tuplestore_begin_heap(), tuplestore_donestoring, tuplestore_putvalues(), TYPEFUNC_COMPOSITE, values, work_mem, LogicalRepCtxStruct::workers, and XLogRecPtrIsInvalid.

909 {
910 #define PG_STAT_GET_SUBSCRIPTION_COLS 8
911  Oid subid = PG_ARGISNULL(0) ? InvalidOid : PG_GETARG_OID(0);
912  int i;
913  ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
914  TupleDesc tupdesc;
915  Tuplestorestate *tupstore;
916  MemoryContext per_query_ctx;
917  MemoryContext oldcontext;
918 
919  /* check to see if caller supports us returning a tuplestore */
920  if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
921  ereport(ERROR,
922  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
923  errmsg("set-valued function called in context that cannot accept a set")));
924  if (!(rsinfo->allowedModes & SFRM_Materialize))
925  ereport(ERROR,
926  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
927  errmsg("materialize mode required, but it is not " \
928  "allowed in this context")));
929 
930  /* Build a tuple descriptor for our result type */
931  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
932  elog(ERROR, "return type must be a row type");
933 
934  per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
935  oldcontext = MemoryContextSwitchTo(per_query_ctx);
936 
937  tupstore = tuplestore_begin_heap(true, false, work_mem);
938  rsinfo->returnMode = SFRM_Materialize;
939  rsinfo->setResult = tupstore;
940  rsinfo->setDesc = tupdesc;
941 
942  MemoryContextSwitchTo(oldcontext);
943 
944  /* Make sure we get consistent view of the workers. */
945  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
946 
947  for (i = 0; i <= max_logical_replication_workers; i++)
948  {
949  /* for each row */
951  bool nulls[PG_STAT_GET_SUBSCRIPTION_COLS];
952  int worker_pid;
953  LogicalRepWorker worker;
954 
955  memcpy(&worker, &LogicalRepCtx->workers[i],
956  sizeof(LogicalRepWorker));
957  if (!worker.proc || !IsBackendPid(worker.proc->pid))
958  continue;
959 
960  if (OidIsValid(subid) && worker.subid != subid)
961  continue;
962 
963  worker_pid = worker.proc->pid;
964 
965  MemSet(values, 0, sizeof(values));
966  MemSet(nulls, 0, sizeof(nulls));
967 
968  values[0] = ObjectIdGetDatum(worker.subid);
969  if (OidIsValid(worker.relid))
970  values[1] = ObjectIdGetDatum(worker.relid);
971  else
972  nulls[1] = true;
973  values[2] = Int32GetDatum(worker_pid);
974  if (XLogRecPtrIsInvalid(worker.last_lsn))
975  nulls[3] = true;
976  else
977  values[3] = LSNGetDatum(worker.last_lsn);
978  if (worker.last_send_time == 0)
979  nulls[4] = true;
980  else
981  values[4] = TimestampTzGetDatum(worker.last_send_time);
982  if (worker.last_recv_time == 0)
983  nulls[5] = true;
984  else
985  values[5] = TimestampTzGetDatum(worker.last_recv_time);
986  if (XLogRecPtrIsInvalid(worker.reply_lsn))
987  nulls[6] = true;
988  else
989  values[6] = LSNGetDatum(worker.reply_lsn);
990  if (worker.reply_time == 0)
991  nulls[7] = true;
992  else
993  values[7] = TimestampTzGetDatum(worker.reply_time);
994 
995  tuplestore_putvalues(tupstore, tupdesc, values, nulls);
996 
997  /*
998  * If only a single subscription was requested, and we found it,
999  * break.
1000  */
1001  if (OidIsValid(subid))
1002  break;
1003  }
1004 
1005  LWLockRelease(LogicalRepWorkerLock);
1006 
1007  /* clean up and return the tuplestore */
1008  tuplestore_donestoring(tupstore);
1009 
1010  return (Datum) 0;
1011 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
#define tuplestore_donestoring(state)
Definition: tuplestore.h:60
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:71
TimestampTz last_send_time
XLogRecPtr last_lsn
int errcode(int sqlerrcode)
Definition: elog.c:575
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
#define MemSet(start, val, len)
Definition: c.h:857
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:538
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define TimestampTzGetDatum(X)
Definition: timestamp.h:32
XLogRecPtr reply_lsn
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define ereport(elevel, rest)
Definition: elog.h:122
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
uintptr_t Datum
Definition: postgres.h:372
#define PG_STAT_GET_SUBSCRIPTION_COLS
int work_mem
Definition: globals.c:112
#define InvalidOid
Definition: postgres_ext.h:36
int allowedModes
Definition: execnodes.h:268
SetFunctionReturnMode returnMode
Definition: execnodes.h:270
TimestampTz last_recv_time
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define NULL
Definition: c.h:229
int max_logical_replication_workers
Definition: launcher.c:60
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:202
Tuplestorestate * setResult
Definition: execnodes.h:273
static Datum values[MAXATTR]
Definition: bootstrap.c:163
ExprContext * econtext
Definition: execnodes.h:266
#define Int32GetDatum(X)
Definition: postgres.h:485
TupleDesc setDesc
Definition: execnodes.h:274
int errmsg(const char *fmt,...)
Definition: elog.c:797
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
int i
bool IsBackendPid(int pid)
Definition: procarray.c:2437
#define elog
Definition: elog.h:219
int pid
Definition: proc.h:108
TimestampTz reply_time
static void WaitForReplicationWorkerAttach ( LogicalRepWorker worker,
BackgroundWorkerHandle handle 
)
static

Definition at line 163 of file launcher.c.

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

Referenced by logicalrep_worker_launch().

165 {
167  int rc;
168  uint16 generation;
169 
170  /* Remember generation for future identification. */
171  generation = worker->generation;
172 
173  for (;;)
174  {
175  pid_t pid;
176 
178 
179  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
180 
181  /* Worker either died or has started; no need to do anything. */
182  if (!worker->in_use || worker->proc)
183  {
184  LWLockRelease(LogicalRepWorkerLock);
185  return;
186  }
187 
188  LWLockRelease(LogicalRepWorkerLock);
189 
190  /* Check if worker has died before attaching, and clean up after it. */
191  status = GetBackgroundWorkerPid(handle, &pid);
192 
193  if (status == BGWH_STOPPED)
194  {
195  LWLockAcquire(LogicalRepWorkerLock, LW_EXCLUSIVE);
196  /* Ensure that this was indeed the worker we waited for. */
197  if (generation == worker->generation)
199  LWLockRelease(LogicalRepWorkerLock);
200  return;
201  }
202 
203  /*
204  * We need timeout because we generally don't get notified via latch
205  * about the worker attach.
206  */
207  rc = WaitLatch(MyLatch,
210 
211  if (rc & WL_POSTMASTER_DEATH)
212  proc_exit(1);
213 
215  }
216 
217  return;
218 }
#define WL_TIMEOUT
Definition: latch.h:127
void proc_exit(int code)
Definition: ipc.c:99
void ResetLatch(volatile Latch *latch)
Definition: latch.c:498
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1715
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:336
unsigned short uint16
Definition: c.h:267
static void logicalrep_worker_cleanup(LogicalRepWorker *worker)
Definition: launcher.c:586
BgwHandleStatus
Definition: bgworker.h:101
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
struct Latch * MyLatch
Definition: globals.c:51
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
#define WL_LATCH_SET
Definition: latch.h:124
BgwHandleStatus GetBackgroundWorkerPid(BackgroundWorkerHandle *handle, pid_t *pidp)
Definition: bgworker.c:1041

Variable Documentation

volatile sig_atomic_t got_SIGHUP = false

Definition at line 83 of file launcher.c.

Referenced by ApplyLauncherMain(), and logicalrep_worker_sighup().

volatile sig_atomic_t got_SIGTERM = false

Definition at line 84 of file launcher.c.

Referenced by ApplyLauncherMain(), and logicalrep_worker_sigterm().

LogicalRepCtxStruct* LogicalRepCtx

Definition at line 74 of file launcher.c.

int max_sync_workers_per_subscription = 2

Definition at line 61 of file launcher.c.

Referenced by logicalrep_worker_launch(), and process_syncing_tables_for_apply().

bool on_commit_launcher_wakeup = false
static

Definition at line 86 of file launcher.c.

Referenced by ApplyLauncherWakeupAtCommit(), and AtEOXact_ApplyLauncher().