PostgreSQL Source Code git master
Loading...
Searching...
No Matches
worker_internal.h
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * worker_internal.h
4 * Internal headers shared by logical replication workers.
5 *
6 * Portions Copyright (c) 2016-2026, PostgreSQL Global Development Group
7 *
8 * src/include/replication/worker_internal.h
9 *
10 *-------------------------------------------------------------------------
11 */
12#ifndef WORKER_INTERNAL_H
13#define WORKER_INTERNAL_H
14
15#include "access/xlogdefs.h"
17#include "datatype/timestamp.h"
18#include "miscadmin.h"
21#include "storage/buffile.h"
22#include "storage/fileset.h"
23#include "storage/shm_mq.h"
24#include "storage/shm_toc.h"
25#include "storage/spin.h"
26
27/* Different types of worker */
36
37typedef struct LogicalRepWorker
38{
39 /* What type of worker is this? */
41
42 /* Time at which this worker was launched. */
44
45 /* Indicates if this slot is used or free. */
46 bool in_use;
47
48 /* Increased every time the slot is taken by new worker. */
50
51 /* Pointer to proc array. NULL if not running. */
53
54 /* Database id to connect to. */
56
57 /* User to use for connection (will be same as owner of subscription). */
59
60 /* Subscription id for the worker. */
62
63 /* Used for initial table synchronization. */
68
69 /*
70 * Used to create the changes and subxact files for the streaming
71 * transactions. Upon the arrival of the first streaming transaction or
72 * when the first-time leader apply worker times out while sending changes
73 * to the parallel apply worker, the fileset will be initialized, and it
74 * will be deleted when the worker exits. Under this, separate buffiles
75 * would be created for each transaction which will be deleted after the
76 * transaction is finished.
77 */
79
80 /*
81 * PID of leader apply worker if this slot is used for a parallel apply
82 * worker, InvalidPid otherwise.
83 */
85
86 /* Indicates whether apply can be performed in parallel. */
88
89 /*
90 * Changes made by this transaction and subsequent ones must be preserved.
91 * This ensures that update_deleted conflicts can be accurately detected
92 * during the apply phase of logical replication by this worker.
93 *
94 * The logical replication launcher manages an internal replication slot
95 * named "pg_conflict_detection". It asynchronously collects this ID to
96 * decide when to advance the xmin value of the slot.
97 *
98 * This ID is set to InvalidTransactionId when the apply worker stops
99 * retaining information needed for conflict detection.
100 */
102
103 /* Stats. */
109
112
113/*
114 * State of the transaction in parallel apply worker.
115 *
116 * The enum values must have the same order as the transaction state
117 * transitions.
118 */
125
126/*
127 * State of fileset used to communicate changes from leader to parallel
128 * apply worker.
129 *
130 * FS_EMPTY indicates an initial state where the leader doesn't need to use
131 * the file to communicate with the parallel apply worker.
132 *
133 * FS_SERIALIZE_IN_PROGRESS indicates that the leader is serializing changes
134 * to the file.
135 *
136 * FS_SERIALIZE_DONE indicates that the leader has serialized all changes to
137 * the file.
138 *
139 * FS_READY indicates that it is now ok for a parallel apply worker to
140 * read the file.
141 */
149
150/*
151 * Struct for sharing information between leader apply worker and parallel
152 * apply workers.
153 */
155{
157
159
160 /*
161 * State used to ensure commit ordering.
162 *
163 * The parallel apply worker will set it to PARALLEL_TRANS_FINISHED after
164 * handling the transaction finish commands while the apply leader will
165 * wait for it to become PARALLEL_TRANS_FINISHED before proceeding in
166 * transaction finish commands (e.g. STREAM_COMMIT/STREAM_PREPARE/
167 * STREAM_ABORT).
168 */
170
171 /* Information from the corresponding LogicalRepWorker slot. */
174
175 /*
176 * Indicates whether there are pending streaming blocks in the queue. The
177 * parallel apply worker will check it before starting to wait.
178 */
180
181 /*
182 * XactLastCommitEnd from the parallel apply worker. This is required by
183 * the leader worker so it can update the lsn_mappings.
184 */
186
187 /*
188 * After entering PARTIAL_SERIALIZE mode, the leader apply worker will
189 * serialize changes to the file, and share the fileset with the parallel
190 * apply worker when processing the transaction finish command. Then the
191 * parallel apply worker will apply all the spooled messages.
192 *
193 * FileSet is used here instead of SharedFileSet because we need it to
194 * survive after releasing the shared memory so that the leader apply
195 * worker can re-use the same fileset for the next streaming transaction.
196 */
200
201/*
202 * Information which is used to manage the parallel apply worker.
203 */
205{
206 /*
207 * This queue is used to send changes from the leader apply worker to the
208 * parallel apply worker.
209 */
211
212 /*
213 * This queue is used to transfer error messages from the parallel apply
214 * worker to the leader apply worker.
215 */
217
219
220 /*
221 * Indicates whether the leader apply worker needs to serialize the
222 * remaining changes to a file due to timeout when attempting to send data
223 * to the parallel apply worker via shared memory.
224 */
226
227 /*
228 * True if the worker is being used to process a parallel apply
229 * transaction. False indicates this worker is available for re-use.
230 */
231 bool in_use;
232
235
236/* Main memory context for apply worker. Permanent during worker lifetime. */
238
240
242
244
245/* libpqreceiver connection */
247
248/* Worker and subscription objects. */
251
253
255
257
258extern void logicalrep_worker_attach(int slot);
260 Oid subid, Oid relid,
261 bool only_running);
262extern List *logicalrep_workers_find(Oid subid, bool only_running,
263 bool acquire_lock);
265 Oid dbid, Oid subid, const char *subname,
266 Oid userid, Oid relid,
268 bool retain_dead_tuples);
270 Oid relid);
273 Oid relid);
275
277extern int logicalrep_sync_worker_count(Oid subid);
278
281
282extern bool AllTablesyncsReady(void);
283extern bool HasSubscriptionTablesCached(void);
284extern void UpdateTwoPhaseState(Oid suboid, char new_state);
285
288extern void ProcessSequencesForSync(void);
289
290pg_noreturn extern void FinishSyncWorker(void);
292 uint32 hashvalue);
294 Oid relid, TimestampTz *last_start_time);
298
300extern void stream_stop_internal(TransactionId xid);
301
302/* Common streaming function to apply all the spooled messages */
303extern void apply_spooled_messages(FileSet *stream_fileset, TransactionId xid,
304 XLogRecPtr lsn);
305
306extern void apply_dispatch(StringInfo s);
307
308extern void maybe_reread_subscription(void);
309
310extern void stream_cleanup_files(Oid subid, TransactionId xid);
311
313 char *slotname,
315
317
318extern void InitializeLogRepWorker(void);
319
320extern void SetupApplyOrSyncWorker(int worker_slot);
321
322extern void DisableSubscriptionAndExit(void);
323
324extern void store_flush_position(XLogRecPtr remote_lsn, XLogRecPtr local_lsn);
325
326/* Function for apply error callback */
327extern void apply_error_callback(void *arg);
329
330/* Parallel apply worker setup and interactions */
331extern void pa_allocate_worker(TransactionId xid);
333extern void pa_detach_all_error_mq(void);
334
335extern bool pa_send_data(ParallelApplyWorkerInfo *winfo, Size nbytes,
336 const void *data);
338 bool stream_locked);
339
341 ParallelTransState xact_state);
343
345 TransactionId top_xid);
346extern void pa_reset_subtrans(void);
349 PartialFileSetState fileset_state);
350
351extern void pa_lock_stream(TransactionId xid, LOCKMODE lockmode);
352extern void pa_unlock_stream(TransactionId xid, LOCKMODE lockmode);
353
354extern void pa_lock_transaction(TransactionId xid, LOCKMODE lockmode);
355extern void pa_unlock_transaction(TransactionId xid, LOCKMODE lockmode);
356
357extern void pa_decr_and_wait_stream_block(void);
358
359extern void pa_xact_finish(ParallelApplyWorkerInfo *winfo,
360 XLogRecPtr remote_lsn);
361
362#define isParallelApplyWorker(worker) ((worker)->in_use && \
363 (worker)->type == WORKERTYPE_PARALLEL_APPLY)
364#define isTableSyncWorker(worker) ((worker)->in_use && \
365 (worker)->type == WORKERTYPE_TABLESYNC)
366#define isSequenceSyncWorker(worker) ((worker)->in_use && \
367 (worker)->type == WORKERTYPE_SEQUENCESYNC)
368
369static inline bool
374
375static inline bool
380
381static inline bool
387
388static inline bool
394
395static inline LogicalRepWorkerType
401
402#endif /* WORKER_INTERNAL_H */
#define PGDLLIMPORT
Definition c.h:1421
#define pg_noreturn
Definition c.h:190
#define Assert(condition)
Definition c.h:943
uint16_t uint16
Definition c.h:623
uint32_t uint32
Definition c.h:624
uint32 TransactionId
Definition c.h:736
size_t Size
Definition c.h:689
int64 TimestampTz
Definition timestamp.h:39
uint32 dsm_handle
Definition dsm_impl.h:55
Datum arg
Definition elog.c:1322
int LOCKMODE
Definition lockdefs.h:26
const void * data
NameData subname
uint64_t Datum
Definition postgres.h:70
unsigned int Oid
static int fb(int x)
Definition pg_list.h:54
XLogRecPtr relstate_lsn
TimestampTz last_recv_time
TimestampTz last_seqsync_start_time
LogicalRepWorkerType type
TimestampTz launch_time
TimestampTz reply_time
FileSet * stream_fileset
TransactionId oldest_nonremovable_xid
TimestampTz last_send_time
Definition proc.h:179
shm_mq_handle * error_mq_handle
ParallelApplyWorkerShared * shared
pg_atomic_uint32 pending_stream_count
PartialFileSetState fileset_state
ParallelTransState xact_state
bool AllTablesyncsReady(void)
Definition tablesync.c:1629
ParallelTransState
@ PARALLEL_TRANS_UNKNOWN
@ PARALLEL_TRANS_STARTED
@ PARALLEL_TRANS_FINISHED
#define isParallelApplyWorker(worker)
void set_stream_options(WalRcvStreamOptions *options, char *slotname, XLogRecPtr *origin_startpos)
Definition worker.c:5557
List * logicalrep_workers_find(Oid subid, bool only_running, bool acquire_lock)
Definition launcher.c:303
void pa_set_xact_state(ParallelApplyWorkerShared *wshared, ParallelTransState xact_state)
void stream_cleanup_files(Oid subid, TransactionId xid)
Definition worker.c:5424
void pa_unlock_stream(TransactionId xid, LOCKMODE lockmode)
PGDLLIMPORT ErrorContextCallback * apply_error_context_stack
Definition worker.c:474
PGDLLIMPORT bool in_remote_transaction
Definition worker.c:489
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition launcher.c:756
PGDLLIMPORT MemoryContext ApplyMessageContext
Definition worker.c:476
bool logicalrep_worker_launch(LogicalRepWorkerType wtype, Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid, dsm_handle subworker_dsm, bool retain_dead_tuples)
Definition launcher.c:334
static bool am_parallel_apply_worker(void)
void launch_sync_worker(LogicalRepWorkerType wtype, int nsyncworkers, Oid relid, TimestampTz *last_start_time)
Definition syncutils.c:118
void ProcessSyncingRelations(XLogRecPtr current_lsn)
Definition syncutils.c:156
PGDLLIMPORT LogicalRepWorker * MyLogicalRepWorker
Definition launcher.c:58
void logicalrep_worker_attach(int slot)
Definition launcher.c:767
void stream_stop_internal(TransactionId xid)
Definition worker.c:1869
LogicalRepWorker * logicalrep_worker_find(LogicalRepWorkerType wtype, Oid subid, Oid relid, bool only_running)
Definition launcher.c:268
void ProcessSyncingTablesForSync(XLogRecPtr current_lsn)
Definition tablesync.c:246
void start_apply(XLogRecPtr origin_startpos)
Definition worker.c:5626
void logicalrep_worker_wakeup(LogicalRepWorkerType wtype, Oid subid, Oid relid)
Definition launcher.c:733
void pa_stream_abort(LogicalRepStreamAbortData *abort_data)
bool HasSubscriptionTablesCached(void)
Definition tablesync.c:1659
pg_noreturn void FinishSyncWorker(void)
Definition syncutils.c:50
void pa_lock_stream(TransactionId xid, LOCKMODE lockmode)
void apply_dispatch(StringInfo s)
Definition worker.c:3782
void pa_set_fileset_state(ParallelApplyWorkerShared *wshared, PartialFileSetState fileset_state)
void pa_reset_subtrans(void)
void logicalrep_pa_worker_stop(ParallelApplyWorkerInfo *winfo)
Definition launcher.c:689
void DisableSubscriptionAndExit(void)
Definition worker.c:6007
void pa_lock_transaction(TransactionId xid, LOCKMODE lockmode)
void ReplicationOriginNameForLogicalRep(Oid suboid, Oid relid, char *originname, Size szoriginname)
Definition worker.c:648
#define isSequenceSyncWorker(worker)
void pa_detach_all_error_mq(void)
void logicalrep_reset_seqsync_start_time(void)
Definition launcher.c:882
PGDLLIMPORT struct WalReceiverConn * LogRepWorkerWalRcvConn
Definition worker.c:482
PGDLLIMPORT bool InitializingApplyWorker
Definition worker.c:504
void FetchRelationStates(bool *has_pending_subtables, bool *has_pending_subsequences, bool *started_tx)
Definition syncutils.c:203
void stream_start_internal(TransactionId xid, bool first_segment)
Definition worker.c:1694
void pa_start_subtrans(TransactionId current_xid, TransactionId top_xid)
void pa_switch_to_partial_serialize(ParallelApplyWorkerInfo *winfo, bool stream_locked)
void set_apply_error_context_origin(char *originname)
Definition worker.c:6364
LogicalRepWorkerType
@ WORKERTYPE_TABLESYNC
@ WORKERTYPE_UNKNOWN
@ WORKERTYPE_SEQUENCESYNC
@ WORKERTYPE_PARALLEL_APPLY
@ WORKERTYPE_APPLY
void pa_xact_finish(ParallelApplyWorkerInfo *winfo, XLogRecPtr remote_lsn)
PartialFileSetState
@ FS_EMPTY
@ FS_SERIALIZE_DONE
@ FS_READY
@ FS_SERIALIZE_IN_PROGRESS
void logicalrep_worker_stop(LogicalRepWorkerType wtype, Oid subid, Oid relid)
Definition launcher.c:662
static bool am_sequencesync_worker(void)
PGDLLIMPORT List * table_states_not_ready
Definition tablesync.c:127
static LogicalRepWorkerType get_logical_worker_type(void)
void ProcessSyncingTablesForApply(XLogRecPtr current_lsn)
Definition tablesync.c:368
void SetupApplyOrSyncWorker(int worker_slot)
Definition worker.c:5947
static bool am_tablesync_worker(void)
static bool am_leader_apply_worker(void)
#define isTableSyncWorker(worker)
PGDLLIMPORT Subscription * MySubscription
Definition worker.c:484
void apply_error_callback(void *arg)
Definition worker.c:6222
void store_flush_position(XLogRecPtr remote_lsn, XLogRecPtr local_lsn)
Definition worker.c:3946
int logicalrep_sync_worker_count(Oid subid)
Definition launcher.c:937
void maybe_reread_subscription(void)
Definition worker.c:5045
void InitializeLogRepWorker(void)
Definition worker.c:5779
bool pa_send_data(ParallelApplyWorkerInfo *winfo, Size nbytes, const void *data)
void pa_allocate_worker(TransactionId xid)
PGDLLIMPORT ParallelApplyWorkerShared * MyParallelShared
void apply_spooled_messages(FileSet *stream_fileset, TransactionId xid, XLogRecPtr lsn)
Definition worker.c:2267
void UpdateTwoPhaseState(Oid suboid, char new_state)
Definition tablesync.c:1680
void InvalidateSyncingRelStates(Datum arg, SysCacheIdentifier cacheid, uint32 hashvalue)
Definition syncutils.c:101
void pa_set_stream_apply_worker(ParallelApplyWorkerInfo *winfo)
void ProcessSequencesForSync(void)
ParallelApplyWorkerInfo * pa_find_worker(TransactionId xid)
void pa_unlock_transaction(TransactionId xid, LOCKMODE lockmode)
void pa_decr_and_wait_stream_block(void)
PGDLLIMPORT MemoryContext ApplyContext
Definition worker.c:477
uint64 XLogRecPtr
Definition xlogdefs.h:21