Branch data Line data Source code
1 : : /* -------------------------------------------------------------------------
2 : : *
3 : : * decode.c
4 : : * This module decodes WAL records read using xlogreader.h's APIs for the
5 : : * purpose of logical decoding by passing information to the
6 : : * reorderbuffer module (containing the actual changes) and to the
7 : : * snapbuild module to build a fitting catalog snapshot (to be able to
8 : : * properly decode the changes in the reorderbuffer).
9 : : *
10 : : * NOTE:
11 : : * This basically tries to handle all low level xlog stuff for
12 : : * reorderbuffer.c and snapbuild.c. There's some minor leakage where a
13 : : * specific record's struct is used to pass data along, but those just
14 : : * happen to contain the right amount of data in a convenient
15 : : * format. There isn't and shouldn't be much intelligence about the
16 : : * contents of records in here except turning them into a more usable
17 : : * format.
18 : : *
19 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
20 : : * Portions Copyright (c) 1994, Regents of the University of California
21 : : *
22 : : * IDENTIFICATION
23 : : * src/backend/replication/logical/decode.c
24 : : *
25 : : * -------------------------------------------------------------------------
26 : : */
27 : : #include "postgres.h"
28 : :
29 : : #include "access/heapam_xlog.h"
30 : : #include "access/transam.h"
31 : : #include "access/xact.h"
32 : : #include "access/xlog_internal.h"
33 : : #include "access/xlogreader.h"
34 : : #include "access/xlogrecord.h"
35 : : #include "catalog/pg_control.h"
36 : : #include "commands/repack.h"
37 : : #include "replication/decode.h"
38 : : #include "replication/logical.h"
39 : : #include "replication/message.h"
40 : : #include "replication/reorderbuffer.h"
41 : : #include "replication/snapbuild.h"
42 : : #include "storage/standbydefs.h"
43 : :
44 : : /* individual record(group)'s handlers */
45 : : static void DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
46 : : static void DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
47 : : static void DecodeDelete(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
48 : : static void DecodeTruncate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
49 : : static void DecodeMultiInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
50 : : static void DecodeSpecConfirm(LogicalDecodingContext *ctx, XLogRecordBuffer *buf);
51 : :
52 : : static void DecodeCommit(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
53 : : xl_xact_parsed_commit *parsed, TransactionId xid,
54 : : bool two_phase);
55 : : static void DecodeAbort(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
56 : : xl_xact_parsed_abort *parsed, TransactionId xid,
57 : : bool two_phase);
58 : : static void DecodePrepare(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
59 : : xl_xact_parsed_prepare *parsed);
60 : :
61 : :
62 : : /* common function to decode tuples */
63 : : static void DecodeXLogTuple(char *data, Size len, HeapTuple tuple);
64 : :
65 : : /* helper functions for decoding transactions */
66 : : static inline bool FilterPrepare(LogicalDecodingContext *ctx,
67 : : TransactionId xid, const char *gid);
68 : : static bool DecodeTXNNeedSkip(LogicalDecodingContext *ctx,
69 : : XLogRecordBuffer *buf, Oid txn_dbid,
70 : : ReplOriginId origin_id);
71 : :
72 : : /*
73 : : * Take every XLogReadRecord()ed record and perform the actions required to
74 : : * decode it using the output plugin already setup in the logical decoding
75 : : * context.
76 : : *
77 : : * NB: Note that every record's xid needs to be processed by reorderbuffer
78 : : * (xids contained in the content of records are not relevant for this rule).
79 : : * That means that for records which'd otherwise not go through the
80 : : * reorderbuffer ReorderBufferProcessXid() has to be called. We don't want to
81 : : * call ReorderBufferProcessXid for each record type by default, because
82 : : * e.g. empty xacts can be handled more efficiently if there's no previous
83 : : * state for them.
84 : : *
85 : : * We also support the ability to fast forward thru records, skipping some
86 : : * record types completely - see individual record types for details.
87 : : */
88 : : void
89 : 2114284 : LogicalDecodingProcessRecord(LogicalDecodingContext *ctx, XLogReaderState *record)
90 : : {
91 : : XLogRecordBuffer buf;
92 : : TransactionId txid;
93 : : RmgrData rmgr;
94 : :
95 : 2114284 : buf.origptr = ctx->reader->ReadRecPtr;
96 : 2114284 : buf.endptr = ctx->reader->EndRecPtr;
97 : 2114284 : buf.record = record;
98 : :
99 : 2114284 : txid = XLogRecGetTopXid(record);
100 : :
101 : : /*
102 : : * If the top-level xid is valid, we need to assign the subxact to the
103 : : * top-level xact. We need to do this for all records, hence we do it
104 : : * before the switch.
105 : : */
106 [ + + ]: 2114284 : if (TransactionIdIsValid(txid))
107 : : {
108 : 677 : ReorderBufferAssignChild(ctx->reorder,
109 : : txid,
110 : 677 : XLogRecGetXid(record),
111 : : buf.origptr);
112 : : }
113 : :
114 : 2114284 : rmgr = GetRmgr(XLogRecGetRmid(record));
115 : :
116 [ + + ]: 2114284 : if (rmgr.rm_decode != NULL)
117 : 1553803 : rmgr.rm_decode(ctx, &buf);
118 : : else
119 : : {
120 : : /* just deal with xid, and done */
121 : 560481 : ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(record),
122 : : buf.origptr);
123 : : }
124 : 2114266 : }
125 : :
126 : : /*
127 : : * Handle rmgr XLOG_ID records for LogicalDecodingProcessRecord().
128 : : */
129 : : void
130 : 10560 : xlog_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
131 : : {
132 : 10560 : SnapBuild *builder = ctx->snapshot_builder;
133 : 10560 : uint8 info = XLogRecGetInfo(buf->record) & ~XLR_INFO_MASK;
134 : :
135 : 10560 : ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(buf->record),
136 : : buf->origptr);
137 : :
138 [ + + - + : 10560 : switch (info)
- ]
139 : : {
140 : : /* this is also used in END_OF_RECOVERY checkpoints */
141 : 122 : case XLOG_CHECKPOINT_SHUTDOWN:
142 : : case XLOG_END_OF_RECOVERY:
143 : 122 : SnapBuildSerializationPoint(builder, buf->origptr);
144 : :
145 : 122 : break;
146 : 89 : case XLOG_CHECKPOINT_ONLINE:
147 : :
148 : : /*
149 : : * a RUNNING_XACTS record will have been logged near to this, we
150 : : * can restart from there.
151 : : */
152 : 89 : break;
153 : 0 : case XLOG_LOGICAL_DECODING_STATUS_CHANGE:
154 : : {
155 : : bool logical_decoding;
156 : :
157 : 0 : memcpy(&logical_decoding, XLogRecGetData(buf->record), sizeof(bool));
158 : :
159 : : /*
160 : : * Error out as we should not decode this WAL record.
161 : : *
162 : : * Logical decoding is disabled, and existing logical slots on
163 : : * the standby are invalidated when this WAL record is
164 : : * replayed. No logical decoder can process this WAL record
165 : : * until replay completes, and by then the slots are already
166 : : * invalidated. Furthermore, no new logical slots can be
167 : : * created while logical decoding is disabled. This cannot
168 : : * occur even on primary either, since it will not restart
169 : : * with wal_level < replica if any logical slots exist.
170 : : */
171 [ # # ]: 0 : elog(ERROR, "unexpected logical decoding status change %d",
172 : : logical_decoding);
173 : :
174 : : break;
175 : : }
176 : 10349 : case XLOG_NOOP:
177 : : case XLOG_NEXTOID:
178 : : case XLOG_SWITCH:
179 : : case XLOG_BACKUP_END:
180 : : case XLOG_PARAMETER_CHANGE:
181 : : case XLOG_RESTORE_POINT:
182 : : case XLOG_FPW_CHANGE:
183 : : case XLOG_FPI_FOR_HINT:
184 : : case XLOG_FPI:
185 : : case XLOG_OVERWRITE_CONTRECORD:
186 : : case XLOG_CHECKPOINT_REDO:
187 : 10349 : break;
188 : 0 : default:
189 [ # # ]: 0 : elog(ERROR, "unexpected RM_XLOG_ID record type: %u", info);
190 : : }
191 : 10560 : }
192 : :
193 : : void
194 : 0 : xlog2_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
195 : : {
196 : 0 : uint8 info = XLogRecGetInfo(buf->record) & ~XLR_INFO_MASK;
197 : :
198 : 0 : ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(buf->record), buf->origptr);
199 : :
200 [ # # ]: 0 : switch (info)
201 : : {
202 : 0 : case XLOG2_CHECKSUMS:
203 : 0 : break;
204 : 0 : default:
205 [ # # ]: 0 : elog(ERROR, "unexpected RM_XLOG2_ID record type: %u", info);
206 : : }
207 : 0 : }
208 : :
209 : : /*
210 : : * Handle rmgr XACT_ID records for LogicalDecodingProcessRecord().
211 : : */
212 : : void
213 : 12604 : xact_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
214 : : {
215 : 12604 : SnapBuild *builder = ctx->snapshot_builder;
216 : 12604 : ReorderBuffer *reorder = ctx->reorder;
217 : 12604 : XLogReaderState *r = buf->record;
218 : 12604 : uint8 info = XLogRecGetInfo(r) & XLOG_XACT_OPMASK;
219 : :
220 : : /*
221 : : * If the snapshot isn't yet fully built, we cannot decode anything, so
222 : : * bail out.
223 : : */
224 [ + + ]: 12604 : if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT)
225 : 24 : return;
226 : :
227 [ + + + + : 12580 : switch (info)
+ - ]
228 : : {
229 : 4616 : case XLOG_XACT_COMMIT:
230 : : case XLOG_XACT_COMMIT_PREPARED:
231 : : {
232 : : xl_xact_commit *xlrec;
233 : : xl_xact_parsed_commit parsed;
234 : : TransactionId xid;
235 : 4616 : bool two_phase = false;
236 : :
237 : 4616 : xlrec = (xl_xact_commit *) XLogRecGetData(r);
238 : 4616 : ParseCommitRecord(XLogRecGetInfo(buf->record), xlrec, &parsed);
239 : :
240 [ + + ]: 4616 : if (!TransactionIdIsValid(parsed.twophase_xid))
241 : 4487 : xid = XLogRecGetXid(r);
242 : : else
243 : 129 : xid = parsed.twophase_xid;
244 : :
245 : : /*
246 : : * We would like to process the transaction in a two-phase
247 : : * manner iff output plugin supports two-phase commits and
248 : : * doesn't filter the transaction at prepare time.
249 : : */
250 [ + + ]: 4616 : if (info == XLOG_XACT_COMMIT_PREPARED)
251 : 129 : two_phase = !(FilterPrepare(ctx, xid,
252 : 129 : parsed.twophase_gid));
253 : :
254 : 4616 : DecodeCommit(ctx, buf, &parsed, xid, two_phase);
255 : 4600 : break;
256 : : }
257 : 326 : case XLOG_XACT_ABORT:
258 : : case XLOG_XACT_ABORT_PREPARED:
259 : : {
260 : : xl_xact_abort *xlrec;
261 : : xl_xact_parsed_abort parsed;
262 : : TransactionId xid;
263 : 326 : bool two_phase = false;
264 : :
265 : 326 : xlrec = (xl_xact_abort *) XLogRecGetData(r);
266 : 326 : ParseAbortRecord(XLogRecGetInfo(buf->record), xlrec, &parsed);
267 : :
268 [ + + ]: 326 : if (!TransactionIdIsValid(parsed.twophase_xid))
269 : 275 : xid = XLogRecGetXid(r);
270 : : else
271 : 51 : xid = parsed.twophase_xid;
272 : :
273 : : /*
274 : : * We would like to process the transaction in a two-phase
275 : : * manner iff output plugin supports two-phase commits and
276 : : * doesn't filter the transaction at prepare time.
277 : : */
278 [ + + ]: 326 : if (info == XLOG_XACT_ABORT_PREPARED)
279 : 51 : two_phase = !(FilterPrepare(ctx, xid,
280 : 51 : parsed.twophase_gid));
281 : :
282 : 326 : DecodeAbort(ctx, buf, &parsed, xid, two_phase);
283 : 326 : break;
284 : : }
285 : 115 : case XLOG_XACT_ASSIGNMENT:
286 : :
287 : : /*
288 : : * We assign subxact to the toplevel xact while processing each
289 : : * record if required. So, we don't need to do anything here. See
290 : : * LogicalDecodingProcessRecord.
291 : : */
292 : 115 : break;
293 : 7319 : case XLOG_XACT_INVALIDATIONS:
294 : : {
295 : : TransactionId xid;
296 : : xl_xact_invals *invals;
297 : :
298 : 7319 : xid = XLogRecGetXid(r);
299 : 7319 : invals = (xl_xact_invals *) XLogRecGetData(r);
300 : :
301 : : /*
302 : : * Execute the invalidations for xid-less transactions,
303 : : * otherwise, accumulate them so that they can be processed at
304 : : * the commit time.
305 : : */
306 [ + + ]: 7319 : if (TransactionIdIsValid(xid))
307 : : {
308 [ + + ]: 7269 : if (!ctx->fast_forward)
309 : 7194 : ReorderBufferAddInvalidations(reorder, xid,
310 : : buf->origptr,
311 : 7194 : invals->nmsgs,
312 : 7194 : invals->msgs);
313 : 7269 : ReorderBufferXidSetCatalogChanges(ctx->reorder, xid,
314 : : buf->origptr);
315 : : }
316 [ + - ]: 50 : else if (!ctx->fast_forward)
317 : 50 : ReorderBufferImmediateInvalidation(ctx->reorder,
318 : 50 : invals->nmsgs,
319 : 50 : invals->msgs);
320 : :
321 : 7319 : break;
322 : : }
323 : 204 : case XLOG_XACT_PREPARE:
324 : : {
325 : : xl_xact_parsed_prepare parsed;
326 : : xl_xact_prepare *xlrec;
327 : :
328 : : /* ok, parse it */
329 : 204 : xlrec = (xl_xact_prepare *) XLogRecGetData(r);
330 : 204 : ParsePrepareRecord(XLogRecGetInfo(buf->record),
331 : : xlrec, &parsed);
332 : :
333 : : /*
334 : : * We would like to process the transaction in a two-phase
335 : : * manner iff output plugin supports two-phase commits and
336 : : * doesn't filter the transaction at prepare time.
337 : : */
338 [ + + ]: 204 : if (FilterPrepare(ctx, parsed.twophase_xid,
339 : : parsed.twophase_gid))
340 : : {
341 : 21 : ReorderBufferProcessXid(reorder, parsed.twophase_xid,
342 : : buf->origptr);
343 : 21 : break;
344 : : }
345 : :
346 : : /*
347 : : * Note that if the prepared transaction has locked [user]
348 : : * catalog tables exclusively then decoding prepare can block
349 : : * till the main transaction is committed because it needs to
350 : : * lock the catalog tables.
351 : : *
352 : : * XXX Now, this can even lead to a deadlock if the prepare
353 : : * transaction is waiting to get it logically replicated for
354 : : * distributed 2PC. This can be avoided by disallowing
355 : : * preparing transactions that have locked [user] catalog
356 : : * tables exclusively but as of now, we ask users not to do
357 : : * such an operation.
358 : : */
359 : 183 : DecodePrepare(ctx, buf, &parsed);
360 : 183 : break;
361 : : }
362 : 0 : default:
363 [ # # ]: 0 : elog(ERROR, "unexpected RM_XACT_ID record type: %u", info);
364 : : }
365 : : }
366 : :
367 : : /*
368 : : * Handle rmgr STANDBY_ID records for LogicalDecodingProcessRecord().
369 : : */
370 : : void
371 : 5091 : standby_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
372 : : {
373 : 5091 : SnapBuild *builder = ctx->snapshot_builder;
374 : 5091 : XLogReaderState *r = buf->record;
375 : 5091 : uint8 info = XLogRecGetInfo(r) & ~XLR_INFO_MASK;
376 : :
377 : 5091 : ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(r), buf->origptr);
378 : :
379 [ + + + - ]: 5091 : switch (info)
380 : : {
381 : 1872 : case XLOG_RUNNING_XACTS:
382 : : {
383 : 1872 : xl_running_xacts *running = (xl_running_xacts *) XLogRecGetData(r);
384 : :
385 : : /*
386 : : * Update this decoder's idea of transactions currently
387 : : * running. In doing so we will determine whether we have
388 : : * reached consistent status.
389 : : */
390 : 1872 : SnapBuildProcessRunningXacts(builder, buf->origptr, running);
391 : :
392 : : /*
393 : : * Abort all transactions that we keep track of, that are
394 : : * older than the record's oldestRunningXid. This is the most
395 : : * convenient spot for doing so since, in contrast to shutdown
396 : : * or end-of-recovery checkpoints, we have information about
397 : : * all running transactions which includes prepared ones,
398 : : * while shutdown checkpoints just know that no non-prepared
399 : : * transactions are in progress.
400 : : */
401 : 1870 : ReorderBufferAbortOld(ctx->reorder, running->oldestRunningXid);
402 : : }
403 : 1870 : break;
404 : 3165 : case XLOG_STANDBY_LOCK:
405 : 3165 : break;
406 : 54 : case XLOG_INVALIDATIONS:
407 : :
408 : : /*
409 : : * We are processing the invalidations at the command level via
410 : : * XLOG_XACT_INVALIDATIONS. So we don't need to do anything here.
411 : : */
412 : 54 : break;
413 : 0 : default:
414 [ # # ]: 0 : elog(ERROR, "unexpected RM_STANDBY_ID record type: %u", info);
415 : : }
416 : 5089 : }
417 : :
418 : : /*
419 : : * Handle rmgr HEAP2_ID records for LogicalDecodingProcessRecord().
420 : : */
421 : : void
422 : 43005 : heap2_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
423 : : {
424 : 43005 : uint8 info = XLogRecGetInfo(buf->record) & XLOG_HEAP_OPMASK;
425 : 43005 : TransactionId xid = XLogRecGetXid(buf->record);
426 : 43005 : SnapBuild *builder = ctx->snapshot_builder;
427 : :
428 : 43005 : ReorderBufferProcessXid(ctx->reorder, xid, buf->origptr);
429 : :
430 : : /*
431 : : * If we don't have snapshot or we are just fast-forwarding, there is no
432 : : * point in decoding data changes. However, it's crucial to build the base
433 : : * snapshot during fast-forward mode (as is done in
434 : : * SnapBuildProcessChange()) because we require the snapshot's xmin when
435 : : * determining the candidate catalog_xmin for the replication slot. See
436 : : * SnapBuildProcessRunningXacts().
437 : : */
438 [ + + ]: 43005 : if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT)
439 : 14 : return;
440 : :
441 [ + + + + : 42991 : switch (info)
- ]
442 : : {
443 : 8039 : case XLOG_HEAP2_MULTI_INSERT:
444 [ + - ]: 8039 : if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
445 [ + + ]: 8039 : !ctx->fast_forward &&
446 [ + + ]: 7936 : !change_useless_for_repack(buf))
447 : 7781 : DecodeMultiInsert(ctx, buf);
448 : 8039 : break;
449 : 33407 : case XLOG_HEAP2_NEW_CID:
450 [ + + ]: 33407 : if (!ctx->fast_forward)
451 : : {
452 : : xl_heap_new_cid *xlrec;
453 : :
454 : 33024 : xlrec = (xl_heap_new_cid *) XLogRecGetData(buf->record);
455 : 33024 : SnapBuildProcessNewCid(builder, xid, buf->origptr, xlrec);
456 : : }
457 : 33407 : break;
458 : 90 : case XLOG_HEAP2_REWRITE:
459 : :
460 : : /*
461 : : * Although these records only exist to serve the needs of logical
462 : : * decoding, all the work happens as part of crash or archive
463 : : * recovery, so we don't need to do anything here.
464 : : */
465 : 90 : break;
466 : :
467 : : /*
468 : : * Everything else here is just low level physical stuff we're not
469 : : * interested in.
470 : : */
471 : 1455 : case XLOG_HEAP2_PRUNE_ON_ACCESS:
472 : : case XLOG_HEAP2_PRUNE_VACUUM_SCAN:
473 : : case XLOG_HEAP2_PRUNE_VACUUM_CLEANUP:
474 : : case XLOG_HEAP2_LOCK_UPDATED:
475 : 1455 : break;
476 : 0 : default:
477 [ # # ]: 0 : elog(ERROR, "unexpected RM_HEAP2_ID record type: %u", info);
478 : : }
479 : : }
480 : :
481 : : /*
482 : : * Handle rmgr HEAP_ID records for LogicalDecodingProcessRecord().
483 : : */
484 : : void
485 : 1482443 : heap_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
486 : : {
487 : 1482443 : uint8 info = XLogRecGetInfo(buf->record) & XLOG_HEAP_OPMASK;
488 : 1482443 : TransactionId xid = XLogRecGetXid(buf->record);
489 : 1482443 : SnapBuild *builder = ctx->snapshot_builder;
490 : :
491 : 1482443 : ReorderBufferProcessXid(ctx->reorder, xid, buf->origptr);
492 : :
493 : : /*
494 : : * If we don't have snapshot or we are just fast-forwarding, there is no
495 : : * point in decoding data changes. However, it's crucial to build the base
496 : : * snapshot during fast-forward mode (as is done in
497 : : * SnapBuildProcessChange()) because we require the snapshot's xmin when
498 : : * determining the candidate catalog_xmin for the replication slot. See
499 : : * SnapBuildProcessRunningXacts().
500 : : */
501 [ + + ]: 1482443 : if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT)
502 : 11 : return;
503 : :
504 [ + + + + : 1482432 : switch (info)
+ + + - ]
505 : : {
506 : 908747 : case XLOG_HEAP_INSERT:
507 [ + - ]: 908747 : if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
508 [ + + ]: 908747 : !ctx->fast_forward &&
509 [ + + ]: 908549 : !change_useless_for_repack(buf))
510 : 908121 : DecodeInsert(ctx, buf);
511 : 908747 : break;
512 : :
513 : : /*
514 : : * Treat HOT update as normal updates. There is no useful
515 : : * information in the fact that we could make it a HOT update
516 : : * locally and the WAL layout is compatible.
517 : : */
518 : 162309 : case XLOG_HEAP_HOT_UPDATE:
519 : : case XLOG_HEAP_UPDATE:
520 [ + - ]: 162309 : if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
521 [ + + ]: 162309 : !ctx->fast_forward &&
522 [ + + ]: 162296 : !change_useless_for_repack(buf))
523 : 162229 : DecodeUpdate(ctx, buf);
524 : 162309 : break;
525 : :
526 : 220171 : case XLOG_HEAP_DELETE:
527 [ + - ]: 220171 : if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
528 [ + + ]: 220171 : !ctx->fast_forward &&
529 [ + + ]: 220077 : !change_useless_for_repack(buf))
530 : 220025 : DecodeDelete(ctx, buf);
531 : 220171 : break;
532 : :
533 : 57 : case XLOG_HEAP_TRUNCATE:
534 [ + - ]: 57 : if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
535 [ + + ]: 57 : !ctx->fast_forward &&
536 [ + - ]: 55 : !change_useless_for_repack(buf))
537 : 55 : DecodeTruncate(ctx, buf);
538 : 57 : break;
539 : :
540 : 1435 : case XLOG_HEAP_INPLACE:
541 : :
542 : : /*
543 : : * Inplace updates are only ever performed on catalog tuples and
544 : : * can, per definition, not change tuple visibility. Since we
545 : : * also don't decode catalog tuples, we're not interested in the
546 : : * record's contents.
547 : : */
548 : 1435 : break;
549 : :
550 : 19702 : case XLOG_HEAP_CONFIRM:
551 [ + - ]: 19702 : if (SnapBuildProcessChange(builder, xid, buf->origptr) &&
552 [ + - ]: 19702 : !ctx->fast_forward &&
553 [ + - ]: 19702 : !change_useless_for_repack(buf))
554 : 19702 : DecodeSpecConfirm(ctx, buf);
555 : 19702 : break;
556 : :
557 : 170011 : case XLOG_HEAP_LOCK:
558 : : /* we don't care about row level locks for now */
559 : 170011 : break;
560 : :
561 : 0 : default:
562 [ # # ]: 0 : elog(ERROR, "unexpected RM_HEAP_ID record type: %u", info);
563 : : break;
564 : : }
565 : : }
566 : :
567 : : /*
568 : : * Ask output plugin whether we want to skip this PREPARE and send
569 : : * this transaction as a regular commit later.
570 : : */
571 : : static inline bool
572 : 384 : FilterPrepare(LogicalDecodingContext *ctx, TransactionId xid,
573 : : const char *gid)
574 : : {
575 : : /*
576 : : * Skip if decoding of two-phase transactions at PREPARE time is not
577 : : * enabled. In that case, all two-phase transactions are considered
578 : : * filtered out and will be applied as regular transactions at COMMIT
579 : : * PREPARED.
580 : : */
581 [ + + ]: 384 : if (!ctx->twophase)
582 : 23 : return true;
583 : :
584 : : /*
585 : : * The filter_prepare callback is optional. When not supplied, all
586 : : * prepared transactions should go through.
587 : : */
588 [ + + ]: 361 : if (ctx->callbacks.filter_prepare_cb == NULL)
589 : 191 : return false;
590 : :
591 : 170 : return filter_prepare_cb_wrapper(ctx, xid, gid);
592 : : }
593 : :
594 : : static inline bool
595 : 1305100 : FilterByOrigin(LogicalDecodingContext *ctx, ReplOriginId origin_id)
596 : : {
597 [ + + ]: 1305100 : if (ctx->callbacks.filter_by_origin_cb == NULL)
598 : 232 : return false;
599 : :
600 : 1304868 : return filter_by_origin_cb_wrapper(ctx, origin_id);
601 : : }
602 : :
603 : : /*
604 : : * Handle rmgr LOGICALMSG_ID records for LogicalDecodingProcessRecord().
605 : : */
606 : : void
607 : 100 : logicalmsg_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
608 : : {
609 : 100 : SnapBuild *builder = ctx->snapshot_builder;
610 : 100 : XLogReaderState *r = buf->record;
611 : 100 : TransactionId xid = XLogRecGetXid(r);
612 : 100 : uint8 info = XLogRecGetInfo(r) & ~XLR_INFO_MASK;
613 : 100 : ReplOriginId origin_id = XLogRecGetOrigin(r);
614 : 100 : Snapshot snapshot = NULL;
615 : : xl_logical_message *message;
616 : :
617 [ - + ]: 100 : if (info != XLOG_LOGICAL_MESSAGE)
618 [ # # ]: 0 : elog(ERROR, "unexpected RM_LOGICALMSG_ID record type: %u", info);
619 : :
620 : 100 : ReorderBufferProcessXid(ctx->reorder, XLogRecGetXid(r), buf->origptr);
621 : :
622 : : /* If we don't have snapshot, there is no point in decoding messages */
623 [ - + ]: 100 : if (SnapBuildCurrentState(builder) < SNAPBUILD_FULL_SNAPSHOT)
624 : 0 : return;
625 : :
626 : 100 : message = (xl_logical_message *) XLogRecGetData(r);
627 : :
628 [ + + + + ]: 198 : if (message->dbId != ctx->slot->data.database ||
629 : 98 : FilterByOrigin(ctx, origin_id))
630 : 4 : return;
631 : :
632 [ + + ]: 96 : if (message->transactional &&
633 [ - + ]: 41 : !SnapBuildProcessChange(builder, xid, buf->origptr))
634 : 0 : return;
635 [ + + + - ]: 151 : else if (!message->transactional &&
636 [ + + ]: 110 : (SnapBuildCurrentState(builder) != SNAPBUILD_CONSISTENT ||
637 : 55 : SnapBuildXactNeedsSkip(builder, buf->origptr)))
638 : 44 : return;
639 : :
640 : : /*
641 : : * We also skip decoding in fast_forward mode. This check must be last
642 : : * because we don't want to set the processing_required flag unless we
643 : : * have a decodable message.
644 : : */
645 [ + + ]: 52 : if (ctx->fast_forward)
646 : : {
647 : : /*
648 : : * We need to set processing_required flag to notify the message's
649 : : * existence to the caller. Usually, the flag is set when either the
650 : : * COMMIT or ABORT records are decoded, but this must be turned on
651 : : * here because the non-transactional logical message is decoded
652 : : * without waiting for these records.
653 : : */
654 [ + - ]: 3 : if (!message->transactional)
655 : 3 : ctx->processing_required = true;
656 : :
657 : 3 : return;
658 : : }
659 : :
660 : : /*
661 : : * If this is a non-transactional change, get the snapshot we're expected
662 : : * to use. We only get here when the snapshot is consistent, and the
663 : : * change is not meant to be skipped.
664 : : *
665 : : * For transactional changes we don't need a snapshot, we'll use the
666 : : * regular snapshot maintained by ReorderBuffer. We just leave it NULL.
667 : : */
668 [ + + ]: 49 : if (!message->transactional)
669 : 8 : snapshot = SnapBuildGetOrBuildSnapshot(builder);
670 : :
671 : 49 : ReorderBufferQueueMessage(ctx->reorder, xid, snapshot, buf->endptr,
672 : 49 : message->transactional,
673 : 49 : message->message, /* first part of message is
674 : : * prefix */
675 : : message->message_size,
676 : 49 : message->message + message->prefix_size);
677 : : }
678 : :
679 : : /*
680 : : * Consolidated commit record handling between the different form of commit
681 : : * records.
682 : : *
683 : : * 'two_phase' indicates that caller wants to process the transaction in two
684 : : * phases, first process prepare if not already done and then process
685 : : * commit_prepared.
686 : : */
687 : : static void
688 : 4616 : DecodeCommit(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
689 : : xl_xact_parsed_commit *parsed, TransactionId xid,
690 : : bool two_phase)
691 : : {
692 : 4616 : XLogRecPtr origin_lsn = InvalidXLogRecPtr;
693 : 4616 : TimestampTz commit_time = parsed->xact_time;
694 : 4616 : ReplOriginId origin_id = XLogRecGetOrigin(buf->record);
695 : : int i;
696 : :
697 [ + + ]: 4616 : if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
698 : : {
699 : 86 : origin_lsn = parsed->origin_lsn;
700 : 86 : commit_time = parsed->origin_timestamp;
701 : : }
702 : :
703 : 4616 : SnapBuildCommitTxn(ctx->snapshot_builder, buf->origptr, xid,
704 : : parsed->nsubxacts, parsed->subxacts,
705 : : parsed->xinfo);
706 : :
707 : : /* ----
708 : : * Check whether we are interested in this specific transaction, and tell
709 : : * the reorderbuffer to forget the content of the (sub-)transactions
710 : : * if not.
711 : : *
712 : : * We can't just use ReorderBufferAbort() here, because we need to execute
713 : : * the transaction's invalidations. This currently won't be needed if
714 : : * we're just skipping over the transaction because currently we only do
715 : : * so during startup, to get to the first transaction the client needs. As
716 : : * we have reset the catalog caches before starting to read WAL, and we
717 : : * haven't yet touched any catalogs, there can't be anything to invalidate.
718 : : * But if we're "forgetting" this commit because it happened in another
719 : : * database, the invalidations might be important, because they could be
720 : : * for shared catalogs and we might have loaded data into the relevant
721 : : * syscaches.
722 : : * ---
723 : : */
724 [ + + ]: 4616 : if (DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id))
725 : : {
726 [ + + ]: 3118 : for (i = 0; i < parsed->nsubxacts; i++)
727 : : {
728 : 1037 : ReorderBufferForget(ctx->reorder, parsed->subxacts[i], buf->origptr);
729 : : }
730 : 2081 : ReorderBufferForget(ctx->reorder, xid, buf->origptr);
731 : :
732 : 2081 : return;
733 : : }
734 : :
735 : : /* tell the reorderbuffer about the surviving subtransactions */
736 [ + + ]: 2806 : for (i = 0; i < parsed->nsubxacts; i++)
737 : : {
738 : 271 : ReorderBufferCommitChild(ctx->reorder, xid, parsed->subxacts[i],
739 : : buf->origptr, buf->endptr);
740 : : }
741 : :
742 : : /*
743 : : * Send the final commit record if the transaction data is already
744 : : * decoded, otherwise, process the entire transaction.
745 : : */
746 [ + + ]: 2535 : if (two_phase)
747 : : {
748 : 36 : ReorderBufferFinishPrepared(ctx->reorder, xid, buf->origptr, buf->endptr,
749 : 36 : SnapBuildGetTwoPhaseAt(ctx->snapshot_builder),
750 : : commit_time, origin_id, origin_lsn,
751 : 36 : parsed->twophase_gid, true);
752 : : }
753 : : else
754 : : {
755 : 2499 : ReorderBufferCommit(ctx->reorder, xid, buf->origptr, buf->endptr,
756 : : commit_time, origin_id, origin_lsn);
757 : : }
758 : :
759 : : /*
760 : : * Update the decoding stats at transaction prepare/commit/abort.
761 : : * Additionally we send the stats when we spill or stream the changes to
762 : : * avoid losing them in case the decoding is interrupted. It is not clear
763 : : * that sending more or less frequently than this would be better.
764 : : */
765 : 2519 : UpdateDecodingStats(ctx);
766 : : }
767 : :
768 : : /*
769 : : * Decode PREPARE record. Similar logic as in DecodeCommit.
770 : : *
771 : : * Note that we don't skip prepare even if have detected concurrent abort
772 : : * because it is quite possible that we had already sent some changes before we
773 : : * detect abort in which case we need to abort those changes in the subscriber.
774 : : * To abort such changes, we do send the prepare and then the rollback prepared
775 : : * which is what happened on the publisher-side as well. Now, we can invent a
776 : : * new abort API wherein in such cases we send abort and skip sending prepared
777 : : * and rollback prepared but then it is not that straightforward because we
778 : : * might have streamed this transaction by that time in which case it is
779 : : * handled when the rollback is encountered. It is not impossible to optimize
780 : : * the concurrent abort case but it can introduce design complexity w.r.t
781 : : * handling different cases so leaving it for now as it doesn't seem worth it.
782 : : */
783 : : static void
784 : 183 : DecodePrepare(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
785 : : xl_xact_parsed_prepare *parsed)
786 : : {
787 : 183 : SnapBuild *builder = ctx->snapshot_builder;
788 : 183 : XLogRecPtr origin_lsn = parsed->origin_lsn;
789 : 183 : TimestampTz prepare_time = parsed->xact_time;
790 : 183 : ReplOriginId origin_id = XLogRecGetOrigin(buf->record);
791 : : int i;
792 : 183 : TransactionId xid = parsed->twophase_xid;
793 : :
794 [ + + ]: 183 : if (parsed->origin_timestamp != 0)
795 : 8 : prepare_time = parsed->origin_timestamp;
796 : :
797 : : /*
798 : : * Remember the prepare info for a txn so that it can be used later in
799 : : * commit prepared if required. See ReorderBufferFinishPrepared.
800 : : */
801 [ - + ]: 183 : if (!ReorderBufferRememberPrepareInfo(ctx->reorder, xid, buf->origptr,
802 : : buf->endptr, prepare_time, origin_id,
803 : : origin_lsn))
804 : 0 : return;
805 : :
806 : : /* We can't start streaming unless a consistent state is reached. */
807 [ + + ]: 183 : if (SnapBuildCurrentState(builder) < SNAPBUILD_CONSISTENT)
808 : : {
809 : 3 : ReorderBufferSkipPrepare(ctx->reorder, xid);
810 : 3 : return;
811 : : }
812 : :
813 : : /*
814 : : * Check whether we need to process this transaction. See
815 : : * DecodeTXNNeedSkip for the reasons why we sometimes want to skip the
816 : : * transaction.
817 : : *
818 : : * We can't call ReorderBufferForget as we did in DecodeCommit as the txn
819 : : * hasn't yet been committed, removing this txn before a commit might
820 : : * result in the computation of an incorrect restart_lsn. See
821 : : * SnapBuildProcessRunningXacts. But we need to process cache
822 : : * invalidations if there are any for the reasons mentioned in
823 : : * DecodeCommit.
824 : : */
825 [ + + ]: 180 : if (DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id))
826 : : {
827 : 131 : ReorderBufferSkipPrepare(ctx->reorder, xid);
828 : 131 : ReorderBufferInvalidate(ctx->reorder, xid, buf->origptr);
829 : 131 : return;
830 : : }
831 : :
832 : : /* Tell the reorderbuffer about the surviving subtransactions. */
833 [ + + ]: 50 : for (i = 0; i < parsed->nsubxacts; i++)
834 : : {
835 : 1 : ReorderBufferCommitChild(ctx->reorder, xid, parsed->subxacts[i],
836 : : buf->origptr, buf->endptr);
837 : : }
838 : :
839 : : /* replay actions of all transaction + subtransactions in order */
840 : 49 : ReorderBufferPrepare(ctx->reorder, xid, parsed->twophase_gid);
841 : :
842 : : /*
843 : : * Update the decoding stats at transaction prepare/commit/abort.
844 : : * Additionally we send the stats when we spill or stream the changes to
845 : : * avoid losing them in case the decoding is interrupted. It is not clear
846 : : * that sending more or less frequently than this would be better.
847 : : */
848 : 49 : UpdateDecodingStats(ctx);
849 : : }
850 : :
851 : :
852 : : /*
853 : : * Get the data from the various forms of abort records and pass it on to
854 : : * snapbuild.c and reorderbuffer.c.
855 : : *
856 : : * 'two_phase' indicates to finish prepared transaction.
857 : : */
858 : : static void
859 : 326 : DecodeAbort(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
860 : : xl_xact_parsed_abort *parsed, TransactionId xid,
861 : : bool two_phase)
862 : : {
863 : 326 : XLogRecPtr origin_lsn = InvalidXLogRecPtr;
864 : 326 : TimestampTz abort_time = parsed->xact_time;
865 : 326 : ReplOriginId origin_id = XLogRecGetOrigin(buf->record);
866 : : bool skip_xact;
867 : :
868 [ + + ]: 326 : if (parsed->xinfo & XACT_XINFO_HAS_ORIGIN)
869 : : {
870 : 4 : origin_lsn = parsed->origin_lsn;
871 : 4 : abort_time = parsed->origin_timestamp;
872 : : }
873 : :
874 : : /*
875 : : * Check whether we need to process this transaction. See
876 : : * DecodeTXNNeedSkip for the reasons why we sometimes want to skip the
877 : : * transaction.
878 : : */
879 : 326 : skip_xact = DecodeTXNNeedSkip(ctx, buf, parsed->dbId, origin_id);
880 : :
881 : : /*
882 : : * Send the final rollback record for a prepared transaction unless we
883 : : * need to skip it. For non-two-phase xacts, simply forget the xact.
884 : : */
885 [ + + + + ]: 326 : if (two_phase && !skip_xact)
886 : : {
887 : 13 : ReorderBufferFinishPrepared(ctx->reorder, xid, buf->origptr, buf->endptr,
888 : : InvalidXLogRecPtr,
889 : : abort_time, origin_id, origin_lsn,
890 : 13 : parsed->twophase_gid, false);
891 : : }
892 : : else
893 : : {
894 : : /*
895 : : * Remove tuplecid changes queued by the aborted subtransactions from
896 : : * the toplevel's list.
897 : : */
898 : 313 : ReorderBufferCleanupAbortedTupleCids(ctx->reorder, xid,
899 : : parsed->nsubxacts,
900 : : parsed->subxacts);
901 : :
902 [ + + ]: 321 : for (int i = 0; i < parsed->nsubxacts; i++)
903 : : {
904 : 8 : ReorderBufferAbort(ctx->reorder, parsed->subxacts[i],
905 : 8 : buf->record->EndRecPtr, abort_time);
906 : : }
907 : :
908 : 313 : ReorderBufferAbort(ctx->reorder, xid, buf->record->EndRecPtr,
909 : : abort_time);
910 : : }
911 : :
912 : : /* update the decoding stats */
913 : 326 : UpdateDecodingStats(ctx);
914 : 326 : }
915 : :
916 : : /*
917 : : * Parse XLOG_HEAP_INSERT (not MULTI_INSERT!) records into tuplebufs.
918 : : *
919 : : * Inserts can contain the new tuple.
920 : : */
921 : : static void
922 : 908121 : DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
923 : : {
924 : : Size datalen;
925 : : char *tupledata;
926 : : Size tuplelen;
927 : 908121 : XLogReaderState *r = buf->record;
928 : : xl_heap_insert *xlrec;
929 : : ReorderBufferChange *change;
930 : : RelFileLocator target_locator;
931 : :
932 : 908121 : xlrec = (xl_heap_insert *) XLogRecGetData(r);
933 : :
934 : : /*
935 : : * Ignore insert records without new tuples (this does happen when
936 : : * raw_heap_insert marks the TOAST record as HEAP_INSERT_NO_LOGICAL).
937 : : */
938 [ + + ]: 908121 : if (!(xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE))
939 : 5607 : return;
940 : :
941 : : /* only interested in our database */
942 : 902722 : XLogRecGetBlockTag(r, 0, &target_locator, NULL, NULL);
943 [ - + ]: 902722 : if (target_locator.dbOid != ctx->slot->data.database)
944 : 0 : return;
945 : :
946 : : /* output plugin doesn't look for this origin, no need to queue */
947 [ + + ]: 902722 : if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
948 : 208 : return;
949 : :
950 : 902514 : change = ReorderBufferAllocChange(ctx->reorder);
951 [ + + ]: 902514 : if (!(xlrec->flags & XLH_INSERT_IS_SPECULATIVE))
952 : 882812 : change->action = REORDER_BUFFER_CHANGE_INSERT;
953 : : else
954 : 19702 : change->action = REORDER_BUFFER_CHANGE_INTERNAL_SPEC_INSERT;
955 : 902514 : change->origin_id = XLogRecGetOrigin(r);
956 : :
957 : 902514 : memcpy(&change->data.tp.rlocator, &target_locator, sizeof(RelFileLocator));
958 : :
959 : 902514 : tupledata = XLogRecGetBlockData(r, 0, &datalen);
960 : 902514 : tuplelen = datalen - SizeOfHeapHeader;
961 : :
962 : 902514 : change->data.tp.newtuple =
963 : 902514 : ReorderBufferAllocTupleBuf(ctx->reorder, tuplelen);
964 : :
965 : 902514 : DecodeXLogTuple(tupledata, datalen, change->data.tp.newtuple);
966 : :
967 : 902514 : change->data.tp.clear_toast_afterwards = true;
968 : :
969 : 902514 : ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r), buf->origptr,
970 : : change,
971 : 902514 : xlrec->flags & XLH_INSERT_ON_TOAST_RELATION);
972 : : }
973 : :
974 : : /*
975 : : * Parse XLOG_HEAP_UPDATE and XLOG_HEAP_HOT_UPDATE, which have the same layout
976 : : * in the record, from wal into proper tuplebufs.
977 : : *
978 : : * Updates can possibly contain a new tuple and the old primary key.
979 : : */
980 : : static void
981 : 162229 : DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
982 : : {
983 : 162229 : XLogReaderState *r = buf->record;
984 : : xl_heap_update *xlrec;
985 : : ReorderBufferChange *change;
986 : : char *data;
987 : : RelFileLocator target_locator;
988 : :
989 : 162229 : xlrec = (xl_heap_update *) XLogRecGetData(r);
990 : :
991 : : /*
992 : : * Ignore update records without a new tuple. This happens when the update
993 : : * is done on a catalog relation, or when the caller of heap_update()
994 : : * asked for the change not to be decoded, as REPACK (CONCURRENTLY) does
995 : : * for the transient heap.
996 : : */
997 [ + + ]: 162229 : if (!(xlrec->flags & XLH_UPDATE_CONTAINS_NEW_TUPLE))
998 : 2471 : return;
999 : :
1000 : : /* only interested in our database */
1001 : 159759 : XLogRecGetBlockTag(r, 0, &target_locator, NULL, NULL);
1002 [ - + ]: 159759 : if (target_locator.dbOid != ctx->slot->data.database)
1003 : 0 : return;
1004 : :
1005 : : /* output plugin doesn't look for this origin, no need to queue */
1006 [ + + ]: 159759 : if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1007 : 1 : return;
1008 : :
1009 : 159758 : change = ReorderBufferAllocChange(ctx->reorder);
1010 : 159758 : change->action = REORDER_BUFFER_CHANGE_UPDATE;
1011 : 159758 : change->origin_id = XLogRecGetOrigin(r);
1012 : 159758 : memcpy(&change->data.tp.rlocator, &target_locator, sizeof(RelFileLocator));
1013 : :
1014 [ + - ]: 159758 : if (xlrec->flags & XLH_UPDATE_CONTAINS_NEW_TUPLE)
1015 : : {
1016 : : Size datalen;
1017 : : Size tuplelen;
1018 : :
1019 : 159758 : data = XLogRecGetBlockData(r, 0, &datalen);
1020 : :
1021 : 159758 : tuplelen = datalen - SizeOfHeapHeader;
1022 : :
1023 : 159758 : change->data.tp.newtuple =
1024 : 159758 : ReorderBufferAllocTupleBuf(ctx->reorder, tuplelen);
1025 : :
1026 : 159758 : DecodeXLogTuple(data, datalen, change->data.tp.newtuple);
1027 : : }
1028 : :
1029 [ + + ]: 159758 : if (xlrec->flags & XLH_UPDATE_CONTAINS_OLD)
1030 : : {
1031 : : Size datalen;
1032 : : Size tuplelen;
1033 : :
1034 : : /* caution, remaining data in record is not aligned */
1035 : 362 : data = XLogRecGetData(r) + SizeOfHeapUpdate;
1036 : 362 : datalen = XLogRecGetDataLen(r) - SizeOfHeapUpdate;
1037 : 362 : tuplelen = datalen - SizeOfHeapHeader;
1038 : :
1039 : 362 : change->data.tp.oldtuple =
1040 : 362 : ReorderBufferAllocTupleBuf(ctx->reorder, tuplelen);
1041 : :
1042 : 362 : DecodeXLogTuple(data, datalen, change->data.tp.oldtuple);
1043 : : }
1044 : :
1045 : 159758 : change->data.tp.clear_toast_afterwards = true;
1046 : :
1047 : 159758 : ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r), buf->origptr,
1048 : : change, false);
1049 : : }
1050 : :
1051 : : /*
1052 : : * Parse XLOG_HEAP_DELETE from wal into proper tuplebufs.
1053 : : *
1054 : : * Deletes can possibly contain the old primary key.
1055 : : */
1056 : : static void
1057 : 220025 : DecodeDelete(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
1058 : : {
1059 : 220025 : XLogReaderState *r = buf->record;
1060 : : xl_heap_delete *xlrec;
1061 : : ReorderBufferChange *change;
1062 : : RelFileLocator target_locator;
1063 : :
1064 : 220025 : xlrec = (xl_heap_delete *) XLogRecGetData(r);
1065 : :
1066 : : /*
1067 : : * Skip changes that were marked as ignorable at origin.
1068 : : *
1069 : : * (This is used for changes that affect relations not visible to other
1070 : : * transactions, such as the transient table during concurrent repack.)
1071 : : */
1072 [ - + ]: 220025 : if (xlrec->flags & XLH_DELETE_NO_LOGICAL)
1073 : 66 : return;
1074 : :
1075 : : /* only interested in our database */
1076 : 220025 : XLogRecGetBlockTag(r, 0, &target_locator, NULL, NULL);
1077 [ + + ]: 220025 : if (target_locator.dbOid != ctx->slot->data.database)
1078 : 48 : return;
1079 : :
1080 : : /* output plugin doesn't look for this origin, no need to queue */
1081 [ + + ]: 219977 : if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1082 : 18 : return;
1083 : :
1084 : 219959 : change = ReorderBufferAllocChange(ctx->reorder);
1085 : :
1086 [ - + ]: 219959 : if (xlrec->flags & XLH_DELETE_IS_SUPER)
1087 : 0 : change->action = REORDER_BUFFER_CHANGE_INTERNAL_SPEC_ABORT;
1088 : : else
1089 : 219959 : change->action = REORDER_BUFFER_CHANGE_DELETE;
1090 : :
1091 : 219959 : change->origin_id = XLogRecGetOrigin(r);
1092 : :
1093 : 219959 : memcpy(&change->data.tp.rlocator, &target_locator, sizeof(RelFileLocator));
1094 : :
1095 : : /* old primary key stored */
1096 [ + + ]: 219959 : if (xlrec->flags & XLH_DELETE_CONTAINS_OLD)
1097 : : {
1098 : 150885 : Size datalen = XLogRecGetDataLen(r) - SizeOfHeapDelete;
1099 : 150885 : Size tuplelen = datalen - SizeOfHeapHeader;
1100 : :
1101 : : Assert(XLogRecGetDataLen(r) > (SizeOfHeapDelete + SizeOfHeapHeader));
1102 : :
1103 : 150885 : change->data.tp.oldtuple =
1104 : 150885 : ReorderBufferAllocTupleBuf(ctx->reorder, tuplelen);
1105 : :
1106 : 150885 : DecodeXLogTuple((char *) xlrec + SizeOfHeapDelete,
1107 : : datalen, change->data.tp.oldtuple);
1108 : : }
1109 : :
1110 : 219959 : change->data.tp.clear_toast_afterwards = true;
1111 : :
1112 : 219959 : ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r), buf->origptr,
1113 : : change, false);
1114 : : }
1115 : :
1116 : : /*
1117 : : * Parse XLOG_HEAP_TRUNCATE from wal
1118 : : */
1119 : : static void
1120 : 55 : DecodeTruncate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
1121 : : {
1122 : 55 : XLogReaderState *r = buf->record;
1123 : : xl_heap_truncate *xlrec;
1124 : : ReorderBufferChange *change;
1125 : :
1126 : 55 : xlrec = (xl_heap_truncate *) XLogRecGetData(r);
1127 : :
1128 : : /* only interested in our database */
1129 [ - + ]: 55 : if (xlrec->dbId != ctx->slot->data.database)
1130 : 0 : return;
1131 : :
1132 : : /* output plugin doesn't look for this origin, no need to queue */
1133 [ + + ]: 55 : if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1134 : 1 : return;
1135 : :
1136 : 54 : change = ReorderBufferAllocChange(ctx->reorder);
1137 : 54 : change->action = REORDER_BUFFER_CHANGE_TRUNCATE;
1138 : 54 : change->origin_id = XLogRecGetOrigin(r);
1139 [ + + ]: 54 : if (xlrec->flags & XLH_TRUNCATE_CASCADE)
1140 : 1 : change->data.truncate.cascade = true;
1141 [ + + ]: 54 : if (xlrec->flags & XLH_TRUNCATE_RESTART_SEQS)
1142 : 2 : change->data.truncate.restart_seqs = true;
1143 : 54 : change->data.truncate.nrelids = xlrec->nrelids;
1144 : 108 : change->data.truncate.relids = ReorderBufferAllocRelids(ctx->reorder,
1145 : 54 : xlrec->nrelids);
1146 : 54 : memcpy(change->data.truncate.relids, xlrec->relids,
1147 : 54 : xlrec->nrelids * sizeof(Oid));
1148 : 54 : ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r),
1149 : : buf->origptr, change, false);
1150 : : }
1151 : :
1152 : : /*
1153 : : * Decode XLOG_HEAP2_MULTI_INSERT record into multiple tuplebufs.
1154 : : *
1155 : : * Currently MULTI_INSERT will always contain the full tuples.
1156 : : */
1157 : : static void
1158 : 7781 : DecodeMultiInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
1159 : : {
1160 : 7781 : XLogReaderState *r = buf->record;
1161 : : xl_heap_multi_insert *xlrec;
1162 : : int i;
1163 : : char *data;
1164 : : char *tupledata;
1165 : : Size tuplelen;
1166 : : RelFileLocator rlocator;
1167 : :
1168 : 7781 : xlrec = (xl_heap_multi_insert *) XLogRecGetData(r);
1169 : :
1170 : : /*
1171 : : * Ignore insert records without new tuples. This happens when a
1172 : : * multi_insert is done on a catalog or on a non-persistent relation.
1173 : : */
1174 [ + + ]: 7781 : if (!(xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE))
1175 : 7763 : return;
1176 : :
1177 : : /* only interested in our database */
1178 : 51 : XLogRecGetBlockTag(r, 0, &rlocator, NULL, NULL);
1179 [ + + ]: 51 : if (rlocator.dbOid != ctx->slot->data.database)
1180 : 33 : return;
1181 : :
1182 : : /* output plugin doesn't look for this origin, no need to queue */
1183 [ - + ]: 18 : if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1184 : 0 : return;
1185 : :
1186 : : /*
1187 : : * We know that this multi_insert isn't for a catalog, so the block should
1188 : : * always have data even if a full-page write of it is taken.
1189 : : */
1190 : 18 : tupledata = XLogRecGetBlockData(r, 0, &tuplelen);
1191 : : Assert(tupledata != NULL);
1192 : :
1193 : 18 : data = tupledata;
1194 [ + + ]: 1272 : for (i = 0; i < xlrec->ntuples; i++)
1195 : : {
1196 : : ReorderBufferChange *change;
1197 : : xl_multi_insert_tuple *xlhdr;
1198 : : int datalen;
1199 : : HeapTuple tuple;
1200 : : HeapTupleHeader header;
1201 : :
1202 : 1254 : change = ReorderBufferAllocChange(ctx->reorder);
1203 : 1254 : change->action = REORDER_BUFFER_CHANGE_INSERT;
1204 : 1254 : change->origin_id = XLogRecGetOrigin(r);
1205 : :
1206 : 1254 : memcpy(&change->data.tp.rlocator, &rlocator, sizeof(RelFileLocator));
1207 : :
1208 : 1254 : xlhdr = (xl_multi_insert_tuple *) SHORTALIGN(data);
1209 : 1254 : data = ((char *) xlhdr) + SizeOfMultiInsertTuple;
1210 : 1254 : datalen = xlhdr->datalen;
1211 : :
1212 : 1254 : change->data.tp.newtuple =
1213 : 1254 : ReorderBufferAllocTupleBuf(ctx->reorder, datalen);
1214 : :
1215 : 1254 : tuple = change->data.tp.newtuple;
1216 : 1254 : header = tuple->t_data;
1217 : :
1218 : : /* not a disk based tuple */
1219 : 1254 : ItemPointerSetInvalid(&tuple->t_self);
1220 : :
1221 : : /*
1222 : : * We can only figure this out after reassembling the transactions.
1223 : : */
1224 : 1254 : tuple->t_tableOid = InvalidOid;
1225 : :
1226 : 1254 : tuple->t_len = datalen + SizeofHeapTupleHeader;
1227 : :
1228 : 1254 : memset(header, 0, SizeofHeapTupleHeader);
1229 : :
1230 : 1254 : memcpy((char *) tuple->t_data + SizeofHeapTupleHeader, data, datalen);
1231 : 1254 : header->t_infomask = xlhdr->t_infomask;
1232 : 1254 : header->t_infomask2 = xlhdr->t_infomask2;
1233 : 1254 : header->t_hoff = xlhdr->t_hoff;
1234 : :
1235 : : /*
1236 : : * Reset toast reassembly state only after the last row in the last
1237 : : * xl_multi_insert_tuple record emitted by one heap_multi_insert()
1238 : : * call.
1239 : : */
1240 [ + + ]: 1254 : if (xlrec->flags & XLH_INSERT_LAST_IN_MULTI &&
1241 [ + + ]: 222 : (i + 1) == xlrec->ntuples)
1242 : 12 : change->data.tp.clear_toast_afterwards = true;
1243 : : else
1244 : 1242 : change->data.tp.clear_toast_afterwards = false;
1245 : :
1246 : 1254 : ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r),
1247 : : buf->origptr, change, false);
1248 : :
1249 : : /* move to the next xl_multi_insert_tuple entry */
1250 : 1254 : data += datalen;
1251 : : }
1252 : : Assert(data == tupledata + tuplelen);
1253 : : }
1254 : :
1255 : : /*
1256 : : * Parse XLOG_HEAP_CONFIRM from wal into a confirmation change.
1257 : : *
1258 : : * This is pretty trivial, all the state essentially already setup by the
1259 : : * speculative insertion.
1260 : : */
1261 : : static void
1262 : 19702 : DecodeSpecConfirm(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
1263 : : {
1264 : 19702 : XLogReaderState *r = buf->record;
1265 : : ReorderBufferChange *change;
1266 : : RelFileLocator target_locator;
1267 : :
1268 : : /* only interested in our database */
1269 : 19702 : XLogRecGetBlockTag(r, 0, &target_locator, NULL, NULL);
1270 [ - + ]: 19702 : if (target_locator.dbOid != ctx->slot->data.database)
1271 : 0 : return;
1272 : :
1273 : : /* output plugin doesn't look for this origin, no need to queue */
1274 [ - + ]: 19702 : if (FilterByOrigin(ctx, XLogRecGetOrigin(r)))
1275 : 0 : return;
1276 : :
1277 : 19702 : change = ReorderBufferAllocChange(ctx->reorder);
1278 : 19702 : change->action = REORDER_BUFFER_CHANGE_INTERNAL_SPEC_CONFIRM;
1279 : 19702 : change->origin_id = XLogRecGetOrigin(r);
1280 : :
1281 : 19702 : memcpy(&change->data.tp.rlocator, &target_locator, sizeof(RelFileLocator));
1282 : :
1283 : 19702 : change->data.tp.clear_toast_afterwards = true;
1284 : :
1285 : 19702 : ReorderBufferQueueChange(ctx->reorder, XLogRecGetXid(r), buf->origptr,
1286 : : change, false);
1287 : : }
1288 : :
1289 : :
1290 : : /*
1291 : : * Read a HeapTuple as WAL logged by heap_insert, heap_update and heap_delete
1292 : : * (but not by heap_multi_insert) into a tuplebuf.
1293 : : *
1294 : : * The size 'len' and the pointer 'data' in the record need to be
1295 : : * computed outside as they are record specific.
1296 : : */
1297 : : static void
1298 : 1213519 : DecodeXLogTuple(char *data, Size len, HeapTuple tuple)
1299 : : {
1300 : : xl_heap_header xlhdr;
1301 : 1213519 : int datalen = len - SizeOfHeapHeader;
1302 : : HeapTupleHeader header;
1303 : :
1304 : : Assert(datalen >= 0);
1305 : :
1306 : 1213519 : tuple->t_len = datalen + SizeofHeapTupleHeader;
1307 : 1213519 : header = tuple->t_data;
1308 : :
1309 : : /* not a disk based tuple */
1310 : 1213519 : ItemPointerSetInvalid(&tuple->t_self);
1311 : :
1312 : : /* we can only figure this out after reassembling the transactions */
1313 : 1213519 : tuple->t_tableOid = InvalidOid;
1314 : :
1315 : : /* data is not stored aligned, copy to aligned storage */
1316 : 1213519 : memcpy(&xlhdr, data, SizeOfHeapHeader);
1317 : :
1318 : 1213519 : memset(header, 0, SizeofHeapTupleHeader);
1319 : :
1320 : 1213519 : memcpy(((char *) tuple->t_data) + SizeofHeapTupleHeader,
1321 : 1213519 : data + SizeOfHeapHeader,
1322 : : datalen);
1323 : :
1324 : 1213519 : header->t_infomask = xlhdr.t_infomask;
1325 : 1213519 : header->t_infomask2 = xlhdr.t_infomask2;
1326 : 1213519 : header->t_hoff = xlhdr.t_hoff;
1327 : 1213519 : }
1328 : :
1329 : : /*
1330 : : * Check whether we are interested in this specific transaction.
1331 : : *
1332 : : * There can be several reasons we might not be interested in this
1333 : : * transaction:
1334 : : * 1) We might not be interested in decoding transactions up to this
1335 : : * LSN. This can happen because we previously decoded it and now just
1336 : : * are restarting or if we haven't assembled a consistent snapshot yet.
1337 : : * 2) The transaction happened in another database.
1338 : : * 3) The output plugin is not interested in the origin.
1339 : : * 4) We are doing fast-forwarding
1340 : : */
1341 : : static bool
1342 : 5122 : DecodeTXNNeedSkip(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
1343 : : Oid txn_dbid, ReplOriginId origin_id)
1344 : : {
1345 [ + + + + ]: 5122 : if (SnapBuildXactNeedsSkip(ctx->snapshot_builder, buf->origptr) ||
1346 [ + + + + ]: 5474 : (txn_dbid != InvalidOid && txn_dbid != ctx->slot->data.database) ||
1347 : 2769 : FilterByOrigin(ctx, origin_id))
1348 : 2391 : return true;
1349 : :
1350 : : /*
1351 : : * We also skip decoding in fast_forward mode. In passing set the
1352 : : * processing_required flag to indicate that if it were not for
1353 : : * fast_forward mode, processing would have been required.
1354 : : */
1355 [ + + ]: 2731 : if (ctx->fast_forward)
1356 : : {
1357 : 40 : ctx->processing_required = true;
1358 : 40 : return true;
1359 : : }
1360 : :
1361 : 2691 : return false;
1362 : : }
|