PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
logicallauncher.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void ApplyLauncherRegister (void)
 
void ApplyLauncherMain (Datum main_arg)
 
Size ApplyLauncherShmemSize (void)
 
void ApplyLauncherShmemInit (void)
 
void ApplyLauncherWakeup (void)
 
void ApplyLauncherWakeupAtCommit (void)
 
void AtCommit_ApplyLauncher (void)
 

Variables

int max_logical_replication_workers
 

Function Documentation

void ApplyLauncherMain ( Datum  main_arg)

Definition at line 558 of file launcher.c.

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

Referenced by ApplyLauncherRegister().

559 {
560  ereport(LOG,
561  (errmsg("logical replication launcher started")));
562 
563  /* Establish signal handlers. */
566 
567  /* Make it easy to identify our processes. */
568  SetConfigOption("application_name", MyBgworkerEntry->bgw_name,
570 
572 
573  /*
574  * Establish connection to nailed catalogs (we only ever access
575  * pg_subscription).
576  */
578 
579  /* Enter main loop */
580  while (!got_SIGTERM)
581  {
582  int rc;
583  List *sublist;
584  ListCell *lc;
585  MemoryContext subctx;
586  MemoryContext oldctx;
588  TimestampTz last_start_time = 0;
589  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
590 
591  now = GetCurrentTimestamp();
592 
593  /* Limit the start retry to once a wal_retrieve_retry_interval */
594  if (TimestampDifferenceExceeds(last_start_time, now,
596  {
597  /* Use temporary context for the database list and worker info. */
599  "Logical Replication Launcher sublist",
603  oldctx = MemoryContextSwitchTo(subctx);
604 
605  /* Block any concurrent DROP SUBSCRIPTION. */
606  LWLockAcquire(LogicalRepLauncherLock, LW_EXCLUSIVE);
607 
608  /* search for subscriptions to start or stop. */
609  sublist = get_subscription_list();
610 
611  /* Start the missing workers for enabled subscriptions. */
612  foreach(lc, sublist)
613  {
614  Subscription *sub = (Subscription *) lfirst(lc);
615  LogicalRepWorker *w;
616 
617  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
618  w = logicalrep_worker_find(sub->oid);
619  LWLockRelease(LogicalRepWorkerLock);
620 
621  if (sub->enabled && w == NULL)
622  {
623  logicalrep_worker_launch(sub->dbid, sub->oid, sub->name, sub->owner);
624  last_start_time = now;
625  wait_time = wal_retrieve_retry_interval;
626  /* Limit to one worker per mainloop cycle. */
627  break;
628  }
629  }
630 
631  LWLockRelease(LogicalRepLauncherLock);
632 
633  /* Switch back to original memory context. */
634  MemoryContextSwitchTo(oldctx);
635  /* Clean the temporary memory. */
636  MemoryContextDelete(subctx);
637  }
638  else
639  {
640  /*
641  * The wait in previous cycle was interrupted in less than
642  * wal_retrieve_retry_interval since last worker was started,
643  * this usually means crash of the worker, so we should retry
644  * in wal_retrieve_retry_interval again.
645  */
646  wait_time = wal_retrieve_retry_interval;
647  }
648 
649  /* Wait for more work. */
650  rc = WaitLatch(&MyProc->procLatch,
652  wait_time,
654 
655  /* emergency bailout if postmaster has died */
656  if (rc & WL_POSTMASTER_DEATH)
657  proc_exit(1);
658 
660  }
661 
663 
664  /* ... and if it returns, we're done */
665  ereport(LOG,
666  (errmsg("logical replication launcher shutting down")));
667 
668  proc_exit(0);
669 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
int MyProcPid
Definition: globals.c:38
#define WL_TIMEOUT
Definition: latch.h:127
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1688
PGPROC * MyProc
Definition: proc.c:67
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:462
#define LOG
Definition: elog.h:26
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1821
#define ALLOCSET_DEFAULT_MINSIZE
Definition: memutils.h:142
Latch procLatch
Definition: proc.h:93
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
double TimestampTz
Definition: timestamp.h:51
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:301
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:6633
LogicalRepWorker * logicalrep_worker_find(Oid subid)
Definition: launcher.c:204
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:43
int wal_retrieve_retry_interval
Definition: xlog.c:105
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
#define DEFAULT_NAPTIME_PER_CYCLE
Definition: launcher.c:56
void logicalrep_worker_sigterm(SIGNAL_ARGS)
Definition: launcher.c:457
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
static List * get_subscription_list(void)
Definition: launcher.c:88
#define NULL
Definition: c.h:226
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define lfirst(lc)
Definition: pg_list.h:106
bool got_SIGTERM
Definition: launcher.c:75
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
int errmsg(const char *fmt,...)
Definition: elog.c:797
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:70
#define ALLOCSET_DEFAULT_INITSIZE
Definition: memutils.h:143
#define ALLOCSET_DEFAULT_MAXSIZE
Definition: memutils.h:144
Definition: pg_list.h:45
#define WL_LATCH_SET
Definition: latch.h:124
void BackgroundWorkerInitializeConnection(char *dbname, char *username)
Definition: postmaster.c:5445
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1652
void logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid)
Definition: launcher.c:228
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5497
void ApplyLauncherRegister ( void  )

Definition at line 485 of file launcher.c.

References ApplyLauncherMain(), BackgroundWorker::bgw_flags, BackgroundWorker::bgw_main, 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().

486 {
487  BackgroundWorker bgw;
488 
490  return;
491 
497  "logical replication launcher");
498  bgw.bgw_restart_time = 5;
499  bgw.bgw_notify_pid = 0;
500  bgw.bgw_main_arg = (Datum) 0;
501 
503 }
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:770
void ApplyLauncherMain(Datum main_arg)
Definition: launcher.c:558
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
Datum bgw_main_arg
Definition: bgworker.h:97
bgworker_main_type bgw_main
Definition: bgworker.h:94
uintptr_t Datum
Definition: postgres.h:374
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:58
#define BGW_MAXLEN
Definition: bgworker.h:85
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:92
pid_t bgw_notify_pid
Definition: bgworker.h:99
void ApplyLauncherShmemInit ( void  )

Definition at line 510 of file launcher.c.

References ApplyLauncherShmemSize(), and ShmemInitStruct().

Referenced by CreateSharedMemoryAndSemaphores().

511 {
512  bool found;
513 
515  ShmemInitStruct("Logical Replication Launcher Data",
517  &found);
518 
519  if (!found)
521 }
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:470
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:70
Size ApplyLauncherShmemSize ( void  )

Definition at line 470 of file launcher.c.

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

Referenced by ApplyLauncherShmemInit(), and CreateSharedMemoryAndSemaphores().

471 {
472  Size size;
473 
474  /*
475  * Need the fixed struct and the array of LogicalRepWorker.
476  */
477  size = sizeof(LogicalRepCtxStruct);
478  size = MAXALIGN(size);
480  sizeof(LogicalRepWorker)));
481  return size;
482 }
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:58
size_t Size
Definition: c.h:352
#define MAXALIGN(LEN)
Definition: c.h:583
struct LogicalRepCtxStruct LogicalRepCtxStruct
void ApplyLauncherWakeup ( void  )

Definition at line 548 of file launcher.c.

References IsBackendPid(), LogicalRepCtxStruct::launcher_pid, and SIGUSR1.

Referenced by AtCommit_ApplyLauncher().

549 {
552 }
#define SIGUSR1
Definition: win32.h:211
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:70
bool IsBackendPid(int pid)
Definition: procarray.c:2424
void ApplyLauncherWakeupAtCommit ( void  )

Definition at line 541 of file launcher.c.

References on_commit_launcher_wakeup.

Referenced by CreateSubscription().

542 {
545 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:76
void AtCommit_ApplyLauncher ( void  )

Definition at line 527 of file launcher.c.

References ApplyLauncherWakeup(), and on_commit_launcher_wakeup.

Referenced by CommitTransaction().

528 {
531 }
void ApplyLauncherWakeup(void)
Definition: launcher.c:548
static bool on_commit_launcher_wakeup
Definition: launcher.c:76

Variable Documentation