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)
 
bool XactManipulatesLogicalReplicationWorkers (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 873 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.

874 {
875  TimestampTz last_start_time = 0;
876 
877  ereport(DEBUG1,
878  (errmsg("logical replication launcher started")));
879 
881 
884 
885  /* Establish signal handlers. */
887  pqsignal(SIGTERM, die);
889 
890  /*
891  * Establish connection to nailed catalogs (we only ever access
892  * pg_subscription).
893  */
895 
896  /* Enter main loop */
897  for (;;)
898  {
899  int rc;
900  List *sublist;
901  ListCell *lc;
902  MemoryContext subctx;
903  MemoryContext oldctx;
905  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
906 
908 
909  now = GetCurrentTimestamp();
910 
911  /* Limit the start retry to once a wal_retrieve_retry_interval */
912  if (TimestampDifferenceExceeds(last_start_time, now,
914  {
915  /* Use temporary context for the database list and worker info. */
917  "Logical Replication Launcher sublist",
921  oldctx = MemoryContextSwitchTo(subctx);
922 
923  /* search for subscriptions to start or stop. */
924  sublist = get_subscription_list();
925 
926  /* Start the missing workers for enabled subscriptions. */
927  foreach(lc, sublist)
928  {
929  Subscription *sub = (Subscription *) lfirst(lc);
930  LogicalRepWorker *w;
931 
932  if (!sub->enabled)
933  continue;
934 
935  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
936  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
937  LWLockRelease(LogicalRepWorkerLock);
938 
939  if (w == NULL)
940  {
941  last_start_time = now;
942  wait_time = wal_retrieve_retry_interval;
943 
944  logicalrep_worker_launch(sub->dbid, sub->oid, sub->name,
945  sub->owner, InvalidOid);
946  }
947  }
948 
949  /* Switch back to original memory context. */
950  MemoryContextSwitchTo(oldctx);
951  /* Clean the temporary memory. */
952  MemoryContextDelete(subctx);
953  }
954  else
955  {
956  /*
957  * The wait in previous cycle was interrupted in less than
958  * wal_retrieve_retry_interval since last worker was started, this
959  * usually means crash of the worker, so we should retry in
960  * wal_retrieve_retry_interval again.
961  */
962  wait_time = wal_retrieve_retry_interval;
963  }
964 
965  /* Wait for more work. */
966  rc = WaitLatch(MyLatch,
968  wait_time,
970 
971  /* emergency bailout if postmaster has died */
972  if (rc & WL_POSTMASTER_DEATH)
973  proc_exit(1);
974 
975  if (rc & WL_LATCH_SET)
976  {
979  }
980 
981  if (got_SIGHUP)
982  {
983  got_SIGHUP = false;
985  }
986  }
987 
988  /* Not reachable */
989 }
static volatile sig_atomic_t got_SIGHUP
Definition: launcher.c:91
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:497
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:672
#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:696
#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:105
#define NULL
Definition: c.h:229
void logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid)
Definition: launcher.c:288
#define Assert(condition)
Definition: c.h:676
#define lfirst(lc)
Definition: pg_list.h:106
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:237
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:98
Definition: pg_list.h:45
#define WL_LATCH_SET
Definition: latch.h:124
void BackgroundWorkerInitializeConnection(char *dbname, char *username)
Definition: postmaster.c:5527
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1534
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5579
void ApplyLauncherRegister ( void  )

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

757 {
758  BackgroundWorker bgw;
759 
761  return;
762 
763  memset(&bgw, 0, sizeof(bgw));
767  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
768  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
770  "logical replication launcher");
771  bgw.bgw_restart_time = 5;
772  bgw.bgw_notify_pid = 0;
773  bgw.bgw_main_arg = (Datum) 0;
774 
776 }
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 783 of file launcher.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

784 {
785  bool found;
786 
788  ShmemInitStruct("Logical Replication Launcher Data",
790  &found);
791 
792  if (!found)
793  {
794  int slot;
795 
797 
798  /* Initialize memory and spin locks for each worker slot. */
799  for (slot = 0; slot < max_logical_replication_workers; slot++)
800  {
801  LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
802 
803  memset(worker, 0, sizeof(LogicalRepWorker));
804  SpinLockInit(&worker->relmutex);
805  }
806  }
807 }
#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:737
int max_logical_replication_workers
Definition: launcher.c:60
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
Size ApplyLauncherShmemSize ( void  )

Definition at line 737 of file launcher.c.

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

Referenced by ApplyLauncherShmemInit(), and CreateSharedMemoryAndSemaphores().

738 {
739  Size size;
740 
741  /*
742  * Need the fixed struct and the array of LogicalRepWorker.
743  */
744  size = sizeof(LogicalRepCtxStruct);
745  size = MAXALIGN(size);
747  sizeof(LogicalRepWorker)));
748  return size;
749 }
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 856 of file launcher.c.

References on_commit_launcher_wakeup.

Referenced by AlterSubscription(), and CreateSubscription().

857 {
860 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:93
void AtEOXact_ApplyLauncher ( bool  isCommit)

Definition at line 823 of file launcher.c.

References ApplyLauncherWakeup(), lfirst, logicalrep_worker_stop(), NIL, on_commit_launcher_wakeup, LogicalRepWorkerId::relid, and LogicalRepWorkerId::subid.

Referenced by AbortTransaction(), and CommitTransaction().

824 {
825  if (isCommit)
826  {
827  ListCell *lc;
828 
829  foreach(lc, on_commit_stop_workers)
830  {
831  LogicalRepWorkerId *wid = lfirst(lc);
832 
833  logicalrep_worker_stop(wid->subid, wid->relid);
834  }
835 
838  }
839 
840  /*
841  * No need to pfree on_commit_stop_workers. It was allocated in
842  * transaction memory context, which is going to be cleaned soon.
843  */
846 }
#define NIL
Definition: pg_list.h:69
void logicalrep_worker_stop(Oid subid, Oid relid)
Definition: launcher.c:447
static bool on_commit_launcher_wakeup
Definition: launcher.c:93
#define lfirst(lc)
Definition: pg_list.h:106
static List * on_commit_stop_workers
Definition: launcher.c:82
static void ApplyLauncherWakeup(void)
Definition: launcher.c:863
bool IsLogicalLauncher ( void  )

Definition at line 995 of file launcher.c.

References LogicalRepCtxStruct::launcher_pid, and MyProcPid.

Referenced by ProcessInterrupts().

996 {
998 }
int MyProcPid
Definition: globals.c:39
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
bool XactManipulatesLogicalReplicationWorkers ( void  )

Definition at line 814 of file launcher.c.

References NIL.

Referenced by PrepareTransaction().

815 {
816  return (on_commit_stop_workers != NIL);
817 }
#define NIL
Definition: pg_list.h:69
static List * on_commit_stop_workers
Definition: launcher.c:82

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