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)
 
bool XactManipulatesLogicalReplicationWorkers (void)
 
void AtEOXact_ApplyLauncher (bool isCommit)
 
void AtEOSubXact_ApplyLauncher (bool isCommit, int nestDepth)
 
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 972 of file launcher.c.

References ALLOCSET_DEFAULT_SIZES, 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(), ProcessConfigFile(), ResetLatch(), SIGHUP, TimestampDifferenceExceeds(), TopMemoryContext, WAIT_EVENT_LOGICAL_LAUNCHER_MAIN, WaitLatch(), wal_retrieve_retry_interval, WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and WL_TIMEOUT.

973 {
974  TimestampTz last_start_time = 0;
975 
976  ereport(DEBUG1,
977  (errmsg("logical replication launcher started")));
978 
980 
983 
984  /* Establish signal handlers. */
986  pqsignal(SIGTERM, die);
988 
989  /*
990  * Establish connection to nailed catalogs (we only ever access
991  * pg_subscription).
992  */
994 
995  /* Enter main loop */
996  for (;;)
997  {
998  int rc;
999  List *sublist;
1000  ListCell *lc;
1001  MemoryContext subctx;
1002  MemoryContext oldctx;
1003  TimestampTz now;
1004  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
1005 
1007 
1008  now = GetCurrentTimestamp();
1009 
1010  /* Limit the start retry to once a wal_retrieve_retry_interval */
1011  if (TimestampDifferenceExceeds(last_start_time, now,
1013  {
1014  /* Use temporary context for the database list and worker info. */
1016  "Logical Replication Launcher sublist",
1018  oldctx = MemoryContextSwitchTo(subctx);
1019 
1020  /* search for subscriptions to start or stop. */
1021  sublist = get_subscription_list();
1022 
1023  /* Start the missing workers for enabled subscriptions. */
1024  foreach(lc, sublist)
1025  {
1026  Subscription *sub = (Subscription *) lfirst(lc);
1027  LogicalRepWorker *w;
1028 
1029  if (!sub->enabled)
1030  continue;
1031 
1032  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1033  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
1034  LWLockRelease(LogicalRepWorkerLock);
1035 
1036  if (w == NULL)
1037  {
1038  last_start_time = now;
1039  wait_time = wal_retrieve_retry_interval;
1040 
1041  logicalrep_worker_launch(sub->dbid, sub->oid, sub->name,
1042  sub->owner, InvalidOid);
1043  }
1044  }
1045 
1046  /* Switch back to original memory context. */
1047  MemoryContextSwitchTo(oldctx);
1048  /* Clean the temporary memory. */
1049  MemoryContextDelete(subctx);
1050  }
1051  else
1052  {
1053  /*
1054  * The wait in previous cycle was interrupted in less than
1055  * wal_retrieve_retry_interval since last worker was started, this
1056  * usually means crash of the worker, so we should retry in
1057  * wal_retrieve_retry_interval again.
1058  */
1059  wait_time = wal_retrieve_retry_interval;
1060  }
1061 
1062  /* Wait for more work. */
1063  rc = WaitLatch(MyLatch,
1065  wait_time,
1067 
1068  if (rc & WL_LATCH_SET)
1069  {
1072  }
1073 
1074  if (got_SIGHUP)
1075  {
1076  got_SIGHUP = false;
1078  }
1079  }
1080 
1081  /* Not reachable */
1082 }
static volatile sig_atomic_t got_SIGHUP
Definition: launcher.c:104
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
#define DEBUG1
Definition: elog.h:25
int MyProcPid
Definition: globals.c:40
#define WL_TIMEOUT
Definition: latch.h:127
void ProcessConfigFile(GucContext context)
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1583
int64 TimestampTz
Definition: timestamp.h:39
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition: timestamp.c:1682
void ResetLatch(Latch *latch)
Definition: latch.c:519
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:344
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define SIGHUP
Definition: win32_port.h:154
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:705
#define ereport(elevel, rest)
Definition: elog.h:141
MemoryContext TopMemoryContext
Definition: mcxt.c:44
Definition: guc.h:72
int wal_retrieve_retry_interval
Definition: xlog.c:106
void BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
Definition: postmaster.c:5685
#define DEFAULT_NAPTIME_PER_CYCLE
Definition: launcher.c:59
uintptr_t Datum
Definition: postgres.h:367
static void logicalrep_launcher_sighup(SIGNAL_ARGS)
Definition: launcher.c:729
#define InvalidOid
Definition: postgres_ext.h:36
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
static List * get_subscription_list(void)
Definition: launcher.c:118
void logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid)
Definition: launcher.c:294
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:243
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
int errmsg(const char *fmt,...)
Definition: elog.c:784
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:75
struct Latch * MyLatch
Definition: globals.c:54
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
Definition: pg_list.h:50
#define WL_LATCH_SET
Definition: latch.h:124
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1547
#define die(msg)
Definition: pg_test_fsync.c:97
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5737

◆ ApplyLauncherRegister()

void ApplyLauncherRegister ( void  )

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

790 {
791  BackgroundWorker bgw;
792 
794  return;
795 
796  memset(&bgw, 0, sizeof(bgw));
800  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
801  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
803  "logical replication launcher");
805  "logical replication launcher");
806  bgw.bgw_restart_time = 5;
807  bgw.bgw_notify_pid = 0;
808  bgw.bgw_main_arg = (Datum) 0;
809 
811 }
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:848
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:367
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:61
#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:192

◆ ApplyLauncherShmemInit()

void ApplyLauncherShmemInit ( void  )

Definition at line 818 of file launcher.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

819 {
820  bool found;
821 
823  ShmemInitStruct("Logical Replication Launcher Data",
825  &found);
826 
827  if (!found)
828  {
829  int slot;
830 
832 
833  /* Initialize memory and spin locks for each worker slot. */
834  for (slot = 0; slot < max_logical_replication_workers; slot++)
835  {
836  LogicalRepWorker *worker = &LogicalRepCtx->workers[slot];
837 
838  memset(worker, 0, sizeof(LogicalRepWorker));
839  SpinLockInit(&worker->relmutex);
840  }
841  }
842 }
#define SpinLockInit(lock)
Definition: spin.h:60
LogicalRepWorker workers[FLEXIBLE_ARRAY_MEMBER]
Definition: launcher.c:72
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
Size ApplyLauncherShmemSize(void)
Definition: launcher.c:770
int max_logical_replication_workers
Definition: launcher.c:61
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:75

◆ ApplyLauncherShmemSize()

Size ApplyLauncherShmemSize ( void  )

Definition at line 770 of file launcher.c.

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

Referenced by ApplyLauncherShmemInit(), and CreateSharedMemoryAndSemaphores().

771 {
772  Size size;
773 
774  /*
775  * Need the fixed struct and the array of LogicalRepWorker.
776  */
777  size = sizeof(LogicalRepCtxStruct);
778  size = MAXALIGN(size);
780  sizeof(LogicalRepWorker)));
781  return size;
782 }
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:61
size_t Size
Definition: c.h:466
#define MAXALIGN(LEN)
Definition: c.h:685
struct LogicalRepCtxStruct LogicalRepCtxStruct

◆ ApplyLauncherWakeupAtCommit()

void ApplyLauncherWakeupAtCommit ( void  )

Definition at line 955 of file launcher.c.

References on_commit_launcher_wakeup.

Referenced by AlterSubscription(), and CreateSubscription().

956 {
959 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:106

◆ AtEOSubXact_ApplyLauncher()

void AtEOSubXact_ApplyLauncher ( bool  isCommit,
int  nestDepth 
)

Definition at line 900 of file launcher.c.

References Assert, list_concat(), list_free_deep(), StopWorkersData::nestDepth, StopWorkersData::parent, pfree(), and StopWorkersData::workers.

Referenced by AbortSubTransaction(), and CommitSubTransaction().

901 {
902  StopWorkersData *parent;
903 
904  /* Exit immediately if there's no work to do at this level. */
905  if (on_commit_stop_workers == NULL ||
906  on_commit_stop_workers->nestDepth < nestDepth)
907  return;
908 
909  Assert(on_commit_stop_workers->nestDepth == nestDepth);
910 
911  parent = on_commit_stop_workers->parent;
912 
913  if (isCommit)
914  {
915  /*
916  * If the upper stack element is not an immediate parent
917  * subtransaction, just decrement the notional nesting depth without
918  * doing any real work. Else, we need to merge the current workers
919  * list into the parent.
920  */
921  if (!parent || parent->nestDepth < nestDepth - 1)
922  {
924  return;
925  }
926 
927  parent->workers =
929  }
930  else
931  {
932  /*
933  * Abandon everything that was done at this nesting level. Explicitly
934  * free memory to avoid a transaction-lifespan leak.
935  */
937  }
938 
939  /*
940  * We have taken care of the current subtransaction workers list for both
941  * abort or commit. So we are ready to pop the stack.
942  */
944  on_commit_stop_workers = parent;
945 }
List * workers
Definition: launcher.c:86
List * list_concat(List *list1, const List *list2)
Definition: list.c:516
static StopWorkersData * on_commit_stop_workers
Definition: launcher.c:95
void list_free_deep(List *list)
Definition: list.c:1391
void pfree(void *pointer)
Definition: mcxt.c:1056
#define Assert(condition)
Definition: c.h:732
struct StopWorkersData * parent
Definition: launcher.c:87

◆ AtEOXact_ApplyLauncher()

void AtEOXact_ApplyLauncher ( bool  isCommit)

Definition at line 858 of file launcher.c.

References ApplyLauncherWakeup(), Assert, lfirst, logicalrep_worker_stop(), StopWorkersData::nestDepth, on_commit_launcher_wakeup, StopWorkersData::parent, LogicalRepWorkerId::relid, LogicalRepWorkerId::subid, LogicalRepCtxStruct::workers, and StopWorkersData::workers.

Referenced by AbortTransaction(), and CommitTransaction().

859 {
860 
861  Assert(on_commit_stop_workers == NULL ||
863  on_commit_stop_workers->parent == NULL));
864 
865  if (isCommit)
866  {
867  ListCell *lc;
868 
869  if (on_commit_stop_workers != NULL)
870  {
871  List *workers = on_commit_stop_workers->workers;
872 
873  foreach(lc, workers)
874  {
875  LogicalRepWorkerId *wid = lfirst(lc);
876 
877  logicalrep_worker_stop(wid->subid, wid->relid);
878  }
879  }
880 
883  }
884 
885  /*
886  * No need to pfree on_commit_stop_workers. It was allocated in
887  * transaction memory context, which is going to be cleaned soon.
888  */
889  on_commit_stop_workers = NULL;
891 }
List * workers
Definition: launcher.c:86
static StopWorkersData * on_commit_stop_workers
Definition: launcher.c:95
void logicalrep_worker_stop(Oid subid, Oid relid)
Definition: launcher.c:464
static bool on_commit_launcher_wakeup
Definition: launcher.c:106
#define Assert(condition)
Definition: c.h:732
#define lfirst(lc)
Definition: pg_list.h:190
struct StopWorkersData * parent
Definition: launcher.c:87
static void ApplyLauncherWakeup(void)
Definition: launcher.c:962
Definition: pg_list.h:50

◆ IsLogicalLauncher()

bool IsLogicalLauncher ( void  )

Definition at line 1088 of file launcher.c.

References LogicalRepCtxStruct::launcher_pid, and MyProcPid.

Referenced by ProcessInterrupts().

1089 {
1091 }
int MyProcPid
Definition: globals.c:40
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:75

◆ XactManipulatesLogicalReplicationWorkers()

bool XactManipulatesLogicalReplicationWorkers ( void  )

Definition at line 849 of file launcher.c.

Referenced by PrepareTransaction().

850 {
851  return (on_commit_stop_workers != NULL);
852 }
static StopWorkersData * on_commit_stop_workers
Definition: launcher.c:95

Variable Documentation

◆ max_logical_replication_workers

◆ max_sync_workers_per_subscription

int max_sync_workers_per_subscription

Definition at line 62 of file launcher.c.

Referenced by logicalrep_worker_launch(), and process_syncing_tables_for_apply().