Debian Codegraph @

src/backend/access/heap/heapam_xlog.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * heapam_xlog.c
4 * WAL replay logic for heap access method.
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/access/heap/heapam_xlog.c
12 *
13 *-------------------------------------------------------------------------
14 */
15 #include "postgres.h"
16
17 #include "access/bufmask.h"
18 #include "access/heapam.h"
19 #include "access/visibilitymap.h"
20 #include "access/xlog.h"
21 #include "access/xlogutils.h"
22 #include "storage/freespace.h"
23 #include "storage/standby.h"
24
25 /*
26 * Clear visibility map bits for a single heap block during heap redo.
27 *
28 * Used by records that modify one heap block and, at most, its corresponding
29 * VM block (insert, delete, multi_insert, lock). Records that can touch
30 * multiple heap or VM blocks (e.g. updates) replay the VM changes inline
31 * instead.
32 *
33 * 'record' is the WAL record being replayed
34 * 'target_locator' identifies the relation whose VM is being updated
35 * 'heap_blkno' is the heap block whose VM bits should be cleared
36 * 'wal_vm_block_id' is the WAL block reference id of the VM page
37 * 'flags' specifies which visibility map bits to clear
38 */
39 static void
40 heap_xlog_vm_clear(XLogReaderState *record,
41 RelFileLocator target_locator,
42 BlockNumber heap_blkno,
43 uint8 wal_vm_block_id, uint8 flags)
44 {
45 XLogRecPtr lsn = record->EndRecPtr;
46 Relation reln = CreateFakeRelcacheEntry(target_locator);
47 Buffer vmbuffer = InvalidBuffer;
48
49 /*
50 * If the vmbuffer was registered, use the recovery-specific routines to
51 * read it. These will either apply an FPI or indicate that we should
52 * clear the requested bits ourselves.
53 *
54 * Originally, clearing the VM did not register the VM buffers, so since
55 * registering the VM buffer was a bug fix, we keep a fallback path to
56 * support replay of WAL generated from before the fix.
57 */
58 if (XLogRecHasBlockRef(record, wal_vm_block_id))
59 {
60 if (XLogReadBufferForRedo(record, wal_vm_block_id,
61 &vmbuffer) == BLK_NEEDS_REDO)
62 {
63 if (visibilitymap_clear_locked(reln,
64 heap_blkno, vmbuffer,
65 flags))
66 PageSetLSN(BufferGetPage(vmbuffer), lsn);
67 }
68 if (BufferIsValid(vmbuffer))
69 UnlockReleaseBuffer(vmbuffer);
70 }
71 else
72 {
73 /*
74 * This is the backwards compatibility path to clear VM bits for
75 * records predating VM buffer registration. It is also invoked if the
76 * heap page's PD_ALL_VISIBLE was cleared but the VM bits were already
77 * clear. The WAL record flags do not distinguish between these two
78 * situations. Though this is wasted effort, the behavior is
79 * historical and the situation should be rare.
80 */
81 visibilitymap_pin(reln, heap_blkno, &vmbuffer);
82 visibilitymap_clear(reln, heap_blkno, vmbuffer, flags);
83 ReleaseBuffer(vmbuffer);
84 }
85
86 FreeFakeRelcacheEntry(reln);
87 }
88
89
90 /*
91 * Replay XLOG_HEAP2_PRUNE_* records.
92 */
93 static void
94 heap_xlog_prune_freeze(XLogReaderState *record)
95 {
96 XLogRecPtr lsn = record->EndRecPtr;
97 char *maindataptr = XLogRecGetData(record);
98 xl_heap_prune xlrec;
99 Buffer buffer;
100 RelFileLocator rlocator;
101 BlockNumber blkno;
102 XLogRedoAction action;
103
104 XLogRecGetBlockTag(record, 0, &rlocator, NULL, &blkno);
105 memcpy(&xlrec, maindataptr, SizeOfHeapPrune);
106 maindataptr += SizeOfHeapPrune;
107
108 /*
109 * We will take an ordinary exclusive lock or a cleanup lock depending on
110 * whether the XLHP_CLEANUP_LOCK flag is set. With an ordinary exclusive
111 * lock, we better not be doing anything that requires moving existing
112 * tuple data.
113 */
114 Assert((xlrec.flags & XLHP_CLEANUP_LOCK) != 0 ||
115 (xlrec.flags & (XLHP_HAS_REDIRECTIONS | XLHP_HAS_DEAD_ITEMS)) == 0);
116
117 /*
118 * We are about to remove and/or freeze tuples. In Hot Standby mode,
119 * ensure that there are no queries running for which the removed tuples
120 * are still visible or which still consider the frozen xids as running.
121 * The conflict horizon XID comes after xl_heap_prune.
122 */
123 if ((xlrec.flags & XLHP_HAS_CONFLICT_HORIZON) != 0)
124 {
125 TransactionId snapshot_conflict_horizon;
126
127 /* memcpy() because snapshot_conflict_horizon is stored unaligned */
128 memcpy(&snapshot_conflict_horizon, maindataptr, sizeof(TransactionId));
129 maindataptr += sizeof(TransactionId);
130
131 if (InHotStandby)
132 ResolveRecoveryConflictWithSnapshot(snapshot_conflict_horizon,
133 (xlrec.flags & XLHP_IS_CATALOG_REL) != 0,
134 rlocator);
135 }
136
137 /*
138 * If we have a full-page image, restore it and we're done.
139 */
140 action = XLogReadBufferForRedoExtended(record, 0, RBM_NORMAL,
141 (xlrec.flags & XLHP_CLEANUP_LOCK) != 0,
142 &buffer);
143 if (action == BLK_NEEDS_REDO)
144 {
145 Page page = (Page) BufferGetPage(buffer);
146 OffsetNumber *redirected;
147 OffsetNumber *nowdead;
148 OffsetNumber *nowunused;
149 int nredirected;
150 int ndead;
151 int nunused;
152 int nplans;
153 Size datalen;
154 xlhp_freeze_plan *plans;
155 OffsetNumber *frz_offsets;
156 char *dataptr = XLogRecGetBlockData(record, 0, &datalen);
157
158 heap_xlog_deserialize_prune_and_freeze(dataptr, xlrec.flags,
159 &nplans, &plans, &frz_offsets,
160 &nredirected, &redirected,
161 &ndead, &nowdead,
162 &nunused, &nowunused);
163
164 /*
165 * Update all line pointers per the record, and repair fragmentation
166 * if needed.
167 */
168 if (nredirected > 0 || ndead > 0 || nunused > 0)
169 heap_page_prune_execute(buffer,
170 (xlrec.flags & XLHP_CLEANUP_LOCK) == 0,
171 redirected, nredirected,
172 nowdead, ndead,
173 nowunused, nunused);
174
175 /* Freeze tuples */
176 for (int p = 0; p < nplans; p++)
177 {
178 HeapTupleFreeze frz;
179
180 /*
181 * Convert freeze plan representation from WAL record into
182 * per-tuple format used by heap_execute_freeze_tuple
183 */
184 frz.xmax = plans[p].xmax;
185 frz.t_infomask2 = plans[p].t_infomask2;
186 frz.t_infomask = plans[p].t_infomask;
187 frz.frzflags = plans[p].frzflags;
188 frz.offset = InvalidOffsetNumber; /* unused, but be tidy */
189
190 for (int i = 0; i < plans[p].ntuples; i++)
191 {
192 OffsetNumber offset = *(frz_offsets++);
193 ItemId lp;
194 HeapTupleHeader tuple;
195
196 lp = PageGetItemId(page, offset);
197 tuple = (HeapTupleHeader) PageGetItem(page, lp);
198 heap_execute_freeze_tuple(tuple, &frz);
199 }
200 }
201
202 /* There should be no more data */
203 Assert((char *) frz_offsets == dataptr + datalen);
204
205 /*
206 * Note: we don't worry about updating the page's prunability hints.
207 * At worst this will cause an extra prune cycle to occur soon.
208 */
209
210 PageSetLSN(page, lsn);
211 MarkBufferDirty(buffer);
212 }
213
214 /*
215 * If we released any space or line pointers, update the free space map.
216 *
217 * Do this regardless of a full-page image being applied, since the FSM
218 * data is not in the page anyway.
219 */
220 if (BufferIsValid(buffer))
221 {
222 if (xlrec.flags & (XLHP_HAS_REDIRECTIONS |
223 XLHP_HAS_DEAD_ITEMS |
224 XLHP_HAS_NOW_UNUSED_ITEMS))
225 {
226 Size freespace = PageGetHeapFreeSpace(BufferGetPage(buffer));
227
228 UnlockReleaseBuffer(buffer);
229
230 XLogRecordPageWithFreeSpace(rlocator, blkno, freespace);
231 }
232 else
233 UnlockReleaseBuffer(buffer);
234 }
235 }
236
237 /*
238 * Replay XLOG_HEAP2_VISIBLE records.
239 *
240 * The critical integrity requirement here is that we must never end up with
241 * a situation where the visibility map bit is set, and the page-level
242 * PD_ALL_VISIBLE bit is clear. If that were to occur, then a subsequent
243 * page modification would fail to clear the visibility map bit.
244 */
245 static void
246 heap_xlog_visible(XLogReaderState *record)
247 {
248 XLogRecPtr lsn = record->EndRecPtr;
249 xl_heap_visible *xlrec = (xl_heap_visible *) XLogRecGetData(record);
250 Buffer vmbuffer = InvalidBuffer;
251 Buffer buffer;
252 Page page;
253 RelFileLocator rlocator;
254 BlockNumber blkno;
255 XLogRedoAction action;
256
257 Assert((xlrec->flags & VISIBILITYMAP_XLOG_VALID_BITS) == xlrec->flags);
258
259 XLogRecGetBlockTag(record, 1, &rlocator, NULL, &blkno);
260
261 /*
262 * If there are any Hot Standby transactions running that have an xmin
263 * horizon old enough that this page isn't all-visible for them, they
264 * might incorrectly decide that an index-only scan can skip a heap fetch.
265 *
266 * NB: It might be better to throw some kind of "soft" conflict here that
267 * forces any index-only scan that is in flight to perform heap fetches,
268 * rather than killing the transaction outright.
269 */
270 if (InHotStandby)
271 ResolveRecoveryConflictWithSnapshot(xlrec->snapshotConflictHorizon,
272 xlrec->flags & VISIBILITYMAP_XLOG_CATALOG_REL,
273 rlocator);
274
275 /*
276 * Read the heap page, if it still exists. If the heap file has dropped or
277 * truncated later in recovery, we don't need to update the page, but we'd
278 * better still update the visibility map.
279 */
280 action = XLogReadBufferForRedo(record, 1, &buffer);
281 if (action == BLK_NEEDS_REDO)
282 {
283 /*
284 * We don't bump the LSN of the heap page when setting the visibility
285 * map bit (unless checksums or wal_hint_bits is enabled, in which
286 * case we must). This exposes us to torn page hazards, but since
287 * we're not inspecting the existing page contents in any way, we
288 * don't care.
289 */
290 page = BufferGetPage(buffer);
291
292 PageSetAllVisible(page);
293
294 if (XLogHintBitIsNeeded())
295 PageSetLSN(page, lsn);
296
297 MarkBufferDirty(buffer);
298 }
299 else if (action == BLK_RESTORED)
300 {
301 /*
302 * If heap block was backed up, we already restored it and there's
303 * nothing more to do. (This can only happen with checksums or
304 * wal_log_hints enabled.)
305 */
306 }
307
308 if (BufferIsValid(buffer))
309 {
310 Size space = PageGetFreeSpace(BufferGetPage(buffer));
311
312 UnlockReleaseBuffer(buffer);
313
314 /*
315 * Since FSM is not WAL-logged and only updated heuristically, it
316 * easily becomes stale in standbys. If the standby is later promoted
317 * and runs VACUUM, it will skip updating individual free space
318 * figures for pages that became all-visible (or all-frozen, depending
319 * on the vacuum mode,) which is troublesome when FreeSpaceMapVacuum
320 * propagates too optimistic free space values to upper FSM layers;
321 * later inserters try to use such pages only to find out that they
322 * are unusable. This can cause long stalls when there are many such
323 * pages.
324 *
325 * Forestall those problems by updating FSM's idea about a page that
326 * is becoming all-visible or all-frozen.
327 *
328 * Do this regardless of a full-page image being applied, since the
329 * FSM data is not in the page anyway.
330 */
331 if (xlrec->flags & VISIBILITYMAP_VALID_BITS)
332 XLogRecordPageWithFreeSpace(rlocator, blkno, space);
333 }
334
335 /*
336 * Even if we skipped the heap page update due to the LSN interlock, it's
337 * still safe to update the visibility map. Any WAL record that clears
338 * the visibility map bit does so before checking the page LSN, so any
339 * bits that need to be cleared will still be cleared.
340 */
341 if (XLogReadBufferForRedoExtended(record, 0, RBM_ZERO_ON_ERROR, false,
342 &vmbuffer) == BLK_NEEDS_REDO)
343 {
344 Page vmpage = BufferGetPage(vmbuffer);
345 Relation reln;
346 uint8 vmbits;
347
348 /* initialize the page if it was read as zeros */
349 if (PageIsNew(vmpage))
350 PageInit(vmpage, BLCKSZ, 0);
351
352 /* remove VISIBILITYMAP_XLOG_* */
353 vmbits = xlrec->flags & VISIBILITYMAP_VALID_BITS;
354
355 /*
356 * XLogReadBufferForRedoExtended locked the buffer. But
357 * visibilitymap_set will handle locking itself.
358 */
359 LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK);
360
361 reln = CreateFakeRelcacheEntry(rlocator);
362 visibilitymap_pin(reln, blkno, &vmbuffer);
363
364 visibilitymap_set(reln, blkno, InvalidBuffer, lsn, vmbuffer,
365 xlrec->snapshotConflictHorizon, vmbits);
366
367 ReleaseBuffer(vmbuffer);
368 FreeFakeRelcacheEntry(reln);
369 }
370 else if (BufferIsValid(vmbuffer))
371 UnlockReleaseBuffer(vmbuffer);
372 }
373
374 /*
375 * Given an "infobits" field from an XLog record, set the correct bits in the
376 * given infomask and infomask2 for the tuple touched by the record.
377 *
378 * (This is the reverse of compute_infobits).
379 */
380 static void
381 fix_infomask_from_infobits(uint8 infobits, uint16 *infomask, uint16 *infomask2)
382 {
383 *infomask &= ~(HEAP_XMAX_IS_MULTI | HEAP_XMAX_LOCK_ONLY |
384 HEAP_XMAX_KEYSHR_LOCK | HEAP_XMAX_EXCL_LOCK);
385 *infomask2 &= ~HEAP_KEYS_UPDATED;
386
387 if (infobits & XLHL_XMAX_IS_MULTI)
388 *infomask |= HEAP_XMAX_IS_MULTI;
389 if (infobits & XLHL_XMAX_LOCK_ONLY)
390 *infomask |= HEAP_XMAX_LOCK_ONLY;
391 if (infobits & XLHL_XMAX_EXCL_LOCK)
392 *infomask |= HEAP_XMAX_EXCL_LOCK;
393 /* note HEAP_XMAX_SHR_LOCK isn't considered here */
394 if (infobits & XLHL_XMAX_KEYSHR_LOCK)
395 *infomask |= HEAP_XMAX_KEYSHR_LOCK;
396
397 if (infobits & XLHL_KEYS_UPDATED)
398 *infomask2 |= HEAP_KEYS_UPDATED;
399 }
400
401 /*
402 * Replay XLOG_HEAP_DELETE records.
403 */
404 static void
405 heap_xlog_delete(XLogReaderState *record)
406 {
407 XLogRecPtr lsn = record->EndRecPtr;
408 xl_heap_delete *xlrec = (xl_heap_delete *) XLogRecGetData(record);
409 Buffer buffer;
410 Page page;
411 ItemId lp = NULL;
412 HeapTupleHeader htup;
413 BlockNumber blkno;
414 RelFileLocator target_locator;
415 ItemPointerData target_tid;
416
417 XLogRecGetBlockTag(record, HEAP_DELETE_BLKREF_HEAP, &target_locator, NULL,
418 &blkno);
419 ItemPointerSetBlockNumber(&target_tid, blkno);
420 ItemPointerSetOffsetNumber(&target_tid, xlrec->offnum);
421
422 /*
423 * The visibility map may need to be fixed even if the heap page is
424 * already up-to-date.
425 */
426 if (xlrec->flags & XLH_DELETE_ALL_VISIBLE_CLEARED)
427 heap_xlog_vm_clear(record, target_locator,
428 blkno, HEAP_DELETE_BLKREF_VM,
429 VISIBILITYMAP_VALID_BITS);
430
431 if (XLogReadBufferForRedo(record, HEAP_DELETE_BLKREF_HEAP,
432 &buffer) == BLK_NEEDS_REDO)
433 {
434 page = BufferGetPage(buffer);
435
436 if (PageGetMaxOffsetNumber(page) >= xlrec->offnum)
437 lp = PageGetItemId(page, xlrec->offnum);
438
439 if (PageGetMaxOffsetNumber(page) < xlrec->offnum || !ItemIdIsNormal(lp))
440 elog(PANIC, "invalid lp");
441
442 htup = (HeapTupleHeader) PageGetItem(page, lp);
443
444 htup->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED);
445 htup->t_infomask2 &= ~HEAP_KEYS_UPDATED;
446 HeapTupleHeaderClearHotUpdated(htup);
447 fix_infomask_from_infobits(xlrec->infobits_set,
448 &htup->t_infomask, &htup->t_infomask2);
449 if (!(xlrec->flags & XLH_DELETE_IS_SUPER))
450 HeapTupleHeaderSetXmax(htup, xlrec->xmax);
451 else
452 HeapTupleHeaderSetXmin(htup, InvalidTransactionId);
453 HeapTupleHeaderSetCmax(htup, FirstCommandId, false);
454
455 /* Mark the page as a candidate for pruning */
456 PageSetPrunable(page, XLogRecGetXid(record));
457
458 if (xlrec->flags & XLH_DELETE_ALL_VISIBLE_CLEARED)
459 PageClearAllVisible(page);
460
461 /* Make sure t_ctid is set correctly */
462 if (xlrec->flags & XLH_DELETE_IS_PARTITION_MOVE)
463 HeapTupleHeaderSetMovedPartitions(htup);
464 else
465 htup->t_ctid = target_tid;
466 PageSetLSN(page, lsn);
467 MarkBufferDirty(buffer);
468 }
469 if (BufferIsValid(buffer))
470 UnlockReleaseBuffer(buffer);
471 }
472
473 /*
474 * Replay XLOG_HEAP_INSERT records.
475 */
476 static void
477 heap_xlog_insert(XLogReaderState *record)
478 {
479 XLogRecPtr lsn = record->EndRecPtr;
480 xl_heap_insert *xlrec = (xl_heap_insert *) XLogRecGetData(record);
481 Buffer buffer;
482 Page page;
483 union
484 {
485 HeapTupleHeaderData hdr;
486 char data[MaxHeapTupleSize];
487 } tbuf;
488 HeapTupleHeader htup;
489 xl_heap_header xlhdr;
490 uint32 newlen;
491 Size freespace = 0;
492 RelFileLocator target_locator;
493 BlockNumber blkno;
494 ItemPointerData target_tid;
495 XLogRedoAction action;
496
497 XLogRecGetBlockTag(record, HEAP_INSERT_BLKREF_HEAP, &target_locator, NULL,
498 &blkno);
499 ItemPointerSetBlockNumber(&target_tid, blkno);
500 ItemPointerSetOffsetNumber(&target_tid, xlrec->offnum);
501
502 /* No freezing in the heap_insert() code path */
503 Assert(!(xlrec->flags & XLH_INSERT_ALL_FROZEN_SET));
504
505 /*
506 * The visibility map may need to be fixed even if the heap page is
507 * already up-to-date.
508 */
509 if (xlrec->flags & XLH_INSERT_ALL_VISIBLE_CLEARED)
510 heap_xlog_vm_clear(record, target_locator,
511 blkno, HEAP_INSERT_BLKREF_VM,
512 VISIBILITYMAP_VALID_BITS);
513
514 /*
515 * If we inserted the first and only tuple on the page, re-initialize the
516 * page from scratch.
517 */
518 if (XLogRecGetInfo(record) & XLOG_HEAP_INIT_PAGE)
519 {
520 buffer = XLogInitBufferForRedo(record, HEAP_INSERT_BLKREF_HEAP);
521 page = BufferGetPage(buffer);
522 PageInit(page, BufferGetPageSize(buffer), 0);
523 action = BLK_NEEDS_REDO;
524 }
525 else
526 action = XLogReadBufferForRedo(record, HEAP_INSERT_BLKREF_HEAP,
527 &buffer);
528 if (action == BLK_NEEDS_REDO)
529 {
530 Size datalen;
531 char *data;
532
533 page = BufferGetPage(buffer);
534
535 if (PageGetMaxOffsetNumber(page) + 1 < xlrec->offnum)
536 elog(PANIC, "invalid max offset number");
537
538 data = XLogRecGetBlockData(record, HEAP_INSERT_BLKREF_HEAP, &datalen);
539
540 newlen = datalen - SizeOfHeapHeader;
541 Assert(datalen > SizeOfHeapHeader && newlen <= MaxHeapTupleSize);
542 memcpy(&xlhdr, data, SizeOfHeapHeader);
543 data += SizeOfHeapHeader;
544
545 htup = &tbuf.hdr;
546 MemSet(htup, 0, SizeofHeapTupleHeader);
547 /* PG73FORMAT: get bitmap [+ padding] [+ oid] + data */
548 memcpy((char *) htup + SizeofHeapTupleHeader,
549 data,
550 newlen);
551 newlen += SizeofHeapTupleHeader;
552 htup->t_infomask2 = xlhdr.t_infomask2;
553 htup->t_infomask = xlhdr.t_infomask;
554 htup->t_hoff = xlhdr.t_hoff;
555 HeapTupleHeaderSetXmin(htup, XLogRecGetXid(record));
556 HeapTupleHeaderSetCmin(htup, FirstCommandId);
557 htup->t_ctid = target_tid;
558
559 if (PageAddItem(page, (Item) htup, newlen, xlrec->offnum,
560 true, true) == InvalidOffsetNumber)
561 elog(PANIC, "failed to add tuple");
562
563 freespace = PageGetHeapFreeSpace(page); /* needed to update FSM below */
564
565 PageSetLSN(page, lsn);
566
567 if (xlrec->flags & XLH_INSERT_ALL_VISIBLE_CLEARED)
568 PageClearAllVisible(page);
569
570 MarkBufferDirty(buffer);
571 }
572 if (BufferIsValid(buffer))
573 UnlockReleaseBuffer(buffer);
574
575 /*
576 * If the page is running low on free space, update the FSM as well.
577 * Arbitrarily, our definition of "low" is less than 20%. We can't do much
578 * better than that without knowing the fill-factor for the table.
579 *
580 * XXX: Don't do this if the page was restored from full page image. We
581 * don't bother to update the FSM in that case, it doesn't need to be
582 * totally accurate anyway.
583 */
584 if (action == BLK_NEEDS_REDO && freespace < BLCKSZ / 5)
585 XLogRecordPageWithFreeSpace(target_locator, blkno, freespace);
586 }
587
588 /*
589 * Replay XLOG_HEAP2_MULTI_INSERT records.
590 */
591 static void
592 heap_xlog_multi_insert(XLogReaderState *record)
593 {
594 XLogRecPtr lsn = record->EndRecPtr;
595 xl_heap_multi_insert *xlrec;
596 RelFileLocator rlocator;
597 BlockNumber blkno;
598 Buffer buffer;
599 Page page;
600 union
601 {
602 HeapTupleHeaderData hdr;
603 char data[MaxHeapTupleSize];
604 } tbuf;
605 HeapTupleHeader htup;
606 uint32 newlen;
607 Size freespace = 0;
608 int i;
609 bool isinit = (XLogRecGetInfo(record) & XLOG_HEAP_INIT_PAGE) != 0;
610 XLogRedoAction action;
611
612 /*
613 * Insertion doesn't overwrite MVCC data, so no conflict processing is
614 * required.
615 */
616 xlrec = (xl_heap_multi_insert *) XLogRecGetData(record);
617
618 XLogRecGetBlockTag(record, HEAP_MULTI_INSERT_BLKREF_HEAP, &rlocator, NULL,
619 &blkno);
620
621 /* check that the mutually exclusive flags are not both set */
622 Assert(!((xlrec->flags & XLH_INSERT_ALL_VISIBLE_CLEARED) &&
623 (xlrec->flags & XLH_INSERT_ALL_FROZEN_SET)));
624
625 /*
626 * The visibility map may need to be fixed even if the heap page is
627 * already up-to-date.
628 *
629 * Clear the VM (if needed) before clearing the heap page-level visibility
630 * flag (PD_ALL_VISIBLE) to prevent the heap page from being marked
631 * all-visible in the VM while its PD_ALL_VISIBLE is clear.
632 */
633 if (xlrec->flags & XLH_INSERT_ALL_VISIBLE_CLEARED)
634 heap_xlog_vm_clear(record, rlocator,
635 blkno, HEAP_MULTI_INSERT_BLKREF_VM,
636 VISIBILITYMAP_VALID_BITS);
637
638 if (isinit)
639 {
640 buffer = XLogInitBufferForRedo(record, HEAP_MULTI_INSERT_BLKREF_HEAP);
641 page = BufferGetPage(buffer);
642 PageInit(page, BufferGetPageSize(buffer), 0);
643 action = BLK_NEEDS_REDO;
644 }
645 else
646 action = XLogReadBufferForRedo(record, HEAP_MULTI_INSERT_BLKREF_HEAP,
647 &buffer);
648 if (action == BLK_NEEDS_REDO)
649 {
650 char *tupdata;
651 char *endptr;
652 Size len;
653
654 /* Tuples are stored as block data */
655 tupdata = XLogRecGetBlockData(record, HEAP_MULTI_INSERT_BLKREF_HEAP,
656 &len);
657 endptr = tupdata + len;
658
659 page = (Page) BufferGetPage(buffer);
660
661 for (i = 0; i < xlrec->ntuples; i++)
662 {
663 OffsetNumber offnum;
664 xl_multi_insert_tuple *xlhdr;
665
666 /*
667 * If we're reinitializing the page, the tuples are stored in
668 * order from FirstOffsetNumber. Otherwise there's an array of
669 * offsets in the WAL record, and the tuples come after that.
670 */
671 if (isinit)
672 offnum = FirstOffsetNumber + i;
673 else
674 offnum = xlrec->offsets[i];
675 if (PageGetMaxOffsetNumber(page) + 1 < offnum)
676 elog(PANIC, "invalid max offset number");
677
678 xlhdr = (xl_multi_insert_tuple *) SHORTALIGN(tupdata);
679 tupdata = ((char *) xlhdr) + SizeOfMultiInsertTuple;
680
681 newlen = xlhdr->datalen;
682 Assert(newlen <= MaxHeapTupleSize);
683 htup = &tbuf.hdr;
684 MemSet(htup, 0, SizeofHeapTupleHeader);
685 /* PG73FORMAT: get bitmap [+ padding] [+ oid] + data */
686 memcpy((char *) htup + SizeofHeapTupleHeader,
687 tupdata,
688 newlen);
689 tupdata += newlen;
690
691 newlen += SizeofHeapTupleHeader;
692 htup->t_infomask2 = xlhdr->t_infomask2;
693 htup->t_infomask = xlhdr->t_infomask;
694 htup->t_hoff = xlhdr->t_hoff;
695 HeapTupleHeaderSetXmin(htup, XLogRecGetXid(record));
696 HeapTupleHeaderSetCmin(htup, FirstCommandId);
697 ItemPointerSetBlockNumber(&htup->t_ctid, blkno);
698 ItemPointerSetOffsetNumber(&htup->t_ctid, offnum);
699
700 offnum = PageAddItem(page, (Item) htup, newlen, offnum, true, true);
701 if (offnum == InvalidOffsetNumber)
702 elog(PANIC, "failed to add tuple");
703 }
704 if (tupdata != endptr)
705 elog(PANIC, "total tuple length mismatch");
706
707 freespace = PageGetHeapFreeSpace(page); /* needed to update FSM below */
708
709 PageSetLSN(page, lsn);
710
711 if (xlrec->flags & XLH_INSERT_ALL_VISIBLE_CLEARED)
712 PageClearAllVisible(page);
713
714 /* XLH_INSERT_ALL_FROZEN_SET implies that all tuples are visible */
715 if (xlrec->flags & XLH_INSERT_ALL_FROZEN_SET)
716 PageSetAllVisible(page);
717
718 MarkBufferDirty(buffer);
719 }
720 if (BufferIsValid(buffer))
721 UnlockReleaseBuffer(buffer);
722
723 /*
724 * If the page is running low on free space, update the FSM as well.
725 * Arbitrarily, our definition of "low" is less than 20%. We can't do much
726 * better than that without knowing the fill-factor for the table.
727 *
728 * XXX: Don't do this if the page was restored from full page image. We
729 * don't bother to update the FSM in that case, it doesn't need to be
730 * totally accurate anyway.
731 */
732 if (action == BLK_NEEDS_REDO && freespace < BLCKSZ / 5)
733 XLogRecordPageWithFreeSpace(rlocator, blkno, freespace);
734 }
735
736 /*
737 * Replay XLOG_HEAP_UPDATE and XLOG_HEAP_HOT_UPDATE records.
738 */
739 static void
740 heap_xlog_update(XLogReaderState *record, bool hot_update)
741 {
742 XLogRecPtr lsn = record->EndRecPtr;
743 xl_heap_update *xlrec = (xl_heap_update *) XLogRecGetData(record);
744 RelFileLocator rlocator;
745 BlockNumber oldblk;
746 BlockNumber newblk;
747 ItemPointerData newtid;
748 Buffer obuffer,
749 nbuffer;
750 Page page;
751 bool new_cleared,
752 old_cleared;
753 OffsetNumber offnum;
754 ItemId lp = NULL;
755 HeapTupleData oldtup;
756 HeapTupleHeader htup;
757 uint16 prefixlen = 0,
758 suffixlen = 0;
759 char *newp;
760 union
761 {
762 HeapTupleHeaderData hdr;
763 char data[MaxHeapTupleSize];
764 } tbuf;
765 xl_heap_header xlhdr;
766 uint32 newlen;
767 Size freespace = 0;
768 XLogRedoAction oldaction;
769 XLogRedoAction newaction;
770
771 /* initialize to keep the compiler quiet */
772 oldtup.t_data = NULL;
773 oldtup.t_len = 0;
774
775 XLogRecGetBlockTag(record, HEAP_UPDATE_BLKREF_HEAP_NEW, &rlocator, NULL,
776 &newblk);
777 if (XLogRecGetBlockTagExtended(record, HEAP_UPDATE_BLKREF_HEAP_OLD, NULL, NULL,
778 &oldblk, NULL))
779 {
780 /* HOT updates are never done across pages */
781 Assert(!hot_update);
782 }
783 else
784 oldblk = newblk;
785
786 ItemPointerSet(&newtid, newblk, xlrec->new_offnum);
787
788 /*
789 * The visibility map may need to be fixed even if the heap page is
790 * already up-to-date.
791 */
792 new_cleared = (xlrec->flags & XLH_UPDATE_NEW_ALL_VISIBLE_CLEARED) != 0;
793 old_cleared = (xlrec->flags & XLH_UPDATE_OLD_ALL_VISIBLE_CLEARED) != 0;
794 if (new_cleared || old_cleared)
795 {
796 Relation reln = CreateFakeRelcacheEntry(rlocator);
797 bool has_vm_old = XLogRecHasBlockRef(record, HEAP_UPDATE_BLKREF_VM_OLD);
798 bool has_vm_new = XLogRecHasBlockRef(record, HEAP_UPDATE_BLKREF_VM_NEW);
799
800 if (has_vm_new)
801 {
802 Buffer vmbuffer_new = InvalidBuffer;
803
804 Assert(xlrec->flags & XLH_UPDATE_NEW_ALL_VISIBLE_CLEARED);
805
806 if (XLogReadBufferForRedo(record, HEAP_UPDATE_BLKREF_VM_NEW,
807 &vmbuffer_new) == BLK_NEEDS_REDO)
808 {
809 /*
810 * If both the old and new heap pages were all-visible and
811 * their VM bits are on the same VM page, that single VM page
812 * is registered as HEAP_UPDATE_BLKREF_VM_NEW. Clear both heap
813 * blocks' VM bits from the single provided VM buffer. It's
814 * possible that one of the page's VM bits were already clear,
815 * but visibilitymap_clear() is harmless as long as we provide
816 * it the correct bits.
817 *
818 * We must verify that oldblk's VM bits really are on this VM
819 * page, rather than relying on the absence of a separate
820 * VM_OLD block reference: VM_OLD is also omitted when oldblk
821 * is on a different VM page but its bit was already clear.
822 */
823 if (xlrec->flags & XLH_UPDATE_OLD_ALL_VISIBLE_CLEARED &&
824 visibilitymap_pin_ok(oldblk, vmbuffer_new))
825 {
826 if (visibilitymap_clear_locked(reln, oldblk, vmbuffer_new,
827 VISIBILITYMAP_VALID_BITS))
828 PageSetLSN(BufferGetPage(vmbuffer_new), lsn);
829 }
830 /* If VM_NEW is registered, we are sure newblk is on VM_NEW */
831 if (visibilitymap_clear_locked(reln, newblk, vmbuffer_new,
832 VISIBILITYMAP_VALID_BITS))
833 PageSetLSN(BufferGetPage(vmbuffer_new), lsn);
834 }
835 if (BufferIsValid(vmbuffer_new))
836 UnlockReleaseBuffer(vmbuffer_new);
837 }
838 if (has_vm_old)
839 {
840 Buffer vmbuffer_old = InvalidBuffer;
841
842 Assert(xlrec->flags & XLH_UPDATE_OLD_ALL_VISIBLE_CLEARED);
843
844 if (XLogReadBufferForRedo(record, HEAP_UPDATE_BLKREF_VM_OLD, &vmbuffer_old) ==
845 BLK_NEEDS_REDO)
846 {
847 if (visibilitymap_clear_locked(reln, oldblk, vmbuffer_old,
848 VISIBILITYMAP_VALID_BITS))
849 PageSetLSN(BufferGetPage(vmbuffer_old), lsn);
850 }
851 if (BufferIsValid(vmbuffer_old))
852 UnlockReleaseBuffer(vmbuffer_old);
853 }
854 if (!has_vm_old && !has_vm_new)
855 {
856 /*
857 * Backwards compatibility path. Previously, the VM buffers were
858 * not registered in the WAL record. We need this path to replay
859 * WAL generated by a not-yet-patched primary during upgrade.
860 */
861 if (old_cleared)
862 {
863 Buffer vmbuffer = InvalidBuffer;
864
865 visibilitymap_pin(reln, oldblk, &vmbuffer);
866 visibilitymap_clear(reln, oldblk, vmbuffer,
867 VISIBILITYMAP_VALID_BITS);
868 ReleaseBuffer(vmbuffer);
869 }
870 if (new_cleared)
871 {
872 Buffer vmbuffer = InvalidBuffer;
873
874 visibilitymap_pin(reln, newblk, &vmbuffer);
875 visibilitymap_clear(reln, newblk, vmbuffer,
876 VISIBILITYMAP_VALID_BITS);
877 ReleaseBuffer(vmbuffer);
878 }
879 }
880
881 FreeFakeRelcacheEntry(reln);
882 }
883
884 /*
885 * In normal operation, it is important to lock the two pages in
886 * page-number order, to avoid possible deadlocks against other update
887 * operations going the other way. However, during WAL replay there can
888 * be no other update happening, so we don't need to worry about that.
889 * Notice we also don't worry about this when locking VM buffers above.
890 *
891 * But we *do* need to worry that we don't expose an inconsistent state to
892 * Hot Standby queries --- so the original page can't be unlocked before
893 * we've added the new tuple to the new page.
894 */
895
896 /* Deal with old tuple version */
897 oldaction = XLogReadBufferForRedo(record, (oldblk == newblk) ?
898 HEAP_UPDATE_BLKREF_HEAP_NEW : HEAP_UPDATE_BLKREF_HEAP_OLD,
899 &obuffer);
900 if (oldaction == BLK_NEEDS_REDO)
901 {
902 page = BufferGetPage(obuffer);
903 offnum = xlrec->old_offnum;
904 if (PageGetMaxOffsetNumber(page) >= offnum)
905 lp = PageGetItemId(page, offnum);
906
907 if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsNormal(lp))
908 elog(PANIC, "invalid lp");
909
910 htup = (HeapTupleHeader) PageGetItem(page, lp);
911
912 oldtup.t_data = htup;
913 oldtup.t_len = ItemIdGetLength(lp);
914
915 htup->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED);
916 htup->t_infomask2 &= ~HEAP_KEYS_UPDATED;
917 if (hot_update)
918 HeapTupleHeaderSetHotUpdated(htup);
919 else
920 HeapTupleHeaderClearHotUpdated(htup);
921 fix_infomask_from_infobits(xlrec->old_infobits_set, &htup->t_infomask,
922 &htup->t_infomask2);
923 HeapTupleHeaderSetXmax(htup, xlrec->old_xmax);
924 HeapTupleHeaderSetCmax(htup, FirstCommandId, false);
925 /* Set forward chain link in t_ctid */
926 htup->t_ctid = newtid;
927
928 /* Mark the page as a candidate for pruning */
929 PageSetPrunable(page, XLogRecGetXid(record));
930
931 if (old_cleared)
932 PageClearAllVisible(page);
933
934 PageSetLSN(page, lsn);
935 MarkBufferDirty(obuffer);
936 }
937
938 /*
939 * Read the page the new tuple goes into, if different from old.
940 */
941 if (oldblk == newblk)
942 {
943 nbuffer = obuffer;
944 newaction = oldaction;
945 }
946 else if (XLogRecGetInfo(record) & XLOG_HEAP_INIT_PAGE)
947 {
948 nbuffer = XLogInitBufferForRedo(record, HEAP_UPDATE_BLKREF_HEAP_NEW);
949 page = (Page) BufferGetPage(nbuffer);
950 PageInit(page, BufferGetPageSize(nbuffer), 0);
951 newaction = BLK_NEEDS_REDO;
952 }
953 else
954 newaction = XLogReadBufferForRedo(record, HEAP_UPDATE_BLKREF_HEAP_NEW,
955 &nbuffer);
956
957 /* Deal with new tuple */
958 if (newaction == BLK_NEEDS_REDO)
959 {
960 char *recdata;
961 char *recdata_end;
962 Size datalen;
963 Size tuplen;
964
965 recdata = XLogRecGetBlockData(record, HEAP_UPDATE_BLKREF_HEAP_NEW,
966 &datalen);
967 recdata_end = recdata + datalen;
968
969 page = BufferGetPage(nbuffer);
970
971 offnum = xlrec->new_offnum;
972 if (PageGetMaxOffsetNumber(page) + 1 < offnum)
973 elog(PANIC, "invalid max offset number");
974
975 if (xlrec->flags & XLH_UPDATE_PREFIX_FROM_OLD)
976 {
977 Assert(newblk == oldblk);
978 memcpy(&prefixlen, recdata, sizeof(uint16));
979 recdata += sizeof(uint16);
980 }
981 if (xlrec->flags & XLH_UPDATE_SUFFIX_FROM_OLD)
982 {
983 Assert(newblk == oldblk);
984 memcpy(&suffixlen, recdata, sizeof(uint16));
985 recdata += sizeof(uint16);
986 }
987
988 memcpy(&xlhdr, recdata, SizeOfHeapHeader);
989 recdata += SizeOfHeapHeader;
990
991 tuplen = recdata_end - recdata;
992 Assert(tuplen <= MaxHeapTupleSize);
993
994 htup = &tbuf.hdr;
995 MemSet(htup, 0, SizeofHeapTupleHeader);
996
997 /*
998 * Reconstruct the new tuple using the prefix and/or suffix from the
999 * old tuple, and the data stored in the WAL record.
1000 */
1001 newp = (char *) htup + SizeofHeapTupleHeader;
1002 if (prefixlen > 0)
1003 {
1004 int len;
1005
1006 /* copy bitmap [+ padding] [+ oid] from WAL record */
1007 len = xlhdr.t_hoff - SizeofHeapTupleHeader;
1008 memcpy(newp, recdata, len);
1009 recdata += len;
1010 newp += len;
1011
1012 /* copy prefix from old tuple */
1013 memcpy(newp, (char *) oldtup.t_data + oldtup.t_data->t_hoff, prefixlen);
1014 newp += prefixlen;
1015
1016 /* copy new tuple data from WAL record */
1017 len = tuplen - (xlhdr.t_hoff - SizeofHeapTupleHeader);
1018 memcpy(newp, recdata, len);
1019 recdata += len;
1020 newp += len;
1021 }
1022 else
1023 {
1024 /*
1025 * copy bitmap [+ padding] [+ oid] + data from record, all in one
1026 * go
1027 */
1028 memcpy(newp, recdata, tuplen);
1029 recdata += tuplen;
1030 newp += tuplen;
1031 }
1032 Assert(recdata == recdata_end);
1033
1034 /* copy suffix from old tuple */
1035 if (suffixlen > 0)
1036 memcpy(newp, (char *) oldtup.t_data + oldtup.t_len - suffixlen, suffixlen);
1037
1038 newlen = SizeofHeapTupleHeader + tuplen + prefixlen + suffixlen;
1039 htup->t_infomask2 = xlhdr.t_infomask2;
1040 htup->t_infomask = xlhdr.t_infomask;
1041 htup->t_hoff = xlhdr.t_hoff;
1042
1043 HeapTupleHeaderSetXmin(htup, XLogRecGetXid(record));
1044 HeapTupleHeaderSetCmin(htup, FirstCommandId);
1045 HeapTupleHeaderSetXmax(htup, xlrec->new_xmax);
1046 /* Make sure there is no forward chain link in t_ctid */
1047 htup->t_ctid = newtid;
1048
1049 offnum = PageAddItem(page, (Item) htup, newlen, offnum, true, true);
1050 if (offnum == InvalidOffsetNumber)
1051 elog(PANIC, "failed to add tuple");
1052
1053 if (new_cleared)
1054 PageClearAllVisible(page);
1055
1056 freespace = PageGetHeapFreeSpace(page); /* needed to update FSM below */
1057
1058 PageSetLSN(page, lsn);
1059 MarkBufferDirty(nbuffer);
1060 }
1061
1062 if (BufferIsValid(nbuffer) && nbuffer != obuffer)
1063 UnlockReleaseBuffer(nbuffer);
1064 if (BufferIsValid(obuffer))
1065 UnlockReleaseBuffer(obuffer);
1066
1067 /*
1068 * If the new page is running low on free space, update the FSM as well.
1069 * Arbitrarily, our definition of "low" is less than 20%. We can't do much
1070 * better than that without knowing the fill-factor for the table.
1071 *
1072 * However, don't update the FSM on HOT updates, because after crash
1073 * recovery, either the old or the new tuple will certainly be dead and
1074 * prunable. After pruning, the page will have roughly as much free space
1075 * as it did before the update, assuming the new tuple is about the same
1076 * size as the old one.
1077 *
1078 * XXX: Don't do this if the page was restored from full page image. We
1079 * don't bother to update the FSM in that case, it doesn't need to be
1080 * totally accurate anyway.
1081 */
1082 if (newaction == BLK_NEEDS_REDO && !hot_update && freespace < BLCKSZ / 5)
1083 XLogRecordPageWithFreeSpace(rlocator, newblk, freespace);
1084 }
1085
1086 /*
1087 * Replay XLOG_HEAP_CONFIRM records.
1088 */
1089 static void
1090 heap_xlog_confirm(XLogReaderState *record)
1091 {
1092 XLogRecPtr lsn = record->EndRecPtr;
1093 xl_heap_confirm *xlrec = (xl_heap_confirm *) XLogRecGetData(record);
1094 Buffer buffer;
1095 Page page;
1096 OffsetNumber offnum;
1097 ItemId lp = NULL;
1098 HeapTupleHeader htup;
1099
1100 if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
1101 {
1102 page = BufferGetPage(buffer);
1103
1104 offnum = xlrec->offnum;
1105 if (PageGetMaxOffsetNumber(page) >= offnum)
1106 lp = PageGetItemId(page, offnum);
1107
1108 if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsNormal(lp))
1109 elog(PANIC, "invalid lp");
1110
1111 htup = (HeapTupleHeader) PageGetItem(page, lp);
1112
1113 /*
1114 * Confirm tuple as actually inserted
1115 */
1116 ItemPointerSet(&htup->t_ctid, BufferGetBlockNumber(buffer), offnum);
1117
1118 PageSetLSN(page, lsn);
1119 MarkBufferDirty(buffer);
1120 }
1121 if (BufferIsValid(buffer))
1122 UnlockReleaseBuffer(buffer);
1123 }
1124
1125 /*
1126 * Replay XLOG_HEAP_LOCK records.
1127 */
1128 static void
1129 heap_xlog_lock(XLogReaderState *record)
1130 {
1131 XLogRecPtr lsn = record->EndRecPtr;
1132 xl_heap_lock *xlrec = (xl_heap_lock *) XLogRecGetData(record);
1133 Buffer buffer;
1134 Page page;
1135 OffsetNumber offnum;
1136 ItemId lp = NULL;
1137 HeapTupleHeader htup;
1138
1139 /*
1140 * The visibility map may need to be fixed even if the heap page is
1141 * already up-to-date.
1142 */
1143 if (xlrec->flags & XLH_LOCK_ALL_FROZEN_CLEARED)
1144 {
1145 BlockNumber block;
1146 RelFileLocator rlocator;
1147
1148 XLogRecGetBlockTag(record, HEAP_LOCK_BLKREF_HEAP, &rlocator, NULL,
1149 &block);
1150
1151 heap_xlog_vm_clear(record, rlocator,
1152 block, HEAP_LOCK_BLKREF_VM,
1153 VISIBILITYMAP_ALL_FROZEN);
1154 }
1155
1156 if (XLogReadBufferForRedo(record, HEAP_LOCK_BLKREF_HEAP,
1157 &buffer) == BLK_NEEDS_REDO)
1158 {
1159 page = (Page) BufferGetPage(buffer);
1160
1161 offnum = xlrec->offnum;
1162 if (PageGetMaxOffsetNumber(page) >= offnum)
1163 lp = PageGetItemId(page, offnum);
1164
1165 if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsNormal(lp))
1166 elog(PANIC, "invalid lp");
1167
1168 htup = (HeapTupleHeader) PageGetItem(page, lp);
1169
1170 htup->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED);
1171 htup->t_infomask2 &= ~HEAP_KEYS_UPDATED;
1172 fix_infomask_from_infobits(xlrec->infobits_set, &htup->t_infomask,
1173 &htup->t_infomask2);
1174
1175 /*
1176 * Clear relevant update flags, but only if the modified infomask says
1177 * there's no update.
1178 */
1179 if (HEAP_XMAX_IS_LOCKED_ONLY(htup->t_infomask))
1180 {
1181 HeapTupleHeaderClearHotUpdated(htup);
1182 /* Make sure there is no forward chain link in t_ctid */
1183 ItemPointerSet(&htup->t_ctid,
1184 BufferGetBlockNumber(buffer),
1185 offnum);
1186 }
1187 HeapTupleHeaderSetXmax(htup, xlrec->xmax);
1188 HeapTupleHeaderSetCmax(htup, FirstCommandId, false);
1189 PageSetLSN(page, lsn);
1190 MarkBufferDirty(buffer);
1191 }
1192 if (BufferIsValid(buffer))
1193 UnlockReleaseBuffer(buffer);
1194 }
1195
1196 /*
1197 * Replay XLOG_HEAP2_LOCK_UPDATED records.
1198 */
1199 static void
1200 heap_xlog_lock_updated(XLogReaderState *record)
1201 {
1202 XLogRecPtr lsn = record->EndRecPtr;
1203 xl_heap_lock_updated *xlrec;
1204 Buffer buffer;
1205 Page page;
1206 OffsetNumber offnum;
1207 ItemId lp = NULL;
1208 HeapTupleHeader htup;
1209
1210 xlrec = (xl_heap_lock_updated *) XLogRecGetData(record);
1211
1212 /*
1213 * The visibility map may need to be fixed even if the heap page is
1214 * already up-to-date.
1215 */
1216 if (xlrec->flags & XLH_LOCK_ALL_FROZEN_CLEARED)
1217 {
1218 BlockNumber block;
1219 RelFileLocator rlocator;
1220
1221 XLogRecGetBlockTag(record, HEAP_LOCK_BLKREF_HEAP, &rlocator, NULL,
1222 &block);
1223
1224 heap_xlog_vm_clear(record, rlocator,
1225 block, HEAP_LOCK_BLKREF_VM,
1226 VISIBILITYMAP_ALL_FROZEN);
1227 }
1228
1229 if (XLogReadBufferForRedo(record, HEAP_LOCK_BLKREF_HEAP,
1230 &buffer) == BLK_NEEDS_REDO)
1231 {
1232 page = BufferGetPage(buffer);
1233
1234 offnum = xlrec->offnum;
1235 if (PageGetMaxOffsetNumber(page) >= offnum)
1236 lp = PageGetItemId(page, offnum);
1237
1238 if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsNormal(lp))
1239 elog(PANIC, "invalid lp");
1240
1241 htup = (HeapTupleHeader) PageGetItem(page, lp);
1242
1243 htup->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED);
1244 htup->t_infomask2 &= ~HEAP_KEYS_UPDATED;
1245 fix_infomask_from_infobits(xlrec->infobits_set, &htup->t_infomask,
1246 &htup->t_infomask2);
1247 HeapTupleHeaderSetXmax(htup, xlrec->xmax);
1248
1249 PageSetLSN(page, lsn);
1250 MarkBufferDirty(buffer);
1251 }
1252 if (BufferIsValid(buffer))
1253 UnlockReleaseBuffer(buffer);
1254 }
1255
1256 /*
1257 * Replay XLOG_HEAP_INPLACE records.
1258 */
1259 static void
1260 heap_xlog_inplace(XLogReaderState *record)
1261 {
1262 XLogRecPtr lsn = record->EndRecPtr;
1263 xl_heap_inplace *xlrec = (xl_heap_inplace *) XLogRecGetData(record);
1264 Buffer buffer;
1265 Page page;
1266 OffsetNumber offnum;
1267 ItemId lp = NULL;
1268 HeapTupleHeader htup;
1269 uint32 oldlen;
1270 Size newlen;
1271
1272 if (XLogReadBufferForRedo(record, 0, &buffer) == BLK_NEEDS_REDO)
1273 {
1274 char *newtup = XLogRecGetBlockData(record, 0, &newlen);
1275
1276 page = BufferGetPage(buffer);
1277
1278 offnum = xlrec->offnum;
1279 if (PageGetMaxOffsetNumber(page) >= offnum)
1280 lp = PageGetItemId(page, offnum);
1281
1282 if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsNormal(lp))
1283 elog(PANIC, "invalid lp");
1284
1285 htup = (HeapTupleHeader) PageGetItem(page, lp);
1286
1287 oldlen = ItemIdGetLength(lp) - htup->t_hoff;
1288 if (oldlen != newlen)
1289 elog(PANIC, "wrong tuple length");
1290
1291 memcpy((char *) htup + htup->t_hoff, newtup, newlen);
1292
1293 PageSetLSN(page, lsn);
1294 MarkBufferDirty(buffer);
1295 }
1296 if (BufferIsValid(buffer))
1297 UnlockReleaseBuffer(buffer);
1298
1299 ProcessCommittedInvalidationMessages(xlrec->msgs,
1300 xlrec->nmsgs,
1301 xlrec->relcacheInitFileInval,
1302 xlrec->dbId,
1303 xlrec->tsId);
1304 }
1305
1306 void
1307 heap_redo(XLogReaderState *record)
1308 {
1309 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1310
1311 /*
1312 * These operations don't overwrite MVCC data so no conflict processing is
1313 * required. The ones in heap2 rmgr do.
1314 */
1315
1316 switch (info & XLOG_HEAP_OPMASK)
1317 {
1318 case XLOG_HEAP_INSERT:
1319 heap_xlog_insert(record);
1320 break;
1321 case XLOG_HEAP_DELETE:
1322 heap_xlog_delete(record);
1323 break;
1324 case XLOG_HEAP_UPDATE:
1325 heap_xlog_update(record, false);
1326 break;
1327 case XLOG_HEAP_TRUNCATE:
1328
1329 /*
1330 * TRUNCATE is a no-op because the actions are already logged as
1331 * SMGR WAL records. TRUNCATE WAL record only exists for logical
1332 * decoding.
1333 */
1334 break;
1335 case XLOG_HEAP_HOT_UPDATE:
1336 heap_xlog_update(record, true);
1337 break;
1338 case XLOG_HEAP_CONFIRM:
1339 heap_xlog_confirm(record);
1340 break;
1341 case XLOG_HEAP_LOCK:
1342 heap_xlog_lock(record);
1343 break;
1344 case XLOG_HEAP_INPLACE:
1345 heap_xlog_inplace(record);
1346 break;
1347 default:
1348 elog(PANIC, "heap_redo: unknown op code %u", info);
1349 }
1350 }
1351
1352 void
1353 heap2_redo(XLogReaderState *record)
1354 {
1355 uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1356
1357 switch (info & XLOG_HEAP_OPMASK)
1358 {
1359 case XLOG_HEAP2_PRUNE_ON_ACCESS:
1360 case XLOG_HEAP2_PRUNE_VACUUM_SCAN:
1361 case XLOG_HEAP2_PRUNE_VACUUM_CLEANUP:
1362 heap_xlog_prune_freeze(record);
1363 break;
1364 case XLOG_HEAP2_VISIBLE:
1365 heap_xlog_visible(record);
1366 break;
1367 case XLOG_HEAP2_MULTI_INSERT:
1368 heap_xlog_multi_insert(record);
1369 break;
1370 case XLOG_HEAP2_LOCK_UPDATED:
1371 heap_xlog_lock_updated(record);
1372 break;
1373 case XLOG_HEAP2_NEW_CID:
1374
1375 /*
1376 * Nothing to do on a real replay, only used during logical
1377 * decoding.
1378 */
1379 break;
1380 case XLOG_HEAP2_REWRITE:
1381 heap_xlog_logical_rewrite(record);
1382 break;
1383 default:
1384 elog(PANIC, "heap2_redo: unknown op code %u", info);
1385 }
1386 }
1387
1388 /*
1389 * Mask a heap page before performing consistency checks on it.
1390 */
1391 void
1392 heap_mask(char *pagedata, BlockNumber blkno)
1393 {
1394 Page page = (Page) pagedata;
1395 OffsetNumber off;
1396
1397 mask_page_lsn_and_checksum(page);
1398
1399 mask_page_hint_bits(page);
1400 mask_unused_space(page);
1401
1402 for (off = 1; off <= PageGetMaxOffsetNumber(page); off++)
1403 {
1404 ItemId iid = PageGetItemId(page, off);
1405 char *page_item;
1406
1407 page_item = (char *) (page + ItemIdGetOffset(iid));
1408
1409 if (ItemIdIsNormal(iid))
1410 {
1411 HeapTupleHeader page_htup = (HeapTupleHeader) page_item;
1412
1413 /*
1414 * If xmin of a tuple is not yet frozen, we should ignore
1415 * differences in hint bits, since they can be set without
1416 * emitting WAL.
1417 */
1418 if (!HeapTupleHeaderXminFrozen(page_htup))
1419 page_htup->t_infomask &= ~HEAP_XACT_MASK;
1420 else
1421 {
1422 /* Still we need to mask xmax hint bits. */
1423 page_htup->t_infomask &= ~HEAP_XMAX_INVALID;
1424 page_htup->t_infomask &= ~HEAP_XMAX_COMMITTED;
1425 }
1426
1427 /*
1428 * During replay, we set Command Id to FirstCommandId. Hence, mask
1429 * it. See heap_xlog_insert() for details.
1430 */
1431 page_htup->t_choice.t_heap.t_field3.t_cid = MASK_MARKER;
1432
1433 /*
1434 * For a speculative tuple, heap_insert() does not set ctid in the
1435 * caller-passed heap tuple itself, leaving the ctid field to
1436 * contain a speculative token value - a per-backend monotonically
1437 * increasing identifier. Besides, it does not WAL-log ctid under
1438 * any circumstances.
1439 *
1440 * During redo, heap_xlog_insert() sets t_ctid to current block
1441 * number and self offset number. It doesn't care about any
1442 * speculative insertions on the primary. Hence, we set t_ctid to
1443 * current block number and self offset number to ignore any
1444 * inconsistency.
1445 */
1446 if (HeapTupleHeaderIsSpeculative(page_htup))
1447 ItemPointerSet(&page_htup->t_ctid, blkno, off);
1448
1449 /*
1450 * NB: Not ignoring ctid changes due to the tuple having moved
1451 * (i.e. HeapTupleHeaderIndicatesMovedPartitions), because that's
1452 * important information that needs to be in-sync between primary
1453 * and standby, and thus is WAL logged.
1454 */
1455 }
1456
1457 /*
1458 * Ignore any padding bytes after the tuple, when the length of the
1459 * item is not MAXALIGNed.
1460 */
1461 if (ItemIdHasStorage(iid))
1462 {
1463 int len = ItemIdGetLength(iid);
1464 int padlen = MAXALIGN(len) - len;
1465
1466 if (padlen > 0)
1467 memset(page_item + len, MASK_MARKER, padlen);
1468 }
1469 }
1470 }