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
 
int max_sync_workers_per_subscription
 

Function Documentation

void ApplyLauncherMain ( Datum  main_arg)

Definition at line 632 of file launcher.c.

References ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE, ALLOCSET_DEFAULT_MINSIZE, AllocSetContextCreate(), BackgroundWorkerInitializeConnection(), BackgroundWorkerUnblockSignals(), BackgroundWorker::bgw_name, Subscription::dbid, DEBUG1, DEFAULT_NAPTIME_PER_CYCLE, Subscription::enabled, ereport, errmsg(), get_subscription_list(), GetCurrentTimestamp(), got_SIGTERM, InvalidOid, LogicalRepCtxStruct::launcher_pid, lfirst, logicalrep_worker_find(), logicalrep_worker_launch(), logicalrep_worker_sigterm(), 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().

633 {
634  ereport(DEBUG1,
635  (errmsg("logical replication launcher started")));
636 
637  /* Establish signal handlers. */
640 
641  /* Make it easy to identify our processes. */
642  SetConfigOption("application_name", MyBgworkerEntry->bgw_name,
644 
646 
647  /*
648  * Establish connection to nailed catalogs (we only ever access
649  * pg_subscription).
650  */
652 
653  /* Enter main loop */
654  while (!got_SIGTERM)
655  {
656  int rc;
657  List *sublist;
658  ListCell *lc;
659  MemoryContext subctx;
660  MemoryContext oldctx;
662  TimestampTz last_start_time = 0;
663  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
664 
665  now = GetCurrentTimestamp();
666 
667  /* Limit the start retry to once a wal_retrieve_retry_interval */
668  if (TimestampDifferenceExceeds(last_start_time, now,
670  {
671  /* Use temporary context for the database list and worker info. */
673  "Logical Replication Launcher sublist",
677  oldctx = MemoryContextSwitchTo(subctx);
678 
679  /* search for subscriptions to start or stop. */
680  sublist = get_subscription_list();
681 
682  /* Start the missing workers for enabled subscriptions. */
683  foreach(lc, sublist)
684  {
685  Subscription *sub = (Subscription *) lfirst(lc);
686  LogicalRepWorker *w;
687 
688  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
689  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
690  LWLockRelease(LogicalRepWorkerLock);
691 
692  if (sub->enabled && w == NULL)
693  {
694  logicalrep_worker_launch(sub->dbid, sub->oid, sub->name,
695  sub->owner, InvalidOid);
696  last_start_time = now;
697  wait_time = wal_retrieve_retry_interval;
698  /* Limit to one worker per mainloop cycle. */
699  break;
700  }
701  }
702 
703  /* Switch back to original memory context. */
704  MemoryContextSwitchTo(oldctx);
705  /* Clean the temporary memory. */
706  MemoryContextDelete(subctx);
707  }
708  else
709  {
710  /*
711  * The wait in previous cycle was interrupted in less than
712  * wal_retrieve_retry_interval since last worker was started,
713  * this usually means crash of the worker, so we should retry
714  * in wal_retrieve_retry_interval again.
715  */
716  wait_time = wal_retrieve_retry_interval;
717  }
718 
719  /* Wait for more work. */
720  rc = WaitLatch(&MyProc->procLatch,
722  wait_time,
724 
725  /* emergency bailout if postmaster has died */
726  if (rc & WL_POSTMASTER_DEATH)
727  proc_exit(1);
728 
730  }
731 
733 
734  /* ... and if it returns, we're done */
735  ereport(DEBUG1,
736  (errmsg("logical replication launcher shutting down")));
737 
738  proc_exit(0);
739 }
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
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1569
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:461
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1648
#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:300
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition: guc.c:6651
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:43
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:57
void logicalrep_worker_sigterm(SIGNAL_ARGS)
Definition: launcher.c:495
#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:91
#define NULL
Definition: c.h:229
void logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid)
Definition: launcher.c:233
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:90
#define lfirst(lc)
Definition: pg_list.h:106
bool got_SIGTERM
Definition: launcher.c:78
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:207
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int errmsg(const char *fmt,...)
Definition: elog.c:797
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:73
#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:5452
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1533
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5504
void ApplyLauncherRegister ( void  )

Definition at line 546 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().

547 {
548  BackgroundWorker bgw;
549 
551  return;
552 
558  "logical replication launcher");
559  bgw.bgw_restart_time = 5;
560  bgw.bgw_notify_pid = 0;
561  bgw.bgw_main_arg = (Datum) 0;
562 
564 }
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:805
void ApplyLauncherMain(Datum main_arg)
Definition: launcher.c:632
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: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:59
#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 571 of file launcher.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

572 {
573  bool found;
574 
576  ShmemInitStruct("Logical Replication Launcher Data",
578  &found);
579 
580  if (!found)
581  {
582  int slot;
583 
585 
586  /* Initialize memory and spin locks for each worker slot. */
587  for (slot = 0; slot < max_logical_replication_workers; slot++)
588  {
589  LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
590 
591  memset(worker, 0, sizeof(LogicalRepWorker));
592  SpinLockInit(&worker->relmutex);
593  }
594  }
595 }
#define SpinLockInit(lock)
Definition: spin.h:60
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:70
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:531
int max_logical_replication_workers
Definition: launcher.c:59
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:73
Size ApplyLauncherShmemSize ( void  )

Definition at line 531 of file launcher.c.

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

Referenced by ApplyLauncherShmemInit(), and CreateSharedMemoryAndSemaphores().

532 {
533  Size size;
534 
535  /*
536  * Need the fixed struct and the array of LogicalRepWorker.
537  */
538  size = sizeof(LogicalRepCtxStruct);
539  size = MAXALIGN(size);
541  sizeof(LogicalRepWorker)));
542  return size;
543 }
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:59
size_t Size
Definition: c.h:356
#define MAXALIGN(LEN)
Definition: c.h:588
struct LogicalRepCtxStruct LogicalRepCtxStruct
void ApplyLauncherWakeup ( void  )

Definition at line 622 of file launcher.c.

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

Referenced by AtCommit_ApplyLauncher().

623 {
626 }
#define SIGUSR1
Definition: win32.h:211
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:73
bool IsBackendPid(int pid)
Definition: procarray.c:2425
void ApplyLauncherWakeupAtCommit ( void  )

Definition at line 615 of file launcher.c.

References on_commit_launcher_wakeup.

Referenced by CreateSubscription().

616 {
619 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:79
void AtCommit_ApplyLauncher ( void  )

Definition at line 601 of file launcher.c.

References ApplyLauncherWakeup(), and on_commit_launcher_wakeup.

Referenced by CommitTransaction().

602 {
605 }
void ApplyLauncherWakeup(void)
Definition: launcher.c:622
static bool on_commit_launcher_wakeup
Definition: launcher.c:79

Variable Documentation

int max_sync_workers_per_subscription

Definition at line 60 of file launcher.c.

Referenced by process_syncing_tables_for_apply().