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

884 {
885  TimestampTz last_start_time = 0;
886 
887  ereport(DEBUG1,
888  (errmsg("logical replication launcher started")));
889 
891 
894 
895  /* Establish signal handlers. */
897  pqsignal(SIGTERM, die);
899 
900  /*
901  * Establish connection to nailed catalogs (we only ever access
902  * pg_subscription).
903  */
905 
906  /* Enter main loop */
907  for (;;)
908  {
909  int rc;
910  List *sublist;
911  ListCell *lc;
912  MemoryContext subctx;
913  MemoryContext oldctx;
915  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
916 
918 
919  now = GetCurrentTimestamp();
920 
921  /* Limit the start retry to once a wal_retrieve_retry_interval */
922  if (TimestampDifferenceExceeds(last_start_time, now,
924  {
925  /* Use temporary context for the database list and worker info. */
927  "Logical Replication Launcher sublist",
931  oldctx = MemoryContextSwitchTo(subctx);
932 
933  /* search for subscriptions to start or stop. */
934  sublist = get_subscription_list();
935 
936  /* Start the missing workers for enabled subscriptions. */
937  foreach(lc, sublist)
938  {
939  Subscription *sub = (Subscription *) lfirst(lc);
940  LogicalRepWorker *w;
941 
942  if (!sub->enabled)
943  continue;
944 
945  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
946  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
947  LWLockRelease(LogicalRepWorkerLock);
948 
949  if (w == NULL)
950  {
951  last_start_time = now;
952  wait_time = wal_retrieve_retry_interval;
953 
954  logicalrep_worker_launch(sub->dbid, sub->oid, sub->name,
955  sub->owner, InvalidOid);
956  }
957  }
958 
959  /* Switch back to original memory context. */
960  MemoryContextSwitchTo(oldctx);
961  /* Clean the temporary memory. */
962  MemoryContextDelete(subctx);
963  }
964  else
965  {
966  /*
967  * The wait in previous cycle was interrupted in less than
968  * wal_retrieve_retry_interval since last worker was started, this
969  * usually means crash of the worker, so we should retry in
970  * wal_retrieve_retry_interval again.
971  */
972  wait_time = wal_retrieve_retry_interval;
973  }
974 
975  /* Wait for more work. */
976  rc = WaitLatch(MyLatch,
978  wait_time,
980 
981  /* emergency bailout if postmaster has died */
982  if (rc & WL_POSTMASTER_DEATH)
983  proc_exit(1);
984 
985  if (rc & WL_LATCH_SET)
986  {
989  }
990 
991  if (got_SIGHUP)
992  {
993  got_SIGHUP = false;
995  }
996  }
997 
998  /* Not reachable */
999 }
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:1721
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:680
#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:704
#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
void logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid)
Definition: launcher.c:285
#define Assert(condition)
Definition: c.h:681
#define lfirst(lc)
Definition: pg_list.h:106
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:234
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1117
int errmsg(const char *fmt,...)
Definition: elog.c:797
void die(SIGNAL_ARGS)
Definition: postgres.c:2652
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:5540
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1534
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5592
void ApplyLauncherRegister ( void  )

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

765 {
766  BackgroundWorker bgw;
767 
769  return;
770 
771  memset(&bgw, 0, sizeof(bgw));
775  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
776  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
778  "logical replication launcher");
780  "logical replication launcher");
781  bgw.bgw_restart_time = 5;
782  bgw.bgw_notify_pid = 0;
783  bgw.bgw_main_arg = (Datum) 0;
784 
786 }
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:855
int bgw_restart_time
Definition: bgworker.h:94
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:96
Datum bgw_main_arg
Definition: bgworker.h:97
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: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
void ApplyLauncherShmemInit ( void  )

Definition at line 793 of file launcher.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

794 {
795  bool found;
796 
798  ShmemInitStruct("Logical Replication Launcher Data",
800  &found);
801 
802  if (!found)
803  {
804  int slot;
805 
807 
808  /* Initialize memory and spin locks for each worker slot. */
809  for (slot = 0; slot < max_logical_replication_workers; slot++)
810  {
811  LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
812 
813  memset(worker, 0, sizeof(LogicalRepWorker));
814  SpinLockInit(&worker->relmutex);
815  }
816  }
817 }
#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:745
int max_logical_replication_workers
Definition: launcher.c:60
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:74
Size ApplyLauncherShmemSize ( void  )

Definition at line 745 of file launcher.c.

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

Referenced by ApplyLauncherShmemInit(), and CreateSharedMemoryAndSemaphores().

746 {
747  Size size;
748 
749  /*
750  * Need the fixed struct and the array of LogicalRepWorker.
751  */
752  size = sizeof(LogicalRepCtxStruct);
753  size = MAXALIGN(size);
755  sizeof(LogicalRepWorker)));
756  return size;
757 }
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:350
#define MAXALIGN(LEN)
Definition: c.h:576
struct LogicalRepCtxStruct LogicalRepCtxStruct
void ApplyLauncherWakeupAtCommit ( void  )

Definition at line 866 of file launcher.c.

References on_commit_launcher_wakeup.

Referenced by AlterSubscription(), and CreateSubscription().

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

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

834 {
835  if (isCommit)
836  {
837  ListCell *lc;
838 
839  foreach(lc, on_commit_stop_workers)
840  {
841  LogicalRepWorkerId *wid = lfirst(lc);
842 
843  logicalrep_worker_stop(wid->subid, wid->relid);
844  }
845 
848  }
849 
850  /*
851  * No need to pfree on_commit_stop_workers. It was allocated in
852  * transaction memory context, which is going to be cleaned soon.
853  */
856 }
#define NIL
Definition: pg_list.h:69
void logicalrep_worker_stop(Oid subid, Oid relid)
Definition: launcher.c:455
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:873
bool IsLogicalLauncher ( void  )

Definition at line 1005 of file launcher.c.

References LogicalRepCtxStruct::launcher_pid, and MyProcPid.

Referenced by ProcessInterrupts().

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

Definition at line 824 of file launcher.c.

References NIL.

Referenced by PrepareTransaction().

825 {
826  return (on_commit_stop_workers != NIL);
827 }
#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().