PostgreSQL Source Code  git master
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-2023, 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 <signal.h>
16 
17 #include "access/xlogdefs.h"
19 #include "datatype/timestamp.h"
20 #include "miscadmin.h"
22 #include "storage/buffile.h"
23 #include "storage/fileset.h"
24 #include "storage/lock.h"
25 #include "storage/shm_mq.h"
26 #include "storage/shm_toc.h"
27 #include "storage/spin.h"
28 
29 
30 typedef struct LogicalRepWorker
31 {
32  /* Time at which this worker was launched. */
34 
35  /* Indicates if this slot is used or free. */
36  bool in_use;
37 
38  /* Increased every time the slot is taken by new worker. */
40 
41  /* Pointer to proc array. NULL if not running. */
43 
44  /* Database id to connect to. */
46 
47  /* User to use for connection (will be same as owner of subscription). */
49 
50  /* Subscription id for the worker. */
52 
53  /* Used for initial table synchronization. */
55  char relstate;
58 
59  /*
60  * Used to create the changes and subxact files for the streaming
61  * transactions. Upon the arrival of the first streaming transaction or
62  * when the first-time leader apply worker times out while sending changes
63  * to the parallel apply worker, the fileset will be initialized, and it
64  * will be deleted when the worker exits. Under this, separate buffiles
65  * would be created for each transaction which will be deleted after the
66  * transaction is finished.
67  */
69 
70  /*
71  * PID of leader apply worker if this slot is used for a parallel apply
72  * worker, InvalidPid otherwise.
73  */
74  pid_t leader_pid;
75 
76  /* Indicates whether apply can be performed in parallel. */
78 
79  /* Stats. */
86 
87 /*
88  * State of the transaction in parallel apply worker.
89  *
90  * The enum values must have the same order as the transaction state
91  * transitions.
92  */
93 typedef enum ParallelTransState
94 {
99 
100 /*
101  * State of fileset used to communicate changes from leader to parallel
102  * apply worker.
103  *
104  * FS_EMPTY indicates an initial state where the leader doesn't need to use
105  * the file to communicate with the parallel apply worker.
106  *
107  * FS_SERIALIZE_IN_PROGRESS indicates that the leader is serializing changes
108  * to the file.
109  *
110  * FS_SERIALIZE_DONE indicates that the leader has serialized all changes to
111  * the file.
112  *
113  * FS_READY indicates that it is now ok for a parallel apply worker to
114  * read the file.
115  */
117 {
121  FS_READY
123 
124 /*
125  * Struct for sharing information between leader apply worker and parallel
126  * apply workers.
127  */
129 {
131 
133 
134  /*
135  * State used to ensure commit ordering.
136  *
137  * The parallel apply worker will set it to PARALLEL_TRANS_FINISHED after
138  * handling the transaction finish commands while the apply leader will
139  * wait for it to become PARALLEL_TRANS_FINISHED before proceeding in
140  * transaction finish commands (e.g. STREAM_COMMIT/STREAM_PREPARE/
141  * STREAM_ABORT).
142  */
144 
145  /* Information from the corresponding LogicalRepWorker slot. */
148 
149  /*
150  * Indicates whether there are pending streaming blocks in the queue. The
151  * parallel apply worker will check it before starting to wait.
152  */
154 
155  /*
156  * XactLastCommitEnd from the parallel apply worker. This is required by
157  * the leader worker so it can update the lsn_mappings.
158  */
160 
161  /*
162  * After entering PARTIAL_SERIALIZE mode, the leader apply worker will
163  * serialize changes to the file, and share the fileset with the parallel
164  * apply worker when processing the transaction finish command. Then the
165  * parallel apply worker will apply all the spooled messages.
166  *
167  * FileSet is used here instead of SharedFileSet because we need it to
168  * survive after releasing the shared memory so that the leader apply
169  * worker can re-use the same fileset for the next streaming transaction.
170  */
174 
175 /*
176  * Information which is used to manage the parallel apply worker.
177  */
179 {
180  /*
181  * This queue is used to send changes from the leader apply worker to the
182  * parallel apply worker.
183  */
185 
186  /*
187  * This queue is used to transfer error messages from the parallel apply
188  * worker to the leader apply worker.
189  */
191 
193 
194  /*
195  * Indicates whether the leader apply worker needs to serialize the
196  * remaining changes to a file due to timeout when attempting to send data
197  * to the parallel apply worker via shared memory.
198  */
200 
201  /*
202  * True if the worker is being used to process a parallel apply
203  * transaction. False indicates this worker is available for re-use.
204  */
205  bool in_use;
206 
209 
210 /* Main memory context for apply worker. Permanent during worker lifetime. */
212 
214 
216 
218 
219 /* libpqreceiver connection */
221 
222 /* Worker and subscription objects. */
225 
227 
229 
230 extern void logicalrep_worker_attach(int slot);
231 extern LogicalRepWorker *logicalrep_worker_find(Oid subid, Oid relid,
232  bool only_running);
233 extern List *logicalrep_workers_find(Oid subid, bool only_running);
234 extern bool logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname,
235  Oid userid, Oid relid,
236  dsm_handle subworker_dsm);
237 extern void logicalrep_worker_stop(Oid subid, Oid relid);
239 extern void logicalrep_worker_wakeup(Oid subid, Oid relid);
241 
242 extern int logicalrep_sync_worker_count(Oid subid);
243 
244 extern void ReplicationOriginNameForLogicalRep(Oid suboid, Oid relid,
245  char *originname, Size szoriginname);
246 extern char *LogicalRepSyncTableStart(XLogRecPtr *origin_startpos);
247 
248 extern bool AllTablesyncsReady(void);
249 extern void UpdateTwoPhaseState(Oid suboid, char new_state);
250 
251 extern void process_syncing_tables(XLogRecPtr current_lsn);
252 extern void invalidate_syncing_table_states(Datum arg, int cacheid,
253  uint32 hashvalue);
254 
255 extern void stream_start_internal(TransactionId xid, bool first_segment);
256 extern void stream_stop_internal(TransactionId xid);
257 
258 /* Common streaming function to apply all the spooled messages */
259 extern void apply_spooled_messages(FileSet *stream_fileset, TransactionId xid,
260  XLogRecPtr lsn);
261 
262 extern void apply_dispatch(StringInfo s);
263 
264 extern void maybe_reread_subscription(void);
265 
266 extern void stream_cleanup_files(Oid subid, TransactionId xid);
267 
268 extern void InitializeApplyWorker(void);
269 
270 extern void store_flush_position(XLogRecPtr remote_lsn, XLogRecPtr local_lsn);
271 
272 /* Function for apply error callback */
273 extern void apply_error_callback(void *arg);
274 extern void set_apply_error_context_origin(char *originname);
275 
276 /* Parallel apply worker setup and interactions */
277 extern void pa_allocate_worker(TransactionId xid);
279 extern void pa_detach_all_error_mq(void);
280 
281 extern bool pa_send_data(ParallelApplyWorkerInfo *winfo, Size nbytes,
282  const void *data);
284  bool stream_locked);
285 
286 extern void pa_set_xact_state(ParallelApplyWorkerShared *wshared,
287  ParallelTransState xact_state);
289 
290 extern void pa_start_subtrans(TransactionId current_xid,
291  TransactionId top_xid);
292 extern void pa_reset_subtrans(void);
293 extern void pa_stream_abort(LogicalRepStreamAbortData *abort_data);
295  PartialFileSetState fileset_state);
296 
297 extern void pa_lock_stream(TransactionId xid, LOCKMODE lockmode);
298 extern void pa_unlock_stream(TransactionId xid, LOCKMODE lockmode);
299 
300 extern void pa_lock_transaction(TransactionId xid, LOCKMODE lockmode);
301 extern void pa_unlock_transaction(TransactionId xid, LOCKMODE lockmode);
302 
303 extern void pa_decr_and_wait_stream_block(void);
304 
305 extern void pa_xact_finish(ParallelApplyWorkerInfo *winfo,
306  XLogRecPtr remote_lsn);
307 
308 #define isParallelApplyWorker(worker) ((worker)->leader_pid != InvalidPid)
309 
310 static inline bool
312 {
314 }
315 
316 static inline bool
318 {
319  return (!am_tablesync_worker() &&
321 }
322 
323 static inline bool
325 {
327 }
328 
329 #endif /* WORKER_INTERNAL_H */
unsigned short uint16
Definition: c.h:489
unsigned int uint32
Definition: c.h:490
#define PGDLLIMPORT
Definition: c.h:1321
uint32 TransactionId
Definition: c.h:636
#define OidIsValid(objectId)
Definition: c.h:759
size_t Size
Definition: c.h:589
int64 TimestampTz
Definition: timestamp.h:39
uint32 dsm_handle
Definition: dsm_impl.h:55
int LOCKMODE
Definition: lockdefs.h:26
void * arg
const void * data
NameData subname
uintptr_t Datum
Definition: postgres.h:64
unsigned int Oid
Definition: postgres_ext.h:31
int slock_t
Definition: s_lock.h:754
Definition: pg_list.h:54
XLogRecPtr relstate_lsn
TimestampTz last_recv_time
TimestampTz launch_time
TimestampTz reply_time
FileSet * stream_fileset
XLogRecPtr reply_lsn
XLogRecPtr last_lsn
TimestampTz last_send_time
Definition: proc.h:162
shm_mq_handle * error_mq_handle
shm_mq_handle * mq_handle
ParallelApplyWorkerShared * shared
pg_atomic_uint32 pending_stream_count
PartialFileSetState fileset_state
ParallelTransState xact_state
bool AllTablesyncsReady(void)
Definition: tablesync.c:1580
ParallelTransState
@ PARALLEL_TRANS_UNKNOWN
@ PARALLEL_TRANS_STARTED
@ PARALLEL_TRANS_FINISHED
#define isParallelApplyWorker(worker)
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:249
void pa_set_xact_state(ParallelApplyWorkerShared *wshared, ParallelTransState xact_state)
void stream_cleanup_files(Oid subid, TransactionId xid)
Definition: worker.c:4199
void pa_unlock_stream(TransactionId xid, LOCKMODE lockmode)
PGDLLIMPORT ErrorContextCallback * apply_error_context_stack
Definition: worker.c:305
PGDLLIMPORT bool in_remote_transaction
Definition: worker.c:320
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition: launcher.c:681
PGDLLIMPORT MemoryContext ApplyMessageContext
Definition: worker.c:307
void InitializeApplyWorker(void)
Definition: worker.c:4445
void invalidate_syncing_table_states(Datum arg, int cacheid, uint32 hashvalue)
Definition: tablesync.c:272
static bool am_parallel_apply_worker(void)
PGDLLIMPORT LogicalRepWorker * MyLogicalRepWorker
Definition: launcher.c:61
struct ParallelApplyWorkerShared ParallelApplyWorkerShared
void logicalrep_worker_attach(int slot)
Definition: launcher.c:692
ParallelApplyWorkerInfo * pa_find_worker(TransactionId xid)
void stream_stop_internal(TransactionId xid)
Definition: worker.c:1627
void pa_stream_abort(LogicalRepStreamAbortData *abort_data)
struct ParallelApplyWorkerInfo ParallelApplyWorkerInfo
bool logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname, Oid userid, Oid relid, dsm_handle subworker_dsm)
Definition: launcher.c:306
void pa_lock_stream(TransactionId xid, LOCKMODE lockmode)
void apply_dispatch(StringInfo s)
Definition: worker.c:3285
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:618
List * logicalrep_workers_find(Oid subid, bool only_running)
Definition: launcher.c:281
void process_syncing_tables(XLogRecPtr current_lsn)
Definition: tablesync.c:647
void pa_lock_transaction(TransactionId xid, LOCKMODE lockmode)
void ReplicationOriginNameForLogicalRep(Oid suboid, Oid relid, char *originname, Size szoriginname)
Definition: worker.c:461
char * LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
Definition: tablesync.c:1244
void pa_detach_all_error_mq(void)
PGDLLIMPORT struct WalReceiverConn * LogRepWorkerWalRcvConn
Definition: worker.c:313
PGDLLIMPORT bool InitializingApplyWorker
Definition: worker.c:335
void stream_start_internal(TransactionId xid, bool first_segment)
Definition: worker.c:1453
void pa_start_subtrans(TransactionId current_xid, TransactionId top_xid)
void pa_switch_to_partial_serialize(ParallelApplyWorkerInfo *winfo, bool stream_locked)
void logicalrep_worker_wakeup(Oid subid, Oid relid)
Definition: launcher.c:661
void logicalrep_worker_stop(Oid subid, Oid relid)
Definition: launcher.c:594
void set_apply_error_context_origin(char *originname)
Definition: worker.c:5058
struct LogicalRepWorker LogicalRepWorker
void pa_xact_finish(ParallelApplyWorkerInfo *winfo, XLogRecPtr remote_lsn)
PartialFileSetState
@ FS_EMPTY
@ FS_SERIALIZE_DONE
@ FS_READY
@ FS_SERIALIZE_IN_PROGRESS
static bool am_tablesync_worker(void)
static bool am_leader_apply_worker(void)
PGDLLIMPORT Subscription * MySubscription
Definition: worker.c:315
void apply_error_callback(void *arg)
Definition: worker.c:4916
void store_flush_position(XLogRecPtr remote_lsn, XLogRecPtr local_lsn)
Definition: worker.c:3449
int logicalrep_sync_worker_count(Oid subid)
Definition: launcher.c:832
void maybe_reread_subscription(void)
Definition: worker.c:3872
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:2025
void UpdateTwoPhaseState(Oid suboid, char new_state)
Definition: tablesync.c:1605
void pa_set_stream_apply_worker(ParallelApplyWorkerInfo *winfo)
void pa_unlock_transaction(TransactionId xid, LOCKMODE lockmode)
void pa_decr_and_wait_stream_block(void)
PGDLLIMPORT MemoryContext ApplyContext
Definition: worker.c:308
uint64 XLogRecPtr
Definition: xlogdefs.h:21