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

798 {
799  TimestampTz last_start_time = 0;
800 
801  ereport(DEBUG1,
802  (errmsg_internal("logical replication launcher started")));
803 
805 
808 
809  /* Establish signal handlers. */
811  pqsignal(SIGTERM, die);
813 
814  /*
815  * Establish connection to nailed catalogs (we only ever access
816  * pg_subscription).
817  */
819 
820  /* Enter main loop */
821  for (;;)
822  {
823  int rc;
824  List *sublist;
825  ListCell *lc;
826  MemoryContext subctx;
827  MemoryContext oldctx;
829  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
830 
832 
833  now = GetCurrentTimestamp();
834 
835  /* Limit the start retry to once a wal_retrieve_retry_interval */
836  if (TimestampDifferenceExceeds(last_start_time, now,
838  {
839  /* Use temporary context for the database list and worker info. */
841  "Logical Replication Launcher sublist",
843  oldctx = MemoryContextSwitchTo(subctx);
844 
845  /* search for subscriptions to start or stop. */
846  sublist = get_subscription_list();
847 
848  /* Start the missing workers for enabled subscriptions. */
849  foreach(lc, sublist)
850  {
851  Subscription *sub = (Subscription *) lfirst(lc);
852  LogicalRepWorker *w;
853 
854  if (!sub->enabled)
855  continue;
856 
857  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
858  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
859  LWLockRelease(LogicalRepWorkerLock);
860 
861  if (w == NULL)
862  {
863  last_start_time = now;
864  wait_time = wal_retrieve_retry_interval;
865 
866  logicalrep_worker_launch(sub->dbid, sub->oid, sub->name,
867  sub->owner, InvalidOid);
868  }
869  }
870 
871  /* Switch back to original memory context. */
872  MemoryContextSwitchTo(oldctx);
873  /* Clean the temporary memory. */
874  MemoryContextDelete(subctx);
875  }
876  else
877  {
878  /*
879  * The wait in previous cycle was interrupted in less than
880  * wal_retrieve_retry_interval since last worker was started, this
881  * usually means crash of the worker, so we should retry in
882  * wal_retrieve_retry_interval again.
883  */
884  wait_time = wal_retrieve_retry_interval;
885  }
886 
887  /* Wait for more work. */
888  rc = WaitLatch(MyLatch,
890  wait_time,
892 
893  if (rc & WL_LATCH_SET)
894  {
897  }
898 
900  {
901  ConfigReloadPending = false;
903  }
904  }
905 
906  /* Not reachable */
907 }
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:1816
#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:632
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:5672
#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:1203
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:102
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:5724

◆ ApplyLauncherRegister()

void ApplyLauncherRegister ( void  )

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

703 {
704  BackgroundWorker bgw;
705 
707  return;
708 
709  memset(&bgw, 0, sizeof(bgw));
713  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
714  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
716  "logical replication launcher");
718  "logical replication launcher");
719  bgw.bgw_restart_time = 5;
720  bgw.bgw_notify_pid = 0;
721  bgw.bgw_main_arg = (Datum) 0;
722 
724 }
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:878
int bgw_restart_time
Definition: bgworker.h:94
#define BGWORKER_SHMEM_ACCESS
Definition: bgworker.h:52
char bgw_function_name[BGW_MAXLEN]
Definition: bgworker.h:96
Datum bgw_main_arg
Definition: bgworker.h:97
uintptr_t Datum
Definition: postgres.h:411
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:54
#define BGW_MAXLEN
Definition: bgworker.h:85
BgWorkerStartTime bgw_start_time
Definition: bgworker.h:93
char bgw_type[BGW_MAXLEN]
Definition: bgworker.h:91
pid_t bgw_notify_pid
Definition: bgworker.h:99
char bgw_library_name[BGW_MAXLEN]
Definition: bgworker.h:95
#define snprintf
Definition: port.h:216

◆ ApplyLauncherShmemInit()

void ApplyLauncherShmemInit ( void  )

Definition at line 731 of file launcher.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

732 {
733  bool found;
734 
736  ShmemInitStruct("Logical Replication Launcher Data",
738  &found);
739 
740  if (!found)
741  {
742  int slot;
743 
745 
746  /* Initialize memory and spin locks for each worker slot. */
747  for (slot = 0; slot < max_logical_replication_workers; slot++)
748  {
749  LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
750 
751  memset(worker, 0, sizeof(LogicalRepWorker));
752  SpinLockInit(&worker->relmutex);
753  }
754  }
755 }
#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:683
int max_logical_replication_workers
Definition: launcher.c:54
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:68

◆ ApplyLauncherShmemSize()

Size ApplyLauncherShmemSize ( void  )

Definition at line 683 of file launcher.c.

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

Referenced by ApplyLauncherShmemInit(), and CreateSharedMemoryAndSemaphores().

684 {
685  Size size;
686 
687  /*
688  * Need the fixed struct and the array of LogicalRepWorker.
689  */
690  size = sizeof(LogicalRepCtxStruct);
691  size = MAXALIGN(size);
693  sizeof(LogicalRepWorker)));
694  return size;
695 }
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 780 of file launcher.c.

References on_commit_launcher_wakeup.

Referenced by AlterSubscription(), and CreateSubscription().

781 {
784 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:76

◆ AtEOXact_ApplyLauncher()

void AtEOXact_ApplyLauncher ( bool  isCommit)

Definition at line 761 of file launcher.c.

References ApplyLauncherWakeup(), and on_commit_launcher_wakeup.

Referenced by AbortTransaction(), and CommitTransaction().

762 {
763  if (isCommit)
764  {
767  }
768 
770 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:76
static void ApplyLauncherWakeup(void)
Definition: launcher.c:787

◆ IsLogicalLauncher()

bool IsLogicalLauncher ( void  )

Definition at line 913 of file launcher.c.

References LogicalRepCtxStruct::launcher_pid, and MyProcPid.

Referenced by ProcessInterrupts().

914 {
916 }
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().