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)
 
bool IsLogicalLauncher (void)
 

Variables

int max_logical_replication_workers
 
int max_sync_workers_per_subscription
 

Function Documentation

void ApplyLauncherMain ( Datum  main_arg)

Definition at line 788 of file launcher.c.

References ALLOCSET_DEFAULT_INITSIZE, ALLOCSET_DEFAULT_MAXSIZE, ALLOCSET_DEFAULT_MINSIZE, AllocSetContextCreate(), Assert, BackgroundWorkerInitializeConnection(), BackgroundWorkerUnblockSignals(), before_shmem_exit(), CHECK_FOR_INTERRUPTS, Subscription::dbid, DEBUG1, DEFAULT_NAPTIME_PER_CYCLE, die(), Subscription::enabled, ereport, errmsg(), get_subscription_list(), GetCurrentTimestamp(), got_SIGHUP, InvalidOid, LogicalRepCtxStruct::launcher_pid, lfirst, logicalrep_launcher_onexit(), logicalrep_launcher_sighup(), logicalrep_worker_find(), logicalrep_worker_launch(), LW_SHARED, LWLockAcquire(), LWLockRelease(), MemoryContextDelete(), MemoryContextSwitchTo(), MyLatch, MyProcPid, Subscription::name, now(), NULL, Subscription::oid, Subscription::owner, PGC_SIGHUP, pqsignal(), proc_exit(), ProcessConfigFile(), ResetLatch(), SIGHUP, TimestampDifferenceExceeds(), TopMemoryContext, WAIT_EVENT_LOGICAL_LAUNCHER_MAIN, WaitLatch(), wal_retrieve_retry_interval, WL_LATCH_SET, WL_POSTMASTER_DEATH, and WL_TIMEOUT.

789 {
790  TimestampTz last_start_time = 0;
791 
792  ereport(DEBUG1,
793  (errmsg("logical replication launcher started")));
794 
796 
799 
800  /* Establish signal handlers. */
802  pqsignal(SIGTERM, die);
804 
805  /*
806  * Establish connection to nailed catalogs (we only ever access
807  * pg_subscription).
808  */
810 
811  /* Enter main loop */
812  for (;;)
813  {
814  int rc;
815  List *sublist;
816  ListCell *lc;
817  MemoryContext subctx;
818  MemoryContext oldctx;
820  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
821 
823 
824  now = GetCurrentTimestamp();
825 
826  /* Limit the start retry to once a wal_retrieve_retry_interval */
827  if (TimestampDifferenceExceeds(last_start_time, now,
829  {
830  /* Use temporary context for the database list and worker info. */
832  "Logical Replication Launcher sublist",
836  oldctx = MemoryContextSwitchTo(subctx);
837 
838  /* search for subscriptions to start or stop. */
839  sublist = get_subscription_list();
840 
841  /* Start the missing workers for enabled subscriptions. */
842  foreach(lc, sublist)
843  {
844  Subscription *sub = (Subscription *) lfirst(lc);
845  LogicalRepWorker *w;
846 
847  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
848  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
849  LWLockRelease(LogicalRepWorkerLock);
850 
851  if (sub->enabled && w == NULL)
852  {
853  last_start_time = now;
854  wait_time = wal_retrieve_retry_interval;
855 
856  logicalrep_worker_launch(sub->dbid, sub->oid, sub->name,
857  sub->owner, InvalidOid);
858  }
859  }
860 
861  /* Switch back to original memory context. */
862  MemoryContextSwitchTo(oldctx);
863  /* Clean the temporary memory. */
864  MemoryContextDelete(subctx);
865  }
866  else
867  {
868  /*
869  * The wait in previous cycle was interrupted in less than
870  * wal_retrieve_retry_interval since last worker was started, this
871  * usually means crash of the worker, so we should retry in
872  * wal_retrieve_retry_interval again.
873  */
874  wait_time = wal_retrieve_retry_interval;
875  }
876 
877  /* Wait for more work. */
878  rc = WaitLatch(MyLatch,
880  wait_time,
882 
883  /* emergency bailout if postmaster has died */
884  if (rc & WL_POSTMASTER_DEATH)
885  proc_exit(1);
886 
887  if (rc & WL_LATCH_SET)
888  {
891  }
892 
893  if (got_SIGHUP)
894  {
895  got_SIGHUP = false;
897  }
898  }
899 
900  /* Not reachable */
901 }
static volatile sig_atomic_t got_SIGHUP
Definition: launcher.c:83
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
#define DEBUG1
Definition: elog.h:25
int MyProcPid
Definition: globals.c:39
#define WL_TIMEOUT
Definition: latch.h:127
void ProcessConfigFile(GucContext context)
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1570
int64 TimestampTz
Definition: timestamp.h:39
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void proc_exit(int code)
Definition: ipc.c:99
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
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 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:614
#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
static void logicalrep_launcher_sighup(SIGNAL_ARGS)
Definition: launcher.c:638
#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:97
#define NULL
Definition: c.h:229
void logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid)
Definition: launcher.c:256
#define Assert(condition)
Definition: c.h:675
#define lfirst(lc)
Definition: pg_list.h:106
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:229
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1111
int errmsg(const char *fmt,...)
Definition: elog.c:797
void die(SIGNAL_ARGS)
Definition: postgres.c:2617
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
#define ALLOCSET_DEFAULT_INITSIZE
Definition: memutils.h:163
struct Latch * MyLatch
Definition: globals.c:52
#define ALLOCSET_DEFAULT_MAXSIZE
Definition: memutils.h:164
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:100
Definition: pg_list.h:45
#define WL_LATCH_SET
Definition: latch.h:124
void BackgroundWorkerInitializeConnection(char *dbname, char *username)
Definition: postmaster.c:5466
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1534
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5518
void ApplyLauncherRegister ( void  )

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

699 {
700  BackgroundWorker bgw;
701 
703  return;
704 
705  memset(&bgw, 0, sizeof(bgw));
709  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
710  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
712  "logical replication launcher");
713  bgw.bgw_restart_time = 5;
714  bgw.bgw_notify_pid = 0;
715  bgw.bgw_main_arg = (Datum) 0;
716 
718 }
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 725 of file launcher.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

726 {
727  bool found;
728 
730  ShmemInitStruct("Logical Replication Launcher Data",
732  &found);
733 
734  if (!found)
735  {
736  int slot;
737 
739 
740  /* Initialize memory and spin locks for each worker slot. */
741  for (slot = 0; slot < max_logical_replication_workers; slot++)
742  {
743  LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
744 
745  memset(worker, 0, sizeof(LogicalRepWorker));
746  SpinLockInit(&worker->relmutex);
747  }
748  }
749 }
#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:679
int max_logical_replication_workers
Definition: launcher.c:60
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
Size ApplyLauncherShmemSize ( void  )

Definition at line 679 of file launcher.c.

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

Referenced by ApplyLauncherShmemInit(), and CreateSharedMemoryAndSemaphores().

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

References on_commit_launcher_wakeup.

Referenced by AlterSubscription(), and CreateSubscription().

772 {
775 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:85
void AtEOXact_ApplyLauncher ( bool  isCommit)

Definition at line 755 of file launcher.c.

References ApplyLauncherWakeup(), and on_commit_launcher_wakeup.

Referenced by AbortTransaction(), and CommitTransaction().

756 {
757  if (isCommit && on_commit_launcher_wakeup)
759 
761 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:85
static void ApplyLauncherWakeup(void)
Definition: launcher.c:778
bool IsLogicalLauncher ( void  )

Definition at line 907 of file launcher.c.

References LogicalRepCtxStruct::launcher_pid, and MyProcPid.

Referenced by ProcessInterrupts().

908 {
910 }
int MyProcPid
Definition: globals.c:39
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74

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