PostgreSQL Source Code  git master
logicalworker.h File Reference
#include <signal.h>
Include dependency graph for logicalworker.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void ApplyWorkerMain (Datum main_arg)
 
void ParallelApplyWorkerMain (Datum main_arg)
 
void TablesyncWorkerMain (Datum main_arg)
 
bool IsLogicalWorker (void)
 
bool IsLogicalParallelApplyWorker (void)
 
void HandleParallelApplyMessageInterrupt (void)
 
void HandleParallelApplyMessages (void)
 
void LogicalRepWorkersWakeupAtCommit (Oid subid)
 
void AtEOXact_LogicalRepWorkers (bool isCommit)
 

Variables

PGDLLIMPORT volatile sig_atomic_t ParallelApplyMessagePending
 

Function Documentation

◆ ApplyWorkerMain()

void ApplyWorkerMain ( Datum  main_arg)

Definition at line 4703 of file worker.c.

4704 {
4705  int worker_slot = DatumGetInt32(main_arg);
4706 
4707  InitializingApplyWorker = true;
4708 
4709  SetupApplyOrSyncWorker(worker_slot);
4710 
4711  InitializingApplyWorker = false;
4712 
4713  run_apply_worker();
4714 
4715  proc_exit(0);
4716 }
bool InitializingApplyWorker
Definition: worker.c:336
static void run_apply_worker()
Definition: worker.c:4475
void SetupApplyOrSyncWorker(int worker_slot)
Definition: worker.c:4662
void proc_exit(int code)
Definition: ipc.c:104
static int32 DatumGetInt32(Datum X)
Definition: postgres.h:202

References DatumGetInt32(), InitializingApplyWorker, proc_exit(), run_apply_worker(), and SetupApplyOrSyncWorker().

◆ AtEOXact_LogicalRepWorkers()

void AtEOXact_LogicalRepWorkers ( bool  isCommit)

Definition at line 5021 of file worker.c.

5022 {
5023  if (isCommit && on_commit_wakeup_workers_subids != NIL)
5024  {
5025  ListCell *lc;
5026 
5027  LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
5028  foreach(lc, on_commit_wakeup_workers_subids)
5029  {
5030  Oid subid = lfirst_oid(lc);
5031  List *workers;
5032  ListCell *lc2;
5033 
5034  workers = logicalrep_workers_find(subid, true);
5035  foreach(lc2, workers)
5036  {
5037  LogicalRepWorker *worker = (LogicalRepWorker *) lfirst(lc2);
5038 
5040  }
5041  }
5042  LWLockRelease(LogicalRepWorkerLock);
5043  }
5044 
5045  /* The List storage will be reclaimed automatically in xact cleanup. */
5047 }
static List * on_commit_wakeup_workers_subids
Definition: worker.c:319
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition: launcher.c:702
List * logicalrep_workers_find(Oid subid, bool only_running)
Definition: launcher.c:281
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1195
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1808
@ LW_SHARED
Definition: lwlock.h:117
#define lfirst(lc)
Definition: pg_list.h:172
#define NIL
Definition: pg_list.h:68
#define lfirst_oid(lc)
Definition: pg_list.h:174
unsigned int Oid
Definition: postgres_ext.h:31
Definition: pg_list.h:54

References lfirst, lfirst_oid, logicalrep_worker_wakeup_ptr(), logicalrep_workers_find(), LW_SHARED, LWLockAcquire(), LWLockRelease(), NIL, and on_commit_wakeup_workers_subids.

Referenced by AbortTransaction(), CommitTransaction(), and PrepareTransaction().

◆ HandleParallelApplyMessageInterrupt()

void HandleParallelApplyMessageInterrupt ( void  )

Definition at line 990 of file applyparallelworker.c.

991 {
992  InterruptPending = true;
994  SetLatch(MyLatch);
995 }
volatile sig_atomic_t ParallelApplyMessagePending
volatile sig_atomic_t InterruptPending
Definition: globals.c:30
struct Latch * MyLatch
Definition: globals.c:58
void SetLatch(Latch *latch)
Definition: latch.c:633

References InterruptPending, MyLatch, ParallelApplyMessagePending, and SetLatch().

Referenced by procsignal_sigusr1_handler().

◆ HandleParallelApplyMessages()

void HandleParallelApplyMessages ( void  )

Definition at line 1064 of file applyparallelworker.c.

1065 {
1066  ListCell *lc;
1067  MemoryContext oldcontext;
1068 
1069  static MemoryContext hpam_context = NULL;
1070 
1071  /*
1072  * This is invoked from ProcessInterrupts(), and since some of the
1073  * functions it calls contain CHECK_FOR_INTERRUPTS(), there is a potential
1074  * for recursive calls if more signals are received while this runs. It's
1075  * unclear that recursive entry would be safe, and it doesn't seem useful
1076  * even if it is safe, so let's block interrupts until done.
1077  */
1078  HOLD_INTERRUPTS();
1079 
1080  /*
1081  * Moreover, CurrentMemoryContext might be pointing almost anywhere. We
1082  * don't want to risk leaking data into long-lived contexts, so let's do
1083  * our work here in a private context that we can reset on each use.
1084  */
1085  if (!hpam_context) /* first time through? */
1086  hpam_context = AllocSetContextCreate(TopMemoryContext,
1087  "HandleParallelApplyMessages",
1089  else
1090  MemoryContextReset(hpam_context);
1091 
1092  oldcontext = MemoryContextSwitchTo(hpam_context);
1093 
1095 
1096  foreach(lc, ParallelApplyWorkerPool)
1097  {
1099  Size nbytes;
1100  void *data;
1102 
1103  /*
1104  * The leader will detach from the error queue and set it to NULL
1105  * before preparing to stop all parallel apply workers, so we don't
1106  * need to handle error messages anymore. See
1107  * logicalrep_worker_detach.
1108  */
1109  if (!winfo->error_mq_handle)
1110  continue;
1111 
1112  res = shm_mq_receive(winfo->error_mq_handle, &nbytes, &data, true);
1113 
1114  if (res == SHM_MQ_WOULD_BLOCK)
1115  continue;
1116  else if (res == SHM_MQ_SUCCESS)
1117  {
1118  StringInfoData msg;
1119 
1120  initStringInfo(&msg);
1121  appendBinaryStringInfo(&msg, data, nbytes);
1123  pfree(msg.data);
1124  }
1125  else
1126  ereport(ERROR,
1127  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1128  errmsg("lost connection to the logical replication parallel apply worker")));
1129  }
1130 
1131  MemoryContextSwitchTo(oldcontext);
1132 
1133  /* Might as well clear the context on our way out */
1134  MemoryContextReset(hpam_context);
1135 
1137 }
static List * ParallelApplyWorkerPool
static void HandleParallelApplyMessage(StringInfo msg)
size_t Size
Definition: c.h:594
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:330
void pfree(void *pointer)
Definition: mcxt.c:1456
MemoryContext TopMemoryContext
Definition: mcxt.c:141
#define AllocSetContextCreate
Definition: memutils.h:126
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:150
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:134
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:132
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
const void * data
shm_mq_result shm_mq_receive(shm_mq_handle *mqh, Size *nbytesp, void **datap, bool nowait)
Definition: shm_mq.c:573
shm_mq_result
Definition: shm_mq.h:37
@ SHM_MQ_SUCCESS
Definition: shm_mq.h:38
@ SHM_MQ_WOULD_BLOCK
Definition: shm_mq.h:39
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition: stringinfo.c:233
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
shm_mq_handle * error_mq_handle

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, appendBinaryStringInfo(), StringInfoData::data, data, ereport, errcode(), errmsg(), ERROR, ParallelApplyWorkerInfo::error_mq_handle, HandleParallelApplyMessage(), HOLD_INTERRUPTS, initStringInfo(), lfirst, MemoryContextReset(), MemoryContextSwitchTo(), ParallelApplyMessagePending, ParallelApplyWorkerPool, pfree(), res, RESUME_INTERRUPTS, shm_mq_receive(), SHM_MQ_SUCCESS, SHM_MQ_WOULD_BLOCK, and TopMemoryContext.

Referenced by ProcessInterrupts().

◆ IsLogicalParallelApplyWorker()

bool IsLogicalParallelApplyWorker ( void  )

Definition at line 4771 of file worker.c.

4772 {
4774 }
bool IsLogicalWorker(void)
Definition: worker.c:4762
static bool am_parallel_apply_worker(void)

References am_parallel_apply_worker(), and IsLogicalWorker().

Referenced by mq_putmessage().

◆ IsLogicalWorker()

bool IsLogicalWorker ( void  )

Definition at line 4762 of file worker.c.

4763 {
4764  return MyLogicalRepWorker != NULL;
4765 }
LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:61

References MyLogicalRepWorker.

Referenced by IsLogicalParallelApplyWorker(), and ProcessInterrupts().

◆ LogicalRepWorkersWakeupAtCommit()

void LogicalRepWorkersWakeupAtCommit ( Oid  subid)

Definition at line 5007 of file worker.c.

5008 {
5009  MemoryContext oldcxt;
5010 
5014  MemoryContextSwitchTo(oldcxt);
5015 }
List * list_append_unique_oid(List *list, Oid datum)
Definition: list.c:1379
MemoryContext TopTransactionContext
Definition: mcxt.c:146

References list_append_unique_oid(), MemoryContextSwitchTo(), on_commit_wakeup_workers_subids, and TopTransactionContext.

Referenced by AlterObjectRename_internal(), AlterSubscription(), and AlterSubscriptionOwner_internal().

◆ ParallelApplyWorkerMain()

void ParallelApplyWorkerMain ( Datum  main_arg)

Definition at line 858 of file applyparallelworker.c.

859 {
861  dsm_handle handle;
862  dsm_segment *seg;
863  shm_toc *toc;
864  shm_mq *mq;
865  shm_mq_handle *mqh;
866  shm_mq_handle *error_mqh;
867  RepOriginId originid;
868  int worker_slot = DatumGetInt32(main_arg);
869  char originname[NAMEDATALEN];
870 
872 
873  /* Setup signal handling. */
876  pqsignal(SIGTERM, die);
878 
879  /*
880  * Attach to the dynamic shared memory segment for the parallel apply, and
881  * find its table of contents.
882  *
883  * Like parallel query, we don't need resource owner by this time. See
884  * ParallelWorkerMain.
885  */
886  memcpy(&handle, MyBgworkerEntry->bgw_extra, sizeof(dsm_handle));
887  seg = dsm_attach(handle);
888  if (!seg)
889  ereport(ERROR,
890  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
891  errmsg("could not map dynamic shared memory segment")));
892 
894  if (!toc)
895  ereport(ERROR,
896  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
897  errmsg("invalid magic number in dynamic shared memory segment")));
898 
899  /* Look up the shared information. */
900  shared = shm_toc_lookup(toc, PARALLEL_APPLY_KEY_SHARED, false);
901  MyParallelShared = shared;
902 
903  /*
904  * Attach to the message queue.
905  */
906  mq = shm_toc_lookup(toc, PARALLEL_APPLY_KEY_MQ, false);
908  mqh = shm_mq_attach(mq, seg, NULL);
909 
910  /*
911  * Primary initialization is complete. Now, we can attach to our slot.
912  * This is to ensure that the leader apply worker does not write data to
913  * the uninitialized memory queue.
914  */
915  logicalrep_worker_attach(worker_slot);
916 
917  /*
918  * Register the shutdown callback after we are attached to the worker
919  * slot. This is to ensure that MyLogicalRepWorker remains valid when this
920  * callback is invoked.
921  */
923 
928 
929  /*
930  * Attach to the error queue.
931  */
934  error_mqh = shm_mq_attach(mq, seg, NULL);
935 
936  pq_redirect_to_shm_mq(seg, error_mqh);
939 
942 
944 
945  InitializingApplyWorker = false;
946 
947  /* Setup replication origin tracking. */
950  originname, sizeof(originname));
951  originid = replorigin_by_name(originname, false);
952 
953  /*
954  * The parallel apply worker doesn't need to monopolize this replication
955  * origin which was already acquired by its leader process.
956  */
958  replorigin_session_origin = originid;
960 
961  /*
962  * Setup callback for syscache so that we know when something changes in
963  * the subscription relation state.
964  */
967  (Datum) 0);
968 
969  set_apply_error_context_origin(originname);
970 
972 
973  /*
974  * The parallel apply worker must not get here because the parallel apply
975  * worker will only stop when it receives a SIGTERM or SIGINT from the
976  * leader, or when there is an error. None of these cases will allow the
977  * code to reach here.
978  */
979  Assert(false);
980 }
static void pa_shutdown(int code, Datum arg)
#define PARALLEL_APPLY_KEY_SHARED
ParallelApplyWorkerShared * MyParallelShared
static void LogicalParallelApplyLoop(shm_mq_handle *mqh)
#define PARALLEL_APPLY_KEY_ERROR_QUEUE
#define PARALLEL_APPLY_KEY_MQ
#define PG_LOGICAL_APPLY_SHM_MAGIC
void ReplicationOriginNameForLogicalRep(Oid suboid, Oid relid, char *originname, Size szoriginname)
Definition: worker.c:446
void set_apply_error_context_origin(char *originname)
Definition: worker.c:5053
void InitializeLogRepWorker(void)
Definition: worker.c:4579
Subscription * MySubscription
Definition: worker.c:316
#define InvalidBackendId
Definition: backendid.h:23
void * dsm_segment_address(dsm_segment *seg)
Definition: dsm.c:1096
dsm_segment * dsm_attach(dsm_handle h)
Definition: dsm.c:666
uint32 dsm_handle
Definition: dsm_impl.h:55
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:109
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:61
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1518
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:337
void logicalrep_worker_attach(int slot)
Definition: launcher.c:713
Assert(fmt[strlen(fmt) - 1] !='\n')
RepOriginId replorigin_by_name(const char *roname, bool missing_ok)
Definition: origin.c:221
RepOriginId replorigin_session_origin
Definition: origin.c:156
void replorigin_session_setup(RepOriginId node, int acquired_by)
Definition: origin.c:1095
#define NAMEDATALEN
#define die(msg)
pqsigfunc pqsignal(int signo, pqsigfunc func)
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
#define InvalidOid
Definition: postgres_ext.h:36
void BackgroundWorkerUnblockSignals(void)
Definition: postmaster.c:5636
BackgroundWorker * MyBgworkerEntry
Definition: postmaster.c:194
void pq_set_parallel_leader(pid_t pid, BackendId backend_id)
Definition: pqmq.c:78
void pq_redirect_to_shm_mq(dsm_segment *seg, shm_mq_handle *mqh)
Definition: pqmq.c:53
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition: shm_mq.c:291
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:225
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition: shm_mq.c:207
shm_toc * shm_toc_attach(uint64 magic, void *address)
Definition: shm_toc.c:64
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition: shm_toc.c:232
#define SpinLockRelease(lock)
Definition: spin.h:64
#define SpinLockAcquire(lock)
Definition: spin.h:62
PGPROC * MyProc
Definition: proc.c:66
char bgw_extra[BGW_EXTRALEN]
Definition: bgworker.h:99
TimestampTz last_recv_time
TimestampTz reply_time
TimestampTz last_send_time
Definition: shm_mq.c:73
@ SUBSCRIPTIONRELMAP
Definition: syscache.h:100
void invalidate_syncing_table_states(Datum arg, int cacheid, uint32 hashvalue)
Definition: tablesync.c:274
#define SIGHUP
Definition: win32_port.h:168
void StartTransactionCommand(void)
Definition: xact.c:2937
void CommitTransactionCommand(void)
Definition: xact.c:3034
uint16 RepOriginId
Definition: xlogdefs.h:65

References Assert(), BackgroundWorkerUnblockSignals(), before_shmem_exit(), BackgroundWorker::bgw_extra, CacheRegisterSyscacheCallback(), CommitTransactionCommand(), DatumGetInt32(), die, dsm_attach(), dsm_segment_address(), ereport, errcode(), errmsg(), ERROR, LogicalRepWorker::generation, InitializeLogRepWorker(), InitializingApplyWorker, invalidate_syncing_table_states(), InvalidBackendId, InvalidOid, LogicalRepWorker::last_recv_time, LogicalRepWorker::last_send_time, LogicalRepWorker::leader_pid, LogicalParallelApplyLoop(), logicalrep_worker_attach(), ParallelApplyWorkerShared::logicalrep_worker_generation, ParallelApplyWorkerShared::logicalrep_worker_slot_no, ParallelApplyWorkerShared::mutex, MyBgworkerEntry, MyLogicalRepWorker, MyParallelShared, MyProc, MySubscription, NAMEDATALEN, Subscription::oid, pa_shutdown(), PARALLEL_APPLY_KEY_ERROR_QUEUE, PARALLEL_APPLY_KEY_MQ, PARALLEL_APPLY_KEY_SHARED, PG_LOGICAL_APPLY_SHM_MAGIC, PointerGetDatum(), pq_redirect_to_shm_mq(), pq_set_parallel_leader(), pqsignal(), ReplicationOriginNameForLogicalRep(), replorigin_by_name(), replorigin_session_origin, replorigin_session_setup(), LogicalRepWorker::reply_time, set_apply_error_context_origin(), shm_mq_attach(), shm_mq_set_receiver(), shm_mq_set_sender(), shm_toc_attach(), shm_toc_lookup(), SIGHUP, SignalHandlerForConfigReload(), SignalHandlerForShutdownRequest(), SpinLockAcquire, SpinLockRelease, StartTransactionCommand(), and SUBSCRIPTIONRELMAP.

◆ TablesyncWorkerMain()

void TablesyncWorkerMain ( Datum  main_arg)

Definition at line 1683 of file tablesync.c.

1684 {
1685  int worker_slot = DatumGetInt32(main_arg);
1686 
1687  SetupApplyOrSyncWorker(worker_slot);
1688 
1690 
1691  finish_sync_worker();
1692 }
static void run_tablesync_worker()
Definition: tablesync.c:1657

References DatumGetInt32(), run_tablesync_worker(), and SetupApplyOrSyncWorker().

Variable Documentation

◆ ParallelApplyMessagePending

PGDLLIMPORT volatile sig_atomic_t ParallelApplyMessagePending
extern