PostgreSQL Source Code  git master
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)
 
bool IsLogicalLauncher (void)
 

Variables

int max_logical_replication_workers
 
int max_sync_workers_per_subscription
 

Function Documentation

◆ ApplyLauncherMain()

void ApplyLauncherMain ( Datum  main_arg)

Definition at line 802 of file launcher.c.

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

803 {
804  TimestampTz last_start_time = 0;
805 
806  ereport(DEBUG1,
807  (errmsg_internal("logical replication launcher started")));
808 
810 
813 
814  /* Establish signal handlers. */
816  pqsignal(SIGTERM, die);
818 
819  /*
820  * Establish connection to nailed catalogs (we only ever access
821  * pg_subscription).
822  */
824 
825  /* Enter main loop */
826  for (;;)
827  {
828  int rc;
829  List *sublist;
830  ListCell *lc;
831  MemoryContext subctx;
832  MemoryContext oldctx;
834  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
835 
837 
838  now = GetCurrentTimestamp();
839 
840  /* Limit the start retry to once a wal_retrieve_retry_interval */
841  if (TimestampDifferenceExceeds(last_start_time, now,
843  {
844  /* Use temporary context for the database list and worker info. */
846  "Logical Replication Launcher sublist",
848  oldctx = MemoryContextSwitchTo(subctx);
849 
850  /* search for subscriptions to start or stop. */
851  sublist = get_subscription_list();
852 
853  /* Start the missing workers for enabled subscriptions. */
854  foreach(lc, sublist)
855  {
856  Subscription *sub = (Subscription *) lfirst(lc);
857  LogicalRepWorker *w;
858 
859  if (!sub->enabled)
860  continue;
861 
862  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
863  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
864  LWLockRelease(LogicalRepWorkerLock);
865 
866  if (w == NULL)
867  {
868  last_start_time = now;
869  wait_time = wal_retrieve_retry_interval;
870 
871  logicalrep_worker_launch(sub->dbid, sub->oid, sub->name,
872  sub->owner, InvalidOid);
873  }
874  }
875 
876  /* Switch back to original memory context. */
877  MemoryContextSwitchTo(oldctx);
878  /* Clean the temporary memory. */
879  MemoryContextDelete(subctx);
880  }
881  else
882  {
883  /*
884  * The wait in previous cycle was interrupted in less than
885  * wal_retrieve_retry_interval since last worker was started, this
886  * usually means crash of the worker, so we should retry in
887  * wal_retrieve_retry_interval again.
888  */
889  wait_time = wal_retrieve_retry_interval;
890  }
891 
892  /* Wait for more work. */
893  rc = WaitLatch(MyLatch,
895  wait_time,
897 
898  if (rc & WL_LATCH_SET)
899  {
902  }
903 
905  {
906  ConfigReloadPending = false;
908  }
909  }
910 
911  /* Not reachable */
912 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define AllocSetContextCreate
Definition: memutils.h:173
#define DEBUG1
Definition: elog.h:25
int MyProcPid
Definition: globals.c:43
#define WL_TIMEOUT
Definition: latch.h:128
void ProcessConfigFile(GucContext context)
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1580
int64 TimestampTz
Definition: timestamp.h:39
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:56
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1711
void ResetLatch(Latch *latch)
Definition: latch.c:660
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:452
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1803
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
#define SIGHUP
Definition: win32_port.h:159
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:333
static void logicalrep_launcher_onexit(int code, Datum arg)
Definition: launcher.c:633
MemoryContext TopMemoryContext
Definition: mcxt.c:48
Definition: guc.h:72
int wal_retrieve_retry_interval
Definition: xlog.c:111
void BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
Definition: postmaster.c:5729
#define DEFAULT_NAPTIME_PER_CYCLE
Definition: launcher.c:52
uintptr_t Datum
Definition: postgres.h:411
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
static List * get_subscription_list(void)
Definition: launcher.c:88
int errmsg_internal(const char *fmt,...)
Definition: elog.c:996
void logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid)
Definition: launcher.c:266
#define Assert(condition)
Definition: c.h:804
#define lfirst(lc)
Definition: pg_list.h:169
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:215
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1199
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:68
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:26
struct Latch * MyLatch
Definition: globals.c:57
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
Definition: pg_list.h:50
#define WL_LATCH_SET
Definition: latch.h:125
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1544
#define die(msg)
Definition: pg_test_fsync.c:97
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5781

◆ ApplyLauncherRegister()

void ApplyLauncherRegister ( void  )

Definition at line 707 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, BackgroundWorker::bgw_type, BGWORKER_BACKEND_DATABASE_CONNECTION, BGWORKER_SHMEM_ACCESS, BgWorkerStart_RecoveryFinished, max_logical_replication_workers, RegisterBackgroundWorker(), and snprintf.

Referenced by PostmasterMain().

708 {
709  BackgroundWorker bgw;
710 
712  return;
713 
714  memset(&bgw, 0, sizeof(bgw));
718  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
719  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
721  "logical replication launcher");
723  "logical replication launcher");
724  bgw.bgw_restart_time = 5;
725  bgw.bgw_notify_pid = 0;
726  bgw.bgw_main_arg = (Datum) 0;
727 
729 }
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:872
int bgw_restart_time
Definition: bgworker.h:95
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:53
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:97
Datum bgw_main_arg
Definition: bgworker.h:98
uintptr_t Datum
Definition: postgres.h:411
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:91
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:60
int max_logical_replication_workers
Definition: launcher.c:54
#define BGW_MAXLEN
Definition: bgworker.h:86
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:94
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:92
pid_t bgw_notify_pid
Definition: bgworker.h:100
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:96
#define snprintf
Definition: port.h:216

◆ ApplyLauncherShmemInit()

void ApplyLauncherShmemInit ( void  )

Definition at line 736 of file launcher.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

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

◆ ApplyLauncherShmemSize()

Size ApplyLauncherShmemSize ( void  )

Definition at line 688 of file launcher.c.

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

Referenced by ApplyLauncherShmemInit(), and CalculateShmemSize().

689 {
690  Size size;
691 
692  /*
693  * Need the fixed struct and the array of LogicalRepWorker.
694  */
695  size = sizeof(LogicalRepCtxStruct);
696  size = MAXALIGN(size);
698  sizeof(LogicalRepWorker)));
699  return size;
700 }
Size mul_size(Size s1, Size s2)
Definition: shmem.c:519
Size add_size(Size s1, Size s2)
Definition: shmem.c:502
int max_logical_replication_workers
Definition: launcher.c:54
size_t Size
Definition: c.h:540
#define MAXALIGN(LEN)
Definition: c.h:757
struct LogicalRepCtxStruct LogicalRepCtxStruct

◆ ApplyLauncherWakeupAtCommit()

void ApplyLauncherWakeupAtCommit ( void  )

Definition at line 785 of file launcher.c.

References on_commit_launcher_wakeup.

Referenced by AlterSubscription(), and CreateSubscription().

786 {
789 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:76

◆ AtEOXact_ApplyLauncher()

void AtEOXact_ApplyLauncher ( bool  isCommit)

Definition at line 766 of file launcher.c.

References ApplyLauncherWakeup(), and on_commit_launcher_wakeup.

Referenced by AbortTransaction(), and CommitTransaction().

767 {
768  if (isCommit)
769  {
772  }
773 
775 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:76
static void ApplyLauncherWakeup(void)
Definition: launcher.c:792

◆ IsLogicalLauncher()

bool IsLogicalLauncher ( void  )

Definition at line 918 of file launcher.c.

References LogicalRepCtxStruct::launcher_pid, and MyProcPid.

Referenced by ProcessInterrupts().

919 {
921 }
int MyProcPid
Definition: globals.c:43
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:68

Variable Documentation

◆ max_logical_replication_workers

◆ max_sync_workers_per_subscription

int max_sync_workers_per_subscription

Definition at line 55 of file launcher.c.

Referenced by logicalrep_worker_launch(), and process_syncing_tables_for_apply().