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 ApplyLauncherWakeupAtCommit (void)
 
void AtEOXact_ApplyLauncher (bool isCommit)
 

Variables

int max_logical_replication_workers
 
int max_sync_workers_per_subscription
 

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

Variable Documentation

int max_sync_workers_per_subscription

Definition at line 61 of file launcher.c.

Referenced by logicalrep_worker_launch(), and process_syncing_tables_for_apply().