PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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)
 
void SequenceSyncWorkerMain (Datum main_arg)
 
bool IsLogicalWorker (void)
 
bool IsLogicalParallelApplyWorker (void)
 
void HandleParallelApplyMessageInterrupt (void)
 
void ProcessParallelApplyMessages (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)
extern

Definition at line 5927 of file worker.c.

5928{
5930
5932
5934
5936
5938
5939 proc_exit(0);
5940}
static void run_apply_worker(void)
Definition worker.c:5624
bool InitializingApplyWorker
Definition worker.c:499
void SetupApplyOrSyncWorker(int worker_slot)
Definition worker.c:5886
void proc_exit(int code)
Definition ipc.c:105
static int32 DatumGetInt32(Datum X)
Definition postgres.h:212
static int fb(int x)

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

◆ AtEOXact_LogicalRepWorkers()

void AtEOXact_LogicalRepWorkers ( bool  isCommit)
extern

Definition at line 6273 of file worker.c.

6274{
6276 {
6277 ListCell *lc;
6278
6281 {
6282 Oid subid = lfirst_oid(lc);
6283 List *workers;
6284 ListCell *lc2;
6285
6286 workers = logicalrep_workers_find(subid, true, false);
6287 foreach(lc2, workers)
6288 {
6290
6292 }
6293 }
6295 }
6296
6297 /* The List storage will be reclaimed automatically in xact cleanup. */
6299}
static List * on_commit_wakeup_workers_subids
Definition worker.c:482
List * logicalrep_workers_find(Oid subid, bool only_running, bool acquire_lock)
Definition launcher.c:293
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition launcher.c:746
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1176
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1793
@ LW_SHARED
Definition lwlock.h:113
#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 pg_list.h:54

References fb(), 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  )
extern

Definition at line 997 of file applyparallelworker.c.

998{
999 InterruptPending = true;
1002}
volatile sig_atomic_t ParallelApplyMessagePending
volatile sig_atomic_t InterruptPending
Definition globals.c:32
struct Latch * MyLatch
Definition globals.c:63
void SetLatch(Latch *latch)
Definition latch.c:290

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

Referenced by procsignal_sigusr1_handler().

◆ IsLogicalParallelApplyWorker()

bool IsLogicalParallelApplyWorker ( void  )
extern

Definition at line 6015 of file worker.c.

6016{
6018}
bool IsLogicalWorker(void)
Definition worker.c:6006
static bool am_parallel_apply_worker(void)

References am_parallel_apply_worker(), and IsLogicalWorker().

Referenced by mq_putmessage().

◆ IsLogicalWorker()

bool IsLogicalWorker ( void  )
extern

Definition at line 6006 of file worker.c.

6007{
6008 return MyLogicalRepWorker != NULL;
6009}
LogicalRepWorker * MyLogicalRepWorker
Definition launcher.c:56

References fb(), and MyLogicalRepWorker.

Referenced by IsLogicalParallelApplyWorker(), and ProcessInterrupts().

◆ LogicalRepWorkersWakeupAtCommit()

void LogicalRepWorkersWakeupAtCommit ( Oid  subid)
extern

Definition at line 6259 of file worker.c.

6260{
6262
6267}
List * list_append_unique_oid(List *list, Oid datum)
Definition list.c:1380
MemoryContext TopTransactionContext
Definition mcxt.c:171
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124

References fb(), 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)
extern

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;
866 shm_mq_handle *error_mqh;
870
872
873 /*
874 * Setup signal handling.
875 *
876 * Note: We intentionally used SIGUSR2 to trigger a graceful shutdown
877 * initiated by the leader apply worker. This helps to differentiate it
878 * from the case where we abort the current transaction and exit on
879 * receiving SIGTERM.
880 */
885
886 /*
887 * Attach to the dynamic shared memory segment for the parallel apply, and
888 * find its table of contents.
889 *
890 * Like parallel query, we don't need resource owner by this time. See
891 * ParallelWorkerMain.
892 */
893 memcpy(&handle, MyBgworkerEntry->bgw_extra, sizeof(dsm_handle));
894 seg = dsm_attach(handle);
895 if (!seg)
898 errmsg("could not map dynamic shared memory segment")));
899
901 if (!toc)
904 errmsg("invalid magic number in dynamic shared memory segment")));
905
906 /* Look up the shared information. */
907 shared = shm_toc_lookup(toc, PARALLEL_APPLY_KEY_SHARED, false);
908 MyParallelShared = shared;
909
910 /*
911 * Attach to the message queue.
912 */
915 mqh = shm_mq_attach(mq, seg, NULL);
916
917 /*
918 * Primary initialization is complete. Now, we can attach to our slot.
919 * This is to ensure that the leader apply worker does not write data to
920 * the uninitialized memory queue.
921 */
923
924 /*
925 * Register the shutdown callback after we are attached to the worker
926 * slot. This is to ensure that MyLogicalRepWorker remains valid when this
927 * callback is invoked.
928 */
930
935
936 /*
937 * Attach to the error queue.
938 */
941 error_mqh = shm_mq_attach(mq, seg, NULL);
942
943 pq_redirect_to_shm_mq(seg, error_mqh);
946
949
951
953
954 /* Setup replication origin tracking. */
957 originname, sizeof(originname));
959
960 /*
961 * The parallel apply worker doesn't need to monopolize this replication
962 * origin which was already acquired by its leader process.
963 */
967
968 /*
969 * Setup callback for syscache so that we know when something changes in
970 * the subscription relation state.
971 */
974 (Datum) 0);
975
977
979
980 /*
981 * The parallel apply worker must not get here because the parallel apply
982 * worker will only stop when it receives a SIGTERM or SIGUSR2 from the
983 * leader, or SIGINT from itself, or when there is an error. None of these
984 * cases will allow the code to reach here.
985 */
986 Assert(false);
987}
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:641
void set_apply_error_context_origin(char *originname)
Definition worker.c:6305
void InitializeLogRepWorker(void)
Definition worker.c:5737
Subscription * MySubscription
Definition worker.c:479
void BackgroundWorkerUnblockSignals(void)
Definition bgworker.c:942
#define Assert(condition)
Definition c.h:873
void * dsm_segment_address(dsm_segment *seg)
Definition dsm.c:1095
dsm_segment * dsm_attach(dsm_handle h)
Definition dsm.c:665
uint32 dsm_handle
Definition dsm_impl.h:55
int errcode(int sqlerrcode)
Definition elog.c:863
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:150
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition interrupt.c:104
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition interrupt.c:61
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition inval.c:1816
void before_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:344
void logicalrep_worker_attach(int slot)
Definition launcher.c:757
RepOriginId replorigin_by_name(const char *roname, bool missing_ok)
Definition origin.c:229
RepOriginId replorigin_session_origin
Definition origin.c:166
void replorigin_session_setup(RepOriginId node, int acquired_by)
Definition origin.c:1144
#define NAMEDATALEN
#define die(msg)
#define pqsignal
Definition port.h:547
static Datum PointerGetDatum(const void *X)
Definition postgres.h:352
uint64_t Datum
Definition postgres.h:70
#define InvalidOid
BackgroundWorker * MyBgworkerEntry
Definition postmaster.c:200
void pq_set_parallel_leader(pid_t pid, ProcNumber procNumber)
Definition pqmq.c:82
void pq_redirect_to_shm_mq(dsm_segment *seg, shm_mq_handle *mqh)
Definition pqmq.c:53
#define INVALID_PROC_NUMBER
Definition procnumber.h:26
void shm_mq_set_sender(shm_mq *mq, PGPROC *proc)
Definition shm_mq.c:224
void shm_mq_set_receiver(shm_mq *mq, PGPROC *proc)
Definition shm_mq.c:206
shm_mq_handle * shm_mq_attach(shm_mq *mq, dsm_segment *seg, BackgroundWorkerHandle *handle)
Definition shm_mq.c:290
void * shm_toc_lookup(shm_toc *toc, uint64 key, bool noError)
Definition shm_toc.c:232
shm_toc * shm_toc_attach(uint64 magic, void *address)
Definition shm_toc.c:64
#define SpinLockRelease(lock)
Definition spin.h:61
#define SpinLockAcquire(lock)
Definition spin.h:59
PGPROC * MyProc
Definition proc.c:67
char bgw_extra[BGW_EXTRALEN]
Definition bgworker.h:106
TimestampTz last_recv_time
TimestampTz reply_time
TimestampTz last_send_time
void InvalidateSyncingRelStates(Datum arg, int cacheid, uint32 hashvalue)
Definition syncutils.c:101
#define SIGHUP
Definition win32_port.h:158
#define SIGUSR2
Definition win32_port.h:171
void StartTransactionCommand(void)
Definition xact.c:3080
void CommitTransactionCommand(void)
Definition xact.c:3178
uint16 RepOriginId
Definition xlogdefs.h:69

References Assert, BackgroundWorkerUnblockSignals(), before_shmem_exit(), BackgroundWorker::bgw_extra, CacheRegisterSyscacheCallback(), CommitTransactionCommand(), DatumGetInt32(), die, dsm_attach(), dsm_segment_address(), ereport, errcode(), errmsg(), ERROR, fb(), LogicalRepWorker::generation, InitializeLogRepWorker(), InitializingApplyWorker, INVALID_PROC_NUMBER, InvalidateSyncingRelStates(), 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(), SIGUSR2, SpinLockAcquire, SpinLockRelease, and StartTransactionCommand().

◆ ProcessParallelApplyMessages()

void ProcessParallelApplyMessages ( void  )
extern

Definition at line 1071 of file applyparallelworker.c.

1072{
1073 ListCell *lc;
1074 MemoryContext oldcontext;
1075
1077
1078 /*
1079 * This is invoked from ProcessInterrupts(), and since some of the
1080 * functions it calls contain CHECK_FOR_INTERRUPTS(), there is a potential
1081 * for recursive calls if more signals are received while this runs. It's
1082 * unclear that recursive entry would be safe, and it doesn't seem useful
1083 * even if it is safe, so let's block interrupts until done.
1084 */
1086
1087 /*
1088 * Moreover, CurrentMemoryContext might be pointing almost anywhere. We
1089 * don't want to risk leaking data into long-lived contexts, so let's do
1090 * our work here in a private context that we can reset on each use.
1091 */
1092 if (!hpam_context) /* first time through? */
1094 "ProcessParallelApplyMessages",
1096 else
1098
1099 oldcontext = MemoryContextSwitchTo(hpam_context);
1100
1102
1103 foreach(lc, ParallelApplyWorkerPool)
1104 {
1105 shm_mq_result res;
1106 Size nbytes;
1107 void *data;
1109
1110 /*
1111 * The leader will detach from the error queue and set it to NULL
1112 * before preparing to stop all parallel apply workers, so we don't
1113 * need to handle error messages anymore. See
1114 * logicalrep_worker_detach.
1115 */
1116 if (!winfo->error_mq_handle)
1117 continue;
1118
1119 res = shm_mq_receive(winfo->error_mq_handle, &nbytes, &data, true);
1120
1121 if (res == SHM_MQ_WOULD_BLOCK)
1122 continue;
1123 else if (res == SHM_MQ_SUCCESS)
1124 {
1125 StringInfoData msg;
1126
1127 initStringInfo(&msg);
1128 appendBinaryStringInfo(&msg, data, nbytes);
1130 pfree(msg.data);
1131 }
1132 else
1133 ereport(ERROR,
1135 errmsg("lost connection to the logical replication parallel apply worker")));
1136 }
1137
1138 MemoryContextSwitchTo(oldcontext);
1139
1140 /* Might as well clear the context on our way out */
1142
1144}
static List * ParallelApplyWorkerPool
static void ProcessParallelApplyMessage(StringInfo msg)
size_t Size
Definition c.h:619
void MemoryContextReset(MemoryContext context)
Definition mcxt.c:403
void pfree(void *pointer)
Definition mcxt.c:1616
MemoryContext TopMemoryContext
Definition mcxt.c:166
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
#define RESUME_INTERRUPTS()
Definition miscadmin.h:136
#define HOLD_INTERRUPTS()
Definition miscadmin.h:134
const void * data
shm_mq_result shm_mq_receive(shm_mq_handle *mqh, Size *nbytesp, void **datap, bool nowait)
Definition shm_mq.c:572
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:281
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
shm_mq_handle * error_mq_handle

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

Referenced by ProcessInterrupts().

◆ SequenceSyncWorkerMain()

void SequenceSyncWorkerMain ( Datum  main_arg)
extern

Definition at line 764 of file sequencesync.c.

765{
767
769
771
773}
static void start_sequence_sync(void)
pg_noreturn void FinishSyncWorker(void)
Definition syncutils.c:50

References DatumGetInt32(), fb(), FinishSyncWorker(), SetupApplyOrSyncWorker(), and start_sequence_sync().

◆ TableSyncWorkerMain()

void TableSyncWorkerMain ( Datum  main_arg)
extern

Definition at line 1578 of file tablesync.c.

1579{
1581
1583
1585
1587}
static void run_tablesync_worker(void)
Definition tablesync.c:1552

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

Variable Documentation

◆ ParallelApplyMessagePending