Debian Codegraph @

src/backend/replication/logical/reorderbuffer.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * reorderbuffer.c
4 * PostgreSQL logical replay/reorder buffer management
5 *
6 *
7 * Copyright (c) 2012-2025, PostgreSQL Global Development Group
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/replication/logical/reorderbuffer.c
12 *
13 * NOTES
14 * This module gets handed individual pieces of transactions in the order
15 * they are written to the WAL and is responsible to reassemble them into
16 * toplevel transaction sized pieces. When a transaction is completely
17 * reassembled - signaled by reading the transaction commit record - it
18 * will then call the output plugin (cf. ReorderBufferCommit()) with the
19 * individual changes. The output plugins rely on snapshots built by
20 * snapbuild.c which hands them to us.
21 *
22 * Transactions and subtransactions/savepoints in postgres are not
23 * immediately linked to each other from outside the performing
24 * backend. Only at commit/abort (or special xact_assignment records) they
25 * are linked together. Which means that we will have to splice together a
26 * toplevel transaction from its subtransactions. To do that efficiently we
27 * build a binary heap indexed by the smallest current lsn of the individual
28 * subtransactions' changestreams. As the individual streams are inherently
29 * ordered by LSN - since that is where we build them from - the transaction
30 * can easily be reassembled by always using the subtransaction with the
31 * smallest current LSN from the heap.
32 *
33 * In order to cope with large transactions - which can be several times as
34 * big as the available memory - this module supports spooling the contents
35 * of large transactions to disk. When the transaction is replayed the
36 * contents of individual (sub-)transactions will be read from disk in
37 * chunks.
38 *
39 * This module also has to deal with reassembling toast records from the
40 * individual chunks stored in WAL. When a new (or initial) version of a
41 * tuple is stored in WAL it will always be preceded by the toast chunks
42 * emitted for the columns stored out of line. Within a single toplevel
43 * transaction there will be no other data carrying records between a row's
44 * toast chunks and the row data itself. See ReorderBufferToast* for
45 * details.
46 *
47 * ReorderBuffer uses two special memory context types - SlabContext for
48 * allocations of fixed-length structures (changes and transactions), and
49 * GenerationContext for the variable-length transaction data (allocated
50 * and freed in groups with similar lifespans).
51 *
52 * To limit the amount of memory used by decoded changes, we track memory
53 * used at the reorder buffer level (i.e. total amount of memory), and for
54 * each transaction. When the total amount of used memory exceeds the
55 * limit, the transaction consuming the most memory is then serialized to
56 * disk.
57 *
58 * Only decoded changes are evicted from memory (spilled to disk), not the
59 * transaction records. The number of toplevel transactions is limited,
60 * but a transaction with many subtransactions may still consume significant
61 * amounts of memory. However, the transaction records are fairly small and
62 * are not included in the memory limit.
63 *
64 * The current eviction algorithm is very simple - the transaction is
65 * picked merely by size, while it might be useful to also consider age
66 * (LSN) of the changes for example. With the new Generational memory
67 * allocator, evicting the oldest changes would make it more likely the
68 * memory gets actually freed.
69 *
70 * We use a max-heap with transaction size as the key to efficiently find
71 * the largest transaction. We update the max-heap whenever the memory
72 * counter is updated; however transactions with size 0 are not stored in
73 * the heap, because they have no changes to evict.
74 *
75 * We still rely on max_changes_in_memory when loading serialized changes
76 * back into memory. At that point we can't use the memory limit directly
77 * as we load the subxacts independently. One option to deal with this
78 * would be to count the subxacts, and allow each to allocate 1/N of the
79 * memory limit. That however does not seem very appealing, because with
80 * many subtransactions it may easily cause thrashing (short cycles of
81 * deserializing and applying very few changes). We probably should give
82 * a bit more memory to the oldest subtransactions, because it's likely
83 * they are the source for the next sequence of changes.
84 *
85 * -------------------------------------------------------------------------
86 */
87 #include "postgres.h"
88
89 #include <unistd.h>
90 #include <sys/stat.h>
91
92 #include "access/detoast.h"
93 #include "access/heapam.h"
94 #include "access/rewriteheap.h"
95 #include "access/transam.h"
96 #include "access/xact.h"
97 #include "access/xlog_internal.h"
98 #include "catalog/catalog.h"
99 #include "common/int.h"
100 #include "lib/binaryheap.h"
101 #include "miscadmin.h"
102 #include "pgstat.h"
103 #include "replication/logical.h"
104 #include "replication/reorderbuffer.h"
105 #include "replication/slot.h"
106 #include "replication/snapbuild.h" /* just for SnapBuildSnapDecRefcount */
107 #include "storage/bufmgr.h"
108 #include "storage/fd.h"
109 #include "storage/procarray.h"
110 #include "storage/sinval.h"
111 #include "utils/builtins.h"
112 #include "utils/inval.h"
113 #include "utils/memutils.h"
114 #include "utils/rel.h"
115 #include "utils/relfilenumbermap.h"
116
117 /*
118 * Each transaction has an 8MB limit for invalidation messages distributed from
119 * other transactions. This limit is set considering scenarios with many
120 * concurrent logical decoding operations. When the distributed invalidation
121 * messages reach this threshold, the transaction is marked as
122 * RBTXN_DISTR_INVAL_OVERFLOWED to invalidate the complete cache as we have lost
123 * some inval messages and hence don't know what needs to be invalidated.
124 */
125 #define MAX_DISTR_INVAL_MSG_PER_TXN \
126 ((8 * 1024 * 1024) / sizeof(SharedInvalidationMessage))
127
128 /* entry for a hash table we use to map from xid to our transaction state */
129 typedef struct ReorderBufferTXNByIdEnt
130 {
131 TransactionId xid;
132 ReorderBufferTXN *txn;
133 } ReorderBufferTXNByIdEnt;
134
135 /* data structures for (relfilelocator, ctid) => (cmin, cmax) mapping */
136 typedef struct ReorderBufferTupleCidKey
137 {
138 RelFileLocator rlocator;
139 ItemPointerData tid;
140 } ReorderBufferTupleCidKey;
141
142 typedef struct ReorderBufferTupleCidEnt
143 {
144 ReorderBufferTupleCidKey key;
145 CommandId cmin;
146 CommandId cmax;
147 CommandId combocid; /* just for debugging */
148 } ReorderBufferTupleCidEnt;
149
150 /* Virtual file descriptor with file offset tracking */
151 typedef struct TXNEntryFile
152 {
153 File vfd; /* -1 when the file is closed */
154 off_t curOffset; /* offset for next write or read. Reset to 0
155 * when vfd is opened. */
156 } TXNEntryFile;
157
158 /* k-way in-order change iteration support structures */
159 typedef struct ReorderBufferIterTXNEntry
160 {
161 XLogRecPtr lsn;
162 ReorderBufferChange *change;
163 ReorderBufferTXN *txn;
164 TXNEntryFile file;
165 XLogSegNo segno;
166 } ReorderBufferIterTXNEntry;
167
168 typedef struct ReorderBufferIterTXNState
169 {
170 binaryheap *heap;
171 Size nr_txns;
172 dlist_head old_change;
173 ReorderBufferIterTXNEntry entries[FLEXIBLE_ARRAY_MEMBER];
174 } ReorderBufferIterTXNState;
175
176 /* toast datastructures */
177 typedef struct ReorderBufferToastEnt
178 {
179 Oid chunk_id; /* toast_table.chunk_id */
180 int32 last_chunk_seq; /* toast_table.chunk_seq of the last chunk we
181 * have seen */
182 Size num_chunks; /* number of chunks we've already seen */
183 Size size; /* combined size of chunks seen */
184 dlist_head chunks; /* linked list of chunks */
185 struct varlena *reconstructed; /* reconstructed varlena now pointed to in
186 * main tup */
187 } ReorderBufferToastEnt;
188
189 /* Disk serialization support datastructures */
190 typedef struct ReorderBufferDiskChange
191 {
192 Size size;
193 ReorderBufferChange change;
194 /* data follows */
195 } ReorderBufferDiskChange;
196
197 #define IsSpecInsert(action) \
198 ( \
199 ((action) == REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT) \
200 )
201 #define IsSpecConfirmOrAbort(action) \
202 ( \
203 (((action) == REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM) || \
204 ((action) == REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT)) \
205 )
206 #define IsInsertOrUpdate(action) \
207 ( \
208 (((action) == REORDER_BUFFER_CHANGE_INSERT) || \
209 ((action) == REORDER_BUFFER_CHANGE_UPDATE) || \
210 ((action) == REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT)) \
211 )
212
213 /*
214 * Maximum number of changes kept in memory, per transaction. After that,
215 * changes are spooled to disk.
216 *
217 * The current value should be sufficient to decode the entire transaction
218 * without hitting disk in OLTP workloads, while starting to spool to disk in
219 * other workloads reasonably fast.
220 *
221 * At some point in the future it probably makes sense to have a more elaborate
222 * resource management here, but it's not entirely clear what that would look
223 * like.
224 */
225 int logical_decoding_work_mem;
226 static const Size max_changes_in_memory = 4096; /* XXX for restore only */
227
228 /* GUC variable */
229 int debug_logical_replication_streaming = DEBUG_LOGICAL_REP_STREAMING_BUFFERED;
230
231 /* ---------------------------------------
232 * primary reorderbuffer support routines
233 * ---------------------------------------
234 */
235 static ReorderBufferTXN *ReorderBufferAllocTXN(ReorderBuffer *rb);
236 static void ReorderBufferFreeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn);
237 static ReorderBufferTXN *ReorderBufferTXNByXid(ReorderBuffer *rb,
238 TransactionId xid, bool create, bool *is_new,
239 XLogRecPtr lsn, bool create_as_top);
240 static void ReorderBufferTransferSnapToParent(ReorderBufferTXN *txn,
241 ReorderBufferTXN *subtxn);
242
243 static void AssertTXNLsnOrder(ReorderBuffer *rb);
244
245 /* ---------------------------------------
246 * support functions for lsn-order iterating over the ->changes of a
247 * transaction and its subtransactions
248 *
249 * used for iteration over the k-way heap merge of a transaction and its
250 * subtransactions
251 * ---------------------------------------
252 */
253 static void ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn,
254 ReorderBufferIterTXNState *volatile *iter_state);
255 static ReorderBufferChange *ReorderBufferIterTXNNext(ReorderBuffer *rb, ReorderBufferIterTXNState *state);
256 static void ReorderBufferIterTXNFinish(ReorderBuffer *rb,
257 ReorderBufferIterTXNState *state);
258 static void ReorderBufferExecuteInvalidations(uint32 nmsgs, SharedInvalidationMessage *msgs);
259
260 /*
261 * ---------------------------------------
262 * Disk serialization support functions
263 * ---------------------------------------
264 */
265 static void ReorderBufferCheckMemoryLimit(ReorderBuffer *rb);
266 static void ReorderBufferSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn);
267 static void ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
268 int fd, ReorderBufferChange *change);
269 static Size ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
270 TXNEntryFile *file, XLogSegNo *segno);
271 static void ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
272 char *data);
273 static void ReorderBufferRestoreCleanup(ReorderBuffer *rb, ReorderBufferTXN *txn);
274 static void ReorderBufferTruncateTXN(ReorderBuffer *rb, ReorderBufferTXN *txn,
275 bool txn_prepared);
276 static void ReorderBufferMaybeMarkTXNStreamed(ReorderBuffer *rb, ReorderBufferTXN *txn);
277 static bool ReorderBufferCheckAndTruncateAbortedTXN(ReorderBuffer *rb, ReorderBufferTXN *txn);
278 static void ReorderBufferCleanupSerializedTXNs(const char *slotname);
279 static void ReorderBufferSerializedPath(char *path, ReplicationSlot *slot,
280 TransactionId xid, XLogSegNo segno);
281 static int ReorderBufferTXNSizeCompare(const pairingheap_node *a, const pairingheap_node *b, void *arg);
282
283 static void ReorderBufferFreeSnap(ReorderBuffer *rb, Snapshot snap);
284 static Snapshot ReorderBufferCopySnap(ReorderBuffer *rb, Snapshot orig_snap,
285 ReorderBufferTXN *txn, CommandId cid);
286
287 /*
288 * ---------------------------------------
289 * Streaming support functions
290 * ---------------------------------------
291 */
292 static inline bool ReorderBufferCanStream(ReorderBuffer *rb);
293 static inline bool ReorderBufferCanStartStreaming(ReorderBuffer *rb);
294 static void ReorderBufferStreamTXN(ReorderBuffer *rb, ReorderBufferTXN *txn);
295 static void ReorderBufferStreamCommit(ReorderBuffer *rb, ReorderBufferTXN *txn);
296
297 /* ---------------------------------------
298 * toast reassembly support
299 * ---------------------------------------
300 */
301 static void ReorderBufferToastInitHash(ReorderBuffer *rb, ReorderBufferTXN *txn);
302 static void ReorderBufferToastReset(ReorderBuffer *rb, ReorderBufferTXN *txn);
303 static void ReorderBufferToastReplace(ReorderBuffer *rb, ReorderBufferTXN *txn,
304 Relation relation, ReorderBufferChange *change);
305 static void ReorderBufferToastAppendChunk(ReorderBuffer *rb, ReorderBufferTXN *txn,
306 Relation relation, ReorderBufferChange *change);
307
308 /*
309 * ---------------------------------------
310 * memory accounting
311 * ---------------------------------------
312 */
313 static Size ReorderBufferChangeSize(ReorderBufferChange *change);
314 static void ReorderBufferChangeMemoryUpdate(ReorderBuffer *rb,
315 ReorderBufferChange *change,
316 ReorderBufferTXN *txn,
317 bool addition, Size sz);
318
319 /*
320 * Allocate a new ReorderBuffer and clean out any old serialized state from
321 * prior ReorderBuffer instances for the same slot.
322 */
323 ReorderBuffer *
324 ReorderBufferAllocate(void)
325 {
326 ReorderBuffer *buffer;
327 HASHCTL hash_ctl;
328 MemoryContext new_ctx;
329
330 Assert(MyReplicationSlot != NULL);
331
332 /* allocate memory in own context, to have better accountability */
333 new_ctx = AllocSetContextCreate(CurrentMemoryContext,
334 "ReorderBuffer",
335 ALLOCSET_DEFAULT_SIZES);
336
337 buffer =
338 (ReorderBuffer *) MemoryContextAlloc(new_ctx, sizeof(ReorderBuffer));
339
340 memset(&hash_ctl, 0, sizeof(hash_ctl));
341
342 buffer->context = new_ctx;
343
344 buffer->change_context = SlabContextCreate(new_ctx,
345 "Change",
346 SLAB_DEFAULT_BLOCK_SIZE,
347 sizeof(ReorderBufferChange));
348
349 buffer->txn_context = SlabContextCreate(new_ctx,
350 "TXN",
351 SLAB_DEFAULT_BLOCK_SIZE,
352 sizeof(ReorderBufferTXN));
353
354 /*
355 * To minimize memory fragmentation caused by long-running transactions
356 * with changes spanning multiple memory blocks, we use a single
357 * fixed-size memory block for decoded tuple storage. The performance
358 * testing showed that the default memory block size maintains logical
359 * decoding performance without causing fragmentation due to concurrent
360 * transactions. One might think that we can use the max size as
361 * SLAB_LARGE_BLOCK_SIZE but the test also showed it doesn't help resolve
362 * the memory fragmentation.
363 */
364 buffer->tup_context = GenerationContextCreate(new_ctx,
365 "Tuples",
366 SLAB_DEFAULT_BLOCK_SIZE,
367 SLAB_DEFAULT_BLOCK_SIZE,
368 SLAB_DEFAULT_BLOCK_SIZE);
369
370 hash_ctl.keysize = sizeof(TransactionId);
371 hash_ctl.entrysize = sizeof(ReorderBufferTXNByIdEnt);
372 hash_ctl.hcxt = buffer->context;
373
374 buffer->by_txn = hash_create("ReorderBufferByXid", 1000, &hash_ctl,
375 HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
376
377 buffer->by_txn_last_xid = InvalidTransactionId;
378 buffer->by_txn_last_txn = NULL;
379
380 buffer->outbuf = NULL;
381 buffer->outbufsize = 0;
382 buffer->size = 0;
383
384 /* txn_heap is ordered by transaction size */
385 buffer->txn_heap = pairingheap_allocate(ReorderBufferTXNSizeCompare, NULL);
386
387 buffer->spillTxns = 0;
388 buffer->spillCount = 0;
389 buffer->spillBytes = 0;
390 buffer->streamTxns = 0;
391 buffer->streamCount = 0;
392 buffer->streamBytes = 0;
393 buffer->totalTxns = 0;
394 buffer->totalBytes = 0;
395
396 buffer->current_restart_decoding_lsn = InvalidXLogRecPtr;
397
398 dlist_init(&buffer->toplevel_by_lsn);
399 dlist_init(&buffer->txns_by_base_snapshot_lsn);
400 dclist_init(&buffer->catchange_txns);
401
402 /*
403 * Ensure there's no stale data from prior uses of this slot, in case some
404 * prior exit avoided calling ReorderBufferFree. Failure to do this can
405 * produce duplicated txns, and it's very cheap if there's nothing there.
406 */
407 ReorderBufferCleanupSerializedTXNs(NameStr(MyReplicationSlot->data.name));
408
409 return buffer;
410 }
411
412 /*
413 * Free a ReorderBuffer
414 */
415 void
416 ReorderBufferFree(ReorderBuffer *rb)
417 {
418 MemoryContext context = rb->context;
419
420 /*
421 * We free separately allocated data by entirely scrapping reorderbuffer's
422 * memory context.
423 */
424 MemoryContextDelete(context);
425
426 /* Free disk space used by unconsumed reorder buffers */
427 ReorderBufferCleanupSerializedTXNs(NameStr(MyReplicationSlot->data.name));
428 }
429
430 /*
431 * Allocate a new ReorderBufferTXN.
432 */
433 static ReorderBufferTXN *
434 ReorderBufferAllocTXN(ReorderBuffer *rb)
435 {
436 ReorderBufferTXN *txn;
437
438 txn = (ReorderBufferTXN *)
439 MemoryContextAlloc(rb->txn_context, sizeof(ReorderBufferTXN));
440
441 memset(txn, 0, sizeof(ReorderBufferTXN));
442
443 dlist_init(&txn->changes);
444 dlist_init(&txn->tuplecids);
445 dlist_init(&txn->subtxns);
446
447 /* InvalidCommandId is not zero, so set it explicitly */
448 txn->command_id = InvalidCommandId;
449 txn->output_plugin_private = NULL;
450
451 return txn;
452 }
453
454 /*
455 * Free a ReorderBufferTXN.
456 */
457 static void
458 ReorderBufferFreeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
459 {
460 /* clean the lookup cache if we were cached (quite likely) */
461 if (rb->by_txn_last_xid == txn->xid)
462 {
463 rb->by_txn_last_xid = InvalidTransactionId;
464 rb->by_txn_last_txn = NULL;
465 }
466
467 /* free data that's contained */
468
469 if (txn->gid != NULL)
470 {
471 pfree(txn->gid);
472 txn->gid = NULL;
473 }
474
475 if (txn->tuplecid_hash != NULL)
476 {
477 hash_destroy(txn->tuplecid_hash);
478 txn->tuplecid_hash = NULL;
479 }
480
481 if (txn->invalidations)
482 {
483 pfree(txn->invalidations);
484 txn->invalidations = NULL;
485 }
486
487 if (txn->invalidations_distributed)
488 {
489 pfree(txn->invalidations_distributed);
490 txn->invalidations_distributed = NULL;
491 }
492
493 /* Reset the toast hash */
494 ReorderBufferToastReset(rb, txn);
495
496 /* All changes must be deallocated */
497 Assert(txn->size == 0);
498
499 pfree(txn);
500 }
501
502 /*
503 * Allocate a ReorderBufferChange.
504 */
505 ReorderBufferChange *
506 ReorderBufferAllocChange(ReorderBuffer *rb)
507 {
508 ReorderBufferChange *change;
509
510 change = (ReorderBufferChange *)
511 MemoryContextAlloc(rb->change_context, sizeof(ReorderBufferChange));
512
513 memset(change, 0, sizeof(ReorderBufferChange));
514 return change;
515 }
516
517 /*
518 * Free a ReorderBufferChange and update memory accounting, if requested.
519 */
520 void
521 ReorderBufferFreeChange(ReorderBuffer *rb, ReorderBufferChange *change,
522 bool upd_mem)
523 {
524 /* update memory accounting info */
525 if (upd_mem)
526 ReorderBufferChangeMemoryUpdate(rb, change, NULL, false,
527 ReorderBufferChangeSize(change));
528
529 /* free contained data */
530 switch (change->action)
531 {
532 case REORDER_BUFFER_CHANGE_INSERT:
533 case REORDER_BUFFER_CHANGE_UPDATE:
534 case REORDER_BUFFER_CHANGE_DELETE:
535 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
536 if (change->data.tp.newtuple)
537 {
538 ReorderBufferFreeTupleBuf(change->data.tp.newtuple);
539 change->data.tp.newtuple = NULL;
540 }
541
542 if (change->data.tp.oldtuple)
543 {
544 ReorderBufferFreeTupleBuf(change->data.tp.oldtuple);
545 change->data.tp.oldtuple = NULL;
546 }
547 break;
548 case REORDER_BUFFER_CHANGE_MESSAGE:
549 if (change->data.msg.prefix != NULL)
550 pfree(change->data.msg.prefix);
551 change->data.msg.prefix = NULL;
552 if (change->data.msg.message != NULL)
553 pfree(change->data.msg.message);
554 change->data.msg.message = NULL;
555 break;
556 case REORDER_BUFFER_CHANGE_INVALIDATION:
557 if (change->data.inval.invalidations)
558 pfree(change->data.inval.invalidations);
559 change->data.inval.invalidations = NULL;
560 break;
561 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
562 if (change->data.snapshot)
563 {
564 ReorderBufferFreeSnap(rb, change->data.snapshot);
565 change->data.snapshot = NULL;
566 }
567 break;
568 /* no data in addition to the struct itself */
569 case REORDER_BUFFER_CHANGE_TRUNCATE:
570 if (change->data.truncate.relids != NULL)
571 {
572 ReorderBufferFreeRelids(rb, change->data.truncate.relids);
573 change->data.truncate.relids = NULL;
574 }
575 break;
576 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
577 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT:
578 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
579 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
580 break;
581 }
582
583 pfree(change);
584 }
585
586 /*
587 * Allocate a HeapTuple fitting a tuple of size tuple_len (excluding header
588 * overhead).
589 */
590 HeapTuple
591 ReorderBufferAllocTupleBuf(ReorderBuffer *rb, Size tuple_len)
592 {
593 HeapTuple tuple;
594 Size alloc_len;
595
596 alloc_len = tuple_len + SizeofHeapTupleHeader;
597
598 tuple = (HeapTuple) MemoryContextAlloc(rb->tup_context,
599 HEAPTUPLESIZE + alloc_len);
600 tuple->t_data = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE);
601
602 return tuple;
603 }
604
605 /*
606 * Free a HeapTuple returned by ReorderBufferAllocTupleBuf().
607 */
608 void
609 ReorderBufferFreeTupleBuf(HeapTuple tuple)
610 {
611 pfree(tuple);
612 }
613
614 /*
615 * Allocate an array for relids of truncated relations.
616 *
617 * We use the global memory context (for the whole reorder buffer), because
618 * none of the existing ones seems like a good match (some are SLAB, so we
619 * can't use those, and tup_context is meant for tuple data, not relids). We
620 * could add yet another context, but it seems like an overkill - TRUNCATE is
621 * not particularly common operation, so it does not seem worth it.
622 */
623 Oid *
624 ReorderBufferAllocRelids(ReorderBuffer *rb, int nrelids)
625 {
626 Oid *relids;
627 Size alloc_len;
628
629 alloc_len = sizeof(Oid) * nrelids;
630
631 relids = (Oid *) MemoryContextAlloc(rb->context, alloc_len);
632
633 return relids;
634 }
635
636 /*
637 * Free an array of relids.
638 */
639 void
640 ReorderBufferFreeRelids(ReorderBuffer *rb, Oid *relids)
641 {
642 pfree(relids);
643 }
644
645 /*
646 * Return the ReorderBufferTXN from the given buffer, specified by Xid.
647 * If create is true, and a transaction doesn't already exist, create it
648 * (with the given LSN, and as top transaction if that's specified);
649 * when this happens, is_new is set to true.
650 */
651 static ReorderBufferTXN *
652 ReorderBufferTXNByXid(ReorderBuffer *rb, TransactionId xid, bool create,
653 bool *is_new, XLogRecPtr lsn, bool create_as_top)
654 {
655 ReorderBufferTXN *txn;
656 ReorderBufferTXNByIdEnt *ent;
657 bool found;
658
659 Assert(TransactionIdIsValid(xid));
660
661 /*
662 * Check the one-entry lookup cache first
663 */
664 if (TransactionIdIsValid(rb->by_txn_last_xid) &&
665 rb->by_txn_last_xid == xid)
666 {
667 txn = rb->by_txn_last_txn;
668
669 if (txn != NULL)
670 {
671 /* found it, and it's valid */
672 if (is_new)
673 *is_new = false;
674 return txn;
675 }
676
677 /*
678 * cached as non-existent, and asked not to create? Then nothing else
679 * to do.
680 */
681 if (!create)
682 return NULL;
683 /* otherwise fall through to create it */
684 }
685
686 /*
687 * If the cache wasn't hit or it yielded a "does-not-exist" and we want to
688 * create an entry.
689 */
690
691 /* search the lookup table */
692 ent = (ReorderBufferTXNByIdEnt *)
693 hash_search(rb->by_txn,
694 &xid,
695 create ? HASH_ENTER : HASH_FIND,
696 &found);
697 if (found)
698 txn = ent->txn;
699 else if (create)
700 {
701 /* initialize the new entry, if creation was requested */
702 Assert(ent != NULL);
703 Assert(lsn != InvalidXLogRecPtr);
704
705 ent->txn = ReorderBufferAllocTXN(rb);
706 ent->txn->xid = xid;
707 txn = ent->txn;
708 txn->first_lsn = lsn;
709 txn->restart_decoding_lsn = rb->current_restart_decoding_lsn;
710
711 if (create_as_top)
712 {
713 dlist_push_tail(&rb->toplevel_by_lsn, &txn->node);
714 AssertTXNLsnOrder(rb);
715 }
716 }
717 else
718 txn = NULL; /* not found and not asked to create */
719
720 /* update cache */
721 rb->by_txn_last_xid = xid;
722 rb->by_txn_last_txn = txn;
723
724 if (is_new)
725 *is_new = !found;
726
727 Assert(!create || txn != NULL);
728 return txn;
729 }
730
731 /*
732 * Record the partial change for the streaming of in-progress transactions. We
733 * can stream only complete changes so if we have a partial change like toast
734 * table insert or speculative insert then we mark such a 'txn' so that it
735 * can't be streamed. We also ensure that if the changes in such a 'txn' can
736 * be streamed and are above logical_decoding_work_mem threshold then we stream
737 * them as soon as we have a complete change.
738 */
739 static void
740 ReorderBufferProcessPartialChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
741 ReorderBufferChange *change,
742 bool toast_insert)
743 {
744 ReorderBufferTXN *toptxn;
745
746 /*
747 * The partial changes need to be processed only while streaming
748 * in-progress transactions.
749 */
750 if (!ReorderBufferCanStream(rb))
751 return;
752
753 /* Get the top transaction. */
754 toptxn = rbtxn_get_toptxn(txn);
755
756 /*
757 * Indicate a partial change for toast inserts. The change will be
758 * considered as complete once we get the insert or update on the main
759 * table and we are sure that the pending toast chunks are not required
760 * anymore.
761 *
762 * If we allow streaming when there are pending toast chunks then such
763 * chunks won't be released till the insert (multi_insert) is complete and
764 * we expect the txn to have streamed all changes after streaming. This
765 * restriction is mainly to ensure the correctness of streamed
766 * transactions and it doesn't seem worth uplifting such a restriction
767 * just to allow this case because anyway we will stream the transaction
768 * once such an insert is complete.
769 */
770 if (toast_insert)
771 toptxn->txn_flags |= RBTXN_HAS_PARTIAL_CHANGE;
772 else if (rbtxn_has_partial_change(toptxn) &&
773 IsInsertOrUpdate(change->action) &&
774 change->data.tp.clear_toast_afterwards)
775 toptxn->txn_flags &= ~RBTXN_HAS_PARTIAL_CHANGE;
776
777 /*
778 * Indicate a partial change for speculative inserts. The change will be
779 * considered as complete once we get the speculative confirm or abort
780 * token.
781 */
782 if (IsSpecInsert(change->action))
783 toptxn->txn_flags |= RBTXN_HAS_PARTIAL_CHANGE;
784 else if (rbtxn_has_partial_change(toptxn) &&
785 IsSpecConfirmOrAbort(change->action))
786 toptxn->txn_flags &= ~RBTXN_HAS_PARTIAL_CHANGE;
787
788 /*
789 * Stream the transaction if it is serialized before and the changes are
790 * now complete in the top-level transaction.
791 *
792 * The reason for doing the streaming of such a transaction as soon as we
793 * get the complete change for it is that previously it would have reached
794 * the memory threshold and wouldn't get streamed because of incomplete
795 * changes. Delaying such transactions would increase apply lag for them.
796 */
797 if (ReorderBufferCanStartStreaming(rb) &&
798 !(rbtxn_has_partial_change(toptxn)) &&
799 rbtxn_is_serialized(txn) &&
800 rbtxn_has_streamable_change(toptxn))
801 ReorderBufferStreamTXN(rb, toptxn);
802 }
803
804 /*
805 * Queue a change into a transaction so it can be replayed upon commit or will be
806 * streamed when we reach logical_decoding_work_mem threshold.
807 */
808 void
809 ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn,
810 ReorderBufferChange *change, bool toast_insert)
811 {
812 ReorderBufferTXN *txn;
813
814 txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
815
816 /*
817 * If we have detected that the transaction is aborted while streaming the
818 * previous changes or by checking its CLOG, there is no point in
819 * collecting further changes for it.
820 */
821 if (rbtxn_is_aborted(txn))
822 {
823 /*
824 * We don't need to update memory accounting for this change as we
825 * have not added it to the queue yet.
826 */
827 ReorderBufferFreeChange(rb, change, false);
828 return;
829 }
830
831 /*
832 * The changes that are sent downstream are considered streamable. We
833 * remember such transactions so that only those will later be considered
834 * for streaming.
835 */
836 if (change->action == REORDER_BUFFER_CHANGE_INSERT ||
837 change->action == REORDER_BUFFER_CHANGE_UPDATE ||
838 change->action == REORDER_BUFFER_CHANGE_DELETE ||
839 change->action == REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT ||
840 change->action == REORDER_BUFFER_CHANGE_TRUNCATE ||
841 change->action == REORDER_BUFFER_CHANGE_MESSAGE)
842 {
843 ReorderBufferTXN *toptxn = rbtxn_get_toptxn(txn);
844
845 toptxn->txn_flags |= RBTXN_HAS_STREAMABLE_CHANGE;
846 }
847
848 change->lsn = lsn;
849 change->txn = txn;
850
851 Assert(InvalidXLogRecPtr != lsn);
852 dlist_push_tail(&txn->changes, &change->node);
853 txn->nentries++;
854 txn->nentries_mem++;
855
856 /* update memory accounting information */
857 ReorderBufferChangeMemoryUpdate(rb, change, NULL, true,
858 ReorderBufferChangeSize(change));
859
860 /* process partial change */
861 ReorderBufferProcessPartialChange(rb, txn, change, toast_insert);
862
863 /* check the memory limits and evict something if needed */
864 ReorderBufferCheckMemoryLimit(rb);
865 }
866
867 /*
868 * A transactional message is queued to be processed upon commit and a
869 * non-transactional message gets processed immediately.
870 */
871 void
872 ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid,
873 Snapshot snap, XLogRecPtr lsn,
874 bool transactional, const char *prefix,
875 Size message_size, const char *message)
876 {
877 if (transactional)
878 {
879 MemoryContext oldcontext;
880 ReorderBufferChange *change;
881
882 Assert(xid != InvalidTransactionId);
883
884 /*
885 * We don't expect snapshots for transactional changes - we'll use the
886 * snapshot derived later during apply (unless the change gets
887 * skipped).
888 */
889 Assert(!snap);
890
891 oldcontext = MemoryContextSwitchTo(rb->context);
892
893 change = ReorderBufferAllocChange(rb);
894 change->action = REORDER_BUFFER_CHANGE_MESSAGE;
895 change->data.msg.prefix = pstrdup(prefix);
896 change->data.msg.message_size = message_size;
897 change->data.msg.message = palloc(message_size);
898 memcpy(change->data.msg.message, message, message_size);
899
900 ReorderBufferQueueChange(rb, xid, lsn, change, false);
901
902 MemoryContextSwitchTo(oldcontext);
903 }
904 else
905 {
906 ReorderBufferTXN *txn = NULL;
907 volatile Snapshot snapshot_now = snap;
908
909 /* Non-transactional changes require a valid snapshot. */
910 Assert(snapshot_now);
911
912 if (xid != InvalidTransactionId)
913 txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
914
915 /* setup snapshot to allow catalog access */
916 SetupHistoricSnapshot(snapshot_now, NULL);
917 PG_TRY();
918 {
919 rb->message(rb, txn, lsn, false, prefix, message_size, message);
920
921 TeardownHistoricSnapshot(false);
922 }
923 PG_CATCH();
924 {
925 TeardownHistoricSnapshot(true);
926 PG_RE_THROW();
927 }
928 PG_END_TRY();
929 }
930 }
931
932 /*
933 * AssertTXNLsnOrder
934 * Verify LSN ordering of transaction lists in the reorderbuffer
935 *
936 * Other LSN-related invariants are checked too.
937 *
938 * No-op if assertions are not in use.
939 */
940 static void
941 AssertTXNLsnOrder(ReorderBuffer *rb)
942 {
943 #ifdef USE_ASSERT_CHECKING
944 LogicalDecodingContext *ctx = rb->private_data;
945 dlist_iter iter;
946 XLogRecPtr prev_first_lsn = InvalidXLogRecPtr;
947 XLogRecPtr prev_base_snap_lsn = InvalidXLogRecPtr;
948
949 /*
950 * Skip the verification if we don't reach the LSN at which we start
951 * decoding the contents of transactions yet because until we reach the
952 * LSN, we could have transactions that don't have the association between
953 * the top-level transaction and subtransaction yet and consequently have
954 * the same LSN. We don't guarantee this association until we try to
955 * decode the actual contents of transaction. The ordering of the records
956 * prior to the start_decoding_at LSN should have been checked before the
957 * restart.
958 */
959 if (SnapBuildXactNeedsSkip(ctx->snapshot_builder, ctx->reader->EndRecPtr))
960 return;
961
962 dlist_foreach(iter, &rb->toplevel_by_lsn)
963 {
964 ReorderBufferTXN *cur_txn = dlist_container(ReorderBufferTXN, node,
965 iter.cur);
966
967 /* start LSN must be set */
968 Assert(cur_txn->first_lsn != InvalidXLogRecPtr);
969
970 /* If there is an end LSN, it must be higher than start LSN */
971 if (cur_txn->end_lsn != InvalidXLogRecPtr)
972 Assert(cur_txn->first_lsn <= cur_txn->end_lsn);
973
974 /* Current initial LSN must be strictly higher than previous */
975 if (prev_first_lsn != InvalidXLogRecPtr)
976 Assert(prev_first_lsn < cur_txn->first_lsn);
977
978 /* known-as-subtxn txns must not be listed */
979 Assert(!rbtxn_is_known_subxact(cur_txn));
980
981 prev_first_lsn = cur_txn->first_lsn;
982 }
983
984 dlist_foreach(iter, &rb->txns_by_base_snapshot_lsn)
985 {
986 ReorderBufferTXN *cur_txn = dlist_container(ReorderBufferTXN,
987 base_snapshot_node,
988 iter.cur);
989
990 /* base snapshot (and its LSN) must be set */
991 Assert(cur_txn->base_snapshot != NULL);
992 Assert(cur_txn->base_snapshot_lsn != InvalidXLogRecPtr);
993
994 /* current LSN must be strictly higher than previous */
995 if (prev_base_snap_lsn != InvalidXLogRecPtr)
996 Assert(prev_base_snap_lsn < cur_txn->base_snapshot_lsn);
997
998 /* known-as-subtxn txns must not be listed */
999 Assert(!rbtxn_is_known_subxact(cur_txn));
1000
1001 prev_base_snap_lsn = cur_txn->base_snapshot_lsn;
1002 }
1003 #endif
1004 }
1005
1006 /*
1007 * AssertChangeLsnOrder
1008 *
1009 * Check ordering of changes in the (sub)transaction.
1010 */
1011 static void
1012 AssertChangeLsnOrder(ReorderBufferTXN *txn)
1013 {
1014 #ifdef USE_ASSERT_CHECKING
1015 dlist_iter iter;
1016 XLogRecPtr prev_lsn = txn->first_lsn;
1017
1018 dlist_foreach(iter, &txn->changes)
1019 {
1020 ReorderBufferChange *cur_change;
1021
1022 cur_change = dlist_container(ReorderBufferChange, node, iter.cur);
1023
1024 Assert(txn->first_lsn != InvalidXLogRecPtr);
1025 Assert(cur_change->lsn != InvalidXLogRecPtr);
1026 Assert(txn->first_lsn <= cur_change->lsn);
1027
1028 if (txn->end_lsn != InvalidXLogRecPtr)
1029 Assert(cur_change->lsn <= txn->end_lsn);
1030
1031 Assert(prev_lsn <= cur_change->lsn);
1032
1033 prev_lsn = cur_change->lsn;
1034 }
1035 #endif
1036 }
1037
1038 /*
1039 * ReorderBufferGetOldestTXN
1040 * Return oldest transaction in reorderbuffer
1041 */
1042 ReorderBufferTXN *
1043 ReorderBufferGetOldestTXN(ReorderBuffer *rb)
1044 {
1045 ReorderBufferTXN *txn;
1046
1047 AssertTXNLsnOrder(rb);
1048
1049 if (dlist_is_empty(&rb->toplevel_by_lsn))
1050 return NULL;
1051
1052 txn = dlist_head_element(ReorderBufferTXN, node, &rb->toplevel_by_lsn);
1053
1054 Assert(!rbtxn_is_known_subxact(txn));
1055 Assert(txn->first_lsn != InvalidXLogRecPtr);
1056 return txn;
1057 }
1058
1059 /*
1060 * ReorderBufferGetOldestXmin
1061 * Return oldest Xmin in reorderbuffer
1062 *
1063 * Returns oldest possibly running Xid from the point of view of snapshots
1064 * used in the transactions kept by reorderbuffer, or InvalidTransactionId if
1065 * there are none.
1066 *
1067 * Since snapshots are assigned monotonically, this equals the Xmin of the
1068 * base snapshot with minimal base_snapshot_lsn.
1069 */
1070 TransactionId
1071 ReorderBufferGetOldestXmin(ReorderBuffer *rb)
1072 {
1073 ReorderBufferTXN *txn;
1074
1075 AssertTXNLsnOrder(rb);
1076
1077 if (dlist_is_empty(&rb->txns_by_base_snapshot_lsn))
1078 return InvalidTransactionId;
1079
1080 txn = dlist_head_element(ReorderBufferTXN, base_snapshot_node,
1081 &rb->txns_by_base_snapshot_lsn);
1082 return txn->base_snapshot->xmin;
1083 }
1084
1085 void
1086 ReorderBufferSetRestartPoint(ReorderBuffer *rb, XLogRecPtr ptr)
1087 {
1088 rb->current_restart_decoding_lsn = ptr;
1089 }
1090
1091 /*
1092 * ReorderBufferAssignChild
1093 *
1094 * Make note that we know that subxid is a subtransaction of xid, seen as of
1095 * the given lsn.
1096 */
1097 void
1098 ReorderBufferAssignChild(ReorderBuffer *rb, TransactionId xid,
1099 TransactionId subxid, XLogRecPtr lsn)
1100 {
1101 ReorderBufferTXN *txn;
1102 ReorderBufferTXN *subtxn;
1103 bool new_top;
1104 bool new_sub;
1105
1106 txn = ReorderBufferTXNByXid(rb, xid, true, &new_top, lsn, true);
1107 subtxn = ReorderBufferTXNByXid(rb, subxid, true, &new_sub, lsn, false);
1108
1109 if (!new_sub)
1110 {
1111 if (rbtxn_is_known_subxact(subtxn))
1112 {
1113 /* already associated, nothing to do */
1114 return;
1115 }
1116 else
1117 {
1118 /*
1119 * We already saw this transaction, but initially added it to the
1120 * list of top-level txns. Now that we know it's not top-level,
1121 * remove it from there.
1122 */
1123 dlist_delete(&subtxn->node);
1124 }
1125 }
1126
1127 subtxn->txn_flags |= RBTXN_IS_SUBXACT;
1128 subtxn->toplevel_xid = xid;
1129 Assert(subtxn->nsubtxns == 0);
1130
1131 /* set the reference to top-level transaction */
1132 subtxn->toptxn = txn;
1133
1134 /* add to subtransaction list */
1135 dlist_push_tail(&txn->subtxns, &subtxn->node);
1136 txn->nsubtxns++;
1137
1138 /* Possibly transfer the subtxn's snapshot to its top-level txn. */
1139 ReorderBufferTransferSnapToParent(txn, subtxn);
1140
1141 /* Verify LSN-ordering invariant */
1142 AssertTXNLsnOrder(rb);
1143 }
1144
1145 /*
1146 * ReorderBufferTransferSnapToParent
1147 * Transfer base snapshot from subtxn to top-level txn, if needed
1148 *
1149 * This is done if the top-level txn doesn't have a base snapshot, or if the
1150 * subtxn's base snapshot has an earlier LSN than the top-level txn's base
1151 * snapshot's LSN. This can happen if there are no changes in the toplevel
1152 * txn but there are some in the subtxn, or the first change in subtxn has
1153 * earlier LSN than first change in the top-level txn and we learned about
1154 * their kinship only now.
1155 *
1156 * The subtransaction's snapshot is cleared regardless of the transfer
1157 * happening, since it's not needed anymore in either case.
1158 *
1159 * We do this as soon as we become aware of their kinship, to avoid queueing
1160 * extra snapshots to txns known-as-subtxns -- only top-level txns will
1161 * receive further snapshots.
1162 */
1163 static void
1164 ReorderBufferTransferSnapToParent(ReorderBufferTXN *txn,
1165 ReorderBufferTXN *subtxn)
1166 {
1167 Assert(subtxn->toplevel_xid == txn->xid);
1168
1169 if (subtxn->base_snapshot != NULL)
1170 {
1171 if (txn->base_snapshot == NULL ||
1172 subtxn->base_snapshot_lsn < txn->base_snapshot_lsn)
1173 {
1174 /*
1175 * If the toplevel transaction already has a base snapshot but
1176 * it's newer than the subxact's, purge it.
1177 */
1178 if (txn->base_snapshot != NULL)
1179 {
1180 SnapBuildSnapDecRefcount(txn->base_snapshot);
1181 dlist_delete(&txn->base_snapshot_node);
1182 }
1183
1184 /*
1185 * The snapshot is now the top transaction's; transfer it, and
1186 * adjust the list position of the top transaction in the list by
1187 * moving it to where the subtransaction is.
1188 */
1189 txn->base_snapshot = subtxn->base_snapshot;
1190 txn->base_snapshot_lsn = subtxn->base_snapshot_lsn;
1191 dlist_insert_before(&subtxn->base_snapshot_node,
1192 &txn->base_snapshot_node);
1193
1194 /*
1195 * The subtransaction doesn't have a snapshot anymore (so it
1196 * mustn't be in the list.)
1197 */
1198 subtxn->base_snapshot = NULL;
1199 subtxn->base_snapshot_lsn = InvalidXLogRecPtr;
1200 dlist_delete(&subtxn->base_snapshot_node);
1201 }
1202 else
1203 {
1204 /* Base snap of toplevel is fine, so subxact's is not needed */
1205 SnapBuildSnapDecRefcount(subtxn->base_snapshot);
1206 dlist_delete(&subtxn->base_snapshot_node);
1207 subtxn->base_snapshot = NULL;
1208 subtxn->base_snapshot_lsn = InvalidXLogRecPtr;
1209 }
1210 }
1211 }
1212
1213 /*
1214 * Associate a subtransaction with its toplevel transaction at commit
1215 * time. There may be no further changes added after this.
1216 */
1217 void
1218 ReorderBufferCommitChild(ReorderBuffer *rb, TransactionId xid,
1219 TransactionId subxid, XLogRecPtr commit_lsn,
1220 XLogRecPtr end_lsn)
1221 {
1222 ReorderBufferTXN *subtxn;
1223
1224 subtxn = ReorderBufferTXNByXid(rb, subxid, false, NULL,
1225 InvalidXLogRecPtr, false);
1226
1227 /*
1228 * No need to do anything if that subtxn didn't contain any changes
1229 */
1230 if (!subtxn)
1231 return;
1232
1233 subtxn->final_lsn = commit_lsn;
1234 subtxn->end_lsn = end_lsn;
1235
1236 /*
1237 * Assign this subxact as a child of the toplevel xact (no-op if already
1238 * done.)
1239 */
1240 ReorderBufferAssignChild(rb, xid, subxid, InvalidXLogRecPtr);
1241 }
1242
1243
1244 /*
1245 * Support for efficiently iterating over a transaction's and its
1246 * subtransactions' changes.
1247 *
1248 * We do by doing a k-way merge between transactions/subtransactions. For that
1249 * we model the current heads of the different transactions as a binary heap
1250 * so we easily know which (sub-)transaction has the change with the smallest
1251 * lsn next.
1252 *
1253 * We assume the changes in individual transactions are already sorted by LSN.
1254 */
1255
1256 /*
1257 * Binary heap comparison function.
1258 */
1259 static int
1260 ReorderBufferIterCompare(Datum a, Datum b, void *arg)
1261 {
1262 ReorderBufferIterTXNState *state = (ReorderBufferIterTXNState *) arg;
1263 XLogRecPtr pos_a = state->entries[DatumGetInt32(a)].lsn;
1264 XLogRecPtr pos_b = state->entries[DatumGetInt32(b)].lsn;
1265
1266 if (pos_a < pos_b)
1267 return 1;
1268 else if (pos_a == pos_b)
1269 return 0;
1270 return -1;
1271 }
1272
1273 /*
1274 * Allocate & initialize an iterator which iterates in lsn order over a
1275 * transaction and all its subtransactions.
1276 *
1277 * Note: The iterator state is returned through iter_state parameter rather
1278 * than the function's return value. This is because the state gets cleaned up
1279 * in a PG_CATCH block in the caller, so we want to make sure the caller gets
1280 * back the state even if this function throws an exception.
1281 */
1282 static void
1283 ReorderBufferIterTXNInit(ReorderBuffer *rb, ReorderBufferTXN *txn,
1284 ReorderBufferIterTXNState *volatile *iter_state)
1285 {
1286 Size nr_txns = 0;
1287 ReorderBufferIterTXNState *state;
1288 dlist_iter cur_txn_i;
1289 int32 off;
1290
1291 *iter_state = NULL;
1292
1293 /* Check ordering of changes in the toplevel transaction. */
1294 AssertChangeLsnOrder(txn);
1295
1296 /*
1297 * Calculate the size of our heap: one element for every transaction that
1298 * contains changes. (Besides the transactions already in the reorder
1299 * buffer, we count the one we were directly passed.)
1300 */
1301 if (txn->nentries > 0)
1302 nr_txns++;
1303
1304 dlist_foreach(cur_txn_i, &txn->subtxns)
1305 {
1306 ReorderBufferTXN *cur_txn;
1307
1308 cur_txn = dlist_container(ReorderBufferTXN, node, cur_txn_i.cur);
1309
1310 /* Check ordering of changes in this subtransaction. */
1311 AssertChangeLsnOrder(cur_txn);
1312
1313 if (cur_txn->nentries > 0)
1314 nr_txns++;
1315 }
1316
1317 /* allocate iteration state */
1318 state = (ReorderBufferIterTXNState *)
1319 MemoryContextAllocZero(rb->context,
1320 sizeof(ReorderBufferIterTXNState) +
1321 sizeof(ReorderBufferIterTXNEntry) * nr_txns);
1322
1323 state->nr_txns = nr_txns;
1324 dlist_init(&state->old_change);
1325
1326 for (off = 0; off < state->nr_txns; off++)
1327 {
1328 state->entries[off].file.vfd = -1;
1329 state->entries[off].segno = 0;
1330 }
1331
1332 /* allocate heap */
1333 state->heap = binaryheap_allocate(state->nr_txns,
1334 ReorderBufferIterCompare,
1335 state);
1336
1337 /* Now that the state fields are initialized, it is safe to return it. */
1338 *iter_state = state;
1339
1340 /*
1341 * Now insert items into the binary heap, in an unordered fashion. (We
1342 * will run a heap assembly step at the end; this is more efficient.)
1343 */
1344
1345 off = 0;
1346
1347 /* add toplevel transaction if it contains changes */
1348 if (txn->nentries > 0)
1349 {
1350 ReorderBufferChange *cur_change;
1351
1352 if (rbtxn_is_serialized(txn))
1353 {
1354 /* serialize remaining changes */
1355 ReorderBufferSerializeTXN(rb, txn);
1356 ReorderBufferRestoreChanges(rb, txn, &state->entries[off].file,
1357 &state->entries[off].segno);
1358 }
1359
1360 cur_change = dlist_head_element(ReorderBufferChange, node,
1361 &txn->changes);
1362
1363 state->entries[off].lsn = cur_change->lsn;
1364 state->entries[off].change = cur_change;
1365 state->entries[off].txn = txn;
1366
1367 binaryheap_add_unordered(state->heap, Int32GetDatum(off++));
1368 }
1369
1370 /* add subtransactions if they contain changes */
1371 dlist_foreach(cur_txn_i, &txn->subtxns)
1372 {
1373 ReorderBufferTXN *cur_txn;
1374
1375 cur_txn = dlist_container(ReorderBufferTXN, node, cur_txn_i.cur);
1376
1377 if (cur_txn->nentries > 0)
1378 {
1379 ReorderBufferChange *cur_change;
1380
1381 if (rbtxn_is_serialized(cur_txn))
1382 {
1383 /* serialize remaining changes */
1384 ReorderBufferSerializeTXN(rb, cur_txn);
1385 ReorderBufferRestoreChanges(rb, cur_txn,
1386 &state->entries[off].file,
1387 &state->entries[off].segno);
1388 }
1389 cur_change = dlist_head_element(ReorderBufferChange, node,
1390 &cur_txn->changes);
1391
1392 state->entries[off].lsn = cur_change->lsn;
1393 state->entries[off].change = cur_change;
1394 state->entries[off].txn = cur_txn;
1395
1396 binaryheap_add_unordered(state->heap, Int32GetDatum(off++));
1397 }
1398 }
1399
1400 /* assemble a valid binary heap */
1401 binaryheap_build(state->heap);
1402 }
1403
1404 /*
1405 * Return the next change when iterating over a transaction and its
1406 * subtransactions.
1407 *
1408 * Returns NULL when no further changes exist.
1409 */
1410 static ReorderBufferChange *
1411 ReorderBufferIterTXNNext(ReorderBuffer *rb, ReorderBufferIterTXNState *state)
1412 {
1413 ReorderBufferChange *change;
1414 ReorderBufferIterTXNEntry *entry;
1415 int32 off;
1416
1417 /* nothing there anymore */
1418 if (state->heap->bh_size == 0)
1419 return NULL;
1420
1421 off = DatumGetInt32(binaryheap_first(state->heap));
1422 entry = &state->entries[off];
1423
1424 /* free memory we might have "leaked" in the previous *Next call */
1425 if (!dlist_is_empty(&state->old_change))
1426 {
1427 change = dlist_container(ReorderBufferChange, node,
1428 dlist_pop_head_node(&state->old_change));
1429 ReorderBufferFreeChange(rb, change, true);
1430 Assert(dlist_is_empty(&state->old_change));
1431 }
1432
1433 change = entry->change;
1434
1435 /*
1436 * update heap with information about which transaction has the next
1437 * relevant change in LSN order
1438 */
1439
1440 /* there are in-memory changes */
1441 if (dlist_has_next(&entry->txn->changes, &entry->change->node))
1442 {
1443 dlist_node *next = dlist_next_node(&entry->txn->changes, &change->node);
1444 ReorderBufferChange *next_change =
1445 dlist_container(ReorderBufferChange, node, next);
1446
1447 /* txn stays the same */
1448 state->entries[off].lsn = next_change->lsn;
1449 state->entries[off].change = next_change;
1450
1451 binaryheap_replace_first(state->heap, Int32GetDatum(off));
1452 return change;
1453 }
1454
1455 /* try to load changes from disk */
1456 if (entry->txn->nentries != entry->txn->nentries_mem)
1457 {
1458 /*
1459 * Ugly: restoring changes will reuse *Change records, thus delete the
1460 * current one from the per-tx list and only free in the next call.
1461 */
1462 dlist_delete(&change->node);
1463 dlist_push_tail(&state->old_change, &change->node);
1464
1465 /*
1466 * Update the total bytes processed by the txn for which we are
1467 * releasing the current set of changes and restoring the new set of
1468 * changes.
1469 */
1470 rb->totalBytes += entry->txn->size;
1471 if (ReorderBufferRestoreChanges(rb, entry->txn, &entry->file,
1472 &state->entries[off].segno))
1473 {
1474 /* successfully restored changes from disk */
1475 ReorderBufferChange *next_change =
1476 dlist_head_element(ReorderBufferChange, node,
1477 &entry->txn->changes);
1478
1479 elog(DEBUG2, "restored %u/%u changes from disk",
1480 (uint32) entry->txn->nentries_mem,
1481 (uint32) entry->txn->nentries);
1482
1483 Assert(entry->txn->nentries_mem);
1484 /* txn stays the same */
1485 state->entries[off].lsn = next_change->lsn;
1486 state->entries[off].change = next_change;
1487 binaryheap_replace_first(state->heap, Int32GetDatum(off));
1488
1489 return change;
1490 }
1491 }
1492
1493 /* ok, no changes there anymore, remove */
1494 binaryheap_remove_first(state->heap);
1495
1496 return change;
1497 }
1498
1499 /*
1500 * Deallocate the iterator
1501 */
1502 static void
1503 ReorderBufferIterTXNFinish(ReorderBuffer *rb,
1504 ReorderBufferIterTXNState *state)
1505 {
1506 int32 off;
1507
1508 for (off = 0; off < state->nr_txns; off++)
1509 {
1510 if (state->entries[off].file.vfd != -1)
1511 FileClose(state->entries[off].file.vfd);
1512 }
1513
1514 /* free memory we might have "leaked" in the last *Next call */
1515 if (!dlist_is_empty(&state->old_change))
1516 {
1517 ReorderBufferChange *change;
1518
1519 change = dlist_container(ReorderBufferChange, node,
1520 dlist_pop_head_node(&state->old_change));
1521 ReorderBufferFreeChange(rb, change, true);
1522 Assert(dlist_is_empty(&state->old_change));
1523 }
1524
1525 binaryheap_free(state->heap);
1526 pfree(state);
1527 }
1528
1529 /*
1530 * Cleanup the contents of a transaction, usually after the transaction
1531 * committed or aborted.
1532 */
1533 static void
1534 ReorderBufferCleanupTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
1535 {
1536 bool found;
1537 dlist_mutable_iter iter;
1538 Size mem_freed = 0;
1539
1540 /* cleanup subtransactions & their changes */
1541 dlist_foreach_modify(iter, &txn->subtxns)
1542 {
1543 ReorderBufferTXN *subtxn;
1544
1545 subtxn = dlist_container(ReorderBufferTXN, node, iter.cur);
1546
1547 /*
1548 * Subtransactions are always associated to the toplevel TXN, even if
1549 * they originally were happening inside another subtxn, so we won't
1550 * ever recurse more than one level deep here.
1551 */
1552 Assert(rbtxn_is_known_subxact(subtxn));
1553 Assert(subtxn->nsubtxns == 0);
1554
1555 ReorderBufferCleanupTXN(rb, subtxn);
1556 }
1557
1558 /* cleanup changes in the txn */
1559 dlist_foreach_modify(iter, &txn->changes)
1560 {
1561 ReorderBufferChange *change;
1562
1563 change = dlist_container(ReorderBufferChange, node, iter.cur);
1564
1565 /* Check we're not mixing changes from different transactions. */
1566 Assert(change->txn == txn);
1567
1568 /*
1569 * Instead of updating the memory counter for individual changes, we
1570 * sum up the size of memory to free so we can update the memory
1571 * counter all together below. This saves costs of maintaining the
1572 * max-heap.
1573 */
1574 mem_freed += ReorderBufferChangeSize(change);
1575
1576 ReorderBufferFreeChange(rb, change, false);
1577 }
1578
1579 /* Update the memory counter */
1580 ReorderBufferChangeMemoryUpdate(rb, NULL, txn, false, mem_freed);
1581
1582 /*
1583 * Cleanup the tuplecids we stored for decoding catalog snapshot access.
1584 * They are always stored in the toplevel transaction.
1585 */
1586 dlist_foreach_modify(iter, &txn->tuplecids)
1587 {
1588 ReorderBufferChange *change;
1589
1590 change = dlist_container(ReorderBufferChange, node, iter.cur);
1591
1592 /* Check we're not mixing changes from different transactions. */
1593 Assert(change->txn == txn);
1594 Assert(change->action == REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID);
1595
1596 ReorderBufferFreeChange(rb, change, true);
1597 }
1598
1599 /*
1600 * Cleanup the base snapshot, if set.
1601 */
1602 if (txn->base_snapshot != NULL)
1603 {
1604 SnapBuildSnapDecRefcount(txn->base_snapshot);
1605 dlist_delete(&txn->base_snapshot_node);
1606 }
1607
1608 /*
1609 * Cleanup the snapshot for the last streamed run.
1610 */
1611 if (txn->snapshot_now != NULL)
1612 {
1613 Assert(rbtxn_is_streamed(txn));
1614 ReorderBufferFreeSnap(rb, txn->snapshot_now);
1615 }
1616
1617 /*
1618 * Remove TXN from its containing lists.
1619 *
1620 * Note: if txn is known as subxact, we are deleting the TXN from its
1621 * parent's list of known subxacts; this leaves the parent's nsubxacts
1622 * count too high, but we don't care. Otherwise, we are deleting the TXN
1623 * from the LSN-ordered list of toplevel TXNs. We remove the TXN from the
1624 * list of catalog modifying transactions as well.
1625 */
1626 dlist_delete(&txn->node);
1627 if (rbtxn_has_catalog_changes(txn))
1628 dclist_delete_from(&rb->catchange_txns, &txn->catchange_node);
1629
1630 /* now remove reference from buffer */
1631 hash_search(rb->by_txn, &txn->xid, HASH_REMOVE, &found);
1632 Assert(found);
1633
1634 /* remove entries spilled to disk */
1635 if (rbtxn_is_serialized(txn))
1636 ReorderBufferRestoreCleanup(rb, txn);
1637
1638 /* deallocate */
1639 ReorderBufferFreeTXN(rb, txn);
1640 }
1641
1642 /*
1643 * Discard changes from a transaction (and subtransactions), either after
1644 * streaming, decoding them at PREPARE, or detecting the transaction abort.
1645 * Keep the remaining info - transactions, tuplecids, invalidations and
1646 * snapshots.
1647 *
1648 * We additionally remove tuplecids after decoding the transaction at prepare
1649 * time as we only need to perform invalidation at rollback or commit prepared.
1650 *
1651 * 'txn_prepared' indicates that we have decoded the transaction at prepare
1652 * time.
1653 */
1654 static void
1655 ReorderBufferTruncateTXN(ReorderBuffer *rb, ReorderBufferTXN *txn, bool txn_prepared)
1656 {
1657 dlist_mutable_iter iter;
1658 Size mem_freed = 0;
1659
1660 /* cleanup subtransactions & their changes */
1661 dlist_foreach_modify(iter, &txn->subtxns)
1662 {
1663 ReorderBufferTXN *subtxn;
1664
1665 subtxn = dlist_container(ReorderBufferTXN, node, iter.cur);
1666
1667 /*
1668 * Subtransactions are always associated to the toplevel TXN, even if
1669 * they originally were happening inside another subtxn, so we won't
1670 * ever recurse more than one level deep here.
1671 */
1672 Assert(rbtxn_is_known_subxact(subtxn));
1673 Assert(subtxn->nsubtxns == 0);
1674
1675 ReorderBufferMaybeMarkTXNStreamed(rb, subtxn);
1676 ReorderBufferTruncateTXN(rb, subtxn, txn_prepared);
1677 }
1678
1679 /* cleanup changes in the txn */
1680 dlist_foreach_modify(iter, &txn->changes)
1681 {
1682 ReorderBufferChange *change;
1683
1684 change = dlist_container(ReorderBufferChange, node, iter.cur);
1685
1686 /* Check we're not mixing changes from different transactions. */
1687 Assert(change->txn == txn);
1688
1689 /* remove the change from its containing list */
1690 dlist_delete(&change->node);
1691
1692 /*
1693 * Instead of updating the memory counter for individual changes, we
1694 * sum up the size of memory to free so we can update the memory
1695 * counter all together below. This saves costs of maintaining the
1696 * max-heap.
1697 */
1698 mem_freed += ReorderBufferChangeSize(change);
1699
1700 ReorderBufferFreeChange(rb, change, false);
1701 }
1702
1703 /* Update the memory counter */
1704 ReorderBufferChangeMemoryUpdate(rb, NULL, txn, false, mem_freed);
1705
1706 if (txn_prepared)
1707 {
1708 /*
1709 * If this is a prepared txn, cleanup the tuplecids we stored for
1710 * decoding catalog snapshot access. They are always stored in the
1711 * toplevel transaction.
1712 */
1713 dlist_foreach_modify(iter, &txn->tuplecids)
1714 {
1715 ReorderBufferChange *change;
1716
1717 change = dlist_container(ReorderBufferChange, node, iter.cur);
1718
1719 /* Check we're not mixing changes from different transactions. */
1720 Assert(change->txn == txn);
1721 Assert(change->action == REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID);
1722
1723 /* Remove the change from its containing list. */
1724 dlist_delete(&change->node);
1725
1726 ReorderBufferFreeChange(rb, change, true);
1727 }
1728 }
1729
1730 /*
1731 * Destroy the (relfilelocator, ctid) hashtable, so that we don't leak any
1732 * memory. We could also keep the hash table and update it with new ctid
1733 * values, but this seems simpler and good enough for now.
1734 */
1735 if (txn->tuplecid_hash != NULL)
1736 {
1737 hash_destroy(txn->tuplecid_hash);
1738 txn->tuplecid_hash = NULL;
1739 }
1740
1741 /* If this txn is serialized then clean the disk space. */
1742 if (rbtxn_is_serialized(txn))
1743 {
1744 ReorderBufferRestoreCleanup(rb, txn);
1745 txn->txn_flags &= ~RBTXN_IS_SERIALIZED;
1746
1747 /*
1748 * We set this flag to indicate if the transaction is ever serialized.
1749 * We need this to accurately update the stats as otherwise the same
1750 * transaction can be counted as serialized multiple times.
1751 */
1752 txn->txn_flags |= RBTXN_IS_SERIALIZED_CLEAR;
1753 }
1754
1755 /* also reset the number of entries in the transaction */
1756 txn->nentries_mem = 0;
1757 txn->nentries = 0;
1758 }
1759
1760 /*
1761 * Check the transaction status by CLOG lookup and discard all changes if
1762 * the transaction is aborted. The transaction status is cached in
1763 * txn->txn_flags so we can skip future changes and avoid CLOG lookups on the
1764 * next call.
1765 *
1766 * Return true if the transaction is aborted, otherwise return false.
1767 *
1768 * When the 'debug_logical_replication_streaming' is set to "immediate", we
1769 * don't check the transaction status, meaning the caller will always process
1770 * this transaction.
1771 */
1772 static bool
1773 ReorderBufferCheckAndTruncateAbortedTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
1774 {
1775 /* Quick return for regression tests */
1776 if (unlikely(debug_logical_replication_streaming == DEBUG_LOGICAL_REP_STREAMING_IMMEDIATE))
1777 return false;
1778
1779 /*
1780 * Quick return if the transaction status is already known.
1781 */
1782
1783 if (rbtxn_is_committed(txn))
1784 return false;
1785 if (rbtxn_is_aborted(txn))
1786 {
1787 /* Already-aborted transactions should not have any changes */
1788 Assert(txn->size == 0);
1789
1790 return true;
1791 }
1792
1793 /* Otherwise, check the transaction status using CLOG lookup */
1794
1795 if (TransactionIdIsInProgress(txn->xid))
1796 return false;
1797
1798 if (TransactionIdDidCommit(txn->xid))
1799 {
1800 /*
1801 * Remember the transaction is committed so that we can skip CLOG
1802 * check next time, avoiding the pressure on CLOG lookup.
1803 */
1804 Assert(!rbtxn_is_aborted(txn));
1805 txn->txn_flags |= RBTXN_IS_COMMITTED;
1806 return false;
1807 }
1808
1809 /*
1810 * The transaction aborted. We discard both the changes collected so far
1811 * and the toast reconstruction data. The full cleanup will happen as part
1812 * of decoding ABORT record of this transaction.
1813 */
1814 ReorderBufferTruncateTXN(rb, txn, rbtxn_is_prepared(txn));
1815 ReorderBufferToastReset(rb, txn);
1816
1817 /* All changes should be discarded */
1818 Assert(txn->size == 0);
1819
1820 /*
1821 * Mark the transaction as aborted so we can ignore future changes of this
1822 * transaction.
1823 */
1824 Assert(!rbtxn_is_committed(txn));
1825 txn->txn_flags |= RBTXN_IS_ABORTED;
1826
1827 return true;
1828 }
1829
1830 /*
1831 * Build a hash with a (relfilelocator, ctid) -> (cmin, cmax) mapping for use by
1832 * HeapTupleSatisfiesHistoricMVCC.
1833 */
1834 static void
1835 ReorderBufferBuildTupleCidHash(ReorderBuffer *rb, ReorderBufferTXN *txn)
1836 {
1837 dlist_iter iter;
1838 HASHCTL hash_ctl;
1839
1840 if (!rbtxn_has_catalog_changes(txn) || dlist_is_empty(&txn->tuplecids))
1841 return;
1842
1843 hash_ctl.keysize = sizeof(ReorderBufferTupleCidKey);
1844 hash_ctl.entrysize = sizeof(ReorderBufferTupleCidEnt);
1845 hash_ctl.hcxt = rb->context;
1846
1847 /*
1848 * create the hash with the exact number of to-be-stored tuplecids from
1849 * the start
1850 */
1851 txn->tuplecid_hash =
1852 hash_create("ReorderBufferTupleCid", txn->ntuplecids, &hash_ctl,
1853 HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
1854
1855 dlist_foreach(iter, &txn->tuplecids)
1856 {
1857 ReorderBufferTupleCidKey key;
1858 ReorderBufferTupleCidEnt *ent;
1859 bool found;
1860 ReorderBufferChange *change;
1861
1862 change = dlist_container(ReorderBufferChange, node, iter.cur);
1863
1864 Assert(change->action == REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID);
1865
1866 /* be careful about padding */
1867 memset(&key, 0, sizeof(ReorderBufferTupleCidKey));
1868
1869 key.rlocator = change->data.tuplecid.locator;
1870
1871 ItemPointerCopy(&change->data.tuplecid.tid,
1872 &key.tid);
1873
1874 ent = (ReorderBufferTupleCidEnt *)
1875 hash_search(txn->tuplecid_hash, &key, HASH_ENTER, &found);
1876 if (!found)
1877 {
1878 ent->cmin = change->data.tuplecid.cmin;
1879 ent->cmax = change->data.tuplecid.cmax;
1880 ent->combocid = change->data.tuplecid.combocid;
1881 }
1882 else
1883 {
1884 /*
1885 * Maybe we already saw this tuple before in this transaction, but
1886 * if so it must have the same cmin.
1887 */
1888 Assert(ent->cmin == change->data.tuplecid.cmin);
1889
1890 /*
1891 * cmax may be initially invalid, but once set it can only grow,
1892 * and never become invalid again.
1893 */
1894 Assert((ent->cmax == InvalidCommandId) ||
1895 ((change->data.tuplecid.cmax != InvalidCommandId) &&
1896 (change->data.tuplecid.cmax > ent->cmax)));
1897 ent->cmax = change->data.tuplecid.cmax;
1898 }
1899 }
1900 }
1901
1902 /*
1903 * Copy a provided snapshot so we can modify it privately. This is needed so
1904 * that catalog modifying transactions can look into intermediate catalog
1905 * states.
1906 */
1907 static Snapshot
1908 ReorderBufferCopySnap(ReorderBuffer *rb, Snapshot orig_snap,
1909 ReorderBufferTXN *txn, CommandId cid)
1910 {
1911 Snapshot snap;
1912 dlist_iter iter;
1913 int i = 0;
1914 Size size;
1915
1916 size = sizeof(SnapshotData) +
1917 sizeof(TransactionId) * orig_snap->xcnt +
1918 sizeof(TransactionId) * (txn->nsubtxns + 1);
1919
1920 snap = MemoryContextAllocZero(rb->context, size);
1921 memcpy(snap, orig_snap, sizeof(SnapshotData));
1922
1923 snap->copied = true;
1924 snap->active_count = 1; /* mark as active so nobody frees it */
1925 snap->regd_count = 0;
1926 snap->xip = (TransactionId *) (snap + 1);
1927
1928 memcpy(snap->xip, orig_snap->xip, sizeof(TransactionId) * snap->xcnt);
1929
1930 /*
1931 * snap->subxip contains all txids that belong to our transaction which we
1932 * need to check via cmin/cmax. That's why we store the toplevel
1933 * transaction in there as well.
1934 */
1935 snap->subxip = snap->xip + snap->xcnt;
1936 snap->subxip[i++] = txn->xid;
1937
1938 /*
1939 * txn->nsubtxns isn't decreased when subtransactions abort, so count
1940 * manually. Since it's an upper boundary it is safe to use it for the
1941 * allocation above.
1942 */
1943 snap->subxcnt = 1;
1944
1945 dlist_foreach(iter, &txn->subtxns)
1946 {
1947 ReorderBufferTXN *sub_txn;
1948
1949 sub_txn = dlist_container(ReorderBufferTXN, node, iter.cur);
1950 snap->subxip[i++] = sub_txn->xid;
1951 snap->subxcnt++;
1952 }
1953
1954 /* sort so we can bsearch() later */
1955 qsort(snap->subxip, snap->subxcnt, sizeof(TransactionId), xidComparator);
1956
1957 /* store the specified current CommandId */
1958 snap->curcid = cid;
1959
1960 return snap;
1961 }
1962
1963 /*
1964 * Free a previously ReorderBufferCopySnap'ed snapshot
1965 */
1966 static void
1967 ReorderBufferFreeSnap(ReorderBuffer *rb, Snapshot snap)
1968 {
1969 if (snap->copied)
1970 pfree(snap);
1971 else
1972 SnapBuildSnapDecRefcount(snap);
1973 }
1974
1975 /*
1976 * If the transaction was (partially) streamed, we need to prepare or commit
1977 * it in a 'streamed' way. That is, we first stream the remaining part of the
1978 * transaction, and then invoke stream_prepare or stream_commit message as per
1979 * the case.
1980 */
1981 static void
1982 ReorderBufferStreamCommit(ReorderBuffer *rb, ReorderBufferTXN *txn)
1983 {
1984 /* we should only call this for previously streamed transactions */
1985 Assert(rbtxn_is_streamed(txn));
1986
1987 ReorderBufferStreamTXN(rb, txn);
1988
1989 if (rbtxn_is_prepared(txn))
1990 {
1991 /*
1992 * Note, we send stream prepare even if a concurrent abort is
1993 * detected. See DecodePrepare for more information.
1994 */
1995 Assert(!rbtxn_sent_prepare(txn));
1996 rb->stream_prepare(rb, txn, txn->final_lsn);
1997 txn->txn_flags |= RBTXN_SENT_PREPARE;
1998
1999 /*
2000 * This is a PREPARED transaction, part of a two-phase commit. The
2001 * full cleanup will happen as part of the COMMIT PREPAREDs, so now
2002 * just truncate txn by removing changes and tuplecids.
2003 */
2004 ReorderBufferTruncateTXN(rb, txn, true);
2005 /* Reset the CheckXidAlive */
2006 CheckXidAlive = InvalidTransactionId;
2007 }
2008 else
2009 {
2010 rb->stream_commit(rb, txn, txn->final_lsn);
2011 ReorderBufferCleanupTXN(rb, txn);
2012 }
2013 }
2014
2015 /*
2016 * Set xid to detect concurrent aborts.
2017 *
2018 * While streaming an in-progress transaction or decoding a prepared
2019 * transaction there is a possibility that the (sub)transaction might get
2020 * aborted concurrently. In such case if the (sub)transaction has catalog
2021 * update then we might decode the tuple using wrong catalog version. For
2022 * example, suppose there is one catalog tuple with (xmin: 500, xmax: 0). Now,
2023 * the transaction 501 updates the catalog tuple and after that we will have
2024 * two tuples (xmin: 500, xmax: 501) and (xmin: 501, xmax: 0). Now, if 501 is
2025 * aborted and some other transaction say 502 updates the same catalog tuple
2026 * then the first tuple will be changed to (xmin: 500, xmax: 502). So, the
2027 * problem is that when we try to decode the tuple inserted/updated in 501
2028 * after the catalog update, we will see the catalog tuple with (xmin: 500,
2029 * xmax: 502) as visible because it will consider that the tuple is deleted by
2030 * xid 502 which is not visible to our snapshot. And when we will try to
2031 * decode with that catalog tuple, it can lead to a wrong result or a crash.
2032 * So, it is necessary to detect concurrent aborts to allow streaming of
2033 * in-progress transactions or decoding of prepared transactions.
2034 *
2035 * For detecting the concurrent abort we set CheckXidAlive to the current
2036 * (sub)transaction's xid for which this change belongs to. And, during
2037 * catalog scan we can check the status of the xid and if it is aborted we will
2038 * report a specific error so that we can stop streaming current transaction
2039 * and discard the already streamed changes on such an error. We might have
2040 * already streamed some of the changes for the aborted (sub)transaction, but
2041 * that is fine because when we decode the abort we will stream abort message
2042 * to truncate the changes in the subscriber. Similarly, for prepared
2043 * transactions, we stop decoding if concurrent abort is detected and then
2044 * rollback the changes when rollback prepared is encountered. See
2045 * DecodePrepare.
2046 */
2047 static inline void
2048 SetupCheckXidLive(TransactionId xid)
2049 {
2050 /*
2051 * If the input transaction id is already set as a CheckXidAlive then
2052 * nothing to do.
2053 */
2054 if (TransactionIdEquals(CheckXidAlive, xid))
2055 return;
2056
2057 /*
2058 * setup CheckXidAlive if it's not committed yet. We don't check if the
2059 * xid is aborted. That will happen during catalog access.
2060 */
2061 if (!TransactionIdDidCommit(xid))
2062 CheckXidAlive = xid;
2063 else
2064 CheckXidAlive = InvalidTransactionId;
2065 }
2066
2067 /*
2068 * Helper function for ReorderBufferProcessTXN for applying change.
2069 */
2070 static inline void
2071 ReorderBufferApplyChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
2072 Relation relation, ReorderBufferChange *change,
2073 bool streaming)
2074 {
2075 if (streaming)
2076 rb->stream_change(rb, txn, relation, change);
2077 else
2078 rb->apply_change(rb, txn, relation, change);
2079 }
2080
2081 /*
2082 * Helper function for ReorderBufferProcessTXN for applying the truncate.
2083 */
2084 static inline void
2085 ReorderBufferApplyTruncate(ReorderBuffer *rb, ReorderBufferTXN *txn,
2086 int nrelations, Relation *relations,
2087 ReorderBufferChange *change, bool streaming)
2088 {
2089 if (streaming)
2090 rb->stream_truncate(rb, txn, nrelations, relations, change);
2091 else
2092 rb->apply_truncate(rb, txn, nrelations, relations, change);
2093 }
2094
2095 /*
2096 * Helper function for ReorderBufferProcessTXN for applying the message.
2097 */
2098 static inline void
2099 ReorderBufferApplyMessage(ReorderBuffer *rb, ReorderBufferTXN *txn,
2100 ReorderBufferChange *change, bool streaming)
2101 {
2102 if (streaming)
2103 rb->stream_message(rb, txn, change->lsn, true,
2104 change->data.msg.prefix,
2105 change->data.msg.message_size,
2106 change->data.msg.message);
2107 else
2108 rb->message(rb, txn, change->lsn, true,
2109 change->data.msg.prefix,
2110 change->data.msg.message_size,
2111 change->data.msg.message);
2112 }
2113
2114 /*
2115 * Function to store the command id and snapshot at the end of the current
2116 * stream so that we can reuse the same while sending the next stream.
2117 */
2118 static inline void
2119 ReorderBufferSaveTXNSnapshot(ReorderBuffer *rb, ReorderBufferTXN *txn,
2120 Snapshot snapshot_now, CommandId command_id)
2121 {
2122 txn->command_id = command_id;
2123
2124 /* Avoid copying if it's already copied. */
2125 if (snapshot_now->copied)
2126 txn->snapshot_now = snapshot_now;
2127 else
2128 txn->snapshot_now = ReorderBufferCopySnap(rb, snapshot_now,
2129 txn, command_id);
2130 }
2131
2132 /*
2133 * Mark the given transaction as streamed if it's a top-level transaction
2134 * or has changes.
2135 */
2136 static void
2137 ReorderBufferMaybeMarkTXNStreamed(ReorderBuffer *rb, ReorderBufferTXN *txn)
2138 {
2139 /*
2140 * The top-level transaction, is marked as streamed always, even if it
2141 * does not contain any changes (that is, when all the changes are in
2142 * subtransactions).
2143 *
2144 * For subtransactions, we only mark them as streamed when there are
2145 * changes in them.
2146 *
2147 * We do it this way because of aborts - we don't want to send aborts for
2148 * XIDs the downstream is not aware of. And of course, it always knows
2149 * about the top-level xact (we send the XID in all messages), but we
2150 * never stream XIDs of empty subxacts.
2151 */
2152 if (rbtxn_is_toptxn(txn) || (txn->nentries_mem != 0))
2153 txn->txn_flags |= RBTXN_IS_STREAMED;
2154 }
2155
2156 /*
2157 * Helper function for ReorderBufferProcessTXN to handle the concurrent
2158 * abort of the streaming transaction. This resets the TXN such that it
2159 * can be used to stream the remaining data of transaction being processed.
2160 * This can happen when the subtransaction is aborted and we still want to
2161 * continue processing the main or other subtransactions data.
2162 */
2163 static void
2164 ReorderBufferResetTXN(ReorderBuffer *rb, ReorderBufferTXN *txn,
2165 Snapshot snapshot_now,
2166 CommandId command_id,
2167 XLogRecPtr last_lsn)
2168 {
2169 /* Discard the changes that we just streamed */
2170 ReorderBufferTruncateTXN(rb, txn, rbtxn_is_prepared(txn));
2171
2172 /* Free all resources allocated for toast reconstruction */
2173 ReorderBufferToastReset(rb, txn);
2174
2175 /*
2176 * For the streaming case, stop the stream and remember the command ID and
2177 * snapshot for the streaming run.
2178 */
2179 if (rbtxn_is_streamed(txn))
2180 {
2181 rb->stream_stop(rb, txn, last_lsn);
2182 ReorderBufferSaveTXNSnapshot(rb, txn, snapshot_now, command_id);
2183 }
2184
2185 /* All changes must be deallocated */
2186 Assert(txn->size == 0);
2187 }
2188
2189 /*
2190 * Helper function for ReorderBufferReplay and ReorderBufferStreamTXN.
2191 *
2192 * Send data of a transaction (and its subtransactions) to the
2193 * output plugin. We iterate over the top and subtransactions (using a k-way
2194 * merge) and replay the changes in lsn order.
2195 *
2196 * If streaming is true then data will be sent using stream API.
2197 *
2198 * Note: "volatile" markers on some parameters are to avoid trouble with
2199 * PG_TRY inside the function.
2200 */
2201 static void
2202 ReorderBufferProcessTXN(ReorderBuffer *rb, ReorderBufferTXN *txn,
2203 XLogRecPtr commit_lsn,
2204 volatile Snapshot snapshot_now,
2205 volatile CommandId command_id,
2206 bool streaming)
2207 {
2208 bool using_subtxn;
2209 MemoryContext ccxt = CurrentMemoryContext;
2210 ReorderBufferIterTXNState *volatile iterstate = NULL;
2211 volatile XLogRecPtr prev_lsn = InvalidXLogRecPtr;
2212 ReorderBufferChange *volatile specinsert = NULL;
2213 volatile bool stream_started = false;
2214 ReorderBufferTXN *volatile curtxn = NULL;
2215
2216 /* build data to be able to lookup the CommandIds of catalog tuples */
2217 ReorderBufferBuildTupleCidHash(rb, txn);
2218
2219 /* setup the initial snapshot */
2220 SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
2221
2222 /*
2223 * Decoding needs access to syscaches et al., which in turn use
2224 * heavyweight locks and such. Thus we need to have enough state around to
2225 * keep track of those. The easiest way is to simply use a transaction
2226 * internally. That also allows us to easily enforce that nothing writes
2227 * to the database by checking for xid assignments.
2228 *
2229 * When we're called via the SQL SRF there's already a transaction
2230 * started, so start an explicit subtransaction there.
2231 */
2232 using_subtxn = IsTransactionOrTransactionBlock();
2233
2234 PG_TRY();
2235 {
2236 ReorderBufferChange *change;
2237 int changes_count = 0; /* used to accumulate the number of
2238 * changes */
2239
2240 if (using_subtxn)
2241 BeginInternalSubTransaction(streaming ? "stream" : "replay");
2242 else
2243 StartTransactionCommand();
2244
2245 /*
2246 * We only need to send begin/begin-prepare for non-streamed
2247 * transactions.
2248 */
2249 if (!streaming)
2250 {
2251 if (rbtxn_is_prepared(txn))
2252 rb->begin_prepare(rb, txn);
2253 else
2254 rb->begin(rb, txn);
2255 }
2256
2257 ReorderBufferIterTXNInit(rb, txn, &iterstate);
2258 while ((change = ReorderBufferIterTXNNext(rb, iterstate)) != NULL)
2259 {
2260 Relation relation = NULL;
2261 Oid reloid;
2262
2263 CHECK_FOR_INTERRUPTS();
2264
2265 /*
2266 * We can't call start stream callback before processing first
2267 * change.
2268 */
2269 if (prev_lsn == InvalidXLogRecPtr)
2270 {
2271 if (streaming)
2272 {
2273 txn->origin_id = change->origin_id;
2274 rb->stream_start(rb, txn, change->lsn);
2275 stream_started = true;
2276 }
2277 }
2278
2279 /*
2280 * Enforce correct ordering of changes, merged from multiple
2281 * subtransactions. The changes may have the same LSN due to
2282 * MULTI_INSERT xlog records.
2283 */
2284 Assert(prev_lsn == InvalidXLogRecPtr || prev_lsn <= change->lsn);
2285
2286 prev_lsn = change->lsn;
2287
2288 /*
2289 * Set the current xid to detect concurrent aborts. This is
2290 * required for the cases when we decode the changes before the
2291 * COMMIT record is processed.
2292 */
2293 if (streaming || rbtxn_is_prepared(change->txn))
2294 {
2295 curtxn = change->txn;
2296 SetupCheckXidLive(curtxn->xid);
2297 }
2298
2299 switch (change->action)
2300 {
2301 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
2302
2303 /*
2304 * Confirmation for speculative insertion arrived. Simply
2305 * use as a normal record. It'll be cleaned up at the end
2306 * of INSERT processing.
2307 */
2308 if (specinsert == NULL)
2309 elog(ERROR, "invalid ordering of speculative insertion changes");
2310 Assert(specinsert->data.tp.oldtuple == NULL);
2311 change = specinsert;
2312 change->action = REORDER_BUFFER_CHANGE_INSERT;
2313
2314 /* intentionally fall through */
2315 case REORDER_BUFFER_CHANGE_INSERT:
2316 case REORDER_BUFFER_CHANGE_UPDATE:
2317 case REORDER_BUFFER_CHANGE_DELETE:
2318 Assert(snapshot_now);
2319
2320 reloid = RelidByRelfilenumber(change->data.tp.rlocator.spcOid,
2321 change->data.tp.rlocator.relNumber);
2322
2323 /*
2324 * Mapped catalog tuple without data, emitted while
2325 * catalog table was in the process of being rewritten. We
2326 * can fail to look up the relfilenumber, because the
2327 * relmapper has no "historic" view, in contrast to the
2328 * normal catalog during decoding. Thus repeated rewrites
2329 * can cause a lookup failure. That's OK because we do not
2330 * decode catalog changes anyway. Normally such tuples
2331 * would be skipped over below, but we can't identify
2332 * whether the table should be logically logged without
2333 * mapping the relfilenumber to the oid.
2334 */
2335 if (reloid == InvalidOid &&
2336 change->data.tp.newtuple == NULL &&
2337 change->data.tp.oldtuple == NULL)
2338 goto change_done;
2339 else if (reloid == InvalidOid)
2340 elog(ERROR, "could not map filenumber \"%s\" to relation OID",
2341 relpathperm(change->data.tp.rlocator,
2342 MAIN_FORKNUM).str);
2343
2344 relation = RelationIdGetRelation(reloid);
2345
2346 if (!RelationIsValid(relation))
2347 elog(ERROR, "could not open relation with OID %u (for filenumber \"%s\")",
2348 reloid,
2349 relpathperm(change->data.tp.rlocator,
2350 MAIN_FORKNUM).str);
2351
2352 if (!RelationIsLogicallyLogged(relation))
2353 goto change_done;
2354
2355 /*
2356 * Ignore temporary heaps created during DDL unless the
2357 * plugin has asked for them.
2358 */
2359 if (relation->rd_rel->relrewrite && !rb->output_rewrites)
2360 goto change_done;
2361
2362 /*
2363 * For now ignore sequence changes entirely. Most of the
2364 * time they don't log changes using records we
2365 * understand, so it doesn't make sense to handle the few
2366 * cases we do.
2367 */
2368 if (relation->rd_rel->relkind == RELKIND_SEQUENCE)
2369 goto change_done;
2370
2371 /* user-triggered change */
2372 if (!IsToastRelation(relation))
2373 {
2374 ReorderBufferToastReplace(rb, txn, relation, change);
2375 ReorderBufferApplyChange(rb, txn, relation, change,
2376 streaming);
2377
2378 /*
2379 * Only clear reassembled toast chunks if we're sure
2380 * they're not required anymore. The creator of the
2381 * tuple tells us.
2382 */
2383 if (change->data.tp.clear_toast_afterwards)
2384 ReorderBufferToastReset(rb, txn);
2385 }
2386 /* we're not interested in toast deletions */
2387 else if (change->action == REORDER_BUFFER_CHANGE_INSERT)
2388 {
2389 /*
2390 * Need to reassemble the full toasted Datum in
2391 * memory, to ensure the chunks don't get reused till
2392 * we're done remove it from the list of this
2393 * transaction's changes. Otherwise it will get
2394 * freed/reused while restoring spooled data from
2395 * disk.
2396 */
2397 Assert(change->data.tp.newtuple != NULL);
2398
2399 dlist_delete(&change->node);
2400 ReorderBufferToastAppendChunk(rb, txn, relation,
2401 change);
2402 }
2403
2404 change_done:
2405
2406 /*
2407 * If speculative insertion was confirmed, the record
2408 * isn't needed anymore.
2409 */
2410 if (specinsert != NULL)
2411 {
2412 ReorderBufferFreeChange(rb, specinsert, true);
2413 specinsert = NULL;
2414 }
2415
2416 if (RelationIsValid(relation))
2417 {
2418 RelationClose(relation);
2419 relation = NULL;
2420 }
2421 break;
2422
2423 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
2424
2425 /*
2426 * Speculative insertions are dealt with by delaying the
2427 * processing of the insert until the confirmation record
2428 * arrives. For that we simply unlink the record from the
2429 * chain, so it does not get freed/reused while restoring
2430 * spooled data from disk.
2431 *
2432 * This is safe in the face of concurrent catalog changes
2433 * because the relevant relation can't be changed between
2434 * speculative insertion and confirmation due to
2435 * CheckTableNotInUse() and locking.
2436 */
2437
2438 /* clear out a pending (= failed) speculative insertion */
2439 if (specinsert != NULL)
2440 {
2441 ReorderBufferFreeChange(rb, specinsert, true);
2442 specinsert = NULL;
2443 }
2444
2445 /* and memorize the pending insertion */
2446 dlist_delete(&change->node);
2447 specinsert = change;
2448 break;
2449
2450 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT:
2451
2452 /*
2453 * Abort for speculative insertion arrived. So cleanup the
2454 * specinsert tuple and toast hash.
2455 *
2456 * Note that we get the spec abort change for each toast
2457 * entry but we need to perform the cleanup only the first
2458 * time we get it for the main table.
2459 */
2460 if (specinsert != NULL)
2461 {
2462 /*
2463 * We must clean the toast hash before processing a
2464 * completely new tuple to avoid confusion about the
2465 * previous tuple's toast chunks.
2466 */
2467 Assert(change->data.tp.clear_toast_afterwards);
2468 ReorderBufferToastReset(rb, txn);
2469
2470 /* We don't need this record anymore. */
2471 ReorderBufferFreeChange(rb, specinsert, true);
2472 specinsert = NULL;
2473 }
2474 break;
2475
2476 case REORDER_BUFFER_CHANGE_TRUNCATE:
2477 {
2478 int i;
2479 int nrelids = change->data.truncate.nrelids;
2480 int nrelations = 0;
2481 Relation *relations;
2482
2483 relations = palloc0(nrelids * sizeof(Relation));
2484 for (i = 0; i < nrelids; i++)
2485 {
2486 Oid relid = change->data.truncate.relids[i];
2487 Relation rel;
2488
2489 rel = RelationIdGetRelation(relid);
2490
2491 if (!RelationIsValid(rel))
2492 elog(ERROR, "could not open relation with OID %u", relid);
2493
2494 if (!RelationIsLogicallyLogged(rel))
2495 continue;
2496
2497 relations[nrelations++] = rel;
2498 }
2499
2500 /* Apply the truncate. */
2501 ReorderBufferApplyTruncate(rb, txn, nrelations,
2502 relations, change,
2503 streaming);
2504
2505 for (i = 0; i < nrelations; i++)
2506 RelationClose(relations[i]);
2507
2508 break;
2509 }
2510
2511 case REORDER_BUFFER_CHANGE_MESSAGE:
2512 ReorderBufferApplyMessage(rb, txn, change, streaming);
2513 break;
2514
2515 case REORDER_BUFFER_CHANGE_INVALIDATION:
2516 /* Execute the invalidation messages locally */
2517 ReorderBufferExecuteInvalidations(change->data.inval.ninvalidations,
2518 change->data.inval.invalidations);
2519 break;
2520
2521 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
2522 /* get rid of the old */
2523 TeardownHistoricSnapshot(false);
2524
2525 if (snapshot_now->copied)
2526 {
2527 ReorderBufferFreeSnap(rb, snapshot_now);
2528 snapshot_now =
2529 ReorderBufferCopySnap(rb, change->data.snapshot,
2530 txn, command_id);
2531 }
2532
2533 /*
2534 * Restored from disk, need to be careful not to double
2535 * free. We could introduce refcounting for that, but for
2536 * now this seems infrequent enough not to care.
2537 */
2538 else if (change->data.snapshot->copied)
2539 {
2540 snapshot_now =
2541 ReorderBufferCopySnap(rb, change->data.snapshot,
2542 txn, command_id);
2543 }
2544 else
2545 {
2546 snapshot_now = change->data.snapshot;
2547 }
2548
2549 /* and continue with the new one */
2550 SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
2551 break;
2552
2553 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
2554 Assert(change->data.command_id != InvalidCommandId);
2555
2556 if (command_id < change->data.command_id)
2557 {
2558 command_id = change->data.command_id;
2559
2560 if (!snapshot_now->copied)
2561 {
2562 /* we don't use the global one anymore */
2563 snapshot_now = ReorderBufferCopySnap(rb, snapshot_now,
2564 txn, command_id);
2565 }
2566
2567 snapshot_now->curcid = command_id;
2568
2569 TeardownHistoricSnapshot(false);
2570 SetupHistoricSnapshot(snapshot_now, txn->tuplecid_hash);
2571 }
2572
2573 break;
2574
2575 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
2576 elog(ERROR, "tuplecid value in changequeue");
2577 break;
2578 }
2579
2580 /*
2581 * It is possible that the data is not sent to downstream for a
2582 * long time either because the output plugin filtered it or there
2583 * is a DDL that generates a lot of data that is not processed by
2584 * the plugin. So, in such cases, the downstream can timeout. To
2585 * avoid that we try to send a keepalive message if required.
2586 * Trying to send a keepalive message after every change has some
2587 * overhead, but testing showed there is no noticeable overhead if
2588 * we do it after every ~100 changes.
2589 */
2590 #define CHANGES_THRESHOLD 100
2591
2592 if (++changes_count >= CHANGES_THRESHOLD)
2593 {
2594 rb->update_progress_txn(rb, txn, prev_lsn);
2595 changes_count = 0;
2596 }
2597 }
2598
2599 /* speculative insertion record must be freed by now */
2600 Assert(!specinsert);
2601
2602 /* clean up the iterator */
2603 ReorderBufferIterTXNFinish(rb, iterstate);
2604 iterstate = NULL;
2605
2606 /*
2607 * Update total transaction count and total bytes processed by the
2608 * transaction and its subtransactions. Ensure to not count the
2609 * streamed transaction multiple times.
2610 *
2611 * Note that the statistics computation has to be done after
2612 * ReorderBufferIterTXNFinish as it releases the serialized change
2613 * which we have already accounted in ReorderBufferIterTXNNext.
2614 */
2615 if (!rbtxn_is_streamed(txn))
2616 rb->totalTxns++;
2617
2618 rb->totalBytes += txn->total_size;
2619
2620 /*
2621 * Done with current changes, send the last message for this set of
2622 * changes depending upon streaming mode.
2623 */
2624 if (streaming)
2625 {
2626 if (stream_started)
2627 {
2628 rb->stream_stop(rb, txn, prev_lsn);
2629 stream_started = false;
2630 }
2631 }
2632 else
2633 {
2634 /*
2635 * Call either PREPARE (for two-phase transactions) or COMMIT (for
2636 * regular ones).
2637 */
2638 if (rbtxn_is_prepared(txn))
2639 {
2640 Assert(!rbtxn_sent_prepare(txn));
2641 rb->prepare(rb, txn, commit_lsn);
2642 txn->txn_flags |= RBTXN_SENT_PREPARE;
2643 }
2644 else
2645 rb->commit(rb, txn, commit_lsn);
2646 }
2647
2648 /* this is just a sanity check against bad output plugin behaviour */
2649 if (GetCurrentTransactionIdIfAny() != InvalidTransactionId)
2650 elog(ERROR, "output plugin used XID %u",
2651 GetCurrentTransactionId());
2652
2653 /*
2654 * Remember the command ID and snapshot for the next set of changes in
2655 * streaming mode.
2656 */
2657 if (streaming)
2658 ReorderBufferSaveTXNSnapshot(rb, txn, snapshot_now, command_id);
2659 else if (snapshot_now->copied)
2660 ReorderBufferFreeSnap(rb, snapshot_now);
2661
2662 /* cleanup */
2663 TeardownHistoricSnapshot(false);
2664
2665 /*
2666 * Aborting the current (sub-)transaction as a whole has the right
2667 * semantics. We want all locks acquired in here to be released, not
2668 * reassigned to the parent and we do not want any database access
2669 * have persistent effects.
2670 */
2671 AbortCurrentTransaction();
2672
2673 /* make sure there's no cache pollution */
2674 if (rbtxn_distr_inval_overflowed(txn))
2675 {
2676 Assert(txn->ninvalidations_distributed == 0);
2677 InvalidateSystemCaches();
2678 }
2679 else
2680 {
2681 ReorderBufferExecuteInvalidations(txn->ninvalidations, txn->invalidations);
2682 ReorderBufferExecuteInvalidations(txn->ninvalidations_distributed,
2683 txn->invalidations_distributed);
2684 }
2685
2686 if (using_subtxn)
2687 RollbackAndReleaseCurrentSubTransaction();
2688
2689 /*
2690 * We are here due to one of the four reasons: 1. Decoding an
2691 * in-progress txn. 2. Decoding a prepared txn. 3. Decoding of a
2692 * prepared txn that was (partially) streamed. 4. Decoding a committed
2693 * txn.
2694 *
2695 * For 1, we allow truncation of txn data by removing the changes
2696 * already streamed but still keeping other things like invalidations,
2697 * snapshot, and tuplecids. For 2 and 3, we indicate
2698 * ReorderBufferTruncateTXN to do more elaborate truncation of txn
2699 * data as the entire transaction has been decoded except for commit.
2700 * For 4, as the entire txn has been decoded, we can fully clean up
2701 * the TXN reorder buffer.
2702 */
2703 if (streaming || rbtxn_is_prepared(txn))
2704 {
2705 if (streaming)
2706 ReorderBufferMaybeMarkTXNStreamed(rb, txn);
2707
2708 ReorderBufferTruncateTXN(rb, txn, rbtxn_is_prepared(txn));
2709 /* Reset the CheckXidAlive */
2710 CheckXidAlive = InvalidTransactionId;
2711 }
2712 else
2713 ReorderBufferCleanupTXN(rb, txn);
2714 }
2715 PG_CATCH();
2716 {
2717 MemoryContext ecxt = MemoryContextSwitchTo(ccxt);
2718 ErrorData *errdata = CopyErrorData();
2719
2720 /* TODO: Encapsulate cleanup from the PG_TRY and PG_CATCH blocks */
2721 if (iterstate)
2722 ReorderBufferIterTXNFinish(rb, iterstate);
2723
2724 TeardownHistoricSnapshot(true);
2725
2726 /*
2727 * Force cache invalidation to happen outside of a valid transaction
2728 * to prevent catalog access as we just caught an error.
2729 */
2730 AbortCurrentTransaction();
2731
2732 /* make sure there's no cache pollution */
2733 if (rbtxn_distr_inval_overflowed(txn))
2734 {
2735 Assert(txn->ninvalidations_distributed == 0);
2736 InvalidateSystemCaches();
2737 }
2738 else
2739 {
2740 ReorderBufferExecuteInvalidations(txn->ninvalidations, txn->invalidations);
2741 ReorderBufferExecuteInvalidations(txn->ninvalidations_distributed,
2742 txn->invalidations_distributed);
2743 }
2744
2745 if (using_subtxn)
2746 RollbackAndReleaseCurrentSubTransaction();
2747
2748 /* Free the specinsert change before freeing the ReorderBufferTXN */
2749 if (specinsert != NULL)
2750 {
2751 ReorderBufferFreeChange(rb, specinsert, true);
2752 specinsert = NULL;
2753 }
2754
2755 /*
2756 * The error code ERRCODE_TRANSACTION_ROLLBACK indicates a concurrent
2757 * abort of the (sub)transaction we are streaming or preparing. We
2758 * need to do the cleanup and return gracefully on this error, see
2759 * SetupCheckXidLive.
2760 *
2761 * This error code can be thrown by one of the callbacks we call
2762 * during decoding so we need to ensure that we return gracefully only
2763 * when we are sending the data in streaming mode and the streaming is
2764 * not finished yet or when we are sending the data out on a PREPARE
2765 * during a two-phase commit.
2766 */
2767 if (errdata->sqlerrcode == ERRCODE_TRANSACTION_ROLLBACK &&
2768 (stream_started || rbtxn_is_prepared(txn)))
2769 {
2770 /* curtxn must be set for streaming or prepared transactions */
2771 Assert(curtxn);
2772
2773 /* Cleanup the temporary error state. */
2774 FlushErrorState();
2775 FreeErrorData(errdata);
2776 errdata = NULL;
2777
2778 /* Remember the transaction is aborted. */
2779 Assert(!rbtxn_is_committed(curtxn));
2780 curtxn->txn_flags |= RBTXN_IS_ABORTED;
2781
2782 /* Mark the transaction is streamed if appropriate */
2783 if (stream_started)
2784 ReorderBufferMaybeMarkTXNStreamed(rb, txn);
2785
2786 /* Reset the TXN so that it is allowed to stream remaining data. */
2787 ReorderBufferResetTXN(rb, txn, snapshot_now,
2788 command_id, prev_lsn);
2789 }
2790 else
2791 {
2792 ReorderBufferCleanupTXN(rb, txn);
2793 MemoryContextSwitchTo(ecxt);
2794 PG_RE_THROW();
2795 }
2796 }
2797 PG_END_TRY();
2798 }
2799
2800 /*
2801 * Perform the replay of a transaction and its non-aborted subtransactions.
2802 *
2803 * Subtransactions previously have to be processed by
2804 * ReorderBufferCommitChild(), even if previously assigned to the toplevel
2805 * transaction with ReorderBufferAssignChild.
2806 *
2807 * This interface is called once a prepare or toplevel commit is read for both
2808 * streamed as well as non-streamed transactions.
2809 */
2810 static void
2811 ReorderBufferReplay(ReorderBufferTXN *txn,
2812 ReorderBuffer *rb, TransactionId xid,
2813 XLogRecPtr commit_lsn, XLogRecPtr end_lsn,
2814 TimestampTz commit_time,
2815 RepOriginId origin_id, XLogRecPtr origin_lsn)
2816 {
2817 Snapshot snapshot_now;
2818 CommandId command_id = FirstCommandId;
2819
2820 txn->final_lsn = commit_lsn;
2821 txn->end_lsn = end_lsn;
2822 txn->xact_time.commit_time = commit_time;
2823 txn->origin_id = origin_id;
2824 txn->origin_lsn = origin_lsn;
2825
2826 /*
2827 * If the transaction was (partially) streamed, we need to commit it in a
2828 * 'streamed' way. That is, we first stream the remaining part of the
2829 * transaction, and then invoke stream_commit message.
2830 *
2831 * Called after everything (origin ID, LSN, ...) is stored in the
2832 * transaction to avoid passing that information directly.
2833 */
2834 if (rbtxn_is_streamed(txn))
2835 {
2836 ReorderBufferStreamCommit(rb, txn);
2837 return;
2838 }
2839
2840 /*
2841 * If this transaction has no snapshot, it didn't make any changes to the
2842 * database, so there's nothing to decode. Note that
2843 * ReorderBufferCommitChild will have transferred any snapshots from
2844 * subtransactions if there were any.
2845 */
2846 if (txn->base_snapshot == NULL)
2847 {
2848 Assert(txn->ninvalidations == 0);
2849
2850 /*
2851 * Removing this txn before a commit might result in the computation
2852 * of an incorrect restart_lsn. See SnapBuildProcessRunningXacts.
2853 */
2854 if (!rbtxn_is_prepared(txn))
2855 ReorderBufferCleanupTXN(rb, txn);
2856 return;
2857 }
2858
2859 snapshot_now = txn->base_snapshot;
2860
2861 /* Process and send the changes to output plugin. */
2862 ReorderBufferProcessTXN(rb, txn, commit_lsn, snapshot_now,
2863 command_id, false);
2864 }
2865
2866 /*
2867 * Commit a transaction.
2868 *
2869 * See comments for ReorderBufferReplay().
2870 */
2871 void
2872 ReorderBufferCommit(ReorderBuffer *rb, TransactionId xid,
2873 XLogRecPtr commit_lsn, XLogRecPtr end_lsn,
2874 TimestampTz commit_time,
2875 RepOriginId origin_id, XLogRecPtr origin_lsn)
2876 {
2877 ReorderBufferTXN *txn;
2878
2879 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
2880 false);
2881
2882 /* unknown transaction, nothing to replay */
2883 if (txn == NULL)
2884 return;
2885
2886 ReorderBufferReplay(txn, rb, xid, commit_lsn, end_lsn, commit_time,
2887 origin_id, origin_lsn);
2888 }
2889
2890 /*
2891 * Record the prepare information for a transaction. Also, mark the transaction
2892 * as a prepared transaction.
2893 */
2894 bool
2895 ReorderBufferRememberPrepareInfo(ReorderBuffer *rb, TransactionId xid,
2896 XLogRecPtr prepare_lsn, XLogRecPtr end_lsn,
2897 TimestampTz prepare_time,
2898 RepOriginId origin_id, XLogRecPtr origin_lsn)
2899 {
2900 ReorderBufferTXN *txn;
2901
2902 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr, false);
2903
2904 /* unknown transaction, nothing to do */
2905 if (txn == NULL)
2906 return false;
2907
2908 /*
2909 * Remember the prepare information to be later used by commit prepared in
2910 * case we skip doing prepare.
2911 */
2912 txn->final_lsn = prepare_lsn;
2913 txn->end_lsn = end_lsn;
2914 txn->xact_time.prepare_time = prepare_time;
2915 txn->origin_id = origin_id;
2916 txn->origin_lsn = origin_lsn;
2917
2918 /* Mark this transaction as a prepared transaction */
2919 Assert((txn->txn_flags & RBTXN_PREPARE_STATUS_MASK) == 0);
2920 txn->txn_flags |= RBTXN_IS_PREPARED;
2921
2922 return true;
2923 }
2924
2925 /* Remember that we have skipped prepare */
2926 void
2927 ReorderBufferSkipPrepare(ReorderBuffer *rb, TransactionId xid)
2928 {
2929 ReorderBufferTXN *txn;
2930
2931 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr, false);
2932
2933 /* unknown transaction, nothing to do */
2934 if (txn == NULL)
2935 return;
2936
2937 /* txn must have been marked as a prepared transaction */
2938 Assert((txn->txn_flags & RBTXN_PREPARE_STATUS_MASK) == RBTXN_IS_PREPARED);
2939 txn->txn_flags |= RBTXN_SKIPPED_PREPARE;
2940 }
2941
2942 /*
2943 * Prepare a two-phase transaction.
2944 *
2945 * See comments for ReorderBufferReplay().
2946 */
2947 void
2948 ReorderBufferPrepare(ReorderBuffer *rb, TransactionId xid,
2949 char *gid)
2950 {
2951 ReorderBufferTXN *txn;
2952
2953 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
2954 false);
2955
2956 /* unknown transaction, nothing to replay */
2957 if (txn == NULL)
2958 return;
2959
2960 /*
2961 * txn must have been marked as a prepared transaction and must have
2962 * neither been skipped nor sent a prepare. Also, the prepare info must
2963 * have been updated in it by now.
2964 */
2965 Assert((txn->txn_flags & RBTXN_PREPARE_STATUS_MASK) == RBTXN_IS_PREPARED);
2966 Assert(txn->final_lsn != InvalidXLogRecPtr);
2967
2968 txn->gid = pstrdup(gid);
2969
2970 ReorderBufferReplay(txn, rb, xid, txn->final_lsn, txn->end_lsn,
2971 txn->xact_time.prepare_time, txn->origin_id, txn->origin_lsn);
2972
2973 /*
2974 * Send a prepare if not already done so. The "not already sent" case can
2975 * occur if we have detected a concurrent abort while replaying the
2976 * non-streaming transaction; we still send the prepare so that later when
2977 * rollback prepared is decoded and sent, the downstream should be able to
2978 * rollback such a xact. See comments atop DecodePrepare.
2979 *
2980 * Skip this for a transaction that made no changes to the database (i.e.
2981 * has no base snapshot), as we haven't sent any changes for it. Such a
2982 * transaction is cleaned up without invoking the commit/rollback prepared
2983 * callbacks in ReorderBufferFinishPrepared().
2984 */
2985 if (!rbtxn_sent_prepare(txn) && txn->base_snapshot != NULL)
2986 {
2987 rb->prepare(rb, txn, txn->final_lsn);
2988 txn->txn_flags |= RBTXN_SENT_PREPARE;
2989 }
2990 }
2991
2992 /*
2993 * This is used to handle COMMIT/ROLLBACK PREPARED.
2994 */
2995 void
2996 ReorderBufferFinishPrepared(ReorderBuffer *rb, TransactionId xid,
2997 XLogRecPtr commit_lsn, XLogRecPtr end_lsn,
2998 XLogRecPtr two_phase_at,
2999 TimestampTz commit_time, RepOriginId origin_id,
3000 XLogRecPtr origin_lsn, char *gid, bool is_commit)
3001 {
3002 ReorderBufferTXN *txn;
3003 XLogRecPtr prepare_end_lsn;
3004 TimestampTz prepare_time;
3005
3006 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, commit_lsn, false);
3007
3008 /* unknown transaction, nothing to do */
3009 if (txn == NULL)
3010 return;
3011
3012 /*
3013 * By this time the txn has the prepare record information, remember it to
3014 * be later used for rollback.
3015 */
3016 prepare_end_lsn = txn->end_lsn;
3017 prepare_time = txn->xact_time.prepare_time;
3018
3019 /* add the gid in the txn */
3020 txn->gid = pstrdup(gid);
3021
3022 /*
3023 * It is possible that this transaction is not decoded at prepare time
3024 * either because by that time we didn't have a consistent snapshot, or
3025 * two_phase was not enabled, or it was decoded earlier but we have
3026 * restarted. We only need to send the prepare if it was not decoded
3027 * earlier. We don't need to decode the xact for aborts if it is not done
3028 * already.
3029 */
3030 if ((txn->final_lsn < two_phase_at) && is_commit)
3031 {
3032 /*
3033 * txn must have been marked as a prepared transaction and skipped but
3034 * not sent a prepare. Also, the prepare info must have been updated
3035 * in txn even if we skip prepare.
3036 */
3037 Assert((txn->txn_flags & RBTXN_PREPARE_STATUS_MASK) ==
3038 (RBTXN_IS_PREPARED | RBTXN_SKIPPED_PREPARE));
3039 Assert(txn->final_lsn != InvalidXLogRecPtr);
3040
3041 /*
3042 * By this time the txn has the prepare record information and it is
3043 * important to use that so that downstream gets the accurate
3044 * information. If instead, we have passed commit information here
3045 * then downstream can behave as it has already replayed commit
3046 * prepared after the restart.
3047 */
3048 ReorderBufferReplay(txn, rb, xid, txn->final_lsn, txn->end_lsn,
3049 txn->xact_time.prepare_time, txn->origin_id, txn->origin_lsn);
3050 }
3051
3052 /*
3053 * If this transaction has no snapshot, it didn't make any changes to the
3054 * database, so there's nothing to decode. Note that
3055 * ReorderBufferCommitChild will have transferred any snapshots from
3056 * subtransactions if there were any.
3057 */
3058 if (txn->base_snapshot == NULL)
3059 {
3060 Assert(txn->ninvalidations == 0);
3061 Assert(!rbtxn_sent_prepare(txn));
3062
3063 /*
3064 * Removing this txn before a commit might result in the computation
3065 * of an incorrect restart_lsn. See SnapBuildProcessRunningXacts.
3066 */
3067 ReorderBufferCleanupTXN(rb, txn);
3068 return;
3069 }
3070
3071 txn->final_lsn = commit_lsn;
3072 txn->end_lsn = end_lsn;
3073 txn->xact_time.commit_time = commit_time;
3074 txn->origin_id = origin_id;
3075 txn->origin_lsn = origin_lsn;
3076
3077 if (is_commit)
3078 rb->commit_prepared(rb, txn, commit_lsn);
3079 else
3080 rb->rollback_prepared(rb, txn, prepare_end_lsn, prepare_time);
3081
3082 /* cleanup: make sure there's no cache pollution */
3083 ReorderBufferExecuteInvalidations(txn->ninvalidations,
3084 txn->invalidations);
3085 ReorderBufferCleanupTXN(rb, txn);
3086 }
3087
3088 /*
3089 * Abort a transaction that possibly has previous changes. Needs to be first
3090 * called for subtransactions and then for the toplevel xid.
3091 *
3092 * NB: Transactions handled here have to have actively aborted (i.e. have
3093 * produced an abort record). Implicitly aborted transactions are handled via
3094 * ReorderBufferAbortOld(); transactions we're just not interested in, but
3095 * which have committed are handled in ReorderBufferForget().
3096 *
3097 * This function purges this transaction and its contents from memory and
3098 * disk.
3099 */
3100 void
3101 ReorderBufferAbort(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn,
3102 TimestampTz abort_time)
3103 {
3104 ReorderBufferTXN *txn;
3105
3106 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
3107 false);
3108
3109 /* unknown, nothing to remove */
3110 if (txn == NULL)
3111 return;
3112
3113 txn->xact_time.abort_time = abort_time;
3114
3115 /* For streamed transactions notify the remote node about the abort. */
3116 if (rbtxn_is_streamed(txn))
3117 {
3118 rb->stream_abort(rb, txn, lsn);
3119
3120 /*
3121 * We might have decoded changes for this transaction that could load
3122 * the cache as per the current transaction's view (consider DDL's
3123 * happened in this transaction). We don't want the decoding of future
3124 * transactions to use those cache entries so execute only the inval
3125 * messages in this transaction.
3126 */
3127 if (txn->ninvalidations > 0)
3128 ReorderBufferImmediateInvalidation(rb, txn->ninvalidations,
3129 txn->invalidations);
3130 }
3131
3132 /* cosmetic... */
3133 txn->final_lsn = lsn;
3134
3135 /* remove potential on-disk data, and deallocate */
3136 ReorderBufferCleanupTXN(rb, txn);
3137 }
3138
3139 /*
3140 * Abort all transactions that aren't actually running anymore because the
3141 * server restarted.
3142 *
3143 * NB: These really have to be transactions that have aborted due to a server
3144 * crash/immediate restart, as we don't deal with invalidations here.
3145 */
3146 void
3147 ReorderBufferAbortOld(ReorderBuffer *rb, TransactionId oldestRunningXid)
3148 {
3149 dlist_mutable_iter it;
3150
3151 /*
3152 * Iterate through all (potential) toplevel TXNs and abort all that are
3153 * older than what possibly can be running. Once we've found the first
3154 * that is alive we stop, there might be some that acquired an xid earlier
3155 * but started writing later, but it's unlikely and they will be cleaned
3156 * up in a later call to this function.
3157 */
3158 dlist_foreach_modify(it, &rb->toplevel_by_lsn)
3159 {
3160 ReorderBufferTXN *txn;
3161
3162 txn = dlist_container(ReorderBufferTXN, node, it.cur);
3163
3164 if (TransactionIdPrecedes(txn->xid, oldestRunningXid))
3165 {
3166 elog(DEBUG2, "aborting old transaction %u", txn->xid);
3167
3168 /* Notify the remote node about the crash/immediate restart. */
3169 if (rbtxn_is_streamed(txn))
3170 rb->stream_abort(rb, txn, InvalidXLogRecPtr);
3171
3172 /* remove potential on-disk data, and deallocate this tx */
3173 ReorderBufferCleanupTXN(rb, txn);
3174 }
3175 else
3176 return;
3177 }
3178 }
3179
3180 /*
3181 * Forget the contents of a transaction if we aren't interested in its
3182 * contents. Needs to be first called for subtransactions and then for the
3183 * toplevel xid.
3184 *
3185 * This is significantly different to ReorderBufferAbort() because
3186 * transactions that have committed need to be treated differently from aborted
3187 * ones since they may have modified the catalog.
3188 *
3189 * Note that this is only allowed to be called in the moment a transaction
3190 * commit has just been read, not earlier; otherwise later records referring
3191 * to this xid might re-create the transaction incompletely.
3192 */
3193 void
3194 ReorderBufferForget(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
3195 {
3196 ReorderBufferTXN *txn;
3197
3198 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
3199 false);
3200
3201 /* unknown, nothing to forget */
3202 if (txn == NULL)
3203 return;
3204
3205 /* this transaction mustn't be streamed */
3206 Assert(!rbtxn_is_streamed(txn));
3207
3208 /* cosmetic... */
3209 txn->final_lsn = lsn;
3210
3211 /*
3212 * Process only cache invalidation messages in this transaction if there
3213 * are any. Even if we're not interested in the transaction's contents, it
3214 * could have manipulated the catalog and we need to update the caches
3215 * according to that.
3216 */
3217 if (txn->base_snapshot != NULL && txn->ninvalidations > 0)
3218 ReorderBufferImmediateInvalidation(rb, txn->ninvalidations,
3219 txn->invalidations);
3220 else
3221 Assert(txn->ninvalidations == 0);
3222
3223 /* remove potential on-disk data, and deallocate */
3224 ReorderBufferCleanupTXN(rb, txn);
3225 }
3226
3227 /*
3228 * Invalidate cache for those transactions that need to be skipped just in case
3229 * catalogs were manipulated as part of the transaction.
3230 *
3231 * Note that this is a special-purpose function for prepared transactions where
3232 * we don't want to clean up the TXN even when we decide to skip it. See
3233 * DecodePrepare.
3234 */
3235 void
3236 ReorderBufferInvalidate(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
3237 {
3238 ReorderBufferTXN *txn;
3239
3240 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
3241 false);
3242
3243 /* unknown, nothing to do */
3244 if (txn == NULL)
3245 return;
3246
3247 /*
3248 * Process cache invalidation messages if there are any. Even if we're not
3249 * interested in the transaction's contents, it could have manipulated the
3250 * catalog and we need to update the caches according to that.
3251 */
3252 if (txn->base_snapshot != NULL && txn->ninvalidations > 0)
3253 ReorderBufferImmediateInvalidation(rb, txn->ninvalidations,
3254 txn->invalidations);
3255 else
3256 Assert(txn->ninvalidations == 0);
3257 }
3258
3259
3260 /*
3261 * Execute invalidations happening outside the context of a decoded
3262 * transaction. That currently happens either for xid-less commits
3263 * (cf. RecordTransactionCommit()) or for invalidations in uninteresting
3264 * transactions (via ReorderBufferForget()).
3265 */
3266 void
3267 ReorderBufferImmediateInvalidation(ReorderBuffer *rb, uint32 ninvalidations,
3268 SharedInvalidationMessage *invalidations)
3269 {
3270 bool use_subtxn = IsTransactionOrTransactionBlock();
3271 int i;
3272
3273 if (use_subtxn)
3274 BeginInternalSubTransaction("replay");
3275
3276 /*
3277 * Force invalidations to happen outside of a valid transaction - that way
3278 * entries will just be marked as invalid without accessing the catalog.
3279 * That's advantageous because we don't need to setup the full state
3280 * necessary for catalog access.
3281 */
3282 if (use_subtxn)
3283 AbortCurrentTransaction();
3284
3285 for (i = 0; i < ninvalidations; i++)
3286 LocalExecuteInvalidationMessage(&invalidations[i]);
3287
3288 if (use_subtxn)
3289 RollbackAndReleaseCurrentSubTransaction();
3290 }
3291
3292 /*
3293 * Tell reorderbuffer about an xid seen in the WAL stream. Has to be called at
3294 * least once for every xid in XLogRecord->xl_xid (other places in records
3295 * may, but do not have to be passed through here).
3296 *
3297 * Reorderbuffer keeps some data structures about transactions in LSN order,
3298 * for efficiency. To do that it has to know about when transactions are seen
3299 * first in the WAL. As many types of records are not actually interesting for
3300 * logical decoding, they do not necessarily pass through here.
3301 */
3302 void
3303 ReorderBufferProcessXid(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn)
3304 {
3305 /* many records won't have an xid assigned, centralize check here */
3306 if (xid != InvalidTransactionId)
3307 ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
3308 }
3309
3310 /*
3311 * Add a new snapshot to this transaction that may only used after lsn 'lsn'
3312 * because the previous snapshot doesn't describe the catalog correctly for
3313 * following rows.
3314 */
3315 void
3316 ReorderBufferAddSnapshot(ReorderBuffer *rb, TransactionId xid,
3317 XLogRecPtr lsn, Snapshot snap)
3318 {
3319 ReorderBufferChange *change = ReorderBufferAllocChange(rb);
3320
3321 change->data.snapshot = snap;
3322 change->action = REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT;
3323
3324 ReorderBufferQueueChange(rb, xid, lsn, change, false);
3325 }
3326
3327 /*
3328 * Set up the transaction's base snapshot.
3329 *
3330 * If we know that xid is a subtransaction, set the base snapshot on the
3331 * top-level transaction instead.
3332 */
3333 void
3334 ReorderBufferSetBaseSnapshot(ReorderBuffer *rb, TransactionId xid,
3335 XLogRecPtr lsn, Snapshot snap)
3336 {
3337 ReorderBufferTXN *txn;
3338 bool is_new;
3339
3340 Assert(snap != NULL);
3341
3342 /*
3343 * Fetch the transaction to operate on. If we know it's a subtransaction,
3344 * operate on its top-level transaction instead.
3345 */
3346 txn = ReorderBufferTXNByXid(rb, xid, true, &is_new, lsn, true);
3347 if (rbtxn_is_known_subxact(txn))
3348 txn = ReorderBufferTXNByXid(rb, txn->toplevel_xid, false,
3349 NULL, InvalidXLogRecPtr, false);
3350 Assert(txn->base_snapshot == NULL);
3351
3352 txn->base_snapshot = snap;
3353 txn->base_snapshot_lsn = lsn;
3354 dlist_push_tail(&rb->txns_by_base_snapshot_lsn, &txn->base_snapshot_node);
3355
3356 AssertTXNLsnOrder(rb);
3357 }
3358
3359 /*
3360 * Access the catalog with this CommandId at this point in the changestream.
3361 *
3362 * May only be called for command ids > 1
3363 */
3364 void
3365 ReorderBufferAddNewCommandId(ReorderBuffer *rb, TransactionId xid,
3366 XLogRecPtr lsn, CommandId cid)
3367 {
3368 ReorderBufferChange *change = ReorderBufferAllocChange(rb);
3369
3370 change->data.command_id = cid;
3371 change->action = REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID;
3372
3373 ReorderBufferQueueChange(rb, xid, lsn, change, false);
3374 }
3375
3376 /*
3377 * Update memory counters to account for the new or removed change.
3378 *
3379 * We update two counters - in the reorder buffer, and in the transaction
3380 * containing the change. The reorder buffer counter allows us to quickly
3381 * decide if we reached the memory limit, the transaction counter allows
3382 * us to quickly pick the largest transaction for eviction.
3383 *
3384 * Either txn or change must be non-NULL at least. We update the memory
3385 * counter of txn if it's non-NULL, otherwise change->txn.
3386 *
3387 * When streaming is enabled, we need to update the toplevel transaction
3388 * counters instead - we don't really care about subtransactions as we
3389 * can't stream them individually anyway, and we only pick toplevel
3390 * transactions for eviction. So only toplevel transactions matter.
3391 */
3392 static void
3393 ReorderBufferChangeMemoryUpdate(ReorderBuffer *rb,
3394 ReorderBufferChange *change,
3395 ReorderBufferTXN *txn,
3396 bool addition, Size sz)
3397 {
3398 ReorderBufferTXN *toptxn;
3399
3400 Assert(txn || change);
3401
3402 /*
3403 * Ignore tuple CID changes, because those are not evicted when reaching
3404 * memory limit. So we just don't count them, because it might easily
3405 * trigger a pointless attempt to spill.
3406 */
3407 if (change && change->action == REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID)
3408 return;
3409
3410 if (sz == 0)
3411 return;
3412
3413 if (txn == NULL)
3414 txn = change->txn;
3415 Assert(txn != NULL);
3416
3417 /*
3418 * Update the total size in top level as well. This is later used to
3419 * compute the decoding stats.
3420 */
3421 toptxn = rbtxn_get_toptxn(txn);
3422
3423 if (addition)
3424 {
3425 Size oldsize = txn->size;
3426
3427 txn->size += sz;
3428 rb->size += sz;
3429
3430 /* Update the total size in the top transaction. */
3431 toptxn->total_size += sz;
3432
3433 /* Update the max-heap */
3434 if (oldsize != 0)
3435 pairingheap_remove(rb->txn_heap, &txn->txn_node);
3436 pairingheap_add(rb->txn_heap, &txn->txn_node);
3437 }
3438 else
3439 {
3440 Assert((rb->size >= sz) && (txn->size >= sz));
3441 txn->size -= sz;
3442 rb->size -= sz;
3443
3444 /* Update the total size in the top transaction. */
3445 toptxn->total_size -= sz;
3446
3447 /* Update the max-heap */
3448 pairingheap_remove(rb->txn_heap, &txn->txn_node);
3449 if (txn->size != 0)
3450 pairingheap_add(rb->txn_heap, &txn->txn_node);
3451 }
3452
3453 Assert(txn->size <= rb->size);
3454 }
3455
3456 /*
3457 * Add new (relfilelocator, tid) -> (cmin, cmax) mappings.
3458 *
3459 * We do not include this change type in memory accounting, because we
3460 * keep CIDs in a separate list and do not evict them when reaching
3461 * the memory limit.
3462 */
3463 void
3464 ReorderBufferAddNewTupleCids(ReorderBuffer *rb, TransactionId xid,
3465 XLogRecPtr lsn, RelFileLocator locator,
3466 ItemPointerData tid, CommandId cmin,
3467 CommandId cmax, CommandId combocid)
3468 {
3469 ReorderBufferChange *change = ReorderBufferAllocChange(rb);
3470 ReorderBufferTXN *txn;
3471
3472 txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
3473
3474 change->data.tuplecid.locator = locator;
3475 change->data.tuplecid.tid = tid;
3476 change->data.tuplecid.cmin = cmin;
3477 change->data.tuplecid.cmax = cmax;
3478 change->data.tuplecid.combocid = combocid;
3479 change->lsn = lsn;
3480 change->txn = txn;
3481 change->action = REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID;
3482
3483 dlist_push_tail(&txn->tuplecids, &change->node);
3484 txn->ntuplecids++;
3485 }
3486
3487 /*
3488 * Add new invalidation messages to the reorder buffer queue.
3489 */
3490 static void
3491 ReorderBufferQueueInvalidations(ReorderBuffer *rb, TransactionId xid,
3492 XLogRecPtr lsn, Size nmsgs,
3493 SharedInvalidationMessage *msgs)
3494 {
3495 ReorderBufferChange *change;
3496
3497 change = ReorderBufferAllocChange(rb);
3498 change->action = REORDER_BUFFER_CHANGE_INVALIDATION;
3499 change->data.inval.ninvalidations = nmsgs;
3500 change->data.inval.invalidations = (SharedInvalidationMessage *)
3501 palloc(sizeof(SharedInvalidationMessage) * nmsgs);
3502 memcpy(change->data.inval.invalidations, msgs,
3503 sizeof(SharedInvalidationMessage) * nmsgs);
3504
3505 ReorderBufferQueueChange(rb, xid, lsn, change, false);
3506 }
3507
3508 /*
3509 * A helper function for ReorderBufferAddInvalidations() and
3510 * ReorderBufferAddDistributedInvalidations() to accumulate the invalidation
3511 * messages to the **invals_out.
3512 */
3513 static void
3514 ReorderBufferAccumulateInvalidations(SharedInvalidationMessage **invals_out,
3515 uint32 *ninvals_out,
3516 SharedInvalidationMessage *msgs_new,
3517 Size nmsgs_new)
3518 {
3519 if (*ninvals_out == 0)
3520 {
3521 *ninvals_out = nmsgs_new;
3522 *invals_out = (SharedInvalidationMessage *)
3523 palloc(sizeof(SharedInvalidationMessage) * nmsgs_new);
3524 memcpy(*invals_out, msgs_new, sizeof(SharedInvalidationMessage) * nmsgs_new);
3525 }
3526 else
3527 {
3528 /* Enlarge the array of inval messages */
3529 *invals_out = (SharedInvalidationMessage *)
3530 repalloc(*invals_out, sizeof(SharedInvalidationMessage) *
3531 (*ninvals_out + nmsgs_new));
3532 memcpy(*invals_out + *ninvals_out, msgs_new,
3533 nmsgs_new * sizeof(SharedInvalidationMessage));
3534 *ninvals_out += nmsgs_new;
3535 }
3536 }
3537
3538 /*
3539 * Accumulate the invalidations for executing them later.
3540 *
3541 * This needs to be called for each XLOG_XACT_INVALIDATIONS message and
3542 * accumulates all the invalidation messages in the toplevel transaction, if
3543 * available, otherwise in the current transaction, as well as in the form of
3544 * change in reorder buffer. We require to record it in form of the change
3545 * so that we can execute only the required invalidations instead of executing
3546 * all the invalidations on each CommandId increment. We also need to
3547 * accumulate these in the txn buffer because in some cases where we skip
3548 * processing the transaction (see ReorderBufferForget), we need to execute
3549 * all the invalidations together.
3550 */
3551 void
3552 ReorderBufferAddInvalidations(ReorderBuffer *rb, TransactionId xid,
3553 XLogRecPtr lsn, Size nmsgs,
3554 SharedInvalidationMessage *msgs)
3555 {
3556 ReorderBufferTXN *txn;
3557 MemoryContext oldcontext;
3558
3559 txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
3560
3561 oldcontext = MemoryContextSwitchTo(rb->context);
3562
3563 /*
3564 * Collect all the invalidations under the top transaction, if available,
3565 * so that we can execute them all together. See comments atop this
3566 * function.
3567 */
3568 txn = rbtxn_get_toptxn(txn);
3569
3570 Assert(nmsgs > 0);
3571
3572 ReorderBufferAccumulateInvalidations(&txn->invalidations,
3573 &txn->ninvalidations,
3574 msgs, nmsgs);
3575
3576 ReorderBufferQueueInvalidations(rb, xid, lsn, nmsgs, msgs);
3577
3578 MemoryContextSwitchTo(oldcontext);
3579 }
3580
3581 /*
3582 * Accumulate the invalidations distributed by other committed transactions
3583 * for executing them later.
3584 *
3585 * This function is similar to ReorderBufferAddInvalidations() but stores
3586 * the given inval messages to the txn->invalidations_distributed with the
3587 * overflow check.
3588 *
3589 * This needs to be called by committed transactions to distribute their
3590 * inval messages to in-progress transactions.
3591 */
3592 void
3593 ReorderBufferAddDistributedInvalidations(ReorderBuffer *rb, TransactionId xid,
3594 XLogRecPtr lsn, Size nmsgs,
3595 SharedInvalidationMessage *msgs)
3596 {
3597 ReorderBufferTXN *txn;
3598 MemoryContext oldcontext;
3599
3600 txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
3601
3602 oldcontext = MemoryContextSwitchTo(rb->context);
3603
3604 /*
3605 * Collect all the invalidations under the top transaction, if available,
3606 * so that we can execute them all together. See comments
3607 * ReorderBufferAddInvalidations.
3608 */
3609 txn = rbtxn_get_toptxn(txn);
3610
3611 Assert(nmsgs > 0);
3612
3613 if (!rbtxn_distr_inval_overflowed(txn))
3614 {
3615 /*
3616 * Check the transaction has enough space for storing distributed
3617 * invalidation messages.
3618 */
3619 if (txn->ninvalidations_distributed + nmsgs >= MAX_DISTR_INVAL_MSG_PER_TXN)
3620 {
3621 /*
3622 * Mark the invalidation message as overflowed and free up the
3623 * messages accumulated so far.
3624 */
3625 txn->txn_flags |= RBTXN_DISTR_INVAL_OVERFLOWED;
3626
3627 if (txn->invalidations_distributed)
3628 {
3629 pfree(txn->invalidations_distributed);
3630 txn->invalidations_distributed = NULL;
3631 txn->ninvalidations_distributed = 0;
3632 }
3633 }
3634 else
3635 ReorderBufferAccumulateInvalidations(&txn->invalidations_distributed,
3636 &txn->ninvalidations_distributed,
3637 msgs, nmsgs);
3638 }
3639
3640 /* Queue the invalidation messages into the transaction */
3641 ReorderBufferQueueInvalidations(rb, xid, lsn, nmsgs, msgs);
3642
3643 MemoryContextSwitchTo(oldcontext);
3644 }
3645
3646 /*
3647 * Apply all invalidations we know. Possibly we only need parts at this point
3648 * in the changestream but we don't know which those are.
3649 */
3650 static void
3651 ReorderBufferExecuteInvalidations(uint32 nmsgs, SharedInvalidationMessage *msgs)
3652 {
3653 int i;
3654
3655 for (i = 0; i < nmsgs; i++)
3656 LocalExecuteInvalidationMessage(&msgs[i]);
3657 }
3658
3659 /*
3660 * Mark a transaction as containing catalog changes
3661 */
3662 void
3663 ReorderBufferXidSetCatalogChanges(ReorderBuffer *rb, TransactionId xid,
3664 XLogRecPtr lsn)
3665 {
3666 ReorderBufferTXN *txn;
3667
3668 txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
3669
3670 if (!rbtxn_has_catalog_changes(txn))
3671 {
3672 txn->txn_flags |= RBTXN_HAS_CATALOG_CHANGES;
3673 dclist_push_tail(&rb->catchange_txns, &txn->catchange_node);
3674 }
3675
3676 /*
3677 * Mark top-level transaction as having catalog changes too if one of its
3678 * children has so that the ReorderBufferBuildTupleCidHash can
3679 * conveniently check just top-level transaction and decide whether to
3680 * build the hash table or not.
3681 */
3682 if (rbtxn_is_subtxn(txn))
3683 {
3684 ReorderBufferTXN *toptxn = rbtxn_get_toptxn(txn);
3685
3686 if (!rbtxn_has_catalog_changes(toptxn))
3687 {
3688 toptxn->txn_flags |= RBTXN_HAS_CATALOG_CHANGES;
3689 dclist_push_tail(&rb->catchange_txns, &toptxn->catchange_node);
3690 }
3691 }
3692 }
3693
3694 /*
3695 * Return palloc'ed array of the transactions that have changed catalogs.
3696 * The returned array is sorted in xidComparator order.
3697 *
3698 * The caller must free the returned array when done with it.
3699 */
3700 TransactionId *
3701 ReorderBufferGetCatalogChangesXacts(ReorderBuffer *rb)
3702 {
3703 dlist_iter iter;
3704 TransactionId *xids = NULL;
3705 size_t xcnt = 0;
3706
3707 /* Quick return if the list is empty */
3708 if (dclist_count(&rb->catchange_txns) == 0)
3709 return NULL;
3710
3711 /* Initialize XID array */
3712 xids = (TransactionId *) palloc(sizeof(TransactionId) *
3713 dclist_count(&rb->catchange_txns));
3714 dclist_foreach(iter, &rb->catchange_txns)
3715 {
3716 ReorderBufferTXN *txn = dclist_container(ReorderBufferTXN,
3717 catchange_node,
3718 iter.cur);
3719
3720 Assert(rbtxn_has_catalog_changes(txn));
3721
3722 xids[xcnt++] = txn->xid;
3723 }
3724
3725 qsort(xids, xcnt, sizeof(TransactionId), xidComparator);
3726
3727 Assert(xcnt == dclist_count(&rb->catchange_txns));
3728 return xids;
3729 }
3730
3731 /*
3732 * Query whether a transaction is already *known* to contain catalog
3733 * changes. This can be wrong until directly before the commit!
3734 */
3735 bool
3736 ReorderBufferXidHasCatalogChanges(ReorderBuffer *rb, TransactionId xid)
3737 {
3738 ReorderBufferTXN *txn;
3739
3740 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
3741 false);
3742 if (txn == NULL)
3743 return false;
3744
3745 return rbtxn_has_catalog_changes(txn);
3746 }
3747
3748 /*
3749 * ReorderBufferXidHasBaseSnapshot
3750 * Have we already set the base snapshot for the given txn/subtxn?
3751 */
3752 bool
3753 ReorderBufferXidHasBaseSnapshot(ReorderBuffer *rb, TransactionId xid)
3754 {
3755 ReorderBufferTXN *txn;
3756
3757 txn = ReorderBufferTXNByXid(rb, xid, false,
3758 NULL, InvalidXLogRecPtr, false);
3759
3760 /* transaction isn't known yet, ergo no snapshot */
3761 if (txn == NULL)
3762 return false;
3763
3764 /* a known subtxn? operate on top-level txn instead */
3765 if (rbtxn_is_known_subxact(txn))
3766 txn = ReorderBufferTXNByXid(rb, txn->toplevel_xid, false,
3767 NULL, InvalidXLogRecPtr, false);
3768
3769 return txn->base_snapshot != NULL;
3770 }
3771
3772
3773 /*
3774 * ---------------------------------------
3775 * Disk serialization support
3776 * ---------------------------------------
3777 */
3778
3779 /*
3780 * Ensure the IO buffer is >= sz.
3781 */
3782 static void
3783 ReorderBufferSerializeReserve(ReorderBuffer *rb, Size sz)
3784 {
3785 if (!rb->outbufsize)
3786 {
3787 rb->outbuf = MemoryContextAlloc(rb->context, sz);
3788 rb->outbufsize = sz;
3789 }
3790 else if (rb->outbufsize < sz)
3791 {
3792 rb->outbuf = repalloc(rb->outbuf, sz);
3793 rb->outbufsize = sz;
3794 }
3795 }
3796
3797
3798 /* Compare two transactions by size */
3799 static int
3800 ReorderBufferTXNSizeCompare(const pairingheap_node *a, const pairingheap_node *b, void *arg)
3801 {
3802 const ReorderBufferTXN *ta = pairingheap_const_container(ReorderBufferTXN, txn_node, a);
3803 const ReorderBufferTXN *tb = pairingheap_const_container(ReorderBufferTXN, txn_node, b);
3804
3805 if (ta->size < tb->size)
3806 return -1;
3807 if (ta->size > tb->size)
3808 return 1;
3809 return 0;
3810 }
3811
3812 /*
3813 * Find the largest transaction (toplevel or subxact) to evict (spill to disk).
3814 */
3815 static ReorderBufferTXN *
3816 ReorderBufferLargestTXN(ReorderBuffer *rb)
3817 {
3818 ReorderBufferTXN *largest;
3819
3820 /* Get the largest transaction from the max-heap */
3821 largest = pairingheap_container(ReorderBufferTXN, txn_node,
3822 pairingheap_first(rb->txn_heap));
3823
3824 Assert(largest);
3825 Assert(largest->size > 0);
3826 Assert(largest->size <= rb->size);
3827
3828 return largest;
3829 }
3830
3831 /*
3832 * Find the largest streamable (and non-aborted) toplevel transaction to evict
3833 * (by streaming).
3834 *
3835 * This can be seen as an optimized version of ReorderBufferLargestTXN, which
3836 * should give us the same transaction (because we don't update memory account
3837 * for subtransaction with streaming, so it's always 0). But we can simply
3838 * iterate over the limited number of toplevel transactions that have a base
3839 * snapshot. There is no use of selecting a transaction that doesn't have base
3840 * snapshot because we don't decode such transactions. Also, we do not select
3841 * the transaction which doesn't have any streamable change.
3842 *
3843 * Note that, we skip transactions that contain incomplete changes. There
3844 * is a scope of optimization here such that we can select the largest
3845 * transaction which has incomplete changes. But that will make the code and
3846 * design quite complex and that might not be worth the benefit. If we plan to
3847 * stream the transactions that contain incomplete changes then we need to
3848 * find a way to partially stream/truncate the transaction changes in-memory
3849 * and build a mechanism to partially truncate the spilled files.
3850 * Additionally, whenever we partially stream the transaction we need to
3851 * maintain the last streamed lsn and next time we need to restore from that
3852 * segment and the offset in WAL. As we stream the changes from the top
3853 * transaction and restore them subtransaction wise, we need to even remember
3854 * the subxact from where we streamed the last change.
3855 */
3856 static ReorderBufferTXN *
3857 ReorderBufferLargestStreamableTopTXN(ReorderBuffer *rb)
3858 {
3859 dlist_iter iter;
3860 Size largest_size = 0;
3861 ReorderBufferTXN *largest = NULL;
3862
3863 /* Find the largest top-level transaction having a base snapshot. */
3864 dlist_foreach(iter, &rb->txns_by_base_snapshot_lsn)
3865 {
3866 ReorderBufferTXN *txn;
3867
3868 txn = dlist_container(ReorderBufferTXN, base_snapshot_node, iter.cur);
3869
3870 /* must not be a subtxn */
3871 Assert(!rbtxn_is_known_subxact(txn));
3872 /* base_snapshot must be set */
3873 Assert(txn->base_snapshot != NULL);
3874
3875 /* Don't consider these kinds of transactions for eviction. */
3876 if (rbtxn_has_partial_change(txn) ||
3877 !rbtxn_has_streamable_change(txn) ||
3878 rbtxn_is_aborted(txn))
3879 continue;
3880
3881 /* Find the largest of the eviction candidates. */
3882 if ((largest == NULL || txn->total_size > largest_size) &&
3883 (txn->total_size > 0))
3884 {
3885 largest = txn;
3886 largest_size = txn->total_size;
3887 }
3888 }
3889
3890 return largest;
3891 }
3892
3893 /*
3894 * Check whether the logical_decoding_work_mem limit was reached, and if yes
3895 * pick the largest (sub)transaction at-a-time to evict and spill its changes to
3896 * disk or send to the output plugin until we reach under the memory limit.
3897 *
3898 * If debug_logical_replication_streaming is set to "immediate", stream or
3899 * serialize the changes immediately.
3900 *
3901 * XXX At this point we select the transactions until we reach under the memory
3902 * limit, but we might also adapt a more elaborate eviction strategy - for example
3903 * evicting enough transactions to free certain fraction (e.g. 50%) of the memory
3904 * limit.
3905 */
3906 static void
3907 ReorderBufferCheckMemoryLimit(ReorderBuffer *rb)
3908 {
3909 ReorderBufferTXN *txn;
3910
3911 /*
3912 * Bail out if debug_logical_replication_streaming is buffered and we
3913 * haven't exceeded the memory limit.
3914 */
3915 if (debug_logical_replication_streaming == DEBUG_LOGICAL_REP_STREAMING_BUFFERED &&
3916 rb->size < logical_decoding_work_mem * (Size) 1024)
3917 return;
3918
3919 /*
3920 * If debug_logical_replication_streaming is immediate, loop until there's
3921 * no change. Otherwise, loop until we reach under the memory limit. One
3922 * might think that just by evicting the largest (sub)transaction we will
3923 * come under the memory limit based on assumption that the selected
3924 * transaction is at least as large as the most recent change (which
3925 * caused us to go over the memory limit). However, that is not true
3926 * because a user can reduce the logical_decoding_work_mem to a smaller
3927 * value before the most recent change.
3928 */
3929 while (rb->size >= logical_decoding_work_mem * (Size) 1024 ||
3930 (debug_logical_replication_streaming == DEBUG_LOGICAL_REP_STREAMING_IMMEDIATE &&
3931 rb->size > 0))
3932 {
3933 /*
3934 * Pick the largest non-aborted transaction and evict it from memory
3935 * by streaming, if possible. Otherwise, spill to disk.
3936 */
3937 if (ReorderBufferCanStartStreaming(rb) &&
3938 (txn = ReorderBufferLargestStreamableTopTXN(rb)) != NULL)
3939 {
3940 /* we know there has to be one, because the size is not zero */
3941 Assert(txn && rbtxn_is_toptxn(txn));
3942 Assert(txn->total_size > 0);
3943 Assert(rb->size >= txn->total_size);
3944
3945 /* skip the transaction if aborted */
3946 if (ReorderBufferCheckAndTruncateAbortedTXN(rb, txn))
3947 continue;
3948
3949 ReorderBufferStreamTXN(rb, txn);
3950 }
3951 else
3952 {
3953 /*
3954 * Pick the largest transaction (or subtransaction) and evict it
3955 * from memory by serializing it to disk.
3956 */
3957 txn = ReorderBufferLargestTXN(rb);
3958
3959 /* we know there has to be one, because the size is not zero */
3960 Assert(txn);
3961 Assert(txn->size > 0);
3962 Assert(rb->size >= txn->size);
3963
3964 /* skip the transaction if aborted */
3965 if (ReorderBufferCheckAndTruncateAbortedTXN(rb, txn))
3966 continue;
3967
3968 ReorderBufferSerializeTXN(rb, txn);
3969 }
3970
3971 /*
3972 * After eviction, the transaction should have no entries in memory,
3973 * and should use 0 bytes for changes.
3974 */
3975 Assert(txn->size == 0);
3976 Assert(txn->nentries_mem == 0);
3977 }
3978
3979 /* We must be under the memory limit now. */
3980 Assert(rb->size < logical_decoding_work_mem * (Size) 1024);
3981 }
3982
3983 /*
3984 * Spill data of a large transaction (and its subtransactions) to disk.
3985 */
3986 static void
3987 ReorderBufferSerializeTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
3988 {
3989 dlist_iter subtxn_i;
3990 dlist_mutable_iter change_i;
3991 int fd = -1;
3992 XLogSegNo curOpenSegNo = 0;
3993 Size spilled = 0;
3994 Size size = txn->size;
3995
3996 elog(DEBUG2, "spill %u changes in XID %u to disk",
3997 (uint32) txn->nentries_mem, txn->xid);
3998
3999 /* do the same to all child TXs */
4000 dlist_foreach(subtxn_i, &txn->subtxns)
4001 {
4002 ReorderBufferTXN *subtxn;
4003
4004 subtxn = dlist_container(ReorderBufferTXN, node, subtxn_i.cur);
4005 ReorderBufferSerializeTXN(rb, subtxn);
4006 }
4007
4008 /* serialize changestream */
4009 dlist_foreach_modify(change_i, &txn->changes)
4010 {
4011 ReorderBufferChange *change;
4012
4013 change = dlist_container(ReorderBufferChange, node, change_i.cur);
4014
4015 /*
4016 * store in segment in which it belongs by start lsn, don't split over
4017 * multiple segments tho
4018 */
4019 if (fd == -1 ||
4020 !XLByteInSeg(change->lsn, curOpenSegNo, wal_segment_size))
4021 {
4022 char path[MAXPGPATH];
4023
4024 if (fd != -1)
4025 CloseTransientFile(fd);
4026
4027 XLByteToSeg(change->lsn, curOpenSegNo, wal_segment_size);
4028
4029 /*
4030 * No need to care about TLIs here, only used during a single run,
4031 * so each LSN only maps to a specific WAL record.
4032 */
4033 ReorderBufferSerializedPath(path, MyReplicationSlot, txn->xid,
4034 curOpenSegNo);
4035
4036 /* open segment, create it if necessary */
4037 fd = OpenTransientFile(path,
4038 O_CREAT | O_WRONLY | O_APPEND | PG_BINARY);
4039
4040 if (fd < 0)
4041 ereport(ERROR,
4042 (errcode_for_file_access(),
4043 errmsg("could not open file \"%s\": %m", path)));
4044 }
4045
4046 ReorderBufferSerializeChange(rb, txn, fd, change);
4047 dlist_delete(&change->node);
4048 ReorderBufferFreeChange(rb, change, false);
4049
4050 spilled++;
4051 }
4052
4053 /* Update the memory counter */
4054 ReorderBufferChangeMemoryUpdate(rb, NULL, txn, false, size);
4055
4056 /* update the statistics iff we have spilled anything */
4057 if (spilled)
4058 {
4059 rb->spillCount += 1;
4060 rb->spillBytes += size;
4061
4062 /* don't consider already serialized transactions */
4063 rb->spillTxns += (rbtxn_is_serialized(txn) || rbtxn_is_serialized_clear(txn)) ? 0 : 1;
4064
4065 /* update the decoding stats */
4066 UpdateDecodingStats((LogicalDecodingContext *) rb->private_data);
4067 }
4068
4069 Assert(spilled == txn->nentries_mem);
4070 Assert(dlist_is_empty(&txn->changes));
4071 txn->nentries_mem = 0;
4072 txn->txn_flags |= RBTXN_IS_SERIALIZED;
4073
4074 if (fd != -1)
4075 CloseTransientFile(fd);
4076 }
4077
4078 /*
4079 * Serialize individual change to disk.
4080 */
4081 static void
4082 ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
4083 int fd, ReorderBufferChange *change)
4084 {
4085 ReorderBufferDiskChange *ondisk;
4086 Size sz = sizeof(ReorderBufferDiskChange);
4087
4088 ReorderBufferSerializeReserve(rb, sz);
4089
4090 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
4091 memcpy(&ondisk->change, change, sizeof(ReorderBufferChange));
4092
4093 switch (change->action)
4094 {
4095 /* fall through these, they're all similar enough */
4096 case REORDER_BUFFER_CHANGE_INSERT:
4097 case REORDER_BUFFER_CHANGE_UPDATE:
4098 case REORDER_BUFFER_CHANGE_DELETE:
4099 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
4100 {
4101 char *data;
4102 HeapTuple oldtup,
4103 newtup;
4104 Size oldlen = 0;
4105 Size newlen = 0;
4106
4107 oldtup = change->data.tp.oldtuple;
4108 newtup = change->data.tp.newtuple;
4109
4110 if (oldtup)
4111 {
4112 sz += sizeof(HeapTupleData);
4113 oldlen = oldtup->t_len;
4114 sz += oldlen;
4115 }
4116
4117 if (newtup)
4118 {
4119 sz += sizeof(HeapTupleData);
4120 newlen = newtup->t_len;
4121 sz += newlen;
4122 }
4123
4124 /* make sure we have enough space */
4125 ReorderBufferSerializeReserve(rb, sz);
4126
4127 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
4128 /* might have been reallocated above */
4129 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
4130
4131 if (oldlen)
4132 {
4133 memcpy(data, oldtup, sizeof(HeapTupleData));
4134 data += sizeof(HeapTupleData);
4135
4136 memcpy(data, oldtup->t_data, oldlen);
4137 data += oldlen;
4138 }
4139
4140 if (newlen)
4141 {
4142 memcpy(data, newtup, sizeof(HeapTupleData));
4143 data += sizeof(HeapTupleData);
4144
4145 memcpy(data, newtup->t_data, newlen);
4146 data += newlen;
4147 }
4148 break;
4149 }
4150 case REORDER_BUFFER_CHANGE_MESSAGE:
4151 {
4152 char *data;
4153 Size prefix_size = strlen(change->data.msg.prefix) + 1;
4154
4155 sz += prefix_size + change->data.msg.message_size +
4156 sizeof(Size) + sizeof(Size);
4157 ReorderBufferSerializeReserve(rb, sz);
4158
4159 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
4160
4161 /* might have been reallocated above */
4162 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
4163
4164 /* write the prefix including the size */
4165 memcpy(data, &prefix_size, sizeof(Size));
4166 data += sizeof(Size);
4167 memcpy(data, change->data.msg.prefix,
4168 prefix_size);
4169 data += prefix_size;
4170
4171 /* write the message including the size */
4172 memcpy(data, &change->data.msg.message_size, sizeof(Size));
4173 data += sizeof(Size);
4174 memcpy(data, change->data.msg.message,
4175 change->data.msg.message_size);
4176 data += change->data.msg.message_size;
4177
4178 break;
4179 }
4180 case REORDER_BUFFER_CHANGE_INVALIDATION:
4181 {
4182 char *data;
4183 Size inval_size = sizeof(SharedInvalidationMessage) *
4184 change->data.inval.ninvalidations;
4185
4186 sz += inval_size;
4187
4188 ReorderBufferSerializeReserve(rb, sz);
4189 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
4190
4191 /* might have been reallocated above */
4192 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
4193 memcpy(data, change->data.inval.invalidations, inval_size);
4194 data += inval_size;
4195
4196 break;
4197 }
4198 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
4199 {
4200 Snapshot snap;
4201 char *data;
4202
4203 snap = change->data.snapshot;
4204
4205 sz += sizeof(SnapshotData) +
4206 sizeof(TransactionId) * snap->xcnt +
4207 sizeof(TransactionId) * snap->subxcnt;
4208
4209 /* make sure we have enough space */
4210 ReorderBufferSerializeReserve(rb, sz);
4211 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
4212 /* might have been reallocated above */
4213 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
4214
4215 memcpy(data, snap, sizeof(SnapshotData));
4216 data += sizeof(SnapshotData);
4217
4218 if (snap->xcnt)
4219 {
4220 memcpy(data, snap->xip,
4221 sizeof(TransactionId) * snap->xcnt);
4222 data += sizeof(TransactionId) * snap->xcnt;
4223 }
4224
4225 if (snap->subxcnt)
4226 {
4227 memcpy(data, snap->subxip,
4228 sizeof(TransactionId) * snap->subxcnt);
4229 data += sizeof(TransactionId) * snap->subxcnt;
4230 }
4231 break;
4232 }
4233 case REORDER_BUFFER_CHANGE_TRUNCATE:
4234 {
4235 Size size;
4236 char *data;
4237
4238 /* account for the OIDs of truncated relations */
4239 size = sizeof(Oid) * change->data.truncate.nrelids;
4240 sz += size;
4241
4242 /* make sure we have enough space */
4243 ReorderBufferSerializeReserve(rb, sz);
4244
4245 data = ((char *) rb->outbuf) + sizeof(ReorderBufferDiskChange);
4246 /* might have been reallocated above */
4247 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
4248
4249 memcpy(data, change->data.truncate.relids, size);
4250 data += size;
4251
4252 break;
4253 }
4254 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
4255 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT:
4256 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
4257 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
4258 /* ReorderBufferChange contains everything important */
4259 break;
4260 }
4261
4262 ondisk->size = sz;
4263
4264 errno = 0;
4265 pgstat_report_wait_start(WAIT_EVENT_REORDER_BUFFER_WRITE);
4266 if (write(fd, rb->outbuf, ondisk->size) != ondisk->size)
4267 {
4268 int save_errno = errno;
4269
4270 CloseTransientFile(fd);
4271
4272 /* if write didn't set errno, assume problem is no disk space */
4273 errno = save_errno ? save_errno : ENOSPC;
4274 ereport(ERROR,
4275 (errcode_for_file_access(),
4276 errmsg("could not write to data file for XID %u: %m",
4277 txn->xid)));
4278 }
4279 pgstat_report_wait_end();
4280
4281 /*
4282 * Keep the transaction's final_lsn up to date with each change we send to
4283 * disk, so that ReorderBufferRestoreCleanup works correctly. (We used to
4284 * only do this on commit and abort records, but that doesn't work if a
4285 * system crash leaves a transaction without its abort record).
4286 *
4287 * Make sure not to move it backwards.
4288 */
4289 if (txn->final_lsn < change->lsn)
4290 txn->final_lsn = change->lsn;
4291
4292 Assert(ondisk->change.action == change->action);
4293 }
4294
4295 /* Returns true, if the output plugin supports streaming, false, otherwise. */
4296 static inline bool
4297 ReorderBufferCanStream(ReorderBuffer *rb)
4298 {
4299 LogicalDecodingContext *ctx = rb->private_data;
4300
4301 return ctx->streaming;
4302 }
4303
4304 /* Returns true, if the streaming can be started now, false, otherwise. */
4305 static inline bool
4306 ReorderBufferCanStartStreaming(ReorderBuffer *rb)
4307 {
4308 LogicalDecodingContext *ctx = rb->private_data;
4309 SnapBuild *builder = ctx->snapshot_builder;
4310
4311 /* We can't start streaming unless a consistent state is reached. */
4312 if (SnapBuildCurrentState(builder) < SNAPBUILD_CONSISTENT)
4313 return false;
4314
4315 /*
4316 * We can't start streaming immediately even if the streaming is enabled
4317 * because we previously decoded this transaction and now just are
4318 * restarting.
4319 */
4320 if (ReorderBufferCanStream(rb) &&
4321 !SnapBuildXactNeedsSkip(builder, ctx->reader->ReadRecPtr))
4322 return true;
4323
4324 return false;
4325 }
4326
4327 /*
4328 * Send data of a large transaction (and its subtransactions) to the
4329 * output plugin, but using the stream API.
4330 */
4331 static void
4332 ReorderBufferStreamTXN(ReorderBuffer *rb, ReorderBufferTXN *txn)
4333 {
4334 Snapshot snapshot_now;
4335 CommandId command_id;
4336 Size stream_bytes;
4337 bool txn_is_streamed;
4338
4339 /* We can never reach here for a subtransaction. */
4340 Assert(rbtxn_is_toptxn(txn));
4341
4342 /*
4343 * We can't make any assumptions about base snapshot here, similar to what
4344 * ReorderBufferCommit() does. That relies on base_snapshot getting
4345 * transferred from subxact in ReorderBufferCommitChild(), but that was
4346 * not yet called as the transaction is in-progress.
4347 *
4348 * So just walk the subxacts and use the same logic here. But we only need
4349 * to do that once, when the transaction is streamed for the first time.
4350 * After that we need to reuse the snapshot from the previous run.
4351 *
4352 * Unlike DecodeCommit which adds xids of all the subtransactions in
4353 * snapshot's xip array via SnapBuildCommitTxn, we can't do that here but
4354 * we do add them to subxip array instead via ReorderBufferCopySnap. This
4355 * allows the catalog changes made in subtransactions decoded till now to
4356 * be visible.
4357 */
4358 if (txn->snapshot_now == NULL)
4359 {
4360 dlist_iter subxact_i;
4361
4362 /* make sure this transaction is streamed for the first time */
4363 Assert(!rbtxn_is_streamed(txn));
4364
4365 /* at the beginning we should have invalid command ID */
4366 Assert(txn->command_id == InvalidCommandId);
4367
4368 dlist_foreach(subxact_i, &txn->subtxns)
4369 {
4370 ReorderBufferTXN *subtxn;
4371
4372 subtxn = dlist_container(ReorderBufferTXN, node, subxact_i.cur);
4373 ReorderBufferTransferSnapToParent(txn, subtxn);
4374 }
4375
4376 /*
4377 * If this transaction has no snapshot, it didn't make any changes to
4378 * the database till now, so there's nothing to decode.
4379 */
4380 if (txn->base_snapshot == NULL)
4381 {
4382 Assert(txn->ninvalidations == 0);
4383 return;
4384 }
4385
4386 command_id = FirstCommandId;
4387 snapshot_now = ReorderBufferCopySnap(rb, txn->base_snapshot,
4388 txn, command_id);
4389 }
4390 else
4391 {
4392 /* the transaction must have been already streamed */
4393 Assert(rbtxn_is_streamed(txn));
4394
4395 /*
4396 * Nah, we already have snapshot from the previous streaming run. We
4397 * assume new subxacts can't move the LSN backwards, and so can't beat
4398 * the LSN condition in the previous branch (so no need to walk
4399 * through subxacts again). In fact, we must not do that as we may be
4400 * using snapshot half-way through the subxact.
4401 */
4402 command_id = txn->command_id;
4403
4404 /*
4405 * We can't use txn->snapshot_now directly because after the last
4406 * streaming run, we might have got some new sub-transactions. So we
4407 * need to add them to the snapshot.
4408 */
4409 snapshot_now = ReorderBufferCopySnap(rb, txn->snapshot_now,
4410 txn, command_id);
4411
4412 /* Free the previously copied snapshot. */
4413 Assert(txn->snapshot_now->copied);
4414 ReorderBufferFreeSnap(rb, txn->snapshot_now);
4415 txn->snapshot_now = NULL;
4416 }
4417
4418 /*
4419 * Remember this information to be used later to update stats. We can't
4420 * update the stats here as an error while processing the changes would
4421 * lead to the accumulation of stats even though we haven't streamed all
4422 * the changes.
4423 */
4424 txn_is_streamed = rbtxn_is_streamed(txn);
4425 stream_bytes = txn->total_size;
4426
4427 /* Process and send the changes to output plugin. */
4428 ReorderBufferProcessTXN(rb, txn, InvalidXLogRecPtr, snapshot_now,
4429 command_id, true);
4430
4431 rb->streamCount += 1;
4432 rb->streamBytes += stream_bytes;
4433
4434 /* Don't consider already streamed transaction. */
4435 rb->streamTxns += (txn_is_streamed) ? 0 : 1;
4436
4437 /* update the decoding stats */
4438 UpdateDecodingStats((LogicalDecodingContext *) rb->private_data);
4439
4440 Assert(dlist_is_empty(&txn->changes));
4441 Assert(txn->nentries == 0);
4442 Assert(txn->nentries_mem == 0);
4443 }
4444
4445 /*
4446 * Size of a change in memory.
4447 */
4448 static Size
4449 ReorderBufferChangeSize(ReorderBufferChange *change)
4450 {
4451 Size sz = sizeof(ReorderBufferChange);
4452
4453 switch (change->action)
4454 {
4455 /* fall through these, they're all similar enough */
4456 case REORDER_BUFFER_CHANGE_INSERT:
4457 case REORDER_BUFFER_CHANGE_UPDATE:
4458 case REORDER_BUFFER_CHANGE_DELETE:
4459 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
4460 {
4461 HeapTuple oldtup,
4462 newtup;
4463 Size oldlen = 0;
4464 Size newlen = 0;
4465
4466 oldtup = change->data.tp.oldtuple;
4467 newtup = change->data.tp.newtuple;
4468
4469 if (oldtup)
4470 {
4471 sz += sizeof(HeapTupleData);
4472 oldlen = oldtup->t_len;
4473 sz += oldlen;
4474 }
4475
4476 if (newtup)
4477 {
4478 sz += sizeof(HeapTupleData);
4479 newlen = newtup->t_len;
4480 sz += newlen;
4481 }
4482
4483 break;
4484 }
4485 case REORDER_BUFFER_CHANGE_MESSAGE:
4486 {
4487 Size prefix_size = strlen(change->data.msg.prefix) + 1;
4488
4489 sz += prefix_size + change->data.msg.message_size +
4490 sizeof(Size) + sizeof(Size);
4491
4492 break;
4493 }
4494 case REORDER_BUFFER_CHANGE_INVALIDATION:
4495 {
4496 sz += sizeof(SharedInvalidationMessage) *
4497 change->data.inval.ninvalidations;
4498 break;
4499 }
4500 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
4501 {
4502 Snapshot snap;
4503
4504 snap = change->data.snapshot;
4505
4506 sz += sizeof(SnapshotData) +
4507 sizeof(TransactionId) * snap->xcnt +
4508 sizeof(TransactionId) * snap->subxcnt;
4509
4510 break;
4511 }
4512 case REORDER_BUFFER_CHANGE_TRUNCATE:
4513 {
4514 sz += sizeof(Oid) * change->data.truncate.nrelids;
4515
4516 break;
4517 }
4518 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
4519 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT:
4520 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
4521 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
4522 /* ReorderBufferChange contains everything important */
4523 break;
4524 }
4525
4526 return sz;
4527 }
4528
4529
4530 /*
4531 * Restore a number of changes spilled to disk back into memory.
4532 */
4533 static Size
4534 ReorderBufferRestoreChanges(ReorderBuffer *rb, ReorderBufferTXN *txn,
4535 TXNEntryFile *file, XLogSegNo *segno)
4536 {
4537 Size restored = 0;
4538 XLogSegNo last_segno;
4539 dlist_mutable_iter cleanup_iter;
4540 File *fd = &file->vfd;
4541
4542 Assert(txn->first_lsn != InvalidXLogRecPtr);
4543 Assert(txn->final_lsn != InvalidXLogRecPtr);
4544
4545 /* free current entries, so we have memory for more */
4546 dlist_foreach_modify(cleanup_iter, &txn->changes)
4547 {
4548 ReorderBufferChange *cleanup =
4549 dlist_container(ReorderBufferChange, node, cleanup_iter.cur);
4550
4551 dlist_delete(&cleanup->node);
4552 ReorderBufferFreeChange(rb, cleanup, true);
4553 }
4554 txn->nentries_mem = 0;
4555 Assert(dlist_is_empty(&txn->changes));
4556
4557 XLByteToSeg(txn->final_lsn, last_segno, wal_segment_size);
4558
4559 while (restored < max_changes_in_memory && *segno <= last_segno)
4560 {
4561 int readBytes;
4562 ReorderBufferDiskChange *ondisk;
4563
4564 CHECK_FOR_INTERRUPTS();
4565
4566 if (*fd == -1)
4567 {
4568 char path[MAXPGPATH];
4569
4570 /* first time in */
4571 if (*segno == 0)
4572 XLByteToSeg(txn->first_lsn, *segno, wal_segment_size);
4573
4574 Assert(*segno != 0 || dlist_is_empty(&txn->changes));
4575
4576 /*
4577 * No need to care about TLIs here, only used during a single run,
4578 * so each LSN only maps to a specific WAL record.
4579 */
4580 ReorderBufferSerializedPath(path, MyReplicationSlot, txn->xid,
4581 *segno);
4582
4583 *fd = PathNameOpenFile(path, O_RDONLY | PG_BINARY);
4584
4585 /* No harm in resetting the offset even in case of failure */
4586 file->curOffset = 0;
4587
4588 if (*fd < 0 && errno == ENOENT)
4589 {
4590 *fd = -1;
4591 (*segno)++;
4592 continue;
4593 }
4594 else if (*fd < 0)
4595 ereport(ERROR,
4596 (errcode_for_file_access(),
4597 errmsg("could not open file \"%s\": %m",
4598 path)));
4599 }
4600
4601 /*
4602 * Read the statically sized part of a change which has information
4603 * about the total size. If we couldn't read a record, we're at the
4604 * end of this file.
4605 */
4606 ReorderBufferSerializeReserve(rb, sizeof(ReorderBufferDiskChange));
4607 readBytes = FileRead(file->vfd, rb->outbuf,
4608 sizeof(ReorderBufferDiskChange),
4609 file->curOffset, WAIT_EVENT_REORDER_BUFFER_READ);
4610
4611 /* eof */
4612 if (readBytes == 0)
4613 {
4614 FileClose(*fd);
4615 *fd = -1;
4616 (*segno)++;
4617 continue;
4618 }
4619 else if (readBytes < 0)
4620 ereport(ERROR,
4621 (errcode_for_file_access(),
4622 errmsg("could not read from reorderbuffer spill file: %m")));
4623 else if (readBytes != sizeof(ReorderBufferDiskChange))
4624 ereport(ERROR,
4625 (errcode_for_file_access(),
4626 errmsg("could not read from reorderbuffer spill file: read %d instead of %u bytes",
4627 readBytes,
4628 (uint32) sizeof(ReorderBufferDiskChange))));
4629
4630 file->curOffset += readBytes;
4631
4632 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
4633
4634 ReorderBufferSerializeReserve(rb,
4635 sizeof(ReorderBufferDiskChange) + ondisk->size);
4636 ondisk = (ReorderBufferDiskChange *) rb->outbuf;
4637
4638 readBytes = FileRead(file->vfd,
4639 rb->outbuf + sizeof(ReorderBufferDiskChange),
4640 ondisk->size - sizeof(ReorderBufferDiskChange),
4641 file->curOffset,
4642 WAIT_EVENT_REORDER_BUFFER_READ);
4643
4644 if (readBytes < 0)
4645 ereport(ERROR,
4646 (errcode_for_file_access(),
4647 errmsg("could not read from reorderbuffer spill file: %m")));
4648 else if (readBytes != ondisk->size - sizeof(ReorderBufferDiskChange))
4649 ereport(ERROR,
4650 (errcode_for_file_access(),
4651 errmsg("could not read from reorderbuffer spill file: read %d instead of %u bytes",
4652 readBytes,
4653 (uint32) (ondisk->size - sizeof(ReorderBufferDiskChange)))));
4654
4655 file->curOffset += readBytes;
4656
4657 /*
4658 * ok, read a full change from disk, now restore it into proper
4659 * in-memory format
4660 */
4661 ReorderBufferRestoreChange(rb, txn, rb->outbuf);
4662 restored++;
4663 }
4664
4665 return restored;
4666 }
4667
4668 /*
4669 * Convert change from its on-disk format to in-memory format and queue it onto
4670 * the TXN's ->changes list.
4671 *
4672 * Note: although "data" is declared char*, at entry it points to a
4673 * maxalign'd buffer, making it safe in most of this function to assume
4674 * that the pointed-to data is suitably aligned for direct access.
4675 */
4676 static void
4677 ReorderBufferRestoreChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
4678 char *data)
4679 {
4680 ReorderBufferDiskChange *ondisk;
4681 ReorderBufferChange *change;
4682
4683 ondisk = (ReorderBufferDiskChange *) data;
4684
4685 change = ReorderBufferAllocChange(rb);
4686
4687 /* copy static part */
4688 memcpy(change, &ondisk->change, sizeof(ReorderBufferChange));
4689
4690 data += sizeof(ReorderBufferDiskChange);
4691
4692 /* restore individual stuff */
4693 switch (change->action)
4694 {
4695 /* fall through these, they're all similar enough */
4696 case REORDER_BUFFER_CHANGE_INSERT:
4697 case REORDER_BUFFER_CHANGE_UPDATE:
4698 case REORDER_BUFFER_CHANGE_DELETE:
4699 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT:
4700 if (change->data.tp.oldtuple)
4701 {
4702 uint32 tuplelen = ((HeapTuple) data)->t_len;
4703
4704 change->data.tp.oldtuple =
4705 ReorderBufferAllocTupleBuf(rb, tuplelen - SizeofHeapTupleHeader);
4706
4707 /* restore ->tuple */
4708 memcpy(change->data.tp.oldtuple, data,
4709 sizeof(HeapTupleData));
4710 data += sizeof(HeapTupleData);
4711
4712 /* reset t_data pointer into the new tuplebuf */
4713 change->data.tp.oldtuple->t_data =
4714 (HeapTupleHeader) ((char *) change->data.tp.oldtuple + HEAPTUPLESIZE);
4715
4716 /* restore tuple data itself */
4717 memcpy(change->data.tp.oldtuple->t_data, data, tuplelen);
4718 data += tuplelen;
4719 }
4720
4721 if (change->data.tp.newtuple)
4722 {
4723 /* here, data might not be suitably aligned! */
4724 uint32 tuplelen;
4725
4726 memcpy(&tuplelen, data + offsetof(HeapTupleData, t_len),
4727 sizeof(uint32));
4728
4729 change->data.tp.newtuple =
4730 ReorderBufferAllocTupleBuf(rb, tuplelen - SizeofHeapTupleHeader);
4731
4732 /* restore ->tuple */
4733 memcpy(change->data.tp.newtuple, data,
4734 sizeof(HeapTupleData));
4735 data += sizeof(HeapTupleData);
4736
4737 /* reset t_data pointer into the new tuplebuf */
4738 change->data.tp.newtuple->t_data =
4739 (HeapTupleHeader) ((char *) change->data.tp.newtuple + HEAPTUPLESIZE);
4740
4741 /* restore tuple data itself */
4742 memcpy(change->data.tp.newtuple->t_data, data, tuplelen);
4743 data += tuplelen;
4744 }
4745
4746 break;
4747 case REORDER_BUFFER_CHANGE_MESSAGE:
4748 {
4749 Size prefix_size;
4750
4751 /* read prefix */
4752 memcpy(&prefix_size, data, sizeof(Size));
4753 data += sizeof(Size);
4754 change->data.msg.prefix = MemoryContextAlloc(rb->context,
4755 prefix_size);
4756 memcpy(change->data.msg.prefix, data, prefix_size);
4757 Assert(change->data.msg.prefix[prefix_size - 1] == '\0');
4758 data += prefix_size;
4759
4760 /* read the message */
4761 memcpy(&change->data.msg.message_size, data, sizeof(Size));
4762 data += sizeof(Size);
4763 change->data.msg.message = MemoryContextAlloc(rb->context,
4764 change->data.msg.message_size);
4765 memcpy(change->data.msg.message, data,
4766 change->data.msg.message_size);
4767 data += change->data.msg.message_size;
4768
4769 break;
4770 }
4771 case REORDER_BUFFER_CHANGE_INVALIDATION:
4772 {
4773 Size inval_size = sizeof(SharedInvalidationMessage) *
4774 change->data.inval.ninvalidations;
4775
4776 change->data.inval.invalidations =
4777 MemoryContextAlloc(rb->context, inval_size);
4778
4779 /* read the message */
4780 memcpy(change->data.inval.invalidations, data, inval_size);
4781
4782 break;
4783 }
4784 case REORDER_BUFFER_CHANGE_INTERNAL_SNAPSHOT:
4785 {
4786 Snapshot oldsnap;
4787 Snapshot newsnap;
4788 Size size;
4789
4790 oldsnap = (Snapshot) data;
4791
4792 size = sizeof(SnapshotData) +
4793 sizeof(TransactionId) * oldsnap->xcnt +
4794 sizeof(TransactionId) * (oldsnap->subxcnt + 0);
4795
4796 change->data.snapshot = MemoryContextAllocZero(rb->context, size);
4797
4798 newsnap = change->data.snapshot;
4799
4800 memcpy(newsnap, data, size);
4801 newsnap->xip = (TransactionId *)
4802 (((char *) newsnap) + sizeof(SnapshotData));
4803 newsnap->subxip = newsnap->xip + newsnap->xcnt;
4804 newsnap->copied = true;
4805 break;
4806 }
4807 /* the base struct contains all the data, easy peasy */
4808 case REORDER_BUFFER_CHANGE_TRUNCATE:
4809 {
4810 Oid *relids;
4811
4812 relids = ReorderBufferAllocRelids(rb, change->data.truncate.nrelids);
4813 memcpy(relids, data, change->data.truncate.nrelids * sizeof(Oid));
4814 change->data.truncate.relids = relids;
4815
4816 break;
4817 }
4818 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM:
4819 case REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT:
4820 case REORDER_BUFFER_CHANGE_INTERNAL_COMMAND_ID:
4821 case REORDER_BUFFER_CHANGE_INTERNAL_TUPLECID:
4822 break;
4823 }
4824
4825 dlist_push_tail(&txn->changes, &change->node);
4826 txn->nentries_mem++;
4827
4828 /*
4829 * Update memory accounting for the restored change. We need to do this
4830 * although we don't check the memory limit when restoring the changes in
4831 * this branch (we only do that when initially queueing the changes after
4832 * decoding), because we will release the changes later, and that will
4833 * update the accounting too (subtracting the size from the counters). And
4834 * we don't want to underflow there.
4835 */
4836 ReorderBufferChangeMemoryUpdate(rb, change, NULL, true,
4837 ReorderBufferChangeSize(change));
4838 }
4839
4840 /*
4841 * Remove all on-disk stored for the passed in transaction.
4842 */
4843 static void
4844 ReorderBufferRestoreCleanup(ReorderBuffer *rb, ReorderBufferTXN *txn)
4845 {
4846 XLogSegNo first;
4847 XLogSegNo cur;
4848 XLogSegNo last;
4849
4850 Assert(txn->first_lsn != InvalidXLogRecPtr);
4851 Assert(txn->final_lsn != InvalidXLogRecPtr);
4852
4853 XLByteToSeg(txn->first_lsn, first, wal_segment_size);
4854 XLByteToSeg(txn->final_lsn, last, wal_segment_size);
4855
4856 /* iterate over all possible filenames, and delete them */
4857 for (cur = first; cur <= last; cur++)
4858 {
4859 char path[MAXPGPATH];
4860
4861 ReorderBufferSerializedPath(path, MyReplicationSlot, txn->xid, cur);
4862 if (unlink(path) != 0 && errno != ENOENT)
4863 ereport(ERROR,
4864 (errcode_for_file_access(),
4865 errmsg("could not remove file \"%s\": %m", path)));
4866 }
4867 }
4868
4869 /*
4870 * Remove any leftover serialized reorder buffers from a slot directory after a
4871 * prior crash or decoding session exit.
4872 */
4873 static void
4874 ReorderBufferCleanupSerializedTXNs(const char *slotname)
4875 {
4876 DIR *spill_dir;
4877 struct dirent *spill_de;
4878 struct stat statbuf;
4879 char path[MAXPGPATH * 2 + sizeof(PG_REPLSLOT_DIR)];
4880
4881 sprintf(path, "%s/%s", PG_REPLSLOT_DIR, slotname);
4882
4883 /* we're only handling directories here, skip if it's not ours */
4884 if (lstat(path, &statbuf) == 0 && !S_ISDIR(statbuf.st_mode))
4885 return;
4886
4887 spill_dir = AllocateDir(path);
4888 while ((spill_de = ReadDirExtended(spill_dir, path, INFO)) != NULL)
4889 {
4890 /* only look at names that can be ours */
4891 if (strncmp(spill_de->d_name, "xid", 3) == 0)
4892 {
4893 snprintf(path, sizeof(path),
4894 "%s/%s/%s", PG_REPLSLOT_DIR, slotname,
4895 spill_de->d_name);
4896
4897 if (unlink(path) != 0)
4898 ereport(ERROR,
4899 (errcode_for_file_access(),
4900 errmsg("could not remove file \"%s\" during removal of %s/%s/xid*: %m",
4901 path, PG_REPLSLOT_DIR, slotname)));
4902 }
4903 }
4904 FreeDir(spill_dir);
4905 }
4906
4907 /*
4908 * Given a replication slot, transaction ID and segment number, fill in the
4909 * corresponding spill file into 'path', which is a caller-owned buffer of size
4910 * at least MAXPGPATH.
4911 */
4912 static void
4913 ReorderBufferSerializedPath(char *path, ReplicationSlot *slot, TransactionId xid,
4914 XLogSegNo segno)
4915 {
4916 XLogRecPtr recptr;
4917
4918 XLogSegNoOffsetToRecPtr(segno, 0, wal_segment_size, recptr);
4919
4920 snprintf(path, MAXPGPATH, "%s/%s/xid-%u-lsn-%X-%X.spill",
4921 PG_REPLSLOT_DIR,
4922 NameStr(MyReplicationSlot->data.name),
4923 xid, LSN_FORMAT_ARGS(recptr));
4924 }
4925
4926 /*
4927 * Delete all data spilled to disk after we've restarted/crashed. It will be
4928 * recreated when the respective slots are reused.
4929 */
4930 void
4931 StartupReorderBuffer(void)
4932 {
4933 DIR *logical_dir;
4934 struct dirent *logical_de;
4935
4936 logical_dir = AllocateDir(PG_REPLSLOT_DIR);
4937 while ((logical_de = ReadDir(logical_dir, PG_REPLSLOT_DIR)) != NULL)
4938 {
4939 if (strcmp(logical_de->d_name, ".") == 0 ||
4940 strcmp(logical_de->d_name, "..") == 0)
4941 continue;
4942
4943 /* if it cannot be a slot, skip the directory */
4944 if (!ReplicationSlotValidateName(logical_de->d_name, DEBUG2))
4945 continue;
4946
4947 /*
4948 * ok, has to be a surviving logical slot, iterate and delete
4949 * everything starting with xid-*
4950 */
4951 ReorderBufferCleanupSerializedTXNs(logical_de->d_name);
4952 }
4953 FreeDir(logical_dir);
4954 }
4955
4956 /* ---------------------------------------
4957 * toast reassembly support
4958 * ---------------------------------------
4959 */
4960
4961 /*
4962 * Initialize per tuple toast reconstruction support.
4963 */
4964 static void
4965 ReorderBufferToastInitHash(ReorderBuffer *rb, ReorderBufferTXN *txn)
4966 {
4967 HASHCTL hash_ctl;
4968
4969 Assert(txn->toast_hash == NULL);
4970
4971 hash_ctl.keysize = sizeof(Oid);
4972 hash_ctl.entrysize = sizeof(ReorderBufferToastEnt);
4973 hash_ctl.hcxt = rb->context;
4974 txn->toast_hash = hash_create("ReorderBufferToastHash", 5, &hash_ctl,
4975 HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
4976 }
4977
4978 /*
4979 * Per toast-chunk handling for toast reconstruction
4980 *
4981 * Appends a toast chunk so we can reconstruct it when the tuple "owning" the
4982 * toasted Datum comes along.
4983 */
4984 static void
4985 ReorderBufferToastAppendChunk(ReorderBuffer *rb, ReorderBufferTXN *txn,
4986 Relation relation, ReorderBufferChange *change)
4987 {
4988 ReorderBufferToastEnt *ent;
4989 HeapTuple newtup;
4990 bool found;
4991 int32 chunksize;
4992 bool isnull;
4993 Pointer chunk;
4994 TupleDesc desc = RelationGetDescr(relation);
4995 Oid chunk_id;
4996 int32 chunk_seq;
4997
4998 if (txn->toast_hash == NULL)
4999 ReorderBufferToastInitHash(rb, txn);
5000
5001 Assert(IsToastRelation(relation));
5002
5003 newtup = change->data.tp.newtuple;
5004 chunk_id = DatumGetObjectId(fastgetattr(newtup, 1, desc, &isnull));
5005 Assert(!isnull);
5006 chunk_seq = DatumGetInt32(fastgetattr(newtup, 2, desc, &isnull));
5007 Assert(!isnull);
5008
5009 ent = (ReorderBufferToastEnt *)
5010 hash_search(txn->toast_hash, &chunk_id, HASH_ENTER, &found);
5011
5012 if (!found)
5013 {
5014 Assert(ent->chunk_id == chunk_id);
5015 ent->num_chunks = 0;
5016 ent->last_chunk_seq = 0;
5017 ent->size = 0;
5018 ent->reconstructed = NULL;
5019 dlist_init(&ent->chunks);
5020
5021 if (chunk_seq != 0)
5022 elog(ERROR, "got sequence entry %d for toast chunk %u instead of seq 0",
5023 chunk_seq, chunk_id);
5024 }
5025 else if (found && chunk_seq != ent->last_chunk_seq + 1)
5026 elog(ERROR, "got sequence entry %d for toast chunk %u instead of seq %d",
5027 chunk_seq, chunk_id, ent->last_chunk_seq + 1);
5028
5029 chunk = DatumGetPointer(fastgetattr(newtup, 3, desc, &isnull));
5030 Assert(!isnull);
5031
5032 /* calculate size so we can allocate the right size at once later */
5033 if (!VARATT_IS_EXTENDED(chunk))
5034 chunksize = VARSIZE(chunk) - VARHDRSZ;
5035 else if (VARATT_IS_SHORT(chunk))
5036 /* could happen due to heap_form_tuple doing its thing */
5037 chunksize = VARSIZE_SHORT(chunk) - VARHDRSZ_SHORT;
5038 else
5039 elog(ERROR, "unexpected type of toast chunk");
5040
5041 ent->size += chunksize;
5042 ent->last_chunk_seq = chunk_seq;
5043 ent->num_chunks++;
5044 dlist_push_tail(&ent->chunks, &change->node);
5045 }
5046
5047 /*
5048 * Rejigger change->newtuple to point to in-memory toast tuples instead of
5049 * on-disk toast tuples that may no longer exist (think DROP TABLE or VACUUM).
5050 *
5051 * We cannot replace unchanged toast tuples though, so those will still point
5052 * to on-disk toast data.
5053 *
5054 * While updating the existing change with detoasted tuple data, we need to
5055 * update the memory accounting info, because the change size will differ.
5056 * Otherwise the accounting may get out of sync, triggering serialization
5057 * at unexpected times.
5058 *
5059 * We simply subtract size of the change before rejiggering the tuple, and
5060 * then add the new size. This makes it look like the change was removed
5061 * and then added back, except it only tweaks the accounting info.
5062 *
5063 * In particular it can't trigger serialization, which would be pointless
5064 * anyway as it happens during commit processing right before handing
5065 * the change to the output plugin.
5066 */
5067 static void
5068 ReorderBufferToastReplace(ReorderBuffer *rb, ReorderBufferTXN *txn,
5069 Relation relation, ReorderBufferChange *change)
5070 {
5071 TupleDesc desc;
5072 int natt;
5073 Datum *attrs;
5074 bool *isnull;
5075 bool *free;
5076 HeapTuple tmphtup;
5077 Relation toast_rel;
5078 TupleDesc toast_desc;
5079 MemoryContext oldcontext;
5080 HeapTuple newtup;
5081 Size old_size;
5082
5083 /* no toast tuples changed */
5084 if (txn->toast_hash == NULL)
5085 return;
5086
5087 /*
5088 * We're going to modify the size of the change. So, to make sure the
5089 * accounting is correct we record the current change size and then after
5090 * re-computing the change we'll subtract the recorded size and then
5091 * re-add the new change size at the end. We don't immediately subtract
5092 * the old size because if there is any error before we add the new size,
5093 * we will release the changes and that will update the accounting info
5094 * (subtracting the size from the counters). And we don't want to
5095 * underflow there.
5096 */
5097 old_size = ReorderBufferChangeSize(change);
5098
5099 oldcontext = MemoryContextSwitchTo(rb->context);
5100
5101 /* we should only have toast tuples in an INSERT or UPDATE */
5102 Assert(change->data.tp.newtuple);
5103
5104 desc = RelationGetDescr(relation);
5105
5106 toast_rel = RelationIdGetRelation(relation->rd_rel->reltoastrelid);
5107 if (!RelationIsValid(toast_rel))
5108 elog(ERROR, "could not open toast relation with OID %u (base relation \"%s\")",
5109 relation->rd_rel->reltoastrelid, RelationGetRelationName(relation));
5110
5111 toast_desc = RelationGetDescr(toast_rel);
5112
5113 /* should we allocate from stack instead? */
5114 attrs = palloc0(sizeof(Datum) * desc->natts);
5115 isnull = palloc0(sizeof(bool) * desc->natts);
5116 free = palloc0(sizeof(bool) * desc->natts);
5117
5118 newtup = change->data.tp.newtuple;
5119
5120 heap_deform_tuple(newtup, desc, attrs, isnull);
5121
5122 for (natt = 0; natt < desc->natts; natt++)
5123 {
5124 Form_pg_attribute attr = TupleDescAttr(desc, natt);
5125 ReorderBufferToastEnt *ent;
5126 struct varlena *varlena;
5127
5128 /* va_rawsize is the size of the original datum -- including header */
5129 struct varatt_external toast_pointer;
5130 struct varatt_indirect redirect_pointer;
5131 struct varlena *new_datum = NULL;
5132 struct varlena *reconstructed;
5133 dlist_iter it;
5134 Size data_done = 0;
5135
5136 /* system columns aren't toasted */
5137 if (attr->attnum < 0)
5138 continue;
5139
5140 if (attr->attisdropped)
5141 continue;
5142
5143 /* not a varlena datatype */
5144 if (attr->attlen != -1)
5145 continue;
5146
5147 /* no data */
5148 if (isnull[natt])
5149 continue;
5150
5151 /* ok, we know we have a toast datum */
5152 varlena = (struct varlena *) DatumGetPointer(attrs[natt]);
5153
5154 /* no need to do anything if the tuple isn't external */
5155 if (!VARATT_IS_EXTERNAL(varlena))
5156 continue;
5157
5158 VARATT_EXTERNAL_GET_POINTER(toast_pointer, varlena);
5159
5160 /*
5161 * Check whether the toast tuple changed, replace if so.
5162 */
5163 ent = (ReorderBufferToastEnt *)
5164 hash_search(txn->toast_hash,
5165 &toast_pointer.va_valueid,
5166 HASH_FIND,
5167 NULL);
5168 if (ent == NULL)
5169 continue;
5170
5171 new_datum =
5172 (struct varlena *) palloc0(INDIRECT_POINTER_SIZE);
5173
5174 free[natt] = true;
5175
5176 reconstructed = palloc0(toast_pointer.va_rawsize);
5177
5178 ent->reconstructed = reconstructed;
5179
5180 /* stitch toast tuple back together from its parts */
5181 dlist_foreach(it, &ent->chunks)
5182 {
5183 bool cisnull;
5184 ReorderBufferChange *cchange;
5185 HeapTuple ctup;
5186 Pointer chunk;
5187
5188 cchange = dlist_container(ReorderBufferChange, node, it.cur);
5189 ctup = cchange->data.tp.newtuple;
5190 chunk = DatumGetPointer(fastgetattr(ctup, 3, toast_desc, &cisnull));
5191
5192 Assert(!cisnull);
5193 Assert(!VARATT_IS_EXTERNAL(chunk));
5194 Assert(!VARATT_IS_SHORT(chunk));
5195
5196 memcpy(VARDATA(reconstructed) + data_done,
5197 VARDATA(chunk),
5198 VARSIZE(chunk) - VARHDRSZ);
5199 data_done += VARSIZE(chunk) - VARHDRSZ;
5200 }
5201 Assert(data_done == VARATT_EXTERNAL_GET_EXTSIZE(toast_pointer));
5202
5203 /* make sure its marked as compressed or not */
5204 if (VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer))
5205 SET_VARSIZE_COMPRESSED(reconstructed, data_done + VARHDRSZ);
5206 else
5207 SET_VARSIZE(reconstructed, data_done + VARHDRSZ);
5208
5209 memset(&redirect_pointer, 0, sizeof(redirect_pointer));
5210 redirect_pointer.pointer = reconstructed;
5211
5212 SET_VARTAG_EXTERNAL(new_datum, VARTAG_INDIRECT);
5213 memcpy(VARDATA_EXTERNAL(new_datum), &redirect_pointer,
5214 sizeof(redirect_pointer));
5215
5216 attrs[natt] = PointerGetDatum(new_datum);
5217 }
5218
5219 /*
5220 * Build tuple in separate memory & copy tuple back into the tuplebuf
5221 * passed to the output plugin. We can't directly heap_fill_tuple() into
5222 * the tuplebuf because attrs[] will point back into the current content.
5223 */
5224 tmphtup = heap_form_tuple(desc, attrs, isnull);
5225 Assert(newtup->t_len <= MaxHeapTupleSize);
5226 Assert(newtup->t_data == (HeapTupleHeader) ((char *) newtup + HEAPTUPLESIZE));
5227
5228 memcpy(newtup->t_data, tmphtup->t_data, tmphtup->t_len);
5229 newtup->t_len = tmphtup->t_len;
5230
5231 /*
5232 * free resources we won't further need, more persistent stuff will be
5233 * free'd in ReorderBufferToastReset().
5234 */
5235 RelationClose(toast_rel);
5236 pfree(tmphtup);
5237 for (natt = 0; natt < desc->natts; natt++)
5238 {
5239 if (free[natt])
5240 pfree(DatumGetPointer(attrs[natt]));
5241 }
5242 pfree(attrs);
5243 pfree(free);
5244 pfree(isnull);
5245
5246 MemoryContextSwitchTo(oldcontext);
5247
5248 /* subtract the old change size */
5249 ReorderBufferChangeMemoryUpdate(rb, change, NULL, false, old_size);
5250 /* now add the change back, with the correct size */
5251 ReorderBufferChangeMemoryUpdate(rb, change, NULL, true,
5252 ReorderBufferChangeSize(change));
5253 }
5254
5255 /*
5256 * Free all resources allocated for toast reconstruction.
5257 */
5258 static void
5259 ReorderBufferToastReset(ReorderBuffer *rb, ReorderBufferTXN *txn)
5260 {
5261 HASH_SEQ_STATUS hstat;
5262 ReorderBufferToastEnt *ent;
5263
5264 if (txn->toast_hash == NULL)
5265 return;
5266
5267 /* sequentially walk over the hash and free everything */
5268 hash_seq_init(&hstat, txn->toast_hash);
5269 while ((ent = (ReorderBufferToastEnt *) hash_seq_search(&hstat)) != NULL)
5270 {
5271 dlist_mutable_iter it;
5272
5273 if (ent->reconstructed != NULL)
5274 pfree(ent->reconstructed);
5275
5276 dlist_foreach_modify(it, &ent->chunks)
5277 {
5278 ReorderBufferChange *change =
5279 dlist_container(ReorderBufferChange, node, it.cur);
5280
5281 dlist_delete(&change->node);
5282 ReorderBufferFreeChange(rb, change, true);
5283 }
5284 }
5285
5286 hash_destroy(txn->toast_hash);
5287 txn->toast_hash = NULL;
5288 }
5289
5290
5291 /* ---------------------------------------
5292 * Visibility support for logical decoding
5293 *
5294 *
5295 * Lookup actual cmin/cmax values when using decoding snapshot. We can't
5296 * always rely on stored cmin/cmax values because of two scenarios:
5297 *
5298 * * A tuple got changed multiple times during a single transaction and thus
5299 * has got a combo CID. Combo CIDs are only valid for the duration of a
5300 * single transaction.
5301 * * A tuple with a cmin but no cmax (and thus no combo CID) got
5302 * deleted/updated in another transaction than the one which created it
5303 * which we are looking at right now. As only one of cmin, cmax or combo CID
5304 * is actually stored in the heap we don't have access to the value we
5305 * need anymore.
5306 *
5307 * To resolve those problems we have a per-transaction hash of (cmin,
5308 * cmax) tuples keyed by (relfilelocator, ctid) which contains the actual
5309 * (cmin, cmax) values. That also takes care of combo CIDs by simply
5310 * not caring about them at all. As we have the real cmin/cmax values
5311 * combo CIDs aren't interesting.
5312 *
5313 * As we only care about catalog tuples here the overhead of this
5314 * hashtable should be acceptable.
5315 *
5316 * Heap rewrites complicate this a bit, check rewriteheap.c for
5317 * details.
5318 * -------------------------------------------------------------------------
5319 */
5320
5321 /* struct for sorting mapping files by LSN efficiently */
5322 typedef struct RewriteMappingFile
5323 {
5324 XLogRecPtr lsn;
5325 char fname[MAXPGPATH];
5326 } RewriteMappingFile;
5327
5328 #ifdef NOT_USED
5329 static void
5330 DisplayMapping(HTAB *tuplecid_data)
5331 {
5332 HASH_SEQ_STATUS hstat;
5333 ReorderBufferTupleCidEnt *ent;
5334
5335 hash_seq_init(&hstat, tuplecid_data);
5336 while ((ent = (ReorderBufferTupleCidEnt *) hash_seq_search(&hstat)) != NULL)
5337 {
5338 elog(DEBUG3, "mapping: node: %u/%u/%u tid: %u/%u cmin: %u, cmax: %u",
5339 ent->key.rlocator.dbOid,
5340 ent->key.rlocator.spcOid,
5341 ent->key.rlocator.relNumber,
5342 ItemPointerGetBlockNumber(&ent->key.tid),
5343 ItemPointerGetOffsetNumber(&ent->key.tid),
5344 ent->cmin,
5345 ent->cmax
5346 );
5347 }
5348 }
5349 #endif
5350
5351 /*
5352 * Apply a single mapping file to tuplecid_data.
5353 *
5354 * The mapping file has to have been verified to be a) committed b) for our
5355 * transaction c) applied in LSN order.
5356 */
5357 static void
5358 ApplyLogicalMappingFile(HTAB *tuplecid_data, Oid relid, const char *fname)
5359 {
5360 char path[MAXPGPATH];
5361 int fd;
5362 int readBytes;
5363 LogicalRewriteMappingData map;
5364
5365 sprintf(path, "%s/%s", PG_LOGICAL_MAPPINGS_DIR, fname);
5366 fd = OpenTransientFile(path, O_RDONLY | PG_BINARY);
5367 if (fd < 0)
5368 ereport(ERROR,
5369 (errcode_for_file_access(),
5370 errmsg("could not open file \"%s\": %m", path)));
5371
5372 while (true)
5373 {
5374 ReorderBufferTupleCidKey key;
5375 ReorderBufferTupleCidEnt *ent;
5376 ReorderBufferTupleCidEnt *new_ent;
5377 bool found;
5378
5379 /* be careful about padding */
5380 memset(&key, 0, sizeof(ReorderBufferTupleCidKey));
5381
5382 /* read all mappings till the end of the file */
5383 pgstat_report_wait_start(WAIT_EVENT_REORDER_LOGICAL_MAPPING_READ);
5384 readBytes = read(fd, &map, sizeof(LogicalRewriteMappingData));
5385 pgstat_report_wait_end();
5386
5387 if (readBytes < 0)
5388 ereport(ERROR,
5389 (errcode_for_file_access(),
5390 errmsg("could not read file \"%s\": %m",
5391 path)));
5392 else if (readBytes == 0) /* EOF */
5393 break;
5394 else if (readBytes != sizeof(LogicalRewriteMappingData))
5395 ereport(ERROR,
5396 (errcode_for_file_access(),
5397 errmsg("could not read from file \"%s\": read %d instead of %d bytes",
5398 path, readBytes,
5399 (int32) sizeof(LogicalRewriteMappingData))));
5400
5401 key.rlocator = map.old_locator;
5402 ItemPointerCopy(&map.old_tid,
5403 &key.tid);
5404
5405
5406 ent = (ReorderBufferTupleCidEnt *)
5407 hash_search(tuplecid_data, &key, HASH_FIND, NULL);
5408
5409 /* no existing mapping, no need to update */
5410 if (!ent)
5411 continue;
5412
5413 key.rlocator = map.new_locator;
5414 ItemPointerCopy(&map.new_tid,
5415 &key.tid);
5416
5417 new_ent = (ReorderBufferTupleCidEnt *)
5418 hash_search(tuplecid_data, &key, HASH_ENTER, &found);
5419
5420 if (found)
5421 {
5422 /*
5423 * Make sure the existing mapping makes sense. We sometime update
5424 * old records that did not yet have a cmax (e.g. pg_class' own
5425 * entry while rewriting it) during rewrites, so allow that.
5426 */
5427 Assert(ent->cmin == InvalidCommandId || ent->cmin == new_ent->cmin);
5428 Assert(ent->cmax == InvalidCommandId || ent->cmax == new_ent->cmax);
5429 }
5430 else
5431 {
5432 /* update mapping */
5433 new_ent->cmin = ent->cmin;
5434 new_ent->cmax = ent->cmax;
5435 new_ent->combocid = ent->combocid;
5436 }
5437 }
5438
5439 if (CloseTransientFile(fd) != 0)
5440 ereport(ERROR,
5441 (errcode_for_file_access(),
5442 errmsg("could not close file \"%s\": %m", path)));
5443 }
5444
5445
5446 /*
5447 * Check whether the TransactionId 'xid' is in the pre-sorted array 'xip'.
5448 */
5449 static bool
5450 TransactionIdInArray(TransactionId xid, TransactionId *xip, Size num)
5451 {
5452 return bsearch(&xid, xip, num,
5453 sizeof(TransactionId), xidComparator) != NULL;
5454 }
5455
5456 /*
5457 * list_sort() comparator for sorting RewriteMappingFiles in LSN order.
5458 */
5459 static int
5460 file_sort_by_lsn(const ListCell *a_p, const ListCell *b_p)
5461 {
5462 RewriteMappingFile *a = (RewriteMappingFile *) lfirst(a_p);
5463 RewriteMappingFile *b = (RewriteMappingFile *) lfirst(b_p);
5464
5465 return pg_cmp_u64(a->lsn, b->lsn);
5466 }
5467
5468 /*
5469 * Apply any existing logical remapping files if there are any targeted at our
5470 * transaction for relid.
5471 */
5472 static void
5473 UpdateLogicalMappings(HTAB *tuplecid_data, Oid relid, Snapshot snapshot)
5474 {
5475 DIR *mapping_dir;
5476 struct dirent *mapping_de;
5477 List *files = NIL;
5478 ListCell *file;
5479 Oid dboid = IsSharedRelation(relid) ? InvalidOid : MyDatabaseId;
5480
5481 mapping_dir = AllocateDir(PG_LOGICAL_MAPPINGS_DIR);
5482 while ((mapping_de = ReadDir(mapping_dir, PG_LOGICAL_MAPPINGS_DIR)) != NULL)
5483 {
5484 Oid f_dboid;
5485 Oid f_relid;
5486 TransactionId f_mapped_xid;
5487 TransactionId f_create_xid;
5488 XLogRecPtr f_lsn;
5489 uint32 f_hi,
5490 f_lo;
5491 RewriteMappingFile *f;
5492
5493 if (strcmp(mapping_de->d_name, ".") == 0 ||
5494 strcmp(mapping_de->d_name, "..") == 0)
5495 continue;
5496
5497 /* Ignore files that aren't ours */
5498 if (strncmp(mapping_de->d_name, "map-", 4) != 0)
5499 continue;
5500
5501 if (sscanf(mapping_de->d_name, LOGICAL_REWRITE_FORMAT,
5502 &f_dboid, &f_relid, &f_hi, &f_lo,
5503 &f_mapped_xid, &f_create_xid) != 6)
5504 elog(ERROR, "could not parse filename \"%s\"", mapping_de->d_name);
5505
5506 f_lsn = ((uint64) f_hi) << 32 | f_lo;
5507
5508 /* mapping for another database */
5509 if (f_dboid != dboid)
5510 continue;
5511
5512 /* mapping for another relation */
5513 if (f_relid != relid)
5514 continue;
5515
5516 /* did the creating transaction abort? */
5517 if (!TransactionIdDidCommit(f_create_xid))
5518 continue;
5519
5520 /* not for our transaction */
5521 if (!TransactionIdInArray(f_mapped_xid, snapshot->subxip, snapshot->subxcnt))
5522 continue;
5523
5524 /* ok, relevant, queue for apply */
5525 f = palloc(sizeof(RewriteMappingFile));
5526 f->lsn = f_lsn;
5527 strcpy(f->fname, mapping_de->d_name);
5528 files = lappend(files, f);
5529 }
5530 FreeDir(mapping_dir);
5531
5532 /* sort files so we apply them in LSN order */
5533 list_sort(files, file_sort_by_lsn);
5534
5535 foreach(file, files)
5536 {
5537 RewriteMappingFile *f = (RewriteMappingFile *) lfirst(file);
5538
5539 elog(DEBUG1, "applying mapping: \"%s\" in %u", f->fname,
5540 snapshot->subxip[0]);
5541 ApplyLogicalMappingFile(tuplecid_data, relid, f->fname);
5542 pfree(f);
5543 }
5544 }
5545
5546 /*
5547 * Lookup cmin/cmax of a tuple, during logical decoding where we can't rely on
5548 * combo CIDs.
5549 */
5550 bool
5551 ResolveCminCmaxDuringDecoding(HTAB *tuplecid_data,
5552 Snapshot snapshot,
5553 HeapTuple htup, Buffer buffer,
5554 CommandId *cmin, CommandId *cmax)
5555 {
5556 ReorderBufferTupleCidKey key;
5557 ReorderBufferTupleCidEnt *ent;
5558 ForkNumber forkno;
5559 BlockNumber blockno;
5560 bool updated_mapping = false;
5561
5562 /*
5563 * Return unresolved if tuplecid_data is not valid. That's because when
5564 * streaming in-progress transactions we may run into tuples with the CID
5565 * before actually decoding them. Think e.g. about INSERT followed by
5566 * TRUNCATE, where the TRUNCATE may not be decoded yet when applying the
5567 * INSERT. So in such cases, we assume the CID is from the future
5568 * command.
5569 */
5570 if (tuplecid_data == NULL)
5571 return false;
5572
5573 /* be careful about padding */
5574 memset(&key, 0, sizeof(key));
5575
5576 Assert(!BufferIsLocal(buffer));
5577
5578 /*
5579 * get relfilelocator from the buffer, no convenient way to access it
5580 * other than that.
5581 */
5582 BufferGetTag(buffer, &key.rlocator, &forkno, &blockno);
5583
5584 /* tuples can only be in the main fork */
5585 Assert(forkno == MAIN_FORKNUM);
5586 Assert(blockno == ItemPointerGetBlockNumber(&htup->t_self));
5587
5588 ItemPointerCopy(&htup->t_self,
5589 &key.tid);
5590
5591 restart:
5592 ent = (ReorderBufferTupleCidEnt *)
5593 hash_search(tuplecid_data, &key, HASH_FIND, NULL);
5594
5595 /*
5596 * failed to find a mapping, check whether the table was rewritten and
5597 * apply mapping if so, but only do that once - there can be no new
5598 * mappings while we are in here since we have to hold a lock on the
5599 * relation.
5600 */
5601 if (ent == NULL && !updated_mapping)
5602 {
5603 UpdateLogicalMappings(tuplecid_data, htup->t_tableOid, snapshot);
5604 /* now check but don't update for a mapping again */
5605 updated_mapping = true;
5606 goto restart;
5607 }
5608 else if (ent == NULL)
5609 return false;
5610
5611 if (cmin)
5612 *cmin = ent->cmin;
5613 if (cmax)
5614 *cmax = ent->cmax;
5615 return true;
5616 }
5617
5618 /*
5619 * Count invalidation messages of specified transaction.
5620 *
5621 * Returns number of messages, and msgs is set to the pointer of the linked
5622 * list for the messages.
5623 */
5624 uint32
5625 ReorderBufferGetInvalidations(ReorderBuffer *rb, TransactionId xid,
5626 SharedInvalidationMessage **msgs)
5627 {
5628 ReorderBufferTXN *txn;
5629
5630 txn = ReorderBufferTXNByXid(rb, xid, false, NULL, InvalidXLogRecPtr,
5631 false);
5632
5633 if (txn == NULL)
5634 return 0;
5635
5636 *msgs = txn->invalidations;
5637
5638 return txn->ninvalidations;
5639 }