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 
228 extern void logicalrep_worker_attach(int slot);
229 extern LogicalRepWorker *logicalrep_worker_find(Oid subid, Oid relid,
230  bool only_running);
231 extern List *logicalrep_workers_find(Oid subid, bool only_running);
232 extern bool logicalrep_worker_launch(Oid dbid, Oid subid, const char *subname,
233  Oid userid, Oid relid,
234  dsm_handle subworker_dsm);
235 extern void logicalrep_worker_stop(Oid subid, Oid relid);
236 extern void logicalrep_pa_worker_stop(int slot_no, uint16 generation);
237 extern void logicalrep_worker_wakeup(Oid subid, Oid relid);
239 
240 extern int logicalrep_sync_worker_count(Oid subid);
241 
242 extern void ReplicationOriginNameForLogicalRep(Oid suboid, Oid relid,
243  char *originname, Size szoriginname);
244 extern char *LogicalRepSyncTableStart(XLogRecPtr *origin_startpos);
245 
246 extern bool AllTablesyncsReady(void);
247 extern void UpdateTwoPhaseState(Oid suboid, char new_state);
248 
249 extern void process_syncing_tables(XLogRecPtr current_lsn);
250 extern void invalidate_syncing_table_states(Datum arg, int cacheid,
251  uint32 hashvalue);
252 
253 extern void stream_start_internal(TransactionId xid, bool first_segment);
254 extern void stream_stop_internal(TransactionId xid);
255 
256 /* Common streaming function to apply all the spooled messages */
257 extern void apply_spooled_messages(FileSet *stream_fileset, TransactionId xid,
258  XLogRecPtr lsn);
259 
260 extern void apply_dispatch(StringInfo s);
261 
262 extern void maybe_reread_subscription(void);
263 
264 extern void stream_cleanup_files(Oid subid, TransactionId xid);
265 
266 extern void InitializeApplyWorker(void);
267 
268 extern void store_flush_position(XLogRecPtr remote_lsn, XLogRecPtr local_lsn);
269 
270 /* Function for apply error callback */
271 extern void apply_error_callback(void *arg);
272 extern void set_apply_error_context_origin(char *originname);
273 
274 /* Parallel apply worker setup and interactions */
275 extern void pa_allocate_worker(TransactionId xid);
277 extern void pa_detach_all_error_mq(void);
278 
279 extern bool pa_send_data(ParallelApplyWorkerInfo *winfo, Size nbytes,
280  const void *data);
282  bool stream_locked);
283 
284 extern void pa_set_xact_state(ParallelApplyWorkerShared *wshared,
285  ParallelTransState in_xact);
287 
288 extern void pa_start_subtrans(TransactionId current_xid,
289  TransactionId top_xid);
290 extern void pa_reset_subtrans(void);
291 extern void pa_stream_abort(LogicalRepStreamAbortData *abort_data);
293  PartialFileSetState fileset_state);
294 
295 extern void pa_lock_stream(TransactionId xid, LOCKMODE lockmode);
296 extern void pa_unlock_stream(TransactionId xid, LOCKMODE lockmode);
297 
298 extern void pa_lock_transaction(TransactionId xid, LOCKMODE lockmode);
299 extern void pa_unlock_transaction(TransactionId xid, LOCKMODE lockmode);
300 
301 extern void pa_decr_and_wait_stream_block(void);
302 
303 extern void pa_xact_finish(ParallelApplyWorkerInfo *winfo,
304  XLogRecPtr remote_lsn);
305 
306 #define isParallelApplyWorker(worker) ((worker)->leader_pid != InvalidPid)
307 
308 static inline bool
310 {
312 }
313 
314 static inline bool
316 {
317  return (!am_tablesync_worker() &&
319 }
320 
321 static inline bool
323 {
325 }
326 
327 #endif /* WORKER_INTERNAL_H */
unsigned short uint16
Definition: c.h:489
unsigned int uint32
Definition: c.h:490
#define PGDLLIMPORT
Definition: c.h:1303
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:1558
ParallelTransState
@ PARALLEL_TRANS_UNKNOWN
@ PARALLEL_TRANS_STARTED
@ PARALLEL_TRANS_FINISHED
#define isParallelApplyWorker(worker)
void logicalrep_pa_worker_stop(int slot_no, uint16 generation)
Definition: launcher.c:619
LogicalRepWorker * logicalrep_worker_find(Oid subid, Oid relid, bool only_running)
Definition: launcher.c:249
void stream_cleanup_files(Oid subid, TransactionId xid)
Definition: worker.c:4157
void pa_unlock_stream(TransactionId xid, LOCKMODE lockmode)
PGDLLIMPORT ErrorContextCallback * apply_error_context_stack
Definition: worker.c:304
PGDLLIMPORT bool in_remote_transaction
Definition: worker.c:319
void logicalrep_worker_wakeup_ptr(LogicalRepWorker *worker)
Definition: launcher.c:663
PGDLLIMPORT MemoryContext ApplyMessageContext
Definition: worker.c:306
void InitializeApplyWorker(void)
Definition: worker.c:4403
void invalidate_syncing_table_states(Datum arg, int cacheid, uint32 hashvalue)
Definition: tablesync.c:271
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:674
void pa_set_xact_state(ParallelApplyWorkerShared *wshared, ParallelTransState in_xact)
ParallelApplyWorkerInfo * pa_find_worker(TransactionId xid)
void stream_stop_internal(TransactionId xid)
Definition: worker.c:1618
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:3244
void pa_set_fileset_state(ParallelApplyWorkerShared *wshared, PartialFileSetState fileset_state)
void pa_reset_subtrans(void)
List * logicalrep_workers_find(Oid subid, bool only_running)
Definition: launcher.c:281
void process_syncing_tables(XLogRecPtr current_lsn)
Definition: tablesync.c:646
void pa_lock_transaction(TransactionId xid, LOCKMODE lockmode)
void ReplicationOriginNameForLogicalRep(Oid suboid, Oid relid, char *originname, Size szoriginname)
Definition: worker.c:454
char * LogicalRepSyncTableStart(XLogRecPtr *origin_startpos)
Definition: tablesync.c:1229
void pa_detach_all_error_mq(void)
PGDLLIMPORT struct WalReceiverConn * LogRepWorkerWalRcvConn
Definition: worker.c:312
void stream_start_internal(TransactionId xid, bool first_segment)
Definition: worker.c:1444
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:643
void logicalrep_worker_stop(Oid subid, Oid relid)
Definition: launcher.c:594
void set_apply_error_context_origin(char *originname)
Definition: worker.c:5004
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:314
void apply_error_callback(void *arg)
Definition: worker.c:4862
void store_flush_position(XLogRecPtr remote_lsn, XLogRecPtr local_lsn)
Definition: worker.c:3408
int logicalrep_sync_worker_count(Oid subid)
Definition: launcher.c:811
void maybe_reread_subscription(void)
Definition: worker.c:3831
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:2016
void UpdateTwoPhaseState(Oid suboid, char new_state)
Definition: tablesync.c:1583
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:307
uint64 XLogRecPtr
Definition: xlogdefs.h:21