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

947 {
948  TimestampTz last_start_time = 0;
949 
950  ereport(DEBUG1,
951  (errmsg("logical replication launcher started")));
952 
954 
957 
958  /* Establish signal handlers. */
960  pqsignal(SIGTERM, die);
962 
963  /*
964  * Establish connection to nailed catalogs (we only ever access
965  * pg_subscription).
966  */
968 
969  /* Enter main loop */
970  for (;;)
971  {
972  int rc;
973  List *sublist;
974  ListCell *lc;
975  MemoryContext subctx;
976  MemoryContext oldctx;
978  long wait_time = DEFAULT_NAPTIME_PER_CYCLE;
979 
981 
982  now = GetCurrentTimestamp();
983 
984  /* Limit the start retry to once a wal_retrieve_retry_interval */
985  if (TimestampDifferenceExceeds(last_start_time, now,
987  {
988  /* Use temporary context for the database list and worker info. */
990  "Logical Replication Launcher sublist",
992  oldctx = MemoryContextSwitchTo(subctx);
993 
994  /* search for subscriptions to start or stop. */
995  sublist = get_subscription_list();
996 
997  /* Start the missing workers for enabled subscriptions. */
998  foreach(lc, sublist)
999  {
1000  Subscription *sub = (Subscription *) lfirst(lc);
1001  LogicalRepWorker *w;
1002 
1003  if (!sub->enabled)
1004  continue;
1005 
1006  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
1007  w = logicalrep_worker_find(sub->oid, InvalidOid, false);
1008  LWLockRelease(LogicalRepWorkerLock);
1009 
1010  if (w == NULL)
1011  {
1012  last_start_time = now;
1013  wait_time = wal_retrieve_retry_interval;
1014 
1015  logicalrep_worker_launch(sub->dbid, sub->oid, sub->name,
1016  sub->owner, InvalidOid);
1017  }
1018  }
1019 
1020  /* Switch back to original memory context. */
1021  MemoryContextSwitchTo(oldctx);
1022  /* Clean the temporary memory. */
1023  MemoryContextDelete(subctx);
1024  }
1025  else
1026  {
1027  /*
1028  * The wait in previous cycle was interrupted in less than
1029  * wal_retrieve_retry_interval since last worker was started, this
1030  * usually means crash of the worker, so we should retry in
1031  * wal_retrieve_retry_interval again.
1032  */
1033  wait_time = wal_retrieve_retry_interval;
1034  }
1035 
1036  /* Wait for more work. */
1037  rc = WaitLatch(MyLatch,
1039  wait_time,
1041 
1042  if (rc & WL_LATCH_SET)
1043  {
1046  }
1047 
1048  if (ConfigReloadPending)
1049  {
1050  ConfigReloadPending = false;
1052  }
1053  }
1054 
1055  /* Not reachable */
1056 }
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:1574
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:1673
void ResetLatch(Latch *latch)
Definition: latch.c:588
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:390
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1812
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
#define SIGHUP
Definition: win32_port.h:153
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:693
MemoryContext TopMemoryContext
Definition: mcxt.c:44
Definition: guc.h:72
int wal_retrieve_retry_interval
Definition: xlog.c:109
void BackgroundWorkerInitializeConnection(const char *dbname, const char *username, uint32 flags)
Definition: postmaster.c:5716
#define DEFAULT_NAPTIME_PER_CYCLE
Definition: launcher.c:52
uintptr_t Datum
Definition: postgres.h:367
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:144
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
static List * get_subscription_list(void)
Definition: launcher.c:108
void logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid)
Definition: launcher.c:282
#define Assert(condition)
Definition: c.h:745
#define lfirst(lc)
Definition: pg_list.h:190
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:231
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1208
int errmsg(const char *fmt,...)
Definition: elog.c:824
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:68
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:26
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:1538
#define die(msg)
Definition: pg_test_fsync.c:96
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5768

◆ ApplyLauncherRegister()

void ApplyLauncherRegister ( void  )

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

764 {
765  BackgroundWorker bgw;
766 
768  return;
769 
770  memset(&bgw, 0, sizeof(bgw));
774  snprintf(bgw.bgw_library_name, BGW_MAXLEN, "postgres");
775  snprintf(bgw.bgw_function_name, BGW_MAXLEN, "ApplyLauncherMain");
777  "logical replication launcher");
779  "logical replication launcher");
780  bgw.bgw_restart_time = 5;
781  bgw.bgw_notify_pid = 0;
782  bgw.bgw_main_arg = (Datum) 0;
783 
785 }
void RegisterBackgroundWorker(BackgroundWorker *worker)
Definition: bgworker.c:827
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: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:193

◆ ApplyLauncherShmemInit()

void ApplyLauncherShmemInit ( void  )

Definition at line 792 of file launcher.c.

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

Referenced by CreateSharedMemoryAndSemaphores().

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

◆ ApplyLauncherShmemSize()

Size ApplyLauncherShmemSize ( void  )

Definition at line 744 of file launcher.c.

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

Referenced by ApplyLauncherShmemInit(), and CreateSharedMemoryAndSemaphores().

745 {
746  Size size;
747 
748  /*
749  * Need the fixed struct and the array of LogicalRepWorker.
750  */
751  size = sizeof(LogicalRepCtxStruct);
752  size = MAXALIGN(size);
754  sizeof(LogicalRepWorker)));
755  return size;
756 }
Size mul_size(Size s1, Size s2)
Definition: shmem.c:515
Size add_size(Size s1, Size s2)
Definition: shmem.c:498
int max_logical_replication_workers
Definition: launcher.c:54
size_t Size
Definition: c.h:473
#define MAXALIGN(LEN)
Definition: c.h:698
struct LogicalRepCtxStruct LogicalRepCtxStruct

◆ ApplyLauncherWakeupAtCommit()

void ApplyLauncherWakeupAtCommit ( void  )

Definition at line 929 of file launcher.c.

References on_commit_launcher_wakeup.

Referenced by AlterSubscription(), and CreateSubscription().

930 {
933 }
static bool on_commit_launcher_wakeup
Definition: launcher.c:96

◆ AtEOSubXact_ApplyLauncher()

void AtEOSubXact_ApplyLauncher ( bool  isCommit,
int  nestDepth 
)

Definition at line 874 of file launcher.c.

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

Referenced by AbortSubTransaction(), and CommitSubTransaction().

875 {
876  StopWorkersData *parent;
877 
878  /* Exit immediately if there's no work to do at this level. */
879  if (on_commit_stop_workers == NULL ||
880  on_commit_stop_workers->nestDepth < nestDepth)
881  return;
882 
883  Assert(on_commit_stop_workers->nestDepth == nestDepth);
884 
885  parent = on_commit_stop_workers->parent;
886 
887  if (isCommit)
888  {
889  /*
890  * If the upper stack element is not an immediate parent
891  * subtransaction, just decrement the notional nesting depth without
892  * doing any real work. Else, we need to merge the current workers
893  * list into the parent.
894  */
895  if (!parent || parent->nestDepth < nestDepth - 1)
896  {
898  return;
899  }
900 
901  parent->workers =
903  }
904  else
905  {
906  /*
907  * Abandon everything that was done at this nesting level. Explicitly
908  * free memory to avoid a transaction-lifespan leak.
909  */
911  }
912 
913  /*
914  * We have taken care of the current subtransaction workers list for both
915  * abort or commit. So we are ready to pop the stack.
916  */
918  on_commit_stop_workers = parent;
919 }
List * workers
Definition: launcher.c:79
List * list_concat(List *list1, const List *list2)
Definition: list.c:515
static StopWorkersData * on_commit_stop_workers
Definition: launcher.c:88
void list_free_deep(List *list)
Definition: list.c:1390
void pfree(void *pointer)
Definition: mcxt.c:1056
#define Assert(condition)
Definition: c.h:745
struct StopWorkersData * parent
Definition: launcher.c:80

◆ AtEOXact_ApplyLauncher()

void AtEOXact_ApplyLauncher ( bool  isCommit)

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

833 {
834 
835  Assert(on_commit_stop_workers == NULL ||
837  on_commit_stop_workers->parent == NULL));
838 
839  if (isCommit)
840  {
841  ListCell *lc;
842 
843  if (on_commit_stop_workers != NULL)
844  {
845  List *workers = on_commit_stop_workers->workers;
846 
847  foreach(lc, workers)
848  {
849  LogicalRepWorkerId *wid = lfirst(lc);
850 
851  logicalrep_worker_stop(wid->subid, wid->relid);
852  }
853  }
854 
857  }
858 
859  /*
860  * No need to pfree on_commit_stop_workers. It was allocated in
861  * transaction memory context, which is going to be cleaned soon.
862  */
863  on_commit_stop_workers = NULL;
865 }
List * workers
Definition: launcher.c:79
static StopWorkersData * on_commit_stop_workers
Definition: launcher.c:88
void logicalrep_worker_stop(Oid subid, Oid relid)
Definition: launcher.c:452
static bool on_commit_launcher_wakeup
Definition: launcher.c:96
#define Assert(condition)
Definition: c.h:745
#define lfirst(lc)
Definition: pg_list.h:190
struct StopWorkersData * parent
Definition: launcher.c:80
static void ApplyLauncherWakeup(void)
Definition: launcher.c:936
Definition: pg_list.h:50

◆ IsLogicalLauncher()

bool IsLogicalLauncher ( void  )

Definition at line 1062 of file launcher.c.

References LogicalRepCtxStruct::launcher_pid, and MyProcPid.

Referenced by ProcessInterrupts().

1063 {
1065 }
int MyProcPid
Definition: globals.c:40
LogicalRepCtxStruct * LogicalRepCtx
Definition: launcher.c:68

◆ XactManipulatesLogicalReplicationWorkers()

bool XactManipulatesLogicalReplicationWorkers ( void  )

Definition at line 823 of file launcher.c.

Referenced by PrepareTransaction().

824 {
825  return (on_commit_stop_workers != NULL);
826 }
static StopWorkersData * on_commit_stop_workers
Definition: launcher.c:88

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