Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : * worker.c
3 : : * PostgreSQL logical replication worker (apply)
4 : : *
5 : : * Copyright (c) 2016-2026, PostgreSQL Global Development Group
6 : : *
7 : : * IDENTIFICATION
8 : : * src/backend/replication/logical/worker.c
9 : : *
10 : : * NOTES
11 : : * This file contains the worker which applies logical changes as they come
12 : : * from remote logical replication stream.
13 : : *
14 : : * The main worker (apply) is started by logical replication worker
15 : : * launcher for every enabled subscription in a database. It uses
16 : : * walsender protocol to communicate with publisher.
17 : : *
18 : : * This module includes server facing code and shares libpqwalreceiver
19 : : * module with walreceiver for providing the libpq specific functionality.
20 : : *
21 : : *
22 : : * STREAMED TRANSACTIONS
23 : : * ---------------------
24 : : * Streamed transactions (large transactions exceeding a memory limit on the
25 : : * upstream) are applied using one of two approaches:
26 : : *
27 : : * 1) Write to temporary files and apply when the final commit arrives
28 : : *
29 : : * This approach is used when the user has set the subscription's streaming
30 : : * option as on.
31 : : *
32 : : * Unlike the regular (non-streamed) case, handling streamed transactions has
33 : : * to handle aborts of both the toplevel transaction and subtransactions. This
34 : : * is achieved by tracking offsets for subtransactions, which is then used
35 : : * to truncate the file with serialized changes.
36 : : *
37 : : * The files are placed in tmp file directory by default, and the filenames
38 : : * include both the XID of the toplevel transaction and OID of the
39 : : * subscription. This is necessary so that different workers processing a
40 : : * remote transaction with the same XID doesn't interfere.
41 : : *
42 : : * We use BufFiles instead of using normal temporary files because (a) the
43 : : * BufFile infrastructure supports temporary files that exceed the OS file size
44 : : * limit, (b) provides a way for automatic clean up on the error and (c) provides
45 : : * a way to survive these files across local transactions and allow to open and
46 : : * close at stream start and close. We decided to use FileSet
47 : : * infrastructure as without that it deletes the files on the closure of the
48 : : * file and if we decide to keep stream files open across the start/stop stream
49 : : * then it will consume a lot of memory (more than 8K for each BufFile and
50 : : * there could be multiple such BufFiles as the subscriber could receive
51 : : * multiple start/stop streams for different transactions before getting the
52 : : * commit). Moreover, if we don't use FileSet then we also need to invent
53 : : * a new way to pass filenames to BufFile APIs so that we are allowed to open
54 : : * the file we desired across multiple stream-open calls for the same
55 : : * transaction.
56 : : *
57 : : * 2) Parallel apply workers.
58 : : *
59 : : * This approach is used when the user has set the subscription's streaming
60 : : * option as parallel. See logical/applyparallelworker.c for information about
61 : : * this approach.
62 : : *
63 : : * TWO_PHASE TRANSACTIONS
64 : : * ----------------------
65 : : * Two phase transactions are replayed at prepare and then committed or
66 : : * rolled back at commit prepared and rollback prepared respectively. It is
67 : : * possible to have a prepared transaction that arrives at the apply worker
68 : : * when the tablesync is busy doing the initial copy. In this case, the apply
69 : : * worker skips all the prepared operations [e.g. inserts] while the tablesync
70 : : * is still busy (see the condition of should_apply_changes_for_rel). The
71 : : * tablesync worker might not get such a prepared transaction because say it
72 : : * was prior to the initial consistent point but might have got some later
73 : : * commits. Now, the tablesync worker will exit without doing anything for the
74 : : * prepared transaction skipped by the apply worker as the sync location for it
75 : : * will be already ahead of the apply worker's current location. This would lead
76 : : * to an "empty prepare", because later when the apply worker does the commit
77 : : * prepare, there is nothing in it (the inserts were skipped earlier).
78 : : *
79 : : * To avoid this, and similar prepare confusions the subscription's two_phase
80 : : * commit is enabled only after the initial sync is over. The two_phase option
81 : : * has been implemented as a tri-state with values DISABLED, PENDING, and
82 : : * ENABLED.
83 : : *
84 : : * Even if the user specifies they want a subscription with two_phase = on,
85 : : * internally it will start with a tri-state of PENDING which only becomes
86 : : * ENABLED after all tablesync initializations are completed - i.e. when all
87 : : * tablesync workers have reached their READY state. In other words, the value
88 : : * PENDING is only a temporary state for subscription start-up.
89 : : *
90 : : * Until the two_phase is properly available (ENABLED) the subscription will
91 : : * behave as if two_phase = off. When the apply worker detects that all
92 : : * tablesyncs have become READY (while the tri-state was PENDING) it will
93 : : * restart the apply worker process. This happens in
94 : : * ProcessSyncingTablesForApply.
95 : : *
96 : : * When the (re-started) apply worker finds that all tablesyncs are READY for a
97 : : * two_phase tri-state of PENDING it start streaming messages with the
98 : : * two_phase option which in turn enables the decoding of two-phase commits at
99 : : * the publisher. Then, it updates the tri-state value from PENDING to ENABLED.
100 : : * Now, it is possible that during the time we have not enabled two_phase, the
101 : : * publisher (replication server) would have skipped some prepares but we
102 : : * ensure that such prepares are sent along with commit prepare, see
103 : : * ReorderBufferFinishPrepared.
104 : : *
105 : : * If the subscription has no tables then a two_phase tri-state PENDING is
106 : : * left unchanged. This lets the user still do an ALTER SUBSCRIPTION REFRESH
107 : : * PUBLICATION which might otherwise be disallowed (see below).
108 : : *
109 : : * If ever a user needs to be aware of the tri-state value, they can fetch it
110 : : * from the pg_subscription catalog (see column subtwophasestate).
111 : : *
112 : : * Finally, to avoid problems mentioned in previous paragraphs from any
113 : : * subsequent (not READY) tablesyncs (need to toggle two_phase option from 'on'
114 : : * to 'off' and then again back to 'on') there is a restriction for
115 : : * ALTER SUBSCRIPTION REFRESH PUBLICATION. This command is not permitted when
116 : : * the two_phase tri-state is ENABLED, except when copy_data = false.
117 : : *
118 : : * We can get prepare of the same GID more than once for the genuine cases
119 : : * where we have defined multiple subscriptions for publications on the same
120 : : * server and prepared transaction has operations on tables subscribed to those
121 : : * subscriptions. For such cases, if we use the GID sent by publisher one of
122 : : * the prepares will be successful and others will fail, in which case the
123 : : * server will send them again. Now, this can lead to a deadlock if user has
124 : : * set synchronous_standby_names for all the subscriptions on subscriber. To
125 : : * avoid such deadlocks, we generate a unique GID (consisting of the
126 : : * subscription oid and the xid of the prepared transaction) for each prepare
127 : : * transaction on the subscriber.
128 : : *
129 : : * FAILOVER
130 : : * ----------------------
131 : : * The logical slot on the primary can be synced to the standby by specifying
132 : : * failover = true when creating the subscription. Enabling failover allows us
133 : : * to smoothly transition to the promoted standby, ensuring that we can
134 : : * subscribe to the new primary without losing any data.
135 : : *
136 : : * RETAIN DEAD TUPLES
137 : : * ----------------------
138 : : * Each apply worker that enabled retain_dead_tuples option maintains a
139 : : * non-removable transaction ID (oldest_nonremovable_xid) in shared memory to
140 : : * prevent dead rows from being removed prematurely when the apply worker still
141 : : * needs them to detect update_deleted conflicts. Additionally, this helps to
142 : : * retain the required commit_ts module information, which further helps to
143 : : * detect update_origin_differs and delete_origin_differs conflicts reliably, as
144 : : * otherwise, vacuum freeze could remove the required information.
145 : : *
146 : : * The logical replication launcher manages an internal replication slot named
147 : : * "pg_conflict_detection". It asynchronously aggregates the non-removable
148 : : * transaction ID from all apply workers to determine the appropriate xmin for
149 : : * the slot, thereby retaining necessary tuples.
150 : : *
151 : : * The non-removable transaction ID in the apply worker is advanced to the
152 : : * oldest running transaction ID once all concurrent transactions on the
153 : : * publisher have been applied and flushed locally. The process involves:
154 : : *
155 : : * - RDT_GET_CANDIDATE_XID:
156 : : * Call GetOldestActiveTransactionId() to take oldestRunningXid as the
157 : : * candidate xid.
158 : : *
159 : : * - RDT_REQUEST_PUBLISHER_STATUS:
160 : : * Send a message to the walsender requesting the publisher status, which
161 : : * includes the latest WAL write position and information about transactions
162 : : * that are in the commit phase.
163 : : *
164 : : * - RDT_WAIT_FOR_PUBLISHER_STATUS:
165 : : * Wait for the status from the walsender. After receiving the first status,
166 : : * do not proceed if there are concurrent remote transactions that are still
167 : : * in the commit phase. These transactions might have been assigned an
168 : : * earlier commit timestamp but have not yet written the commit WAL record.
169 : : * Continue to request the publisher status (RDT_REQUEST_PUBLISHER_STATUS)
170 : : * until all these transactions have completed.
171 : : *
172 : : * - RDT_WAIT_FOR_LOCAL_FLUSH:
173 : : * Advance the non-removable transaction ID if the current flush location has
174 : : * reached or surpassed the last received WAL position.
175 : : *
176 : : * - RDT_STOP_CONFLICT_INFO_RETENTION:
177 : : * This phase is required only when max_retention_duration is defined. We
178 : : * enter this phase if the wait time in either the
179 : : * RDT_WAIT_FOR_PUBLISHER_STATUS or RDT_WAIT_FOR_LOCAL_FLUSH phase exceeds
180 : : * configured max_retention_duration. In this phase,
181 : : * pg_subscription.subretentionactive is updated to false within a new
182 : : * transaction, and oldest_nonremovable_xid is set to InvalidTransactionId.
183 : : *
184 : : * - RDT_RESUME_CONFLICT_INFO_RETENTION:
185 : : * This phase is required only when max_retention_duration is defined. We
186 : : * enter this phase if the retention was previously stopped, and the time
187 : : * required to advance the non-removable transaction ID in the
188 : : * RDT_WAIT_FOR_LOCAL_FLUSH phase has decreased to within acceptable limits
189 : : * (or if max_retention_duration is set to 0). During this phase,
190 : : * pg_subscription.subretentionactive is updated to true within a new
191 : : * transaction, and the worker will be restarted.
192 : : *
193 : : * The overall state progression is: GET_CANDIDATE_XID ->
194 : : * REQUEST_PUBLISHER_STATUS -> WAIT_FOR_PUBLISHER_STATUS -> (loop to
195 : : * REQUEST_PUBLISHER_STATUS till concurrent remote transactions end) ->
196 : : * WAIT_FOR_LOCAL_FLUSH -> loop back to GET_CANDIDATE_XID.
197 : : *
198 : : * Retaining the dead tuples for this period is sufficient for ensuring
199 : : * eventual consistency using last-update-wins strategy, as dead tuples are
200 : : * useful for detecting conflicts only during the application of concurrent
201 : : * transactions from remote nodes. After applying and flushing all remote
202 : : * transactions that occurred concurrently with the tuple DELETE, any
203 : : * subsequent UPDATE from a remote node should have a later timestamp. In such
204 : : * cases, it is acceptable to detect an update_missing scenario and convert the
205 : : * UPDATE to an INSERT when applying it. But, for concurrent remote
206 : : * transactions with earlier timestamps than the DELETE, detecting
207 : : * update_deleted is necessary, as the UPDATEs in remote transactions should be
208 : : * ignored if their timestamp is earlier than that of the dead tuples.
209 : : *
210 : : * Note that advancing the non-removable transaction ID is not supported if the
211 : : * publisher is also a physical standby. This is because the logical walsender
212 : : * on the standby can only get the WAL replay position but there may be more
213 : : * WALs that are being replicated from the primary and those WALs could have
214 : : * earlier commit timestamp.
215 : : *
216 : : * Similarly, when the publisher has subscribed to another publisher,
217 : : * information necessary for conflict detection cannot be retained for
218 : : * changes from origins other than the publisher. This is because publisher
219 : : * lacks the information on concurrent transactions of other publishers to
220 : : * which it subscribes. As the information on concurrent transactions is
221 : : * unavailable beyond subscriber's immediate publishers, the non-removable
222 : : * transaction ID might be advanced prematurely before changes from other
223 : : * origins have been fully applied.
224 : : *
225 : : * XXX Retaining information for changes from other origins might be possible
226 : : * by requesting the subscription on that origin to enable retain_dead_tuples
227 : : * and fetching the conflict detection slot.xmin along with the publisher's
228 : : * status. In the RDT_WAIT_FOR_PUBLISHER_STATUS phase, the apply worker could
229 : : * wait for the remote slot's xmin to reach the oldest active transaction ID,
230 : : * ensuring that all transactions from other origins have been applied on the
231 : : * publisher, thereby getting the latest WAL position that includes all
232 : : * concurrent changes. However, this approach may impact performance, so it
233 : : * might not worth the effort.
234 : : *
235 : : * XXX It seems feasible to get the latest commit's WAL location from the
236 : : * publisher and wait till that is applied. However, we can't do that
237 : : * because commit timestamps can regress as a commit with a later LSN is not
238 : : * guaranteed to have a later timestamp than those with earlier LSNs. Having
239 : : * said that, even if that is possible, it won't improve performance much as
240 : : * the apply always lag and moves slowly as compared with the transactions
241 : : * on the publisher.
242 : : *-------------------------------------------------------------------------
243 : : */
244 : :
245 : : #include "postgres.h"
246 : :
247 : : #include <sys/stat.h>
248 : : #include <unistd.h>
249 : :
250 : : #include "access/genam.h"
251 : : #include "access/commit_ts.h"
252 : : #include "access/table.h"
253 : : #include "access/tableam.h"
254 : : #include "access/tupconvert.h"
255 : : #include "access/twophase.h"
256 : : #include "access/xact.h"
257 : : #include "catalog/indexing.h"
258 : : #include "catalog/pg_inherits.h"
259 : : #include "catalog/pg_subscription.h"
260 : : #include "catalog/pg_subscription_rel.h"
261 : : #include "commands/subscriptioncmds.h"
262 : : #include "commands/tablecmds.h"
263 : : #include "commands/trigger.h"
264 : : #include "executor/executor.h"
265 : : #include "executor/execPartition.h"
266 : : #include "libpq/pqformat.h"
267 : : #include "miscadmin.h"
268 : : #include "optimizer/optimizer.h"
269 : : #include "parser/parse_relation.h"
270 : : #include "pgstat.h"
271 : : #include "port/pg_bitutils.h"
272 : : #include "postmaster/bgworker.h"
273 : : #include "postmaster/interrupt.h"
274 : : #include "postmaster/walwriter.h"
275 : : #include "replication/conflict.h"
276 : : #include "replication/logicallauncher.h"
277 : : #include "replication/logicalproto.h"
278 : : #include "replication/logicalrelation.h"
279 : : #include "replication/logicalworker.h"
280 : : #include "replication/origin.h"
281 : : #include "replication/slot.h"
282 : : #include "replication/walreceiver.h"
283 : : #include "replication/worker_internal.h"
284 : : #include "rewrite/rewriteHandler.h"
285 : : #include "storage/buffile.h"
286 : : #include "storage/ipc.h"
287 : : #include "storage/latch.h"
288 : : #include "storage/lmgr.h"
289 : : #include "storage/procarray.h"
290 : : #include "tcop/tcopprot.h"
291 : : #include "utils/acl.h"
292 : : #include "utils/guc.h"
293 : : #include "utils/inval.h"
294 : : #include "utils/lsyscache.h"
295 : : #include "utils/memutils.h"
296 : : #include "utils/pg_lsn.h"
297 : : #include "utils/rel.h"
298 : : #include "utils/rls.h"
299 : : #include "utils/snapmgr.h"
300 : : #include "utils/syscache.h"
301 : : #include "utils/usercontext.h"
302 : : #include "utils/wait_event.h"
303 : :
304 : : #define NAPTIME_PER_CYCLE 1000 /* max sleep time between cycles (1s) */
305 : :
306 : : typedef struct FlushPosition
307 : : {
308 : : dlist_node node;
309 : : XLogRecPtr local_end;
310 : : XLogRecPtr remote_end;
311 : : } FlushPosition;
312 : :
313 : : static dlist_head lsn_mapping = DLIST_STATIC_INIT(lsn_mapping);
314 : :
315 : : typedef struct ApplyExecutionData
316 : : {
317 : : EState *estate; /* executor state, used to track resources */
318 : :
319 : : LogicalRepRelMapEntry *targetRel; /* replication target rel */
320 : : ResultRelInfo *targetRelInfo; /* ResultRelInfo for same */
321 : :
322 : : /* These fields are used when the target relation is partitioned: */
323 : : ModifyTableState *mtstate; /* dummy ModifyTable state */
324 : : PartitionTupleRouting *proute; /* partition routing info */
325 : : } ApplyExecutionData;
326 : :
327 : : /* Struct for saving and restoring apply errcontext information */
328 : : typedef struct ApplyErrorCallbackArg
329 : : {
330 : : LogicalRepMsgType command; /* 0 if invalid */
331 : : LogicalRepRelMapEntry *rel;
332 : :
333 : : /* Remote node information */
334 : : int remote_attnum; /* -1 if invalid */
335 : : TransactionId remote_xid;
336 : : XLogRecPtr finish_lsn;
337 : : char *origin_name;
338 : : } ApplyErrorCallbackArg;
339 : :
340 : : /*
341 : : * The action to be taken for the changes in the transaction.
342 : : *
343 : : * TRANS_LEADER_APPLY:
344 : : * This action means that we are in the leader apply worker or table sync
345 : : * worker. The changes of the transaction are either directly applied or
346 : : * are read from temporary files (for streaming transactions) and then
347 : : * applied by the worker.
348 : : *
349 : : * TRANS_LEADER_SERIALIZE:
350 : : * This action means that we are in the leader apply worker or table sync
351 : : * worker. Changes are written to temporary files and then applied when the
352 : : * final commit arrives.
353 : : *
354 : : * TRANS_LEADER_SEND_TO_PARALLEL:
355 : : * This action means that we are in the leader apply worker and need to send
356 : : * the changes to the parallel apply worker.
357 : : *
358 : : * TRANS_LEADER_PARTIAL_SERIALIZE:
359 : : * This action means that we are in the leader apply worker and have sent some
360 : : * changes directly to the parallel apply worker and the remaining changes are
361 : : * serialized to a file, due to timeout while sending data. The parallel apply
362 : : * worker will apply these serialized changes when the final commit arrives.
363 : : *
364 : : * We can't use TRANS_LEADER_SERIALIZE for this case because, in addition to
365 : : * serializing changes, the leader worker also needs to serialize the
366 : : * STREAM_XXX message to a file, and wait for the parallel apply worker to
367 : : * finish the transaction when processing the transaction finish command. So
368 : : * this new action was introduced to keep the code and logic clear.
369 : : *
370 : : * TRANS_PARALLEL_APPLY:
371 : : * This action means that we are in the parallel apply worker and changes of
372 : : * the transaction are applied directly by the worker.
373 : : */
374 : : typedef enum
375 : : {
376 : : /* The action for non-streaming transactions. */
377 : : TRANS_LEADER_APPLY,
378 : :
379 : : /* Actions for streaming transactions. */
380 : : TRANS_LEADER_SERIALIZE,
381 : : TRANS_LEADER_SEND_TO_PARALLEL,
382 : : TRANS_LEADER_PARTIAL_SERIALIZE,
383 : : TRANS_PARALLEL_APPLY,
384 : : } TransApplyAction;
385 : :
386 : : /*
387 : : * The phases involved in advancing the non-removable transaction ID.
388 : : *
389 : : * See comments atop worker.c for details of the transition between these
390 : : * phases.
391 : : */
392 : : typedef enum
393 : : {
394 : : RDT_GET_CANDIDATE_XID,
395 : : RDT_REQUEST_PUBLISHER_STATUS,
396 : : RDT_WAIT_FOR_PUBLISHER_STATUS,
397 : : RDT_WAIT_FOR_LOCAL_FLUSH,
398 : : RDT_STOP_CONFLICT_INFO_RETENTION,
399 : : RDT_RESUME_CONFLICT_INFO_RETENTION,
400 : : } RetainDeadTuplesPhase;
401 : :
402 : : /*
403 : : * Critical information for managing phase transitions within the
404 : : * RetainDeadTuplesPhase.
405 : : */
406 : : typedef struct RetainDeadTuplesData
407 : : {
408 : : RetainDeadTuplesPhase phase; /* current phase */
409 : : XLogRecPtr remote_lsn; /* WAL write position on the publisher */
410 : :
411 : : /*
412 : : * Oldest transaction ID that was in the commit phase on the publisher.
413 : : * Use FullTransactionId to prevent issues with transaction ID wraparound,
414 : : * where a new remote_oldestxid could falsely appear to originate from the
415 : : * past and block advancement.
416 : : */
417 : : FullTransactionId remote_oldestxid;
418 : :
419 : : /*
420 : : * Next transaction ID to be assigned on the publisher. Use
421 : : * FullTransactionId for consistency and to allow straightforward
422 : : * comparisons with remote_oldestxid.
423 : : */
424 : : FullTransactionId remote_nextxid;
425 : :
426 : : TimestampTz reply_time; /* when the publisher responds with status */
427 : :
428 : : /*
429 : : * Publisher transaction ID that must be awaited to complete before
430 : : * entering the final phase (RDT_WAIT_FOR_LOCAL_FLUSH). Use
431 : : * FullTransactionId for the same reason as remote_nextxid.
432 : : */
433 : : FullTransactionId remote_wait_for;
434 : :
435 : : TransactionId candidate_xid; /* candidate for the non-removable
436 : : * transaction ID */
437 : : TimestampTz flushpos_update_time; /* when the remote flush position was
438 : : * updated in final phase
439 : : * (RDT_WAIT_FOR_LOCAL_FLUSH) */
440 : :
441 : : long table_sync_wait_time; /* time spent waiting for table sync
442 : : * to finish */
443 : :
444 : : /*
445 : : * The following fields are used to determine the timing for the next
446 : : * round of transaction ID advancement.
447 : : */
448 : : TimestampTz last_recv_time; /* when the last message was received */
449 : : TimestampTz candidate_xid_time; /* when the candidate_xid is decided */
450 : : int xid_advance_interval; /* how much time (ms) to wait before
451 : : * attempting to advance the
452 : : * non-removable transaction ID */
453 : : } RetainDeadTuplesData;
454 : :
455 : : /*
456 : : * The minimum (100ms) and maximum (3 minutes) intervals for advancing
457 : : * non-removable transaction IDs. The maximum interval is a bit arbitrary but
458 : : * is sufficient to not cause any undue network traffic.
459 : : */
460 : : #define MIN_XID_ADVANCE_INTERVAL 100
461 : : #define MAX_XID_ADVANCE_INTERVAL 180000
462 : :
463 : : /* errcontext tracker */
464 : : static ApplyErrorCallbackArg apply_error_callback_arg =
465 : : {
466 : : .command = 0,
467 : : .rel = NULL,
468 : : .remote_attnum = -1,
469 : : .remote_xid = InvalidTransactionId,
470 : : .finish_lsn = InvalidXLogRecPtr,
471 : : .origin_name = NULL,
472 : : };
473 : :
474 : : ErrorContextCallback *apply_error_context_stack = NULL;
475 : :
476 : : MemoryContext ApplyMessageContext = NULL;
477 : : MemoryContext ApplyContext = NULL;
478 : :
479 : : /* per stream context for streaming transactions */
480 : : static MemoryContext LogicalStreamingContext = NULL;
481 : :
482 : : WalReceiverConn *LogRepWorkerWalRcvConn = NULL;
483 : :
484 : : Subscription *MySubscription = NULL;
485 : : static bool MySubscriptionValid = false;
486 : :
487 : : static List *on_commit_wakeup_workers_subids = NIL;
488 : :
489 : : bool in_remote_transaction = false;
490 : : static XLogRecPtr remote_final_lsn = InvalidXLogRecPtr;
491 : :
492 : : /* fields valid only when processing streamed transaction */
493 : : static bool in_streamed_transaction = false;
494 : :
495 : : static TransactionId stream_xid = InvalidTransactionId;
496 : :
497 : : /*
498 : : * The number of changes applied by parallel apply worker during one streaming
499 : : * block.
500 : : */
501 : : static uint32 parallel_stream_nchanges = 0;
502 : :
503 : : /* Are we initializing an apply worker? */
504 : : bool InitializingApplyWorker = false;
505 : :
506 : : /*
507 : : * We enable skipping all data modification changes (INSERT, UPDATE, etc.) for
508 : : * the subscription if the remote transaction's finish LSN matches the subskiplsn.
509 : : * Once we start skipping changes, we don't stop it until we skip all changes of
510 : : * the transaction even if pg_subscription is updated and MySubscription->skiplsn
511 : : * gets changed or reset during that. Also, in streaming transaction cases (streaming = on),
512 : : * we don't skip receiving and spooling the changes since we decide whether or not
513 : : * to skip applying the changes when starting to apply changes. The subskiplsn is
514 : : * cleared after successfully skipping the transaction or applying non-empty
515 : : * transaction. The latter prevents the mistakenly specified subskiplsn from
516 : : * being left. Note that we cannot skip the streaming transactions when using
517 : : * parallel apply workers because we cannot get the finish LSN before applying
518 : : * the changes. So, we don't start parallel apply worker when finish LSN is set
519 : : * by the user.
520 : : */
521 : : static XLogRecPtr skip_xact_finish_lsn = InvalidXLogRecPtr;
522 : : #define is_skipping_changes() (unlikely(XLogRecPtrIsValid(skip_xact_finish_lsn)))
523 : :
524 : : /* BufFile handle of the current streaming file */
525 : : static BufFile *stream_fd = NULL;
526 : :
527 : : /*
528 : : * The remote WAL position that has been applied and flushed locally. We record
529 : : * and use this information both while sending feedback to the server and
530 : : * advancing oldest_nonremovable_xid.
531 : : */
532 : : static XLogRecPtr last_flushpos = InvalidXLogRecPtr;
533 : :
534 : : typedef struct SubXactInfo
535 : : {
536 : : TransactionId xid; /* XID of the subxact */
537 : : int fileno; /* file number in the buffile */
538 : : pgoff_t offset; /* offset in the file */
539 : : } SubXactInfo;
540 : :
541 : : /* Sub-transaction data for the current streaming transaction */
542 : : typedef struct ApplySubXactData
543 : : {
544 : : uint32 nsubxacts; /* number of sub-transactions */
545 : : uint32 nsubxacts_max; /* current capacity of subxacts */
546 : : TransactionId subxact_last; /* xid of the last sub-transaction */
547 : : SubXactInfo *subxacts; /* sub-xact offset in changes file */
548 : : } ApplySubXactData;
549 : :
550 : : static ApplySubXactData subxact_data = {0, 0, InvalidTransactionId, NULL};
551 : :
552 : : static inline void subxact_filename(char *path, Oid subid, TransactionId xid);
553 : : static inline void changes_filename(char *path, Oid subid, TransactionId xid);
554 : :
555 : : /*
556 : : * Information about subtransactions of a given toplevel transaction.
557 : : */
558 : : static void subxact_info_write(Oid subid, TransactionId xid);
559 : : static void subxact_info_read(Oid subid, TransactionId xid);
560 : : static void subxact_info_add(TransactionId xid);
561 : : static inline void cleanup_subxact_info(void);
562 : :
563 : : /*
564 : : * Serialize and deserialize changes for a toplevel transaction.
565 : : */
566 : : static void stream_open_file(Oid subid, TransactionId xid,
567 : : bool first_segment);
568 : : static void stream_write_change(char action, StringInfo s);
569 : : static void stream_open_and_write_change(TransactionId xid, char action, StringInfo s);
570 : : static void stream_close_file(void);
571 : :
572 : : static void send_feedback(XLogRecPtr recvpos, bool force, bool requestReply);
573 : :
574 : : static void maybe_advance_nonremovable_xid(RetainDeadTuplesData *rdt_data,
575 : : bool status_received);
576 : : static bool can_advance_nonremovable_xid(RetainDeadTuplesData *rdt_data);
577 : : static void process_rdt_phase_transition(RetainDeadTuplesData *rdt_data,
578 : : bool status_received);
579 : : static void get_candidate_xid(RetainDeadTuplesData *rdt_data);
580 : : static void request_publisher_status(RetainDeadTuplesData *rdt_data);
581 : : static void wait_for_publisher_status(RetainDeadTuplesData *rdt_data,
582 : : bool status_received);
583 : : static void wait_for_local_flush(RetainDeadTuplesData *rdt_data);
584 : : static bool should_stop_conflict_info_retention(RetainDeadTuplesData *rdt_data);
585 : : static void stop_conflict_info_retention(RetainDeadTuplesData *rdt_data);
586 : : static void resume_conflict_info_retention(RetainDeadTuplesData *rdt_data);
587 : : static bool update_retention_status(bool active);
588 : : static void reset_retention_data_fields(RetainDeadTuplesData *rdt_data);
589 : : static void adjust_xid_advance_interval(RetainDeadTuplesData *rdt_data,
590 : : bool new_xid_found);
591 : :
592 : : static void apply_worker_exit(void);
593 : :
594 : : static void apply_handle_commit_internal(LogicalRepCommitData *commit_data);
595 : : static void apply_handle_insert_internal(ApplyExecutionData *edata,
596 : : ResultRelInfo *relinfo,
597 : : TupleTableSlot *remoteslot);
598 : : static void apply_handle_update_internal(ApplyExecutionData *edata,
599 : : ResultRelInfo *relinfo,
600 : : TupleTableSlot *remoteslot,
601 : : LogicalRepTupleData *newtup,
602 : : Oid localindexoid);
603 : : static void apply_handle_delete_internal(ApplyExecutionData *edata,
604 : : ResultRelInfo *relinfo,
605 : : TupleTableSlot *remoteslot,
606 : : Oid localindexoid);
607 : : static bool FindReplTupleInLocalRel(ApplyExecutionData *edata, Relation localrel,
608 : : LogicalRepRelation *remoterel,
609 : : Oid localidxoid,
610 : : TupleTableSlot *remoteslot,
611 : : TupleTableSlot **localslot);
612 : : static bool FindDeletedTupleInLocalRel(Relation localrel,
613 : : Oid localidxoid,
614 : : TupleTableSlot *remoteslot,
615 : : TransactionId *delete_xid,
616 : : ReplOriginId *delete_origin,
617 : : TimestampTz *delete_time);
618 : : static void apply_handle_tuple_routing(ApplyExecutionData *edata,
619 : : TupleTableSlot *remoteslot,
620 : : LogicalRepTupleData *newtup,
621 : : CmdType operation);
622 : :
623 : : /* Functions for skipping changes */
624 : : static void maybe_start_skipping_changes(XLogRecPtr finish_lsn);
625 : : static void stop_skipping_changes(void);
626 : : static void clear_subscription_skip_lsn(XLogRecPtr finish_lsn);
627 : :
628 : : /* Functions for apply error callback */
629 : : static inline void set_apply_error_context_xact(TransactionId xid, XLogRecPtr lsn);
630 : : static inline void reset_apply_error_context_info(void);
631 : :
632 : : static TransApplyAction get_transaction_apply_action(TransactionId xid,
633 : : ParallelApplyWorkerInfo **winfo);
634 : :
635 : : static void set_wal_receiver_timeout(void);
636 : :
637 : : static void on_exit_clear_xact_state(int code, Datum arg);
638 : :
639 : : /*
640 : : * Form the origin name for the subscription.
641 : : *
642 : : * This is a common function for tablesync and other workers. Tablesync workers
643 : : * must pass a valid relid. Other callers must pass relid = InvalidOid.
644 : : *
645 : : * Return the name in the supplied buffer.
646 : : */
647 : : void
1302 akapila@postgresql.o 648 :CBC 1451 : ReplicationOriginNameForLogicalRep(Oid suboid, Oid relid,
649 : : char *originname, Size szoriginname)
650 : : {
651 [ + + ]: 1451 : if (OidIsValid(relid))
652 : : {
653 : : /* Replication origin name for tablesync workers. */
654 : 808 : snprintf(originname, szoriginname, "pg_%u_%u", suboid, relid);
655 : : }
656 : : else
657 : : {
658 : : /* Replication origin name for non-tablesync workers. */
659 : 643 : snprintf(originname, szoriginname, "pg_%u", suboid);
660 : : }
661 : 1451 : }
662 : :
663 : : /*
664 : : * Should this worker apply changes for given relation.
665 : : *
666 : : * This is mainly needed for initial relation data sync as that runs in
667 : : * separate worker process running in parallel and we need some way to skip
668 : : * changes coming to the leader apply worker during the sync of a table.
669 : : *
670 : : * Note we need to do smaller or equals comparison for SYNCDONE state because
671 : : * it might hold position of end of initial slot consistent point WAL
672 : : * record + 1 (ie start of next record) and next record can be COMMIT of
673 : : * transaction we are now processing (which is what we set remote_final_lsn
674 : : * to in apply_handle_begin).
675 : : *
676 : : * Note that for streaming transactions that are being applied in the parallel
677 : : * apply worker, we disallow applying changes if the target table in the
678 : : * subscription is not in the READY state, because we cannot decide whether to
679 : : * apply the change as we won't know remote_final_lsn by that time.
680 : : *
681 : : * We already checked this in pa_can_start() before assigning the
682 : : * streaming transaction to the parallel worker, but it also needs to be
683 : : * checked here because if the user executes ALTER SUBSCRIPTION ... REFRESH
684 : : * PUBLICATION in parallel, the new table can be added to pg_subscription_rel
685 : : * while applying this transaction.
686 : : */
687 : : static bool
3330 peter_e@gmx.net 688 : 168659 : should_apply_changes_for_rel(LogicalRepRelMapEntry *rel)
689 : : {
987 akapila@postgresql.o 690 [ + + + - : 168659 : switch (MyLogicalRepWorker->type)
- - ]
691 : : {
987 akapila@postgresql.o 692 :GBC 10 : case WORKERTYPE_TABLESYNC:
693 : 10 : return MyLogicalRepWorker->relid == rel->localreloid;
694 : :
987 akapila@postgresql.o 695 :CBC 68461 : case WORKERTYPE_PARALLEL_APPLY:
696 : : /* We don't synchronize rel's that are in unknown state. */
697 [ - + ]: 68461 : if (rel->state != SUBREL_STATE_READY &&
987 akapila@postgresql.o 698 [ # # ]:UBC 0 : rel->state != SUBREL_STATE_UNKNOWN)
699 [ # # ]: 0 : ereport(ERROR,
700 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
701 : : errmsg("logical replication parallel apply worker for subscription \"%s\" will stop",
702 : : MySubscription->name),
703 : : errdetail("Cannot handle streamed replication transactions using parallel apply workers until all tables have been synchronized.")));
704 : :
987 akapila@postgresql.o 705 :CBC 68461 : return rel->state == SUBREL_STATE_READY;
706 : :
707 : 100188 : case WORKERTYPE_APPLY:
708 [ + + ]: 100279 : return (rel->state == SUBREL_STATE_READY ||
709 [ + + ]: 91 : (rel->state == SUBREL_STATE_SYNCDONE &&
710 [ + - ]: 19 : rel->statelsn <= remote_final_lsn));
711 : :
181 akapila@postgresql.o 712 :UNC 0 : case WORKERTYPE_SEQUENCESYNC:
713 : : /* Should never happen. */
714 [ # # ]: 0 : elog(ERROR, "sequence synchronization worker is not expected to apply changes");
715 : : break;
716 : :
987 akapila@postgresql.o 717 :UBC 0 : case WORKERTYPE_UNKNOWN:
718 : : /* Should never happen. */
719 [ # # ]: 0 : elog(ERROR, "Unknown worker type");
720 : : }
721 : :
722 : 0 : return false; /* dummy for compiler */
723 : : }
724 : :
725 : : /*
726 : : * Begin one step (one INSERT, UPDATE, etc) of a replication transaction.
727 : : *
728 : : * Start a transaction, if this is the first step (else we keep using the
729 : : * existing transaction).
730 : : * Also provide a global snapshot and ensure we run in ApplyMessageContext.
731 : : */
732 : : static void
1790 tgl@sss.pgh.pa.us 733 :CBC 169120 : begin_replication_step(void)
734 : : {
735 : 169120 : SetCurrentStatementStartTimestamp();
736 : :
737 [ + + ]: 169120 : if (!IsTransactionState())
738 : : {
739 : 999 : StartTransactionCommand();
740 : 999 : maybe_reread_subscription();
741 : : }
742 : :
743 : 169117 : PushActiveSnapshot(GetTransactionSnapshot());
744 : :
3283 peter_e@gmx.net 745 : 169117 : MemoryContextSwitchTo(ApplyMessageContext);
1790 tgl@sss.pgh.pa.us 746 : 169117 : }
747 : :
748 : : /*
749 : : * Finish up one step of a replication transaction.
750 : : * Callers of begin_replication_step() must also call this.
751 : : *
752 : : * We don't close out the transaction here, but we should increment
753 : : * the command counter to make the effects of this step visible.
754 : : */
755 : : static void
756 : 169059 : end_replication_step(void)
757 : : {
758 : 169059 : PopActiveSnapshot();
759 : :
760 : 169059 : CommandCounterIncrement();
3393 peter_e@gmx.net 761 : 169059 : }
762 : :
763 : : /*
764 : : * Handle streamed transactions for both the leader apply worker and the
765 : : * parallel apply workers.
766 : : *
767 : : * In the streaming case (receiving a block of the streamed transaction), for
768 : : * serialize mode, simply redirect it to a file for the proper toplevel
769 : : * transaction, and for parallel mode, the leader apply worker will send the
770 : : * changes to parallel apply workers and the parallel apply worker will define
771 : : * savepoints if needed. (LOGICAL_REP_MSG_RELATION or LOGICAL_REP_MSG_TYPE
772 : : * messages will be applied by both leader apply worker and parallel apply
773 : : * workers).
774 : : *
775 : : * Returns true for streamed transactions (when the change is either serialized
776 : : * to file or sent to parallel apply worker), false otherwise (regular mode or
777 : : * needs to be processed by parallel apply worker).
778 : : *
779 : : * Exception: If the message being processed is LOGICAL_REP_MSG_RELATION
780 : : * or LOGICAL_REP_MSG_TYPE, return false even if the message needs to be sent
781 : : * to a parallel apply worker.
782 : : */
783 : : static bool
1986 akapila@postgresql.o 784 : 345015 : handle_streamed_transaction(LogicalRepMsgType action, StringInfo s)
785 : : {
786 : : TransactionId current_xid;
787 : : ParallelApplyWorkerInfo *winfo;
788 : : TransApplyAction apply_action;
789 : : StringInfoData original_msg;
790 : :
1212 791 : 345015 : apply_action = get_transaction_apply_action(stream_xid, &winfo);
792 : :
793 : : /* not in streaming mode */
794 [ + + ]: 345015 : if (apply_action == TRANS_LEADER_APPLY)
2070 795 : 100615 : return false;
796 : :
797 [ - + ]: 244400 : Assert(TransactionIdIsValid(stream_xid));
798 : :
799 : : /*
800 : : * The parallel apply worker needs the xid in this message to decide
801 : : * whether to define a savepoint, so save the original message that has
802 : : * not moved the cursor after the xid. We will serialize this message to a
803 : : * file in PARTIAL_SERIALIZE mode.
804 : : */
1212 805 : 244400 : original_msg = *s;
806 : :
807 : : /*
808 : : * We should have received XID of the subxact as the first part of the
809 : : * message, so extract it.
810 : : */
811 : 244400 : current_xid = pq_getmsgint(s, 4);
812 : :
813 [ - + ]: 244400 : if (!TransactionIdIsValid(current_xid))
1788 tgl@sss.pgh.pa.us 814 [ # # ]:UBC 0 : ereport(ERROR,
815 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
816 : : errmsg_internal("invalid transaction ID in streamed replication transaction")));
817 : :
1212 akapila@postgresql.o 818 [ + + + + :CBC 244400 : switch (apply_action)
- ]
819 : : {
820 : 102513 : case TRANS_LEADER_SERIALIZE:
821 [ - + ]: 102513 : Assert(stream_fd);
822 : :
823 : : /* Add the new subxact to the array (unless already there). */
824 : 102513 : subxact_info_add(current_xid);
825 : :
826 : : /* Write the change to the current file */
827 : 102513 : stream_write_change(action, s);
828 : 102513 : return true;
829 : :
830 : 68390 : case TRANS_LEADER_SEND_TO_PARALLEL:
831 [ - + ]: 68390 : Assert(winfo);
832 : :
833 : : /*
834 : : * XXX The publisher side doesn't always send relation/type update
835 : : * messages after the streaming transaction, so also update the
836 : : * relation/type in leader apply worker. See function
837 : : * cleanup_rel_sync_cache.
838 : : */
839 [ + - ]: 68390 : if (pa_send_data(winfo, s->len, s->data))
840 [ + + + - ]: 68390 : return (action != LOGICAL_REP_MSG_RELATION &&
841 : : action != LOGICAL_REP_MSG_TYPE);
842 : :
843 : : /*
844 : : * Switch to serialize mode when we are not able to send the
845 : : * change to parallel apply worker.
846 : : */
1212 akapila@postgresql.o 847 :UBC 0 : pa_switch_to_partial_serialize(winfo, false);
848 : :
849 : : pg_fallthrough;
1212 akapila@postgresql.o 850 :CBC 5006 : case TRANS_LEADER_PARTIAL_SERIALIZE:
851 : 5006 : stream_write_change(action, &original_msg);
852 : :
853 : : /* Same reason as TRANS_LEADER_SEND_TO_PARALLEL case. */
854 [ + + + - ]: 5006 : return (action != LOGICAL_REP_MSG_RELATION &&
855 : : action != LOGICAL_REP_MSG_TYPE);
856 : :
857 : 68491 : case TRANS_PARALLEL_APPLY:
858 : 68491 : parallel_stream_nchanges += 1;
859 : :
860 : : /* Define a savepoint for a subxact if needed. */
861 : 68491 : pa_start_subtrans(current_xid, stream_xid);
862 : 68491 : return false;
863 : :
1212 akapila@postgresql.o 864 :UBC 0 : default:
1107 msawada@postgresql.o 865 [ # # ]: 0 : elog(ERROR, "unexpected apply action: %d", (int) apply_action);
866 : : return false; /* silence compiler warning */
867 : : }
868 : : }
869 : :
870 : : /*
871 : : * Executor state preparation for evaluation of constraint expressions,
872 : : * indexes and triggers for the specified relation.
873 : : *
874 : : * Note that the caller must open and close any indexes to be updated.
875 : : */
876 : : static ApplyExecutionData *
1809 tgl@sss.pgh.pa.us 877 :CBC 168557 : create_edata_for_relation(LogicalRepRelMapEntry *rel)
878 : : {
879 : : ApplyExecutionData *edata;
880 : : EState *estate;
881 : : RangeTblEntry *rte;
1156 882 : 168557 : List *perminfos = NIL;
883 : : ResultRelInfo *resultRelInfo;
884 : :
146 michael@paquier.xyz 885 :GNC 168557 : edata = palloc0_object(ApplyExecutionData);
1809 tgl@sss.pgh.pa.us 886 :CBC 168557 : edata->targetRel = rel;
887 : :
888 : 168557 : edata->estate = estate = CreateExecutorState();
889 : :
3393 peter_e@gmx.net 890 : 168557 : rte = makeNode(RangeTblEntry);
891 : 168557 : rte->rtekind = RTE_RELATION;
892 : 168557 : rte->relid = RelationGetRelid(rel->localrel);
893 : 168557 : rte->relkind = rel->localrel->rd_rel->relkind;
2774 tgl@sss.pgh.pa.us 894 : 168557 : rte->rellockmode = AccessShareLock;
895 : :
1156 896 : 168557 : addRTEPermissionInfo(&perminfos, rte);
897 : :
452 amitlan@postgresql.o 898 : 168557 : ExecInitRangeTable(estate, list_make1(rte), perminfos,
899 : : bms_make_singleton(1));
900 : :
1809 tgl@sss.pgh.pa.us 901 : 168557 : edata->targetRelInfo = resultRelInfo = makeNode(ResultRelInfo);
902 : :
903 : : /*
904 : : * Use Relation opened by logicalrep_rel_open() instead of opening it
905 : : * again.
906 : : */
907 : 168557 : InitResultRelInfo(resultRelInfo, rel->localrel, 1, NULL, 0);
908 : :
909 : : /*
910 : : * We put the ResultRelInfo in the es_opened_result_relations list, even
911 : : * though we don't populate the es_result_relations array. That's a bit
912 : : * bogus, but it's enough to make ExecGetTriggerResultRel() find them.
913 : : *
914 : : * ExecOpenIndices() is not called here either, each execution path doing
915 : : * an apply operation being responsible for that.
916 : : */
1839 michael@paquier.xyz 917 : 168557 : estate->es_opened_result_relations =
1809 tgl@sss.pgh.pa.us 918 : 168557 : lappend(estate->es_opened_result_relations, resultRelInfo);
919 : :
3086 simon@2ndQuadrant.co 920 : 168557 : estate->es_output_cid = GetCurrentCommandId(true);
921 : :
922 : : /* Prepare to catch AFTER triggers. */
3350 peter_e@gmx.net 923 : 168557 : AfterTriggerBeginQuery();
924 : :
925 : : /* other fields of edata remain NULL for now */
926 : :
1809 tgl@sss.pgh.pa.us 927 : 168557 : return edata;
928 : : }
929 : :
930 : : /*
931 : : * Finish any operations related to the executor state created by
932 : : * create_edata_for_relation().
933 : : */
934 : : static void
935 : 168509 : finish_edata(ApplyExecutionData *edata)
936 : : {
937 : 168509 : EState *estate = edata->estate;
938 : :
939 : : /* Handle any queued AFTER triggers. */
1839 michael@paquier.xyz 940 : 168509 : AfterTriggerEndQuery(estate);
941 : :
942 : : /* Shut down tuple routing, if any was done. */
1809 tgl@sss.pgh.pa.us 943 [ + + ]: 168509 : if (edata->proute)
944 : 74 : ExecCleanupTupleRouting(edata->mtstate, edata->proute);
945 : :
946 : : /*
947 : : * Cleanup. It might seem that we should call ExecCloseResultRelations()
948 : : * here, but we intentionally don't. It would close the rel we added to
949 : : * es_opened_result_relations above, which is wrong because we took no
950 : : * corresponding refcount. We rely on ExecCleanupTupleRouting() to close
951 : : * any other relations opened during execution.
952 : : */
1839 michael@paquier.xyz 953 : 168509 : ExecResetTupleTable(estate->es_tupleTable, false);
954 : 168509 : FreeExecutorState(estate);
1809 tgl@sss.pgh.pa.us 955 : 168509 : pfree(edata);
1839 michael@paquier.xyz 956 : 168509 : }
957 : :
958 : : /*
959 : : * Executes default values for columns for which we can't map to remote
960 : : * relation columns.
961 : : *
962 : : * This allows us to support tables which have more columns on the downstream
963 : : * than on the upstream.
964 : : */
965 : : static void
3393 peter_e@gmx.net 966 : 96286 : slot_fill_defaults(LogicalRepRelMapEntry *rel, EState *estate,
967 : : TupleTableSlot *slot)
968 : : {
969 : 96286 : TupleDesc desc = RelationGetDescr(rel->localrel);
970 : 96286 : int num_phys_attrs = desc->natts;
971 : : int i;
972 : : int attnum,
973 : 96286 : num_defaults = 0;
974 : : int *defmap;
975 : : ExprState **defexprs;
976 : : ExprContext *econtext;
977 : :
978 [ + - ]: 96286 : econtext = GetPerTupleExprContext(estate);
979 : :
980 : : /* We got all the data via replication, no need to evaluate anything. */
981 [ + + ]: 96286 : if (num_phys_attrs == rel->remoterel.natts)
982 : 56141 : return;
983 : :
60 msawada@postgresql.o 984 :GNC 40145 : defmap = palloc_array(int, num_phys_attrs);
985 : 40145 : defexprs = palloc_array(ExprState *, num_phys_attrs);
986 : :
2330 michael@paquier.xyz 987 [ - + ]:CBC 40145 : Assert(rel->attrmap->maplen == num_phys_attrs);
3393 peter_e@gmx.net 988 [ + + ]: 210663 : for (attnum = 0; attnum < num_phys_attrs; attnum++)
989 : : {
195 drowley@postgresql.o 990 :GNC 170518 : CompactAttribute *cattr = TupleDescCompactAttr(desc, attnum);
991 : : Expr *defexpr;
992 : :
993 [ + - + + ]: 170518 : if (cattr->attisdropped || cattr->attgenerated)
3393 peter_e@gmx.net 994 :CBC 9 : continue;
995 : :
2330 michael@paquier.xyz 996 [ + + ]: 170509 : if (rel->attrmap->attnums[attnum] >= 0)
3393 peter_e@gmx.net 997 : 92268 : continue;
998 : :
999 : 78241 : defexpr = (Expr *) build_column_default(rel->localrel, attnum + 1);
1000 : :
1001 [ + + ]: 78241 : if (defexpr != NULL)
1002 : : {
1003 : : /* Run the expression through planner */
1004 : 70131 : defexpr = expression_planner(defexpr);
1005 : :
1006 : : /* Initialize executable expression in copycontext */
1007 : 70131 : defexprs[num_defaults] = ExecInitExpr(defexpr, NULL);
1008 : 70131 : defmap[num_defaults] = attnum;
1009 : 70131 : num_defaults++;
1010 : : }
1011 : : }
1012 : :
1013 [ + + ]: 110276 : for (i = 0; i < num_defaults; i++)
1014 : 70131 : slot->tts_values[defmap[i]] =
1015 : 70131 : ExecEvalExpr(defexprs[i], econtext, &slot->tts_isnull[defmap[i]]);
1016 : : }
1017 : :
1018 : : /*
1019 : : * Store tuple data into slot.
1020 : : *
1021 : : * Incoming data can be either text or binary format.
1022 : : */
1023 : : static void
2117 tgl@sss.pgh.pa.us 1024 : 168569 : slot_store_data(TupleTableSlot *slot, LogicalRepRelMapEntry *rel,
1025 : : LogicalRepTupleData *tupleData)
1026 : : {
3275 bruce@momjian.us 1027 : 168569 : int natts = slot->tts_tupleDescriptor->natts;
1028 : : int i;
1029 : :
3393 peter_e@gmx.net 1030 : 168569 : ExecClearTuple(slot);
1031 : :
1032 : : /* Call the "in" function for each non-dropped, non-null attribute */
2330 michael@paquier.xyz 1033 [ - + ]: 168569 : Assert(natts == rel->attrmap->maplen);
3393 peter_e@gmx.net 1034 [ + + ]: 698809 : for (i = 0; i < natts; i++)
1035 : : {
3180 andres@anarazel.de 1036 : 530240 : Form_pg_attribute att = TupleDescAttr(slot->tts_tupleDescriptor, i);
2330 michael@paquier.xyz 1037 : 530240 : int remoteattnum = rel->attrmap->attnums[i];
1038 : :
2117 tgl@sss.pgh.pa.us 1039 [ + + + + ]: 530240 : if (!att->attisdropped && remoteattnum >= 0)
3393 peter_e@gmx.net 1040 : 323323 : {
2117 tgl@sss.pgh.pa.us 1041 : 323323 : StringInfo colvalue = &tupleData->colvalues[remoteattnum];
1042 : :
2115 1043 [ - + ]: 323323 : Assert(remoteattnum < tupleData->ncols);
1044 : :
1045 : : /* Set attnum for error callback */
1712 akapila@postgresql.o 1046 : 323323 : apply_error_callback_arg.remote_attnum = remoteattnum;
1047 : :
2117 tgl@sss.pgh.pa.us 1048 [ + + ]: 323323 : if (tupleData->colstatus[remoteattnum] == LOGICALREP_COLUMN_TEXT)
1049 : : {
1050 : : Oid typinput;
1051 : : Oid typioparam;
1052 : :
1053 : 162917 : getTypeInputInfo(att->atttypid, &typinput, &typioparam);
1054 : 325834 : slot->tts_values[i] =
1055 : 162917 : OidInputFunctionCall(typinput, colvalue->data,
1056 : : typioparam, att->atttypmod);
1057 : 162917 : slot->tts_isnull[i] = false;
1058 : : }
1059 [ + + ]: 160406 : else if (tupleData->colstatus[remoteattnum] == LOGICALREP_COLUMN_BINARY)
1060 : : {
1061 : : Oid typreceive;
1062 : : Oid typioparam;
1063 : :
1064 : : /*
1065 : : * In some code paths we may be asked to re-parse the same
1066 : : * tuple data. Reset the StringInfo's cursor so that works.
1067 : : */
1068 : 110074 : colvalue->cursor = 0;
1069 : :
1070 : 110074 : getTypeBinaryInputInfo(att->atttypid, &typreceive, &typioparam);
1071 : 220148 : slot->tts_values[i] =
1072 : 110074 : OidReceiveFunctionCall(typreceive, colvalue,
1073 : : typioparam, att->atttypmod);
1074 : :
1075 : : /* Trouble if it didn't eat the whole buffer */
1076 [ - + ]: 110074 : if (colvalue->cursor != colvalue->len)
2117 tgl@sss.pgh.pa.us 1077 [ # # ]:UBC 0 : ereport(ERROR,
1078 : : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1079 : : errmsg("incorrect binary data format in logical replication column %d",
1080 : : remoteattnum + 1)));
2117 tgl@sss.pgh.pa.us 1081 :CBC 110074 : slot->tts_isnull[i] = false;
1082 : : }
1083 : : else
1084 : : {
1085 : : /*
1086 : : * NULL value from remote. (We don't expect to see
1087 : : * LOGICALREP_COLUMN_UNCHANGED here, but if we do, treat it as
1088 : : * NULL.)
1089 : : */
1090 : 50332 : slot->tts_values[i] = (Datum) 0;
1091 : 50332 : slot->tts_isnull[i] = true;
1092 : : }
1093 : :
1094 : : /* Reset attnum for error callback */
1712 akapila@postgresql.o 1095 : 323323 : apply_error_callback_arg.remote_attnum = -1;
1096 : : }
1097 : : else
1098 : : {
1099 : : /*
1100 : : * We assign NULL to dropped attributes and missing values
1101 : : * (missing values should be later filled using
1102 : : * slot_fill_defaults).
1103 : : */
3393 peter_e@gmx.net 1104 : 206917 : slot->tts_values[i] = (Datum) 0;
1105 : 206917 : slot->tts_isnull[i] = true;
1106 : : }
1107 : : }
1108 : :
1109 : 168569 : ExecStoreVirtualTuple(slot);
1110 : 168569 : }
1111 : :
1112 : : /*
1113 : : * Replace updated columns with data from the LogicalRepTupleData struct.
1114 : : * This is somewhat similar to heap_modify_tuple but also calls the type
1115 : : * input functions on the user data.
1116 : : *
1117 : : * "slot" is filled with a copy of the tuple in "srcslot", replacing
1118 : : * columns provided in "tupleData" and leaving others as-is.
1119 : : *
1120 : : * Caution: unreplaced pass-by-ref columns in "slot" will point into the
1121 : : * storage for "srcslot". This is OK for current usage, but someday we may
1122 : : * need to materialize "slot" at the end to make it independent of "srcslot".
1123 : : */
1124 : : static void
2117 tgl@sss.pgh.pa.us 1125 : 31930 : slot_modify_data(TupleTableSlot *slot, TupleTableSlot *srcslot,
1126 : : LogicalRepRelMapEntry *rel,
1127 : : LogicalRepTupleData *tupleData)
1128 : : {
3275 bruce@momjian.us 1129 : 31930 : int natts = slot->tts_tupleDescriptor->natts;
1130 : : int i;
1131 : :
1132 : : /* We'll fill "slot" with a virtual tuple, so we must start with ... */
3393 peter_e@gmx.net 1133 : 31930 : ExecClearTuple(slot);
1134 : :
1135 : : /*
1136 : : * Copy all the column data from srcslot, so that we'll have valid values
1137 : : * for unreplaced columns.
1138 : : */
2356 tgl@sss.pgh.pa.us 1139 [ - + ]: 31930 : Assert(natts == srcslot->tts_tupleDescriptor->natts);
1140 : 31930 : slot_getallattrs(srcslot);
1141 : 31930 : memcpy(slot->tts_values, srcslot->tts_values, natts * sizeof(Datum));
1142 : 31930 : memcpy(slot->tts_isnull, srcslot->tts_isnull, natts * sizeof(bool));
1143 : :
1144 : : /* Call the "in" function for each replaced attribute */
2330 michael@paquier.xyz 1145 [ - + ]: 31930 : Assert(natts == rel->attrmap->maplen);
3393 peter_e@gmx.net 1146 [ + + ]: 159304 : for (i = 0; i < natts; i++)
1147 : : {
3180 andres@anarazel.de 1148 : 127374 : Form_pg_attribute att = TupleDescAttr(slot->tts_tupleDescriptor, i);
2330 michael@paquier.xyz 1149 : 127374 : int remoteattnum = rel->attrmap->attnums[i];
1150 : :
3105 peter_e@gmx.net 1151 [ + + ]: 127374 : if (remoteattnum < 0)
3393 1152 : 58519 : continue;
1153 : :
2115 tgl@sss.pgh.pa.us 1154 [ - + ]: 68855 : Assert(remoteattnum < tupleData->ncols);
1155 : :
2117 1156 [ + - ]: 68855 : if (tupleData->colstatus[remoteattnum] != LOGICALREP_COLUMN_UNCHANGED)
1157 : : {
1158 : 68855 : StringInfo colvalue = &tupleData->colvalues[remoteattnum];
1159 : :
1160 : : /* Set attnum for error callback */
1712 akapila@postgresql.o 1161 : 68855 : apply_error_callback_arg.remote_attnum = remoteattnum;
1162 : :
2117 tgl@sss.pgh.pa.us 1163 [ + + ]: 68855 : if (tupleData->colstatus[remoteattnum] == LOGICALREP_COLUMN_TEXT)
1164 : : {
1165 : : Oid typinput;
1166 : : Oid typioparam;
1167 : :
1168 : 25451 : getTypeInputInfo(att->atttypid, &typinput, &typioparam);
1169 : 50902 : slot->tts_values[i] =
1170 : 25451 : OidInputFunctionCall(typinput, colvalue->data,
1171 : : typioparam, att->atttypmod);
1172 : 25451 : slot->tts_isnull[i] = false;
1173 : : }
1174 [ + + ]: 43404 : else if (tupleData->colstatus[remoteattnum] == LOGICALREP_COLUMN_BINARY)
1175 : : {
1176 : : Oid typreceive;
1177 : : Oid typioparam;
1178 : :
1179 : : /*
1180 : : * In some code paths we may be asked to re-parse the same
1181 : : * tuple data. Reset the StringInfo's cursor so that works.
1182 : : */
1183 : 43356 : colvalue->cursor = 0;
1184 : :
1185 : 43356 : getTypeBinaryInputInfo(att->atttypid, &typreceive, &typioparam);
1186 : 86712 : slot->tts_values[i] =
1187 : 43356 : OidReceiveFunctionCall(typreceive, colvalue,
1188 : : typioparam, att->atttypmod);
1189 : :
1190 : : /* Trouble if it didn't eat the whole buffer */
1191 [ - + ]: 43356 : if (colvalue->cursor != colvalue->len)
2117 tgl@sss.pgh.pa.us 1192 [ # # ]:UBC 0 : ereport(ERROR,
1193 : : (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
1194 : : errmsg("incorrect binary data format in logical replication column %d",
1195 : : remoteattnum + 1)));
2117 tgl@sss.pgh.pa.us 1196 :CBC 43356 : slot->tts_isnull[i] = false;
1197 : : }
1198 : : else
1199 : : {
1200 : : /* must be LOGICALREP_COLUMN_NULL */
1201 : 48 : slot->tts_values[i] = (Datum) 0;
1202 : 48 : slot->tts_isnull[i] = true;
1203 : : }
1204 : :
1205 : : /* Reset attnum for error callback */
1712 akapila@postgresql.o 1206 : 68855 : apply_error_callback_arg.remote_attnum = -1;
1207 : : }
1208 : : }
1209 : :
1210 : : /* And finally, declare that "slot" contains a valid virtual tuple */
3393 peter_e@gmx.net 1211 : 31930 : ExecStoreVirtualTuple(slot);
1212 : 31930 : }
1213 : :
1214 : : /*
1215 : : * Handle BEGIN message.
1216 : : */
1217 : : static void
1218 : 521 : apply_handle_begin(StringInfo s)
1219 : : {
1220 : : LogicalRepBeginData begin_data;
1221 : :
1222 : : /* There must not be an active streaming transaction. */
1204 akapila@postgresql.o 1223 [ - + ]: 521 : Assert(!TransactionIdIsValid(stream_xid));
1224 : :
3393 peter_e@gmx.net 1225 : 521 : logicalrep_read_begin(s, &begin_data);
1519 akapila@postgresql.o 1226 : 521 : set_apply_error_context_xact(begin_data.xid, begin_data.final_lsn);
1227 : :
3330 peter_e@gmx.net 1228 : 521 : remote_final_lsn = begin_data.final_lsn;
1229 : :
1505 akapila@postgresql.o 1230 : 521 : maybe_start_skipping_changes(begin_data.final_lsn);
1231 : :
3393 peter_e@gmx.net 1232 : 521 : in_remote_transaction = true;
1233 : :
1234 : 521 : pgstat_report_activity(STATE_RUNNING, NULL);
1235 : 521 : }
1236 : :
1237 : : /*
1238 : : * Handle COMMIT message.
1239 : : *
1240 : : * TODO, support tracking of multiple origins
1241 : : */
1242 : : static void
1243 : 461 : apply_handle_commit(StringInfo s)
1244 : : {
1245 : : LogicalRepCommitData commit_data;
1246 : :
1247 : 461 : logicalrep_read_commit(s, &commit_data);
1248 : :
1788 tgl@sss.pgh.pa.us 1249 [ - + ]: 461 : if (commit_data.commit_lsn != remote_final_lsn)
1788 tgl@sss.pgh.pa.us 1250 [ # # ]:UBC 0 : ereport(ERROR,
1251 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
1252 : : errmsg_internal("incorrect commit LSN %X/%08X in commit message (expected %X/%08X)",
1253 : : LSN_FORMAT_ARGS(commit_data.commit_lsn),
1254 : : LSN_FORMAT_ARGS(remote_final_lsn))));
1255 : :
1740 akapila@postgresql.o 1256 :CBC 461 : apply_handle_commit_internal(&commit_data);
1257 : :
1258 : : /*
1259 : : * Process any tables that are being synchronized in parallel, as well as
1260 : : * any newly added tables or sequences.
1261 : : */
201 akapila@postgresql.o 1262 :GNC 461 : ProcessSyncingRelations(commit_data.end_lsn);
1263 : :
3393 peter_e@gmx.net 1264 :CBC 461 : pgstat_report_activity(STATE_IDLE, NULL);
1712 akapila@postgresql.o 1265 : 461 : reset_apply_error_context_info();
3393 peter_e@gmx.net 1266 : 461 : }
1267 : :
1268 : : /*
1269 : : * Handle BEGIN PREPARE message.
1270 : : */
1271 : : static void
1756 akapila@postgresql.o 1272 : 17 : apply_handle_begin_prepare(StringInfo s)
1273 : : {
1274 : : LogicalRepPreparedTxnData begin_data;
1275 : :
1276 : : /* Tablesync should never receive prepare. */
1277 [ - + ]: 17 : if (am_tablesync_worker())
1756 akapila@postgresql.o 1278 [ # # ]:UBC 0 : ereport(ERROR,
1279 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
1280 : : errmsg_internal("tablesync worker received a BEGIN PREPARE message")));
1281 : :
1282 : : /* There must not be an active streaming transaction. */
1204 akapila@postgresql.o 1283 [ - + ]:CBC 17 : Assert(!TransactionIdIsValid(stream_xid));
1284 : :
1756 1285 : 17 : logicalrep_read_begin_prepare(s, &begin_data);
1519 1286 : 17 : set_apply_error_context_xact(begin_data.xid, begin_data.prepare_lsn);
1287 : :
1756 1288 : 17 : remote_final_lsn = begin_data.prepare_lsn;
1289 : :
1505 1290 : 17 : maybe_start_skipping_changes(begin_data.prepare_lsn);
1291 : :
1756 1292 : 17 : in_remote_transaction = true;
1293 : :
1294 : 17 : pgstat_report_activity(STATE_RUNNING, NULL);
1295 : 17 : }
1296 : :
1297 : : /*
1298 : : * Common function to prepare the GID.
1299 : : */
1300 : : static void
1741 1301 : 26 : apply_handle_prepare_internal(LogicalRepPreparedTxnData *prepare_data)
1302 : : {
1303 : : char gid[GIDSIZE];
1304 : :
1305 : : /*
1306 : : * Compute unique GID for two_phase transactions. We don't use GID of
1307 : : * prepared transaction sent by server as that can lead to deadlock when
1308 : : * we have multiple subscriptions from same node point to publications on
1309 : : * the same node. See comments atop worker.c
1310 : : */
1311 : 26 : TwoPhaseTransactionGid(MySubscription->oid, prepare_data->xid,
1312 : : gid, sizeof(gid));
1313 : :
1314 : : /*
1315 : : * BeginTransactionBlock is necessary to balance the EndTransactionBlock
1316 : : * called within the PrepareTransactionBlock below.
1317 : : */
1212 1318 [ + - ]: 26 : if (!IsTransactionBlock())
1319 : : {
1320 : 26 : BeginTransactionBlock();
1321 : 26 : CommitTransactionCommand(); /* Completes the preceding Begin command. */
1322 : : }
1323 : :
1324 : : /*
1325 : : * Update origin state so we can restart streaming from correct position
1326 : : * in case of crash.
1327 : : */
97 msawada@postgresql.o 1328 :GNC 26 : replorigin_xact_state.origin_lsn = prepare_data->end_lsn;
1329 : 26 : replorigin_xact_state.origin_timestamp = prepare_data->prepare_time;
1330 : :
1741 akapila@postgresql.o 1331 :CBC 26 : PrepareTransactionBlock(gid);
1332 : 26 : }
1333 : :
1334 : : /*
1335 : : * Handle PREPARE message.
1336 : : */
1337 : : static void
1756 1338 : 16 : apply_handle_prepare(StringInfo s)
1339 : : {
1340 : : LogicalRepPreparedTxnData prepare_data;
1341 : :
1342 : 16 : logicalrep_read_prepare(s, &prepare_data);
1343 : :
1344 [ - + ]: 16 : if (prepare_data.prepare_lsn != remote_final_lsn)
1756 akapila@postgresql.o 1345 [ # # ]:UBC 0 : ereport(ERROR,
1346 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
1347 : : errmsg_internal("incorrect prepare LSN %X/%08X in prepare message (expected %X/%08X)",
1348 : : LSN_FORMAT_ARGS(prepare_data.prepare_lsn),
1349 : : LSN_FORMAT_ARGS(remote_final_lsn))));
1350 : :
1351 : : /*
1352 : : * Unlike commit, here, we always prepare the transaction even though no
1353 : : * change has happened in this transaction or all changes are skipped. It
1354 : : * is done this way because at commit prepared time, we won't know whether
1355 : : * we have skipped preparing a transaction because of those reasons.
1356 : : *
1357 : : * XXX, We can optimize such that at commit prepared time, we first check
1358 : : * whether we have prepared the transaction or not but that doesn't seem
1359 : : * worthwhile because such cases shouldn't be common.
1360 : : */
1756 akapila@postgresql.o 1361 :CBC 16 : begin_replication_step();
1362 : :
1741 1363 : 16 : apply_handle_prepare_internal(&prepare_data);
1364 : :
1756 1365 : 16 : end_replication_step();
1366 : 16 : CommitTransactionCommand();
1367 : 15 : pgstat_report_stat(false);
1368 : :
1369 : : /*
1370 : : * It is okay not to set the local_end LSN for the prepare because we
1371 : : * always flush the prepare record. So, we can send the acknowledgment of
1372 : : * the remote_end LSN as soon as prepare is finished.
1373 : : *
1374 : : * XXX For the sake of consistency with commit, we could have set it with
1375 : : * the LSN of prepare but as of now we don't track that value similar to
1376 : : * XactLastCommitEnd, and adding it for this purpose doesn't seems worth
1377 : : * it.
1378 : : */
634 1379 : 15 : store_flush_position(prepare_data.end_lsn, InvalidXLogRecPtr);
1380 : :
1756 1381 : 15 : in_remote_transaction = false;
1382 : :
1383 : : /*
1384 : : * Process any tables that are being synchronized in parallel, as well as
1385 : : * any newly added tables or sequences.
1386 : : */
201 akapila@postgresql.o 1387 :GNC 15 : ProcessSyncingRelations(prepare_data.end_lsn);
1388 : :
1389 : : /*
1390 : : * Since we have already prepared the transaction, in a case where the
1391 : : * server crashes before clearing the subskiplsn, it will be left but the
1392 : : * transaction won't be resent. But that's okay because it's a rare case
1393 : : * and the subskiplsn will be cleared when finishing the next transaction.
1394 : : */
1505 akapila@postgresql.o 1395 :CBC 15 : stop_skipping_changes();
1396 : 15 : clear_subscription_skip_lsn(prepare_data.prepare_lsn);
1397 : :
1756 1398 : 15 : pgstat_report_activity(STATE_IDLE, NULL);
1712 1399 : 15 : reset_apply_error_context_info();
1756 1400 : 15 : }
1401 : :
1402 : : /*
1403 : : * Handle a COMMIT PREPARED of a previously PREPARED transaction.
1404 : : *
1405 : : * Note that we don't need to wait here if the transaction was prepared in a
1406 : : * parallel apply worker. In that case, we have already waited for the prepare
1407 : : * to finish in apply_handle_stream_prepare() which will ensure all the
1408 : : * operations in that transaction have happened in the subscriber, so no
1409 : : * concurrent transaction can cause deadlock or transaction dependency issues.
1410 : : */
1411 : : static void
1412 : 22 : apply_handle_commit_prepared(StringInfo s)
1413 : : {
1414 : : LogicalRepCommitPreparedTxnData prepare_data;
1415 : : char gid[GIDSIZE];
1416 : :
1417 : 22 : logicalrep_read_commit_prepared(s, &prepare_data);
1519 1418 : 22 : set_apply_error_context_xact(prepare_data.xid, prepare_data.commit_lsn);
1419 : :
1420 : : /* Compute GID for two_phase transactions. */
1756 1421 : 22 : TwoPhaseTransactionGid(MySubscription->oid, prepare_data.xid,
1422 : : gid, sizeof(gid));
1423 : :
1424 : : /* There is no transaction when COMMIT PREPARED is called */
1425 : 22 : begin_replication_step();
1426 : :
1427 : : /*
1428 : : * Update origin state so we can restart streaming from correct position
1429 : : * in case of crash.
1430 : : */
97 msawada@postgresql.o 1431 :GNC 22 : replorigin_xact_state.origin_lsn = prepare_data.end_lsn;
1432 : 22 : replorigin_xact_state.origin_timestamp = prepare_data.commit_time;
1433 : :
1756 akapila@postgresql.o 1434 :CBC 22 : FinishPreparedTransaction(gid, true);
1435 : 22 : end_replication_step();
1436 : 22 : CommitTransactionCommand();
1437 : 22 : pgstat_report_stat(false);
1438 : :
1212 1439 : 22 : store_flush_position(prepare_data.end_lsn, XactLastCommitEnd);
1756 1440 : 22 : in_remote_transaction = false;
1441 : :
1442 : : /*
1443 : : * Process any tables that are being synchronized in parallel, as well as
1444 : : * any newly added tables or sequences.
1445 : : */
201 akapila@postgresql.o 1446 :GNC 22 : ProcessSyncingRelations(prepare_data.end_lsn);
1447 : :
1505 akapila@postgresql.o 1448 :CBC 22 : clear_subscription_skip_lsn(prepare_data.end_lsn);
1449 : :
1756 1450 : 22 : pgstat_report_activity(STATE_IDLE, NULL);
1712 1451 : 22 : reset_apply_error_context_info();
1756 1452 : 22 : }
1453 : :
1454 : : /*
1455 : : * Handle a ROLLBACK PREPARED of a previously PREPARED TRANSACTION.
1456 : : *
1457 : : * Note that we don't need to wait here if the transaction was prepared in a
1458 : : * parallel apply worker. In that case, we have already waited for the prepare
1459 : : * to finish in apply_handle_stream_prepare() which will ensure all the
1460 : : * operations in that transaction have happened in the subscriber, so no
1461 : : * concurrent transaction can cause deadlock or transaction dependency issues.
1462 : : */
1463 : : static void
1464 : 5 : apply_handle_rollback_prepared(StringInfo s)
1465 : : {
1466 : : LogicalRepRollbackPreparedTxnData rollback_data;
1467 : : char gid[GIDSIZE];
1468 : :
1469 : 5 : logicalrep_read_rollback_prepared(s, &rollback_data);
1519 1470 : 5 : set_apply_error_context_xact(rollback_data.xid, rollback_data.rollback_end_lsn);
1471 : :
1472 : : /* Compute GID for two_phase transactions. */
1756 1473 : 5 : TwoPhaseTransactionGid(MySubscription->oid, rollback_data.xid,
1474 : : gid, sizeof(gid));
1475 : :
1476 : : /*
1477 : : * It is possible that we haven't received prepare because it occurred
1478 : : * before walsender reached a consistent point or the two_phase was still
1479 : : * not enabled by that time, so in such cases, we need to skip rollback
1480 : : * prepared.
1481 : : */
1482 [ + - ]: 5 : if (LookupGXact(gid, rollback_data.prepare_end_lsn,
1483 : : rollback_data.prepare_time))
1484 : : {
1485 : : /*
1486 : : * Update origin state so we can restart streaming from correct
1487 : : * position in case of crash.
1488 : : */
97 msawada@postgresql.o 1489 :GNC 5 : replorigin_xact_state.origin_lsn = rollback_data.rollback_end_lsn;
1490 : 5 : replorigin_xact_state.origin_timestamp = rollback_data.rollback_time;
1491 : :
1492 : : /* There is no transaction when ABORT/ROLLBACK PREPARED is called */
1756 akapila@postgresql.o 1493 :CBC 5 : begin_replication_step();
1494 : 5 : FinishPreparedTransaction(gid, false);
1495 : 5 : end_replication_step();
1496 : 5 : CommitTransactionCommand();
1497 : :
1505 1498 : 5 : clear_subscription_skip_lsn(rollback_data.rollback_end_lsn);
1499 : : }
1500 : :
1756 1501 : 5 : pgstat_report_stat(false);
1502 : :
1503 : : /*
1504 : : * It is okay not to set the local_end LSN for the rollback of prepared
1505 : : * transaction because we always flush the WAL record for it. See
1506 : : * apply_handle_prepare.
1507 : : */
634 1508 : 5 : store_flush_position(rollback_data.rollback_end_lsn, InvalidXLogRecPtr);
1756 1509 : 5 : in_remote_transaction = false;
1510 : :
1511 : : /*
1512 : : * Process any tables that are being synchronized in parallel, as well as
1513 : : * any newly added tables or sequences.
1514 : : */
201 akapila@postgresql.o 1515 :GNC 5 : ProcessSyncingRelations(rollback_data.rollback_end_lsn);
1516 : :
1756 akapila@postgresql.o 1517 :CBC 5 : pgstat_report_activity(STATE_IDLE, NULL);
1712 1518 : 5 : reset_apply_error_context_info();
1756 1519 : 5 : }
1520 : :
1521 : : /*
1522 : : * Handle STREAM PREPARE.
1523 : : */
1524 : : static void
1735 1525 : 15 : apply_handle_stream_prepare(StringInfo s)
1526 : : {
1527 : : LogicalRepPreparedTxnData prepare_data;
1528 : : ParallelApplyWorkerInfo *winfo;
1529 : : TransApplyAction apply_action;
1530 : :
1531 : : /* Save the message before it is consumed. */
1212 1532 : 15 : StringInfoData original_msg = *s;
1533 : :
1735 1534 [ - + ]: 15 : if (in_streamed_transaction)
1735 akapila@postgresql.o 1535 [ # # ]:UBC 0 : ereport(ERROR,
1536 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
1537 : : errmsg_internal("STREAM PREPARE message without STREAM STOP")));
1538 : :
1539 : : /* Tablesync should never receive prepare. */
1735 akapila@postgresql.o 1540 [ - + ]:CBC 15 : if (am_tablesync_worker())
1735 akapila@postgresql.o 1541 [ # # ]:UBC 0 : ereport(ERROR,
1542 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
1543 : : errmsg_internal("tablesync worker received a STREAM PREPARE message")));
1544 : :
1735 akapila@postgresql.o 1545 :CBC 15 : logicalrep_read_stream_prepare(s, &prepare_data);
1519 1546 : 15 : set_apply_error_context_xact(prepare_data.xid, prepare_data.prepare_lsn);
1547 : :
1212 1548 : 15 : apply_action = get_transaction_apply_action(prepare_data.xid, &winfo);
1549 : :
1550 [ + + + + : 15 : switch (apply_action)
- ]
1551 : : {
1204 1552 : 5 : case TRANS_LEADER_APPLY:
1553 : :
1554 : : /*
1555 : : * The transaction has been serialized to file, so replay all the
1556 : : * spooled operations.
1557 : : */
1212 1558 : 5 : apply_spooled_messages(MyLogicalRepWorker->stream_fileset,
1559 : : prepare_data.xid, prepare_data.prepare_lsn);
1560 : :
1561 : : /* Mark the transaction as prepared. */
1562 : 5 : apply_handle_prepare_internal(&prepare_data);
1563 : :
1564 : 5 : CommitTransactionCommand();
1565 : :
1566 : : /*
1567 : : * It is okay not to set the local_end LSN for the prepare because
1568 : : * we always flush the prepare record. See apply_handle_prepare.
1569 : : */
634 1570 : 5 : store_flush_position(prepare_data.end_lsn, InvalidXLogRecPtr);
1571 : :
1212 1572 : 5 : in_remote_transaction = false;
1573 : :
1574 : : /* Unlink the files with serialized changes and subxact info. */
1575 : 5 : stream_cleanup_files(MyLogicalRepWorker->subid, prepare_data.xid);
1576 : :
1577 [ - + ]: 5 : elog(DEBUG1, "finished processing the STREAM PREPARE command");
1578 : 5 : break;
1579 : :
1580 : 4 : case TRANS_LEADER_SEND_TO_PARALLEL:
1581 [ - + ]: 4 : Assert(winfo);
1582 : :
1583 [ + - ]: 4 : if (pa_send_data(winfo, s->len, s->data))
1584 : : {
1585 : : /* Finish processing the streaming transaction. */
1586 : 4 : pa_xact_finish(winfo, prepare_data.end_lsn);
1587 : 3 : break;
1588 : : }
1589 : :
1590 : : /*
1591 : : * Switch to serialize mode when we are not able to send the
1592 : : * change to parallel apply worker.
1593 : : */
1212 akapila@postgresql.o 1594 :UBC 0 : pa_switch_to_partial_serialize(winfo, true);
1595 : :
1596 : : pg_fallthrough;
1212 akapila@postgresql.o 1597 :CBC 1 : case TRANS_LEADER_PARTIAL_SERIALIZE:
1598 [ - + ]: 1 : Assert(winfo);
1599 : :
1600 : 1 : stream_open_and_write_change(prepare_data.xid,
1601 : : LOGICAL_REP_MSG_STREAM_PREPARE,
1602 : : &original_msg);
1603 : :
1604 : 1 : pa_set_fileset_state(winfo->shared, FS_SERIALIZE_DONE);
1605 : :
1606 : : /* Finish processing the streaming transaction. */
1607 : 1 : pa_xact_finish(winfo, prepare_data.end_lsn);
1608 : 1 : break;
1609 : :
1610 : 5 : case TRANS_PARALLEL_APPLY:
1611 : :
1612 : : /*
1613 : : * If the parallel apply worker is applying spooled messages then
1614 : : * close the file before preparing.
1615 : : */
1616 [ + + ]: 5 : if (stream_fd)
1617 : 1 : stream_close_file();
1618 : :
1619 : 5 : begin_replication_step();
1620 : :
1621 : : /* Mark the transaction as prepared. */
1622 : 5 : apply_handle_prepare_internal(&prepare_data);
1623 : :
1624 : 5 : end_replication_step();
1625 : :
1626 : 5 : CommitTransactionCommand();
1627 : :
1628 : : /*
1629 : : * It is okay not to set the local_end LSN for the prepare because
1630 : : * we always flush the prepare record. See apply_handle_prepare.
1631 : : */
634 1632 : 4 : MyParallelShared->last_commit_end = InvalidXLogRecPtr;
1633 : :
1212 1634 : 4 : pa_set_xact_state(MyParallelShared, PARALLEL_TRANS_FINISHED);
1635 : 4 : pa_unlock_transaction(MyParallelShared->xid, AccessExclusiveLock);
1636 : :
1637 : 4 : pa_reset_subtrans();
1638 : :
1639 [ + + ]: 4 : elog(DEBUG1, "finished processing the STREAM PREPARE command");
1640 : 4 : break;
1641 : :
1212 akapila@postgresql.o 1642 :UBC 0 : default:
1204 1643 [ # # ]: 0 : elog(ERROR, "unexpected apply action: %d", (int) apply_action);
1644 : : break;
1645 : : }
1646 : :
1212 akapila@postgresql.o 1647 :CBC 13 : pgstat_report_stat(false);
1648 : :
1649 : : /*
1650 : : * Process any tables that are being synchronized in parallel, as well as
1651 : : * any newly added tables or sequences.
1652 : : */
201 akapila@postgresql.o 1653 :GNC 13 : ProcessSyncingRelations(prepare_data.end_lsn);
1654 : :
1655 : : /*
1656 : : * Similar to prepare case, the subskiplsn could be left in a case of
1657 : : * server crash but it's okay. See the comments in apply_handle_prepare().
1658 : : */
1505 akapila@postgresql.o 1659 :CBC 13 : stop_skipping_changes();
1660 : 13 : clear_subscription_skip_lsn(prepare_data.prepare_lsn);
1661 : :
1735 1662 : 13 : pgstat_report_activity(STATE_IDLE, NULL);
1663 : :
1712 1664 : 13 : reset_apply_error_context_info();
1735 1665 : 13 : }
1666 : :
1667 : : /*
1668 : : * Handle ORIGIN message.
1669 : : *
1670 : : * TODO, support tracking of multiple origins
1671 : : */
1672 : : static void
3393 peter_e@gmx.net 1673 : 9 : apply_handle_origin(StringInfo s)
1674 : : {
1675 : : /*
1676 : : * ORIGIN message can only come inside streaming transaction or inside
1677 : : * remote transaction and before any actual writes.
1678 : : */
2070 akapila@postgresql.o 1679 [ + + ]: 9 : if (!in_streamed_transaction &&
1680 [ + - - + ]: 14 : (!in_remote_transaction ||
1681 [ - - ]: 7 : (IsTransactionState() && !am_tablesync_worker())))
3393 peter_e@gmx.net 1682 [ # # ]:UBC 0 : ereport(ERROR,
1683 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
1684 : : errmsg_internal("ORIGIN message sent out of order")));
3393 peter_e@gmx.net 1685 :CBC 9 : }
1686 : :
1687 : : /*
1688 : : * Initialize fileset (if not already done).
1689 : : *
1690 : : * Create a new file when first_segment is true, otherwise open the existing
1691 : : * file.
1692 : : */
1693 : : void
1212 akapila@postgresql.o 1694 : 362 : stream_start_internal(TransactionId xid, bool first_segment)
1695 : : {
1696 : 362 : begin_replication_step();
1697 : :
1698 : : /*
1699 : : * Initialize the worker's stream_fileset if we haven't yet. This will be
1700 : : * used for the entire duration of the worker so create it in a permanent
1701 : : * context. We create this on the very first streaming message from any
1702 : : * transaction and then use it for this and other streaming transactions.
1703 : : * Now, we could create a fileset at the start of the worker as well but
1704 : : * then we won't be sure that it will ever be used.
1705 : : */
1706 [ + + ]: 362 : if (!MyLogicalRepWorker->stream_fileset)
1707 : : {
1708 : : MemoryContext oldctx;
1709 : :
1710 : 14 : oldctx = MemoryContextSwitchTo(ApplyContext);
1711 : :
146 michael@paquier.xyz 1712 :GNC 14 : MyLogicalRepWorker->stream_fileset = palloc_object(FileSet);
1212 akapila@postgresql.o 1713 :CBC 14 : FileSetInit(MyLogicalRepWorker->stream_fileset);
1714 : :
1715 : 14 : MemoryContextSwitchTo(oldctx);
1716 : : }
1717 : :
1718 : : /* Open the spool file for this transaction. */
1719 : 362 : stream_open_file(MyLogicalRepWorker->subid, xid, first_segment);
1720 : :
1721 : : /* If this is not the first segment, open existing subxact file. */
1722 [ + + ]: 362 : if (!first_segment)
1723 : 330 : subxact_info_read(MyLogicalRepWorker->subid, xid);
1724 : :
1725 : 362 : end_replication_step();
1726 : 362 : }
1727 : :
1728 : : /*
1729 : : * Handle STREAM START message.
1730 : : */
1731 : : static void
2070 1732 : 842 : apply_handle_stream_start(StringInfo s)
1733 : : {
1734 : : bool first_segment;
1735 : : ParallelApplyWorkerInfo *winfo;
1736 : : TransApplyAction apply_action;
1737 : :
1738 : : /* Save the message before it is consumed. */
1212 1739 : 842 : StringInfoData original_msg = *s;
1740 : :
1788 tgl@sss.pgh.pa.us 1741 [ - + ]: 842 : if (in_streamed_transaction)
1788 tgl@sss.pgh.pa.us 1742 [ # # ]:UBC 0 : ereport(ERROR,
1743 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
1744 : : errmsg_internal("duplicate STREAM START message")));
1745 : :
1746 : : /* There must not be an active streaming transaction. */
1204 akapila@postgresql.o 1747 [ - + ]:CBC 842 : Assert(!TransactionIdIsValid(stream_xid));
1748 : :
1749 : : /* notify handle methods we're processing a remote transaction */
2070 1750 : 842 : in_streamed_transaction = true;
1751 : :
1752 : : /* extract XID of the top-level transaction */
1753 : 842 : stream_xid = logicalrep_read_stream_start(s, &first_segment);
1754 : :
1788 tgl@sss.pgh.pa.us 1755 [ - + ]: 842 : if (!TransactionIdIsValid(stream_xid))
1788 tgl@sss.pgh.pa.us 1756 [ # # ]:UBC 0 : ereport(ERROR,
1757 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
1758 : : errmsg_internal("invalid transaction ID in streamed replication transaction")));
1759 : :
1519 akapila@postgresql.o 1760 :CBC 842 : set_apply_error_context_xact(stream_xid, InvalidXLogRecPtr);
1761 : :
1762 : : /* Try to allocate a worker for the streaming transaction. */
1212 1763 [ + + ]: 842 : if (first_segment)
1764 : 86 : pa_allocate_worker(stream_xid);
1765 : :
1766 : 842 : apply_action = get_transaction_apply_action(stream_xid, &winfo);
1767 : :
1768 [ + + + + : 842 : switch (apply_action)
- ]
1769 : : {
1770 : 342 : case TRANS_LEADER_SERIALIZE:
1771 : :
1772 : : /*
1773 : : * Function stream_start_internal starts a transaction. This
1774 : : * transaction will be committed on the stream stop unless it is a
1775 : : * tablesync worker in which case it will be committed after
1776 : : * processing all the messages. We need this transaction for
1777 : : * handling the BufFile, used for serializing the streaming data
1778 : : * and subxact info.
1779 : : */
1780 : 342 : stream_start_internal(stream_xid, first_segment);
1781 : 342 : break;
1782 : :
1783 : 244 : case TRANS_LEADER_SEND_TO_PARALLEL:
1784 [ - + ]: 244 : Assert(winfo);
1785 : :
1786 : : /*
1787 : : * Once we start serializing the changes, the parallel apply
1788 : : * worker will wait for the leader to release the stream lock
1789 : : * until the end of the transaction. So, we don't need to release
1790 : : * the lock or increment the stream count in that case.
1791 : : */
1792 [ + + ]: 244 : if (pa_send_data(winfo, s->len, s->data))
1793 : : {
1794 : : /*
1795 : : * Unlock the shared object lock so that the parallel apply
1796 : : * worker can continue to receive changes.
1797 : : */
1798 [ + + ]: 240 : if (!first_segment)
1799 : 215 : pa_unlock_stream(winfo->shared->xid, AccessExclusiveLock);
1800 : :
1801 : : /*
1802 : : * Increment the number of streaming blocks waiting to be
1803 : : * processed by parallel apply worker.
1804 : : */
1805 : 240 : pg_atomic_add_fetch_u32(&winfo->shared->pending_stream_count, 1);
1806 : :
1807 : : /* Cache the parallel apply worker for this transaction. */
1808 : 240 : pa_set_stream_apply_worker(winfo);
1809 : 240 : break;
1810 : : }
1811 : :
1812 : : /*
1813 : : * Switch to serialize mode when we are not able to send the
1814 : : * change to parallel apply worker.
1815 : : */
1816 : 4 : pa_switch_to_partial_serialize(winfo, !first_segment);
1817 : :
1818 : : pg_fallthrough;
1819 : 15 : case TRANS_LEADER_PARTIAL_SERIALIZE:
1820 [ - + ]: 15 : Assert(winfo);
1821 : :
1822 : : /*
1823 : : * Open the spool file unless it was already opened when switching
1824 : : * to serialize mode. The transaction started in
1825 : : * stream_start_internal will be committed on the stream stop.
1826 : : */
1827 [ + + ]: 15 : if (apply_action != TRANS_LEADER_SEND_TO_PARALLEL)
1828 : 11 : stream_start_internal(stream_xid, first_segment);
1829 : :
1830 : 15 : stream_write_change(LOGICAL_REP_MSG_STREAM_START, &original_msg);
1831 : :
1832 : : /* Cache the parallel apply worker for this transaction. */
1833 : 15 : pa_set_stream_apply_worker(winfo);
1834 : 15 : break;
1835 : :
1836 : 245 : case TRANS_PARALLEL_APPLY:
1837 [ + + ]: 245 : if (first_segment)
1838 : : {
1839 : : /* Hold the lock until the end of the transaction. */
1840 : 29 : pa_lock_transaction(MyParallelShared->xid, AccessExclusiveLock);
1841 : 29 : pa_set_xact_state(MyParallelShared, PARALLEL_TRANS_STARTED);
1842 : :
1843 : : /*
1844 : : * Signal the leader apply worker, as it may be waiting for
1845 : : * us.
1846 : : */
189 akapila@postgresql.o 1847 :GNC 29 : logicalrep_worker_wakeup(WORKERTYPE_APPLY,
1848 : 29 : MyLogicalRepWorker->subid, InvalidOid);
1849 : : }
1850 : :
1212 akapila@postgresql.o 1851 :CBC 245 : parallel_stream_nchanges = 0;
1852 : 245 : break;
1853 : :
1212 akapila@postgresql.o 1854 :UBC 0 : default:
1204 1855 [ # # ]: 0 : elog(ERROR, "unexpected apply action: %d", (int) apply_action);
1856 : : break;
1857 : : }
1858 : :
1212 akapila@postgresql.o 1859 :CBC 842 : pgstat_report_activity(STATE_RUNNING, NULL);
2070 1860 : 842 : }
1861 : :
1862 : : /*
1863 : : * Update the information about subxacts and close the file.
1864 : : *
1865 : : * This function should be called when the stream_start_internal function has
1866 : : * been called.
1867 : : */
1868 : : void
1212 1869 : 362 : stream_stop_internal(TransactionId xid)
1870 : : {
1871 : : /*
1872 : : * Serialize information about subxacts for the toplevel transaction, then
1873 : : * close the stream messages spool file.
1874 : : */
1875 : 362 : subxact_info_write(MyLogicalRepWorker->subid, xid);
2070 1876 : 362 : stream_close_file();
1877 : :
1878 : : /* We must be in a valid transaction state */
1879 [ - + ]: 362 : Assert(IsTransactionState());
1880 : :
1881 : : /* Commit the per-stream transaction */
1908 1882 : 362 : CommitTransactionCommand();
1883 : :
1884 : : /* Reset per-stream context */
2070 1885 : 362 : MemoryContextReset(LogicalStreamingContext);
1886 : 362 : }
1887 : :
1888 : : /*
1889 : : * Handle STREAM STOP message.
1890 : : */
1891 : : static void
1212 1892 : 841 : apply_handle_stream_stop(StringInfo s)
1893 : : {
1894 : : ParallelApplyWorkerInfo *winfo;
1895 : : TransApplyAction apply_action;
1896 : :
1897 [ - + ]: 841 : if (!in_streamed_transaction)
1788 tgl@sss.pgh.pa.us 1898 [ # # ]:UBC 0 : ereport(ERROR,
1899 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
1900 : : errmsg_internal("STREAM STOP message without STREAM START")));
1901 : :
1212 akapila@postgresql.o 1902 :CBC 841 : apply_action = get_transaction_apply_action(stream_xid, &winfo);
1903 : :
1904 [ + + + + : 841 : switch (apply_action)
- ]
1905 : : {
1906 : 342 : case TRANS_LEADER_SERIALIZE:
1907 : 342 : stream_stop_internal(stream_xid);
1908 : 342 : break;
1909 : :
1910 : 240 : case TRANS_LEADER_SEND_TO_PARALLEL:
1911 [ - + ]: 240 : Assert(winfo);
1912 : :
1913 : : /*
1914 : : * Lock before sending the STREAM_STOP message so that the leader
1915 : : * can hold the lock first and the parallel apply worker will wait
1916 : : * for leader to release the lock. See Locking Considerations atop
1917 : : * applyparallelworker.c.
1918 : : */
1919 : 240 : pa_lock_stream(winfo->shared->xid, AccessExclusiveLock);
1920 : :
1921 [ + - ]: 240 : if (pa_send_data(winfo, s->len, s->data))
1922 : : {
1923 : 240 : pa_set_stream_apply_worker(NULL);
1924 : 240 : break;
1925 : : }
1926 : :
1927 : : /*
1928 : : * Switch to serialize mode when we are not able to send the
1929 : : * change to parallel apply worker.
1930 : : */
1212 akapila@postgresql.o 1931 :UBC 0 : pa_switch_to_partial_serialize(winfo, true);
1932 : :
1933 : : pg_fallthrough;
1212 akapila@postgresql.o 1934 :CBC 15 : case TRANS_LEADER_PARTIAL_SERIALIZE:
1935 : 15 : stream_write_change(LOGICAL_REP_MSG_STREAM_STOP, s);
1936 : 15 : stream_stop_internal(stream_xid);
1937 : 15 : pa_set_stream_apply_worker(NULL);
1938 : 15 : break;
1939 : :
1940 : 244 : case TRANS_PARALLEL_APPLY:
1941 [ + + ]: 244 : elog(DEBUG1, "applied %u changes in the streaming chunk",
1942 : : parallel_stream_nchanges);
1943 : :
1944 : : /*
1945 : : * By the time parallel apply worker is processing the changes in
1946 : : * the current streaming block, the leader apply worker may have
1947 : : * sent multiple streaming blocks. This can lead to parallel apply
1948 : : * worker start waiting even when there are more chunk of streams
1949 : : * in the queue. So, try to lock only if there is no message left
1950 : : * in the queue. See Locking Considerations atop
1951 : : * applyparallelworker.c.
1952 : : *
1953 : : * Note that here we have a race condition where we can start
1954 : : * waiting even when there are pending streaming chunks. This can
1955 : : * happen if the leader sends another streaming block and acquires
1956 : : * the stream lock again after the parallel apply worker checks
1957 : : * that there is no pending streaming block and before it actually
1958 : : * starts waiting on a lock. We can handle this case by not
1959 : : * allowing the leader to increment the stream block count during
1960 : : * the time parallel apply worker acquires the lock but it is not
1961 : : * clear whether that is worth the complexity.
1962 : : *
1963 : : * Now, if this missed chunk contains rollback to savepoint, then
1964 : : * there is a risk of deadlock which probably shouldn't happen
1965 : : * after restart.
1966 : : */
1967 : 244 : pa_decr_and_wait_stream_block();
1968 : 242 : break;
1969 : :
1212 akapila@postgresql.o 1970 :UBC 0 : default:
1204 1971 [ # # ]: 0 : elog(ERROR, "unexpected apply action: %d", (int) apply_action);
1972 : : break;
1973 : : }
1974 : :
1212 akapila@postgresql.o 1975 :CBC 839 : in_streamed_transaction = false;
1204 1976 : 839 : stream_xid = InvalidTransactionId;
1977 : :
1978 : : /*
1979 : : * The parallel apply worker could be in a transaction in which case we
1980 : : * need to report the state as STATE_IDLEINTRANSACTION.
1981 : : */
1212 1982 [ + + ]: 839 : if (IsTransactionOrTransactionBlock())
1983 : 242 : pgstat_report_activity(STATE_IDLEINTRANSACTION, NULL);
1984 : : else
1985 : 597 : pgstat_report_activity(STATE_IDLE, NULL);
1986 : :
1987 : 839 : reset_apply_error_context_info();
1988 : 839 : }
1989 : :
1990 : : /*
1991 : : * Helper function to handle STREAM ABORT message when the transaction was
1992 : : * serialized to file.
1993 : : */
1994 : : static void
1995 : 14 : stream_abort_internal(TransactionId xid, TransactionId subxid)
1996 : : {
1997 : : /*
1998 : : * If the two XIDs are the same, it's in fact abort of toplevel xact, so
1999 : : * just delete the files with serialized info.
2000 : : */
2070 2001 [ + + ]: 14 : if (xid == subxid)
2002 : 1 : stream_cleanup_files(MyLogicalRepWorker->subid, xid);
2003 : : else
2004 : : {
2005 : : /*
2006 : : * OK, so it's a subxact. We need to read the subxact file for the
2007 : : * toplevel transaction, determine the offset tracked for the subxact,
2008 : : * and truncate the file with changes. We also remove the subxacts
2009 : : * with higher offsets (or rather higher XIDs).
2010 : : *
2011 : : * We intentionally scan the array from the tail, because we're likely
2012 : : * aborting a change for the most recent subtransactions.
2013 : : *
2014 : : * We can't use the binary search here as subxact XIDs won't
2015 : : * necessarily arrive in sorted order, consider the case where we have
2016 : : * released the savepoint for multiple subtransactions and then
2017 : : * performed rollback to savepoint for one of the earlier
2018 : : * sub-transaction.
2019 : : */
2020 : : int64 i;
2021 : : int64 subidx;
2022 : : BufFile *fd;
2023 : 13 : bool found = false;
2024 : : char path[MAXPGPATH];
2025 : :
2026 : 13 : subidx = -1;
1790 tgl@sss.pgh.pa.us 2027 : 13 : begin_replication_step();
2070 akapila@postgresql.o 2028 : 13 : subxact_info_read(MyLogicalRepWorker->subid, xid);
2029 : :
2030 [ + + ]: 15 : for (i = subxact_data.nsubxacts; i > 0; i--)
2031 : : {
2032 [ + + ]: 11 : if (subxact_data.subxacts[i - 1].xid == subxid)
2033 : : {
2034 : 9 : subidx = (i - 1);
2035 : 9 : found = true;
2036 : 9 : break;
2037 : : }
2038 : : }
2039 : :
2040 : : /*
2041 : : * If it's an empty sub-transaction then we will not find the subxid
2042 : : * here so just cleanup the subxact info and return.
2043 : : */
2044 [ + + ]: 13 : if (!found)
2045 : : {
2046 : : /* Cleanup the subxact info */
2047 : 4 : cleanup_subxact_info();
1790 tgl@sss.pgh.pa.us 2048 : 4 : end_replication_step();
1908 akapila@postgresql.o 2049 : 4 : CommitTransactionCommand();
2070 2050 : 4 : return;
2051 : : }
2052 : :
2053 : : /* open the changes file */
1212 2054 : 9 : changes_filename(path, MyLogicalRepWorker->subid, xid);
2055 : 9 : fd = BufFileOpenFileSet(MyLogicalRepWorker->stream_fileset, path,
2056 : : O_RDWR, false);
2057 : :
2058 : : /* OK, truncate the file at the right offset */
2059 : 9 : BufFileTruncateFileSet(fd, subxact_data.subxacts[subidx].fileno,
2060 : 9 : subxact_data.subxacts[subidx].offset);
2061 : 9 : BufFileClose(fd);
2062 : :
2063 : : /* discard the subxacts added later */
2064 : 9 : subxact_data.nsubxacts = subidx;
2065 : :
2066 : : /* write the updated subxact list */
2067 : 9 : subxact_info_write(MyLogicalRepWorker->subid, xid);
2068 : :
2069 : 9 : end_replication_step();
2070 : 9 : CommitTransactionCommand();
2071 : : }
2072 : : }
2073 : :
2074 : : /*
2075 : : * Handle STREAM ABORT message.
2076 : : */
2077 : : static void
2078 : 38 : apply_handle_stream_abort(StringInfo s)
2079 : : {
2080 : : TransactionId xid;
2081 : : TransactionId subxid;
2082 : : LogicalRepStreamAbortData abort_data;
2083 : : ParallelApplyWorkerInfo *winfo;
2084 : : TransApplyAction apply_action;
2085 : :
2086 : : /* Save the message before it is consumed. */
2087 : 38 : StringInfoData original_msg = *s;
2088 : : bool toplevel_xact;
2089 : :
2090 [ - + ]: 38 : if (in_streamed_transaction)
1212 akapila@postgresql.o 2091 [ # # ]:UBC 0 : ereport(ERROR,
2092 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
2093 : : errmsg_internal("STREAM ABORT message without STREAM STOP")));
2094 : :
2095 : : /* We receive abort information only when we can apply in parallel. */
1212 akapila@postgresql.o 2096 :CBC 38 : logicalrep_read_stream_abort(s, &abort_data,
2097 : 38 : MyLogicalRepWorker->parallel_apply);
2098 : :
2099 : 38 : xid = abort_data.xid;
2100 : 38 : subxid = abort_data.subxid;
2101 : 38 : toplevel_xact = (xid == subxid);
2102 : :
2103 : 38 : set_apply_error_context_xact(subxid, abort_data.abort_lsn);
2104 : :
2105 : 38 : apply_action = get_transaction_apply_action(xid, &winfo);
2106 : :
2107 [ + + + + : 38 : switch (apply_action)
- ]
2108 : : {
1204 2109 : 14 : case TRANS_LEADER_APPLY:
2110 : :
2111 : : /*
2112 : : * We are in the leader apply worker and the transaction has been
2113 : : * serialized to file.
2114 : : */
1212 2115 : 14 : stream_abort_internal(xid, subxid);
2116 : :
2117 [ - + ]: 14 : elog(DEBUG1, "finished processing the STREAM ABORT command");
2118 : 14 : break;
2119 : :
2120 : 10 : case TRANS_LEADER_SEND_TO_PARALLEL:
2121 [ - + ]: 10 : Assert(winfo);
2122 : :
2123 : : /*
2124 : : * For the case of aborting the subtransaction, we increment the
2125 : : * number of streaming blocks and take the lock again before
2126 : : * sending the STREAM_ABORT to ensure that the parallel apply
2127 : : * worker will wait on the lock for the next set of changes after
2128 : : * processing the STREAM_ABORT message if it is not already
2129 : : * waiting for STREAM_STOP message.
2130 : : *
2131 : : * It is important to perform this locking before sending the
2132 : : * STREAM_ABORT message so that the leader can hold the lock first
2133 : : * and the parallel apply worker will wait for the leader to
2134 : : * release the lock. This is the same as what we do in
2135 : : * apply_handle_stream_stop. See Locking Considerations atop
2136 : : * applyparallelworker.c.
2137 : : */
2138 [ + + ]: 10 : if (!toplevel_xact)
2139 : : {
2140 : 9 : pa_unlock_stream(xid, AccessExclusiveLock);
2141 : 9 : pg_atomic_add_fetch_u32(&winfo->shared->pending_stream_count, 1);
2142 : 9 : pa_lock_stream(xid, AccessExclusiveLock);
2143 : : }
2144 : :
2145 [ + - ]: 10 : if (pa_send_data(winfo, s->len, s->data))
2146 : : {
2147 : : /*
2148 : : * Unlike STREAM_COMMIT and STREAM_PREPARE, we don't need to
2149 : : * wait here for the parallel apply worker to finish as that
2150 : : * is not required to maintain the commit order and won't have
2151 : : * the risk of failures due to transaction dependencies and
2152 : : * deadlocks. However, it is possible that before the parallel
2153 : : * worker finishes and we clear the worker info, the xid
2154 : : * wraparound happens on the upstream and a new transaction
2155 : : * with the same xid can appear and that can lead to duplicate
2156 : : * entries in ParallelApplyTxnHash. Yet another problem could
2157 : : * be that we may have serialized the changes in partial
2158 : : * serialize mode and the file containing xact changes may
2159 : : * already exist, and after xid wraparound trying to create
2160 : : * the file for the same xid can lead to an error. To avoid
2161 : : * these problems, we decide to wait for the aborts to finish.
2162 : : *
2163 : : * Note, it is okay to not update the flush location position
2164 : : * for aborts as in worst case that means such a transaction
2165 : : * won't be sent again after restart.
2166 : : */
2167 [ + + ]: 10 : if (toplevel_xact)
2168 : 1 : pa_xact_finish(winfo, InvalidXLogRecPtr);
2169 : :
2170 : 10 : break;
2171 : : }
2172 : :
2173 : : /*
2174 : : * Switch to serialize mode when we are not able to send the
2175 : : * change to parallel apply worker.
2176 : : */
1212 akapila@postgresql.o 2177 :UBC 0 : pa_switch_to_partial_serialize(winfo, true);
2178 : :
2179 : : pg_fallthrough;
1212 akapila@postgresql.o 2180 :CBC 2 : case TRANS_LEADER_PARTIAL_SERIALIZE:
2181 [ - + ]: 2 : Assert(winfo);
2182 : :
2183 : : /*
2184 : : * Parallel apply worker might have applied some changes, so write
2185 : : * the STREAM_ABORT message so that it can rollback the
2186 : : * subtransaction if needed.
2187 : : */
2188 : 2 : stream_open_and_write_change(xid, LOGICAL_REP_MSG_STREAM_ABORT,
2189 : : &original_msg);
2190 : :
2191 [ + + ]: 2 : if (toplevel_xact)
2192 : : {
2193 : 1 : pa_set_fileset_state(winfo->shared, FS_SERIALIZE_DONE);
2194 : 1 : pa_xact_finish(winfo, InvalidXLogRecPtr);
2195 : : }
2196 : 2 : break;
2197 : :
2198 : 12 : case TRANS_PARALLEL_APPLY:
2199 : :
2200 : : /*
2201 : : * If the parallel apply worker is applying spooled messages then
2202 : : * close the file before aborting.
2203 : : */
2204 [ + + + + ]: 12 : if (toplevel_xact && stream_fd)
2205 : 1 : stream_close_file();
2206 : :
2207 : 12 : pa_stream_abort(&abort_data);
2208 : :
2209 : : /*
2210 : : * We need to wait after processing rollback to savepoint for the
2211 : : * next set of changes.
2212 : : *
2213 : : * We have a race condition here due to which we can start waiting
2214 : : * here when there are more chunk of streams in the queue. See
2215 : : * apply_handle_stream_stop.
2216 : : */
2217 [ + + ]: 12 : if (!toplevel_xact)
2218 : 10 : pa_decr_and_wait_stream_block();
2219 : :
2220 [ + + ]: 12 : elog(DEBUG1, "finished processing the STREAM ABORT command");
2221 : 12 : break;
2222 : :
1212 akapila@postgresql.o 2223 :UBC 0 : default:
1204 2224 [ # # ]: 0 : elog(ERROR, "unexpected apply action: %d", (int) apply_action);
2225 : : break;
2226 : : }
2227 : :
1212 akapila@postgresql.o 2228 :CBC 38 : reset_apply_error_context_info();
2229 : 38 : }
2230 : :
2231 : : /*
2232 : : * Ensure that the passed location is fileset's end.
2233 : : */
2234 : : static void
2235 : 4 : ensure_last_message(FileSet *stream_fileset, TransactionId xid, int fileno,
2236 : : pgoff_t offset)
2237 : : {
2238 : : char path[MAXPGPATH];
2239 : : BufFile *fd;
2240 : : int last_fileno;
2241 : : pgoff_t last_offset;
2242 : :
2243 [ - + ]: 4 : Assert(!IsTransactionState());
2244 : :
2245 : 4 : begin_replication_step();
2246 : :
2247 : 4 : changes_filename(path, MyLogicalRepWorker->subid, xid);
2248 : :
2249 : 4 : fd = BufFileOpenFileSet(stream_fileset, path, O_RDONLY, false);
2250 : :
2251 : 4 : BufFileSeek(fd, 0, 0, SEEK_END);
2252 : 4 : BufFileTell(fd, &last_fileno, &last_offset);
2253 : :
2254 : 4 : BufFileClose(fd);
2255 : :
2256 : 4 : end_replication_step();
2257 : :
2258 [ + - - + ]: 4 : if (last_fileno != fileno || last_offset != offset)
1212 akapila@postgresql.o 2259 [ # # ]:UBC 0 : elog(ERROR, "unexpected message left in streaming transaction's changes file \"%s\"",
2260 : : path);
2070 akapila@postgresql.o 2261 :CBC 4 : }
2262 : :
2263 : : /*
2264 : : * Common spoolfile processing.
2265 : : */
2266 : : void
1212 2267 : 31 : apply_spooled_messages(FileSet *stream_fileset, TransactionId xid,
2268 : : XLogRecPtr lsn)
2269 : : {
2270 : : int nchanges;
2271 : : char path[MAXPGPATH];
2070 2272 : 31 : char *buffer = NULL;
2273 : : MemoryContext oldcxt;
2274 : : ResourceOwner oldowner;
2275 : : int fileno;
2276 : : pgoff_t offset;
2277 : :
1212 2278 [ + + ]: 31 : if (!am_parallel_apply_worker())
2279 : 27 : maybe_start_skipping_changes(lsn);
2280 : :
2281 : : /* Make sure we have an open transaction */
1790 tgl@sss.pgh.pa.us 2282 : 31 : begin_replication_step();
2283 : :
2284 : : /*
2285 : : * Allocate file handle and memory required to process all the messages in
2286 : : * TopTransactionContext to avoid them getting reset after each message is
2287 : : * processed.
2288 : : */
2070 akapila@postgresql.o 2289 : 31 : oldcxt = MemoryContextSwitchTo(TopTransactionContext);
2290 : :
2291 : : /* Open the spool file for the committed/prepared transaction */
2292 : 31 : changes_filename(path, MyLogicalRepWorker->subid, xid);
2293 [ - + ]: 31 : elog(DEBUG1, "replaying changes from file \"%s\"", path);
2294 : :
2295 : : /*
2296 : : * Make sure the file is owned by the toplevel transaction so that the
2297 : : * file will not be accidentally closed when aborting a subtransaction.
2298 : : */
1212 2299 : 31 : oldowner = CurrentResourceOwner;
2300 : 31 : CurrentResourceOwner = TopTransactionResourceOwner;
2301 : :
2302 : 31 : stream_fd = BufFileOpenFileSet(stream_fileset, path, O_RDONLY, false);
2303 : :
2304 : 31 : CurrentResourceOwner = oldowner;
2305 : :
2070 2306 : 31 : buffer = palloc(BLCKSZ);
2307 : :
2308 : 31 : MemoryContextSwitchTo(oldcxt);
2309 : :
1741 2310 : 31 : remote_final_lsn = lsn;
2311 : :
2312 : : /*
2313 : : * Make sure the handle apply_dispatch methods are aware we're in a remote
2314 : : * transaction.
2315 : : */
2070 2316 : 31 : in_remote_transaction = true;
2317 : 31 : pgstat_report_activity(STATE_RUNNING, NULL);
2318 : :
1790 tgl@sss.pgh.pa.us 2319 : 31 : end_replication_step();
2320 : :
2321 : : /*
2322 : : * Read the entries one by one and pass them through the same logic as in
2323 : : * apply_dispatch.
2324 : : */
2070 akapila@postgresql.o 2325 : 31 : nchanges = 0;
2326 : : while (true)
2327 : 88470 : {
2328 : : StringInfoData s2;
2329 : : size_t nbytes;
2330 : : int len;
2331 : :
2332 [ - + ]: 88501 : CHECK_FOR_INTERRUPTS();
2333 : :
2334 : : /* read length of the on-disk record */
1205 peter@eisentraut.org 2335 : 88501 : nbytes = BufFileReadMaybeEOF(stream_fd, &len, sizeof(len), true);
2336 : :
2337 : : /* have we reached end of the file? */
2070 akapila@postgresql.o 2338 [ + + ]: 88501 : if (nbytes == 0)
2339 : 26 : break;
2340 : :
2341 : : /* do we have a correct length? */
1788 tgl@sss.pgh.pa.us 2342 [ - + ]: 88475 : if (len <= 0)
1788 tgl@sss.pgh.pa.us 2343 [ # # ]:UBC 0 : elog(ERROR, "incorrect length %d in streaming transaction's changes file \"%s\"",
2344 : : len, path);
2345 : :
2346 : : /* make sure we have sufficiently large buffer */
2070 akapila@postgresql.o 2347 :CBC 88475 : buffer = repalloc(buffer, len);
2348 : :
2349 : : /* and finally read the data into the buffer */
1205 peter@eisentraut.org 2350 : 88475 : BufFileReadExact(stream_fd, buffer, len);
2351 : :
1212 akapila@postgresql.o 2352 : 88475 : BufFileTell(stream_fd, &fileno, &offset);
2353 : :
2354 : : /* init a stringinfo using the buffer and call apply_dispatch */
910 drowley@postgresql.o 2355 : 88475 : initReadOnlyStringInfo(&s2, buffer, len);
2356 : :
2357 : : /* Ensure we are reading the data into our memory context. */
2070 akapila@postgresql.o 2358 : 88475 : oldcxt = MemoryContextSwitchTo(ApplyMessageContext);
2359 : :
2360 : 88475 : apply_dispatch(&s2);
2361 : :
2362 : 88474 : MemoryContextReset(ApplyMessageContext);
2363 : :
2364 : 88474 : MemoryContextSwitchTo(oldcxt);
2365 : :
2366 : 88474 : nchanges++;
2367 : :
2368 : : /*
2369 : : * It is possible the file has been closed because we have processed
2370 : : * the transaction end message like stream_commit in which case that
2371 : : * must be the last message.
2372 : : */
1212 2373 [ + + ]: 88474 : if (!stream_fd)
2374 : : {
2375 : 4 : ensure_last_message(stream_fileset, xid, fileno, offset);
2376 : 4 : break;
2377 : : }
2378 : :
2070 2379 [ + + ]: 88470 : if (nchanges % 1000 == 0)
1788 tgl@sss.pgh.pa.us 2380 [ - + ]: 84 : elog(DEBUG1, "replayed %d changes from file \"%s\"",
2381 : : nchanges, path);
2382 : : }
2383 : :
1212 akapila@postgresql.o 2384 [ + + ]: 30 : if (stream_fd)
2385 : 26 : stream_close_file();
2386 : :
2070 2387 [ - + ]: 30 : elog(DEBUG1, "replayed %d (all) changes from file \"%s\"",
2388 : : nchanges, path);
2389 : :
1741 2390 : 30 : return;
2391 : : }
2392 : :
2393 : : /*
2394 : : * Handle STREAM COMMIT message.
2395 : : */
2396 : : static void
2397 : 61 : apply_handle_stream_commit(StringInfo s)
2398 : : {
2399 : : TransactionId xid;
2400 : : LogicalRepCommitData commit_data;
2401 : : ParallelApplyWorkerInfo *winfo;
2402 : : TransApplyAction apply_action;
2403 : :
2404 : : /* Save the message before it is consumed. */
1212 2405 : 61 : StringInfoData original_msg = *s;
2406 : :
1741 2407 [ - + ]: 61 : if (in_streamed_transaction)
1741 akapila@postgresql.o 2408 [ # # ]:UBC 0 : ereport(ERROR,
2409 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
2410 : : errmsg_internal("STREAM COMMIT message without STREAM STOP")));
2411 : :
1741 akapila@postgresql.o 2412 :CBC 61 : xid = logicalrep_read_stream_commit(s, &commit_data);
1519 2413 : 61 : set_apply_error_context_xact(xid, commit_data.commit_lsn);
2414 : :
1212 2415 : 61 : apply_action = get_transaction_apply_action(xid, &winfo);
2416 : :
2417 [ + + + + : 61 : switch (apply_action)
- ]
2418 : : {
1204 2419 : 22 : case TRANS_LEADER_APPLY:
2420 : :
2421 : : /*
2422 : : * The transaction has been serialized to file, so replay all the
2423 : : * spooled operations.
2424 : : */
1212 2425 : 22 : apply_spooled_messages(MyLogicalRepWorker->stream_fileset, xid,
2426 : : commit_data.commit_lsn);
2427 : :
2428 : 21 : apply_handle_commit_internal(&commit_data);
2429 : :
2430 : : /* Unlink the files with serialized changes and subxact info. */
2431 : 21 : stream_cleanup_files(MyLogicalRepWorker->subid, xid);
2432 : :
2433 [ - + ]: 21 : elog(DEBUG1, "finished processing the STREAM COMMIT command");
2434 : 21 : break;
2435 : :
2436 : 18 : case TRANS_LEADER_SEND_TO_PARALLEL:
2437 [ - + ]: 18 : Assert(winfo);
2438 : :
2439 [ + - ]: 18 : if (pa_send_data(winfo, s->len, s->data))
2440 : : {
2441 : : /* Finish processing the streaming transaction. */
2442 : 18 : pa_xact_finish(winfo, commit_data.end_lsn);
2443 : 17 : break;
2444 : : }
2445 : :
2446 : : /*
2447 : : * Switch to serialize mode when we are not able to send the
2448 : : * change to parallel apply worker.
2449 : : */
1212 akapila@postgresql.o 2450 :UBC 0 : pa_switch_to_partial_serialize(winfo, true);
2451 : :
2452 : : pg_fallthrough;
1212 akapila@postgresql.o 2453 :CBC 2 : case TRANS_LEADER_PARTIAL_SERIALIZE:
2454 [ - + ]: 2 : Assert(winfo);
2455 : :
2456 : 2 : stream_open_and_write_change(xid, LOGICAL_REP_MSG_STREAM_COMMIT,
2457 : : &original_msg);
2458 : :
2459 : 2 : pa_set_fileset_state(winfo->shared, FS_SERIALIZE_DONE);
2460 : :
2461 : : /* Finish processing the streaming transaction. */
2462 : 2 : pa_xact_finish(winfo, commit_data.end_lsn);
2463 : 2 : break;
2464 : :
2465 : 19 : case TRANS_PARALLEL_APPLY:
2466 : :
2467 : : /*
2468 : : * If the parallel apply worker is applying spooled messages then
2469 : : * close the file before committing.
2470 : : */
2471 [ + + ]: 19 : if (stream_fd)
2472 : 2 : stream_close_file();
2473 : :
2474 : 19 : apply_handle_commit_internal(&commit_data);
2475 : :
2476 : 19 : MyParallelShared->last_commit_end = XactLastCommitEnd;
2477 : :
2478 : : /*
2479 : : * It is important to set the transaction state as finished before
2480 : : * releasing the lock. See pa_wait_for_xact_finish.
2481 : : */
2482 : 19 : pa_set_xact_state(MyParallelShared, PARALLEL_TRANS_FINISHED);
2483 : 19 : pa_unlock_transaction(xid, AccessExclusiveLock);
2484 : :
2485 : 19 : pa_reset_subtrans();
2486 : :
2487 [ + + ]: 19 : elog(DEBUG1, "finished processing the STREAM COMMIT command");
2488 : 19 : break;
2489 : :
1212 akapila@postgresql.o 2490 :UBC 0 : default:
1204 2491 [ # # ]: 0 : elog(ERROR, "unexpected apply action: %d", (int) apply_action);
2492 : : break;
2493 : : }
2494 : :
2495 : : /*
2496 : : * Process any tables that are being synchronized in parallel, as well as
2497 : : * any newly added tables or sequences.
2498 : : */
201 akapila@postgresql.o 2499 :GNC 59 : ProcessSyncingRelations(commit_data.end_lsn);
2500 : :
2070 akapila@postgresql.o 2501 :CBC 59 : pgstat_report_activity(STATE_IDLE, NULL);
2502 : :
1712 2503 : 59 : reset_apply_error_context_info();
2070 2504 : 59 : }
2505 : :
2506 : : /*
2507 : : * Helper function for apply_handle_commit and apply_handle_stream_commit.
2508 : : */
2509 : : static void
1740 2510 : 501 : apply_handle_commit_internal(LogicalRepCommitData *commit_data)
2511 : : {
1505 2512 [ + + ]: 501 : if (is_skipping_changes())
2513 : : {
2514 : 2 : stop_skipping_changes();
2515 : :
2516 : : /*
2517 : : * Start a new transaction to clear the subskiplsn, if not started
2518 : : * yet.
2519 : : */
2520 [ + + ]: 2 : if (!IsTransactionState())
2521 : 1 : StartTransactionCommand();
2522 : : }
2523 : :
1908 2524 [ + - ]: 501 : if (IsTransactionState())
2525 : : {
2526 : : /*
2527 : : * The transaction is either non-empty or skipped, so we clear the
2528 : : * subskiplsn.
2529 : : */
1505 2530 : 501 : clear_subscription_skip_lsn(commit_data->commit_lsn);
2531 : :
2532 : : /*
2533 : : * Update origin state so we can restart streaming from correct
2534 : : * position in case of crash.
2535 : : */
97 msawada@postgresql.o 2536 :GNC 501 : replorigin_xact_state.origin_lsn = commit_data->end_lsn;
2537 : 501 : replorigin_xact_state.origin_timestamp = commit_data->committime;
2538 : :
1985 akapila@postgresql.o 2539 :CBC 501 : CommitTransactionCommand();
2540 : :
1212 2541 [ + + ]: 501 : if (IsTransactionBlock())
2542 : : {
2543 : 4 : EndTransactionBlock(false);
2544 : 4 : CommitTransactionCommand();
2545 : : }
2546 : :
1985 2547 : 501 : pgstat_report_stat(false);
2548 : :
1212 2549 : 501 : store_flush_position(commit_data->end_lsn, XactLastCommitEnd);
2550 : : }
2551 : : else
2552 : : {
2553 : : /* Process any invalidation messages that might have accumulated. */
1985 akapila@postgresql.o 2554 :UBC 0 : AcceptInvalidationMessages();
2555 : 0 : maybe_reread_subscription();
2556 : : }
2557 : :
1985 akapila@postgresql.o 2558 :CBC 501 : in_remote_transaction = false;
2559 : 501 : }
2560 : :
2561 : : /*
2562 : : * Handle RELATION message.
2563 : : *
2564 : : * Note we don't do validation against local schema here. The validation
2565 : : * against local schema is postponed until first change for given relation
2566 : : * comes as we only care about it when applying changes for it anyway and we
2567 : : * do less locking this way.
2568 : : */
2569 : : static void
3393 peter_e@gmx.net 2570 : 492 : apply_handle_relation(StringInfo s)
2571 : : {
2572 : : LogicalRepRelation *rel;
2573 : :
1986 akapila@postgresql.o 2574 [ + + ]: 492 : if (handle_streamed_transaction(LOGICAL_REP_MSG_RELATION, s))
2070 2575 : 36 : return;
2576 : :
3393 peter_e@gmx.net 2577 : 456 : rel = logicalrep_read_rel(s);
2578 : 456 : logicalrep_relmap_update(rel);
2579 : :
2580 : : /* Also reset all entries in the partition map that refer to remoterel. */
1419 akapila@postgresql.o 2581 : 456 : logicalrep_partmap_reset_relmap(rel);
2582 : : }
2583 : :
2584 : : /*
2585 : : * Handle TYPE message.
2586 : : *
2587 : : * This implementation pays no attention to TYPE messages; we expect the user
2588 : : * to have set things up so that the incoming data is acceptable to the input
2589 : : * functions for the locally subscribed tables. Hence, we just read and
2590 : : * discard the message.
2591 : : */
2592 : : static void
3393 peter_e@gmx.net 2593 : 18 : apply_handle_type(StringInfo s)
2594 : : {
2595 : : LogicalRepTyp typ;
2596 : :
1986 akapila@postgresql.o 2597 [ - + ]: 18 : if (handle_streamed_transaction(LOGICAL_REP_MSG_TYPE, s))
2070 akapila@postgresql.o 2598 :UBC 0 : return;
2599 : :
3393 peter_e@gmx.net 2600 :CBC 18 : logicalrep_read_typ(s, &typ);
2601 : : }
2602 : :
2603 : : /*
2604 : : * Check that we (the subscription owner) have sufficient privileges on the
2605 : : * target relation to perform the given operation.
2606 : : */
2607 : : static void
1579 jdavis@postgresql.or 2608 : 240842 : TargetPrivilegesCheck(Relation rel, AclMode mode)
2609 : : {
2610 : : Oid relid;
2611 : : AclResult aclresult;
2612 : :
2613 : 240842 : relid = RelationGetRelid(rel);
2614 : 240842 : aclresult = pg_class_aclcheck(relid, GetUserId(), mode);
2615 [ + + ]: 240842 : if (aclresult != ACLCHECK_OK)
2616 : 10 : aclcheck_error(aclresult,
2617 : 10 : get_relkind_objtype(rel->rd_rel->relkind),
2618 : 10 : get_rel_name(relid));
2619 : :
2620 : : /*
2621 : : * We lack the infrastructure to honor RLS policies. It might be possible
2622 : : * to add such infrastructure here, but tablesync workers lack it, too, so
2623 : : * we don't bother. RLS does not ordinarily apply to TRUNCATE commands,
2624 : : * but it seems dangerous to replicate a TRUNCATE and then refuse to
2625 : : * replicate subsequent INSERTs, so we forbid all commands the same.
2626 : : */
2627 [ + + ]: 240832 : if (check_enable_rls(relid, InvalidOid, false) == RLS_ENABLED)
2628 [ + - ]: 3 : ereport(ERROR,
2629 : : (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2630 : : errmsg("user \"%s\" cannot replicate into relation with row-level security enabled: \"%s\"",
2631 : : GetUserNameFromId(GetUserId(), true),
2632 : : RelationGetRelationName(rel))));
2633 : 240829 : }
2634 : :
2635 : : /*
2636 : : * Handle INSERT message.
2637 : : */
2638 : :
2639 : : static void
3393 peter_e@gmx.net 2640 : 206378 : apply_handle_insert(StringInfo s)
2641 : : {
2642 : : LogicalRepRelMapEntry *rel;
2643 : : LogicalRepTupleData newtup;
2644 : : LogicalRepRelId relid;
2645 : : UserContext ucxt;
2646 : : ApplyExecutionData *edata;
2647 : : EState *estate;
2648 : : TupleTableSlot *remoteslot;
2649 : : MemoryContext oldctx;
2650 : : bool run_as_owner;
2651 : :
2652 : : /*
2653 : : * Quick return if we are skipping data modification changes or handling
2654 : : * streamed transactions.
2655 : : */
1505 akapila@postgresql.o 2656 [ + + + + ]: 402755 : if (is_skipping_changes() ||
2657 : 196377 : handle_streamed_transaction(LOGICAL_REP_MSG_INSERT, s))
2070 2658 : 110081 : return;
2659 : :
1790 tgl@sss.pgh.pa.us 2660 : 96369 : begin_replication_step();
2661 : :
3393 peter_e@gmx.net 2662 : 96367 : relid = logicalrep_read_insert(s, &newtup);
2663 : 96367 : rel = logicalrep_rel_open(relid, RowExclusiveLock);
3330 2664 [ + + ]: 96358 : if (!should_apply_changes_for_rel(rel))
2665 : : {
2666 : : /*
2667 : : * The relation can't become interesting in the middle of the
2668 : : * transaction so it's safe to unlock it.
2669 : : */
2670 : 72 : logicalrep_rel_close(rel, RowExclusiveLock);
1790 tgl@sss.pgh.pa.us 2671 : 72 : end_replication_step();
3330 peter_e@gmx.net 2672 : 72 : return;
2673 : : }
2674 : :
2675 : : /*
2676 : : * Make sure that any user-supplied code runs as the table owner, unless
2677 : : * the user has opted out of that behavior.
2678 : : */
1127 rhaas@postgresql.org 2679 : 96286 : run_as_owner = MySubscription->runasowner;
2680 [ + + ]: 96286 : if (!run_as_owner)
2681 : 96277 : SwitchToUntrustedUser(rel->localrel->rd_rel->relowner, &ucxt);
2682 : :
2683 : : /* Set relation for error callback */
1712 akapila@postgresql.o 2684 : 96286 : apply_error_callback_arg.rel = rel;
2685 : :
2686 : : /* Initialize the executor state. */
1809 tgl@sss.pgh.pa.us 2687 : 96286 : edata = create_edata_for_relation(rel);
2688 : 96286 : estate = edata->estate;
3000 andres@anarazel.de 2689 : 96286 : remoteslot = ExecInitExtraTupleSlot(estate,
2728 2690 : 96286 : RelationGetDescr(rel->localrel),
2691 : : &TTSOpsVirtual);
2692 : :
2693 : : /* Process and store remote tuple in the slot */
3393 peter_e@gmx.net 2694 [ - + ]: 96286 : oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
2117 tgl@sss.pgh.pa.us 2695 : 96286 : slot_store_data(remoteslot, rel, &newtup);
3393 peter_e@gmx.net 2696 : 96286 : slot_fill_defaults(rel, estate, remoteslot);
2697 : 96286 : MemoryContextSwitchTo(oldctx);
2698 : :
2699 : : /* For a partitioned table, insert the tuple into a partition. */
2220 peter@eisentraut.org 2700 [ + + ]: 96286 : if (rel->localrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1809 tgl@sss.pgh.pa.us 2701 : 69 : apply_handle_tuple_routing(edata,
2702 : : remoteslot, NULL, CMD_INSERT);
2703 : : else
2704 : : {
440 2705 : 96217 : ResultRelInfo *relinfo = edata->targetRelInfo;
2706 : :
392 akapila@postgresql.o 2707 : 96217 : ExecOpenIndices(relinfo, false);
440 tgl@sss.pgh.pa.us 2708 : 96217 : apply_handle_insert_internal(edata, relinfo, remoteslot);
2709 : 96201 : ExecCloseIndices(relinfo);
2710 : : }
2711 : :
1809 2712 : 96245 : finish_edata(edata);
2713 : :
2714 : : /* Reset relation for error callback */
1712 akapila@postgresql.o 2715 : 96245 : apply_error_callback_arg.rel = NULL;
2716 : :
1127 rhaas@postgresql.org 2717 [ + + ]: 96245 : if (!run_as_owner)
2718 : 96240 : RestoreUserContext(&ucxt);
2719 : :
3393 peter_e@gmx.net 2720 : 96245 : logicalrep_rel_close(rel, NoLock);
2721 : :
1790 tgl@sss.pgh.pa.us 2722 : 96245 : end_replication_step();
2723 : : }
2724 : :
2725 : : /*
2726 : : * Workhorse for apply_handle_insert()
2727 : : * relinfo is for the relation we're actually inserting into
2728 : : * (could be a child partition of edata->targetRelInfo)
2729 : : */
2730 : : static void
1809 2731 : 96287 : apply_handle_insert_internal(ApplyExecutionData *edata,
2732 : : ResultRelInfo *relinfo,
2733 : : TupleTableSlot *remoteslot)
2734 : : {
2735 : 96287 : EState *estate = edata->estate;
2736 : :
2737 : : /* Caller should have opened indexes already. */
440 2738 [ + + + + : 96287 : Assert(relinfo->ri_IndexRelationDescs != NULL ||
- + ]
2739 : : !relinfo->ri_RelationDesc->rd_rel->relhasindex ||
2740 : : RelationGetIndexList(relinfo->ri_RelationDesc) == NIL);
2741 : :
2742 : : /* Caller will not have done this bit. */
2743 [ - + ]: 96287 : Assert(relinfo->ri_onConflictArbiterIndexes == NIL);
623 akapila@postgresql.o 2744 : 96287 : InitConflictIndexes(relinfo);
2745 : :
2746 : : /* Do the insert. */
1579 jdavis@postgresql.or 2747 : 96287 : TargetPrivilegesCheck(relinfo->ri_RelationDesc, ACL_INSERT);
2029 heikki.linnakangas@i 2748 : 96279 : ExecSimpleRelationInsert(relinfo, estate, remoteslot);
2233 peter@eisentraut.org 2749 : 96246 : }
2750 : :
2751 : : /*
2752 : : * Check if the logical replication relation is updatable and throw
2753 : : * appropriate error if it isn't.
2754 : : */
2755 : : static void
3393 peter_e@gmx.net 2756 : 72302 : check_relation_updatable(LogicalRepRelMapEntry *rel)
2757 : : {
2758 : : /*
2759 : : * For partitioned tables, we only need to care if the target partition is
2760 : : * updatable (aka has PK or RI defined for it).
2761 : : */
1414 akapila@postgresql.o 2762 [ + + ]: 72302 : if (rel->localrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
2763 : 30 : return;
2764 : :
2765 : : /* Updatable, no error. */
3393 peter_e@gmx.net 2766 [ + - ]: 72272 : if (rel->updatable)
2767 : 72272 : return;
2768 : :
2769 : : /*
2770 : : * We are in error mode so it's fine this is somewhat slow. It's better to
2771 : : * give user correct error.
2772 : : */
3393 peter_e@gmx.net 2773 [ # # ]:UBC 0 : if (OidIsValid(GetRelationIdentityOrPK(rel->localrel)))
2774 : : {
2775 [ # # ]: 0 : ereport(ERROR,
2776 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2777 : : errmsg("publisher did not send replica identity column "
2778 : : "expected by the logical replication target relation \"%s.%s\"",
2779 : : rel->remoterel.nspname, rel->remoterel.relname)));
2780 : : }
2781 : :
2782 [ # # ]: 0 : ereport(ERROR,
2783 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2784 : : errmsg("logical replication target relation \"%s.%s\" has "
2785 : : "neither REPLICA IDENTITY index nor PRIMARY "
2786 : : "KEY and published relation does not have "
2787 : : "REPLICA IDENTITY FULL",
2788 : : rel->remoterel.nspname, rel->remoterel.relname)));
2789 : : }
2790 : :
2791 : : /*
2792 : : * Handle UPDATE message.
2793 : : *
2794 : : * TODO: FDW support
2795 : : */
2796 : : static void
3393 peter_e@gmx.net 2797 :CBC 66169 : apply_handle_update(StringInfo s)
2798 : : {
2799 : : LogicalRepRelMapEntry *rel;
2800 : : LogicalRepRelId relid;
2801 : : UserContext ucxt;
2802 : : ApplyExecutionData *edata;
2803 : : EState *estate;
2804 : : LogicalRepTupleData oldtup;
2805 : : LogicalRepTupleData newtup;
2806 : : bool has_oldtup;
2807 : : TupleTableSlot *remoteslot;
2808 : : RTEPermissionInfo *target_perminfo;
2809 : : MemoryContext oldctx;
2810 : : bool run_as_owner;
2811 : :
2812 : : /*
2813 : : * Quick return if we are skipping data modification changes or handling
2814 : : * streamed transactions.
2815 : : */
1505 akapila@postgresql.o 2816 [ + + + + ]: 132335 : if (is_skipping_changes() ||
2817 : 66166 : handle_streamed_transaction(LOGICAL_REP_MSG_UPDATE, s))
2070 2818 : 34223 : return;
2819 : :
1790 tgl@sss.pgh.pa.us 2820 : 31946 : begin_replication_step();
2821 : :
3393 peter_e@gmx.net 2822 : 31945 : relid = logicalrep_read_update(s, &has_oldtup, &oldtup,
2823 : : &newtup);
2824 : 31945 : rel = logicalrep_rel_open(relid, RowExclusiveLock);
3330 2825 [ - + ]: 31945 : if (!should_apply_changes_for_rel(rel))
2826 : : {
2827 : : /*
2828 : : * The relation can't become interesting in the middle of the
2829 : : * transaction so it's safe to unlock it.
2830 : : */
3330 peter_e@gmx.net 2831 :UBC 0 : logicalrep_rel_close(rel, RowExclusiveLock);
1790 tgl@sss.pgh.pa.us 2832 : 0 : end_replication_step();
3330 peter_e@gmx.net 2833 : 0 : return;
2834 : : }
2835 : :
2836 : : /* Set relation for error callback */
1712 akapila@postgresql.o 2837 :CBC 31945 : apply_error_callback_arg.rel = rel;
2838 : :
2839 : : /* Check if we can do the update. */
3393 peter_e@gmx.net 2840 : 31945 : check_relation_updatable(rel);
2841 : :
2842 : : /*
2843 : : * Make sure that any user-supplied code runs as the table owner, unless
2844 : : * the user has opted out of that behavior.
2845 : : */
1127 rhaas@postgresql.org 2846 : 31945 : run_as_owner = MySubscription->runasowner;
2847 [ + + ]: 31945 : if (!run_as_owner)
2848 : 31941 : SwitchToUntrustedUser(rel->localrel->rd_rel->relowner, &ucxt);
2849 : :
2850 : : /* Initialize the executor state. */
1809 tgl@sss.pgh.pa.us 2851 : 31944 : edata = create_edata_for_relation(rel);
2852 : 31944 : estate = edata->estate;
3000 andres@anarazel.de 2853 : 31944 : remoteslot = ExecInitExtraTupleSlot(estate,
2728 2854 : 31944 : RelationGetDescr(rel->localrel),
2855 : : &TTSOpsVirtual);
2856 : :
2857 : : /*
2858 : : * Populate updatedCols so that per-column triggers can fire, and so
2859 : : * executor can correctly pass down indexUnchanged hint. This could
2860 : : * include more columns than were actually changed on the publisher
2861 : : * because the logical replication protocol doesn't contain that
2862 : : * information. But it would for example exclude columns that only exist
2863 : : * on the subscriber, since we are not touching those.
2864 : : */
1246 alvherre@alvh.no-ip. 2865 : 31944 : target_perminfo = list_nth(estate->es_rteperminfos, 0);
2311 peter@eisentraut.org 2866 [ + + ]: 159339 : for (int i = 0; i < remoteslot->tts_tupleDescriptor->natts; i++)
2867 : : {
195 drowley@postgresql.o 2868 :GNC 127395 : CompactAttribute *att = TupleDescCompactAttr(remoteslot->tts_tupleDescriptor, i);
2115 tgl@sss.pgh.pa.us 2869 :CBC 127395 : int remoteattnum = rel->attrmap->attnums[i];
2870 : :
2871 [ + + + + ]: 127395 : if (!att->attisdropped && remoteattnum >= 0)
2872 : : {
2873 [ - + ]: 68878 : Assert(remoteattnum < newtup.ncols);
2874 [ + - ]: 68878 : if (newtup.colstatus[remoteattnum] != LOGICALREP_COLUMN_UNCHANGED)
1246 alvherre@alvh.no-ip. 2875 : 68878 : target_perminfo->updatedCols =
2876 : 68878 : bms_add_member(target_perminfo->updatedCols,
2877 : : i + 1 - FirstLowInvalidHeapAttributeNumber);
2878 : : }
2879 : : }
2880 : :
2881 : : /* Build the search tuple. */
3393 peter_e@gmx.net 2882 [ - + ]: 31944 : oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
2117 tgl@sss.pgh.pa.us 2883 : 31944 : slot_store_data(remoteslot, rel,
2884 [ + + ]: 31944 : has_oldtup ? &oldtup : &newtup);
3393 peter_e@gmx.net 2885 : 31944 : MemoryContextSwitchTo(oldctx);
2886 : :
2887 : : /* For a partitioned table, apply update to correct partition. */
2220 peter@eisentraut.org 2888 [ + + ]: 31944 : if (rel->localrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1809 tgl@sss.pgh.pa.us 2889 : 13 : apply_handle_tuple_routing(edata,
2890 : : remoteslot, &newtup, CMD_UPDATE);
2891 : : else
2892 : 31931 : apply_handle_update_internal(edata, edata->targetRelInfo,
2893 : : remoteslot, &newtup, rel->localindexoid);
2894 : :
2895 : 31937 : finish_edata(edata);
2896 : :
2897 : : /* Reset relation for error callback */
1712 akapila@postgresql.o 2898 : 31937 : apply_error_callback_arg.rel = NULL;
2899 : :
1127 rhaas@postgresql.org 2900 [ + + ]: 31937 : if (!run_as_owner)
2901 : 31935 : RestoreUserContext(&ucxt);
2902 : :
2233 peter@eisentraut.org 2903 : 31937 : logicalrep_rel_close(rel, NoLock);
2904 : :
1790 tgl@sss.pgh.pa.us 2905 : 31937 : end_replication_step();
2906 : : }
2907 : :
2908 : : /*
2909 : : * Workhorse for apply_handle_update()
2910 : : * relinfo is for the relation we're actually updating in
2911 : : * (could be a child partition of edata->targetRelInfo)
2912 : : */
2913 : : static void
1809 2914 : 31931 : apply_handle_update_internal(ApplyExecutionData *edata,
2915 : : ResultRelInfo *relinfo,
2916 : : TupleTableSlot *remoteslot,
2917 : : LogicalRepTupleData *newtup,
2918 : : Oid localindexoid)
2919 : : {
2920 : 31931 : EState *estate = edata->estate;
2921 : 31931 : LogicalRepRelMapEntry *relmapentry = edata->targetRel;
2233 peter@eisentraut.org 2922 : 31931 : Relation localrel = relinfo->ri_RelationDesc;
2923 : : EPQState epqstate;
407 akapila@postgresql.o 2924 : 31931 : TupleTableSlot *localslot = NULL;
2925 : 31931 : ConflictTupleInfo conflicttuple = {0};
2926 : : bool found;
2927 : : MemoryContext oldctx;
2928 : :
1082 tgl@sss.pgh.pa.us 2929 : 31931 : EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1, NIL);
392 akapila@postgresql.o 2930 : 31931 : ExecOpenIndices(relinfo, false);
2931 : :
1015 msawada@postgresql.o 2932 : 31931 : found = FindReplTupleInLocalRel(edata, localrel,
2933 : : &relmapentry->remoterel,
2934 : : localindexoid,
2935 : : remoteslot, &localslot);
2936 : :
2937 : : /*
2938 : : * Tuple found.
2939 : : *
2940 : : * Note this will fail if there are other conflicting unique indexes.
2941 : : */
3393 peter_e@gmx.net 2942 [ + + ]: 31926 : if (found)
2943 : : {
2944 : : /*
2945 : : * Report the conflict if the tuple was modified by a different
2946 : : * origin.
2947 : : */
407 akapila@postgresql.o 2948 [ + + ]: 31919 : if (GetTupleTransactionInfo(localslot, &conflicttuple.xmin,
2949 : 2 : &conflicttuple.origin, &conflicttuple.ts) &&
97 msawada@postgresql.o 2950 [ + - ]:GNC 2 : conflicttuple.origin != replorigin_xact_state.origin)
2951 : : {
2952 : : TupleTableSlot *newslot;
2953 : :
2954 : : /* Store the new tuple for conflict reporting */
623 akapila@postgresql.o 2955 :CBC 2 : newslot = table_slot_create(localrel, &estate->es_tupleTable);
2956 : 2 : slot_store_data(newslot, relmapentry, newtup);
2957 : :
407 2958 : 2 : conflicttuple.slot = localslot;
2959 : :
614 2960 : 2 : ReportApplyConflict(estate, relinfo, LOG, CT_UPDATE_ORIGIN_DIFFERS,
2961 : : remoteslot, newslot,
407 akapila@postgresql.o 2962 :ECB (2) : list_make1(&conflicttuple));
2963 : : }
2964 : :
2965 : : /* Process and store remote tuple in the slot */
3393 peter_e@gmx.net 2966 [ + - ]:CBC 31919 : oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
2117 tgl@sss.pgh.pa.us 2967 : 31919 : slot_modify_data(remoteslot, localslot, relmapentry, newtup);
3393 peter_e@gmx.net 2968 : 31919 : MemoryContextSwitchTo(oldctx);
2969 : :
2970 : 31919 : EvalPlanQualSetSlot(&epqstate, remoteslot);
2971 : :
623 akapila@postgresql.o 2972 : 31919 : InitConflictIndexes(relinfo);
2973 : :
2974 : : /* Do the actual update. */
1579 jdavis@postgresql.or 2975 : 31919 : TargetPrivilegesCheck(relinfo->ri_RelationDesc, ACL_UPDATE);
2029 heikki.linnakangas@i 2976 : 31919 : ExecSimpleRelationUpdate(relinfo, estate, &epqstate, localslot,
2977 : : remoteslot);
2978 : : }
2979 : : else
2980 : : {
2981 : : ConflictType type;
623 akapila@postgresql.o 2982 : 7 : TupleTableSlot *newslot = localslot;
2983 : :
2984 : : /*
2985 : : * Detecting whether the tuple was recently deleted or never existed
2986 : : * is crucial to avoid misleading the user during conflict handling.
2987 : : */
274 akapila@postgresql.o 2988 [ + + ]:GNC 7 : if (FindDeletedTupleInLocalRel(localrel, localindexoid, remoteslot,
2989 : : &conflicttuple.xmin,
2990 : : &conflicttuple.origin,
2991 : 3 : &conflicttuple.ts) &&
97 msawada@postgresql.o 2992 [ + - ]: 3 : conflicttuple.origin != replorigin_xact_state.origin)
274 akapila@postgresql.o 2993 : 3 : type = CT_UPDATE_DELETED;
2994 : : else
2995 : 4 : type = CT_UPDATE_MISSING;
2996 : :
2997 : : /* Store the new tuple for conflict reporting */
623 akapila@postgresql.o 2998 :CBC 7 : slot_store_data(newslot, relmapentry, newtup);
2999 : :
3000 : : /*
3001 : : * The tuple to be updated could not be found or was deleted. Do
3002 : : * nothing except for emitting a log message.
3003 : : */
274 akapila@postgresql.o 3004 :GNC 7 : ReportApplyConflict(estate, relinfo, LOG, type, remoteslot, newslot,
3005 : : list_make1(&conflicttuple));
3006 : : }
3007 : :
3008 : : /* Cleanup. */
2233 peter@eisentraut.org 3009 :CBC 31924 : ExecCloseIndices(relinfo);
3393 peter_e@gmx.net 3010 : 31924 : EvalPlanQualEnd(&epqstate);
3011 : 31924 : }
3012 : :
3013 : : /*
3014 : : * Handle DELETE message.
3015 : : *
3016 : : * TODO: FDW support
3017 : : */
3018 : : static void
3019 : 81942 : apply_handle_delete(StringInfo s)
3020 : : {
3021 : : LogicalRepRelMapEntry *rel;
3022 : : LogicalRepTupleData oldtup;
3023 : : LogicalRepRelId relid;
3024 : : UserContext ucxt;
3025 : : ApplyExecutionData *edata;
3026 : : EState *estate;
3027 : : TupleTableSlot *remoteslot;
3028 : : MemoryContext oldctx;
3029 : : bool run_as_owner;
3030 : :
3031 : : /*
3032 : : * Quick return if we are skipping data modification changes or handling
3033 : : * streamed transactions.
3034 : : */
1505 akapila@postgresql.o 3035 [ + - + + ]: 163884 : if (is_skipping_changes() ||
3036 : 81942 : handle_streamed_transaction(LOGICAL_REP_MSG_DELETE, s))
2070 3037 : 41615 : return;
3038 : :
1790 tgl@sss.pgh.pa.us 3039 : 40327 : begin_replication_step();
3040 : :
3393 peter_e@gmx.net 3041 : 40327 : relid = logicalrep_read_delete(s, &oldtup);
3042 : 40327 : rel = logicalrep_rel_open(relid, RowExclusiveLock);
3330 3043 [ - + ]: 40327 : if (!should_apply_changes_for_rel(rel))
3044 : : {
3045 : : /*
3046 : : * The relation can't become interesting in the middle of the
3047 : : * transaction so it's safe to unlock it.
3048 : : */
3330 peter_e@gmx.net 3049 :UBC 0 : logicalrep_rel_close(rel, RowExclusiveLock);
1790 tgl@sss.pgh.pa.us 3050 : 0 : end_replication_step();
3330 peter_e@gmx.net 3051 : 0 : return;
3052 : : }
3053 : :
3054 : : /* Set relation for error callback */
1712 akapila@postgresql.o 3055 :CBC 40327 : apply_error_callback_arg.rel = rel;
3056 : :
3057 : : /* Check if we can do the delete. */
3393 peter_e@gmx.net 3058 : 40327 : check_relation_updatable(rel);
3059 : :
3060 : : /*
3061 : : * Make sure that any user-supplied code runs as the table owner, unless
3062 : : * the user has opted out of that behavior.
3063 : : */
1127 rhaas@postgresql.org 3064 : 40327 : run_as_owner = MySubscription->runasowner;
3065 [ + + ]: 40327 : if (!run_as_owner)
3066 : 40325 : SwitchToUntrustedUser(rel->localrel->rd_rel->relowner, &ucxt);
3067 : :
3068 : : /* Initialize the executor state. */
1809 tgl@sss.pgh.pa.us 3069 : 40327 : edata = create_edata_for_relation(rel);
3070 : 40327 : estate = edata->estate;
3000 andres@anarazel.de 3071 : 40327 : remoteslot = ExecInitExtraTupleSlot(estate,
2728 3072 : 40327 : RelationGetDescr(rel->localrel),
3073 : : &TTSOpsVirtual);
3074 : :
3075 : : /* Build the search tuple. */
3393 peter_e@gmx.net 3076 [ - + ]: 40327 : oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
2117 tgl@sss.pgh.pa.us 3077 : 40327 : slot_store_data(remoteslot, rel, &oldtup);
3393 peter_e@gmx.net 3078 : 40327 : MemoryContextSwitchTo(oldctx);
3079 : :
3080 : : /* For a partitioned table, apply delete to correct partition. */
2220 peter@eisentraut.org 3081 [ + + ]: 40327 : if (rel->localrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1809 tgl@sss.pgh.pa.us 3082 : 17 : apply_handle_tuple_routing(edata,
3083 : : remoteslot, NULL, CMD_DELETE);
3084 : : else
3085 : : {
440 3086 : 40310 : ResultRelInfo *relinfo = edata->targetRelInfo;
3087 : :
3088 : 40310 : ExecOpenIndices(relinfo, false);
3089 : 40310 : apply_handle_delete_internal(edata, relinfo,
3090 : : remoteslot, rel->localindexoid);
3091 : 40310 : ExecCloseIndices(relinfo);
3092 : : }
3093 : :
1809 3094 : 40327 : finish_edata(edata);
3095 : :
3096 : : /* Reset relation for error callback */
1712 akapila@postgresql.o 3097 : 40327 : apply_error_callback_arg.rel = NULL;
3098 : :
1127 rhaas@postgresql.org 3099 [ + + ]: 40327 : if (!run_as_owner)
3100 : 40325 : RestoreUserContext(&ucxt);
3101 : :
2233 peter@eisentraut.org 3102 : 40327 : logicalrep_rel_close(rel, NoLock);
3103 : :
1790 tgl@sss.pgh.pa.us 3104 : 40327 : end_replication_step();
3105 : : }
3106 : :
3107 : : /*
3108 : : * Workhorse for apply_handle_delete()
3109 : : * relinfo is for the relation we're actually deleting from
3110 : : * (could be a child partition of edata->targetRelInfo)
3111 : : */
3112 : : static void
1809 3113 : 40327 : apply_handle_delete_internal(ApplyExecutionData *edata,
3114 : : ResultRelInfo *relinfo,
3115 : : TupleTableSlot *remoteslot,
3116 : : Oid localindexoid)
3117 : : {
3118 : 40327 : EState *estate = edata->estate;
2233 peter@eisentraut.org 3119 : 40327 : Relation localrel = relinfo->ri_RelationDesc;
1809 tgl@sss.pgh.pa.us 3120 : 40327 : LogicalRepRelation *remoterel = &edata->targetRel->remoterel;
3121 : : EPQState epqstate;
3122 : : TupleTableSlot *localslot;
407 akapila@postgresql.o 3123 : 40327 : ConflictTupleInfo conflicttuple = {0};
3124 : : bool found;
3125 : :
1082 tgl@sss.pgh.pa.us 3126 : 40327 : EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1, NIL);
3127 : :
3128 : : /* Caller should have opened indexes already. */
440 3129 [ + + + + : 40327 : Assert(relinfo->ri_IndexRelationDescs != NULL ||
- + ]
3130 : : !localrel->rd_rel->relhasindex ||
3131 : : RelationGetIndexList(localrel) == NIL);
3132 : :
1015 msawada@postgresql.o 3133 : 40327 : found = FindReplTupleInLocalRel(edata, localrel, remoterel, localindexoid,
3134 : : remoteslot, &localslot);
3135 : :
3136 : : /* If found delete it. */
3393 peter_e@gmx.net 3137 [ + + ]: 40327 : if (found)
3138 : : {
3139 : : /*
3140 : : * Report the conflict if the tuple was modified by a different
3141 : : * origin.
3142 : : */
407 akapila@postgresql.o 3143 [ + + ]: 40318 : if (GetTupleTransactionInfo(localslot, &conflicttuple.xmin,
3144 : 6 : &conflicttuple.origin, &conflicttuple.ts) &&
97 msawada@postgresql.o 3145 [ + + ]:GNC 6 : conflicttuple.origin != replorigin_xact_state.origin)
3146 : : {
407 akapila@postgresql.o 3147 :CBC 5 : conflicttuple.slot = localslot;
614 3148 : 5 : ReportApplyConflict(estate, relinfo, LOG, CT_DELETE_ORIGIN_DIFFERS,
3149 : : remoteslot, NULL,
407 akapila@postgresql.o 3150 :ECB (2) : list_make1(&conflicttuple));
3151 : : }
3152 : :
3393 peter_e@gmx.net 3153 :CBC 40318 : EvalPlanQualSetSlot(&epqstate, localslot);
3154 : :
3155 : : /* Do the actual delete. */
1579 jdavis@postgresql.or 3156 : 40318 : TargetPrivilegesCheck(relinfo->ri_RelationDesc, ACL_DELETE);
2029 heikki.linnakangas@i 3157 : 40318 : ExecSimpleRelationDelete(relinfo, estate, &epqstate, localslot);
3158 : : }
3159 : : else
3160 : : {
3161 : : /*
3162 : : * The tuple to be deleted could not be found. Do nothing except for
3163 : : * emitting a log message.
3164 : : */
623 akapila@postgresql.o 3165 : 9 : ReportApplyConflict(estate, relinfo, LOG, CT_DELETE_MISSING,
407 akapila@postgresql.o 3166 :ECB (9) : remoteslot, NULL, list_make1(&conflicttuple));
3167 : : }
3168 : :
3169 : : /* Cleanup. */
3393 peter_e@gmx.net 3170 :CBC 40327 : EvalPlanQualEnd(&epqstate);
3171 : 40327 : }
3172 : :
3173 : : /*
3174 : : * Try to find a tuple received from the publication side (in 'remoteslot') in
3175 : : * the corresponding local relation using either replica identity index,
3176 : : * primary key, index or if needed, sequential scan.
3177 : : *
3178 : : * Local tuple, if found, is returned in '*localslot'.
3179 : : */
3180 : : static bool
1015 msawada@postgresql.o 3181 : 72271 : FindReplTupleInLocalRel(ApplyExecutionData *edata, Relation localrel,
3182 : : LogicalRepRelation *remoterel,
3183 : : Oid localidxoid,
3184 : : TupleTableSlot *remoteslot,
3185 : : TupleTableSlot **localslot)
3186 : : {
3187 : 72271 : EState *estate = edata->estate;
3188 : : bool found;
3189 : :
3190 : : /*
3191 : : * Regardless of the top-level operation, we're performing a read here, so
3192 : : * check for SELECT privileges.
3193 : : */
1578 jdavis@postgresql.or 3194 : 72271 : TargetPrivilegesCheck(localrel, ACL_SELECT);
3195 : :
2225 peter@eisentraut.org 3196 : 72266 : *localslot = table_slot_create(localrel, &estate->es_tupleTable);
3197 : :
1147 akapila@postgresql.o 3198 [ + + - + ]: 72266 : Assert(OidIsValid(localidxoid) ||
3199 : : (remoterel->replident == REPLICA_IDENTITY_FULL));
3200 : :
3201 [ + + ]: 72266 : if (OidIsValid(localidxoid))
3202 : : {
3203 : : #ifdef USE_ASSERT_CHECKING
1015 msawada@postgresql.o 3204 : 72113 : Relation idxrel = index_open(localidxoid, AccessShareLock);
3205 : :
3206 : : /* Index must be PK, RI, or usable for REPLICA IDENTITY FULL tables */
601 akapila@postgresql.o 3207 [ + + + - : 72113 : Assert(GetRelationIdentityOrPK(localrel) == localidxoid ||
- + ]
3208 : : (remoterel->replident == REPLICA_IDENTITY_FULL &&
3209 : : IsIndexUsableForReplicaIdentityFull(idxrel,
3210 : : edata->targetRel->attrmap)));
1015 msawada@postgresql.o 3211 : 72113 : index_close(idxrel, AccessShareLock);
3212 : : #endif
3213 : :
1147 akapila@postgresql.o 3214 : 72113 : found = RelationFindReplTupleByIndex(localrel, localidxoid,
3215 : : LockTupleExclusive,
3216 : : remoteslot, *localslot);
3217 : : }
3218 : : else
2225 peter@eisentraut.org 3219 : 153 : found = RelationFindReplTupleSeq(localrel, LockTupleExclusive,
3220 : : remoteslot, *localslot);
3221 : :
3222 : 72266 : return found;
3223 : : }
3224 : :
3225 : : /*
3226 : : * Determine whether the index can reliably locate the deleted tuple in the
3227 : : * local relation.
3228 : : *
3229 : : * An index may exclude deleted tuples if it was re-indexed or re-created during
3230 : : * change application. Therefore, an index is considered usable only if the
3231 : : * conflict detection slot.xmin (conflict_detection_xmin) is greater than the
3232 : : * index tuple's xmin. This ensures that any tuples deleted prior to the index
3233 : : * creation or re-indexing are not relevant for conflict detection in the
3234 : : * current apply worker.
3235 : : *
3236 : : * Note that indexes may also be excluded if they were modified by other DDL
3237 : : * operations, such as ALTER INDEX. However, this is acceptable, as the
3238 : : * likelihood of such DDL changes coinciding with the need to scan dead
3239 : : * tuples for the update_deleted is low.
3240 : : */
3241 : : static bool
274 akapila@postgresql.o 3242 :GNC 1 : IsIndexUsableForFindingDeletedTuple(Oid localindexoid,
3243 : : TransactionId conflict_detection_xmin)
3244 : : {
3245 : : HeapTuple index_tuple;
3246 : : TransactionId index_xmin;
3247 : :
3248 : 1 : index_tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(localindexoid));
3249 : :
3250 [ - + ]: 1 : if (!HeapTupleIsValid(index_tuple)) /* should not happen */
274 akapila@postgresql.o 3251 [ # # ]:UNC 0 : elog(ERROR, "cache lookup failed for index %u", localindexoid);
3252 : :
3253 : : /*
3254 : : * No need to check for a frozen transaction ID, as
3255 : : * TransactionIdPrecedes() manages it internally, treating it as falling
3256 : : * behind the conflict_detection_xmin.
3257 : : */
274 akapila@postgresql.o 3258 :GNC 1 : index_xmin = HeapTupleHeaderGetXmin(index_tuple->t_data);
3259 : :
3260 : 1 : ReleaseSysCache(index_tuple);
3261 : :
3262 : 1 : return TransactionIdPrecedes(index_xmin, conflict_detection_xmin);
3263 : : }
3264 : :
3265 : : /*
3266 : : * Attempts to locate a deleted tuple in the local relation that matches the
3267 : : * values of the tuple received from the publication side (in 'remoteslot').
3268 : : * The search is performed using either the replica identity index, primary
3269 : : * key, other available index, or a sequential scan if necessary.
3270 : : *
3271 : : * Returns true if the deleted tuple is found. If found, the transaction ID,
3272 : : * origin, and commit timestamp of the deletion are stored in '*delete_xid',
3273 : : * '*delete_origin', and '*delete_time' respectively.
3274 : : */
3275 : : static bool
3276 : 9 : FindDeletedTupleInLocalRel(Relation localrel, Oid localidxoid,
3277 : : TupleTableSlot *remoteslot,
3278 : : TransactionId *delete_xid, ReplOriginId *delete_origin,
3279 : : TimestampTz *delete_time)
3280 : : {
3281 : : TransactionId oldestxmin;
3282 : :
3283 : : /*
3284 : : * Return false if either dead tuples are not retained or commit timestamp
3285 : : * data is not available.
3286 : : */
3287 [ + + - + ]: 9 : if (!MySubscription->retaindeadtuples || !track_commit_timestamp)
3288 : 6 : return false;
3289 : :
3290 : : /*
3291 : : * For conflict detection, we use the leader worker's
3292 : : * oldest_nonremovable_xid value instead of invoking
3293 : : * GetOldestNonRemovableTransactionId() or using the conflict detection
3294 : : * slot's xmin. The oldest_nonremovable_xid acts as a threshold to
3295 : : * identify tuples that were recently deleted. These deleted tuples are no
3296 : : * longer visible to concurrent transactions. However, if a remote update
3297 : : * matches such a tuple, we log an update_deleted conflict.
3298 : : *
3299 : : * While GetOldestNonRemovableTransactionId() and slot.xmin may return
3300 : : * transaction IDs older than oldest_nonremovable_xid, for our current
3301 : : * purpose, it is acceptable to treat tuples deleted by transactions prior
3302 : : * to oldest_nonremovable_xid as update_missing conflicts.
3303 : : */
245 3304 [ + - ]: 3 : if (am_leader_apply_worker())
3305 : : {
3306 : 3 : oldestxmin = MyLogicalRepWorker->oldest_nonremovable_xid;
3307 : : }
3308 : : else
3309 : : {
3310 : : LogicalRepWorker *leader;
3311 : :
3312 : : /*
3313 : : * Obtain the information from the leader apply worker as only the
3314 : : * leader manages oldest_nonremovable_xid (see
3315 : : * maybe_advance_nonremovable_xid() for details).
3316 : : */
245 akapila@postgresql.o 3317 :UNC 0 : LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
189 3318 : 0 : leader = logicalrep_worker_find(WORKERTYPE_APPLY,
3319 : 0 : MyLogicalRepWorker->subid, InvalidOid,
3320 : : false);
238 3321 [ # # ]: 0 : if (!leader)
3322 : : {
3323 [ # # ]: 0 : ereport(ERROR,
3324 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3325 : : errmsg("could not detect conflict as the leader apply worker has exited")));
3326 : : }
3327 : :
245 3328 : 0 : SpinLockAcquire(&leader->relmutex);
3329 : 0 : oldestxmin = leader->oldest_nonremovable_xid;
3330 : 0 : SpinLockRelease(&leader->relmutex);
3331 : 0 : LWLockRelease(LogicalRepWorkerLock);
3332 : : }
3333 : :
3334 : : /*
3335 : : * Return false if the leader apply worker has stopped retaining
3336 : : * information for detecting conflicts. This implies that update_deleted
3337 : : * can no longer be reliably detected.
3338 : : */
245 akapila@postgresql.o 3339 [ - + ]:GNC 3 : if (!TransactionIdIsValid(oldestxmin))
245 akapila@postgresql.o 3340 :UNC 0 : return false;
3341 : :
274 akapila@postgresql.o 3342 [ + + + - ]:GNC 4 : if (OidIsValid(localidxoid) &&
3343 : 1 : IsIndexUsableForFindingDeletedTuple(localidxoid, oldestxmin))
3344 : 1 : return RelationFindDeletedTupleInfoByIndex(localrel, localidxoid,
3345 : : remoteslot, oldestxmin,
3346 : : delete_xid, delete_origin,
3347 : : delete_time);
3348 : : else
3349 : 2 : return RelationFindDeletedTupleInfoSeq(localrel, remoteslot,
3350 : : oldestxmin, delete_xid,
3351 : : delete_origin, delete_time);
3352 : : }
3353 : :
3354 : : /*
3355 : : * This handles insert, update, delete on a partitioned table.
3356 : : */
3357 : : static void
1809 tgl@sss.pgh.pa.us 3358 :CBC 99 : apply_handle_tuple_routing(ApplyExecutionData *edata,
3359 : : TupleTableSlot *remoteslot,
3360 : : LogicalRepTupleData *newtup,
3361 : : CmdType operation)
3362 : : {
3363 : 99 : EState *estate = edata->estate;
3364 : 99 : LogicalRepRelMapEntry *relmapentry = edata->targetRel;
3365 : 99 : ResultRelInfo *relinfo = edata->targetRelInfo;
2220 peter@eisentraut.org 3366 : 99 : Relation parentrel = relinfo->ri_RelationDesc;
3367 : : ModifyTableState *mtstate;
3368 : : PartitionTupleRouting *proute;
3369 : : ResultRelInfo *partrelinfo;
3370 : : Relation partrel;
3371 : : TupleTableSlot *remoteslot_part;
3372 : : TupleConversionMap *map;
3373 : : MemoryContext oldctx;
1414 akapila@postgresql.o 3374 : 99 : LogicalRepRelMapEntry *part_entry = NULL;
3375 : 99 : AttrMap *attrmap = NULL;
3376 : :
3377 : : /* ModifyTableState is needed for ExecFindPartition(). */
1809 tgl@sss.pgh.pa.us 3378 : 99 : edata->mtstate = mtstate = makeNode(ModifyTableState);
2220 peter@eisentraut.org 3379 : 99 : mtstate->ps.plan = NULL;
3380 : 99 : mtstate->ps.state = estate;
3381 : 99 : mtstate->operation = operation;
3382 : 99 : mtstate->resultRelInfo = relinfo;
3383 : :
3384 : : /* ... as is PartitionTupleRouting. */
1809 tgl@sss.pgh.pa.us 3385 : 99 : edata->proute = proute = ExecSetupPartitionTupleRouting(estate, parentrel);
3386 : :
3387 : : /*
3388 : : * Find the partition to which the "search tuple" belongs.
3389 : : */
2220 peter@eisentraut.org 3390 [ - + ]: 99 : Assert(remoteslot != NULL);
3391 [ + - ]: 99 : oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
3392 : 99 : partrelinfo = ExecFindPartition(mtstate, relinfo, proute,
3393 : : remoteslot, estate);
3394 [ - + ]: 99 : Assert(partrelinfo != NULL);
3395 : 99 : partrel = partrelinfo->ri_RelationDesc;
3396 : :
3397 : : /*
3398 : : * Check for supported relkind. We need this since partitions might be of
3399 : : * unsupported relkinds; and the set of partitions can change, so checking
3400 : : * at CREATE/ALTER SUBSCRIPTION would be insufficient.
3401 : : */
1280 tgl@sss.pgh.pa.us 3402 : 99 : CheckSubscriptionRelkind(partrel->rd_rel->relkind,
194 akapila@postgresql.o 3403 :GNC 99 : relmapentry->remoterel.relkind,
1280 tgl@sss.pgh.pa.us 3404 :CBC 99 : get_namespace_name(RelationGetNamespace(partrel)),
3405 : 99 : RelationGetRelationName(partrel));
3406 : :
3407 : : /*
3408 : : * To perform any of the operations below, the tuple must match the
3409 : : * partition's rowtype. Convert if needed or just copy, using a dedicated
3410 : : * slot to store the tuple in any case.
3411 : : */
2024 heikki.linnakangas@i 3412 : 99 : remoteslot_part = partrelinfo->ri_PartitionTupleSlot;
2220 peter@eisentraut.org 3413 [ + + ]: 99 : if (remoteslot_part == NULL)
3414 : 66 : remoteslot_part = table_slot_create(partrel, &estate->es_tupleTable);
1250 alvherre@alvh.no-ip. 3415 : 99 : map = ExecGetRootToChildMap(partrelinfo, estate);
2220 peter@eisentraut.org 3416 [ + + ]: 99 : if (map != NULL)
3417 : : {
1414 akapila@postgresql.o 3418 : 33 : attrmap = map->attrMap;
3419 : 33 : remoteslot_part = execute_attr_map_slot(attrmap, remoteslot,
3420 : : remoteslot_part);
3421 : : }
3422 : : else
3423 : : {
2220 peter@eisentraut.org 3424 : 66 : remoteslot_part = ExecCopySlot(remoteslot_part, remoteslot);
3425 : 66 : slot_getallattrs(remoteslot_part);
3426 : : }
3427 : 99 : MemoryContextSwitchTo(oldctx);
3428 : :
3429 : : /* Check if we can do the update or delete on the leaf partition. */
1414 akapila@postgresql.o 3430 [ + + + + ]: 99 : if (operation == CMD_UPDATE || operation == CMD_DELETE)
3431 : : {
3432 : 30 : part_entry = logicalrep_partition_open(relmapentry, partrel,
3433 : : attrmap);
3434 : 30 : check_relation_updatable(part_entry);
3435 : : }
3436 : :
2220 peter@eisentraut.org 3437 [ + + + - ]: 99 : switch (operation)
3438 : : {
3439 : 69 : case CMD_INSERT:
1809 tgl@sss.pgh.pa.us 3440 : 69 : apply_handle_insert_internal(edata, partrelinfo,
3441 : : remoteslot_part);
2220 peter@eisentraut.org 3442 : 44 : break;
3443 : :
3444 : 17 : case CMD_DELETE:
1809 tgl@sss.pgh.pa.us 3445 : 17 : apply_handle_delete_internal(edata, partrelinfo,
3446 : : remoteslot_part,
3447 : : part_entry->localindexoid);
2220 peter@eisentraut.org 3448 : 17 : break;
3449 : :
3450 : 13 : case CMD_UPDATE:
3451 : :
3452 : : /*
3453 : : * For UPDATE, depending on whether or not the updated tuple
3454 : : * satisfies the partition's constraint, perform a simple UPDATE
3455 : : * of the partition or move the updated tuple into a different
3456 : : * suitable partition.
3457 : : */
3458 : : {
3459 : : TupleTableSlot *localslot;
3460 : : ResultRelInfo *partrelinfo_new;
3461 : : Relation partrel_new;
3462 : : bool found;
3463 : : EPQState epqstate;
407 akapila@postgresql.o 3464 : 13 : ConflictTupleInfo conflicttuple = {0};
3465 : :
3466 : : /* Get the matching local tuple from the partition. */
1015 msawada@postgresql.o 3467 : 13 : found = FindReplTupleInLocalRel(edata, partrel,
3468 : : &part_entry->remoterel,
3469 : : part_entry->localindexoid,
3470 : : remoteslot_part, &localslot);
1789 tgl@sss.pgh.pa.us 3471 [ + + ]: 13 : if (!found)
3472 : : {
3473 : : ConflictType type;
623 akapila@postgresql.o 3474 : 2 : TupleTableSlot *newslot = localslot;
3475 : :
3476 : : /*
3477 : : * Detecting whether the tuple was recently deleted or
3478 : : * never existed is crucial to avoid misleading the user
3479 : : * during conflict handling.
3480 : : */
274 akapila@postgresql.o 3481 [ - + ]:GNC 2 : if (FindDeletedTupleInLocalRel(partrel,
3482 : : part_entry->localindexoid,
3483 : : remoteslot_part,
3484 : : &conflicttuple.xmin,
3485 : : &conflicttuple.origin,
274 akapila@postgresql.o 3486 :UNC 0 : &conflicttuple.ts) &&
97 msawada@postgresql.o 3487 [ # # ]: 0 : conflicttuple.origin != replorigin_xact_state.origin)
274 akapila@postgresql.o 3488 : 0 : type = CT_UPDATE_DELETED;
3489 : : else
274 akapila@postgresql.o 3490 :GNC 2 : type = CT_UPDATE_MISSING;
3491 : :
3492 : : /* Store the new tuple for conflict reporting */
623 akapila@postgresql.o 3493 :CBC 2 : slot_store_data(newslot, part_entry, newtup);
3494 : :
3495 : : /*
3496 : : * The tuple to be updated could not be found or was
3497 : : * deleted. Do nothing except for emitting a log message.
3498 : : */
407 3499 : 2 : ReportApplyConflict(estate, partrelinfo, LOG,
3500 : : type, remoteslot_part, newslot,
3501 : : list_make1(&conflicttuple));
3502 : :
1789 tgl@sss.pgh.pa.us 3503 : 2 : return;
3504 : : }
3505 : :
3506 : : /*
3507 : : * Report the conflict if the tuple was modified by a
3508 : : * different origin.
3509 : : */
407 akapila@postgresql.o 3510 [ + + ]: 11 : if (GetTupleTransactionInfo(localslot, &conflicttuple.xmin,
3511 : : &conflicttuple.origin,
3512 : 1 : &conflicttuple.ts) &&
97 msawada@postgresql.o 3513 [ + - ]:GNC 1 : conflicttuple.origin != replorigin_xact_state.origin)
3514 : : {
3515 : : TupleTableSlot *newslot;
3516 : :
3517 : : /* Store the new tuple for conflict reporting */
623 akapila@postgresql.o 3518 :CBC 1 : newslot = table_slot_create(partrel, &estate->es_tupleTable);
3519 : 1 : slot_store_data(newslot, part_entry, newtup);
3520 : :
407 3521 : 1 : conflicttuple.slot = localslot;
3522 : :
614 3523 : 1 : ReportApplyConflict(estate, partrelinfo, LOG, CT_UPDATE_ORIGIN_DIFFERS,
3524 : : remoteslot_part, newslot,
407 akapila@postgresql.o 3525 :ECB (1) : list_make1(&conflicttuple));
3526 : : }
3527 : :
3528 : : /*
3529 : : * Apply the update to the local tuple, putting the result in
3530 : : * remoteslot_part.
3531 : : */
1789 tgl@sss.pgh.pa.us 3532 [ + - ]:CBC 11 : oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
3533 : 11 : slot_modify_data(remoteslot_part, localslot, part_entry,
3534 : : newtup);
3535 : 11 : MemoryContextSwitchTo(oldctx);
3536 : :
642 akapila@postgresql.o 3537 : 11 : EvalPlanQualInit(&epqstate, estate, NULL, NIL, -1, NIL);
3538 : :
3539 : : /*
3540 : : * Does the updated tuple still satisfy the current
3541 : : * partition's constraint?
3542 : : */
2057 tgl@sss.pgh.pa.us 3543 [ + - + + ]: 22 : if (!partrel->rd_rel->relispartition ||
2220 peter@eisentraut.org 3544 : 11 : ExecPartitionCheck(partrelinfo, remoteslot_part, estate,
3545 : : false))
3546 : : {
3547 : : /*
3548 : : * Yes, so simply UPDATE the partition. We don't call
3549 : : * apply_handle_update_internal() here, which would
3550 : : * normally do the following work, to avoid repeating some
3551 : : * work already done above to find the local tuple in the
3552 : : * partition.
3553 : : */
623 akapila@postgresql.o 3554 : 10 : InitConflictIndexes(partrelinfo);
3555 : :
2220 peter@eisentraut.org 3556 : 10 : EvalPlanQualSetSlot(&epqstate, remoteslot_part);
1579 jdavis@postgresql.or 3557 : 10 : TargetPrivilegesCheck(partrelinfo->ri_RelationDesc,
3558 : : ACL_UPDATE);
2029 heikki.linnakangas@i 3559 : 10 : ExecSimpleRelationUpdate(partrelinfo, estate, &epqstate,
3560 : : localslot, remoteslot_part);
3561 : : }
3562 : : else
3563 : : {
3564 : : /* Move the tuple into the new partition. */
3565 : :
3566 : : /*
3567 : : * New partition will be found using tuple routing, which
3568 : : * can only occur via the parent table. We might need to
3569 : : * convert the tuple to the parent's rowtype. Note that
3570 : : * this is the tuple found in the partition, not the
3571 : : * original search tuple received by this function.
3572 : : */
2220 peter@eisentraut.org 3573 [ + - ]: 1 : if (map)
3574 : : {
3575 : : TupleConversionMap *PartitionToRootMap =
1082 tgl@sss.pgh.pa.us 3576 : 1 : convert_tuples_by_name(RelationGetDescr(partrel),
3577 : : RelationGetDescr(parentrel));
3578 : :
3579 : : remoteslot =
2220 peter@eisentraut.org 3580 : 1 : execute_attr_map_slot(PartitionToRootMap->attrMap,
3581 : : remoteslot_part, remoteslot);
3582 : : }
3583 : : else
3584 : : {
2220 peter@eisentraut.org 3585 :UBC 0 : remoteslot = ExecCopySlot(remoteslot, remoteslot_part);
3586 : 0 : slot_getallattrs(remoteslot);
3587 : : }
3588 : :
3589 : : /* Find the new partition. */
2220 peter@eisentraut.org 3590 [ + - ]:CBC 1 : oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
3591 : 1 : partrelinfo_new = ExecFindPartition(mtstate, relinfo,
3592 : : proute, remoteslot,
3593 : : estate);
3594 : 1 : MemoryContextSwitchTo(oldctx);
3595 [ - + ]: 1 : Assert(partrelinfo_new != partrelinfo);
1280 tgl@sss.pgh.pa.us 3596 : 1 : partrel_new = partrelinfo_new->ri_RelationDesc;
3597 : :
3598 : : /* Check that new partition also has supported relkind. */
3599 : 1 : CheckSubscriptionRelkind(partrel_new->rd_rel->relkind,
194 akapila@postgresql.o 3600 :GNC 1 : relmapentry->remoterel.relkind,
1280 tgl@sss.pgh.pa.us 3601 :CBC 1 : get_namespace_name(RelationGetNamespace(partrel_new)),
3602 : 1 : RelationGetRelationName(partrel_new));
3603 : :
3604 : : /* DELETE old tuple found in the old partition. */
642 akapila@postgresql.o 3605 : 1 : EvalPlanQualSetSlot(&epqstate, localslot);
3606 : 1 : TargetPrivilegesCheck(partrelinfo->ri_RelationDesc, ACL_DELETE);
3607 : 1 : ExecSimpleRelationDelete(partrelinfo, estate, &epqstate, localslot);
3608 : :
3609 : : /* INSERT new tuple into the new partition. */
3610 : :
3611 : : /*
3612 : : * Convert the replacement tuple to match the destination
3613 : : * partition rowtype.
3614 : : */
2220 peter@eisentraut.org 3615 [ + - ]: 1 : oldctx = MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
2024 heikki.linnakangas@i 3616 : 1 : remoteslot_part = partrelinfo_new->ri_PartitionTupleSlot;
2220 peter@eisentraut.org 3617 [ + - ]: 1 : if (remoteslot_part == NULL)
1280 tgl@sss.pgh.pa.us 3618 : 1 : remoteslot_part = table_slot_create(partrel_new,
3619 : : &estate->es_tupleTable);
1250 alvherre@alvh.no-ip. 3620 : 1 : map = ExecGetRootToChildMap(partrelinfo_new, estate);
2220 peter@eisentraut.org 3621 [ - + ]: 1 : if (map != NULL)
3622 : : {
2220 peter@eisentraut.org 3623 :UBC 0 : remoteslot_part = execute_attr_map_slot(map->attrMap,
3624 : : remoteslot,
3625 : : remoteslot_part);
3626 : : }
3627 : : else
3628 : : {
2220 peter@eisentraut.org 3629 :CBC 1 : remoteslot_part = ExecCopySlot(remoteslot_part,
3630 : : remoteslot);
3631 : 1 : slot_getallattrs(remoteslot);
3632 : : }
3633 : 1 : MemoryContextSwitchTo(oldctx);
1809 tgl@sss.pgh.pa.us 3634 : 1 : apply_handle_insert_internal(edata, partrelinfo_new,
3635 : : remoteslot_part);
3636 : : }
3637 : :
642 akapila@postgresql.o 3638 : 11 : EvalPlanQualEnd(&epqstate);
3639 : : }
2220 peter@eisentraut.org 3640 : 11 : break;
3641 : :
2220 peter@eisentraut.org 3642 :UBC 0 : default:
3643 [ # # ]: 0 : elog(ERROR, "unrecognized CmdType: %d", (int) operation);
3644 : : break;
3645 : : }
3646 : : }
3647 : :
3648 : : /*
3649 : : * Handle TRUNCATE message.
3650 : : *
3651 : : * TODO: FDW support
3652 : : */
3653 : : static void
2950 peter_e@gmx.net 3654 :CBC 20 : apply_handle_truncate(StringInfo s)
3655 : : {
2931 tgl@sss.pgh.pa.us 3656 : 20 : bool cascade = false;
3657 : 20 : bool restart_seqs = false;
3658 : 20 : List *remote_relids = NIL;
3659 : 20 : List *remote_rels = NIL;
3660 : 20 : List *rels = NIL;
2220 peter@eisentraut.org 3661 : 20 : List *part_rels = NIL;
2931 tgl@sss.pgh.pa.us 3662 : 20 : List *relids = NIL;
3663 : 20 : List *relids_logged = NIL;
3664 : : ListCell *lc;
1810 3665 : 20 : LOCKMODE lockmode = AccessExclusiveLock;
3666 : :
3667 : : /*
3668 : : * Quick return if we are skipping data modification changes or handling
3669 : : * streamed transactions.
3670 : : */
1505 akapila@postgresql.o 3671 [ + - - + ]: 40 : if (is_skipping_changes() ||
3672 : 20 : handle_streamed_transaction(LOGICAL_REP_MSG_TRUNCATE, s))
2070 akapila@postgresql.o 3673 :UBC 0 : return;
3674 : :
1790 tgl@sss.pgh.pa.us 3675 :CBC 20 : begin_replication_step();
3676 : :
2950 peter_e@gmx.net 3677 : 20 : remote_relids = logicalrep_read_truncate(s, &cascade, &restart_seqs);
3678 : :
3679 [ + - + + : 49 : foreach(lc, remote_relids)
+ + ]
3680 : : {
3681 : 29 : LogicalRepRelId relid = lfirst_oid(lc);
3682 : : LogicalRepRelMapEntry *rel;
3683 : :
1810 akapila@postgresql.o 3684 : 29 : rel = logicalrep_rel_open(relid, lockmode);
2950 peter_e@gmx.net 3685 [ - + ]: 29 : if (!should_apply_changes_for_rel(rel))
3686 : : {
3687 : : /*
3688 : : * The relation can't become interesting in the middle of the
3689 : : * transaction so it's safe to unlock it.
3690 : : */
1810 akapila@postgresql.o 3691 :UBC 0 : logicalrep_rel_close(rel, lockmode);
2950 peter_e@gmx.net 3692 : 0 : continue;
3693 : : }
3694 : :
2950 peter_e@gmx.net 3695 :CBC 29 : remote_rels = lappend(remote_rels, rel);
1579 jdavis@postgresql.or 3696 : 29 : TargetPrivilegesCheck(rel->localrel, ACL_TRUNCATE);
2950 peter_e@gmx.net 3697 : 29 : rels = lappend(rels, rel->localrel);
3698 : 29 : relids = lappend_oid(relids, rel->localreloid);
3699 [ + + - + : 29 : if (RelationIsLogicallyLogged(rel->localrel))
+ - - + -
- - - + -
+ - ]
2934 peter_e@gmx.net 3700 :GBC 1 : relids_logged = lappend_oid(relids_logged, rel->localreloid);
3701 : :
3702 : : /*
3703 : : * Truncate partitions if we got a message to truncate a partitioned
3704 : : * table.
3705 : : */
2220 peter@eisentraut.org 3706 [ + + ]:CBC 29 : if (rel->localrel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
3707 : : {
3708 : : ListCell *child;
3709 : 4 : List *children = find_all_inheritors(rel->localreloid,
3710 : : lockmode,
3711 : : NULL);
3712 : :
3713 [ + - + + : 15 : foreach(child, children)
+ + ]
3714 : : {
3715 : 11 : Oid childrelid = lfirst_oid(child);
3716 : : Relation childrel;
3717 : :
3718 [ + + ]: 11 : if (list_member_oid(relids, childrelid))
3719 : 4 : continue;
3720 : :
3721 : : /* find_all_inheritors already got lock */
3722 : 7 : childrel = table_open(childrelid, NoLock);
3723 : :
3724 : : /*
3725 : : * Ignore temp tables of other backends. See similar code in
3726 : : * ExecuteTruncate().
3727 : : */
3728 [ - + - - ]: 7 : if (RELATION_IS_OTHER_TEMP(childrel))
3729 : : {
1810 akapila@postgresql.o 3730 :UBC 0 : table_close(childrel, lockmode);
2220 peter@eisentraut.org 3731 : 0 : continue;
3732 : : }
3733 : :
1579 jdavis@postgresql.or 3734 :CBC 7 : TargetPrivilegesCheck(childrel, ACL_TRUNCATE);
2220 peter@eisentraut.org 3735 : 7 : rels = lappend(rels, childrel);
3736 : 7 : part_rels = lappend(part_rels, childrel);
3737 : 7 : relids = lappend_oid(relids, childrelid);
3738 : : /* Log this relation only if needed for logical decoding */
3739 [ + - - + : 7 : if (RelationIsLogicallyLogged(childrel))
- - - - -
- - - - -
- - ]
2220 peter@eisentraut.org 3740 :UBC 0 : relids_logged = lappend_oid(relids_logged, childrelid);
3741 : : }
3742 : : }
3743 : : }
3744 : :
3745 : : /*
3746 : : * Even if we used CASCADE on the upstream primary we explicitly default
3747 : : * to replaying changes without further cascading. This might be later
3748 : : * changeable with a user specified option.
3749 : : *
3750 : : * MySubscription->runasowner tells us whether we want to execute
3751 : : * replication actions as the subscription owner; the last argument to
3752 : : * TruncateGuts tells it whether we want to switch to the table owner.
3753 : : * Those are exactly opposite conditions.
3754 : : */
1853 fujii@postgresql.org 3755 :CBC 20 : ExecuteTruncateGuts(rels,
3756 : : relids,
3757 : : relids_logged,
3758 : : DROP_RESTRICT,
3759 : : restart_seqs,
1127 rhaas@postgresql.org 3760 : 20 : !MySubscription->runasowner);
2950 peter_e@gmx.net 3761 [ + - + + : 49 : foreach(lc, remote_rels)
+ + ]
3762 : : {
3763 : 29 : LogicalRepRelMapEntry *rel = lfirst(lc);
3764 : :
3765 : 29 : logicalrep_rel_close(rel, NoLock);
3766 : : }
2220 peter@eisentraut.org 3767 [ + + + + : 27 : foreach(lc, part_rels)
+ + ]
3768 : : {
3769 : 7 : Relation rel = lfirst(lc);
3770 : :
3771 : 7 : table_close(rel, NoLock);
3772 : : }
3773 : :
1790 tgl@sss.pgh.pa.us 3774 : 20 : end_replication_step();
3775 : : }
3776 : :
3777 : :
3778 : : /*
3779 : : * Logical replication protocol message dispatcher.
3780 : : */
3781 : : void
3393 peter_e@gmx.net 3782 : 357867 : apply_dispatch(StringInfo s)
3783 : : {
2010 akapila@postgresql.o 3784 : 357867 : LogicalRepMsgType action = pq_getmsgbyte(s);
3785 : : LogicalRepMsgType saved_command;
3786 : :
3787 : : /*
3788 : : * Set the current command being applied. Since this function can be
3789 : : * called recursively when applying spooled changes, save the current
3790 : : * command.
3791 : : */
1712 3792 : 357867 : saved_command = apply_error_callback_arg.command;
3793 : 357867 : apply_error_callback_arg.command = action;
3794 : :
3393 peter_e@gmx.net 3795 [ + + + + : 357867 : switch (action)
+ + + + +
- + + + +
+ + + + +
- ]
3796 : : {
2010 akapila@postgresql.o 3797 : 521 : case LOGICAL_REP_MSG_BEGIN:
3393 peter_e@gmx.net 3798 : 521 : apply_handle_begin(s);
1712 akapila@postgresql.o 3799 : 521 : break;
3800 : :
2010 3801 : 461 : case LOGICAL_REP_MSG_COMMIT:
3393 peter_e@gmx.net 3802 : 461 : apply_handle_commit(s);
1712 akapila@postgresql.o 3803 : 461 : break;
3804 : :
2010 3805 : 206378 : case LOGICAL_REP_MSG_INSERT:
3393 peter_e@gmx.net 3806 : 206378 : apply_handle_insert(s);
1712 akapila@postgresql.o 3807 : 206326 : break;
3808 : :
2010 3809 : 66169 : case LOGICAL_REP_MSG_UPDATE:
3393 peter_e@gmx.net 3810 : 66169 : apply_handle_update(s);
1712 akapila@postgresql.o 3811 : 66160 : break;
3812 : :
2010 3813 : 81942 : case LOGICAL_REP_MSG_DELETE:
3393 peter_e@gmx.net 3814 : 81942 : apply_handle_delete(s);
1712 akapila@postgresql.o 3815 : 81942 : break;
3816 : :
2010 3817 : 20 : case LOGICAL_REP_MSG_TRUNCATE:
2950 peter_e@gmx.net 3818 : 20 : apply_handle_truncate(s);
1712 akapila@postgresql.o 3819 : 20 : break;
3820 : :
2010 3821 : 492 : case LOGICAL_REP_MSG_RELATION:
3393 peter_e@gmx.net 3822 : 492 : apply_handle_relation(s);
1712 akapila@postgresql.o 3823 : 492 : break;
3824 : :
2010 3825 : 18 : case LOGICAL_REP_MSG_TYPE:
3393 peter_e@gmx.net 3826 : 18 : apply_handle_type(s);
1712 akapila@postgresql.o 3827 : 18 : break;
3828 : :
2010 3829 : 9 : case LOGICAL_REP_MSG_ORIGIN:
3393 peter_e@gmx.net 3830 : 9 : apply_handle_origin(s);
1712 akapila@postgresql.o 3831 : 9 : break;
3832 : :
1855 akapila@postgresql.o 3833 :UBC 0 : case LOGICAL_REP_MSG_MESSAGE:
3834 : :
3835 : : /*
3836 : : * Logical replication does not use generic logical messages yet.
3837 : : * Although, it could be used by other applications that use this
3838 : : * output plugin.
3839 : : */
1712 3840 : 0 : break;
3841 : :
2010 akapila@postgresql.o 3842 :CBC 842 : case LOGICAL_REP_MSG_STREAM_START:
2070 3843 : 842 : apply_handle_stream_start(s);
1712 3844 : 842 : break;
3845 : :
1720 3846 : 841 : case LOGICAL_REP_MSG_STREAM_STOP:
2070 3847 : 841 : apply_handle_stream_stop(s);
1712 3848 : 839 : break;
3849 : :
2010 3850 : 38 : case LOGICAL_REP_MSG_STREAM_ABORT:
2070 3851 : 38 : apply_handle_stream_abort(s);
1712 3852 : 38 : break;
3853 : :
2010 3854 : 61 : case LOGICAL_REP_MSG_STREAM_COMMIT:
2070 3855 : 61 : apply_handle_stream_commit(s);
1712 3856 : 59 : break;
3857 : :
1756 3858 : 17 : case LOGICAL_REP_MSG_BEGIN_PREPARE:
3859 : 17 : apply_handle_begin_prepare(s);
1712 3860 : 17 : break;
3861 : :
1756 3862 : 16 : case LOGICAL_REP_MSG_PREPARE:
3863 : 16 : apply_handle_prepare(s);
1712 3864 : 15 : break;
3865 : :
1756 3866 : 22 : case LOGICAL_REP_MSG_COMMIT_PREPARED:
3867 : 22 : apply_handle_commit_prepared(s);
1712 3868 : 22 : break;
3869 : :
1756 3870 : 5 : case LOGICAL_REP_MSG_ROLLBACK_PREPARED:
3871 : 5 : apply_handle_rollback_prepared(s);
1712 3872 : 5 : break;
3873 : :
1735 3874 : 15 : case LOGICAL_REP_MSG_STREAM_PREPARE:
3875 : 15 : apply_handle_stream_prepare(s);
1712 3876 : 13 : break;
3877 : :
1712 akapila@postgresql.o 3878 :UBC 0 : default:
3879 [ # # ]: 0 : ereport(ERROR,
3880 : : (errcode(ERRCODE_PROTOCOL_VIOLATION),
3881 : : errmsg("invalid logical replication message type \"??? (%d)\"", action)));
3882 : : }
3883 : :
3884 : : /* Reset the current command */
1712 akapila@postgresql.o 3885 :CBC 357799 : apply_error_callback_arg.command = saved_command;
3393 peter_e@gmx.net 3886 : 357799 : }
3887 : :
3888 : : /*
3889 : : * Figure out which write/flush positions to report to the walsender process.
3890 : : *
3891 : : * We can't simply report back the last LSN the walsender sent us because the
3892 : : * local transaction might not yet be flushed to disk locally. Instead we
3893 : : * build a list that associates local with remote LSNs for every commit. When
3894 : : * reporting back the flush position to the sender we iterate that list and
3895 : : * check which entries on it are already locally flushed. Those we can report
3896 : : * as having been flushed.
3897 : : *
3898 : : * The have_pending_txes is true if there are outstanding transactions that
3899 : : * need to be flushed.
3900 : : */
3901 : : static void
3902 : 21001 : get_flush_position(XLogRecPtr *write, XLogRecPtr *flush,
3903 : : bool *have_pending_txes)
3904 : : {
3905 : : dlist_mutable_iter iter;
1642 rhaas@postgresql.org 3906 : 21001 : XLogRecPtr local_flush = GetFlushRecPtr(NULL);
3907 : :
3393 peter_e@gmx.net 3908 : 21001 : *write = InvalidXLogRecPtr;
3909 : 21001 : *flush = InvalidXLogRecPtr;
3910 : :
3911 [ + - + + ]: 21539 : dlist_foreach_modify(iter, &lsn_mapping)
3912 : : {
3913 : 3861 : FlushPosition *pos =
1082 tgl@sss.pgh.pa.us 3914 :ECB (2395) : dlist_container(FlushPosition, node, iter.cur);
3915 : :
3393 peter_e@gmx.net 3916 :CBC 3861 : *write = pos->remote_end;
3917 : :
3918 [ + + ]: 3861 : if (pos->local_end <= local_flush)
3919 : : {
3920 : 538 : *flush = pos->remote_end;
3921 : 538 : dlist_delete(iter.cur);
3922 : 538 : pfree(pos);
3923 : : }
3924 : : else
3925 : : {
3926 : : /*
3927 : : * Don't want to uselessly iterate over the rest of the list which
3928 : : * could potentially be long. Instead get the last element and
3929 : : * grab the write position from there.
3930 : : */
3931 : 3323 : pos = dlist_tail_element(FlushPosition, node,
3932 : : &lsn_mapping);
3933 : 3323 : *write = pos->remote_end;
3934 : 3323 : *have_pending_txes = true;
3935 : 3323 : return;
3936 : : }
3937 : : }
3938 : :
3939 : 17678 : *have_pending_txes = !dlist_is_empty(&lsn_mapping);
3940 : : }
3941 : :
3942 : : /*
3943 : : * Store current remote/local lsn pair in the tracking list.
3944 : : */
3945 : : void
1212 akapila@postgresql.o 3946 : 571 : store_flush_position(XLogRecPtr remote_lsn, XLogRecPtr local_lsn)
3947 : : {
3948 : : FlushPosition *flushpos;
3949 : :
3950 : : /*
3951 : : * Skip for parallel apply workers, because the lsn_mapping is maintained
3952 : : * by the leader apply worker.
3953 : : */
3954 [ + + ]: 571 : if (am_parallel_apply_worker())
3955 : 19 : return;
3956 : :
3957 : : /* Need to do this in permanent context */
3283 peter_e@gmx.net 3958 : 552 : MemoryContextSwitchTo(ApplyContext);
3959 : :
3960 : : /* Track commit lsn */
146 michael@paquier.xyz 3961 :GNC 552 : flushpos = palloc_object(FlushPosition);
1212 akapila@postgresql.o 3962 :CBC 552 : flushpos->local_end = local_lsn;
3393 peter_e@gmx.net 3963 : 552 : flushpos->remote_end = remote_lsn;
3964 : :
3965 : 552 : dlist_push_tail(&lsn_mapping, &flushpos->node);
3283 3966 : 552 : MemoryContextSwitchTo(ApplyMessageContext);
3967 : : }
3968 : :
3969 : :
3970 : : /* Update statistics of the worker. */
3971 : : static void
3393 3972 : 209432 : UpdateWorkerStats(XLogRecPtr last_lsn, TimestampTz send_time, bool reply)
3973 : : {
3974 : 209432 : MyLogicalRepWorker->last_lsn = last_lsn;
3975 : 209432 : MyLogicalRepWorker->last_send_time = send_time;
3976 : 209432 : MyLogicalRepWorker->last_recv_time = GetCurrentTimestamp();
3977 [ + + ]: 209432 : if (reply)
3978 : : {
3979 : 2194 : MyLogicalRepWorker->reply_lsn = last_lsn;
3980 : 2194 : MyLogicalRepWorker->reply_time = send_time;
3981 : : }
3982 : 209432 : }
3983 : :
3984 : : /*
3985 : : * Apply main loop.
3986 : : */
3987 : : static void
3330 3988 : 447 : LogicalRepApplyLoop(XLogRecPtr last_received)
3989 : : {
2391 michael@paquier.xyz 3990 : 447 : TimestampTz last_recv_timestamp = GetCurrentTimestamp();
2069 tgl@sss.pgh.pa.us 3991 : 447 : bool ping_sent = false;
3992 : : TimeLineID tli;
3993 : : ErrorContextCallback errcallback;
286 akapila@postgresql.o 3994 :GNC 447 : RetainDeadTuplesData rdt_data = {0};
3995 : :
3996 : : /*
3997 : : * Init the ApplyMessageContext which we clean up after each replication
3998 : : * protocol message.
3999 : : */
3283 peter_e@gmx.net 4000 :CBC 447 : ApplyMessageContext = AllocSetContextCreate(ApplyContext,
4001 : : "ApplyMessageContext",
4002 : : ALLOCSET_DEFAULT_SIZES);
4003 : :
4004 : : /*
4005 : : * This memory context is used for per-stream data when the streaming mode
4006 : : * is enabled. This context is reset on each stream stop.
4007 : : */
2070 akapila@postgresql.o 4008 : 447 : LogicalStreamingContext = AllocSetContextCreate(ApplyContext,
4009 : : "LogicalStreamingContext",
4010 : : ALLOCSET_DEFAULT_SIZES);
4011 : :
4012 : : /* mark as idle, before starting to loop */
3393 peter_e@gmx.net 4013 : 447 : pgstat_report_activity(STATE_IDLE, NULL);
4014 : :
4015 : : /*
4016 : : * Push apply error context callback. Fields will be filled while applying
4017 : : * a change.
4018 : : */
1712 akapila@postgresql.o 4019 : 447 : errcallback.callback = apply_error_callback;
4020 : 447 : errcallback.previous = error_context_stack;
4021 : 447 : error_context_stack = &errcallback;
1212 4022 : 447 : apply_error_context_stack = error_context_stack;
4023 : :
4024 : : /* This outer loop iterates once per wait. */
4025 : : for (;;)
3393 peter_e@gmx.net 4026 : 18150 : {
4027 : 18597 : pgsocket fd = PGINVALID_SOCKET;
4028 : : int rc;
4029 : : int len;
4030 : 18597 : char *buf = NULL;
4031 : 18597 : bool endofstream = false;
4032 : : long wait_time;
4033 : :
3259 4034 [ - + ]: 18597 : CHECK_FOR_INTERRUPTS();
4035 : :
3283 4036 : 18597 : MemoryContextSwitchTo(ApplyMessageContext);
4037 : :
1819 alvherre@alvh.no-ip. 4038 : 18597 : len = walrcv_receive(LogRepWorkerWalRcvConn, &buf, &fd);
4039 : :
3393 peter_e@gmx.net 4040 [ + + ]: 18577 : if (len != 0)
4041 : : {
4042 : : /* Loop to process all available data (without blocking). */
4043 : : for (;;)
4044 : : {
4045 [ - + ]: 226877 : CHECK_FOR_INTERRUPTS();
4046 : :
4047 [ + + ]: 226877 : if (len == 0)
4048 : : {
4049 : 17433 : break;
4050 : : }
4051 [ + + ]: 209444 : else if (len < 0)
4052 : : {
4053 [ + - ]: 11 : ereport(LOG,
4054 : : (errmsg("data stream from publisher has ended")));
4055 : 11 : endofstream = true;
4056 : 11 : break;
4057 : : }
4058 : : else
4059 : : {
4060 : : int c;
4061 : : StringInfoData s;
4062 : :
1063 akapila@postgresql.o 4063 [ - + ]: 209433 : if (ConfigReloadPending)
4064 : : {
1063 akapila@postgresql.o 4065 :UBC 0 : ConfigReloadPending = false;
4066 : 0 : ProcessConfigFile(PGC_SIGHUP);
4067 : : }
4068 : :
4069 : : /* Reset timeout. */
3393 peter_e@gmx.net 4070 :CBC 209433 : last_recv_timestamp = GetCurrentTimestamp();
4071 : 209433 : ping_sent = false;
4072 : :
286 akapila@postgresql.o 4073 :GNC 209433 : rdt_data.last_recv_time = last_recv_timestamp;
4074 : :
4075 : : /* Ensure we are reading the data into our memory context. */
3283 peter_e@gmx.net 4076 :CBC 209433 : MemoryContextSwitchTo(ApplyMessageContext);
4077 : :
922 drowley@postgresql.o 4078 : 209433 : initReadOnlyStringInfo(&s, buf, len);
4079 : :
3393 peter_e@gmx.net 4080 : 209433 : c = pq_getmsgbyte(&s);
4081 : :
272 nathan@postgresql.or 4082 [ + + ]:GNC 209433 : if (c == PqReplMsg_WALData)
4083 : : {
4084 : : XLogRecPtr start_lsn;
4085 : : XLogRecPtr end_lsn;
4086 : : TimestampTz send_time;
4087 : :
3393 peter_e@gmx.net 4088 :CBC 205417 : start_lsn = pq_getmsgint64(&s);
4089 : 205417 : end_lsn = pq_getmsgint64(&s);
3358 tgl@sss.pgh.pa.us 4090 : 205417 : send_time = pq_getmsgint64(&s);
4091 : :
3393 peter_e@gmx.net 4092 [ + + ]: 205417 : if (last_received < start_lsn)
4093 : 149815 : last_received = start_lsn;
4094 : :
4095 [ - + ]: 205417 : if (last_received < end_lsn)
3393 peter_e@gmx.net 4096 :UBC 0 : last_received = end_lsn;
4097 : :
3393 peter_e@gmx.net 4098 :CBC 205417 : UpdateWorkerStats(last_received, send_time, false);
4099 : :
4100 : 205417 : apply_dispatch(&s);
4101 : :
286 akapila@postgresql.o 4102 :GNC 205353 : maybe_advance_nonremovable_xid(&rdt_data, false);
4103 : : }
272 nathan@postgresql.or 4104 [ + + ]: 4016 : else if (c == PqReplMsg_Keepalive)
4105 : : {
4106 : : XLogRecPtr end_lsn;
4107 : : TimestampTz timestamp;
4108 : : bool reply_requested;
4109 : :
3330 peter_e@gmx.net 4110 :CBC 2195 : end_lsn = pq_getmsgint64(&s);
3358 tgl@sss.pgh.pa.us 4111 : 2195 : timestamp = pq_getmsgint64(&s);
3393 peter_e@gmx.net 4112 : 2195 : reply_requested = pq_getmsgbyte(&s);
4113 : :
3330 4114 [ + + ]: 2195 : if (last_received < end_lsn)
4115 : 1066 : last_received = end_lsn;
4116 : :
4117 : 2195 : send_feedback(last_received, reply_requested, false);
4118 : :
286 akapila@postgresql.o 4119 :GNC 2194 : maybe_advance_nonremovable_xid(&rdt_data, false);
4120 : :
3393 peter_e@gmx.net 4121 :CBC 2194 : UpdateWorkerStats(last_received, timestamp, true);
4122 : : }
272 nathan@postgresql.or 4123 [ + - ]:GNC 1821 : else if (c == PqReplMsg_PrimaryStatusUpdate)
4124 : : {
286 akapila@postgresql.o 4125 : 1821 : rdt_data.remote_lsn = pq_getmsgint64(&s);
4126 : 1821 : rdt_data.remote_oldestxid = FullTransactionIdFromU64((uint64) pq_getmsgint64(&s));
4127 : 1821 : rdt_data.remote_nextxid = FullTransactionIdFromU64((uint64) pq_getmsgint64(&s));
4128 : 1821 : rdt_data.reply_time = pq_getmsgint64(&s);
4129 : :
4130 : : /*
4131 : : * This should never happen, see
4132 : : * ProcessStandbyPSRequestMessage. But if it happens
4133 : : * due to a bug, we don't want to proceed as it can
4134 : : * incorrectly advance oldest_nonremovable_xid.
4135 : : */
180 alvherre@kurilemu.de 4136 [ - + ]: 1821 : if (!XLogRecPtrIsValid(rdt_data.remote_lsn))
286 akapila@postgresql.o 4137 [ # # ]:UNC 0 : elog(ERROR, "cannot get the latest WAL position from the publisher");
4138 : :
286 akapila@postgresql.o 4139 :GNC 1821 : maybe_advance_nonremovable_xid(&rdt_data, true);
4140 : :
4141 : 1821 : UpdateWorkerStats(last_received, rdt_data.reply_time, false);
4142 : : }
4143 : : /* other message types are purposefully ignored */
4144 : :
3283 peter_e@gmx.net 4145 :CBC 209368 : MemoryContextReset(ApplyMessageContext);
4146 : : }
4147 : :
1819 alvherre@alvh.no-ip. 4148 : 209368 : len = walrcv_receive(LogRepWorkerWalRcvConn, &buf, &fd);
4149 : : }
4150 : : }
4151 : :
4152 : : /* confirm all writes so far */
3230 tgl@sss.pgh.pa.us 4153 : 18511 : send_feedback(last_received, false, false);
4154 : :
4155 : : /* Reset the timestamp if no message was received */
286 akapila@postgresql.o 4156 :GNC 18511 : rdt_data.last_recv_time = 0;
4157 : :
4158 : 18511 : maybe_advance_nonremovable_xid(&rdt_data, false);
4159 : :
2070 akapila@postgresql.o 4160 [ + + + + ]:CBC 18510 : if (!in_remote_transaction && !in_streamed_transaction)
4161 : : {
4162 : : /*
4163 : : * If we didn't get any transactions for a while there might be
4164 : : * unconsumed invalidation messages in the queue, consume them
4165 : : * now.
4166 : : */
3330 peter_e@gmx.net 4167 : 4736 : AcceptInvalidationMessages();
3258 4168 : 4736 : maybe_reread_subscription();
4169 : :
4170 : : /*
4171 : : * Process any relations that are being synchronized in parallel
4172 : : * and any newly added tables or sequences.
4173 : : */
201 akapila@postgresql.o 4174 :GNC 4693 : ProcessSyncingRelations(last_received);
4175 : : }
4176 : :
4177 : : /* Cleanup the memory. */
902 nathan@postgresql.or 4178 :CBC 18261 : MemoryContextReset(ApplyMessageContext);
3393 peter_e@gmx.net 4179 : 18261 : MemoryContextSwitchTo(TopMemoryContext);
4180 : :
4181 : : /* Check if we need to exit the streaming loop. */
4182 [ + + ]: 18261 : if (endofstream)
4183 : 11 : break;
4184 : :
4185 : : /*
4186 : : * Wait for more data or latch. If we have unflushed transactions,
4187 : : * wake up after WalWriterDelay to see if they've been flushed yet (in
4188 : : * which case we should send a feedback message). Otherwise, there's
4189 : : * no particular urgency about waking up unless we get data or a
4190 : : * signal.
4191 : : */
3230 tgl@sss.pgh.pa.us 4192 [ + + ]: 18250 : if (!dlist_is_empty(&lsn_mapping))
4193 : 2544 : wait_time = WalWriterDelay;
4194 : : else
4195 : 15706 : wait_time = NAPTIME_PER_CYCLE;
4196 : :
4197 : : /*
4198 : : * Ensure to wake up when it's possible to advance the non-removable
4199 : : * transaction ID, or when the retention duration may have exceeded
4200 : : * max_retention_duration.
4201 : : */
245 akapila@postgresql.o 4202 [ + + ]:GNC 18250 : if (MySubscription->retentionactive)
4203 : : {
4204 [ + + ]: 1720 : if (rdt_data.phase == RDT_GET_CANDIDATE_XID &&
4205 [ + - ]: 111 : rdt_data.xid_advance_interval)
4206 : 111 : wait_time = Min(wait_time, rdt_data.xid_advance_interval);
4207 [ + + ]: 1609 : else if (MySubscription->maxretention > 0)
4208 : 1 : wait_time = Min(wait_time, MySubscription->maxretention);
4209 : : }
4210 : :
3255 andres@anarazel.de 4211 :CBC 18250 : rc = WaitLatchOrSocket(MyLatch,
4212 : : WL_SOCKET_READABLE | WL_LATCH_SET |
4213 : : WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
4214 : : fd, wait_time,
4215 : : WAIT_EVENT_LOGICAL_APPLY_MAIN);
4216 : :
4217 [ + + ]: 18250 : if (rc & WL_LATCH_SET)
4218 : : {
4219 : 760 : ResetLatch(MyLatch);
4220 [ + + ]: 760 : CHECK_FOR_INTERRUPTS();
4221 : : }
4222 : :
2331 rhaas@postgresql.org 4223 [ + + ]: 18150 : if (ConfigReloadPending)
4224 : : {
4225 : 10 : ConfigReloadPending = false;
3312 peter_e@gmx.net 4226 : 10 : ProcessConfigFile(PGC_SIGHUP);
4227 : : }
4228 : :
3393 4229 [ + + ]: 18150 : if (rc & WL_TIMEOUT)
4230 : : {
4231 : : /*
4232 : : * We didn't receive anything new. If we haven't heard anything
4233 : : * from the server for more than wal_receiver_timeout / 2, ping
4234 : : * the server. Also, if it's been longer than
4235 : : * wal_receiver_status_interval since the last update we sent,
4236 : : * send a status update to the primary anyway, to report any
4237 : : * progress in applying WAL.
4238 : : */
4239 : 283 : bool requestReply = false;
4240 : :
4241 : : /*
4242 : : * Check if time since last receive from primary has reached the
4243 : : * configured limit.
4244 : : */
4245 [ + - ]: 283 : if (wal_receiver_timeout > 0)
4246 : : {
4247 : 283 : TimestampTz now = GetCurrentTimestamp();
4248 : : TimestampTz timeout;
4249 : :
4250 : 283 : timeout =
4251 : 283 : TimestampTzPlusMilliseconds(last_recv_timestamp,
4252 : : wal_receiver_timeout);
4253 : :
4254 [ - + ]: 283 : if (now >= timeout)
3393 peter_e@gmx.net 4255 [ # # ]:UBC 0 : ereport(ERROR,
4256 : : (errcode(ERRCODE_CONNECTION_FAILURE),
4257 : : errmsg("terminating logical replication worker due to timeout")));
4258 : :
4259 : : /* Check to see if it's time for a ping. */
3393 peter_e@gmx.net 4260 [ + - ]:CBC 283 : if (!ping_sent)
4261 : : {
4262 : 283 : timeout = TimestampTzPlusMilliseconds(last_recv_timestamp,
4263 : : (wal_receiver_timeout / 2));
4264 [ - + ]: 283 : if (now >= timeout)
4265 : : {
3393 peter_e@gmx.net 4266 :UBC 0 : requestReply = true;
4267 : 0 : ping_sent = true;
4268 : : }
4269 : : }
4270 : : }
4271 : :
3393 peter_e@gmx.net 4272 :CBC 283 : send_feedback(last_received, requestReply, requestReply);
4273 : :
286 akapila@postgresql.o 4274 :GNC 283 : maybe_advance_nonremovable_xid(&rdt_data, false);
4275 : :
4276 : : /*
4277 : : * Force reporting to ensure long idle periods don't lead to
4278 : : * arbitrarily delayed stats. Stats can only be reported outside
4279 : : * of (implicit or explicit) transactions. That shouldn't lead to
4280 : : * stats being delayed for long, because transactions are either
4281 : : * sent as a whole on commit or streamed. Streamed transactions
4282 : : * are spilled to disk and applied on commit.
4283 : : */
1454 andres@anarazel.de 4284 [ + - ]:CBC 283 : if (!IsTransactionState())
4285 : 283 : pgstat_report_stat(true);
4286 : : }
4287 : : }
4288 : :
4289 : : /* Pop the error context stack */
1712 akapila@postgresql.o 4290 : 11 : error_context_stack = errcallback.previous;
1212 4291 : 11 : apply_error_context_stack = error_context_stack;
4292 : :
4293 : : /* All done */
1819 alvherre@alvh.no-ip. 4294 : 11 : walrcv_endstreaming(LogRepWorkerWalRcvConn, &tli);
3393 peter_e@gmx.net 4295 :UBC 0 : }
4296 : :
4297 : : /*
4298 : : * Send a Standby Status Update message to server.
4299 : : *
4300 : : * 'recvpos' is the latest LSN we've received data to, force is set if we need
4301 : : * to send a response to avoid timeouts.
4302 : : */
4303 : : static void
3393 peter_e@gmx.net 4304 :CBC 20989 : send_feedback(XLogRecPtr recvpos, bool force, bool requestReply)
4305 : : {
4306 : : static StringInfo reply_message = NULL;
4307 : : static TimestampTz send_time = 0;
4308 : :
4309 : : static XLogRecPtr last_recvpos = InvalidXLogRecPtr;
4310 : : static XLogRecPtr last_writepos = InvalidXLogRecPtr;
4311 : :
4312 : : XLogRecPtr writepos;
4313 : : XLogRecPtr flushpos;
4314 : : TimestampTz now;
4315 : : bool have_pending_txes;
4316 : :
4317 : : /*
4318 : : * If the user doesn't want status to be reported to the publisher, be
4319 : : * sure to exit before doing anything at all.
4320 : : */
4321 [ + + - + ]: 20989 : if (!force && wal_receiver_status_interval <= 0)
4322 : 9947 : return;
4323 : :
4324 : : /* It's legal to not pass a recvpos */
4325 [ - + ]: 20989 : if (recvpos < last_recvpos)
3393 peter_e@gmx.net 4326 :UBC 0 : recvpos = last_recvpos;
4327 : :
3393 peter_e@gmx.net 4328 :CBC 20989 : get_flush_position(&writepos, &flushpos, &have_pending_txes);
4329 : :
4330 : : /*
4331 : : * No outstanding transactions to flush, we can report the latest received
4332 : : * position. This is important for synchronous replication.
4333 : : */
4334 [ + + ]: 20989 : if (!have_pending_txes)
4335 : 17671 : flushpos = writepos = recvpos;
4336 : :
4337 [ - + ]: 20989 : if (writepos < last_writepos)
3393 peter_e@gmx.net 4338 :UBC 0 : writepos = last_writepos;
4339 : :
3393 peter_e@gmx.net 4340 [ + + ]:CBC 20989 : if (flushpos < last_flushpos)
4341 : 3257 : flushpos = last_flushpos;
4342 : :
4343 : 20989 : now = GetCurrentTimestamp();
4344 : :
4345 : : /* if we've already reported everything we're good */
4346 [ + + ]: 20989 : if (!force &&
4347 [ + + ]: 20747 : writepos == last_writepos &&
4348 [ + + ]: 10209 : flushpos == last_flushpos &&
4349 [ + + ]: 10023 : !TimestampDifferenceExceeds(send_time, now,
4350 : : wal_receiver_status_interval * 1000))
4351 : 9947 : return;
4352 : 11042 : send_time = now;
4353 : :
4354 [ + + ]: 11042 : if (!reply_message)
4355 : : {
3275 bruce@momjian.us 4356 : 447 : MemoryContext oldctx = MemoryContextSwitchTo(ApplyContext);
4357 : :
3393 peter_e@gmx.net 4358 : 447 : reply_message = makeStringInfo();
4359 : 447 : MemoryContextSwitchTo(oldctx);
4360 : : }
4361 : : else
4362 : 10595 : resetStringInfo(reply_message);
4363 : :
272 nathan@postgresql.or 4364 :GNC 11042 : pq_sendbyte(reply_message, PqReplMsg_StandbyStatusUpdate);
3240 tgl@sss.pgh.pa.us 4365 :CBC 11042 : pq_sendint64(reply_message, recvpos); /* write */
4366 : 11042 : pq_sendint64(reply_message, flushpos); /* flush */
4367 : 11042 : pq_sendint64(reply_message, writepos); /* apply */
3275 bruce@momjian.us 4368 : 11042 : pq_sendint64(reply_message, now); /* sendTime */
3393 peter_e@gmx.net 4369 : 11042 : pq_sendbyte(reply_message, requestReply); /* replyRequested */
4370 : :
302 alvherre@kurilemu.de 4371 [ + + ]:GNC 11042 : elog(DEBUG2, "sending feedback (force %d) to recv %X/%08X, write %X/%08X, flush %X/%08X",
4372 : : force,
4373 : : LSN_FORMAT_ARGS(recvpos),
4374 : : LSN_FORMAT_ARGS(writepos),
4375 : : LSN_FORMAT_ARGS(flushpos));
4376 : :
1819 alvherre@alvh.no-ip. 4377 :CBC 11042 : walrcv_send(LogRepWorkerWalRcvConn,
4378 : : reply_message->data, reply_message->len);
4379 : :
3393 peter_e@gmx.net 4380 [ + + ]: 11041 : if (recvpos > last_recvpos)
4381 : 10539 : last_recvpos = recvpos;
4382 [ + + ]: 11041 : if (writepos > last_writepos)
4383 : 10541 : last_writepos = writepos;
4384 [ + + ]: 11041 : if (flushpos > last_flushpos)
4385 : 10334 : last_flushpos = flushpos;
4386 : : }
4387 : :
4388 : : /*
4389 : : * Attempt to advance the non-removable transaction ID.
4390 : : *
4391 : : * See comments atop worker.c for details.
4392 : : */
4393 : : static void
286 akapila@postgresql.o 4394 :GNC 228162 : maybe_advance_nonremovable_xid(RetainDeadTuplesData *rdt_data,
4395 : : bool status_received)
4396 : : {
4397 [ + + ]: 228162 : if (!can_advance_nonremovable_xid(rdt_data))
4398 : 224383 : return;
4399 : :
4400 : 3779 : process_rdt_phase_transition(rdt_data, status_received);
4401 : : }
4402 : :
4403 : : /*
4404 : : * Preliminary check to determine if advancing the non-removable transaction ID
4405 : : * is allowed.
4406 : : */
4407 : : static bool
4408 : 228162 : can_advance_nonremovable_xid(RetainDeadTuplesData *rdt_data)
4409 : : {
4410 : : /*
4411 : : * It is sufficient to manage non-removable transaction ID for a
4412 : : * subscription by the main apply worker to detect update_deleted reliably
4413 : : * even for table sync or parallel apply workers.
4414 : : */
4415 [ + + ]: 228162 : if (!am_leader_apply_worker())
4416 : 434 : return false;
4417 : :
4418 : : /* No need to advance if retaining dead tuples is not required */
4419 [ + + ]: 227728 : if (!MySubscription->retaindeadtuples)
4420 : 223949 : return false;
4421 : :
4422 : 3779 : return true;
4423 : : }
4424 : :
4425 : : /*
4426 : : * Process phase transitions during the non-removable transaction ID
4427 : : * advancement. See comments atop worker.c for details of the transition.
4428 : : */
4429 : : static void
4430 : 5675 : process_rdt_phase_transition(RetainDeadTuplesData *rdt_data,
4431 : : bool status_received)
4432 : : {
4433 [ + + + + : 5675 : switch (rdt_data->phase)
+ + - ]
4434 : : {
4435 : 279 : case RDT_GET_CANDIDATE_XID:
4436 : 279 : get_candidate_xid(rdt_data);
4437 : 279 : break;
4438 : 1823 : case RDT_REQUEST_PUBLISHER_STATUS:
4439 : 1823 : request_publisher_status(rdt_data);
4440 : 1823 : break;
4441 : 3477 : case RDT_WAIT_FOR_PUBLISHER_STATUS:
4442 : 3477 : wait_for_publisher_status(rdt_data, status_received);
4443 : 3477 : break;
4444 : 94 : case RDT_WAIT_FOR_LOCAL_FLUSH:
4445 : 94 : wait_for_local_flush(rdt_data);
4446 : 94 : break;
245 4447 : 1 : case RDT_STOP_CONFLICT_INFO_RETENTION:
4448 : 1 : stop_conflict_info_retention(rdt_data);
4449 : 1 : break;
232 4450 : 1 : case RDT_RESUME_CONFLICT_INFO_RETENTION:
4451 : 1 : resume_conflict_info_retention(rdt_data);
232 akapila@postgresql.o 4452 :UNC 0 : break;
4453 : : }
286 akapila@postgresql.o 4454 :GNC 5674 : }
4455 : :
4456 : : /*
4457 : : * Workhorse for the RDT_GET_CANDIDATE_XID phase.
4458 : : */
4459 : : static void
4460 : 279 : get_candidate_xid(RetainDeadTuplesData *rdt_data)
4461 : : {
4462 : : TransactionId oldest_running_xid;
4463 : : TimestampTz now;
4464 : :
4465 : : /*
4466 : : * Use last_recv_time when applying changes in the loop to avoid
4467 : : * unnecessary system time retrieval. If last_recv_time is not available,
4468 : : * obtain the current timestamp.
4469 : : */
4470 [ + + ]: 279 : now = rdt_data->last_recv_time ? rdt_data->last_recv_time : GetCurrentTimestamp();
4471 : :
4472 : : /*
4473 : : * Compute the candidate_xid and request the publisher status at most once
4474 : : * per xid_advance_interval. Refer to adjust_xid_advance_interval() for
4475 : : * details on how this value is dynamically adjusted. This is to avoid
4476 : : * using CPU and network resources without making much progress.
4477 : : */
4478 [ + + ]: 279 : if (!TimestampDifferenceExceeds(rdt_data->candidate_xid_time, now,
4479 : : rdt_data->xid_advance_interval))
4480 : 204 : return;
4481 : :
4482 : : /*
4483 : : * Immediately update the timer, even if the function returns later
4484 : : * without setting candidate_xid due to inactivity on the subscriber. This
4485 : : * avoids frequent calls to GetOldestActiveTransactionId.
4486 : : */
4487 : 75 : rdt_data->candidate_xid_time = now;
4488 : :
4489 : : /*
4490 : : * Consider transactions in the current database, as only dead tuples from
4491 : : * this database are required for conflict detection.
4492 : : */
4493 : 75 : oldest_running_xid = GetOldestActiveTransactionId(false, false);
4494 : :
4495 : : /*
4496 : : * Oldest active transaction ID (oldest_running_xid) can't be behind any
4497 : : * of its previously computed value.
4498 : : */
4499 [ - + ]: 75 : Assert(TransactionIdPrecedesOrEquals(MyLogicalRepWorker->oldest_nonremovable_xid,
4500 : : oldest_running_xid));
4501 : :
4502 : : /* Return if the oldest_nonremovable_xid cannot be advanced */
4503 [ + + ]: 75 : if (TransactionIdEquals(MyLogicalRepWorker->oldest_nonremovable_xid,
4504 : : oldest_running_xid))
4505 : : {
4506 : 35 : adjust_xid_advance_interval(rdt_data, false);
4507 : 35 : return;
4508 : : }
4509 : :
4510 : 40 : adjust_xid_advance_interval(rdt_data, true);
4511 : :
4512 : 40 : rdt_data->candidate_xid = oldest_running_xid;
4513 : 40 : rdt_data->phase = RDT_REQUEST_PUBLISHER_STATUS;
4514 : :
4515 : : /* process the next phase */
4516 : 40 : process_rdt_phase_transition(rdt_data, false);
4517 : : }
4518 : :
4519 : : /*
4520 : : * Workhorse for the RDT_REQUEST_PUBLISHER_STATUS phase.
4521 : : */
4522 : : static void
4523 : 1823 : request_publisher_status(RetainDeadTuplesData *rdt_data)
4524 : : {
4525 : : static StringInfo request_message = NULL;
4526 : :
4527 [ + + ]: 1823 : if (!request_message)
4528 : : {
4529 : 12 : MemoryContext oldctx = MemoryContextSwitchTo(ApplyContext);
4530 : :
4531 : 12 : request_message = makeStringInfo();
4532 : 12 : MemoryContextSwitchTo(oldctx);
4533 : : }
4534 : : else
4535 : 1811 : resetStringInfo(request_message);
4536 : :
4537 : : /*
4538 : : * Send the current time to update the remote walsender's latest reply
4539 : : * message received time.
4540 : : */
272 nathan@postgresql.or 4541 : 1823 : pq_sendbyte(request_message, PqReplMsg_PrimaryStatusRequest);
286 akapila@postgresql.o 4542 : 1823 : pq_sendint64(request_message, GetCurrentTimestamp());
4543 : :
4544 [ + + ]: 1823 : elog(DEBUG2, "sending publisher status request message");
4545 : :
4546 : : /* Send a request for the publisher status */
4547 : 1823 : walrcv_send(LogRepWorkerWalRcvConn,
4548 : : request_message->data, request_message->len);
4549 : :
4550 : 1823 : rdt_data->phase = RDT_WAIT_FOR_PUBLISHER_STATUS;
4551 : :
4552 : : /*
4553 : : * Skip calling maybe_advance_nonremovable_xid() since further transition
4554 : : * is possible only once we receive the publisher status message.
4555 : : */
4556 : 1823 : }
4557 : :
4558 : : /*
4559 : : * Workhorse for the RDT_WAIT_FOR_PUBLISHER_STATUS phase.
4560 : : */
4561 : : static void
4562 : 3477 : wait_for_publisher_status(RetainDeadTuplesData *rdt_data,
4563 : : bool status_received)
4564 : : {
4565 : : /*
4566 : : * Return if we have requested but not yet received the publisher status.
4567 : : */
4568 [ + + ]: 3477 : if (!status_received)
4569 : 1656 : return;
4570 : :
4571 : : /*
4572 : : * We don't need to maintain oldest_nonremovable_xid if we decide to stop
4573 : : * retaining conflict information for this worker.
4574 : : */
245 4575 [ - + ]: 1821 : if (should_stop_conflict_info_retention(rdt_data))
4576 : : {
232 akapila@postgresql.o 4577 :UNC 0 : rdt_data->phase = RDT_STOP_CONFLICT_INFO_RETENTION;
245 4578 : 0 : return;
4579 : : }
4580 : :
286 akapila@postgresql.o 4581 [ + + ]:GNC 1821 : if (!FullTransactionIdIsValid(rdt_data->remote_wait_for))
4582 : 38 : rdt_data->remote_wait_for = rdt_data->remote_nextxid;
4583 : :
4584 : : /*
4585 : : * Check if all remote concurrent transactions that were active at the
4586 : : * first status request have now completed. If completed, proceed to the
4587 : : * next phase; otherwise, continue checking the publisher status until
4588 : : * these transactions finish.
4589 : : *
4590 : : * It's possible that transactions in the commit phase during the last
4591 : : * cycle have now finished committing, but remote_oldestxid remains older
4592 : : * than remote_wait_for. This can happen if some old transaction came in
4593 : : * the commit phase when we requested status in this cycle. We do not
4594 : : * handle this case explicitly as it's rare and the benefit doesn't
4595 : : * justify the required complexity. Tracking would require either caching
4596 : : * all xids at the publisher or sending them to subscribers. The condition
4597 : : * will resolve naturally once the remaining transactions are finished.
4598 : : *
4599 : : * Directly advancing the non-removable transaction ID is possible if
4600 : : * there are no activities on the publisher since the last advancement
4601 : : * cycle. However, it requires maintaining two fields, last_remote_nextxid
4602 : : * and last_remote_lsn, within the structure for comparison with the
4603 : : * current cycle's values. Considering the minimal cost of continuing in
4604 : : * RDT_WAIT_FOR_LOCAL_FLUSH without awaiting changes, we opted not to
4605 : : * advance the transaction ID here.
4606 : : */
4607 [ + + ]: 1821 : if (FullTransactionIdPrecedesOrEquals(rdt_data->remote_wait_for,
4608 : : rdt_data->remote_oldestxid))
4609 : 38 : rdt_data->phase = RDT_WAIT_FOR_LOCAL_FLUSH;
4610 : : else
4611 : 1783 : rdt_data->phase = RDT_REQUEST_PUBLISHER_STATUS;
4612 : :
4613 : : /* process the next phase */
4614 : 1821 : process_rdt_phase_transition(rdt_data, false);
4615 : : }
4616 : :
4617 : : /*
4618 : : * Workhorse for the RDT_WAIT_FOR_LOCAL_FLUSH phase.
4619 : : */
4620 : : static void
4621 : 94 : wait_for_local_flush(RetainDeadTuplesData *rdt_data)
4622 : : {
180 alvherre@kurilemu.de 4623 [ + - - + ]: 94 : Assert(XLogRecPtrIsValid(rdt_data->remote_lsn) &&
4624 : : TransactionIdIsValid(rdt_data->candidate_xid));
4625 : :
4626 : : /*
4627 : : * We expect the publisher and subscriber clocks to be in sync using time
4628 : : * sync service like NTP. Otherwise, we will advance this worker's
4629 : : * oldest_nonremovable_xid prematurely, leading to the removal of rows
4630 : : * required to detect update_deleted reliably. This check primarily
4631 : : * addresses scenarios where the publisher's clock falls behind; if the
4632 : : * publisher's clock is ahead, subsequent transactions will naturally bear
4633 : : * later commit timestamps, conforming to the design outlined atop
4634 : : * worker.c.
4635 : : *
4636 : : * XXX Consider waiting for the publisher's clock to catch up with the
4637 : : * subscriber's before proceeding to the next phase.
4638 : : */
286 akapila@postgresql.o 4639 [ - + ]: 94 : if (TimestampDifferenceExceeds(rdt_data->reply_time,
4640 : : rdt_data->candidate_xid_time, 0))
286 akapila@postgresql.o 4641 [ # # ]:UNC 0 : ereport(ERROR,
4642 : : errmsg_internal("oldest_nonremovable_xid transaction ID could be advanced prematurely"),
4643 : : errdetail_internal("The clock on the publisher is behind that of the subscriber."));
4644 : :
4645 : : /*
4646 : : * Do not attempt to advance the non-removable transaction ID when table
4647 : : * sync is in progress. During this time, changes from a single
4648 : : * transaction may be applied by multiple table sync workers corresponding
4649 : : * to the target tables. So, it's necessary for all table sync workers to
4650 : : * apply and flush the corresponding changes before advancing the
4651 : : * transaction ID, otherwise, dead tuples that are still needed for
4652 : : * conflict detection in table sync workers could be removed prematurely.
4653 : : * However, confirming the apply and flush progress across all table sync
4654 : : * workers is complex and not worth the effort, so we simply return if not
4655 : : * all tables are in the READY state.
4656 : : *
4657 : : * Advancing the transaction ID is necessary even when no tables are
4658 : : * currently subscribed, to avoid retaining dead tuples unnecessarily.
4659 : : * While it might seem safe to skip all phases and directly assign
4660 : : * candidate_xid to oldest_nonremovable_xid during the
4661 : : * RDT_GET_CANDIDATE_XID phase in such cases, this is unsafe. If users
4662 : : * concurrently add tables to the subscription, the apply worker may not
4663 : : * process invalidations in time. Consequently,
4664 : : * HasSubscriptionTablesCached() might miss the new tables, leading to
4665 : : * premature advancement of oldest_nonremovable_xid.
4666 : : *
4667 : : * Performing the check during RDT_WAIT_FOR_LOCAL_FLUSH is safe, as
4668 : : * invalidations are guaranteed to be processed before applying changes
4669 : : * from newly added tables while waiting for the local flush to reach
4670 : : * remote_lsn.
4671 : : *
4672 : : * Additionally, even if we check for subscription tables during
4673 : : * RDT_GET_CANDIDATE_XID, they might be dropped before reaching
4674 : : * RDT_WAIT_FOR_LOCAL_FLUSH. Therefore, it's still necessary to verify
4675 : : * subscription tables at this stage to prevent unnecessary tuple
4676 : : * retention.
4677 : : */
201 akapila@postgresql.o 4678 [ + + + + ]:GNC 94 : if (HasSubscriptionTablesCached() && !AllTablesyncsReady())
4679 : : {
4680 : : TimestampTz now;
4681 : :
245 4682 : 26 : now = rdt_data->last_recv_time
4683 [ + + ]: 13 : ? rdt_data->last_recv_time : GetCurrentTimestamp();
4684 : :
4685 : : /*
4686 : : * Record the time spent waiting for table sync, it is needed for the
4687 : : * timeout check in should_stop_conflict_info_retention().
4688 : : */
4689 : 13 : rdt_data->table_sync_wait_time =
4690 : 13 : TimestampDifferenceMilliseconds(rdt_data->candidate_xid_time, now);
4691 : :
4692 : 13 : return;
4693 : : }
4694 : :
4695 : : /*
4696 : : * We don't need to maintain oldest_nonremovable_xid if we decide to stop
4697 : : * retaining conflict information for this worker.
4698 : : */
4699 [ + + ]: 81 : if (should_stop_conflict_info_retention(rdt_data))
4700 : : {
232 4701 : 1 : rdt_data->phase = RDT_STOP_CONFLICT_INFO_RETENTION;
286 4702 : 1 : return;
4703 : : }
4704 : :
4705 : : /*
4706 : : * Update and check the remote flush position if we are applying changes
4707 : : * in a loop. This is done at most once per WalWriterDelay to avoid
4708 : : * performing costly operations in get_flush_position() too frequently
4709 : : * during change application.
4710 : : */
4711 [ + + + + : 106 : if (last_flushpos < rdt_data->remote_lsn && rdt_data->last_recv_time &&
+ + ]
4712 : 26 : TimestampDifferenceExceeds(rdt_data->flushpos_update_time,
4713 : : rdt_data->last_recv_time, WalWriterDelay))
4714 : : {
4715 : : XLogRecPtr writepos;
4716 : : XLogRecPtr flushpos;
4717 : : bool have_pending_txes;
4718 : :
4719 : : /* Fetch the latest remote flush position */
4720 : 12 : get_flush_position(&writepos, &flushpos, &have_pending_txes);
4721 : :
4722 [ + + ]: 12 : if (flushpos > last_flushpos)
4723 : 1 : last_flushpos = flushpos;
4724 : :
4725 : 12 : rdt_data->flushpos_update_time = rdt_data->last_recv_time;
4726 : : }
4727 : :
4728 : : /* Return to wait for the changes to be applied */
4729 [ + + ]: 80 : if (last_flushpos < rdt_data->remote_lsn)
4730 : 44 : return;
4731 : :
4732 : : /*
4733 : : * Reaching this point implies should_stop_conflict_info_retention()
4734 : : * returned false earlier, meaning that the most recent duration for
4735 : : * advancing the non-removable transaction ID is within the
4736 : : * max_retention_duration or max_retention_duration is set to 0.
4737 : : *
4738 : : * Therefore, if conflict info retention was previously stopped due to a
4739 : : * timeout, it is now safe to resume retention.
4740 : : */
232 4741 [ + + ]: 36 : if (!MySubscription->retentionactive)
4742 : : {
4743 : 1 : rdt_data->phase = RDT_RESUME_CONFLICT_INFO_RETENTION;
4744 : 1 : return;
4745 : : }
4746 : :
4747 : : /*
4748 : : * Reaching here means the remote WAL position has been received, and all
4749 : : * transactions up to that position on the publisher have been applied and
4750 : : * flushed locally. So, we can advance the non-removable transaction ID.
4751 : : */
286 4752 : 35 : SpinLockAcquire(&MyLogicalRepWorker->relmutex);
4753 : 35 : MyLogicalRepWorker->oldest_nonremovable_xid = rdt_data->candidate_xid;
4754 : 35 : SpinLockRelease(&MyLogicalRepWorker->relmutex);
4755 : :
264 heikki.linnakangas@i 4756 [ + + ]: 35 : elog(DEBUG2, "confirmed flush up to remote lsn %X/%08X: new oldest_nonremovable_xid %u",
4757 : : LSN_FORMAT_ARGS(rdt_data->remote_lsn),
4758 : : rdt_data->candidate_xid);
4759 : :
4760 : : /* Notify launcher to update the xmin of the conflict slot */
286 akapila@postgresql.o 4761 : 35 : ApplyLauncherWakeup();
4762 : :
245 4763 : 35 : reset_retention_data_fields(rdt_data);
4764 : :
4765 : : /* process the next phase */
4766 : 35 : process_rdt_phase_transition(rdt_data, false);
4767 : : }
4768 : :
4769 : : /*
4770 : : * Check whether conflict information retention should be stopped due to
4771 : : * exceeding the maximum wait time (max_retention_duration).
4772 : : *
4773 : : * If retention should be stopped, return true. Otherwise, return false.
4774 : : */
4775 : : static bool
4776 : 1902 : should_stop_conflict_info_retention(RetainDeadTuplesData *rdt_data)
4777 : : {
4778 : : TimestampTz now;
4779 : :
4780 [ - + ]: 1902 : Assert(TransactionIdIsValid(rdt_data->candidate_xid));
4781 [ + + - + ]: 1902 : Assert(rdt_data->phase == RDT_WAIT_FOR_PUBLISHER_STATUS ||
4782 : : rdt_data->phase == RDT_WAIT_FOR_LOCAL_FLUSH);
4783 : :
4784 [ + + ]: 1902 : if (!MySubscription->maxretention)
4785 : 1901 : return false;
4786 : :
4787 : : /*
4788 : : * Use last_recv_time when applying changes in the loop to avoid
4789 : : * unnecessary system time retrieval. If last_recv_time is not available,
4790 : : * obtain the current timestamp.
4791 : : */
4792 [ - + ]: 1 : now = rdt_data->last_recv_time ? rdt_data->last_recv_time : GetCurrentTimestamp();
4793 : :
4794 : : /*
4795 : : * Return early if the wait time has not exceeded the configured maximum
4796 : : * (max_retention_duration). Time spent waiting for table synchronization
4797 : : * is excluded from this calculation, as it occurs infrequently.
4798 : : */
4799 [ - + ]: 1 : if (!TimestampDifferenceExceeds(rdt_data->candidate_xid_time, now,
4800 : 1 : MySubscription->maxretention +
4801 : 1 : rdt_data->table_sync_wait_time))
245 akapila@postgresql.o 4802 :UNC 0 : return false;
4803 : :
245 akapila@postgresql.o 4804 :GNC 1 : return true;
4805 : : }
4806 : :
4807 : : /*
4808 : : * Workhorse for the RDT_STOP_CONFLICT_INFO_RETENTION phase.
4809 : : */
4810 : : static void
4811 : 1 : stop_conflict_info_retention(RetainDeadTuplesData *rdt_data)
4812 : : {
4813 : : /* Stop retention if not yet */
232 4814 [ + - ]: 1 : if (MySubscription->retentionactive)
4815 : : {
4816 : : /*
4817 : : * If the retention status cannot be updated (e.g., due to active
4818 : : * transaction), skip further processing to avoid inconsistent
4819 : : * retention behavior.
4820 : : */
4821 [ - + ]: 1 : if (!update_retention_status(false))
232 akapila@postgresql.o 4822 :UNC 0 : return;
4823 : :
232 akapila@postgresql.o 4824 :GNC 1 : SpinLockAcquire(&MyLogicalRepWorker->relmutex);
4825 : 1 : MyLogicalRepWorker->oldest_nonremovable_xid = InvalidTransactionId;
4826 : 1 : SpinLockRelease(&MyLogicalRepWorker->relmutex);
4827 : :
4828 [ + - ]: 1 : ereport(LOG,
4829 : : errmsg("logical replication worker for subscription \"%s\" has stopped retaining the information for detecting conflicts",
4830 : : MySubscription->name),
4831 : : errdetail("Retention is stopped because the apply process has not caught up with the publisher within the configured max_retention_duration."));
4832 : : }
4833 : :
4834 [ - + ]: 1 : Assert(!TransactionIdIsValid(MyLogicalRepWorker->oldest_nonremovable_xid));
4835 : :
4836 : : /*
4837 : : * If retention has been stopped, reset to the initial phase to retry
4838 : : * resuming retention. This reset is required to recalculate the current
4839 : : * wait time and resume retention if the time falls within
4840 : : * max_retention_duration.
4841 : : */
4842 : 1 : reset_retention_data_fields(rdt_data);
4843 : : }
4844 : :
4845 : : /*
4846 : : * Workhorse for the RDT_RESUME_CONFLICT_INFO_RETENTION phase.
4847 : : */
4848 : : static void
4849 : 1 : resume_conflict_info_retention(RetainDeadTuplesData *rdt_data)
4850 : : {
4851 : : /* We can't resume retention without updating retention status. */
4852 [ - + ]: 1 : if (!update_retention_status(true))
232 akapila@postgresql.o 4853 :UNC 0 : return;
4854 : :
232 akapila@postgresql.o 4855 [ + - - + ]:GNC 1 : ereport(LOG,
4856 : : errmsg("logical replication worker for subscription \"%s\" will resume retaining the information for detecting conflicts",
4857 : : MySubscription->name),
4858 : : MySubscription->maxretention
4859 : : ? errdetail("Retention is re-enabled because the apply process has caught up with the publisher within the configured max_retention_duration.")
4860 : : : errdetail("Retention is re-enabled because max_retention_duration has been set to unlimited."));
4861 : :
4862 : : /*
4863 : : * Restart the worker to let the launcher initialize
4864 : : * oldest_nonremovable_xid at startup.
4865 : : *
4866 : : * While it's technically possible to derive this value on-the-fly using
4867 : : * the conflict detection slot's xmin, doing so risks a race condition:
4868 : : * the launcher might clean slot.xmin just after retention resumes. This
4869 : : * would make oldest_nonremovable_xid unreliable, especially during xid
4870 : : * wraparound.
4871 : : *
4872 : : * Although this can be prevented by introducing heavy weight locking, the
4873 : : * complexity it will bring doesn't seem worthwhile given how rarely
4874 : : * retention is resumed.
4875 : : */
4876 : 1 : apply_worker_exit();
4877 : : }
4878 : :
4879 : : /*
4880 : : * Updates pg_subscription.subretentionactive to the given value within a
4881 : : * new transaction.
4882 : : *
4883 : : * If already inside an active transaction, skips the update and returns
4884 : : * false.
4885 : : *
4886 : : * Returns true if the update is successfully performed.
4887 : : */
4888 : : static bool
4889 : 2 : update_retention_status(bool active)
4890 : : {
4891 : : /*
4892 : : * Do not update the catalog during an active transaction. The transaction
4893 : : * may be started during change application, leading to a possible
4894 : : * rollback of catalog updates if the application fails subsequently.
4895 : : */
245 4896 [ - + ]: 2 : if (IsTransactionState())
232 akapila@postgresql.o 4897 :UNC 0 : return false;
4898 : :
245 akapila@postgresql.o 4899 :GNC 2 : StartTransactionCommand();
4900 : :
4901 : : /*
4902 : : * Updating pg_subscription might involve TOAST table access, so ensure we
4903 : : * have a valid snapshot.
4904 : : */
4905 : 2 : PushActiveSnapshot(GetTransactionSnapshot());
4906 : :
4907 : : /* Update pg_subscription.subretentionactive */
232 4908 : 2 : UpdateDeadTupleRetentionStatus(MySubscription->oid, active);
4909 : :
245 4910 : 2 : PopActiveSnapshot();
4911 : 2 : CommitTransactionCommand();
4912 : :
4913 : : /* Notify launcher to update the conflict slot */
4914 : 2 : ApplyLauncherWakeup();
4915 : :
232 4916 : 2 : MySubscription->retentionactive = active;
4917 : :
4918 : 2 : return true;
4919 : : }
4920 : :
4921 : : /*
4922 : : * Reset all data fields of RetainDeadTuplesData except those used to
4923 : : * determine the timing for the next round of transaction ID advancement. We
4924 : : * can even use flushpos_update_time in the next round to decide whether to get
4925 : : * the latest flush position.
4926 : : */
4927 : : static void
245 4928 : 36 : reset_retention_data_fields(RetainDeadTuplesData *rdt_data)
4929 : : {
286 4930 : 36 : rdt_data->phase = RDT_GET_CANDIDATE_XID;
4931 : 36 : rdt_data->remote_lsn = InvalidXLogRecPtr;
4932 : 36 : rdt_data->remote_oldestxid = InvalidFullTransactionId;
4933 : 36 : rdt_data->remote_nextxid = InvalidFullTransactionId;
4934 : 36 : rdt_data->reply_time = 0;
4935 : 36 : rdt_data->remote_wait_for = InvalidFullTransactionId;
4936 : 36 : rdt_data->candidate_xid = InvalidTransactionId;
245 4937 : 36 : rdt_data->table_sync_wait_time = 0;
286 4938 : 36 : }
4939 : :
4940 : : /*
4941 : : * Adjust the interval for advancing non-removable transaction IDs.
4942 : : *
4943 : : * If there is no activity on the node or retention has been stopped, we
4944 : : * progressively double the interval used to advance non-removable transaction
4945 : : * ID. This helps conserve CPU and network resources when there's little benefit
4946 : : * to frequent updates.
4947 : : *
4948 : : * The interval is capped by the lowest of the following:
4949 : : * - wal_receiver_status_interval (if set and retention is active),
4950 : : * - a default maximum of 3 minutes,
4951 : : * - max_retention_duration (if retention is active).
4952 : : *
4953 : : * This ensures the interval never exceeds the retention boundary, even if other
4954 : : * limits are higher. Once activity resumes on the node and the retention is
4955 : : * active, the interval is reset to lesser of 100ms and max_retention_duration,
4956 : : * allowing timely advancement of non-removable transaction ID.
4957 : : *
4958 : : * XXX The use of wal_receiver_status_interval is a bit arbitrary so we can
4959 : : * consider the other interval or a separate GUC if the need arises.
4960 : : */
4961 : : static void
4962 : 75 : adjust_xid_advance_interval(RetainDeadTuplesData *rdt_data, bool new_xid_found)
4963 : : {
232 4964 [ + + + + ]: 75 : if (rdt_data->xid_advance_interval && !new_xid_found)
286 4965 : 32 : {
4966 : 32 : int max_interval = wal_receiver_status_interval
4967 : 64 : ? wal_receiver_status_interval * 1000
4968 [ + - ]: 32 : : MAX_XID_ADVANCE_INTERVAL;
4969 : :
4970 : : /*
4971 : : * No new transaction ID has been assigned since the last check, so
4972 : : * double the interval, but not beyond the maximum allowable value.
4973 : : */
4974 : 32 : rdt_data->xid_advance_interval = Min(rdt_data->xid_advance_interval * 2,
4975 : : max_interval);
4976 : : }
232 4977 [ + + ]: 43 : else if (rdt_data->xid_advance_interval &&
4978 [ + + ]: 30 : !MySubscription->retentionactive)
4979 : : {
4980 : : /*
4981 : : * Retention has been stopped, so double the interval-capped at a
4982 : : * maximum of 3 minutes. The wal_receiver_status_interval is
4983 : : * intentionally not used as an upper bound, since the likelihood of
4984 : : * retention resuming is lower than that of general activity resuming.
4985 : : */
4986 : 1 : rdt_data->xid_advance_interval = Min(rdt_data->xid_advance_interval * 2,
4987 : : MAX_XID_ADVANCE_INTERVAL);
4988 : : }
4989 : : else
4990 : : {
4991 : : /*
4992 : : * A new transaction ID was found or the interval is not yet
4993 : : * initialized, so set the interval to the minimum value.
4994 : : */
286 4995 : 42 : rdt_data->xid_advance_interval = MIN_XID_ADVANCE_INTERVAL;
4996 : : }
4997 : :
4998 : : /*
4999 : : * Ensure the wait time remains within the maximum retention time limit
5000 : : * when retention is active. Skip this cap when maxretention is zero,
5001 : : * which means unlimited retention (no timeout).
5002 : : */
7 5003 [ + + - + ]: 75 : if (MySubscription->retentionactive && MySubscription->maxretention > 0)
232 akapila@postgresql.o 5004 :UNC 0 : rdt_data->xid_advance_interval = Min(rdt_data->xid_advance_interval,
5005 : : MySubscription->maxretention);
286 akapila@postgresql.o 5006 :GNC 75 : }
5007 : :
5008 : : /*
5009 : : * Exit routine for apply workers due to subscription parameter changes.
5010 : : */
5011 : : static void
1212 akapila@postgresql.o 5012 :CBC 47 : apply_worker_exit(void)
5013 : : {
5014 [ - + ]: 47 : if (am_parallel_apply_worker())
5015 : : {
5016 : : /*
5017 : : * Don't stop the parallel apply worker as the leader will detect the
5018 : : * subscription parameter change and restart logical replication later
5019 : : * anyway. This also prevents the leader from reporting errors when
5020 : : * trying to communicate with a stopped parallel apply worker, which
5021 : : * would accidentally disable subscriptions if disable_on_error was
5022 : : * set.
5023 : : */
1212 akapila@postgresql.o 5024 :UBC 0 : return;
5025 : : }
5026 : :
5027 : : /*
5028 : : * Reset the last-start time for this apply worker so that the launcher
5029 : : * will restart it without waiting for wal_retrieve_retry_interval if the
5030 : : * subscription is still active, and so that we won't leak that hash table
5031 : : * entry if it isn't.
5032 : : */
1005 akapila@postgresql.o 5033 [ + - ]:CBC 47 : if (am_leader_apply_worker())
1199 tgl@sss.pgh.pa.us 5034 : 47 : ApplyLauncherForgetWorkerStartTime(MyLogicalRepWorker->subid);
5035 : :
1212 akapila@postgresql.o 5036 : 47 : proc_exit(0);
5037 : : }
5038 : :
5039 : : /*
5040 : : * Reread subscription info if needed.
5041 : : *
5042 : : * For significant changes, we react by exiting the current process; a new
5043 : : * one will be launched afterwards if needed.
5044 : : */
5045 : : void
3258 peter_e@gmx.net 5046 : 5792 : maybe_reread_subscription(void)
5047 : : {
5048 : : Subscription *newsub;
3275 bruce@momjian.us 5049 : 5792 : bool started_tx = false;
5050 : :
5051 : : /* When cache state is valid there is nothing to do here. */
3258 peter_e@gmx.net 5052 [ + + ]: 5792 : if (MySubscriptionValid)
5053 : 5699 : return;
5054 : :
5055 : : /* This function might be called inside or outside of transaction. */
3330 5056 [ + + ]: 93 : if (!IsTransactionState())
5057 : : {
5058 : 87 : StartTransactionCommand();
5059 : 87 : started_tx = true;
5060 : : }
5061 : :
60 jdavis@postgresql.or 5062 :GNC 93 : newsub = GetSubscription(MyLogicalRepWorker->subid, true, true);
5063 : :
42 5064 [ + - ]: 93 : if (newsub)
5065 : : {
5066 : 93 : MemoryContextSetParent(newsub->cxt, ApplyContext);
5067 : : }
5068 : : else
5069 : : {
5070 : : /*
5071 : : * Exit if the subscription was removed. This normally should not
5072 : : * happen as the worker gets killed during DROP SUBSCRIPTION.
5073 : : */
3393 peter_e@gmx.net 5074 [ # # ]:UBC 0 : ereport(LOG,
5075 : : (errmsg("logical replication worker for subscription \"%s\" will stop because the subscription was removed",
5076 : : MySubscription->name)));
5077 : :
5078 : : /* Ensure we remove no-longer-useful entry for worker's start time */
1005 akapila@postgresql.o 5079 [ # # ]: 0 : if (am_leader_apply_worker())
1199 tgl@sss.pgh.pa.us 5080 : 0 : ApplyLauncherForgetWorkerStartTime(MyLogicalRepWorker->subid);
5081 : :
3393 peter_e@gmx.net 5082 : 0 : proc_exit(0);
5083 : : }
5084 : :
5085 : : /* Exit if the subscription was disabled. */
3283 peter_e@gmx.net 5086 [ + + ]:CBC 93 : if (!newsub->enabled)
5087 : : {
5088 [ + - ]: 13 : ereport(LOG,
5089 : : (errmsg("logical replication worker for subscription \"%s\" will stop because the subscription was disabled",
5090 : : MySubscription->name)));
5091 : :
1212 akapila@postgresql.o 5092 : 13 : apply_worker_exit();
5093 : : }
5094 : :
5095 : : /* !slotname should never happen when enabled is true. */
3283 peter_e@gmx.net 5096 [ - + ]: 80 : Assert(newsub->slotname);
5097 : :
5098 : : /* two-phase cannot be altered while the worker is running */
1756 akapila@postgresql.o 5099 [ - + ]: 80 : Assert(newsub->twophasestate == MySubscription->twophasestate);
5100 : :
5101 : : /*
5102 : : * Exit if any parameter that affects the remote connection was changed.
5103 : : * The launcher will start a new worker but note that the parallel apply
5104 : : * worker won't restart if the streaming option's value is changed from
5105 : : * 'parallel' to any other value or the server decides not to stream the
5106 : : * in-progress transaction.
5107 : : */
2117 tgl@sss.pgh.pa.us 5108 [ + + ]: 80 : if (strcmp(newsub->conninfo, MySubscription->conninfo) != 0 ||
5109 [ + + ]: 75 : strcmp(newsub->name, MySubscription->name) != 0 ||
5110 [ + - ]: 74 : strcmp(newsub->slotname, MySubscription->slotname) != 0 ||
5111 [ + + ]: 74 : newsub->binary != MySubscription->binary ||
2070 akapila@postgresql.o 5112 [ + + ]: 68 : newsub->stream != MySubscription->stream ||
1111 5113 [ + - ]: 63 : newsub->passwordrequired != MySubscription->passwordrequired ||
1384 5114 [ + + ]: 63 : strcmp(newsub->origin, MySubscription->origin) != 0 ||
1579 jdavis@postgresql.or 5115 [ + + ]: 61 : newsub->owner != MySubscription->owner ||
2117 tgl@sss.pgh.pa.us 5116 [ + + ]: 60 : !equal(newsub->publications, MySubscription->publications))
5117 : : {
1212 akapila@postgresql.o 5118 [ - + ]: 29 : if (am_parallel_apply_worker())
1212 akapila@postgresql.o 5119 [ # # ]:UBC 0 : ereport(LOG,
5120 : : (errmsg("logical replication parallel apply worker for subscription \"%s\" will stop because of a parameter change",
5121 : : MySubscription->name)));
5122 : : else
1212 akapila@postgresql.o 5123 [ + - ]:CBC 29 : ereport(LOG,
5124 : : (errmsg("logical replication worker for subscription \"%s\" will restart because of a parameter change",
5125 : : MySubscription->name)));
5126 : :
5127 : 29 : apply_worker_exit();
5128 : : }
5129 : :
5130 : : /*
5131 : : * Exit if the subscription owner's superuser privileges have been
5132 : : * revoked.
5133 : : */
931 5134 [ + + + + ]: 51 : if (!newsub->ownersuperuser && MySubscription->ownersuperuser)
5135 : : {
5136 [ - + ]: 4 : if (am_parallel_apply_worker())
931 akapila@postgresql.o 5137 [ # # ]:UBC 0 : ereport(LOG,
5138 : : errmsg("logical replication parallel apply worker for subscription \"%s\" will stop because the subscription owner's superuser privileges have been revoked",
5139 : : MySubscription->name));
5140 : : else
931 akapila@postgresql.o 5141 [ + - ]:CBC 4 : ereport(LOG,
5142 : : errmsg("logical replication worker for subscription \"%s\" will restart because the subscription owner's superuser privileges have been revoked",
5143 : : MySubscription->name));
5144 : :
5145 : 4 : apply_worker_exit();
5146 : : }
5147 : :
5148 : : /* Check for other changes that should never happen too. */
3319 peter_e@gmx.net 5149 [ - + ]: 47 : if (newsub->dbid != MySubscription->dbid)
5150 : : {
3393 peter_e@gmx.net 5151 [ # # ]:UBC 0 : elog(ERROR, "subscription %u changed unexpectedly",
5152 : : MyLogicalRepWorker->subid);
5153 : : }
5154 : :
5155 : : /* Clean old subscription info and switch to new one. */
42 jdavis@postgresql.or 5156 :GNC 47 : MemoryContextDelete(MySubscription->cxt);
3393 peter_e@gmx.net 5157 :CBC 47 : MySubscription = newsub;
5158 : :
5159 : : /* Change synchronous commit according to the user's wishes */
3308 5160 : 47 : SetConfigOption("synchronous_commit", MySubscription->synccommit,
5161 : : PGC_BACKEND, PGC_S_OVERRIDE);
5162 : :
5163 : : /* Change wal_receiver_timeout according to the user's wishes */
74 fujii@postgresql.org 5164 :GNC 47 : set_wal_receiver_timeout();
5165 : :
3330 peter_e@gmx.net 5166 [ + + ]:CBC 47 : if (started_tx)
5167 : 44 : CommitTransactionCommand();
5168 : :
3393 5169 : 47 : MySubscriptionValid = true;
5170 : : }
5171 : :
5172 : : /*
5173 : : * Change wal_receiver_timeout to MySubscription->walrcvtimeout.
5174 : : */
5175 : : static void
74 fujii@postgresql.org 5176 :GNC 566 : set_wal_receiver_timeout(void)
5177 : : {
5178 : : bool parsed;
5179 : : int val;
5180 : 566 : int prev_timeout = wal_receiver_timeout;
5181 : :
5182 : : /*
5183 : : * Set the wal_receiver_timeout GUC to MySubscription->walrcvtimeout,
5184 : : * which comes from the subscription's wal_receiver_timeout option. If the
5185 : : * value is -1, reset the GUC to its default, meaning it will inherit from
5186 : : * the server config, command line, or role/database settings.
5187 : : */
5188 : 566 : parsed = parse_int(MySubscription->walrcvtimeout, &val, 0, NULL);
5189 [ + - + - ]: 566 : if (parsed && val == -1)
5190 : 566 : SetConfigOption("wal_receiver_timeout", NULL,
5191 : : PGC_BACKEND, PGC_S_SESSION);
5192 : : else
74 fujii@postgresql.org 5193 :UNC 0 : SetConfigOption("wal_receiver_timeout", MySubscription->walrcvtimeout,
5194 : : PGC_BACKEND, PGC_S_SESSION);
5195 : :
5196 : : /*
5197 : : * Log the wal_receiver_timeout setting (in milliseconds) as a debug
5198 : : * message when it changes, to verify it was set correctly.
5199 : : */
74 fujii@postgresql.org 5200 [ - + ]:GNC 566 : if (prev_timeout != wal_receiver_timeout)
74 fujii@postgresql.org 5201 [ # # ]:UNC 0 : elog(DEBUG1, "logical replication worker for subscription \"%s\" wal_receiver_timeout: %d ms",
5202 : : MySubscription->name, wal_receiver_timeout);
74 fujii@postgresql.org 5203 :GNC 566 : }
5204 : :
5205 : : /*
5206 : : * Callback from subscription syscache invalidation. Also needed for server or
5207 : : * user mapping invalidation, which can change the connection information for
5208 : : * subscriptions that connect using a server object.
5209 : : */
5210 : : static void
76 michael@paquier.xyz 5211 : 97 : subscription_change_cb(Datum arg, SysCacheIdentifier cacheid, uint32 hashvalue)
5212 : : {
3393 peter_e@gmx.net 5213 :CBC 97 : MySubscriptionValid = false;
5214 : 97 : }
5215 : :
5216 : : /*
5217 : : * subxact_info_write
5218 : : * Store information about subxacts for a toplevel transaction.
5219 : : *
5220 : : * For each subxact we store offset of its first change in the main file.
5221 : : * The file is always over-written as a whole.
5222 : : *
5223 : : * XXX We should only store subxacts that were not aborted yet.
5224 : : */
5225 : : static void
2070 akapila@postgresql.o 5226 : 371 : subxact_info_write(Oid subid, TransactionId xid)
5227 : : {
5228 : : char path[MAXPGPATH];
5229 : : Size len;
5230 : : BufFile *fd;
5231 : :
5232 [ - + ]: 371 : Assert(TransactionIdIsValid(xid));
5233 : :
5234 : : /* construct the subxact filename */
1706 5235 : 371 : subxact_filename(path, subid, xid);
5236 : :
5237 : : /* Delete the subxacts file, if exists. */
2070 5238 [ + + ]: 371 : if (subxact_data.nsubxacts == 0)
5239 : : {
1706 5240 : 289 : cleanup_subxact_info();
5241 : 289 : BufFileDeleteFileSet(MyLogicalRepWorker->stream_fileset, path, true);
5242 : :
2070 5243 : 289 : return;
5244 : : }
5245 : :
5246 : : /*
5247 : : * Create the subxact file if it not already created, otherwise open the
5248 : : * existing file.
5249 : : */
1706 5250 : 82 : fd = BufFileOpenFileSet(MyLogicalRepWorker->stream_fileset, path, O_RDWR,
5251 : : true);
5252 [ + + ]: 82 : if (fd == NULL)
5253 : 8 : fd = BufFileCreateFileSet(MyLogicalRepWorker->stream_fileset, path);
5254 : :
2070 5255 : 82 : len = sizeof(SubXactInfo) * subxact_data.nsubxacts;
5256 : :
5257 : : /* Write the subxact count and subxact info */
5258 : 82 : BufFileWrite(fd, &subxact_data.nsubxacts, sizeof(subxact_data.nsubxacts));
5259 : 82 : BufFileWrite(fd, subxact_data.subxacts, len);
5260 : :
5261 : 82 : BufFileClose(fd);
5262 : :
5263 : : /* free the memory allocated for subxact info */
5264 : 82 : cleanup_subxact_info();
5265 : : }
5266 : :
5267 : : /*
5268 : : * subxact_info_read
5269 : : * Restore information about subxacts of a streamed transaction.
5270 : : *
5271 : : * Read information about subxacts into the structure subxact_data that can be
5272 : : * used later.
5273 : : */
5274 : : static void
5275 : 343 : subxact_info_read(Oid subid, TransactionId xid)
5276 : : {
5277 : : char path[MAXPGPATH];
5278 : : Size len;
5279 : : BufFile *fd;
5280 : : MemoryContext oldctx;
5281 : :
5282 [ - + ]: 343 : Assert(!subxact_data.subxacts);
5283 [ - + ]: 343 : Assert(subxact_data.nsubxacts == 0);
5284 [ - + ]: 343 : Assert(subxact_data.nsubxacts_max == 0);
5285 : :
5286 : : /*
5287 : : * If the subxact file doesn't exist that means we don't have any subxact
5288 : : * info.
5289 : : */
5290 : 343 : subxact_filename(path, subid, xid);
1706 5291 : 343 : fd = BufFileOpenFileSet(MyLogicalRepWorker->stream_fileset, path, O_RDONLY,
5292 : : true);
5293 [ + + ]: 343 : if (fd == NULL)
5294 : 264 : return;
5295 : :
5296 : : /* read number of subxact items */
1205 peter@eisentraut.org 5297 : 79 : BufFileReadExact(fd, &subxact_data.nsubxacts, sizeof(subxact_data.nsubxacts));
5298 : :
2070 akapila@postgresql.o 5299 : 79 : len = sizeof(SubXactInfo) * subxact_data.nsubxacts;
5300 : :
5301 : : /* we keep the maximum as a power of 2 */
237 michael@paquier.xyz 5302 :GNC 79 : subxact_data.nsubxacts_max = 1 << pg_ceil_log2_32(subxact_data.nsubxacts);
5303 : :
5304 : : /*
5305 : : * Allocate subxact information in the logical streaming context. We need
5306 : : * this information during the complete stream so that we can add the sub
5307 : : * transaction info to this. On stream stop we will flush this information
5308 : : * to the subxact file and reset the logical streaming context.
5309 : : */
2070 akapila@postgresql.o 5310 :CBC 79 : oldctx = MemoryContextSwitchTo(LogicalStreamingContext);
60 msawada@postgresql.o 5311 :GNC 79 : subxact_data.subxacts = palloc_array(SubXactInfo,
5312 : : subxact_data.nsubxacts_max);
2070 akapila@postgresql.o 5313 :CBC 79 : MemoryContextSwitchTo(oldctx);
5314 : :
1205 peter@eisentraut.org 5315 [ + - ]: 79 : if (len > 0)
5316 : 79 : BufFileReadExact(fd, subxact_data.subxacts, len);
5317 : :
2070 akapila@postgresql.o 5318 : 79 : BufFileClose(fd);
5319 : : }
5320 : :
5321 : : /*
5322 : : * subxact_info_add
5323 : : * Add information about a subxact (offset in the main file).
5324 : : */
5325 : : static void
5326 : 102513 : subxact_info_add(TransactionId xid)
5327 : : {
5328 : 102513 : SubXactInfo *subxacts = subxact_data.subxacts;
5329 : : int64 i;
5330 : :
5331 : : /* We must have a valid top level stream xid and a stream fd. */
5332 [ - + ]: 102513 : Assert(TransactionIdIsValid(stream_xid));
5333 [ - + ]: 102513 : Assert(stream_fd != NULL);
5334 : :
5335 : : /*
5336 : : * If the XID matches the toplevel transaction, we don't want to add it.
5337 : : */
5338 [ + + ]: 102513 : if (stream_xid == xid)
5339 : 92389 : return;
5340 : :
5341 : : /*
5342 : : * In most cases we're checking the same subxact as we've already seen in
5343 : : * the last call, so make sure to ignore it (this change comes later).
5344 : : */
5345 [ + + ]: 10124 : if (subxact_data.subxact_last == xid)
5346 : 10048 : return;
5347 : :
5348 : : /* OK, remember we're processing this XID. */
5349 : 76 : subxact_data.subxact_last = xid;
5350 : :
5351 : : /*
5352 : : * Check if the transaction is already present in the array of subxact. We
5353 : : * intentionally scan the array from the tail, because we're likely adding
5354 : : * a change for the most recent subtransactions.
5355 : : *
5356 : : * XXX Can we rely on the subxact XIDs arriving in sorted order? That
5357 : : * would allow us to use binary search here.
5358 : : */
5359 [ + + ]: 95 : for (i = subxact_data.nsubxacts; i > 0; i--)
5360 : : {
5361 : : /* found, so we're done */
5362 [ + + ]: 76 : if (subxacts[i - 1].xid == xid)
5363 : 57 : return;
5364 : : }
5365 : :
5366 : : /* This is a new subxact, so we need to add it to the array. */
5367 [ + + ]: 19 : if (subxact_data.nsubxacts == 0)
5368 : : {
5369 : : MemoryContext oldctx;
5370 : :
5371 : 8 : subxact_data.nsubxacts_max = 128;
5372 : :
5373 : : /*
5374 : : * Allocate this memory for subxacts in per-stream context, see
5375 : : * subxact_info_read.
5376 : : */
5377 : 8 : oldctx = MemoryContextSwitchTo(LogicalStreamingContext);
60 msawada@postgresql.o 5378 :GNC 8 : subxacts = palloc_array(SubXactInfo, subxact_data.nsubxacts_max);
2070 akapila@postgresql.o 5379 :CBC 8 : MemoryContextSwitchTo(oldctx);
5380 : : }
5381 [ + + ]: 11 : else if (subxact_data.nsubxacts == subxact_data.nsubxacts_max)
5382 : : {
5383 : 10 : subxact_data.nsubxacts_max *= 2;
60 msawada@postgresql.o 5384 :GNC 10 : subxacts = repalloc_array(subxacts, SubXactInfo,
5385 : : subxact_data.nsubxacts_max);
5386 : : }
5387 : :
2070 akapila@postgresql.o 5388 :CBC 19 : subxacts[subxact_data.nsubxacts].xid = xid;
5389 : :
5390 : : /*
5391 : : * Get the current offset of the stream file and store it as offset of
5392 : : * this subxact.
5393 : : */
5394 : 19 : BufFileTell(stream_fd,
5395 : 19 : &subxacts[subxact_data.nsubxacts].fileno,
5396 : 19 : &subxacts[subxact_data.nsubxacts].offset);
5397 : :
5398 : 19 : subxact_data.nsubxacts++;
5399 : 19 : subxact_data.subxacts = subxacts;
5400 : : }
5401 : :
5402 : : /* format filename for file containing the info about subxacts */
5403 : : static inline void
5404 : 745 : subxact_filename(char *path, Oid subid, TransactionId xid)
5405 : : {
5406 : 745 : snprintf(path, MAXPGPATH, "%u-%u.subxacts", subid, xid);
5407 : 745 : }
5408 : :
5409 : : /* format filename for file containing serialized changes */
5410 : : static inline void
5411 : 437 : changes_filename(char *path, Oid subid, TransactionId xid)
5412 : : {
5413 : 437 : snprintf(path, MAXPGPATH, "%u-%u.changes", subid, xid);
5414 : 437 : }
5415 : :
5416 : : /*
5417 : : * stream_cleanup_files
5418 : : * Cleanup files for a subscription / toplevel transaction.
5419 : : *
5420 : : * Remove files with serialized changes and subxact info for a particular
5421 : : * toplevel transaction. Each subscription has a separate set of files
5422 : : * for any toplevel transaction.
5423 : : */
5424 : : void
5425 : 31 : stream_cleanup_files(Oid subid, TransactionId xid)
5426 : : {
5427 : : char path[MAXPGPATH];
5428 : :
5429 : : /* Delete the changes file. */
5430 : 31 : changes_filename(path, subid, xid);
1706 5431 : 31 : BufFileDeleteFileSet(MyLogicalRepWorker->stream_fileset, path, false);
5432 : :
5433 : : /* Delete the subxact file, if it exists. */
5434 : 31 : subxact_filename(path, subid, xid);
5435 : 31 : BufFileDeleteFileSet(MyLogicalRepWorker->stream_fileset, path, true);
2070 5436 : 31 : }
5437 : :
5438 : : /*
5439 : : * stream_open_file
5440 : : * Open a file that we'll use to serialize changes for a toplevel
5441 : : * transaction.
5442 : : *
5443 : : * Open a file for streamed changes from a toplevel transaction identified
5444 : : * by stream_xid (global variable). If it's the first chunk of streamed
5445 : : * changes for this transaction, create the buffile, otherwise open the
5446 : : * previously created file.
5447 : : */
5448 : : static void
5449 : 362 : stream_open_file(Oid subid, TransactionId xid, bool first_segment)
5450 : : {
5451 : : char path[MAXPGPATH];
5452 : : MemoryContext oldcxt;
5453 : :
5454 [ - + ]: 362 : Assert(OidIsValid(subid));
5455 [ - + ]: 362 : Assert(TransactionIdIsValid(xid));
5456 [ - + ]: 362 : Assert(stream_fd == NULL);
5457 : :
5458 : :
5459 : 362 : changes_filename(path, subid, xid);
5460 [ - + ]: 362 : elog(DEBUG1, "opening file \"%s\" for streamed changes", path);
5461 : :
5462 : : /*
5463 : : * Create/open the buffiles under the logical streaming context so that we
5464 : : * have those files until stream stop.
5465 : : */
5466 : 362 : oldcxt = MemoryContextSwitchTo(LogicalStreamingContext);
5467 : :
5468 : : /*
5469 : : * If this is the first streamed segment, create the changes file.
5470 : : * Otherwise, just open the file for writing, in append mode.
5471 : : */
5472 [ + + ]: 362 : if (first_segment)
1706 5473 : 32 : stream_fd = BufFileCreateFileSet(MyLogicalRepWorker->stream_fileset,
5474 : : path);
5475 : : else
5476 : : {
5477 : : /*
5478 : : * Open the file and seek to the end of the file because we always
5479 : : * append the changes file.
5480 : : */
5481 : 330 : stream_fd = BufFileOpenFileSet(MyLogicalRepWorker->stream_fileset,
5482 : : path, O_RDWR, false);
2070 5483 : 330 : BufFileSeek(stream_fd, 0, 0, SEEK_END);
5484 : : }
5485 : :
5486 : 362 : MemoryContextSwitchTo(oldcxt);
5487 : 362 : }
5488 : :
5489 : : /*
5490 : : * stream_close_file
5491 : : * Close the currently open file with streamed changes.
5492 : : */
5493 : : static void
5494 : 392 : stream_close_file(void)
5495 : : {
5496 [ - + ]: 392 : Assert(stream_fd != NULL);
5497 : :
5498 : 392 : BufFileClose(stream_fd);
5499 : :
5500 : 392 : stream_fd = NULL;
5501 : 392 : }
5502 : :
5503 : : /*
5504 : : * stream_write_change
5505 : : * Serialize a change to a file for the current toplevel transaction.
5506 : : *
5507 : : * The change is serialized in a simple format, with length (not including
5508 : : * the length), action code (identifying the message type) and message
5509 : : * contents (without the subxact TransactionId value).
5510 : : */
5511 : : static void
5512 : 107554 : stream_write_change(char action, StringInfo s)
5513 : : {
5514 : : int len;
5515 : :
5516 [ - + ]: 107554 : Assert(stream_fd != NULL);
5517 : :
5518 : : /* total on-disk size, including the action type character */
5519 : 107554 : len = (s->len - s->cursor) + sizeof(char);
5520 : :
5521 : : /* first write the size */
5522 : 107554 : BufFileWrite(stream_fd, &len, sizeof(len));
5523 : :
5524 : : /* then the action */
5525 : 107554 : BufFileWrite(stream_fd, &action, sizeof(action));
5526 : :
5527 : : /* and finally the remaining part of the buffer (after the XID) */
5528 : 107554 : len = (s->len - s->cursor);
5529 : :
5530 : 107554 : BufFileWrite(stream_fd, &s->data[s->cursor], len);
5531 : 107554 : }
5532 : :
5533 : : /*
5534 : : * stream_open_and_write_change
5535 : : * Serialize a message to a file for the given transaction.
5536 : : *
5537 : : * This function is similar to stream_write_change except that it will open the
5538 : : * target file if not already before writing the message and close the file at
5539 : : * the end.
5540 : : */
5541 : : static void
1212 5542 : 5 : stream_open_and_write_change(TransactionId xid, char action, StringInfo s)
5543 : : {
5544 [ - + ]: 5 : Assert(!in_streamed_transaction);
5545 : :
5546 [ + - ]: 5 : if (!stream_fd)
5547 : 5 : stream_start_internal(xid, false);
5548 : :
5549 : 5 : stream_write_change(action, s);
5550 : 5 : stream_stop_internal(xid);
5551 : 5 : }
5552 : :
5553 : : /*
5554 : : * Sets streaming options including replication slot name and origin start
5555 : : * position. Workers need these options for logical replication.
5556 : : */
5557 : : void
1006 5558 : 447 : set_stream_options(WalRcvStreamOptions *options,
5559 : : char *slotname,
5560 : : XLogRecPtr *origin_startpos)
5561 : : {
5562 : : int server_version;
5563 : :
5564 : 447 : options->logical = true;
5565 : 447 : options->startpoint = *origin_startpos;
5566 : 447 : options->slotname = slotname;
5567 : :
5568 : 447 : server_version = walrcv_server_version(LogRepWorkerWalRcvConn);
5569 : 447 : options->proto.logical.proto_version =
5570 [ - + - - : 447 : server_version >= 160000 ? LOGICALREP_PROTO_STREAM_PARALLEL_VERSION_NUM :
- - ]
5571 : : server_version >= 150000 ? LOGICALREP_PROTO_TWOPHASE_VERSION_NUM :
5572 : : server_version >= 140000 ? LOGICALREP_PROTO_STREAM_VERSION_NUM :
5573 : : LOGICALREP_PROTO_VERSION_NUM;
5574 : :
5575 : 447 : options->proto.logical.publication_names = MySubscription->publications;
5576 : 447 : options->proto.logical.binary = MySubscription->binary;
5577 : :
5578 : : /*
5579 : : * Assign the appropriate option value for streaming option according to
5580 : : * the 'streaming' mode and the publisher's ability to support that mode.
5581 : : */
5582 [ + - ]: 447 : if (server_version >= 160000 &&
5583 [ + + ]: 447 : MySubscription->stream == LOGICALREP_STREAM_PARALLEL)
5584 : : {
5585 : 414 : options->proto.logical.streaming_str = "parallel";
5586 : 414 : MyLogicalRepWorker->parallel_apply = true;
5587 : : }
5588 [ + - ]: 33 : else if (server_version >= 140000 &&
5589 [ + + ]: 33 : MySubscription->stream != LOGICALREP_STREAM_OFF)
5590 : : {
5591 : 25 : options->proto.logical.streaming_str = "on";
5592 : 25 : MyLogicalRepWorker->parallel_apply = false;
5593 : : }
5594 : : else
5595 : : {
5596 : 8 : options->proto.logical.streaming_str = NULL;
5597 : 8 : MyLogicalRepWorker->parallel_apply = false;
5598 : : }
5599 : :
5600 : 447 : options->proto.logical.twophase = false;
5601 : 447 : options->proto.logical.origin = pstrdup(MySubscription->origin);
5602 : 447 : }
5603 : :
5604 : : /*
5605 : : * Cleanup the memory for subxacts and reset the related variables.
5606 : : */
5607 : : static inline void
153 nathan@postgresql.or 5608 :GNC 375 : cleanup_subxact_info(void)
5609 : : {
2070 akapila@postgresql.o 5610 [ + + ]:CBC 375 : if (subxact_data.subxacts)
5611 : 87 : pfree(subxact_data.subxacts);
5612 : :
5613 : 375 : subxact_data.subxacts = NULL;
5614 : 375 : subxact_data.subxact_last = InvalidTransactionId;
5615 : 375 : subxact_data.nsubxacts = 0;
5616 : 375 : subxact_data.nsubxacts_max = 0;
5617 : 375 : }
5618 : :
5619 : : /*
5620 : : * Common function to run the apply loop with error handling. Disable the
5621 : : * subscription, if necessary.
5622 : : *
5623 : : * Note that we don't handle FATAL errors which are probably because
5624 : : * of system resource error and are not repeatable.
5625 : : */
5626 : : void
1006 5627 : 447 : start_apply(XLogRecPtr origin_startpos)
5628 : : {
1513 5629 [ + + ]: 447 : PG_TRY();
5630 : : {
1006 5631 : 447 : LogicalRepApplyLoop(origin_startpos);
5632 : : }
1513 5633 : 94 : PG_CATCH();
5634 : : {
5635 : : /*
5636 : : * Reset the origin state to prevent the advancement of origin
5637 : : * progress if we fail to apply. Otherwise, this will result in
5638 : : * transaction loss as that transaction won't be sent again by the
5639 : : * server.
5640 : : */
97 msawada@postgresql.o 5641 :GNC 94 : replorigin_xact_clear(true);
5642 : :
1513 akapila@postgresql.o 5643 [ + + ]:CBC 94 : if (MySubscription->disableonerr)
5644 : 3 : DisableSubscriptionAndExit();
5645 : : else
5646 : : {
5647 : : /*
5648 : : * Report the worker failed while applying changes. Abort the
5649 : : * current transaction so that the stats message is sent in an
5650 : : * idle state.
5651 : : */
5652 : 91 : AbortOutOfAnyTransaction();
74 akapila@postgresql.o 5653 :GNC 91 : pgstat_report_subscription_error(MySubscription->oid);
5654 : :
1513 akapila@postgresql.o 5655 :CBC 91 : PG_RE_THROW();
5656 : : }
5657 : : }
1513 akapila@postgresql.o 5658 [ # # ]:UBC 0 : PG_END_TRY();
5659 : 0 : }
5660 : :
5661 : : /*
5662 : : * Runs the leader apply worker.
5663 : : *
5664 : : * It sets up replication origin, streaming options and then starts streaming.
5665 : : */
5666 : : static void
153 nathan@postgresql.or 5667 :GNC 285 : run_apply_worker(void)
5668 : : {
5669 : : char originname[NAMEDATALEN];
1006 akapila@postgresql.o 5670 :CBC 285 : XLogRecPtr origin_startpos = InvalidXLogRecPtr;
5671 : 285 : char *slotname = NULL;
5672 : : WalRcvStreamOptions options;
5673 : : ReplOriginId originid;
5674 : : TimeLineID startpointTLI;
5675 : : char *err;
5676 : : bool must_use_password;
5677 : :
5678 : 285 : slotname = MySubscription->slotname;
5679 : :
5680 : : /*
5681 : : * This shouldn't happen if the subscription is enabled, but guard against
5682 : : * DDL bugs or manual catalog changes. (libpqwalreceiver will crash if
5683 : : * slot is NULL.)
5684 : : */
5685 [ - + ]: 285 : if (!slotname)
1006 akapila@postgresql.o 5686 [ # # ]:UBC 0 : ereport(ERROR,
5687 : : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
5688 : : errmsg("subscription has no replication slot set")));
5689 : :
5690 : : /* Setup replication origin tracking. */
1006 akapila@postgresql.o 5691 :CBC 285 : ReplicationOriginNameForLogicalRep(MySubscription->oid, InvalidOid,
5692 : : originname, sizeof(originname));
5693 : 285 : StartTransactionCommand();
5694 : 285 : originid = replorigin_by_name(originname, true);
5695 [ - + ]: 285 : if (!OidIsValid(originid))
1006 akapila@postgresql.o 5696 :UBC 0 : originid = replorigin_create(originname);
1006 akapila@postgresql.o 5697 :CBC 285 : replorigin_session_setup(originid, 0);
97 msawada@postgresql.o 5698 :GNC 285 : replorigin_xact_state.origin = originid;
1006 akapila@postgresql.o 5699 :CBC 285 : origin_startpos = replorigin_session_get_progress(false);
931 5700 : 285 : CommitTransactionCommand();
5701 : :
5702 : : /* Is the use of a password mandatory? */
1006 5703 [ + + ]: 545 : must_use_password = MySubscription->passwordrequired &&
931 5704 [ + + ]: 260 : !MySubscription->ownersuperuser;
5705 : :
1006 5706 : 285 : LogRepWorkerWalRcvConn = walrcv_connect(MySubscription->conninfo, true,
5707 : : true, must_use_password,
5708 : : MySubscription->name, &err);
5709 : :
5710 [ + + ]: 278 : if (LogRepWorkerWalRcvConn == NULL)
5711 [ + - ]: 29 : ereport(ERROR,
5712 : : (errcode(ERRCODE_CONNECTION_FAILURE),
5713 : : errmsg("apply worker for subscription \"%s\" could not connect to the publisher: %s",
5714 : : MySubscription->name, err)));
5715 : :
5716 : : /*
5717 : : * We don't really use the output identify_system for anything but it does
5718 : : * some initializations on the upstream so let's still call it.
5719 : : */
5720 : 249 : (void) walrcv_identify_system(LogRepWorkerWalRcvConn, &startpointTLI);
5721 : :
5722 : 249 : set_apply_error_context_origin(originname);
5723 : :
5724 : 249 : set_stream_options(&options, slotname, &origin_startpos);
5725 : :
5726 : : /*
5727 : : * Even when the two_phase mode is requested by the user, it remains as
5728 : : * the tri-state PENDING until all tablesyncs have reached READY state.
5729 : : * Only then, can it become ENABLED.
5730 : : *
5731 : : * Note: If the subscription has no tables then leave the state as
5732 : : * PENDING, which allows ALTER SUBSCRIPTION ... REFRESH PUBLICATION to
5733 : : * work.
5734 : : */
5735 [ + + + + ]: 264 : if (MySubscription->twophasestate == LOGICALREP_TWOPHASE_STATE_PENDING &&
5736 : 15 : AllTablesyncsReady())
5737 : : {
5738 : : /* Start streaming with two_phase enabled */
5739 : 9 : options.proto.logical.twophase = true;
5740 : 9 : walrcv_startstreaming(LogRepWorkerWalRcvConn, &options);
5741 : :
5742 : 9 : StartTransactionCommand();
5743 : :
5744 : : /*
5745 : : * Updating pg_subscription might involve TOAST table access, so
5746 : : * ensure we have a valid snapshot.
5747 : : */
340 nathan@postgresql.or 5748 : 9 : PushActiveSnapshot(GetTransactionSnapshot());
5749 : :
1006 akapila@postgresql.o 5750 : 9 : UpdateTwoPhaseState(MySubscription->oid, LOGICALREP_TWOPHASE_STATE_ENABLED);
5751 : 9 : MySubscription->twophasestate = LOGICALREP_TWOPHASE_STATE_ENABLED;
340 nathan@postgresql.or 5752 : 9 : PopActiveSnapshot();
1006 akapila@postgresql.o 5753 : 9 : CommitTransactionCommand();
5754 : : }
5755 : : else
5756 : : {
5757 : 240 : walrcv_startstreaming(LogRepWorkerWalRcvConn, &options);
5758 : : }
5759 : :
5760 [ + + + + : 249 : ereport(DEBUG1,
+ - + - ]
5761 : : (errmsg_internal("logical replication apply worker for subscription \"%s\" two_phase is %s",
5762 : : MySubscription->name,
5763 : : MySubscription->twophasestate == LOGICALREP_TWOPHASE_STATE_DISABLED ? "DISABLED" :
5764 : : MySubscription->twophasestate == LOGICALREP_TWOPHASE_STATE_PENDING ? "PENDING" :
5765 : : MySubscription->twophasestate == LOGICALREP_TWOPHASE_STATE_ENABLED ? "ENABLED" :
5766 : : "?")));
5767 : :
5768 : : /* Run the main loop. */
5769 : 249 : start_apply(origin_startpos);
1513 akapila@postgresql.o 5770 :UBC 0 : }
5771 : :
5772 : : /*
5773 : : * Common initialization for leader apply worker, parallel apply worker,
5774 : : * tablesync worker and sequencesync worker.
5775 : : *
5776 : : * Initialize the database connection, in-memory subscription and necessary
5777 : : * config options.
5778 : : */
5779 : : void
1006 akapila@postgresql.o 5780 :CBC 593 : InitializeLogRepWorker(void)
5781 : : {
5782 : : /* Run as replica session replication role. */
3393 peter_e@gmx.net 5783 : 593 : SetConfigOption("session_replication_role", "replica",
5784 : : PGC_SUSET, PGC_S_OVERRIDE);
5785 : :
5786 : : /* Connect to our database. */
5787 : 593 : BackgroundWorkerInitializeConnectionByOid(MyLogicalRepWorker->dbid,
2952 magnus@hagander.net 5788 : 593 : MyLogicalRepWorker->userid,
5789 : : 0);
5790 : :
5791 : : /*
5792 : : * Set always-secure search path, so malicious users can't redirect user
5793 : : * code (e.g. pg_index.indexprs).
5794 : : */
2094 noah@leadboat.com 5795 : 588 : SetConfigOption("search_path", "", PGC_SUSET, PGC_S_OVERRIDE);
5796 : :
3283 peter_e@gmx.net 5797 : 588 : ApplyContext = AllocSetContextCreate(TopMemoryContext,
5798 : : "ApplyContext",
5799 : : ALLOCSET_DEFAULT_SIZES);
5800 : :
3393 5801 : 588 : StartTransactionCommand();
5802 : :
5803 : : /*
5804 : : * Lock the subscription to prevent it from being concurrently dropped,
5805 : : * then re-verify its existence. After the initialization, the worker will
5806 : : * be terminated gracefully if the subscription is dropped.
5807 : : */
259 akapila@postgresql.o 5808 : 588 : LockSharedObject(SubscriptionRelationId, MyLogicalRepWorker->subid, 0,
5809 : : AccessShareLock);
5810 : :
60 jdavis@postgresql.or 5811 :GNC 586 : MySubscription = GetSubscription(MyLogicalRepWorker->subid, true, true);
5812 : :
42 5813 [ + + ]: 586 : if (MySubscription)
5814 : : {
5815 : 519 : MemoryContextSetParent(MySubscription->cxt, ApplyContext);
5816 : : }
5817 : : else
5818 : : {
2951 peter_e@gmx.net 5819 [ + - ]:CBC 67 : ereport(LOG,
5820 : : (errmsg("logical replication worker for subscription %u will not start because the subscription was removed during startup",
5821 : : MyLogicalRepWorker->subid)));
5822 : :
5823 : : /* Ensure we remove no-longer-useful entry for worker's start time */
1005 akapila@postgresql.o 5824 [ + - ]: 67 : if (am_leader_apply_worker())
1199 tgl@sss.pgh.pa.us 5825 : 67 : ApplyLauncherForgetWorkerStartTime(MyLogicalRepWorker->subid);
5826 : :
2951 peter_e@gmx.net 5827 : 67 : proc_exit(0);
5828 : : }
5829 : :
3393 5830 : 519 : MySubscriptionValid = true;
5831 : :
5832 [ - + ]: 519 : if (!MySubscription->enabled)
5833 : : {
3393 peter_e@gmx.net 5834 [ # # ]:UBC 0 : ereport(LOG,
5835 : : (errmsg("logical replication worker for subscription \"%s\" will not start because the subscription was disabled during startup",
5836 : : MySubscription->name)));
5837 : :
1212 akapila@postgresql.o 5838 : 0 : apply_worker_exit();
5839 : : }
5840 : :
5841 : : /*
5842 : : * Restart the worker if retain_dead_tuples was enabled during startup.
5843 : : *
5844 : : * At this point, the replication slot used for conflict detection might
5845 : : * not exist yet, or could be dropped soon if the launcher perceives
5846 : : * retain_dead_tuples as disabled. To avoid unnecessary tracking of
5847 : : * oldest_nonremovable_xid when the slot is absent or at risk of being
5848 : : * dropped, a restart is initiated.
5849 : : *
5850 : : * The oldest_nonremovable_xid should be initialized only when the
5851 : : * subscription's retention is active before launching the worker. See
5852 : : * logicalrep_worker_launch.
5853 : : */
286 akapila@postgresql.o 5854 [ + + ]:GNC 519 : if (am_leader_apply_worker() &&
5855 [ + + ]: 285 : MySubscription->retaindeadtuples &&
245 5856 [ + - ]: 15 : MySubscription->retentionactive &&
286 5857 [ - + ]: 15 : !TransactionIdIsValid(MyLogicalRepWorker->oldest_nonremovable_xid))
5858 : : {
286 akapila@postgresql.o 5859 [ # # ]:UNC 0 : ereport(LOG,
5860 : : errmsg("logical replication worker for subscription \"%s\" will restart because the option %s was enabled during startup",
5861 : : MySubscription->name, "retain_dead_tuples"));
5862 : :
5863 : 0 : apply_worker_exit();
5864 : : }
5865 : :
5866 : : /* Setup synchronous commit according to the user's wishes */
2951 peter_e@gmx.net 5867 :CBC 519 : SetConfigOption("synchronous_commit", MySubscription->synccommit,
5868 : : PGC_BACKEND, PGC_S_OVERRIDE);
5869 : :
5870 : : /* Change wal_receiver_timeout according to the user's wishes */
74 fujii@postgresql.org 5871 :GNC 519 : set_wal_receiver_timeout();
5872 : :
5873 : : /*
5874 : : * Keep us informed about subscription or role changes. Note that the
5875 : : * role's superuser privilege can be revoked.
5876 : : */
3393 peter_e@gmx.net 5877 :CBC 519 : CacheRegisterSyscacheCallback(SUBSCRIPTIONOID,
5878 : : subscription_change_cb,
5879 : : (Datum) 0);
5880 : : /* Changes to foreign servers may affect subscriptions using SERVER. */
60 jdavis@postgresql.or 5881 :GNC 519 : CacheRegisterSyscacheCallback(FOREIGNSERVEROID,
5882 : : subscription_change_cb,
5883 : : (Datum) 0);
5884 : : /* Changes to user mappings may affect subscriptions using SERVER. */
5885 : 519 : CacheRegisterSyscacheCallback(USERMAPPINGOID,
5886 : : subscription_change_cb,
5887 : : (Datum) 0);
5888 : :
5889 : : /*
5890 : : * Changes to FDW connection_function may affect subscriptions using
5891 : : * SERVER.
5892 : : */
5893 : 519 : CacheRegisterSyscacheCallback(FOREIGNDATAWRAPPEROID,
5894 : : subscription_change_cb,
5895 : : (Datum) 0);
5896 : :
931 akapila@postgresql.o 5897 :CBC 519 : CacheRegisterSyscacheCallback(AUTHOID,
5898 : : subscription_change_cb,
5899 : : (Datum) 0);
5900 : :
3330 peter_e@gmx.net 5901 [ + + ]: 519 : if (am_tablesync_worker())
3268 5902 [ + - ]: 212 : ereport(LOG,
5903 : : errmsg("logical replication table synchronization worker for subscription \"%s\", table \"%s\" has started",
5904 : : MySubscription->name,
5905 : : get_rel_name(MyLogicalRepWorker->relid)));
181 akapila@postgresql.o 5906 [ + + ]:GNC 307 : else if (am_sequencesync_worker())
5907 [ + - ]: 10 : ereport(LOG,
5908 : : errmsg("logical replication sequence synchronization worker for subscription \"%s\" has started",
5909 : : MySubscription->name));
5910 : : else
3268 peter_e@gmx.net 5911 [ + - ]:CBC 297 : ereport(LOG,
5912 : : errmsg("logical replication apply worker for subscription \"%s\" has started",
5913 : : MySubscription->name));
5914 : :
3393 5915 : 519 : CommitTransactionCommand();
5916 : :
5917 : : /*
5918 : : * Register a callback to reset the origin state before aborting any
5919 : : * pending transaction during shutdown (see ShutdownPostgres()). This will
5920 : : * avoid origin advancement for an incomplete transaction which could
5921 : : * otherwise lead to its loss as such a transaction won't be sent by the
5922 : : * server again.
5923 : : *
5924 : : * Note that even a LOG or DEBUG statement placed after setting the origin
5925 : : * state may process a shutdown signal before committing the current apply
5926 : : * operation. So, it is important to register such a callback here.
5927 : : *
5928 : : * Register this callback here to ensure that all types of logical
5929 : : * replication workers that set up origins and apply remote transactions
5930 : : * are protected.
5931 : : */
97 msawada@postgresql.o 5932 :GNC 519 : before_shmem_exit(on_exit_clear_xact_state, (Datum) 0);
1212 akapila@postgresql.o 5933 :CBC 519 : }
5934 : :
5935 : : /*
5936 : : * Callback on exit to clear transaction-level replication origin state.
5937 : : */
5938 : : static void
97 msawada@postgresql.o 5939 :GNC 519 : on_exit_clear_xact_state(int code, Datum arg)
5940 : : {
5941 : 519 : replorigin_xact_clear(true);
622 akapila@postgresql.o 5942 :CBC 519 : }
5943 : :
5944 : : /*
5945 : : * Common function to setup the leader apply, tablesync and sequencesync worker.
5946 : : */
5947 : : void
1006 5948 : 581 : SetupApplyOrSyncWorker(int worker_slot)
5949 : : {
5950 : : /* Attach to slot */
1212 5951 : 581 : logicalrep_worker_attach(worker_slot);
5952 : :
181 akapila@postgresql.o 5953 [ + + + + :GNC 581 : Assert(am_tablesync_worker() || am_sequencesync_worker() || am_leader_apply_worker());
- + ]
5954 : :
5955 : : /* Setup signal handling */
1212 akapila@postgresql.o 5956 :CBC 581 : pqsignal(SIGHUP, SignalHandlerForConfigReload);
5957 : 581 : BackgroundWorkerUnblockSignals();
5958 : :
5959 : : /*
5960 : : * We don't currently need any ResourceOwner in a walreceiver process, but
5961 : : * if we did, we could call CreateAuxProcessResourceOwner here.
5962 : : */
5963 : :
5964 : : /* Initialise stats to a sanish value */
5965 : 581 : MyLogicalRepWorker->last_send_time = MyLogicalRepWorker->last_recv_time =
5966 : 581 : MyLogicalRepWorker->reply_time = GetCurrentTimestamp();
5967 : :
5968 : : /* Load the libpq-specific functions */
5969 : 581 : load_file("libpqwalreceiver", false);
5970 : :
1006 5971 : 581 : InitializeLogRepWorker();
5972 : :
5973 : : /* Connect to the origin and start the replication. */
3393 peter_e@gmx.net 5974 [ + + ]: 507 : elog(DEBUG1, "connecting to publisher using connection string \"%s\"",
5975 : : MySubscription->conninfo);
5976 : :
5977 : : /*
5978 : : * Setup callback for syscache so that we know when something changes in
5979 : : * the subscription relation state.
5980 : : */
3330 5981 : 507 : CacheRegisterSyscacheCallback(SUBSCRIPTIONRELMAP,
5982 : : InvalidateSyncingRelStates,
5983 : : (Datum) 0);
1006 akapila@postgresql.o 5984 : 507 : }
5985 : :
5986 : : /* Logical Replication Apply worker entry point */
5987 : : void
5988 : 357 : ApplyWorkerMain(Datum main_arg)
5989 : : {
5990 : 357 : int worker_slot = DatumGetInt32(main_arg);
5991 : :
5992 : 357 : InitializingApplyWorker = true;
5993 : :
5994 : 357 : SetupApplyOrSyncWorker(worker_slot);
5995 : :
5996 : 285 : InitializingApplyWorker = false;
5997 : :
5998 : 285 : run_apply_worker();
5999 : :
1513 akapila@postgresql.o 6000 :UBC 0 : proc_exit(0);
6001 : : }
6002 : :
6003 : : /*
6004 : : * After error recovery, disable the subscription in a new transaction
6005 : : * and exit cleanly.
6006 : : */
6007 : : void
1513 akapila@postgresql.o 6008 :CBC 4 : DisableSubscriptionAndExit(void)
6009 : : {
6010 : : /*
6011 : : * Emit the error message, and recover from the error state to an idle
6012 : : * state
6013 : : */
6014 : 4 : HOLD_INTERRUPTS();
6015 : :
6016 : 4 : EmitErrorReport();
6017 : 4 : AbortOutOfAnyTransaction();
6018 : 4 : FlushErrorState();
6019 : :
6020 [ - + ]: 4 : RESUME_INTERRUPTS();
6021 : :
6022 : : /*
6023 : : * Report the worker failed during sequence synchronization, table
6024 : : * synchronization, or apply.
6025 : : */
74 akapila@postgresql.o 6026 :GNC 4 : pgstat_report_subscription_error(MyLogicalRepWorker->subid);
6027 : :
6028 : : /* Disable the subscription */
1513 akapila@postgresql.o 6029 :CBC 4 : StartTransactionCommand();
6030 : :
6031 : : /*
6032 : : * Updating pg_subscription might involve TOAST table access, so ensure we
6033 : : * have a valid snapshot.
6034 : : */
340 nathan@postgresql.or 6035 : 4 : PushActiveSnapshot(GetTransactionSnapshot());
6036 : :
1513 akapila@postgresql.o 6037 : 4 : DisableSubscription(MySubscription->oid);
340 nathan@postgresql.or 6038 : 4 : PopActiveSnapshot();
1513 akapila@postgresql.o 6039 : 4 : CommitTransactionCommand();
6040 : :
6041 : : /* Ensure we remove no-longer-useful entry for worker's start time */
1005 6042 [ + + ]: 4 : if (am_leader_apply_worker())
1199 tgl@sss.pgh.pa.us 6043 : 3 : ApplyLauncherForgetWorkerStartTime(MyLogicalRepWorker->subid);
6044 : :
6045 : : /* Notify the subscription has been disabled and exit */
1513 akapila@postgresql.o 6046 [ + - ]: 4 : ereport(LOG,
6047 : : errmsg("subscription \"%s\" has been disabled because of an error",
6048 : : MySubscription->name));
6049 : :
6050 : : /*
6051 : : * Skip the track_commit_timestamp check when disabling the worker due to
6052 : : * an error, as verifying commit timestamps is unnecessary in this
6053 : : * context.
6054 : : */
245 akapila@postgresql.o 6055 :GNC 4 : CheckSubDeadTupleRetention(false, true, WARNING,
6056 : 4 : MySubscription->retaindeadtuples,
6057 : 4 : MySubscription->retentionactive, false);
6058 : :
3393 peter_e@gmx.net 6059 :CBC 4 : proc_exit(0);
6060 : : }
6061 : :
6062 : : /*
6063 : : * Is current process a logical replication worker?
6064 : : */
6065 : : bool
3259 6066 : 2775 : IsLogicalWorker(void)
6067 : : {
6068 : 2775 : return MyLogicalRepWorker != NULL;
6069 : : }
6070 : :
6071 : : /*
6072 : : * Is current process a logical replication parallel apply worker?
6073 : : */
6074 : : bool
1212 akapila@postgresql.o 6075 : 2026 : IsLogicalParallelApplyWorker(void)
6076 : : {
6077 [ + + + - ]: 2026 : return IsLogicalWorker() && am_parallel_apply_worker();
6078 : : }
6079 : :
6080 : : /*
6081 : : * Start skipping changes of the transaction if the given LSN matches the
6082 : : * LSN specified by subscription's skiplsn.
6083 : : */
6084 : : static void
1505 6085 : 565 : maybe_start_skipping_changes(XLogRecPtr finish_lsn)
6086 : : {
6087 [ - + ]: 565 : Assert(!is_skipping_changes());
6088 [ - + ]: 565 : Assert(!in_remote_transaction);
6089 [ - + ]: 565 : Assert(!in_streamed_transaction);
6090 : :
6091 : : /*
6092 : : * Quick return if it's not requested to skip this transaction. This
6093 : : * function is called for every remote transaction and we assume that
6094 : : * skipping the transaction is not used often.
6095 : : */
180 alvherre@kurilemu.de 6096 [ + + - + :GNC 565 : if (likely(!XLogRecPtrIsValid(MySubscription->skiplsn) ||
+ + ]
6097 : : MySubscription->skiplsn != finish_lsn))
1505 akapila@postgresql.o 6098 :CBC 562 : return;
6099 : :
6100 : : /* Start skipping all changes of this transaction */
6101 : 3 : skip_xact_finish_lsn = finish_lsn;
6102 : :
6103 [ + - ]: 3 : ereport(LOG,
6104 : : errmsg("logical replication starts skipping transaction at LSN %X/%08X",
6105 : : LSN_FORMAT_ARGS(skip_xact_finish_lsn)));
6106 : : }
6107 : :
6108 : : /*
6109 : : * Stop skipping changes by resetting skip_xact_finish_lsn if enabled.
6110 : : */
6111 : : static void
6112 : 30 : stop_skipping_changes(void)
6113 : : {
6114 [ + + ]: 30 : if (!is_skipping_changes())
6115 : 27 : return;
6116 : :
6117 [ + - ]: 3 : ereport(LOG,
6118 : : errmsg("logical replication completed skipping transaction at LSN %X/%08X",
6119 : : LSN_FORMAT_ARGS(skip_xact_finish_lsn)));
6120 : :
6121 : : /* Stop skipping changes */
6122 : 3 : skip_xact_finish_lsn = InvalidXLogRecPtr;
6123 : : }
6124 : :
6125 : : /*
6126 : : * Clear subskiplsn of pg_subscription catalog.
6127 : : *
6128 : : * finish_lsn is the transaction's finish LSN that is used to check if the
6129 : : * subskiplsn matches it. If not matched, we raise a warning when clearing the
6130 : : * subskiplsn in order to inform users for cases e.g., where the user mistakenly
6131 : : * specified the wrong subskiplsn.
6132 : : */
6133 : : static void
6134 : 556 : clear_subscription_skip_lsn(XLogRecPtr finish_lsn)
6135 : : {
6136 : : Relation rel;
6137 : : Form_pg_subscription subform;
6138 : : HeapTuple tup;
6139 : 556 : XLogRecPtr myskiplsn = MySubscription->skiplsn;
6140 : 556 : bool started_tx = false;
6141 : :
180 alvherre@kurilemu.de 6142 [ + + - + ]:GNC 556 : if (likely(!XLogRecPtrIsValid(myskiplsn)) || am_parallel_apply_worker())
1505 akapila@postgresql.o 6143 :CBC 553 : return;
6144 : :
6145 [ + + ]: 3 : if (!IsTransactionState())
6146 : : {
6147 : 1 : StartTransactionCommand();
6148 : 1 : started_tx = true;
6149 : : }
6150 : :
6151 : : /*
6152 : : * Updating pg_subscription might involve TOAST table access, so ensure we
6153 : : * have a valid snapshot.
6154 : : */
340 nathan@postgresql.or 6155 : 3 : PushActiveSnapshot(GetTransactionSnapshot());
6156 : :
6157 : : /*
6158 : : * Protect subskiplsn of pg_subscription from being concurrently updated
6159 : : * while clearing it.
6160 : : */
1505 akapila@postgresql.o 6161 : 3 : LockSharedObject(SubscriptionRelationId, MySubscription->oid, 0,
6162 : : AccessShareLock);
6163 : :
6164 : 3 : rel = table_open(SubscriptionRelationId, RowExclusiveLock);
6165 : :
6166 : : /* Fetch the existing tuple. */
6167 : 3 : tup = SearchSysCacheCopy1(SUBSCRIPTIONOID,
6168 : : ObjectIdGetDatum(MySubscription->oid));
6169 : :
6170 [ - + ]: 3 : if (!HeapTupleIsValid(tup))
1505 akapila@postgresql.o 6171 [ # # ]:UBC 0 : elog(ERROR, "subscription \"%s\" does not exist", MySubscription->name);
6172 : :
1505 akapila@postgresql.o 6173 :CBC 3 : subform = (Form_pg_subscription) GETSTRUCT(tup);
6174 : :
6175 : : /*
6176 : : * Clear the subskiplsn. If the user has already changed subskiplsn before
6177 : : * clearing it we don't update the catalog and the replication origin
6178 : : * state won't get advanced. So in the worst case, if the server crashes
6179 : : * before sending an acknowledgment of the flush position the transaction
6180 : : * will be sent again and the user needs to set subskiplsn again. We can
6181 : : * reduce the possibility by logging a replication origin WAL record to
6182 : : * advance the origin LSN instead but there is no way to advance the
6183 : : * origin timestamp and it doesn't seem to be worth doing anything about
6184 : : * it since it's a very rare case.
6185 : : */
6186 [ + - ]: 3 : if (subform->subskiplsn == myskiplsn)
6187 : : {
6188 : : bool nulls[Natts_pg_subscription];
6189 : : bool replaces[Natts_pg_subscription];
6190 : : Datum values[Natts_pg_subscription];
6191 : :
6192 : 3 : memset(values, 0, sizeof(values));
6193 : 3 : memset(nulls, false, sizeof(nulls));
6194 : 3 : memset(replaces, false, sizeof(replaces));
6195 : :
6196 : : /* reset subskiplsn */
6197 : 3 : values[Anum_pg_subscription_subskiplsn - 1] = LSNGetDatum(InvalidXLogRecPtr);
6198 : 3 : replaces[Anum_pg_subscription_subskiplsn - 1] = true;
6199 : :
6200 : 3 : tup = heap_modify_tuple(tup, RelationGetDescr(rel), values, nulls,
6201 : : replaces);
6202 : 3 : CatalogTupleUpdate(rel, &tup->t_self, tup);
6203 : :
6204 [ - + ]: 3 : if (myskiplsn != finish_lsn)
1505 akapila@postgresql.o 6205 [ # # ]:UBC 0 : ereport(WARNING,
6206 : : errmsg("skip-LSN of subscription \"%s\" cleared", MySubscription->name),
6207 : : errdetail("Remote transaction's finish WAL location (LSN) %X/%08X did not match skip-LSN %X/%08X.",
6208 : : LSN_FORMAT_ARGS(finish_lsn),
6209 : : LSN_FORMAT_ARGS(myskiplsn)));
6210 : : }
6211 : :
1505 akapila@postgresql.o 6212 :CBC 3 : heap_freetuple(tup);
6213 : 3 : table_close(rel, NoLock);
6214 : :
340 nathan@postgresql.or 6215 : 3 : PopActiveSnapshot();
6216 : :
1505 akapila@postgresql.o 6217 [ + + ]: 3 : if (started_tx)
6218 : 1 : CommitTransactionCommand();
6219 : : }
6220 : :
6221 : : /* Error callback to give more context info about the change being applied */
6222 : : void
1712 6223 : 2653 : apply_error_callback(void *arg)
6224 : : {
6225 : 2653 : ApplyErrorCallbackArg *errarg = &apply_error_callback_arg;
6226 : :
6227 [ + + ]: 2653 : if (apply_error_callback_arg.command == 0)
6228 : 2241 : return;
6229 : :
1519 6230 [ - + ]: 412 : Assert(errarg->origin_name);
6231 : :
1520 6232 [ + + ]: 412 : if (errarg->rel == NULL)
6233 : : {
6234 [ - + ]: 333 : if (!TransactionIdIsValid(errarg->remote_xid))
1319 peter@eisentraut.org 6235 :UBC 0 : errcontext("processing remote data for replication origin \"%s\" during message type \"%s\"",
6236 : : errarg->origin_name,
6237 : : logicalrep_message_type(errarg->command));
180 alvherre@kurilemu.de 6238 [ + + ]:GNC 333 : else if (!XLogRecPtrIsValid(errarg->finish_lsn))
1319 peter@eisentraut.org 6239 :CBC 260 : errcontext("processing remote data for replication origin \"%s\" during message type \"%s\" in transaction %u",
6240 : : errarg->origin_name,
6241 : : logicalrep_message_type(errarg->command),
6242 : : errarg->remote_xid);
6243 : : else
302 alvherre@kurilemu.de 6244 :GNC 146 : errcontext("processing remote data for replication origin \"%s\" during message type \"%s\" in transaction %u, finished at %X/%08X",
6245 : : errarg->origin_name,
6246 : : logicalrep_message_type(errarg->command),
6247 : : errarg->remote_xid,
1519 akapila@postgresql.o 6248 :CBC 73 : LSN_FORMAT_ARGS(errarg->finish_lsn));
6249 : : }
6250 : : else
6251 : : {
1212 6252 [ + - ]: 79 : if (errarg->remote_attnum < 0)
6253 : : {
180 alvherre@kurilemu.de 6254 [ + + ]:GNC 79 : if (!XLogRecPtrIsValid(errarg->finish_lsn))
1212 akapila@postgresql.o 6255 :GBC 4 : errcontext("processing remote data for replication origin \"%s\" during message type \"%s\" for replication target relation \"%s.%s\" in transaction %u",
6256 : : errarg->origin_name,
6257 : : logicalrep_message_type(errarg->command),
6258 : 2 : errarg->rel->remoterel.nspname,
6259 : 2 : errarg->rel->remoterel.relname,
6260 : : errarg->remote_xid);
6261 : : else
302 alvherre@kurilemu.de 6262 :GNC 154 : errcontext("processing remote data for replication origin \"%s\" during message type \"%s\" for replication target relation \"%s.%s\" in transaction %u, finished at %X/%08X",
6263 : : errarg->origin_name,
6264 : : logicalrep_message_type(errarg->command),
1212 akapila@postgresql.o 6265 :CBC 77 : errarg->rel->remoterel.nspname,
6266 : 77 : errarg->rel->remoterel.relname,
6267 : : errarg->remote_xid,
6268 : 77 : LSN_FORMAT_ARGS(errarg->finish_lsn));
6269 : : }
6270 : : else
6271 : : {
180 alvherre@kurilemu.de 6272 [ # # ]:UNC 0 : if (!XLogRecPtrIsValid(errarg->finish_lsn))
1212 akapila@postgresql.o 6273 :UBC 0 : errcontext("processing remote data for replication origin \"%s\" during message type \"%s\" for replication target relation \"%s.%s\" column \"%s\" in transaction %u",
6274 : : errarg->origin_name,
6275 : : logicalrep_message_type(errarg->command),
6276 : 0 : errarg->rel->remoterel.nspname,
6277 : 0 : errarg->rel->remoterel.relname,
6278 : 0 : errarg->rel->remoterel.attnames[errarg->remote_attnum],
6279 : : errarg->remote_xid);
6280 : : else
302 alvherre@kurilemu.de 6281 :UNC 0 : errcontext("processing remote data for replication origin \"%s\" during message type \"%s\" for replication target relation \"%s.%s\" column \"%s\" in transaction %u, finished at %X/%08X",
6282 : : errarg->origin_name,
6283 : : logicalrep_message_type(errarg->command),
1212 akapila@postgresql.o 6284 :UBC 0 : errarg->rel->remoterel.nspname,
6285 : 0 : errarg->rel->remoterel.relname,
6286 : 0 : errarg->rel->remoterel.attnames[errarg->remote_attnum],
6287 : : errarg->remote_xid,
6288 : 0 : LSN_FORMAT_ARGS(errarg->finish_lsn));
6289 : : }
6290 : : }
6291 : : }
6292 : :
6293 : : /* Set transaction information of apply error callback */
6294 : : static inline void
1519 akapila@postgresql.o 6295 :CBC 2973 : set_apply_error_context_xact(TransactionId xid, XLogRecPtr lsn)
6296 : : {
1712 6297 : 2973 : apply_error_callback_arg.remote_xid = xid;
1519 6298 : 2973 : apply_error_callback_arg.finish_lsn = lsn;
1712 6299 : 2973 : }
6300 : :
6301 : : /* Reset all information of apply error callback */
6302 : : static inline void
6303 : 1452 : reset_apply_error_context_info(void)
6304 : : {
6305 : 1452 : apply_error_callback_arg.command = 0;
6306 : 1452 : apply_error_callback_arg.rel = NULL;
6307 : 1452 : apply_error_callback_arg.remote_attnum = -1;
1519 6308 : 1452 : set_apply_error_context_xact(InvalidTransactionId, InvalidXLogRecPtr);
1712 6309 : 1452 : }
6310 : :
6311 : : /*
6312 : : * Request wakeup of the workers for the given subscription OID
6313 : : * at commit of the current transaction.
6314 : : *
6315 : : * This is used to ensure that the workers process assorted changes
6316 : : * as soon as possible.
6317 : : */
6318 : : void
1215 tgl@sss.pgh.pa.us 6319 : 280 : LogicalRepWorkersWakeupAtCommit(Oid subid)
6320 : : {
6321 : : MemoryContext oldcxt;
6322 : :
6323 : 280 : oldcxt = MemoryContextSwitchTo(TopTransactionContext);
6324 : 280 : on_commit_wakeup_workers_subids =
6325 : 280 : list_append_unique_oid(on_commit_wakeup_workers_subids, subid);
6326 : 280 : MemoryContextSwitchTo(oldcxt);
6327 : 280 : }
6328 : :
6329 : : /*
6330 : : * Wake up the workers of any subscriptions that were changed in this xact.
6331 : : */
6332 : : void
6333 : 423286 : AtEOXact_LogicalRepWorkers(bool isCommit)
6334 : : {
6335 [ + + + + ]: 423286 : if (isCommit && on_commit_wakeup_workers_subids != NIL)
6336 : : {
6337 : : ListCell *lc;
6338 : :
6339 : 274 : LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
6340 [ + - + + : 548 : foreach(lc, on_commit_wakeup_workers_subids)
+ + ]
6341 : : {
6342 : 274 : Oid subid = lfirst_oid(lc);
6343 : : List *workers;
6344 : : ListCell *lc2;
6345 : :
650 akapila@postgresql.o 6346 : 274 : workers = logicalrep_workers_find(subid, true, false);
1215 tgl@sss.pgh.pa.us 6347 [ + + + + : 351 : foreach(lc2, workers)
+ + ]
6348 : : {
6349 : 77 : LogicalRepWorker *worker = (LogicalRepWorker *) lfirst(lc2);
6350 : :
6351 : 77 : logicalrep_worker_wakeup_ptr(worker);
6352 : : }
6353 : : }
6354 : 274 : LWLockRelease(LogicalRepWorkerLock);
6355 : : }
6356 : :
6357 : : /* The List storage will be reclaimed automatically in xact cleanup. */
6358 : 423286 : on_commit_wakeup_workers_subids = NIL;
6359 : 423286 : }
6360 : :
6361 : : /*
6362 : : * Allocate the origin name in long-lived context for error context message.
6363 : : */
6364 : : void
1212 akapila@postgresql.o 6365 : 459 : set_apply_error_context_origin(char *originname)
6366 : : {
6367 : 459 : apply_error_callback_arg.origin_name = MemoryContextStrdup(ApplyContext,
6368 : : originname);
6369 : 459 : }
6370 : :
6371 : : /*
6372 : : * Return the action to be taken for the given transaction. See
6373 : : * TransApplyAction for information on each of the actions.
6374 : : *
6375 : : * *winfo is assigned to the destination parallel worker info when the leader
6376 : : * apply worker has to pass all the transaction's changes to the parallel
6377 : : * apply worker.
6378 : : */
6379 : : static TransApplyAction
6380 : 346812 : get_transaction_apply_action(TransactionId xid, ParallelApplyWorkerInfo **winfo)
6381 : : {
6382 : 346812 : *winfo = NULL;
6383 : :
6384 [ + + ]: 346812 : if (am_parallel_apply_worker())
6385 : : {
6386 : 69016 : return TRANS_PARALLEL_APPLY;
6387 : : }
6388 : :
6389 : : /*
6390 : : * If we are processing this transaction using a parallel apply worker
6391 : : * then either we send the changes to the parallel worker or if the worker
6392 : : * is busy then serialize the changes to the file which will later be
6393 : : * processed by the parallel worker.
6394 : : */
6395 : 277796 : *winfo = pa_find_worker(xid);
6396 : :
1204 6397 [ + + + + ]: 277796 : if (*winfo && (*winfo)->serialize_changes)
6398 : : {
6399 : 5037 : return TRANS_LEADER_PARTIAL_SERIALIZE;
6400 : : }
6401 [ + + ]: 272759 : else if (*winfo)
6402 : : {
6403 : 68906 : return TRANS_LEADER_SEND_TO_PARALLEL;
6404 : : }
6405 : :
6406 : : /*
6407 : : * If there is no parallel worker involved to process this transaction
6408 : : * then we either directly apply the change or serialize it to a file
6409 : : * which will later be applied when the transaction finish message is
6410 : : * processed.
6411 : : */
6412 [ + + ]: 203853 : else if (in_streamed_transaction)
6413 : : {
6414 : 103197 : return TRANS_LEADER_SERIALIZE;
6415 : : }
6416 : : else
6417 : : {
6418 : 100656 : return TRANS_LEADER_APPLY;
6419 : : }
6420 : : }
|