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

PGDLLIMPORT int max_logical_replication_workers
 
PGDLLIMPORT int max_sync_workers_per_subscription
 

Function Documentation

◆ ApplyLauncherMain()

void ApplyLauncherMain ( Datum  main_arg)

Definition at line 801 of file launcher.c.

802 {
803  TimestampTz last_start_time = 0;
804 
805  ereport(DEBUG1,
806  (errmsg_internal("logical replication launcher started")));
807 
809 
812 
813  /* Establish signal handlers. */
815  pqsignal(SIGTERM, die);
817 
818  /*
819  * Establish connection to nailed catalogs (we only ever access
820  * pg_subscription).
821  */
823 
824  /* Enter main loop */
825  for (;;)
826  {
827  int rc;
828  List *sublist;
829  ListCell *lc;
830  MemoryContext subctx;
831  MemoryContext oldctx;
833  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
834 
836 
838 
839  /* Limit the start retry to once a wal_retrieve_retry_interval */
840  if (TimestampDifferenceExceeds(last_start_time, now,
842  {
843  /* Use temporary context for the database list and worker info. */
845  "Logical Replication Launcher sublist",
847  oldctx = MemoryContextSwitchTo(subctx);
848 
849  /* search for subscriptions to start or stop. */
850  sublist = get_subscription_list();
851 
852  /* Start the missing workers for enabled subscriptions. */
853  foreach(lc, sublist)
854  {
855  Subscription *sub = (Subscription *) lfirst(lc);
856  LogicalRepWorker *w;
857 
858  if (!sub->enabled)
859  continue;
860 
861  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
862  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
863  LWLockRelease(LogicalRepWorkerLock);
864 
865  if (w == NULL)
866  {
867  last_start_time = now;
868  wait_time = wal_retrieve_retry_interval;
869 
870  logicalrep_worker_launch(sub->dbid, sub->oid, sub->name,
871  sub->owner, InvalidOid);
872  }
873  }
874 
875  /* Switch back to original memory context. */
876  MemoryContextSwitchTo(oldctx);
877  /* Clean the temporary memory. */
878  MemoryContextDelete(subctx);
879  }
880  else
881  {
882  /*
883  * The wait in previous cycle was interrupted in less than
884  * wal_retrieve_retry_interval since last worker was started, this
885  * usually means crash of the worker, so we should retry in
886  * wal_retrieve_retry_interval again.
887  */
888  wait_time = wal_retrieve_retry_interval;
889  }
890 
891  /* Wait for more work. */
892  rc = WaitLatch(MyLatch,
894  wait_time,
896 
897  if (rc & WL_LATCH_SET)
898  {
901  }
902 
904  {
905  ConfigReloadPending = false;
907  }
908  }
909 
910  /* Not reachable */
911 }
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1705
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1574
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1538
int64 TimestampTz
Definition: timestamp.h:39
int errmsg_internal(const char *fmt,...)
Definition: elog.c:991
#define DEBUG1
Definition: elog.h:24
#define ereport(elevel,...)
Definition: elog.h:143
int MyProcPid
Definition: globals.c:44
struct Latch * MyLatch
Definition: globals.c:58
@ PGC_SIGHUP
Definition: guc.h:72
void ProcessConfigFile(GucContext context)
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:27
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:61
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:333
void ResetLatch(Latch *latch)
Definition: latch.c:658
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:451
#define WL_TIMEOUT
Definition: latch.h:128
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130
#define WL_LATCH_SET
Definition: latch.h:125
#define DEFAULT_NAPTIME_PER_CYCLE
Definition: launcher.c:53
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:214
static List * get_subscription_list(void)
Definition: launcher.c:87
static void logicalrep_launcher_onexit(int code, Datum arg)
Definition: launcher.c:632
void logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid)
Definition: launcher.c:265
static LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:69
Assert(fmt[strlen(fmt) - 1] !='\n')
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1196
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1800
@ LW_SHARED
Definition: lwlock.h:105
MemoryContext TopMemoryContext
Definition: mcxt.c:48
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
#define AllocSetContextCreate
Definition: memutils.h:173
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:197
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define lfirst(lc)
Definition: pg_list.h:169
#define die(msg)
Definition: pg_test_fsync.c:95
uintptr_t Datum
Definition: postgres.h:411
#define InvalidOid
Definition: postgres_ext.h:36
void BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
Definition: postmaster.c:5661
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5713
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:180
Definition: pg_list.h:51
@ WAIT_EVENT_LOGICAL_LAUNCHER_MAIN
Definition: wait_event.h:44
#define SIGHUP
Definition: win32_port.h:167
int wal_retrieve_retry_interval
Definition: xlog.c:135

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

◆ ApplyLauncherRegister()

void ApplyLauncherRegister ( void  )

Definition at line 706 of file launcher.c.

707 {
708  BackgroundWorker bgw;
709 
711  return;
712 
713  memset(&bgw, 0, sizeof(bgw));
717  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
718  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
720  "logical replication launcher");
722  "logical replication launcher");
723  bgw.bgw_restart_time = 5;
724  bgw.bgw_notify_pid = 0;
725  bgw.bgw_main_arg = (Datum) 0;
726 
728 }
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:872
@ BgWorkerStart_RecoveryFinished
Definition: bgworker.h:81
#define BGWORKER_BACKEND_DATABASE_CONNECTION
Definition: bgworker.h:60
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:53
#define BGW_MAXLEN
Definition: bgworker.h:86
int max_logical_replication_workers
Definition: launcher.c:55
#define snprintf
Definition: port.h:225
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:97
Datum bgw_main_arg
Definition: bgworker.h:98
char bgw_name[BGW_MAXLEN]
Definition: bgworker.h:91
int bgw_restart_time
Definition: bgworker.h:95
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:92
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:94
pid_t bgw_notify_pid
Definition: bgworker.h:100
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:96

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

◆ ApplyLauncherShmemInit()

void ApplyLauncherShmemInit ( void  )

Definition at line 735 of file launcher.c.

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

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

Referenced by CreateSharedMemoryAndSemaphores().

◆ ApplyLauncherShmemSize()

Size ApplyLauncherShmemSize ( void  )

Definition at line 687 of file launcher.c.

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

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

Referenced by ApplyLauncherShmemInit(), and CalculateShmemSize().

◆ ApplyLauncherWakeupAtCommit()

void ApplyLauncherWakeupAtCommit ( void  )

Definition at line 784 of file launcher.c.

785 {
788 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:77

References on_commit_launcher_wakeup.

Referenced by AlterSubscription(), AlterSubscriptionOwner_internal(), and CreateSubscription().

◆ AtEOXact_ApplyLauncher()

void AtEOXact_ApplyLauncher ( bool  isCommit)

Definition at line 765 of file launcher.c.

766 {
767  if (isCommit)
768  {
771  }
772 
774 }
static void ApplyLauncherWakeup(void)
Definition: launcher.c:791

References ApplyLauncherWakeup(), and on_commit_launcher_wakeup.

Referenced by AbortTransaction(), and CommitTransaction().

◆ IsLogicalLauncher()

bool IsLogicalLauncher ( void  )

Definition at line 917 of file launcher.c.

918 {
920 }

References LogicalRepCtxStruct::launcher_pid, LogicalRepCtx, and MyProcPid.

Referenced by ProcessInterrupts().

Variable Documentation

◆ max_logical_replication_workers

◆ max_sync_workers_per_subscription

PGDLLIMPORT int max_sync_workers_per_subscription
extern

Definition at line 56 of file launcher.c.

Referenced by logicalrep_worker_launch(), and process_syncing_tables_for_apply().