PostgreSQL Source Code git master
Loading...
Searching...
No Matches
heapam.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * heapam.c
4 * heap access method code
5 *
6 * Portions Copyright (c) 1996-2026, 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.c
12 *
13 *
14 * INTERFACE ROUTINES
15 * heap_beginscan - begin relation scan
16 * heap_rescan - restart a relation scan
17 * heap_endscan - end relation scan
18 * heap_getnext - retrieve next tuple in scan
19 * heap_fetch - retrieve tuple with given tid
20 * heap_insert - insert tuple into a relation
21 * heap_multi_insert - insert multiple tuples into a relation
22 * heap_delete - delete a tuple from a relation
23 * heap_update - replace a tuple in a relation with another tuple
24 *
25 * NOTES
26 * This file contains the heap_ routines which implement
27 * the POSTGRES heap access method used for all POSTGRES
28 * relations.
29 *
30 *-------------------------------------------------------------------------
31 */
32#include "postgres.h"
33
34#include "access/heapam.h"
35#include "access/heaptoast.h"
36#include "access/hio.h"
37#include "access/multixact.h"
38#include "access/subtrans.h"
39#include "access/syncscan.h"
40#include "access/valid.h"
42#include "access/xloginsert.h"
43#include "catalog/pg_database.h"
44#include "catalog/pg_database_d.h"
45#include "commands/vacuum.h"
47#include "pgstat.h"
48#include "port/pg_bitutils.h"
49#include "storage/lmgr.h"
50#include "storage/predicate.h"
51#include "storage/proc.h"
52#include "storage/procarray.h"
53#include "utils/datum.h"
55#include "utils/inval.h"
56#include "utils/spccache.h"
57#include "utils/syscache.h"
58
59
67 bool walLogical);
68#ifdef USE_ASSERT_CHECKING
70 const ItemPointerData *otid,
73#endif
78 bool *has_external);
79static bool heap_acquire_tuplock(Relation relation, const ItemPointerData *tid,
81 bool *have_tuple_lock);
83 BlockNumber block,
84 ScanDirection dir);
86 ScanDirection dir);
96 TransactionId xid,
101 uint16 t_infomask);
103 LockTupleMode lockmode, bool *current_is_member);
105 Relation rel, const ItemPointerData *ctid, XLTW_Oper oper,
106 int *remaining);
109 bool logLockFailure);
114 bool *copy);
115
116
117/*
118 * This table lists the heavyweight lock mode that corresponds to each tuple
119 * lock mode, as well as one or two corresponding MultiXactStatus values:
120 * .lockstatus to merely lock tuples, and .updstatus to update them. The
121 * latter is set to -1 if the corresponding tuple lock mode does not allow
122 * updating tuples -- see get_mxact_status_for_lock().
123 *
124 * These interact with InplaceUpdateTupleLock, an alias for ExclusiveLock.
125 *
126 * Don't look at lockstatus/updstatus directly! Use get_mxact_status_for_lock
127 * instead.
128 */
129static const struct
130{
135
136{
138 .hwlock = AccessShareLock,
139 .lockstatus = MultiXactStatusForKeyShare,
140 /* KeyShare does not allow updating tuples */
141 .updstatus = -1
142 },
143 [LockTupleShare] = {
144 .hwlock = RowShareLock,
145 .lockstatus = MultiXactStatusForShare,
146 /* Share does not allow updating tuples */
147 .updstatus = -1
148 },
150 .hwlock = ExclusiveLock,
151 .lockstatus = MultiXactStatusForNoKeyUpdate,
152 .updstatus = MultiXactStatusNoKeyUpdate
153 },
155 .hwlock = AccessExclusiveLock,
156 .lockstatus = MultiXactStatusForUpdate,
157 .updstatus = MultiXactStatusUpdate
158 }
160
161/* Get the LOCKMODE for a given MultiXactStatus */
162#define LOCKMODE_from_mxstatus(status) \
163 (tupleLockExtraInfo[TUPLOCK_from_mxstatus((status))].hwlock)
164
165/*
166 * Acquire heavyweight locks on tuples, using a LockTupleMode strength value.
167 * This is more readable than having every caller translate it to lock.h's
168 * LOCKMODE.
169 */
170#define LockTupleTuplock(rel, tup, mode) \
171 LockTuple((rel), (tup), tupleLockExtraInfo[mode].hwlock)
172#define UnlockTupleTuplock(rel, tup, mode) \
173 UnlockTuple((rel), (tup), tupleLockExtraInfo[mode].hwlock)
174#define ConditionalLockTupleTuplock(rel, tup, mode, log) \
175 ConditionalLockTuple((rel), (tup), tupleLockExtraInfo[mode].hwlock, (log))
176
177#ifdef USE_PREFETCH
178/*
179 * heap_index_delete_tuples and index_delete_prefetch_buffer use this
180 * structure to coordinate prefetching activity
181 */
182typedef struct
183{
185 int next_item;
186 int ndeltids;
187 TM_IndexDelete *deltids;
189#endif
190
191/* heap_index_delete_tuples bottom-up index deletion costing constants */
192#define BOTTOMUP_MAX_NBLOCKS 6
193#define BOTTOMUP_TOLERANCE_NBLOCKS 3
194
195/*
196 * heap_index_delete_tuples uses this when determining which heap blocks it
197 * must visit to help its bottom-up index deletion caller
198 */
199typedef struct IndexDeleteCounts
200{
201 int16 npromisingtids; /* Number of "promising" TIDs in group */
202 int16 ntids; /* Number of TIDs in group */
203 int16 ifirsttid; /* Offset to group's first deltid */
205
206/*
207 * This table maps tuple lock strength values for each particular
208 * MultiXactStatus value.
209 */
211{
212 LockTupleKeyShare, /* ForKeyShare */
213 LockTupleShare, /* ForShare */
214 LockTupleNoKeyExclusive, /* ForNoKeyUpdate */
215 LockTupleExclusive, /* ForUpdate */
216 LockTupleNoKeyExclusive, /* NoKeyUpdate */
217 LockTupleExclusive /* Update */
218};
219
220/* Get the LockTupleMode for a given MultiXactStatus */
221#define TUPLOCK_from_mxstatus(status) \
222 (MultiXactStatusLock[(status)])
223
224/*
225 * Check that we have a valid snapshot if we might need TOAST access.
226 */
227static inline void
229{
230#ifdef USE_ASSERT_CHECKING
231
232 /* bootstrap mode in particular breaks this rule */
234 return;
235
236 /* if the relation doesn't have a TOAST table, we are good */
237 if (!OidIsValid(rel->rd_rel->reltoastrelid))
238 return;
239
241
242#endif /* USE_ASSERT_CHECKING */
243}
244
245/* ----------------------------------------------------------------
246 * heap support routines
247 * ----------------------------------------------------------------
248 */
249
250/*
251 * Streaming read API callback for parallel sequential scans. Returns the next
252 * block the caller wants from the read stream or InvalidBlockNumber when done.
253 */
254static BlockNumber
256 void *callback_private_data,
257 void *per_buffer_data)
258{
259 HeapScanDesc scan = (HeapScanDesc) callback_private_data;
260
263
264 if (unlikely(!scan->rs_inited))
265 {
266 /* parallel scan */
270 scan->rs_startblock,
271 scan->rs_numblocks);
272
273 /* may return InvalidBlockNumber if there are no more blocks */
277 scan->rs_inited = true;
278 }
279 else
280 {
283 scan->rs_base.rs_parallel);
284 }
285
286 return scan->rs_prefetch_block;
287}
288
289/*
290 * Streaming read API callback for serial sequential and TID range scans.
291 * Returns the next block the caller wants from the read stream or
292 * InvalidBlockNumber when done.
293 */
294static BlockNumber
296 void *callback_private_data,
297 void *per_buffer_data)
298{
299 HeapScanDesc scan = (HeapScanDesc) callback_private_data;
300
301 if (unlikely(!scan->rs_inited))
302 {
304 scan->rs_inited = true;
305 }
306 else
308 scan->rs_prefetch_block,
309 scan->rs_dir);
310
311 return scan->rs_prefetch_block;
312}
313
314/*
315 * Read stream API callback for bitmap heap scans.
316 * Returns the next block the caller wants from the read stream or
317 * InvalidBlockNumber when done.
318 */
319static BlockNumber
321 void *per_buffer_data)
322{
323 TBMIterateResult *tbmres = per_buffer_data;
326 TableScanDesc sscan = &hscan->rs_base;
327
328 for (;;)
329 {
331
332 /* no more entries in the bitmap */
333 if (!tbm_iterate(&sscan->st.rs_tbmiterator, tbmres))
334 return InvalidBlockNumber;
335
336 /*
337 * Ignore any claimed entries past what we think is the end of the
338 * relation. It may have been extended after the start of our scan (we
339 * only hold an AccessShareLock, and it could be inserts from this
340 * backend). We don't take this optimization in SERIALIZABLE
341 * isolation though, as we need to examine all invisible tuples
342 * reachable by the index.
343 */
345 tbmres->blockno >= hscan->rs_nblocks)
346 continue;
347
348 return tbmres->blockno;
349 }
350
351 /* not reachable */
352 Assert(false);
353}
354
355/* ----------------
356 * initscan - scan code common to heap_beginscan and heap_rescan
357 * ----------------
358 */
359static void
361{
363 bool allow_strat;
364 bool allow_sync;
365
366 /*
367 * Determine the number of blocks we have to scan.
368 *
369 * It is sufficient to do this once at scan start, since any tuples added
370 * while the scan is in progress will be invisible to my snapshot anyway.
371 * (That is not true when using a non-MVCC snapshot. However, we couldn't
372 * guarantee to return tuples added after scan start anyway, since they
373 * might go into pages we already scanned. To guarantee consistent
374 * results for a non-MVCC snapshot, the caller must hold some higher-level
375 * lock that ensures the interesting tuple(s) won't change.)
376 */
377 if (scan->rs_base.rs_parallel != NULL)
378 {
380 scan->rs_nblocks = bpscan->phs_nblocks;
381 }
382 else
384
385 /*
386 * If the table is large relative to NBuffers, use a bulk-read access
387 * strategy and enable synchronized scanning (see syncscan.c). Although
388 * the thresholds for these features could be different, we make them the
389 * same so that there are only two behaviors to tune rather than four.
390 * (However, some callers need to be able to disable one or both of these
391 * behaviors, independently of the size of the table; also there is a GUC
392 * variable that can disable synchronized scanning.)
393 *
394 * Note that table_block_parallelscan_initialize has a very similar test;
395 * if you change this, consider changing that one, too.
396 */
398 scan->rs_nblocks > NBuffers / 4)
399 {
401 allow_sync = (scan->rs_base.rs_flags & SO_ALLOW_SYNC) != 0;
402 }
403 else
404 allow_strat = allow_sync = false;
405
406 if (allow_strat)
407 {
408 /* During a rescan, keep the previous strategy object. */
409 if (scan->rs_strategy == NULL)
411 }
412 else
413 {
414 if (scan->rs_strategy != NULL)
416 scan->rs_strategy = NULL;
417 }
418
419 if (scan->rs_base.rs_parallel != NULL)
420 {
421 /* For parallel scan, believe whatever ParallelTableScanDesc says. */
424 else
426
427 /*
428 * If not rescanning, initialize the startblock. Finding the actual
429 * start location is done in table_block_parallelscan_startblock_init,
430 * based on whether an alternative start location has been set with
431 * heap_setscanlimits, or using the syncscan location, when syncscan
432 * is enabled.
433 */
434 if (!keep_startblock)
436 }
437 else
438 {
439 if (keep_startblock)
440 {
441 /*
442 * When rescanning, we want to keep the previous startblock
443 * setting, so that rewinding a cursor doesn't generate surprising
444 * results. Reset the active syncscan setting, though.
445 */
448 else
450 }
452 {
455 }
456 else
457 {
459 scan->rs_startblock = 0;
460 }
461 }
462
464 scan->rs_inited = false;
465 scan->rs_ctup.t_data = NULL;
467 scan->rs_cbuf = InvalidBuffer;
469 scan->rs_ntuples = 0;
470 scan->rs_cindex = 0;
471
472 /*
473 * Initialize to ForwardScanDirection because it is most common and
474 * because heap scans go forward before going backward (e.g. CURSORs).
475 */
478
479 /* page-at-a-time fields are always invalid when not rs_inited */
480
481 /*
482 * copy the scan key, if appropriate
483 */
484 if (key != NULL && scan->rs_base.rs_nkeys > 0)
485 memcpy(scan->rs_base.rs_key, key, scan->rs_base.rs_nkeys * sizeof(ScanKeyData));
486
487 /*
488 * Currently, we only have a stats counter for sequential heap scans (but
489 * e.g for bitmap scans the underlying bitmap index scans will be counted,
490 * and for sample scans we update stats for tuple fetches).
491 */
492 if (scan->rs_base.rs_flags & SO_TYPE_SEQSCAN)
494}
495
496/*
497 * heap_setscanlimits - restrict range of a heapscan
498 *
499 * startBlk is the page to start at
500 * numBlks is number of pages to scan (InvalidBlockNumber means "all")
501 */
502void
504{
506
507 Assert(!scan->rs_inited); /* else too late to change */
508 /* else rs_startblock is significant */
510
511 /* Check startBlk is valid (but allow case of zero blocks...) */
512 Assert(startBlk == 0 || startBlk < scan->rs_nblocks);
513
514 scan->rs_startblock = startBlk;
515 scan->rs_numblocks = numBlks;
516}
517
518/*
519 * Per-tuple loop for heap_prepare_pagescan(). Pulled out so it can be called
520 * multiple times, with constant arguments for all_visible,
521 * check_serializable.
522 */
524static int
526 Page page, Buffer buffer,
527 BlockNumber block, int lines,
528 bool all_visible, bool check_serializable)
529{
530 Oid relid = RelationGetRelid(scan->rs_base.rs_rd);
531 int ntup = 0;
532 int nvis = 0;
534
535 /* page at a time should have been disabled otherwise */
536 Assert(IsMVCCSnapshot(snapshot));
537
538 /* first find all tuples on the page */
540 {
543
545 continue;
546
547 /*
548 * If the page is not all-visible or we need to check serializability,
549 * maintain enough state to be able to refind the tuple efficiently,
550 * without again first needing to fetch the item and then via that the
551 * tuple.
552 */
553 if (!all_visible || check_serializable)
554 {
555 tup = &batchmvcc.tuples[ntup];
556
558 tup->t_len = ItemIdGetLength(lpp);
559 tup->t_tableOid = relid;
560 ItemPointerSet(&(tup->t_self), block, lineoff);
561 }
562
563 /*
564 * If the page is all visible, these fields otherwise won't be
565 * populated in loop below.
566 */
567 if (all_visible)
568 {
570 {
571 batchmvcc.visible[ntup] = true;
572 }
573 scan->rs_vistuples[ntup] = lineoff;
574 }
575
576 ntup++;
577 }
578
580
581 /*
582 * Unless the page is all visible, test visibility for all tuples one go.
583 * That is considerably more efficient than calling
584 * HeapTupleSatisfiesMVCC() one-by-one.
585 */
586 if (all_visible)
587 nvis = ntup;
588 else
589 nvis = HeapTupleSatisfiesMVCCBatch(snapshot, buffer,
590 ntup,
591 &batchmvcc,
592 scan->rs_vistuples);
593
594 /*
595 * So far we don't have batch API for testing serializabilty, so do so
596 * one-by-one.
597 */
599 {
600 for (int i = 0; i < ntup; i++)
601 {
603 scan->rs_base.rs_rd,
604 &batchmvcc.tuples[i],
605 buffer, snapshot);
606 }
607 }
608
609 return nvis;
610}
611
612/*
613 * heap_prepare_pagescan - Prepare current scan page to be scanned in pagemode
614 *
615 * Preparation currently consists of 1. prune the scan's rs_cbuf page, and 2.
616 * fill the rs_vistuples[] array with the OffsetNumbers of visible tuples.
617 */
618void
620{
622 Buffer buffer = scan->rs_cbuf;
623 BlockNumber block = scan->rs_cblock;
624 Snapshot snapshot;
625 Page page;
626 int lines;
627 bool all_visible;
629
630 Assert(BufferGetBlockNumber(buffer) == block);
631
632 /* ensure we're not accidentally being used when not in pagemode */
634 snapshot = scan->rs_base.rs_snapshot;
635
636 /*
637 * Prune and repair fragmentation for the whole page, if possible.
638 */
639 heap_page_prune_opt(scan->rs_base.rs_rd, buffer, &scan->rs_vmbuffer,
640 sscan->rs_flags & SO_HINT_REL_READ_ONLY);
641
642 /*
643 * We must hold share lock on the buffer content while examining tuple
644 * visibility. Afterwards, however, the tuples we have found to be
645 * visible are guaranteed good as long as we hold the buffer pin.
646 */
648
649 page = BufferGetPage(buffer);
650 lines = PageGetMaxOffsetNumber(page);
651
652 /*
653 * If the all-visible flag indicates that all tuples on the page are
654 * visible to everyone, we can skip the per-tuple visibility tests.
655 *
656 * Note: In hot standby, a tuple that's already visible to all
657 * transactions on the primary might still be invisible to a read-only
658 * transaction in the standby. We partly handle this problem by tracking
659 * the minimum xmin of visible tuples as the cut-off XID while marking a
660 * page all-visible on the primary and WAL log that along with the
661 * visibility map SET operation. In hot standby, we wait for (or abort)
662 * all transactions that can potentially may not see one or more tuples on
663 * the page. That's how index-only scans work fine in hot standby. A
664 * crucial difference between index-only scans and heap scans is that the
665 * index-only scan completely relies on the visibility map where as heap
666 * scan looks at the page-level PD_ALL_VISIBLE flag. We are not sure if
667 * the page-level flag can be trusted in the same way, because it might
668 * get propagated somehow without being explicitly WAL-logged, e.g. via a
669 * full page write. Until we can prove that beyond doubt, let's check each
670 * tuple for visibility the hard way.
671 */
672 all_visible = PageIsAllVisible(page) && !snapshot->takenDuringRecovery;
675
676 /*
677 * We call page_collect_tuples() with constant arguments, to get the
678 * compiler to constant fold the constant arguments. Separate calls with
679 * constant arguments, rather than variables, are needed on several
680 * compilers to actually perform constant folding.
681 */
682 if (likely(all_visible))
683 {
685 scan->rs_ntuples = page_collect_tuples(scan, snapshot, page, buffer,
686 block, lines, true, false);
687 else
688 scan->rs_ntuples = page_collect_tuples(scan, snapshot, page, buffer,
689 block, lines, true, true);
690 }
691 else
692 {
694 scan->rs_ntuples = page_collect_tuples(scan, snapshot, page, buffer,
695 block, lines, false, false);
696 else
697 scan->rs_ntuples = page_collect_tuples(scan, snapshot, page, buffer,
698 block, lines, false, true);
699 }
700
702}
703
704/*
705 * heap_fetch_next_buffer - read and pin the next block from MAIN_FORKNUM.
706 *
707 * Read the next block of the scan relation from the read stream and save it
708 * in the scan descriptor. It is already pinned.
709 */
710static inline void
712{
713 Assert(scan->rs_read_stream);
714
715 /* release previous scan buffer, if any */
716 if (BufferIsValid(scan->rs_cbuf))
717 {
718 ReleaseBuffer(scan->rs_cbuf);
719 scan->rs_cbuf = InvalidBuffer;
720 }
721
722 /*
723 * Be sure to check for interrupts at least once per page. Checks at
724 * higher code levels won't be able to stop a seqscan that encounters many
725 * pages' worth of consecutive dead tuples.
726 */
728
729 /*
730 * If the scan direction is changing, reset the prefetch block to the
731 * current block. Otherwise, we will incorrectly prefetch the blocks
732 * between the prefetch block and the current block again before
733 * prefetching blocks in the new, correct scan direction.
734 */
735 if (unlikely(scan->rs_dir != dir))
736 {
737 scan->rs_prefetch_block = scan->rs_cblock;
739 }
740
741 scan->rs_dir = dir;
742
744 if (BufferIsValid(scan->rs_cbuf))
746}
747
748/*
749 * heapgettup_initial_block - return the first BlockNumber to scan
750 *
751 * Returns InvalidBlockNumber when there are no blocks to scan. This can
752 * occur with empty tables and in parallel scans when parallel workers get all
753 * of the pages before we can get a chance to get our first page.
754 */
757{
758 Assert(!scan->rs_inited);
759 Assert(scan->rs_base.rs_parallel == NULL);
760
761 /* When there are no pages to scan, return InvalidBlockNumber */
762 if (scan->rs_nblocks == 0 || scan->rs_numblocks == 0)
763 return InvalidBlockNumber;
764
765 if (ScanDirectionIsForward(dir))
766 {
767 return scan->rs_startblock;
768 }
769 else
770 {
771 /*
772 * Disable reporting to syncscan logic in a backwards scan; it's not
773 * very likely anyone else is doing the same thing at the same time,
774 * and much more likely that we'll just bollix things for forward
775 * scanners.
776 */
778
779 /*
780 * Start from last page of the scan. Ensure we take into account
781 * rs_numblocks if it's been adjusted by heap_setscanlimits().
782 */
783 if (scan->rs_numblocks != InvalidBlockNumber)
784 return (scan->rs_startblock + scan->rs_numblocks - 1) % scan->rs_nblocks;
785
786 if (scan->rs_startblock > 0)
787 return scan->rs_startblock - 1;
788
789 return scan->rs_nblocks - 1;
790 }
791}
792
793
794/*
795 * heapgettup_start_page - helper function for heapgettup()
796 *
797 * Return the next page to scan based on the scan->rs_cbuf and set *linesleft
798 * to the number of tuples on this page. Also set *lineoff to the first
799 * offset to scan with forward scans getting the first offset and backward
800 * getting the final offset on the page.
801 */
802static Page
805{
806 Page page;
807
808 Assert(scan->rs_inited);
810
811 /* Caller is responsible for ensuring buffer is locked if needed */
812 page = BufferGetPage(scan->rs_cbuf);
813
815
816 if (ScanDirectionIsForward(dir))
818 else
820
821 /* lineoff now references the physically previous or next tid */
822 return page;
823}
824
825
826/*
827 * heapgettup_continue_page - helper function for heapgettup()
828 *
829 * Return the next page to scan based on the scan->rs_cbuf and set *linesleft
830 * to the number of tuples left to scan on this page. Also set *lineoff to
831 * the next offset to scan according to the ScanDirection in 'dir'.
832 */
833static inline Page
836{
837 Page page;
838
839 Assert(scan->rs_inited);
841
842 /* Caller is responsible for ensuring buffer is locked if needed */
843 page = BufferGetPage(scan->rs_cbuf);
844
845 if (ScanDirectionIsForward(dir))
846 {
848 *linesleft = PageGetMaxOffsetNumber(page) - (*lineoff) + 1;
849 }
850 else
851 {
852 /*
853 * The previous returned tuple may have been vacuumed since the
854 * previous scan when we use a non-MVCC snapshot, so we must
855 * re-establish the lineoff <= PageGetMaxOffsetNumber(page) invariant
856 */
858 *linesleft = *lineoff;
859 }
860
861 /* lineoff now references the physically previous or next tid */
862 return page;
863}
864
865/*
866 * heapgettup_advance_block - helper for heap_fetch_next_buffer()
867 *
868 * Given the current block number, the scan direction, and various information
869 * contained in the scan descriptor, calculate the BlockNumber to scan next
870 * and return it. If there are no further blocks to scan, return
871 * InvalidBlockNumber to indicate this fact to the caller.
872 *
873 * This should not be called to determine the initial block number -- only for
874 * subsequent blocks.
875 *
876 * This also adjusts rs_numblocks when a limit has been imposed by
877 * heap_setscanlimits().
878 */
879static inline BlockNumber
881{
882 Assert(scan->rs_base.rs_parallel == NULL);
883
885 {
886 block++;
887
888 /* wrap back to the start of the heap */
889 if (block >= scan->rs_nblocks)
890 block = 0;
891
892 /*
893 * Report our new scan position for synchronization purposes. We don't
894 * do that when moving backwards, however. That would just mess up any
895 * other forward-moving scanners.
896 *
897 * Note: we do this before checking for end of scan so that the final
898 * state of the position hint is back at the start of the rel. That's
899 * not strictly necessary, but otherwise when you run the same query
900 * multiple times the starting position would shift a little bit
901 * backwards on every invocation, which is confusing. We don't
902 * guarantee any specific ordering in general, though.
903 */
904 if (scan->rs_base.rs_flags & SO_ALLOW_SYNC)
905 ss_report_location(scan->rs_base.rs_rd, block);
906
907 /* we're done if we're back at where we started */
908 if (block == scan->rs_startblock)
909 return InvalidBlockNumber;
910
911 /* check if the limit imposed by heap_setscanlimits() is met */
912 if (scan->rs_numblocks != InvalidBlockNumber)
913 {
914 if (--scan->rs_numblocks == 0)
915 return InvalidBlockNumber;
916 }
917
918 return block;
919 }
920 else
921 {
922 /* we're done if the last block is the start position */
923 if (block == scan->rs_startblock)
924 return InvalidBlockNumber;
925
926 /* check if the limit imposed by heap_setscanlimits() is met */
927 if (scan->rs_numblocks != InvalidBlockNumber)
928 {
929 if (--scan->rs_numblocks == 0)
930 return InvalidBlockNumber;
931 }
932
933 /* wrap to the end of the heap when the last page was page 0 */
934 if (block == 0)
935 block = scan->rs_nblocks;
936
937 block--;
938
939 return block;
940 }
941}
942
943/* ----------------
944 * heapgettup - fetch next heap tuple
945 *
946 * Initialize the scan if not already done; then advance to the next
947 * tuple as indicated by "dir"; return the next tuple in scan->rs_ctup,
948 * or set scan->rs_ctup.t_data = NULL if no more tuples.
949 *
950 * Note: the reason nkeys/key are passed separately, even though they are
951 * kept in the scan descriptor, is that the caller may not want us to check
952 * the scankeys.
953 *
954 * Note: when we fall off the end of the scan in either direction, we
955 * reset rs_inited. This means that a further request with the same
956 * scan direction will restart the scan, which is a bit odd, but a
957 * request with the opposite scan direction will start a fresh scan
958 * in the proper direction. The latter is required behavior for cursors,
959 * while the former case is generally undefined behavior in Postgres
960 * so we don't care too much.
961 * ----------------
962 */
963static void
965 ScanDirection dir,
966 int nkeys,
967 ScanKey key)
968{
969 HeapTuple tuple = &(scan->rs_ctup);
970 Page page;
972 int linesleft;
973
974 if (likely(scan->rs_inited))
975 {
976 /* continue from previously returned page/tuple */
978 page = heapgettup_continue_page(scan, dir, &linesleft, &lineoff);
979 goto continue_page;
980 }
981
982 /*
983 * advance the scan until we find a qualifying tuple or run out of stuff
984 * to scan
985 */
986 while (true)
987 {
988 heap_fetch_next_buffer(scan, dir);
989
990 /* did we run out of blocks to scan? */
991 if (!BufferIsValid(scan->rs_cbuf))
992 break;
993
995
997 page = heapgettup_start_page(scan, dir, &linesleft, &lineoff);
999
1000 /*
1001 * Only continue scanning the page while we have lines left.
1002 *
1003 * Note that this protects us from accessing line pointers past
1004 * PageGetMaxOffsetNumber(); both for forward scans when we resume the
1005 * table scan, and for when we start scanning a new page.
1006 */
1007 for (; linesleft > 0; linesleft--, lineoff += dir)
1008 {
1009 bool visible;
1011
1012 if (!ItemIdIsNormal(lpp))
1013 continue;
1014
1015 tuple->t_data = (HeapTupleHeader) PageGetItem(page, lpp);
1016 tuple->t_len = ItemIdGetLength(lpp);
1017 ItemPointerSet(&(tuple->t_self), scan->rs_cblock, lineoff);
1018
1019 visible = HeapTupleSatisfiesVisibility(tuple,
1020 scan->rs_base.rs_snapshot,
1021 scan->rs_cbuf);
1022
1024 tuple, scan->rs_cbuf,
1025 scan->rs_base.rs_snapshot);
1026
1027 /* skip tuples not visible to this snapshot */
1028 if (!visible)
1029 continue;
1030
1031 /* skip any tuples that don't match the scan key */
1032 if (key != NULL &&
1034 nkeys, key))
1035 continue;
1036
1038 scan->rs_coffset = lineoff;
1039 return;
1040 }
1041
1042 /*
1043 * if we get here, it means we've exhausted the items on this page and
1044 * it's time to move to the next.
1045 */
1047 }
1048
1049 /* end of scan */
1050 if (BufferIsValid(scan->rs_cbuf))
1051 ReleaseBuffer(scan->rs_cbuf);
1052
1053 scan->rs_cbuf = InvalidBuffer;
1056 tuple->t_data = NULL;
1057 scan->rs_inited = false;
1058}
1059
1060/* ----------------
1061 * heapgettup_pagemode - fetch next heap tuple in page-at-a-time mode
1062 *
1063 * Same API as heapgettup, but used in page-at-a-time mode
1064 *
1065 * The internal logic is much the same as heapgettup's too, but there are some
1066 * differences: we do not take the buffer content lock (that only needs to
1067 * happen inside heap_prepare_pagescan), and we iterate through just the
1068 * tuples listed in rs_vistuples[] rather than all tuples on the page. Notice
1069 * that lineindex is 0-based, where the corresponding loop variable lineoff in
1070 * heapgettup is 1-based.
1071 * ----------------
1072 */
1073static void
1075 ScanDirection dir,
1076 int nkeys,
1077 ScanKey key)
1078{
1079 HeapTuple tuple = &(scan->rs_ctup);
1080 Page page;
1083
1084 if (likely(scan->rs_inited))
1085 {
1086 /* continue from previously returned page/tuple */
1087 page = BufferGetPage(scan->rs_cbuf);
1088
1089 lineindex = scan->rs_cindex + dir;
1090 if (ScanDirectionIsForward(dir))
1091 linesleft = scan->rs_ntuples - lineindex;
1092 else
1093 linesleft = scan->rs_cindex;
1094 /* lineindex now references the next or previous visible tid */
1095
1096 goto continue_page;
1097 }
1098
1099 /*
1100 * advance the scan until we find a qualifying tuple or run out of stuff
1101 * to scan
1102 */
1103 while (true)
1104 {
1105 heap_fetch_next_buffer(scan, dir);
1106
1107 /* did we run out of blocks to scan? */
1108 if (!BufferIsValid(scan->rs_cbuf))
1109 break;
1110
1112
1113 /* prune the page and determine visible tuple offsets */
1115 page = BufferGetPage(scan->rs_cbuf);
1116 linesleft = scan->rs_ntuples;
1118
1119 /* block is the same for all tuples, set it once outside the loop */
1120 ItemPointerSetBlockNumber(&tuple->t_self, scan->rs_cblock);
1121
1122 /* lineindex now references the next or previous visible tid */
1124
1125 for (; linesleft > 0; linesleft--, lineindex += dir)
1126 {
1127 ItemId lpp;
1129
1130 Assert(lineindex < scan->rs_ntuples);
1131 lineoff = scan->rs_vistuples[lineindex];
1132 lpp = PageGetItemId(page, lineoff);
1134
1135 tuple->t_data = (HeapTupleHeader) PageGetItem(page, lpp);
1136 tuple->t_len = ItemIdGetLength(lpp);
1138
1139 /* skip any tuples that don't match the scan key */
1140 if (key != NULL &&
1141 !HeapKeyTest(tuple, RelationGetDescr(scan->rs_base.rs_rd),
1142 nkeys, key))
1143 continue;
1144
1145 scan->rs_cindex = lineindex;
1146 return;
1147 }
1148 }
1149
1150 /* end of scan */
1151 if (BufferIsValid(scan->rs_cbuf))
1152 ReleaseBuffer(scan->rs_cbuf);
1153 scan->rs_cbuf = InvalidBuffer;
1156 tuple->t_data = NULL;
1157 scan->rs_inited = false;
1158}
1159
1160
1161/* ----------------------------------------------------------------
1162 * heap access method interface
1163 * ----------------------------------------------------------------
1164 */
1165
1166
1169 int nkeys, ScanKey key,
1170 ParallelTableScanDesc parallel_scan,
1171 uint32 flags)
1172{
1173 HeapScanDesc scan;
1174
1175 /*
1176 * increment relation ref count while scanning relation
1177 *
1178 * This is just to make really sure the relcache entry won't go away while
1179 * the scan has a pointer to it. Caller should be holding the rel open
1180 * anyway, so this is redundant in all normal scenarios...
1181 */
1183
1184 /*
1185 * allocate and initialize scan descriptor
1186 */
1187 if (flags & SO_TYPE_BITMAPSCAN)
1188 {
1190
1191 /*
1192 * Bitmap Heap scans do not have any fields that a normal Heap Scan
1193 * does not have, so no special initializations required here.
1194 */
1195 scan = (HeapScanDesc) bscan;
1196 }
1197 else
1199
1200 scan->rs_base.rs_rd = relation;
1201 scan->rs_base.rs_snapshot = snapshot;
1202 scan->rs_base.rs_nkeys = nkeys;
1203 scan->rs_base.rs_flags = flags;
1204 scan->rs_base.rs_parallel = parallel_scan;
1205 scan->rs_base.rs_instrument = NULL;
1206 scan->rs_strategy = NULL; /* set in initscan */
1207 scan->rs_cbuf = InvalidBuffer;
1208
1209 /*
1210 * Disable page-at-a-time mode if it's not a MVCC-safe snapshot.
1211 */
1212 if (!(snapshot && IsMVCCSnapshot(snapshot)))
1214
1215 /* Check that a historic snapshot is not used for non-catalog tables */
1216 if (snapshot &&
1217 IsHistoricMVCCSnapshot(snapshot) &&
1219 {
1220 ereport(ERROR,
1222 errmsg("cannot query non-catalog table \"%s\" during logical decoding",
1223 RelationGetRelationName(relation))));
1224 }
1225
1226 /*
1227 * For seqscan and sample scans in a serializable transaction, acquire a
1228 * predicate lock on the entire relation. This is required not only to
1229 * lock all the matching tuples, but also to conflict with new insertions
1230 * into the table. In an indexscan, we take page locks on the index pages
1231 * covering the range specified in the scan qual, but in a heap scan there
1232 * is nothing more fine-grained to lock. A bitmap scan is a different
1233 * story, there we have already scanned the index and locked the index
1234 * pages covering the predicate. But in that case we still have to lock
1235 * any matching heap tuples. For sample scan we could optimize the locking
1236 * to be at least page-level granularity, but we'd need to add per-tuple
1237 * locking for that.
1238 */
1240 {
1241 /*
1242 * Ensure a missing snapshot is noticed reliably, even if the
1243 * isolation mode means predicate locking isn't performed (and
1244 * therefore the snapshot isn't used here).
1245 */
1246 Assert(snapshot);
1247 PredicateLockRelation(relation, snapshot);
1248 }
1249
1250 /* we only need to set this up once */
1251 scan->rs_ctup.t_tableOid = RelationGetRelid(relation);
1252
1253 /*
1254 * Allocate memory to keep track of page allocation for parallel workers
1255 * when doing a parallel scan.
1256 */
1257 if (parallel_scan != NULL)
1259 else
1261
1262 /*
1263 * we do this here instead of in initscan() because heap_rescan also calls
1264 * initscan() and we don't want to allocate memory again
1265 */
1266 if (nkeys > 0)
1267 scan->rs_base.rs_key = palloc_array(ScanKeyData, nkeys);
1268 else
1269 scan->rs_base.rs_key = NULL;
1270
1271 initscan(scan, key, false);
1272
1273 scan->rs_read_stream = NULL;
1274
1275 /*
1276 * Set up a read stream for sequential scans and TID range scans. This
1277 * should be done after initscan() because initscan() allocates the
1278 * BufferAccessStrategy object passed to the read stream API.
1279 */
1280 if (scan->rs_base.rs_flags & SO_TYPE_SEQSCAN ||
1282 {
1284
1285 if (scan->rs_base.rs_parallel)
1287 else
1289
1290 /* ---
1291 * It is safe to use batchmode as the only locks taken by `cb`
1292 * are never taken while waiting for IO:
1293 * - SyncScanLock is used in the non-parallel case
1294 * - in the parallel case, only spinlocks and atomics are used
1295 * ---
1296 */
1299 scan->rs_strategy,
1300 scan->rs_base.rs_rd,
1302 cb,
1303 scan,
1304 0);
1305 }
1306 else if (scan->rs_base.rs_flags & SO_TYPE_BITMAPSCAN)
1307 {
1310 scan->rs_strategy,
1311 scan->rs_base.rs_rd,
1314 scan,
1315 sizeof(TBMIterateResult));
1316 }
1317
1318 /* enable read stream instrumentation */
1319 if ((flags & SO_SCAN_INSTRUMENT) && (scan->rs_read_stream != NULL))
1320 {
1323 &scan->rs_base.rs_instrument->io);
1324 }
1325
1326 scan->rs_vmbuffer = InvalidBuffer;
1327
1328 return (TableScanDesc) scan;
1329}
1330
1331void
1333 bool allow_strat, bool allow_sync, bool allow_pagemode)
1334{
1336
1337 if (set_params)
1338 {
1339 if (allow_strat)
1341 else
1343
1344 if (allow_sync)
1346 else
1348
1349 if (allow_pagemode && scan->rs_base.rs_snapshot &&
1352 else
1354 }
1355
1356 /*
1357 * unpin scan buffers
1358 */
1359 if (BufferIsValid(scan->rs_cbuf))
1360 {
1361 ReleaseBuffer(scan->rs_cbuf);
1362 scan->rs_cbuf = InvalidBuffer;
1363 }
1364
1365 if (BufferIsValid(scan->rs_vmbuffer))
1366 {
1368 scan->rs_vmbuffer = InvalidBuffer;
1369 }
1370
1371 /*
1372 * SO_TYPE_BITMAPSCAN would be cleaned up here, but it does not hold any
1373 * additional data vs a normal HeapScan
1374 */
1375
1376 /*
1377 * The read stream is reset on rescan. This must be done before
1378 * initscan(), as some state referred to by read_stream_reset() is reset
1379 * in initscan().
1380 */
1381 if (scan->rs_read_stream)
1383
1384 /*
1385 * reinitialize scan descriptor
1386 */
1387 initscan(scan, key, true);
1388}
1389
1390void
1392{
1394
1395 /* Note: no locking manipulations needed */
1396
1397 /*
1398 * unpin scan buffers
1399 */
1400 if (BufferIsValid(scan->rs_cbuf))
1401 ReleaseBuffer(scan->rs_cbuf);
1402
1403 if (BufferIsValid(scan->rs_vmbuffer))
1405
1406 /*
1407 * Must free the read stream before freeing the BufferAccessStrategy.
1408 */
1409 if (scan->rs_read_stream)
1411
1412 /*
1413 * decrement relation reference count and free scan descriptor storage
1414 */
1416
1417 if (scan->rs_base.rs_key)
1418 pfree(scan->rs_base.rs_key);
1419
1420 if (scan->rs_strategy != NULL)
1422
1423 if (scan->rs_parallelworkerdata != NULL)
1425
1426 if (scan->rs_base.rs_flags & SO_TEMP_SNAPSHOT)
1428
1429 if (scan->rs_base.rs_instrument)
1431
1432 pfree(scan);
1433}
1434
1437{
1439
1440 /*
1441 * This is still widely used directly, without going through table AM, so
1442 * add a safety check. It's possible we should, at a later point,
1443 * downgrade this to an assert. The reason for checking the AM routine,
1444 * rather than the AM oid, is that this allows to write regression tests
1445 * that create another AM reusing the heap handler.
1446 */
1447 if (unlikely(sscan->rs_rd->rd_tableam != GetHeapamTableAmRoutine()))
1448 ereport(ERROR,
1450 errmsg_internal("only heap AM is supported")));
1451
1452 /* Note: no locking manipulations needed */
1453
1455 heapgettup_pagemode(scan, direction,
1456 scan->rs_base.rs_nkeys, scan->rs_base.rs_key);
1457 else
1458 heapgettup(scan, direction,
1459 scan->rs_base.rs_nkeys, scan->rs_base.rs_key);
1460
1461 if (scan->rs_ctup.t_data == NULL)
1462 return NULL;
1463
1464 /*
1465 * if we get here it means we have a new current scan tuple, so point to
1466 * the proper return buffer and return the tuple.
1467 */
1468
1470
1471 return &scan->rs_ctup;
1472}
1473
1474bool
1476{
1478
1479 /* Note: no locking manipulations needed */
1480
1481 if (sscan->rs_flags & SO_ALLOW_PAGEMODE)
1482 heapgettup_pagemode(scan, direction, sscan->rs_nkeys, sscan->rs_key);
1483 else
1484 heapgettup(scan, direction, sscan->rs_nkeys, sscan->rs_key);
1485
1486 if (scan->rs_ctup.t_data == NULL)
1487 {
1488 ExecClearTuple(slot);
1489 return false;
1490 }
1491
1492 /*
1493 * if we get here it means we have a new current scan tuple, so point to
1494 * the proper return buffer and return the tuple.
1495 */
1496
1498
1499 ExecStoreBufferHeapTuple(&scan->rs_ctup, slot,
1500 scan->rs_cbuf);
1501 return true;
1502}
1503
1504void
1507{
1513
1514 /*
1515 * For relations without any pages, we can simply leave the TID range
1516 * unset. There will be no tuples to scan, therefore no tuples outside
1517 * the given TID range.
1518 */
1519 if (scan->rs_nblocks == 0)
1520 return;
1521
1522 /*
1523 * Set up some ItemPointers which point to the first and last possible
1524 * tuples in the heap.
1525 */
1528
1529 /*
1530 * If the given maximum TID is below the highest possible TID in the
1531 * relation, then restrict the range to that, otherwise we scan to the end
1532 * of the relation.
1533 */
1536
1537 /*
1538 * If the given minimum TID is above the lowest possible TID in the
1539 * relation, then restrict the range to only scan for TIDs above that.
1540 */
1543
1544 /*
1545 * Check for an empty range and protect from would be negative results
1546 * from the numBlks calculation below.
1547 */
1549 {
1550 /* Set an empty range of blocks to scan */
1552 return;
1553 }
1554
1555 /*
1556 * Calculate the first block and the number of blocks we must scan. We
1557 * could be more aggressive here and perform some more validation to try
1558 * and further narrow the scope of blocks to scan by checking if the
1559 * lowestItem has an offset above MaxOffsetNumber. In this case, we could
1560 * advance startBlk by one. Likewise, if highestItem has an offset of 0
1561 * we could scan one fewer blocks. However, such an optimization does not
1562 * seem worth troubling over, currently.
1563 */
1565
1568
1569 /* Set the start block and number of blocks to scan */
1571
1572 /* Finally, set the TID range in sscan */
1573 ItemPointerCopy(&lowestItem, &sscan->st.tidrange.rs_mintid);
1574 ItemPointerCopy(&highestItem, &sscan->st.tidrange.rs_maxtid);
1575}
1576
1577bool
1579 TupleTableSlot *slot)
1580{
1582 ItemPointer mintid = &sscan->st.tidrange.rs_mintid;
1583 ItemPointer maxtid = &sscan->st.tidrange.rs_maxtid;
1584
1585 /* Note: no locking manipulations needed */
1586 for (;;)
1587 {
1588 if (sscan->rs_flags & SO_ALLOW_PAGEMODE)
1589 heapgettup_pagemode(scan, direction, sscan->rs_nkeys, sscan->rs_key);
1590 else
1591 heapgettup(scan, direction, sscan->rs_nkeys, sscan->rs_key);
1592
1593 if (scan->rs_ctup.t_data == NULL)
1594 {
1595 ExecClearTuple(slot);
1596 return false;
1597 }
1598
1599 /*
1600 * heap_set_tidrange will have used heap_setscanlimits to limit the
1601 * range of pages we scan to only ones that can contain the TID range
1602 * we're scanning for. Here we must filter out any tuples from these
1603 * pages that are outside of that range.
1604 */
1605 if (ItemPointerCompare(&scan->rs_ctup.t_self, mintid) < 0)
1606 {
1607 ExecClearTuple(slot);
1608
1609 /*
1610 * When scanning backwards, the TIDs will be in descending order.
1611 * Future tuples in this direction will be lower still, so we can
1612 * just return false to indicate there will be no more tuples.
1613 */
1614 if (ScanDirectionIsBackward(direction))
1615 return false;
1616
1617 continue;
1618 }
1619
1620 /*
1621 * Likewise for the final page, we must filter out TIDs greater than
1622 * maxtid.
1623 */
1624 if (ItemPointerCompare(&scan->rs_ctup.t_self, maxtid) > 0)
1625 {
1626 ExecClearTuple(slot);
1627
1628 /*
1629 * When scanning forward, the TIDs will be in ascending order.
1630 * Future tuples in this direction will be higher still, so we can
1631 * just return false to indicate there will be no more tuples.
1632 */
1633 if (ScanDirectionIsForward(direction))
1634 return false;
1635 continue;
1636 }
1637
1638 break;
1639 }
1640
1641 /*
1642 * if we get here it means we have a new current scan tuple, so point to
1643 * the proper return buffer and return the tuple.
1644 */
1646
1647 ExecStoreBufferHeapTuple(&scan->rs_ctup, slot, scan->rs_cbuf);
1648 return true;
1649}
1650
1651/*
1652 * heap_fetch - retrieve tuple with given tid
1653 *
1654 * On entry, tuple->t_self is the TID to fetch. We pin the buffer holding
1655 * the tuple, fill in the remaining fields of *tuple, and check the tuple
1656 * against the specified snapshot.
1657 *
1658 * If successful (tuple found and passes snapshot time qual), then *userbuf
1659 * is set to the buffer holding the tuple and true is returned. The caller
1660 * must unpin the buffer when done with the tuple.
1661 *
1662 * If the tuple is not found (ie, item number references a deleted slot),
1663 * then tuple->t_data is set to NULL, *userbuf is set to InvalidBuffer,
1664 * and false is returned.
1665 *
1666 * If the tuple is found but fails the time qual check, then the behavior
1667 * depends on the keep_buf parameter. If keep_buf is false, the results
1668 * are the same as for the tuple-not-found case. If keep_buf is true,
1669 * then tuple->t_data and *userbuf are returned as for the success case,
1670 * and again the caller must unpin the buffer; but false is returned.
1671 *
1672 * heap_fetch does not follow HOT chains: only the exact TID requested will
1673 * be fetched.
1674 *
1675 * It is somewhat inconsistent that we ereport() on invalid block number but
1676 * return false on invalid item number. There are a couple of reasons though.
1677 * One is that the caller can relatively easily check the block number for
1678 * validity, but cannot check the item number without reading the page
1679 * himself. Another is that when we are following a t_ctid link, we can be
1680 * reasonably confident that the page number is valid (since VACUUM shouldn't
1681 * truncate off the destination page without having killed the referencing
1682 * tuple first), but the item number might well not be good.
1683 */
1684bool
1686 Snapshot snapshot,
1687 HeapTuple tuple,
1688 Buffer *userbuf,
1689 bool keep_buf)
1690{
1691 ItemPointer tid = &(tuple->t_self);
1692 ItemId lp;
1693 Buffer buffer;
1694 Page page;
1695 OffsetNumber offnum;
1696 bool valid;
1697
1698 /*
1699 * Fetch and pin the appropriate page of the relation.
1700 */
1701 buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
1702
1703 /*
1704 * Need share lock on buffer to examine tuple commit status.
1705 */
1707 page = BufferGetPage(buffer);
1708
1709 /*
1710 * We'd better check for out-of-range offnum in case of VACUUM since the
1711 * TID was obtained.
1712 */
1713 offnum = ItemPointerGetOffsetNumber(tid);
1715 {
1716 UnlockReleaseBuffer(buffer);
1718 tuple->t_data = NULL;
1719 return false;
1720 }
1721
1722 /*
1723 * get the item line pointer corresponding to the requested tid
1724 */
1725 lp = PageGetItemId(page, offnum);
1726
1727 /*
1728 * Must check for deleted tuple.
1729 */
1730 if (!ItemIdIsNormal(lp))
1731 {
1732 UnlockReleaseBuffer(buffer);
1734 tuple->t_data = NULL;
1735 return false;
1736 }
1737
1738 /*
1739 * fill in *tuple fields
1740 */
1741 tuple->t_data = (HeapTupleHeader) PageGetItem(page, lp);
1742 tuple->t_len = ItemIdGetLength(lp);
1743 tuple->t_tableOid = RelationGetRelid(relation);
1744
1745 /*
1746 * check tuple visibility, then release lock
1747 */
1748 valid = HeapTupleSatisfiesVisibility(tuple, snapshot, buffer);
1749
1750 if (valid)
1751 PredicateLockTID(relation, &(tuple->t_self), snapshot,
1753
1754 HeapCheckForSerializableConflictOut(valid, relation, tuple, buffer, snapshot);
1755
1757
1758 if (valid)
1759 {
1760 /*
1761 * All checks passed, so return the tuple as valid. Caller is now
1762 * responsible for releasing the buffer.
1763 */
1764 *userbuf = buffer;
1765
1766 return true;
1767 }
1768
1769 /* Tuple failed time qual, but maybe caller wants to see it anyway. */
1770 if (keep_buf)
1771 *userbuf = buffer;
1772 else
1773 {
1774 ReleaseBuffer(buffer);
1776 tuple->t_data = NULL;
1777 }
1778
1779 return false;
1780}
1781
1782/*
1783 * heap_get_latest_tid - get the latest tid of a specified tuple
1784 *
1785 * Actually, this gets the latest version that is visible according to the
1786 * scan's snapshot. Create a scan using SnapshotDirty to get the very latest,
1787 * possibly uncommitted version.
1788 *
1789 * *tid is both an input and an output parameter: it is updated to
1790 * show the latest version of the row. Note that it will not be changed
1791 * if no version of the row passes the snapshot test.
1792 */
1793void
1795 ItemPointer tid)
1796{
1797 Relation relation = sscan->rs_rd;
1798 Snapshot snapshot = sscan->rs_snapshot;
1799 ItemPointerData ctid;
1801
1802 /*
1803 * table_tuple_get_latest_tid() verified that the passed in tid is valid.
1804 * Assume that t_ctid links are valid however - there shouldn't be invalid
1805 * ones in the table.
1806 */
1808
1809 /*
1810 * Loop to chase down t_ctid links. At top of loop, ctid is the tuple we
1811 * need to examine, and *tid is the TID we will return if ctid turns out
1812 * to be bogus.
1813 *
1814 * Note that we will loop until we reach the end of the t_ctid chain.
1815 * Depending on the snapshot passed, there might be at most one visible
1816 * version of the row, but we don't try to optimize for that.
1817 */
1818 ctid = *tid;
1819 priorXmax = InvalidTransactionId; /* cannot check first XMIN */
1820 for (;;)
1821 {
1822 Buffer buffer;
1823 Page page;
1824 OffsetNumber offnum;
1825 ItemId lp;
1826 HeapTupleData tp;
1827 bool valid;
1828
1829 /*
1830 * Read, pin, and lock the page.
1831 */
1832 buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(&ctid));
1834 page = BufferGetPage(buffer);
1835
1836 /*
1837 * Check for bogus item number. This is not treated as an error
1838 * condition because it can happen while following a t_ctid link. We
1839 * just assume that the prior tid is OK and return it unchanged.
1840 */
1841 offnum = ItemPointerGetOffsetNumber(&ctid);
1843 {
1844 UnlockReleaseBuffer(buffer);
1845 break;
1846 }
1847 lp = PageGetItemId(page, offnum);
1848 if (!ItemIdIsNormal(lp))
1849 {
1850 UnlockReleaseBuffer(buffer);
1851 break;
1852 }
1853
1854 /* OK to access the tuple */
1855 tp.t_self = ctid;
1856 tp.t_data = (HeapTupleHeader) PageGetItem(page, lp);
1857 tp.t_len = ItemIdGetLength(lp);
1858 tp.t_tableOid = RelationGetRelid(relation);
1859
1860 /*
1861 * After following a t_ctid link, we might arrive at an unrelated
1862 * tuple. Check for XMIN match.
1863 */
1866 {
1867 UnlockReleaseBuffer(buffer);
1868 break;
1869 }
1870
1871 /*
1872 * Check tuple visibility; if visible, set it as the new result
1873 * candidate.
1874 */
1875 valid = HeapTupleSatisfiesVisibility(&tp, snapshot, buffer);
1876 HeapCheckForSerializableConflictOut(valid, relation, &tp, buffer, snapshot);
1877 if (valid)
1878 *tid = ctid;
1879
1880 /*
1881 * If there's a valid t_ctid link, follow it, else we're done.
1882 */
1883 if ((tp.t_data->t_infomask & HEAP_XMAX_INVALID) ||
1887 {
1888 UnlockReleaseBuffer(buffer);
1889 break;
1890 }
1891
1892 ctid = tp.t_data->t_ctid;
1894 UnlockReleaseBuffer(buffer);
1895 } /* end of loop */
1896}
1897
1898
1899/*
1900 * UpdateXmaxHintBits - update tuple hint bits after xmax transaction ends
1901 *
1902 * This is called after we have waited for the XMAX transaction to terminate.
1903 * If the transaction aborted, we guarantee the XMAX_INVALID hint bit will
1904 * be set on exit. If the transaction committed, we set the XMAX_COMMITTED
1905 * hint bit if possible --- but beware that that may not yet be possible,
1906 * if the transaction committed asynchronously.
1907 *
1908 * Note that if the transaction was a locker only, we set HEAP_XMAX_INVALID
1909 * even if it commits.
1910 *
1911 * Hence callers should look only at XMAX_INVALID.
1912 *
1913 * Note this is not allowed for tuples whose xmax is a multixact.
1914 */
1915static void
1917{
1920
1922 {
1923 if (!HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_infomask) &&
1926 xid);
1927 else
1930 }
1931}
1932
1933
1934/*
1935 * GetBulkInsertState - prepare status object for a bulk insert
1936 */
1939{
1940 BulkInsertState bistate;
1941
1944 bistate->current_buf = InvalidBuffer;
1945 bistate->next_free = InvalidBlockNumber;
1946 bistate->last_free = InvalidBlockNumber;
1947 bistate->already_extended_by = 0;
1948 return bistate;
1949}
1950
1951/*
1952 * FreeBulkInsertState - clean up after finishing a bulk insert
1953 */
1954void
1956{
1957 if (bistate->current_buf != InvalidBuffer)
1958 ReleaseBuffer(bistate->current_buf);
1959 FreeAccessStrategy(bistate->strategy);
1960 pfree(bistate);
1961}
1962
1963/*
1964 * ReleaseBulkInsertStatePin - release a buffer currently held in bistate
1965 */
1966void
1968{
1969 if (bistate->current_buf != InvalidBuffer)
1970 ReleaseBuffer(bistate->current_buf);
1971 bistate->current_buf = InvalidBuffer;
1972
1973 /*
1974 * Despite the name, we also reset bulk relation extension state.
1975 * Otherwise we can end up erroring out due to looking for free space in
1976 * ->next_free of one partition, even though ->next_free was set when
1977 * extending another partition. It could obviously also be bad for
1978 * efficiency to look at existing blocks at offsets from another
1979 * partition, even if we don't error out.
1980 */
1981 bistate->next_free = InvalidBlockNumber;
1982 bistate->last_free = InvalidBlockNumber;
1983}
1984
1985
1986/*
1987 * heap_insert - insert tuple into a heap
1988 *
1989 * The new tuple is stamped with current transaction ID and the specified
1990 * command ID.
1991 *
1992 * See table_tuple_insert for comments about most of the input flags, except
1993 * that this routine directly takes a tuple rather than a slot.
1994 *
1995 * There's corresponding HEAP_INSERT_ options to all the TABLE_INSERT_
1996 * options, and there additionally is HEAP_INSERT_SPECULATIVE which is used to
1997 * implement table_tuple_insert_speculative().
1998 *
1999 * On return the header fields of *tup are updated to match the stored tuple;
2000 * in particular tup->t_self receives the actual TID where the tuple was
2001 * stored. But note that any toasting of fields within the tuple data is NOT
2002 * reflected into *tup.
2003 */
2004void
2007{
2010 Buffer buffer;
2011 Page page;
2012 Buffer vmbuffer = InvalidBuffer;
2013 bool clear_all_visible = false;
2014 bool vmbuffer_modified = false;
2015
2016 /* Cheap, simplistic check that the tuple matches the rel's rowtype. */
2019
2020 AssertHasSnapshotForToast(relation);
2021
2022 /*
2023 * Fill in tuple header fields and toast the tuple if necessary.
2024 *
2025 * Note: below this point, heaptup is the data we actually intend to store
2026 * into the relation; tup is the caller's original untoasted data.
2027 */
2028 heaptup = heap_prepare_insert(relation, tup, xid, cid, options);
2029
2030 /*
2031 * Find buffer to insert this tuple into. If the page is all visible,
2032 * this will also pin the requisite visibility map page.
2033 */
2034 buffer = RelationGetBufferForTuple(relation, heaptup->t_len,
2035 InvalidBuffer, options, bistate,
2036 &vmbuffer, NULL,
2037 0);
2038
2039 page = BufferGetPage(buffer);
2040
2041 /*
2042 * We're about to do the actual insert -- but check for conflict first, to
2043 * avoid possibly having to roll back work we've just done.
2044 *
2045 * This is safe without a recheck as long as there is no possibility of
2046 * another process scanning the page between this check and the insert
2047 * being visible to the scan (i.e., an exclusive buffer content lock is
2048 * continuously held from this point until the tuple insert is visible).
2049 *
2050 * For a heap insert, we only need to check for table-level SSI locks. Our
2051 * new tuple can't possibly conflict with existing tuple locks, and heap
2052 * page locks are only consolidated versions of tuple locks; they do not
2053 * lock "gaps" as index page locks do. So we don't need to specify a
2054 * buffer when making the call, which makes for a faster check.
2055 */
2057
2058 /* Lock the vmbuffer before the critical section */
2059 if (PageIsAllVisible(page))
2060 {
2062 clear_all_visible = true;
2063 }
2064
2065 /* NO EREPORT(ERROR) from here till changes are logged */
2067
2068 RelationPutHeapTuple(relation, buffer, heaptup,
2070
2072 {
2073 /* It's possible the VM bits were already clear */
2074 if (visibilitymap_clear(relation->rd_locator,
2076 vmbuffer, VISIBILITYMAP_VALID_BITS))
2077 vmbuffer_modified = true;
2078
2079 PageClearAllVisible(page);
2080 }
2081
2082 /*
2083 * Set pd_prune_xid to trigger heap_page_prune_and_freeze() once the page
2084 * is full so that we can set the page all-visible in the VM on the next
2085 * page access.
2086 *
2087 * Setting pd_prune_xid is also handy if the inserting transaction
2088 * eventually aborts making this tuple DEAD and hence available for
2089 * pruning. If no other tuple in this page is UPDATEd/DELETEd, the aborted
2090 * tuple would never otherwise be pruned until next vacuum is triggered.
2091 *
2092 * Don't set it if we are in bootstrap mode or we are inserting a frozen
2093 * tuple, as there is no further pruning/freezing needed in those cases.
2094 */
2096 PageSetPrunable(page, xid);
2097
2098 MarkBufferDirty(buffer);
2099
2100 /* XLOG stuff */
2101 if (RelationNeedsWAL(relation))
2102 {
2106 uint8 info = XLOG_HEAP_INSERT;
2107 int bufflags = 0;
2108
2109 /*
2110 * If this is a catalog, we need to transmit combo CIDs to properly
2111 * decode, so log that as well.
2112 */
2114 log_heap_new_cid(relation, heaptup);
2115
2116 /*
2117 * If this is the single and first tuple on page, we can reinit the
2118 * page instead of restoring the whole thing. Set flag, and hide
2119 * buffer references from XLogInsert.
2120 */
2123 {
2124 info |= XLOG_HEAP_INIT_PAGE;
2126 }
2127
2128 xlrec.offnum = ItemPointerGetOffsetNumber(&heaptup->t_self);
2129 xlrec.flags = 0;
2135
2136 /*
2137 * For logical decoding, we need the tuple even if we're doing a full
2138 * page write, so make sure it's included even if we take a full-page
2139 * image. (XXX We could alternatively store a pointer into the FPW).
2140 */
2141 if (RelationIsLogicallyLogged(relation) &&
2143 {
2146
2147 if (IsToastRelation(relation))
2149 }
2150
2153
2154 xlhdr.t_infomask2 = heaptup->t_data->t_infomask2;
2155 xlhdr.t_infomask = heaptup->t_data->t_infomask;
2156 xlhdr.t_hoff = heaptup->t_data->t_hoff;
2157
2158 /*
2159 * note we mark xlhdr as belonging to buffer; if XLogInsert decides to
2160 * write the whole page to the xlog, we don't need to store
2161 * xl_heap_header in the xlog.
2162 */
2167 /* PG73FORMAT: write bitmap [+ padding] [+ oid] + data */
2169 (char *) heaptup->t_data + SizeofHeapTupleHeader,
2171
2172 /* filtering by origin on a row level is much more efficient */
2174
2177
2178 recptr = XLogInsert(RM_HEAP_ID, info);
2179
2180 PageSetLSN(page, recptr);
2181
2183 PageSetLSN(BufferGetPage(vmbuffer), recptr);
2184 }
2185
2187
2188 UnlockReleaseBuffer(buffer);
2189
2190 /*
2191 * We locked vmbuffer if clear_all_visible was true regardless of whether
2192 * or not we ended up modifying the vmbuffer.
2193 */
2195 LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK);
2196 if (BufferIsValid(vmbuffer))
2197 ReleaseBuffer(vmbuffer);
2198
2199 /*
2200 * If tuple is cacheable, mark it for invalidation from the caches in case
2201 * we abort. Note it is OK to do this after releasing the buffer, because
2202 * the heaptup data structure is all in local memory, not in the shared
2203 * buffer.
2204 */
2206
2207 /* Note: speculative insertions are counted too, even if aborted later */
2208 pgstat_count_heap_insert(relation, 1);
2209
2210 /*
2211 * If heaptup is a private copy, release it. Don't forget to copy t_self
2212 * back to the caller's image, too.
2213 */
2214 if (heaptup != tup)
2215 {
2216 tup->t_self = heaptup->t_self;
2218 }
2219}
2220
2221/*
2222 * Subroutine for heap_insert(). Prepares a tuple for insertion. This sets the
2223 * tuple header fields and toasts the tuple if necessary. Returns a toasted
2224 * version of the tuple if it was toasted, or the original tuple if not. Note
2225 * that in any case, the header fields are also set in the original tuple.
2226 */
2227static HeapTuple
2230{
2231 /*
2232 * To allow parallel inserts, we need to ensure that they are safe to be
2233 * performed in workers. We have the infrastructure to allow parallel
2234 * inserts in general except for the cases where inserts generate a new
2235 * CommandId (eg. inserts into a table having a foreign key column).
2236 */
2237 if (IsParallelWorker())
2238 ereport(ERROR,
2240 errmsg("cannot insert tuples in a parallel worker")));
2241
2242 tup->t_data->t_infomask &= ~(HEAP_XACT_MASK);
2243 tup->t_data->t_infomask2 &= ~(HEAP2_XACT_MASK);
2244 tup->t_data->t_infomask |= HEAP_XMAX_INVALID;
2245 HeapTupleHeaderSetXmin(tup->t_data, xid);
2248
2249 HeapTupleHeaderSetCmin(tup->t_data, cid);
2250 HeapTupleHeaderSetXmax(tup->t_data, 0); /* for cleanliness */
2251 tup->t_tableOid = RelationGetRelid(relation);
2252
2253 /*
2254 * If the new tuple is too big for storage or contains already toasted
2255 * out-of-line attributes from some other relation, invoke the toaster.
2256 */
2257 if (relation->rd_rel->relkind != RELKIND_RELATION &&
2258 relation->rd_rel->relkind != RELKIND_MATVIEW)
2259 {
2260 /* toast table entries should never be recursively toasted */
2262 return tup;
2263 }
2264 else if (HeapTupleHasExternal(tup) || tup->t_len > TOAST_TUPLE_THRESHOLD)
2265 return heap_toast_insert_or_update(relation, tup, NULL, options);
2266 else
2267 return tup;
2268}
2269
2270/*
2271 * Helper for heap_multi_insert() that computes the number of entire pages
2272 * that inserting the remaining heaptuples requires. Used to determine how
2273 * much the relation needs to be extended by.
2274 */
2275static int
2277{
2279 int npages = 1;
2280
2281 for (int i = done; i < ntuples; i++)
2282 {
2283 size_t tup_sz = sizeof(ItemIdData) + MAXALIGN(heaptuples[i]->t_len);
2284
2285 if (page_avail < tup_sz)
2286 {
2287 npages++;
2289 }
2290 page_avail -= tup_sz;
2291 }
2292
2293 return npages;
2294}
2295
2296/*
2297 * heap_multi_insert - insert multiple tuples into a heap
2298 *
2299 * This is like heap_insert(), but inserts multiple tuples in one operation.
2300 * That's faster than calling heap_insert() in a loop, because when multiple
2301 * tuples can be inserted on a single page, we can write just a single WAL
2302 * record covering all of them, and only need to lock/unlock the page once.
2303 *
2304 * Note: this leaks memory into the current memory context. You can create a
2305 * temporary context before calling this, if that's a problem.
2306 */
2307void
2308heap_multi_insert(Relation relation, TupleTableSlot **slots, int ntuples,
2310{
2313 int i;
2314 int ndone;
2316 Page page;
2317 Buffer vmbuffer = InvalidBuffer;
2318 bool needwal;
2322 bool starting_with_empty_page = false;
2323 int npages = 0;
2324 int npages_used = 0;
2325
2326 /* currently not needed (thus unsupported) for heap_multi_insert() */
2328
2329 AssertHasSnapshotForToast(relation);
2330
2331 needwal = RelationNeedsWAL(relation);
2334
2335 /* Toast and set header data in all the slots */
2336 heaptuples = palloc(ntuples * sizeof(HeapTuple));
2337 for (i = 0; i < ntuples; i++)
2338 {
2339 HeapTuple tuple;
2340
2341 tuple = ExecFetchSlotHeapTuple(slots[i], true, NULL);
2342 slots[i]->tts_tableOid = RelationGetRelid(relation);
2343 tuple->t_tableOid = slots[i]->tts_tableOid;
2344 heaptuples[i] = heap_prepare_insert(relation, tuple, xid, cid,
2345 options);
2346 }
2347
2348 /*
2349 * We're about to do the actual inserts -- but check for conflict first,
2350 * to minimize the possibility of having to roll back work we've just
2351 * done.
2352 *
2353 * A check here does not definitively prevent a serialization anomaly;
2354 * that check MUST be done at least past the point of acquiring an
2355 * exclusive buffer content lock on every buffer that will be affected,
2356 * and MAY be done after all inserts are reflected in the buffers and
2357 * those locks are released; otherwise there is a race condition. Since
2358 * multiple buffers can be locked and unlocked in the loop below, and it
2359 * would not be feasible to identify and lock all of those buffers before
2360 * the loop, we must do a final check at the end.
2361 *
2362 * The check here could be omitted with no loss of correctness; it is
2363 * present strictly as an optimization.
2364 *
2365 * For heap inserts, we only need to check for table-level SSI locks. Our
2366 * new tuples can't possibly conflict with existing tuple locks, and heap
2367 * page locks are only consolidated versions of tuple locks; they do not
2368 * lock "gaps" as index page locks do. So we don't need to specify a
2369 * buffer when making the call, which makes for a faster check.
2370 */
2372
2373 ndone = 0;
2374 while (ndone < ntuples)
2375 {
2376 Buffer buffer;
2377 bool clear_all_visible = false;
2378 bool all_frozen_set = false;
2379 bool vmbuffer_modified = false;
2380 int nthispage;
2381
2383
2384 /*
2385 * Compute number of pages needed to fit the to-be-inserted tuples in
2386 * the worst case. This will be used to determine how much to extend
2387 * the relation by in RelationGetBufferForTuple(), if needed. If we
2388 * filled a prior page from scratch, we can just update our last
2389 * computation, but if we started with a partially filled page,
2390 * recompute from scratch, the number of potentially required pages
2391 * can vary due to tuples needing to fit onto the page, page headers
2392 * etc.
2393 */
2394 if (ndone == 0 || !starting_with_empty_page)
2395 {
2396 npages = heap_multi_insert_pages(heaptuples, ndone, ntuples,
2398 npages_used = 0;
2399 }
2400 else
2401 npages_used++;
2402
2403 /*
2404 * Find buffer where at least the next tuple will fit. If the page is
2405 * all-visible, this will also pin the requisite visibility map page.
2406 *
2407 * Also pin visibility map page if COPY FREEZE inserts tuples into an
2408 * empty page. See all_frozen_set below.
2409 */
2410 buffer = RelationGetBufferForTuple(relation, heaptuples[ndone]->t_len,
2411 InvalidBuffer, options, bistate,
2412 &vmbuffer, NULL,
2413 npages - npages_used);
2414 page = BufferGetPage(buffer);
2415
2417
2419 {
2420 all_frozen_set = true;
2421 /* Lock the vmbuffer before entering the critical section */
2423 }
2424 else if (PageIsAllVisible(page) && !(options & HEAP_INSERT_FROZEN))
2425 {
2426 clear_all_visible = true;
2427 /* Lock the vmbuffer before entering the critical section */
2429 }
2430
2431 /* NO EREPORT(ERROR) from here till changes are logged */
2433
2434 /*
2435 * RelationGetBufferForTuple has ensured that the first tuple fits.
2436 * Put that on the page, and then as many other tuples as fit.
2437 */
2438 RelationPutHeapTuple(relation, buffer, heaptuples[ndone], false);
2439
2440 /*
2441 * For logical decoding we need combo CIDs to properly decode the
2442 * catalog.
2443 */
2444 if (needwal && need_cids)
2445 log_heap_new_cid(relation, heaptuples[ndone]);
2446
2447 for (nthispage = 1; ndone + nthispage < ntuples; nthispage++)
2448 {
2450
2451 if (PageGetHeapFreeSpace(page) < MAXALIGN(heaptup->t_len) + saveFreeSpace)
2452 break;
2453
2454 RelationPutHeapTuple(relation, buffer, heaptup, false);
2455
2456 /*
2457 * For logical decoding we need combo CIDs to properly decode the
2458 * catalog.
2459 */
2460 if (needwal && need_cids)
2461 log_heap_new_cid(relation, heaptup);
2462 }
2463
2464 /*
2465 * If the page is all visible, need to clear that, unless we're only
2466 * going to add further frozen rows to it.
2467 *
2468 * If we're only adding already frozen rows to a previously empty
2469 * page, mark it as all-frozen and update the visibility map. We're
2470 * already holding a pin on the vmbuffer.
2471 */
2473 {
2475 /* It's possible the VM bits were already clear */
2476 if (visibilitymap_clear(relation->rd_locator,
2477 BufferGetBlockNumber(buffer),
2478 vmbuffer, VISIBILITYMAP_VALID_BITS))
2479 vmbuffer_modified = true;
2480
2481 PageClearAllVisible(page);
2482 }
2483 else if (all_frozen_set)
2484 {
2485 PageSetAllVisible(page);
2486 PageClearPrunable(page);
2488 vmbuffer,
2491 relation->rd_locator);
2492 }
2493
2494 /*
2495 * Set pd_prune_xid. See heap_insert() for more on why we do this when
2496 * inserting tuples. This only makes sense if we aren't already
2497 * setting the page frozen in the VM and we're not in bootstrap mode.
2498 */
2500 PageSetPrunable(page, xid);
2501
2502 MarkBufferDirty(buffer);
2503
2504 /* XLOG stuff */
2505 if (needwal)
2506 {
2510 char *tupledata;
2511 int totaldatalen;
2512 char *scratchptr = scratch.data;
2513 bool init;
2514 int bufflags = 0;
2515
2516 /*
2517 * If the page was previously empty, we can reinit the page
2518 * instead of restoring the whole thing.
2519 */
2521
2522 /* allocate xl_heap_multi_insert struct from the scratch area */
2525
2526 /*
2527 * Allocate offsets array. Unless we're reinitializing the page,
2528 * in that case the tuples are stored in order starting at
2529 * FirstOffsetNumber and we don't need to store the offsets
2530 * explicitly.
2531 */
2532 if (!init)
2533 scratchptr += nthispage * sizeof(OffsetNumber);
2534
2535 /* the rest of the scratch space is used for tuple data */
2536 tupledata = scratchptr;
2537
2538 /* check that the mutually exclusive flags are not both set */
2540
2541 xlrec->flags = 0;
2544
2545 /*
2546 * We don't have to worry about including a conflict xid in the
2547 * WAL record, as HEAP_INSERT_FROZEN intentionally violates
2548 * visibility rules.
2549 */
2550 if (all_frozen_set)
2552
2553 xlrec->ntuples = nthispage;
2554
2555 /*
2556 * Write out an xl_multi_insert_tuple and the tuple data itself
2557 * for each tuple.
2558 */
2559 for (i = 0; i < nthispage; i++)
2560 {
2562 xl_multi_insert_tuple *tuphdr;
2563 int datalen;
2564
2565 if (!init)
2566 xlrec->offsets[i] = ItemPointerGetOffsetNumber(&heaptup->t_self);
2567 /* xl_multi_insert_tuple needs two-byte alignment. */
2569 scratchptr = ((char *) tuphdr) + SizeOfMultiInsertTuple;
2570
2571 tuphdr->t_infomask2 = heaptup->t_data->t_infomask2;
2572 tuphdr->t_infomask = heaptup->t_data->t_infomask;
2573 tuphdr->t_hoff = heaptup->t_data->t_hoff;
2574
2575 /* write bitmap [+ padding] [+ oid] + data */
2576 datalen = heaptup->t_len - SizeofHeapTupleHeader;
2578 (char *) heaptup->t_data + SizeofHeapTupleHeader,
2579 datalen);
2580 tuphdr->datalen = datalen;
2581 scratchptr += datalen;
2582 }
2583 totaldatalen = scratchptr - tupledata;
2584 Assert((scratchptr - scratch.data) < BLCKSZ);
2585
2586 if (need_tuple_data)
2588
2589 /*
2590 * Signal that this is the last xl_heap_multi_insert record
2591 * emitted by this call to heap_multi_insert(). Needed for logical
2592 * decoding so it knows when to cleanup temporary data.
2593 */
2594 if (ndone + nthispage == ntuples)
2596
2597 if (init)
2598 {
2599 info |= XLOG_HEAP_INIT_PAGE;
2601 }
2602
2603 /*
2604 * If we're doing logical decoding, include the new tuple data
2605 * even if we take a full-page image of the page.
2606 */
2607 if (need_tuple_data)
2609
2611 XLogRegisterData(xlrec, tupledata - scratch.data);
2616
2618 totaldatalen);
2619
2620 /* filtering by origin on a row level is much more efficient */
2622
2623 recptr = XLogInsert(RM_HEAP2_ID, info);
2624
2625 PageSetLSN(page, recptr);
2627 {
2628 Assert(BufferIsDirty(vmbuffer));
2629 PageSetLSN(BufferGetPage(vmbuffer), recptr);
2630 }
2631 }
2632
2634
2635 /*
2636 * We locked vmbuffer if clear_all_visible was true regardless of
2637 * whether or not we ended up modifying the vmbuffer.
2638 */
2640 LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK);
2641
2642 UnlockReleaseBuffer(buffer);
2643 ndone += nthispage;
2644
2645 /*
2646 * NB: Only release vmbuffer after inserting all tuples - it's fairly
2647 * likely that we'll insert into subsequent heap pages that are likely
2648 * to use the same vm page.
2649 */
2650 }
2651
2652 /* We're done with inserting all tuples, so release the last vmbuffer. */
2653 if (vmbuffer != InvalidBuffer)
2654 ReleaseBuffer(vmbuffer);
2655
2656 /*
2657 * We're done with the actual inserts. Check for conflicts again, to
2658 * ensure that all rw-conflicts in to these inserts are detected. Without
2659 * this final check, a sequential scan of the heap may have locked the
2660 * table after the "before" check, missing one opportunity to detect the
2661 * conflict, and then scanned the table before the new tuples were there,
2662 * missing the other chance to detect the conflict.
2663 *
2664 * For heap inserts, we only need to check for table-level SSI locks. Our
2665 * new tuples can't possibly conflict with existing tuple locks, and heap
2666 * page locks are only consolidated versions of tuple locks; they do not
2667 * lock "gaps" as index page locks do. So we don't need to specify a
2668 * buffer when making the call.
2669 */
2671
2672 /*
2673 * If tuples are cacheable, mark them for invalidation from the caches in
2674 * case we abort. Note it is OK to do this after releasing the buffer,
2675 * because the heaptuples data structure is all in local memory, not in
2676 * the shared buffer.
2677 */
2678 if (IsCatalogRelation(relation))
2679 {
2680 for (i = 0; i < ntuples; i++)
2682 }
2683
2684 /* copy t_self fields back to the caller's slots */
2685 for (i = 0; i < ntuples; i++)
2686 slots[i]->tts_tid = heaptuples[i]->t_self;
2687
2688 pgstat_count_heap_insert(relation, ntuples);
2689}
2690
2691/*
2692 * simple_heap_insert - insert a tuple
2693 *
2694 * Currently, this routine differs from heap_insert only in supplying
2695 * a default command ID and not allowing access to the speedup options.
2696 *
2697 * This should be used rather than using heap_insert directly in most places
2698 * where we are modifying system catalogs.
2699 */
2700void
2702{
2703 heap_insert(relation, tup, GetCurrentCommandId(true), 0, NULL);
2704}
2705
2706/*
2707 * Given infomask/infomask2, compute the bits that must be saved in the
2708 * "infobits" field of xl_heap_delete, xl_heap_update, xl_heap_lock,
2709 * xl_heap_lock_updated WAL records.
2710 *
2711 * See fix_infomask_from_infobits.
2712 */
2713static uint8
2715{
2716 return
2720 /* note we ignore HEAP_XMAX_SHR_LOCK here */
2722 ((infomask2 & HEAP_KEYS_UPDATED) != 0 ?
2723 XLHL_KEYS_UPDATED : 0);
2724}
2725
2726/*
2727 * Given two versions of the same t_infomask for a tuple, compare them and
2728 * return whether the relevant status for a tuple Xmax has changed. This is
2729 * used after a buffer lock has been released and reacquired: we want to ensure
2730 * that the tuple state continues to be the same it was when we previously
2731 * examined it.
2732 *
2733 * Note the Xmax field itself must be compared separately.
2734 */
2735static inline bool
2737{
2738 const uint16 interesting =
2740
2741 if ((new_infomask & interesting) != (old_infomask & interesting))
2742 return true;
2743
2744 return false;
2745}
2746
2747/*
2748 * heap_delete - delete a tuple
2749 *
2750 * See table_tuple_delete() for an explanation of the parameters, except that
2751 * this routine directly takes a tuple rather than a slot.
2752 *
2753 * In the failure cases, the routine fills *tmfd with the tuple's t_ctid,
2754 * t_xmax (resolving a possible MultiXact, if necessary), and t_cmax (the last
2755 * only for TM_SelfModified, since we cannot obtain cmax from a combo CID
2756 * generated by another transaction).
2757 */
2761 bool wait, TM_FailureData *tmfd)
2762{
2765 ItemId lp;
2766 HeapTupleData tp;
2767 Page page;
2768 BlockNumber block;
2769 Buffer buffer;
2770 Buffer vmbuffer = InvalidBuffer;
2771 bool vmbuffer_modified = false;
2772 TransactionId new_xmax;
2777 bool have_tuple_lock = false;
2778 bool iscombo;
2779 bool clear_all_visible = false;
2780 HeapTuple old_key_tuple = NULL; /* replica identity of the tuple */
2781 bool old_key_copied = false;
2782
2784
2785 AssertHasSnapshotForToast(relation);
2786
2787 /*
2788 * Forbid this during a parallel operation, lest it allocate a combo CID.
2789 * Other workers might need that combo CID for visibility checks, and we
2790 * have no provision for broadcasting it to them.
2791 */
2792 if (IsInParallelMode())
2793 ereport(ERROR,
2795 errmsg("cannot delete tuples during a parallel operation")));
2796
2797 block = ItemPointerGetBlockNumber(tid);
2798 buffer = ReadBuffer(relation, block);
2799 page = BufferGetPage(buffer);
2800
2801 /*
2802 * Before locking the buffer, pin the visibility map page if it appears to
2803 * be necessary. Since we haven't got the lock yet, someone else might be
2804 * in the middle of changing this, so we'll need to recheck after we have
2805 * the lock.
2806 */
2807 if (PageIsAllVisible(page))
2808 visibilitymap_pin(relation, block, &vmbuffer);
2809
2811
2814
2815 tp.t_tableOid = RelationGetRelid(relation);
2816 tp.t_data = (HeapTupleHeader) PageGetItem(page, lp);
2817 tp.t_len = ItemIdGetLength(lp);
2818 tp.t_self = *tid;
2819
2820l1:
2821
2822 /*
2823 * If we didn't pin the visibility map page and the page has become all
2824 * visible while we were busy locking the buffer, we'll have to unlock and
2825 * re-lock, to avoid holding the buffer lock across an I/O. That's a bit
2826 * unfortunate, but hopefully shouldn't happen often.
2827 */
2828 if (vmbuffer == InvalidBuffer && PageIsAllVisible(page))
2829 {
2831 visibilitymap_pin(relation, block, &vmbuffer);
2833 }
2834
2835 result = HeapTupleSatisfiesUpdate(&tp, cid, buffer);
2836
2837 if (result == TM_Invisible)
2838 {
2839 UnlockReleaseBuffer(buffer);
2840 ereport(ERROR,
2842 errmsg("attempted to delete invisible tuple")));
2843 }
2844 else if (result == TM_BeingModified && wait)
2845 {
2848
2849 /* must copy state data before unlocking buffer */
2852
2853 /*
2854 * Sleep until concurrent transaction ends -- except when there's a
2855 * single locker and it's our own transaction. Note we don't care
2856 * which lock mode the locker has, because we need the strongest one.
2857 *
2858 * Before sleeping, we need to acquire tuple lock to establish our
2859 * priority for the tuple (see heap_lock_tuple). LockTuple will
2860 * release us when we are next-in-line for the tuple.
2861 *
2862 * If we are forced to "start over" below, we keep the tuple lock;
2863 * this arranges that we stay at the head of the line while rechecking
2864 * tuple state.
2865 */
2867 {
2868 bool current_is_member = false;
2869
2872 {
2874
2875 /*
2876 * Acquire the lock, if necessary (but skip it when we're
2877 * requesting a lock and already have one; avoids deadlock).
2878 */
2879 if (!current_is_member)
2882
2883 /* wait for multixact */
2885 relation, &(tp.t_self), XLTW_Delete,
2886 NULL);
2888
2889 /*
2890 * If xwait had just locked the tuple then some other xact
2891 * could update this tuple before we get to this point. Check
2892 * for xmax change, and start over if so.
2893 *
2894 * We also must start over if we didn't pin the VM page, and
2895 * the page has become all visible.
2896 */
2897 if ((vmbuffer == InvalidBuffer && PageIsAllVisible(page)) ||
2900 xwait))
2901 goto l1;
2902 }
2903
2904 /*
2905 * You might think the multixact is necessarily done here, but not
2906 * so: it could have surviving members, namely our own xact or
2907 * other subxacts of this backend. It is legal for us to delete
2908 * the tuple in either case, however (the latter case is
2909 * essentially a situation of upgrading our former shared lock to
2910 * exclusive). We don't bother changing the on-disk hint bits
2911 * since we are about to overwrite the xmax altogether.
2912 */
2913 }
2915 {
2916 /*
2917 * Wait for regular transaction to end; but first, acquire tuple
2918 * lock.
2919 */
2923 XactLockTableWait(xwait, relation, &(tp.t_self), XLTW_Delete);
2925
2926 /*
2927 * xwait is done, but if xwait had just locked the tuple then some
2928 * other xact could update this tuple before we get to this point.
2929 * Check for xmax change, and start over if so.
2930 *
2931 * We also must start over if we didn't pin the VM page, and the
2932 * page has become all visible.
2933 */
2934 if ((vmbuffer == InvalidBuffer && PageIsAllVisible(page)) ||
2937 xwait))
2938 goto l1;
2939
2940 /* Otherwise check if it committed or aborted */
2941 UpdateXmaxHintBits(tp.t_data, buffer, xwait);
2942 }
2943
2944 /*
2945 * We may overwrite if previous xmax aborted, or if it committed but
2946 * only locked the tuple without updating it.
2947 */
2948 if ((tp.t_data->t_infomask & HEAP_XMAX_INVALID) ||
2951 result = TM_Ok;
2952 else if (!ItemPointerEquals(&tp.t_self, &tp.t_data->t_ctid))
2954 else
2956 }
2957
2958 /* sanity check the result HeapTupleSatisfiesUpdate() and the logic above */
2959 if (result != TM_Ok)
2960 {
2962 result == TM_Updated ||
2963 result == TM_Deleted ||
2968 }
2969
2971 {
2972 /* Perform additional check for transaction-snapshot mode RI updates */
2973 if (!HeapTupleSatisfiesVisibility(&tp, crosscheck, buffer))
2975 }
2976
2977 if (result != TM_Ok)
2978 {
2979 tmfd->ctid = tp.t_data->t_ctid;
2981 if (result == TM_SelfModified)
2983 else
2984 tmfd->cmax = InvalidCommandId;
2985 UnlockReleaseBuffer(buffer);
2986 if (have_tuple_lock)
2988 if (vmbuffer != InvalidBuffer)
2989 ReleaseBuffer(vmbuffer);
2990 return result;
2991 }
2992
2993 /*
2994 * We're about to do the actual delete -- check for conflict first, to
2995 * avoid possibly having to roll back work we've just done.
2996 *
2997 * This is safe without a recheck as long as there is no possibility of
2998 * another process scanning the page between this check and the delete
2999 * being visible to the scan (i.e., an exclusive buffer content lock is
3000 * continuously held from this point until the tuple delete is visible).
3001 */
3003
3004 /* replace cid with a combo CID if necessary */
3006
3007 /*
3008 * Compute replica identity tuple before entering the critical section so
3009 * we don't PANIC upon a memory allocation failure.
3010 */
3012 ExtractReplicaIdentity(relation, &tp, true, &old_key_copied) : NULL;
3013
3014 /*
3015 * If this is the first possibly-multixact-able operation in the current
3016 * transaction, set my per-backend OldestMemberMXactId setting. We can be
3017 * certain that the transaction will never become a member of any older
3018 * MultiXactIds than that. (We have to do this even if we end up just
3019 * using our own TransactionId below, since some other backend could
3020 * incorporate our XID into a MultiXact immediately afterwards.)
3021 */
3023
3026 xid, LockTupleExclusive, true,
3027 &new_xmax, &new_infomask, &new_infomask2);
3028
3029 /* Lock the VM before entering the critical section */
3030 if (PageIsAllVisible(page))
3031 {
3032 clear_all_visible = true;
3034 }
3035
3037
3038 /*
3039 * If this transaction commits, the tuple will become DEAD sooner or
3040 * later. Set flag that this page is a candidate for pruning once our xid
3041 * falls below the OldestXmin horizon. If the transaction finally aborts,
3042 * the subsequent page pruning will be a no-op and the hint will be
3043 * cleared.
3044 */
3045 PageSetPrunable(page, xid);
3046
3048 {
3049 /* It's possible the VM bits were already clear */
3051 vmbuffer, VISIBILITYMAP_VALID_BITS))
3052 vmbuffer_modified = true;
3053
3054 PageClearAllVisible(page);
3055 }
3056
3057 /* store transaction information of xact deleting the tuple */
3063 HeapTupleHeaderSetXmax(tp.t_data, new_xmax);
3065 /* Make sure there is no forward chain link in t_ctid */
3066 tp.t_data->t_ctid = tp.t_self;
3067
3068 /* Signal that this is actually a move into another partition */
3069 if (changingPart)
3071
3072 MarkBufferDirty(buffer);
3073
3074 /*
3075 * XLOG stuff
3076 *
3077 * NB: heap_abort_speculative() uses the same xlog record and replay
3078 * routines.
3079 */
3080 if (RelationNeedsWAL(relation))
3081 {
3085
3086 /*
3087 * For logical decode we need combo CIDs to properly decode the
3088 * catalog
3089 */
3091 log_heap_new_cid(relation, &tp);
3092
3093 xlrec.flags = 0;
3096 if (changingPart)
3098 xlrec.infobits_set = compute_infobits(tp.t_data->t_infomask,
3099 tp.t_data->t_infomask2);
3101 xlrec.xmax = new_xmax;
3102
3103 if (old_key_tuple != NULL)
3104 {
3105 if (relation->rd_rel->relreplident == REPLICA_IDENTITY_FULL)
3107 else
3109 }
3110
3111 /*
3112 * Mark the change as not-for-logical-decoding if caller requested so.
3113 *
3114 * (This is used for changes that affect relations not visible to
3115 * other transactions, such as the transient table during concurrent
3116 * repack.)
3117 */
3118 if (!walLogical)
3120
3123
3125
3126 /*
3127 * Log replica identity of the deleted tuple if there is one
3128 */
3129 if (old_key_tuple != NULL)
3130 {
3131 xlhdr.t_infomask2 = old_key_tuple->t_data->t_infomask2;
3132 xlhdr.t_infomask = old_key_tuple->t_data->t_infomask;
3133 xlhdr.t_hoff = old_key_tuple->t_data->t_hoff;
3134
3136 XLogRegisterData((char *) old_key_tuple->t_data
3138 old_key_tuple->t_len
3140 }
3141
3142 /* filtering by origin on a row level is much more efficient */
3144
3147
3149
3150 PageSetLSN(page, recptr);
3151
3153 PageSetLSN(BufferGetPage(vmbuffer), recptr);
3154 }
3155
3157
3158 /*
3159 * Release VM lock first, since it covers many heap blocks. We locked
3160 * vmbuffer if clear_all_visible was true regardless of whether or not we
3161 * ended up modifying the vmbuffer.
3162 */
3164 LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK);
3165
3167
3168 if (vmbuffer != InvalidBuffer)
3169 ReleaseBuffer(vmbuffer);
3170
3171 /*
3172 * If the tuple has toasted out-of-line attributes, we need to delete
3173 * those items too. We have to do this before releasing the buffer
3174 * because we need to look at the contents of the tuple, but it's OK to
3175 * release the content lock on the buffer first.
3176 */
3177 if (relation->rd_rel->relkind != RELKIND_RELATION &&
3178 relation->rd_rel->relkind != RELKIND_MATVIEW)
3179 {
3180 /* toast table entries should never be recursively toasted */
3182 }
3183 else if (HeapTupleHasExternal(&tp))
3184 heap_toast_delete(relation, &tp, false);
3185
3186 /*
3187 * Mark tuple for invalidation from system caches at next command
3188 * boundary. We have to do this before releasing the buffer because we
3189 * need to look at the contents of the tuple.
3190 */
3191 CacheInvalidateHeapTuple(relation, &tp, NULL);
3192
3193 /* Now we can release the buffer */
3194 ReleaseBuffer(buffer);
3195
3196 /*
3197 * Release the lmgr tuple lock, if we had it.
3198 */
3199 if (have_tuple_lock)
3201
3202 pgstat_count_heap_delete(relation);
3203
3206
3207 return TM_Ok;
3208}
3209
3210/*
3211 * simple_heap_delete - delete a tuple
3212 *
3213 * This routine may be used to delete a tuple when concurrent updates of
3214 * the target tuple are not expected (for example, because we have a lock
3215 * on the relation associated with the tuple). Any failure is reported
3216 * via ereport().
3217 */
3218void
3220{
3222 TM_FailureData tmfd;
3223
3224 result = heap_delete(relation, tid,
3225 GetCurrentCommandId(true),
3226 0,
3228 true /* wait for commit */ ,
3229 &tmfd);
3230 switch (result)
3231 {
3232 case TM_SelfModified:
3233 /* Tuple was already updated in current command? */
3234 elog(ERROR, "tuple already updated by self");
3235 break;
3236
3237 case TM_Ok:
3238 /* done successfully */
3239 break;
3240
3241 case TM_Updated:
3242 elog(ERROR, "tuple concurrently updated");
3243 break;
3244
3245 case TM_Deleted:
3246 elog(ERROR, "tuple concurrently deleted");
3247 break;
3248
3249 default:
3250 elog(ERROR, "unrecognized heap_delete status: %u", result);
3251 break;
3252 }
3253}
3254
3255/*
3256 * heap_update - replace a tuple
3257 *
3258 * See table_tuple_update() for an explanation of the parameters, except that
3259 * this routine directly takes a tuple rather than a slot.
3260 *
3261 * In the failure cases, the routine fills *tmfd with the tuple's t_ctid,
3262 * t_xmax (resolving a possible MultiXact, if necessary), and t_cmax (the last
3263 * only for TM_SelfModified, since we cannot obtain cmax from a combo CID
3264 * generated by another transaction).
3265 */
3269 TM_FailureData *tmfd, LockTupleMode *lockmode,
3271{
3280 ItemId lp;
3284 bool old_key_copied = false;
3286 Page page,
3287 newpage;
3288 BlockNumber block;
3290 Buffer buffer,
3291 newbuf,
3292 vmbuffer = InvalidBuffer,
3294 bool unlock_vmbuffer = false;
3295 bool unlock_vmbuffer_new = false;
3296 bool need_toast;
3298 pagefree;
3299 bool have_tuple_lock = false;
3300 bool iscombo;
3301 bool use_hot_update = false;
3302 bool summarized_update = false;
3303 bool key_intact;
3304 bool clear_all_visible = false;
3305 bool clear_all_visible_new = false;
3306 bool vmbuffer_modified = false;
3307 bool vmbuffer_new_modified = false;
3308 bool checked_lockers;
3309 bool locker_remains;
3310 bool id_has_external = false;
3317
3319
3320 /* Cheap, simplistic check that the tuple matches the rel's rowtype. */
3323
3324 AssertHasSnapshotForToast(relation);
3325
3326 /*
3327 * Forbid this during a parallel operation, lest it allocate a combo CID.
3328 * Other workers might need that combo CID for visibility checks, and we
3329 * have no provision for broadcasting it to them.
3330 */
3331 if (IsInParallelMode())
3332 ereport(ERROR,
3334 errmsg("cannot update tuples during a parallel operation")));
3335
3336#ifdef USE_ASSERT_CHECKING
3338#endif
3339
3340 /*
3341 * Fetch the list of attributes to be checked for various operations.
3342 *
3343 * For HOT considerations, this is wasted effort if we fail to update or
3344 * have to put the new tuple on a different page. But we must compute the
3345 * list before obtaining buffer lock --- in the worst case, if we are
3346 * doing an update on one of the relevant system catalogs, we could
3347 * deadlock if we try to fetch the list later. In any case, the relcache
3348 * caches the data so this is usually pretty cheap.
3349 *
3350 * We also need columns used by the replica identity and columns that are
3351 * considered the "key" of rows in the table.
3352 *
3353 * Note that we get copies of each bitmap, so we need not worry about
3354 * relcache flush happening midway through.
3355 */
3368
3370 INJECTION_POINT("heap_update-before-pin", NULL);
3371 buffer = ReadBuffer(relation, block);
3372 page = BufferGetPage(buffer);
3373
3374 /*
3375 * Before locking the buffer, pin the visibility map page if it appears to
3376 * be necessary. Since we haven't got the lock yet, someone else might be
3377 * in the middle of changing this, so we'll need to recheck after we have
3378 * the lock.
3379 */
3380 if (PageIsAllVisible(page))
3381 visibilitymap_pin(relation, block, &vmbuffer);
3382
3384
3386
3387 /*
3388 * Usually, a buffer pin and/or snapshot blocks pruning of otid, ensuring
3389 * we see LP_NORMAL here. When the otid origin is a syscache, we may have
3390 * neither a pin nor a snapshot. Hence, we may see other LP_ states, each
3391 * of which indicates concurrent pruning.
3392 *
3393 * Failing with TM_Updated would be most accurate. However, unlike other
3394 * TM_Updated scenarios, we don't know the successor ctid in LP_UNUSED and
3395 * LP_DEAD cases. While the distinction between TM_Updated and TM_Deleted
3396 * does matter to SQL statements UPDATE and MERGE, those SQL statements
3397 * hold a snapshot that ensures LP_NORMAL. Hence, the choice between
3398 * TM_Updated and TM_Deleted affects only the wording of error messages.
3399 * Settle on TM_Deleted, for two reasons. First, it avoids complicating
3400 * the specification of when tmfd->ctid is valid. Second, it creates
3401 * error log evidence that we took this branch.
3402 *
3403 * Since it's possible to see LP_UNUSED at otid, it's also possible to see
3404 * LP_NORMAL for a tuple that replaced LP_UNUSED. If it's a tuple for an
3405 * unrelated row, we'll fail with "duplicate key value violates unique".
3406 * XXX if otid is the live, newer version of the newtup row, we'll discard
3407 * changes originating in versions of this catalog row after the version
3408 * the caller got from syscache. See syscache-update-pruned.spec.
3409 */
3410 if (!ItemIdIsNormal(lp))
3411 {
3413
3414 UnlockReleaseBuffer(buffer);
3416 if (vmbuffer != InvalidBuffer)
3417 ReleaseBuffer(vmbuffer);
3418 tmfd->ctid = *otid;
3419 tmfd->xmax = InvalidTransactionId;
3420 tmfd->cmax = InvalidCommandId;
3422
3427 /* modified_attrs not yet initialized */
3429 return TM_Deleted;
3430 }
3431
3432 /*
3433 * Fill in enough data in oldtup for HeapDetermineColumnsInfo to work
3434 * properly.
3435 */
3436 oldtup.t_tableOid = RelationGetRelid(relation);
3437 oldtup.t_data = (HeapTupleHeader) PageGetItem(page, lp);
3438 oldtup.t_len = ItemIdGetLength(lp);
3439 oldtup.t_self = *otid;
3440
3441 /* the new tuple is ready, except for this: */
3442 newtup->t_tableOid = RelationGetRelid(relation);
3443
3444 /*
3445 * Determine columns modified by the update. Additionally, identify
3446 * whether any of the unmodified replica identity key attributes in the
3447 * old tuple is externally stored or not. This is required because for
3448 * such attributes the flattened value won't be WAL logged as part of the
3449 * new tuple so we must include it as part of the old_key_tuple. See
3450 * ExtractReplicaIdentity.
3451 */
3453 id_attrs, &oldtup,
3455
3456 /*
3457 * If we're not updating any "key" column, we can grab a weaker lock type.
3458 * This allows for more concurrency when we are running simultaneously
3459 * with foreign key checks.
3460 *
3461 * Note that if a column gets detoasted while executing the update, but
3462 * the value ends up being the same, this test will fail and we will use
3463 * the stronger lock. This is acceptable; the important case to optimize
3464 * is updates that don't manipulate key columns, not those that
3465 * serendipitously arrive at the same key values.
3466 */
3468 {
3469 *lockmode = LockTupleNoKeyExclusive;
3471 key_intact = true;
3472
3473 /*
3474 * If this is the first possibly-multixact-able operation in the
3475 * current transaction, set my per-backend OldestMemberMXactId
3476 * setting. We can be certain that the transaction will never become a
3477 * member of any older MultiXactIds than that. (We have to do this
3478 * even if we end up just using our own TransactionId below, since
3479 * some other backend could incorporate our XID into a MultiXact
3480 * immediately afterwards.)
3481 */
3483 }
3484 else
3485 {
3486 *lockmode = LockTupleExclusive;
3488 key_intact = false;
3489 }
3490
3491 /*
3492 * Note: beyond this point, use oldtup not otid to refer to old tuple.
3493 * otid may very well point at newtup->t_self, which we will overwrite
3494 * with the new tuple's location, so there's great risk of confusion if we
3495 * use otid anymore.
3496 */
3497
3498l2:
3499 checked_lockers = false;
3500 locker_remains = false;
3502
3503 /* see below about the "no wait" case */
3504 Assert(result != TM_BeingModified || wait);
3505
3506 if (result == TM_Invisible)
3507 {
3508 UnlockReleaseBuffer(buffer);
3509 ereport(ERROR,
3511 errmsg("attempted to update invisible tuple")));
3512 }
3513 else if (result == TM_BeingModified && wait)
3514 {
3517 bool can_continue = false;
3518
3519 /*
3520 * XXX note that we don't consider the "no wait" case here. This
3521 * isn't a problem currently because no caller uses that case, but it
3522 * should be fixed if such a caller is introduced. It wasn't a
3523 * problem previously because this code would always wait, but now
3524 * that some tuple locks do not conflict with one of the lock modes we
3525 * use, it is possible that this case is interesting to handle
3526 * specially.
3527 *
3528 * This may cause failures with third-party code that calls
3529 * heap_update directly.
3530 */
3531
3532 /* must copy state data before unlocking buffer */
3534 infomask = oldtup.t_data->t_infomask;
3535
3536 /*
3537 * Now we have to do something about the existing locker. If it's a
3538 * multi, sleep on it; we might be awakened before it is completely
3539 * gone (or even not sleep at all in some cases); we need to preserve
3540 * it as locker, unless it is gone completely.
3541 *
3542 * If it's not a multi, we need to check for sleeping conditions
3543 * before actually going to sleep. If the update doesn't conflict
3544 * with the locks, we just continue without sleeping (but making sure
3545 * it is preserved).
3546 *
3547 * Before sleeping, we need to acquire tuple lock to establish our
3548 * priority for the tuple (see heap_lock_tuple). LockTuple will
3549 * release us when we are next-in-line for the tuple. Note we must
3550 * not acquire the tuple lock until we're sure we're going to sleep;
3551 * otherwise we're open for race conditions with other transactions
3552 * holding the tuple lock which sleep on us.
3553 *
3554 * If we are forced to "start over" below, we keep the tuple lock;
3555 * this arranges that we stay at the head of the line while rechecking
3556 * tuple state.
3557 */
3559 {
3561 int remain;
3562 bool current_is_member = false;
3563
3565 *lockmode, &current_is_member))
3566 {
3568
3569 /*
3570 * Acquire the lock, if necessary (but skip it when we're
3571 * requesting a lock and already have one; avoids deadlock).
3572 */
3573 if (!current_is_member)
3574 heap_acquire_tuplock(relation, &(oldtup.t_self), *lockmode,
3576
3577 /* wait for multixact */
3579 relation, &oldtup.t_self, XLTW_Update,
3580 &remain);
3581 checked_lockers = true;
3582 locker_remains = remain != 0;
3584
3585 /*
3586 * If xwait had just locked the tuple then some other xact
3587 * could update this tuple before we get to this point. Check
3588 * for xmax change, and start over if so.
3589 */
3590 if (xmax_infomask_changed(oldtup.t_data->t_infomask,
3591 infomask) ||
3593 xwait))
3594 goto l2;
3595 }
3596
3597 /*
3598 * Note that the multixact may not be done by now. It could have
3599 * surviving members; our own xact or other subxacts of this
3600 * backend, and also any other concurrent transaction that locked
3601 * the tuple with LockTupleKeyShare if we only got
3602 * LockTupleNoKeyExclusive. If this is the case, we have to be
3603 * careful to mark the updated tuple with the surviving members in
3604 * Xmax.
3605 *
3606 * Note that there could have been another update in the
3607 * MultiXact. In that case, we need to check whether it committed
3608 * or aborted. If it aborted we are safe to update it again;
3609 * otherwise there is an update conflict, and we have to return
3610 * TableTuple{Deleted, Updated} below.
3611 *
3612 * In the LockTupleExclusive case, we still need to preserve the
3613 * surviving members: those would include the tuple locks we had
3614 * before this one, which are important to keep in case this
3615 * subxact aborts.
3616 */
3617 if (!HEAP_XMAX_IS_LOCKED_ONLY(oldtup.t_data->t_infomask))
3619 else
3621
3622 /*
3623 * There was no UPDATE in the MultiXact; or it aborted. No
3624 * TransactionIdIsInProgress() call needed here, since we called
3625 * MultiXactIdWait() above.
3626 */
3629 can_continue = true;
3630 }
3632 {
3633 /*
3634 * The only locker is ourselves; we can avoid grabbing the tuple
3635 * lock here, but must preserve our locking information.
3636 */
3637 checked_lockers = true;
3638 locker_remains = true;
3639 can_continue = true;
3640 }
3642 {
3643 /*
3644 * If it's just a key-share locker, and we're not changing the key
3645 * columns, we don't need to wait for it to end; but we need to
3646 * preserve it as locker.
3647 */
3648 checked_lockers = true;
3649 locker_remains = true;
3650 can_continue = true;
3651 }
3652 else
3653 {
3654 /*
3655 * Wait for regular transaction to end; but first, acquire tuple
3656 * lock.
3657 */
3659 heap_acquire_tuplock(relation, &(oldtup.t_self), *lockmode,
3661 XactLockTableWait(xwait, relation, &oldtup.t_self,
3662 XLTW_Update);
3663 checked_lockers = true;
3665
3666 /*
3667 * xwait is done, but if xwait had just locked the tuple then some
3668 * other xact could update this tuple before we get to this point.
3669 * Check for xmax change, and start over if so.
3670 */
3671 if (xmax_infomask_changed(oldtup.t_data->t_infomask, infomask) ||
3674 goto l2;
3675
3676 /* Otherwise check if it committed or aborted */
3677 UpdateXmaxHintBits(oldtup.t_data, buffer, xwait);
3678 if (oldtup.t_data->t_infomask & HEAP_XMAX_INVALID)
3679 can_continue = true;
3680 }
3681
3682 if (can_continue)
3683 result = TM_Ok;
3684 else if (!ItemPointerEquals(&oldtup.t_self, &oldtup.t_data->t_ctid))
3686 else
3688 }
3689
3690 /* Sanity check the result HeapTupleSatisfiesUpdate() and the logic above */
3691 if (result != TM_Ok)
3692 {
3694 result == TM_Updated ||
3695 result == TM_Deleted ||
3697 Assert(!(oldtup.t_data->t_infomask & HEAP_XMAX_INVALID));
3699 !ItemPointerEquals(&oldtup.t_self, &oldtup.t_data->t_ctid));
3700 }
3701
3703 {
3704 /* Perform additional check for transaction-snapshot mode RI updates */
3707 }
3708
3709 if (result != TM_Ok)
3710 {
3711 tmfd->ctid = oldtup.t_data->t_ctid;
3712 tmfd->xmax = HeapTupleHeaderGetUpdateXid(oldtup.t_data);
3713 if (result == TM_SelfModified)
3714 tmfd->cmax = HeapTupleHeaderGetCmax(oldtup.t_data);
3715 else
3716 tmfd->cmax = InvalidCommandId;
3717 UnlockReleaseBuffer(buffer);
3718 if (have_tuple_lock)
3719 UnlockTupleTuplock(relation, &(oldtup.t_self), *lockmode);
3720 if (vmbuffer != InvalidBuffer)
3721 ReleaseBuffer(vmbuffer);
3723
3730 return result;
3731 }
3732
3733 /*
3734 * If we didn't pin the visibility map page and the page has become all
3735 * visible while we were busy locking the buffer, or during some
3736 * subsequent window during which we had it unlocked, we'll have to unlock
3737 * and re-lock, to avoid holding the buffer lock across an I/O. That's a
3738 * bit unfortunate, especially since we'll now have to recheck whether the
3739 * tuple has been locked or updated under us, but hopefully it won't
3740 * happen very often.
3741 */
3742 if (vmbuffer == InvalidBuffer && PageIsAllVisible(page))
3743 {
3745 visibilitymap_pin(relation, block, &vmbuffer);
3747 goto l2;
3748 }
3749
3750 /* Fill in transaction status data */
3751
3752 /*
3753 * If the tuple we're updating is locked, we need to preserve the locking
3754 * info in the old tuple's Xmax. Prepare a new Xmax value for this.
3755 */
3757 oldtup.t_data->t_infomask,
3758 oldtup.t_data->t_infomask2,
3759 xid, *lockmode, true,
3762
3763 /*
3764 * And also prepare an Xmax value for the new copy of the tuple. If there
3765 * was no xmax previously, or there was one but all lockers are now gone,
3766 * then use InvalidTransactionId; otherwise, get the xmax from the old
3767 * tuple. (In rare cases that might also be InvalidTransactionId and yet
3768 * not have the HEAP_XMAX_INVALID bit set; that's fine.)
3769 */
3770 if ((oldtup.t_data->t_infomask & HEAP_XMAX_INVALID) ||
3771 HEAP_LOCKED_UPGRADED(oldtup.t_data->t_infomask) ||
3774 else
3776
3778 {
3781 }
3782 else
3783 {
3784 /*
3785 * If we found a valid Xmax for the new tuple, then the infomask bits
3786 * to use on the new tuple depend on what was there on the old one.
3787 * Note that since we're doing an update, the only possibility is that
3788 * the lockers had FOR KEY SHARE lock.
3789 */
3790 if (oldtup.t_data->t_infomask & HEAP_XMAX_IS_MULTI)
3791 {
3794 }
3795 else
3796 {
3799 }
3800 }
3801
3802 /*
3803 * Prepare the new tuple with the appropriate initial values of Xmin and
3804 * Xmax, as well as initial infomask bits as computed above.
3805 */
3806 newtup->t_data->t_infomask &= ~(HEAP_XACT_MASK);
3807 newtup->t_data->t_infomask2 &= ~(HEAP2_XACT_MASK);
3808 HeapTupleHeaderSetXmin(newtup->t_data, xid);
3810 newtup->t_data->t_infomask |= HEAP_UPDATED | infomask_new_tuple;
3811 newtup->t_data->t_infomask2 |= infomask2_new_tuple;
3813
3814 /*
3815 * Replace cid with a combo CID if necessary. Note that we already put
3816 * the plain cid into the new tuple.
3817 */
3819
3820 /*
3821 * If the toaster needs to be activated, OR if the new tuple will not fit
3822 * on the same page as the old, then we need to release the content lock
3823 * (but not the pin!) on the old tuple's buffer while we are off doing
3824 * TOAST and/or table-file-extension work. We must mark the old tuple to
3825 * show that it's locked, else other processes may try to update it
3826 * themselves.
3827 *
3828 * We need to invoke the toaster if there are already any out-of-line
3829 * toasted values present, or if the new tuple is over-threshold.
3830 */
3831 if (relation->rd_rel->relkind != RELKIND_RELATION &&
3832 relation->rd_rel->relkind != RELKIND_MATVIEW)
3833 {
3834 /* toast table entries should never be recursively toasted */
3837 need_toast = false;
3838 }
3839 else
3842 newtup->t_len > TOAST_TUPLE_THRESHOLD);
3843
3845
3846 newtupsize = MAXALIGN(newtup->t_len);
3847
3849 {
3853 bool cleared_all_frozen = false;
3854
3855 /*
3856 * To prevent concurrent sessions from updating the tuple, we have to
3857 * temporarily mark it locked, while we release the page-level lock.
3858 *
3859 * To satisfy the rule that any xid potentially appearing in a buffer
3860 * written out to disk, we unfortunately have to WAL log this
3861 * temporary modification. We can reuse xl_heap_lock for this
3862 * purpose. If we crash/error before following through with the
3863 * actual update, xmax will be of an aborted transaction, allowing
3864 * other sessions to proceed.
3865 */
3866
3867 /*
3868 * Compute xmax / infomask appropriate for locking the tuple. This has
3869 * to be done separately from the combo that's going to be used for
3870 * updating, because the potentially created multixact would otherwise
3871 * be wrong.
3872 */
3874 oldtup.t_data->t_infomask,
3875 oldtup.t_data->t_infomask2,
3876 xid, *lockmode, false,
3879
3881
3882 if (PageIsAllVisible(page))
3883 {
3885 unlock_vmbuffer = true;
3886 }
3887
3889
3890 /* Clear obsolete visibility flags ... */
3891 oldtup.t_data->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED);
3892 oldtup.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
3894 /* ... and store info about transaction updating this tuple */
3897 oldtup.t_data->t_infomask |= infomask_lock_old_tuple;
3898 oldtup.t_data->t_infomask2 |= infomask2_lock_old_tuple;
3900
3901 /* temporarily make it look not-updated, but locked */
3902 oldtup.t_data->t_ctid = oldtup.t_self;
3903
3904 /*
3905 * Clear all-frozen bit on visibility map if needed. We could
3906 * immediately reset ALL_VISIBLE, but given that the WAL logging
3907 * overhead would be unchanged, that doesn't seem necessarily
3908 * worthwhile.
3909 */
3910 if (PageIsAllVisible(page))
3911 {
3912 /* It's possible all-frozen was already clear */
3913 if (visibilitymap_clear(relation->rd_locator, block, vmbuffer,
3915 cleared_all_frozen = true;
3916 }
3917
3918 MarkBufferDirty(buffer);
3919
3920 if (RelationNeedsWAL(relation))
3921 {
3924
3927
3928 xlrec.offnum = ItemPointerGetOffsetNumber(&oldtup.t_self);
3930 xlrec.infobits_set = compute_infobits(oldtup.t_data->t_infomask,
3931 oldtup.t_data->t_infomask2);
3932 xlrec.flags =
3935
3938
3940 PageSetLSN(page, recptr);
3941
3943 PageSetLSN(BufferGetPage(vmbuffer), recptr);
3944 }
3945
3947
3948 /* release VM lock first, since it covers many heap blocks */
3949 if (unlock_vmbuffer)
3950 LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK);
3951 unlock_vmbuffer = false;
3952
3954
3955 /*
3956 * Let the toaster do its thing, if needed.
3957 *
3958 * Note: below this point, heaptup is the data we actually intend to
3959 * store into the relation; newtup is the caller's original untoasted
3960 * data.
3961 */
3962 if (need_toast)
3963 {
3964 /* Note we always use WAL and FSM during updates */
3966 newtupsize = MAXALIGN(heaptup->t_len);
3967 }
3968 else
3969 heaptup = newtup;
3970
3971 /*
3972 * Now, do we need a new page for the tuple, or not? This is a bit
3973 * tricky since someone else could have added tuples to the page while
3974 * we weren't looking. We have to recheck the available space after
3975 * reacquiring the buffer lock. But don't bother to do that if the
3976 * former amount of free space is still not enough; it's unlikely
3977 * there's more free now than before.
3978 *
3979 * What's more, if we need to get a new page, we will need to acquire
3980 * buffer locks on both old and new pages. To avoid deadlock against
3981 * some other backend trying to get the same two locks in the other
3982 * order, we must be consistent about the order we get the locks in.
3983 * We use the rule "lock the lower-numbered page of the relation
3984 * first". To implement this, we must do RelationGetBufferForTuple
3985 * while not holding the lock on the old page, and we must rely on it
3986 * to get the locks on both pages in the correct order.
3987 *
3988 * Another consideration is that we need visibility map page pin(s) if
3989 * we will have to clear the all-visible flag on either page. If we
3990 * call RelationGetBufferForTuple, we rely on it to acquire any such
3991 * pins; but if we don't, we have to handle that here. Hence we need
3992 * a loop.
3993 */
3994 for (;;)
3995 {
3996 if (newtupsize > pagefree)
3997 {
3998 /* It doesn't fit, must use RelationGetBufferForTuple. */
3999 newbuf = RelationGetBufferForTuple(relation, heaptup->t_len,
4000 buffer, 0, NULL,
4001 &vmbuffer_new, &vmbuffer,
4002 0);
4003 /* We're all done. */
4004 break;
4005 }
4006 /* Acquire VM page pin if needed and we don't have it. */
4007 if (vmbuffer == InvalidBuffer && PageIsAllVisible(page))
4008 visibilitymap_pin(relation, block, &vmbuffer);
4009 /* Re-acquire the lock on the old tuple's page. */
4011 /* Re-check using the up-to-date free space */
4013 if (newtupsize > pagefree ||
4014 (vmbuffer == InvalidBuffer && PageIsAllVisible(page)))
4015 {
4016 /*
4017 * Rats, it doesn't fit anymore, or somebody just now set the
4018 * all-visible flag. We must now unlock and loop to avoid
4019 * deadlock. Fortunately, this path should seldom be taken.
4020 */
4022 }
4023 else
4024 {
4025 /* We're all done. */
4026 newbuf = buffer;
4027 break;
4028 }
4029 }
4030 }
4031 else
4032 {
4033 /* No TOAST work needed, and it'll fit on same page */
4034 newbuf = buffer;
4035 heaptup = newtup;
4036 }
4037
4039
4040 /*
4041 * We're about to do the actual update -- check for conflict first, to
4042 * avoid possibly having to roll back work we've just done.
4043 *
4044 * This is safe without a recheck as long as there is no possibility of
4045 * another process scanning the pages between this check and the update
4046 * being visible to the scan (i.e., exclusive buffer content lock(s) are
4047 * continuously held from this point until the tuple update is visible).
4048 *
4049 * For the new tuple the only check needed is at the relation level, but
4050 * since both tuples are in the same relation and the check for oldtup
4051 * will include checking the relation level, there is no benefit to a
4052 * separate check for the new tuple.
4053 */
4054 CheckForSerializableConflictIn(relation, &oldtup.t_self,
4055 BufferGetBlockNumber(buffer));
4056
4057 /*
4058 * At this point newbuf and buffer are both pinned and locked, and newbuf
4059 * has enough space for the new tuple. If they are the same buffer, only
4060 * one pin is held.
4061 */
4062
4063 if (newbuf == buffer)
4064 {
4065 /*
4066 * Since the new tuple is going into the same page, we might be able
4067 * to do a HOT update. Check if any of the index columns have been
4068 * changed.
4069 */
4071 {
4072 use_hot_update = true;
4073
4074 /*
4075 * If none of the columns that are used in hot-blocking indexes
4076 * were updated, we can apply HOT, but we do still need to check
4077 * if we need to update the summarizing indexes, and update those
4078 * indexes if the columns were updated, or we may fail to detect
4079 * e.g. value bound changes in BRIN minmax indexes.
4080 */
4082 summarized_update = true;
4083 }
4084 }
4085 else
4086 {
4087 /* Set a hint that the old page could use prune/defrag */
4088 PageSetFull(page);
4089 }
4090
4091 /*
4092 * Compute replica identity tuple before entering the critical section so
4093 * we don't PANIC upon a memory allocation failure.
4094 * ExtractReplicaIdentity() will return NULL if nothing needs to be
4095 * logged. Pass old key required as true only if the replica identity key
4096 * columns are modified or it has external data.
4097 */
4102
4105
4106 /*
4107 * Clear PD_ALL_VISIBLE flags and reset visibility map bits for any heap
4108 * pages that were all-visible. If there are two heap pages, we may need
4109 * to clear VM bits for both.
4110 */
4112 vmbuffer_new == vmbuffer)
4113 {
4114 /*
4115 * This is the more complicated case: both the new and old heap pages
4116 * are all-visible and both their VM bits are on the same page of the
4117 * VM, so we register a single VM buffer as HEAP_UPDATE_BLKREF_VM_NEW
4118 * in the WAL record. We must be careful to only lock and register one
4119 * buffer, even though we modify it twice -- once for each heap
4120 * block's VM bits.
4121 */
4123 unlock_vmbuffer_new = true;
4124
4125 /* We will not lock or attempt to modify old VM buffer */
4126 }
4127 else
4128 {
4129 /*
4130 * In all the remaining cases, we will clear at most one heap block's
4131 * VM bits per VM page.
4132 */
4133 Buffer vmbuffers[2] = {
4134 clear_all_visible ? vmbuffer : InvalidBuffer,
4136 };
4137
4138 /*
4139 * When both pages need different VM pages cleared, acquire the VM
4140 * buffer locks in VM block order to avoid deadlocks between backends
4141 * updating tuples in opposite directions across VM pages.
4142 */
4145 {
4146 Buffer swap = vmbuffers[0];
4147
4148 vmbuffers[0] = vmbuffers[1];
4149 vmbuffers[1] = swap;
4150 }
4151
4153 vmbuffers[0] != vmbuffers[1]);
4154
4155 if (BufferIsValid(vmbuffers[0]))
4157 if (BufferIsValid(vmbuffers[1]))
4159
4161 unlock_vmbuffer = true;
4163 unlock_vmbuffer_new = true;
4164 }
4165
4166 /* NO EREPORT(ERROR) from here till changes are logged */
4168
4169 /*
4170 * If this transaction commits, the old tuple will become DEAD sooner or
4171 * later. Set flag that this page is a candidate for pruning once our xid
4172 * falls below the OldestXmin horizon. If the transaction finally aborts,
4173 * the subsequent page pruning will be a no-op and the hint will be
4174 * cleared.
4175 *
4176 * We set the new page prunable as well. See heap_insert() for more on why
4177 * we do this when inserting tuples.
4178 */
4179 PageSetPrunable(page, xid);
4180 if (newbuf != buffer)
4182
4183 if (use_hot_update)
4184 {
4185 /* Mark the old tuple as HOT-updated */
4187 /* And mark the new tuple as heap-only */
4189 /* Mark the caller's copy too, in case different from heaptup */
4191 }
4192 else
4193 {
4194 /* Make sure tuples are correctly marked as not-HOT */
4198 }
4199
4200 RelationPutHeapTuple(relation, newbuf, heaptup, false); /* insert new tuple */
4201
4202
4203 /* Clear obsolete visibility flags, possibly set by ourselves above... */
4204 oldtup.t_data->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED);
4205 oldtup.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
4206 /* ... and store info about transaction updating this tuple */
4209 oldtup.t_data->t_infomask |= infomask_old_tuple;
4210 oldtup.t_data->t_infomask2 |= infomask2_old_tuple;
4212
4213 /* record address of new tuple in t_ctid of old one */
4214 oldtup.t_data->t_ctid = heaptup->t_self;
4215
4216 /*
4217 * Clear PD_ALL_VISIBLE flags and reset all visibilitymap bits. In all
4218 * cases, it's possible that PD_ALL_VISIBLE was set but the corresponding
4219 * visibility map bits were already clear.
4220 */
4222 {
4223 if (visibilitymap_clear(relation->rd_locator, block,
4224 vmbuffer, VISIBILITYMAP_VALID_BITS))
4225 {
4226 /*
4227 * When old and new heap blocks' VM bits are on the same VM page,
4228 * that page is registered in the WAL record only once. If both
4229 * heap pages were PD_ALL_VISIBLE and either VM bit needs
4230 * clearing, we register the VM buffer as
4231 * HEAP_UPDATE_BLKREF_VM_NEW.
4232 */
4233 if (clear_all_visible_new && vmbuffer == vmbuffer_new)
4234 vmbuffer_new_modified = true;
4235 else
4236 vmbuffer_modified = true;
4237 }
4238
4239 PageClearAllVisible(page);
4240 }
4242 {
4243 /*
4244 * If both heap blocks' VM bits are on the same VM buffer, this will
4245 * clear the new heap block's VM bits from the shared vmbuffer.
4246 */
4249 vmbuffer_new_modified = true;
4250
4252 }
4253
4254 if (newbuf != buffer)
4256 MarkBufferDirty(buffer);
4257
4258 /* XLOG stuff */
4259 if (RelationNeedsWAL(relation))
4260 {
4262
4263 /*
4264 * For logical decoding we need combo CIDs to properly decode the
4265 * catalog.
4266 */
4268 {
4269 log_heap_new_cid(relation, &oldtup);
4270 log_heap_new_cid(relation, heaptup);
4271 }
4272
4273 recptr = log_heap_update(relation, buffer,
4274 vmbuffer_modified ? vmbuffer : InvalidBuffer,
4275 newbuf,
4277 &oldtup, heaptup,
4281 walLogical);
4282 if (newbuf != buffer)
4283 {
4285 }
4286 PageSetLSN(page, recptr);
4287
4289 PageSetLSN(BufferGetPage(vmbuffer), recptr);
4292 }
4293
4295
4296 if (unlock_vmbuffer)
4297 LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK);
4300
4301 if (newbuf != buffer)
4304
4305 /*
4306 * Mark old tuple for invalidation from system caches at next command
4307 * boundary, and mark the new tuple for invalidation in case we abort. We
4308 * have to do this before releasing the buffer because oldtup is in the
4309 * buffer. (heaptup is all in local memory, but it's necessary to process
4310 * both tuple versions in one call to inval.c so we can avoid redundant
4311 * sinval messages.)
4312 */
4314
4315 /* Now we can release the buffer(s) */
4316 if (newbuf != buffer)
4318 ReleaseBuffer(buffer);
4321 if (BufferIsValid(vmbuffer))
4322 ReleaseBuffer(vmbuffer);
4323
4324 /*
4325 * Release the lmgr tuple lock, if we had it.
4326 */
4327 if (have_tuple_lock)
4328 UnlockTupleTuplock(relation, &(oldtup.t_self), *lockmode);
4329
4330 pgstat_count_heap_update(relation, use_hot_update, newbuf != buffer);
4331
4332 /*
4333 * If heaptup is a private copy, release it. Don't forget to copy t_self
4334 * back to the caller's image, too.
4335 */
4336 if (heaptup != newtup)
4337 {
4338 newtup->t_self = heaptup->t_self;
4340 }
4341
4342 /*
4343 * If it is a HOT update, the update may still need to update summarized
4344 * indexes, lest we fail to update those summaries and get incorrect
4345 * results (for example, minmax bounds of the block may change with this
4346 * update).
4347 */
4348 if (use_hot_update)
4349 {
4352 else
4354 }
4355 else
4357
4360
4367
4368 return TM_Ok;
4369}
4370
4371#ifdef USE_ASSERT_CHECKING
4372/*
4373 * Confirm adequate lock held during heap_update(), per rules from
4374 * README.tuplock section "Locking to write inplace-updated tables".
4375 */
4376static void
4378 const ItemPointerData *otid,
4380{
4381 /* LOCKTAG_TUPLE acceptable for any catalog */
4382 switch (RelationGetRelid(relation))
4383 {
4384 case RelationRelationId:
4385 case DatabaseRelationId:
4386 {
4388
4390 relation->rd_lockInfo.lockRelId.dbId,
4391 relation->rd_lockInfo.lockRelId.relId,
4395 return;
4396 }
4397 break;
4398 default:
4399 Assert(!IsInplaceUpdateRelation(relation));
4400 return;
4401 }
4402
4403 switch (RelationGetRelid(relation))
4404 {
4405 case RelationRelationId:
4406 {
4407 /* LOCKTAG_TUPLE or LOCKTAG_RELATION ok */
4409 Oid relid = classForm->oid;
4410 Oid dbid;
4411 LOCKTAG tag;
4412
4413 if (IsSharedRelation(relid))
4414 dbid = InvalidOid;
4415 else
4416 dbid = MyDatabaseId;
4417
4418 if (classForm->relkind == RELKIND_INDEX)
4419 {
4420 Relation irel = index_open(relid, AccessShareLock);
4421
4422 SET_LOCKTAG_RELATION(tag, dbid, irel->rd_index->indrelid);
4424 }
4425 else
4426 SET_LOCKTAG_RELATION(tag, dbid, relid);
4427
4428 if (!LockHeldByMe(&tag, ShareUpdateExclusiveLock, false) &&
4429 !LockHeldByMe(&tag, ShareRowExclusiveLock, true))
4430 elog(WARNING,
4431 "missing lock for relation \"%s\" (OID %u, relkind %c) @ TID (%u,%u)",
4432 NameStr(classForm->relname),
4433 relid,
4434 classForm->relkind,
4437 }
4438 break;
4439 case DatabaseRelationId:
4440 {
4441 /* LOCKTAG_TUPLE required */
4443
4444 elog(WARNING,
4445 "missing lock on database \"%s\" (OID %u) @ TID (%u,%u)",
4446 NameStr(dbForm->datname),
4447 dbForm->oid,
4450 }
4451 break;
4452 }
4453}
4454
4455/*
4456 * Confirm adequate relation lock held, per rules from README.tuplock section
4457 * "Locking to write inplace-updated tables".
4458 */
4459static void
4461{
4463 Oid relid = classForm->oid;
4464 Oid dbid;
4465 LOCKTAG tag;
4466
4467 if (IsSharedRelation(relid))
4468 dbid = InvalidOid;
4469 else
4470 dbid = MyDatabaseId;
4471
4472 if (classForm->relkind == RELKIND_INDEX)
4473 {
4474 Relation irel = index_open(relid, AccessShareLock);
4475
4476 SET_LOCKTAG_RELATION(tag, dbid, irel->rd_index->indrelid);
4478 }
4479 else
4480 SET_LOCKTAG_RELATION(tag, dbid, relid);
4481
4482 if (!LockHeldByMe(&tag, ShareUpdateExclusiveLock, true))
4483 elog(WARNING,
4484 "missing lock for relation \"%s\" (OID %u, relkind %c) @ TID (%u,%u)",
4485 NameStr(classForm->relname),
4486 relid,
4487 classForm->relkind,
4490}
4491#endif
4492
4493/*
4494 * Check if the specified attribute's values are the same. Subroutine for
4495 * HeapDetermineColumnsInfo.
4496 */
4497static bool
4499 bool isnull1, bool isnull2)
4500{
4501 /*
4502 * If one value is NULL and other is not, then they are certainly not
4503 * equal
4504 */
4505 if (isnull1 != isnull2)
4506 return false;
4507
4508 /*
4509 * If both are NULL, they can be considered equal.
4510 */
4511 if (isnull1)
4512 return true;
4513
4514 /*
4515 * We do simple binary comparison of the two datums. This may be overly
4516 * strict because there can be multiple binary representations for the
4517 * same logical value. But we should be OK as long as there are no false
4518 * positives. Using a type-specific equality operator is messy because
4519 * there could be multiple notions of equality in different operator
4520 * classes; furthermore, we cannot safely invoke user-defined functions
4521 * while holding exclusive buffer lock.
4522 */
4523 if (attrnum <= 0)
4524 {
4525 /* The only allowed system columns are OIDs, so do this */
4527 }
4528 else
4529 {
4530 CompactAttribute *att;
4531
4533 att = TupleDescCompactAttr(tupdesc, attrnum - 1);
4534 return datumIsEqual(value1, value2, att->attbyval, att->attlen);
4535 }
4536}
4537
4538/*
4539 * Check which columns are being updated.
4540 *
4541 * Given an updated tuple, determine (and return into the output bitmapset),
4542 * from those listed as interesting, the set of columns that changed.
4543 *
4544 * has_external indicates if any of the unmodified attributes (from those
4545 * listed as interesting) of the old tuple is a member of external_cols and is
4546 * stored externally.
4547 */
4548static Bitmapset *
4553 bool *has_external)
4554{
4555 int attidx;
4557 TupleDesc tupdesc = RelationGetDescr(relation);
4558
4559 attidx = -1;
4560 while ((attidx = bms_next_member(interesting_cols, attidx)) >= 0)
4561 {
4562 /* attidx is zero-based, attrnum is the normal attribute number */
4564 Datum value1,
4565 value2;
4566 bool isnull1,
4567 isnull2;
4568
4569 /*
4570 * If it's a whole-tuple reference, say "not equal". It's not really
4571 * worth supporting this case, since it could only succeed after a
4572 * no-op update, which is hardly a case worth optimizing for.
4573 */
4574 if (attrnum == 0)
4575 {
4576 modified = bms_add_member(modified, attidx);
4577 continue;
4578 }
4579
4580 /*
4581 * Likewise, automatically say "not equal" for any system attribute
4582 * other than tableOID; we cannot expect these to be consistent in a
4583 * HOT chain, or even to be set correctly yet in the new tuple.
4584 */
4585 if (attrnum < 0)
4586 {
4587 if (attrnum != TableOidAttributeNumber)
4588 {
4589 modified = bms_add_member(modified, attidx);
4590 continue;
4591 }
4592 }
4593
4594 /*
4595 * Extract the corresponding values. XXX this is pretty inefficient
4596 * if there are many indexed columns. Should we do a single
4597 * heap_deform_tuple call on each tuple, instead? But that doesn't
4598 * work for system columns ...
4599 */
4600 value1 = heap_getattr(oldtup, attrnum, tupdesc, &isnull1);
4601 value2 = heap_getattr(newtup, attrnum, tupdesc, &isnull2);
4602
4603 if (!heap_attr_equals(tupdesc, attrnum, value1,
4604 value2, isnull1, isnull2))
4605 {
4606 modified = bms_add_member(modified, attidx);
4607 continue;
4608 }
4609
4610 /*
4611 * No need to check attributes that can't be stored externally. Note
4612 * that system attributes can't be stored externally.
4613 */
4614 if (attrnum < 0 || isnull1 ||
4615 TupleDescCompactAttr(tupdesc, attrnum - 1)->attlen != -1)
4616 continue;
4617
4618 /*
4619 * Check if the old tuple's attribute is stored externally and is a
4620 * member of external_cols.
4621 */
4624 *has_external = true;
4625 }
4626
4627 return modified;
4628}
4629
4630/*
4631 * simple_heap_update - replace a tuple
4632 *
4633 * This routine may be used to update a tuple when concurrent updates of
4634 * the target tuple are not expected (for example, because we have a lock
4635 * on the relation associated with the tuple). Any failure is reported
4636 * via ereport().
4637 */
4638void
4641{
4643 TM_FailureData tmfd;
4644 LockTupleMode lockmode;
4645
4646 result = heap_update(relation, otid, tup,
4647 GetCurrentCommandId(true), 0,
4649 true /* wait for commit */ ,
4650 &tmfd, &lockmode, update_indexes);
4651 switch (result)
4652 {
4653 case TM_SelfModified:
4654 /* Tuple was already updated in current command? */
4655 elog(ERROR, "tuple already updated by self");
4656 break;
4657
4658 case TM_Ok:
4659 /* done successfully */
4660 break;
4661
4662 case TM_Updated:
4663 elog(ERROR, "tuple concurrently updated");
4664 break;
4665
4666 case TM_Deleted:
4667 elog(ERROR, "tuple concurrently deleted");
4668 break;
4669
4670 default:
4671 elog(ERROR, "unrecognized heap_update status: %u", result);
4672 break;
4673 }
4674}
4675
4676
4677/*
4678 * Return the MultiXactStatus corresponding to the given tuple lock mode.
4679 */
4680static MultiXactStatus
4682{
4683 int retval;
4684
4685 if (is_update)
4686 retval = tupleLockExtraInfo[mode].updstatus;
4687 else
4688 retval = tupleLockExtraInfo[mode].lockstatus;
4689
4690 if (retval == -1)
4691 elog(ERROR, "invalid lock tuple mode %d/%s", mode,
4692 is_update ? "true" : "false");
4693
4694 return (MultiXactStatus) retval;
4695}
4696
4697/*
4698 * heap_lock_tuple - lock a tuple in shared or exclusive mode
4699 *
4700 * Note that this acquires a buffer pin, which the caller must release.
4701 *
4702 * Input parameters:
4703 * relation: relation containing tuple (caller must hold suitable lock)
4704 * cid: current command ID (used for visibility test, and stored into
4705 * tuple's cmax if lock is successful)
4706 * mode: indicates if shared or exclusive tuple lock is desired
4707 * wait_policy: what to do if tuple lock is not available
4708 * follow_updates: if true, follow the update chain to also lock descendant
4709 * tuples.
4710 *
4711 * Output parameters:
4712 * *tuple: all fields filled in
4713 * *buffer: set to buffer holding tuple (pinned but not locked at exit)
4714 * *tmfd: filled in failure cases (see below)
4715 *
4716 * Function results are the same as the ones for table_tuple_lock().
4717 *
4718 * In the failure cases other than TM_Invisible, the routine fills
4719 * *tmfd with the tuple's t_ctid, t_xmax (resolving a possible MultiXact,
4720 * if necessary), and t_cmax (the last only for TM_SelfModified,
4721 * since we cannot obtain cmax from a combo CID generated by another
4722 * transaction).
4723 * See comments for struct TM_FailureData for additional info.
4724 *
4725 * See README.tuplock for a thorough explanation of this mechanism.
4726 */
4730 bool follow_updates,
4731 Buffer *buffer, TM_FailureData *tmfd)
4732{
4734 ItemPointer tid = &(tuple->t_self);
4735 ItemId lp;
4736 Page page;
4737 Buffer vmbuffer = InvalidBuffer;
4738 bool unlock_vmbuffer = false;
4739 BlockNumber block;
4740 TransactionId xid,
4741 xmax;
4745 bool first_time = true;
4746 bool skip_tuple_lock = false;
4747 bool have_tuple_lock = false;
4748 bool cleared_all_frozen = false;
4749
4750 *buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
4751 block = ItemPointerGetBlockNumber(tid);
4752 page = BufferGetPage(*buffer);
4753
4754 /*
4755 * Before locking the buffer, pin the visibility map page if it appears to
4756 * be necessary. Since we haven't got the lock yet, someone else might be
4757 * in the middle of changing this, so we'll need to recheck after we have
4758 * the lock.
4759 */
4760 if (PageIsAllVisible(page))
4761 visibilitymap_pin(relation, block, &vmbuffer);
4762
4764
4767
4768 tuple->t_data = (HeapTupleHeader) PageGetItem(page, lp);
4769 tuple->t_len = ItemIdGetLength(lp);
4770 tuple->t_tableOid = RelationGetRelid(relation);
4771
4772l3:
4773 result = HeapTupleSatisfiesUpdate(tuple, cid, *buffer);
4774
4775 if (result == TM_Invisible)
4776 {
4777 /*
4778 * This is possible, but only when locking a tuple for ON CONFLICT DO
4779 * SELECT/UPDATE. We return this value here rather than throwing an
4780 * error in order to give that case the opportunity to throw a more
4781 * specific error.
4782 */
4784 goto out_locked;
4785 }
4786 else if (result == TM_BeingModified ||
4787 result == TM_Updated ||
4788 result == TM_Deleted)
4789 {
4793 bool require_sleep;
4794 ItemPointerData t_ctid;
4795
4796 /* must copy state data before unlocking buffer */
4798 infomask = tuple->t_data->t_infomask;
4799 infomask2 = tuple->t_data->t_infomask2;
4800 ItemPointerCopy(&tuple->t_data->t_ctid, &t_ctid);
4801
4803
4804 /*
4805 * If any subtransaction of the current top transaction already holds
4806 * a lock as strong as or stronger than what we're requesting, we
4807 * effectively hold the desired lock already. We *must* succeed
4808 * without trying to take the tuple lock, else we will deadlock
4809 * against anyone wanting to acquire a stronger lock.
4810 *
4811 * Note we only do this the first time we loop on the HTSU result;
4812 * there is no point in testing in subsequent passes, because
4813 * evidently our own transaction cannot have acquired a new lock after
4814 * the first time we checked.
4815 */
4816 if (first_time)
4817 {
4818 first_time = false;
4819
4821 {
4822 int i;
4823 int nmembers;
4824 MultiXactMember *members;
4825
4826 /*
4827 * We don't need to allow old multixacts here; if that had
4828 * been the case, HeapTupleSatisfiesUpdate would have returned
4829 * MayBeUpdated and we wouldn't be here.
4830 */
4831 nmembers =
4832 GetMultiXactIdMembers(xwait, &members, false,
4834
4835 for (i = 0; i < nmembers; i++)
4836 {
4837 /* only consider members of our own transaction */
4838 if (!TransactionIdIsCurrentTransactionId(members[i].xid))
4839 continue;
4840
4841 if (TUPLOCK_from_mxstatus(members[i].status) >= mode)
4842 {
4843 pfree(members);
4844 result = TM_Ok;
4845 goto out_unlocked;
4846 }
4847 else
4848 {
4849 /*
4850 * Disable acquisition of the heavyweight tuple lock.
4851 * Otherwise, when promoting a weaker lock, we might
4852 * deadlock with another locker that has acquired the
4853 * heavyweight tuple lock and is waiting for our
4854 * transaction to finish.
4855 *
4856 * Note that in this case we still need to wait for
4857 * the multixact if required, to avoid acquiring
4858 * conflicting locks.
4859 */
4860 skip_tuple_lock = true;
4861 }
4862 }
4863
4864 if (members)
4865 pfree(members);
4866 }
4868 {
4869 switch (mode)
4870 {
4871 case LockTupleKeyShare:
4875 result = TM_Ok;
4876 goto out_unlocked;
4877 case LockTupleShare:
4880 {
4881 result = TM_Ok;
4882 goto out_unlocked;
4883 }
4884 break;
4887 {
4888 result = TM_Ok;
4889 goto out_unlocked;
4890 }
4891 break;
4892 case LockTupleExclusive:
4895 {
4896 result = TM_Ok;
4897 goto out_unlocked;
4898 }
4899 break;
4900 }
4901 }
4902 }
4903
4904 /*
4905 * Initially assume that we will have to wait for the locking
4906 * transaction(s) to finish. We check various cases below in which
4907 * this can be turned off.
4908 */
4909 require_sleep = true;
4910 if (mode == LockTupleKeyShare)
4911 {
4912 /*
4913 * If we're requesting KeyShare, and there's no update present, we
4914 * don't need to wait. Even if there is an update, we can still
4915 * continue if the key hasn't been modified.
4916 *
4917 * However, if there are updates, we need to walk the update chain
4918 * to mark future versions of the row as locked, too. That way,
4919 * if somebody deletes that future version, we're protected
4920 * against the key going away. This locking of future versions
4921 * could block momentarily, if a concurrent transaction is
4922 * deleting a key; or it could return a value to the effect that
4923 * the transaction deleting the key has already committed. So we
4924 * do this before re-locking the buffer; otherwise this would be
4925 * prone to deadlocks.
4926 *
4927 * Note that the TID we're locking was grabbed before we unlocked
4928 * the buffer. For it to change while we're not looking, the
4929 * other properties we're testing for below after re-locking the
4930 * buffer would also change, in which case we would restart this
4931 * loop above.
4932 */
4934 {
4935 bool updated;
4936
4938
4939 /*
4940 * If there are updates, follow the update chain; bail out if
4941 * that cannot be done.
4942 */
4943 if (follow_updates && updated &&
4944 !ItemPointerEquals(&tuple->t_self, &t_ctid))
4945 {
4946 TM_Result res;
4947
4948 res = heap_lock_updated_tuple(relation,
4949 infomask, xwait, &t_ctid,
4951 mode);
4952 if (res != TM_Ok)
4953 {
4954 result = res;
4955 /* recovery code expects to have buffer lock held */
4957 goto failed;
4958 }
4959 }
4960
4962
4963 /*
4964 * Make sure it's still an appropriate lock, else start over.
4965 * Also, if it wasn't updated before we released the lock, but
4966 * is updated now, we start over too; the reason is that we
4967 * now need to follow the update chain to lock the new
4968 * versions.
4969 */
4970 if (!HeapTupleHeaderIsOnlyLocked(tuple->t_data) &&
4971 ((tuple->t_data->t_infomask2 & HEAP_KEYS_UPDATED) ||
4972 !updated))
4973 goto l3;
4974
4975 /* Things look okay, so we can skip sleeping */
4976 require_sleep = false;
4977
4978 /*
4979 * Note we allow Xmax to change here; other updaters/lockers
4980 * could have modified it before we grabbed the buffer lock.
4981 * However, this is not a problem, because with the recheck we
4982 * just did we ensure that they still don't conflict with the
4983 * lock we want.
4984 */
4985 }
4986 }
4987 else if (mode == LockTupleShare)
4988 {
4989 /*
4990 * If we're requesting Share, we can similarly avoid sleeping if
4991 * there's no update and no exclusive lock present.
4992 */
4995 {
4997
4998 /*
4999 * Make sure it's still an appropriate lock, else start over.
5000 * See above about allowing xmax to change.
5001 */
5004 goto l3;
5005 require_sleep = false;
5006 }
5007 }
5008 else if (mode == LockTupleNoKeyExclusive)
5009 {
5010 /*
5011 * If we're requesting NoKeyExclusive, we might also be able to
5012 * avoid sleeping; just ensure that there no conflicting lock
5013 * already acquired.
5014 */
5016 {
5018 mode, NULL))
5019 {
5020 /*
5021 * No conflict, but if the xmax changed under us in the
5022 * meantime, start over.
5023 */
5027 xwait))
5028 goto l3;
5029
5030 /* otherwise, we're good */
5031 require_sleep = false;
5032 }
5033 }
5035 {
5037
5038 /* if the xmax changed in the meantime, start over */
5041 xwait))
5042 goto l3;
5043 /* otherwise, we're good */
5044 require_sleep = false;
5045 }
5046 }
5047
5048 /*
5049 * As a check independent from those above, we can also avoid sleeping
5050 * if the current transaction is the sole locker of the tuple. Note
5051 * that the strength of the lock already held is irrelevant; this is
5052 * not about recording the lock in Xmax (which will be done regardless
5053 * of this optimization, below). Also, note that the cases where we
5054 * hold a lock stronger than we are requesting are already handled
5055 * above by not doing anything.
5056 *
5057 * Note we only deal with the non-multixact case here; MultiXactIdWait
5058 * is well equipped to deal with this situation on its own.
5059 */
5062 {
5063 /* ... but if the xmax changed in the meantime, start over */
5067 xwait))
5068 goto l3;
5070 require_sleep = false;
5071 }
5072
5073 /*
5074 * Time to sleep on the other transaction/multixact, if necessary.
5075 *
5076 * If the other transaction is an update/delete that's already
5077 * committed, then sleeping cannot possibly do any good: if we're
5078 * required to sleep, get out to raise an error instead.
5079 *
5080 * By here, we either have already acquired the buffer exclusive lock,
5081 * or we must wait for the locking transaction or multixact; so below
5082 * we ensure that we grab buffer lock after the sleep.
5083 */
5085 {
5087 goto failed;
5088 }
5089 else if (require_sleep)
5090 {
5091 /*
5092 * Acquire tuple lock to establish our priority for the tuple, or
5093 * die trying. LockTuple will release us when we are next-in-line
5094 * for the tuple. We must do this even if we are share-locking,
5095 * but not if we already have a weaker lock on the tuple.
5096 *
5097 * If we are forced to "start over" below, we keep the tuple lock;
5098 * this arranges that we stay at the head of the line while
5099 * rechecking tuple state.
5100 */
5101 if (!skip_tuple_lock &&
5102 !heap_acquire_tuplock(relation, tid, mode, wait_policy,
5104 {
5105 /*
5106 * This can only happen if wait_policy is Skip and the lock
5107 * couldn't be obtained.
5108 */
5110 /* recovery code expects to have buffer lock held */
5112 goto failed;
5113 }
5114
5116 {
5118
5119 /* We only ever lock tuples, never update them */
5120 if (status >= MultiXactStatusNoKeyUpdate)
5121 elog(ERROR, "invalid lock mode in heap_lock_tuple");
5122
5123 /* wait for multixact to end, or die trying */
5124 switch (wait_policy)
5125 {
5126 case LockWaitBlock:
5128 relation, &tuple->t_self, XLTW_Lock, NULL);
5129 break;
5130 case LockWaitSkip:
5132 status, infomask, relation,
5133 NULL, false))
5134 {
5136 /* recovery code expects to have buffer lock held */
5138 goto failed;
5139 }
5140 break;
5141 case LockWaitError:
5143 status, infomask, relation,
5145 ereport(ERROR,
5147 errmsg("could not obtain lock on row in relation \"%s\"",
5148 RelationGetRelationName(relation))));
5149
5150 break;
5151 }
5152
5153 /*
5154 * Of course, the multixact might not be done here: if we're
5155 * requesting a light lock mode, other transactions with light
5156 * locks could still be alive, as well as locks owned by our
5157 * own xact or other subxacts of this backend. We need to
5158 * preserve the surviving MultiXact members. Note that it
5159 * isn't absolutely necessary in the latter case, but doing so
5160 * is simpler.
5161 */
5162 }
5163 else
5164 {
5165 /* wait for regular transaction to end, or die trying */
5166 switch (wait_policy)
5167 {
5168 case LockWaitBlock:
5169 XactLockTableWait(xwait, relation, &tuple->t_self,
5170 XLTW_Lock);
5171 break;
5172 case LockWaitSkip:
5174 {
5176 /* recovery code expects to have buffer lock held */
5178 goto failed;
5179 }
5180 break;
5181 case LockWaitError:
5183 ereport(ERROR,
5185 errmsg("could not obtain lock on row in relation \"%s\"",
5186 RelationGetRelationName(relation))));
5187 break;
5188 }
5189 }
5190
5191 /* if there are updates, follow the update chain */
5193 !ItemPointerEquals(&tuple->t_self, &t_ctid))
5194 {
5195 TM_Result res;
5196
5197 res = heap_lock_updated_tuple(relation,
5198 infomask, xwait, &t_ctid,
5200 mode);
5201 if (res != TM_Ok)
5202 {
5203 result = res;
5204 /* recovery code expects to have buffer lock held */
5206 goto failed;
5207 }
5208 }
5209
5211
5212 /*
5213 * xwait is done, but if xwait had just locked the tuple then some
5214 * other xact could update this tuple before we get to this point.
5215 * Check for xmax change, and start over if so.
5216 */
5219 xwait))
5220 goto l3;
5221
5223 {
5224 /*
5225 * Otherwise check if it committed or aborted. Note we cannot
5226 * be here if the tuple was only locked by somebody who didn't
5227 * conflict with us; that would have been handled above. So
5228 * that transaction must necessarily be gone by now. But
5229 * don't check for this in the multixact case, because some
5230 * locker transactions might still be running.
5231 */
5232 UpdateXmaxHintBits(tuple->t_data, *buffer, xwait);
5233 }
5234 }
5235
5236 /* By here, we're certain that we hold buffer exclusive lock again */
5237
5238 /*
5239 * We may lock if previous xmax aborted, or if it committed but only
5240 * locked the tuple without updating it; or if we didn't have to wait
5241 * at all for whatever reason.
5242 */
5243 if (!require_sleep ||
5244 (tuple->t_data->t_infomask & HEAP_XMAX_INVALID) ||
5247 result = TM_Ok;
5248 else if (!ItemPointerEquals(&tuple->t_self, &tuple->t_data->t_ctid))
5250 else
5252 }
5253
5254failed:
5255 if (result != TM_Ok)
5256 {
5259
5260 /*
5261 * When locking a tuple under LockWaitSkip semantics and we fail with
5262 * TM_WouldBlock above, it's possible for concurrent transactions to
5263 * release the lock and set HEAP_XMAX_INVALID in the meantime. So
5264 * this assert is slightly different from the equivalent one in
5265 * heap_delete and heap_update.
5266 */
5268 !(tuple->t_data->t_infomask & HEAP_XMAX_INVALID));
5270 !ItemPointerEquals(&tuple->t_self, &tuple->t_data->t_ctid));
5271 tmfd->ctid = tuple->t_data->t_ctid;
5272 tmfd->xmax = HeapTupleHeaderGetUpdateXid(tuple->t_data);
5273 if (result == TM_SelfModified)
5274 tmfd->cmax = HeapTupleHeaderGetCmax(tuple->t_data);
5275 else
5276 tmfd->cmax = InvalidCommandId;
5277 goto out_locked;
5278 }
5279
5280 /*
5281 * If we didn't pin the visibility map page and the page has become all
5282 * visible while we were busy locking the buffer, or during some
5283 * subsequent window during which we had it unlocked, we'll have to unlock
5284 * and re-lock, to avoid holding the buffer lock across I/O. That's a bit
5285 * unfortunate, especially since we'll now have to recheck whether the
5286 * tuple has been locked or updated under us, but hopefully it won't
5287 * happen very often.
5288 */
5289 if (vmbuffer == InvalidBuffer && PageIsAllVisible(page))
5290 {
5292 visibilitymap_pin(relation, block, &vmbuffer);
5294 goto l3;
5295 }
5296
5297 xmax = HeapTupleHeaderGetRawXmax(tuple->t_data);
5298 old_infomask = tuple->t_data->t_infomask;
5299
5300 /*
5301 * If this is the first possibly-multixact-able operation in the current
5302 * transaction, set my per-backend OldestMemberMXactId setting. We can be
5303 * certain that the transaction will never become a member of any older
5304 * MultiXactIds than that. (We have to do this even if we end up just
5305 * using our own TransactionId below, since some other backend could
5306 * incorporate our XID into a MultiXact immediately afterwards.)
5307 */
5309
5310 /*
5311 * Compute the new xmax and infomask to store into the tuple. Note we do
5312 * not modify the tuple just yet, because that would leave it in the wrong
5313 * state if multixact.c elogs.
5314 */
5316 GetCurrentTransactionId(), mode, false,
5317 &xid, &new_infomask, &new_infomask2);
5318
5319 /* Lock VM buffer before entering critical section */
5320 if (PageIsAllVisible(page))
5321 {
5323 unlock_vmbuffer = true;
5324 }
5325
5327
5328 /*
5329 * Store transaction information of xact locking the tuple.
5330 *
5331 * Note: Cmax is meaningless in this context, so don't set it; this avoids
5332 * possibly generating a useless combo CID. Moreover, if we're locking a
5333 * previously updated tuple, it's important to preserve the Cmax.
5334 *
5335 * Also reset the HOT UPDATE bit, but only if there's no update; otherwise
5336 * we would break the HOT chain.
5337 */
5340 tuple->t_data->t_infomask |= new_infomask;
5341 tuple->t_data->t_infomask2 |= new_infomask2;
5344 HeapTupleHeaderSetXmax(tuple->t_data, xid);
5345
5346 /*
5347 * Make sure there is no forward chain link in t_ctid. Note that in the
5348 * cases where the tuple has been updated, we must not overwrite t_ctid,
5349 * because it was set by the updater. Moreover, if the tuple has been
5350 * updated, we need to follow the update chain to lock the new versions of
5351 * the tuple as well.
5352 */
5354 tuple->t_data->t_ctid = *tid;
5355
5356 /* Clear only the all-frozen bit on visibility map if needed */
5357 if (PageIsAllVisible(page))
5358 {
5359 if (visibilitymap_clear(relation->rd_locator, block, vmbuffer,
5361 cleared_all_frozen = true;
5362 }
5363
5364
5365 MarkBufferDirty(*buffer);
5366
5367 /*
5368 * XLOG stuff. You might think that we don't need an XLOG record because
5369 * there is no state change worth restoring after a crash. You would be
5370 * wrong however: we have just written either a TransactionId or a
5371 * MultiXactId that may never have been seen on disk before, and we need
5372 * to make sure that there are XLOG entries covering those ID numbers.
5373 * Else the same IDs might be re-used after a crash, which would be
5374 * disastrous if this page made it to disk before the crash. Essentially
5375 * we have to enforce the WAL log-before-data rule even in this case.
5376 * (Also, in a PITR log-shipping or 2PC environment, we have to have XLOG
5377 * entries for everything anyway.)
5378 */
5379 if (RelationNeedsWAL(relation))
5380 {
5383
5386
5387 xlrec.offnum = ItemPointerGetOffsetNumber(&tuple->t_self);
5388 xlrec.xmax = xid;
5389 xlrec.infobits_set = compute_infobits(new_infomask,
5390 tuple->t_data->t_infomask2);
5393
5396
5397 /* we don't decode row locks atm, so no need to log the origin */
5398
5400
5401 PageSetLSN(page, recptr);
5402
5404 PageSetLSN(BufferGetPage(vmbuffer), recptr);
5405 }
5406
5408
5409 /* release VM lock first, since it covers many heap blocks */
5410 if (unlock_vmbuffer)
5411 {
5412 LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK);
5413 unlock_vmbuffer = false;
5414 }
5415
5416 result = TM_Ok;
5417
5421
5423 if (BufferIsValid(vmbuffer))
5424 ReleaseBuffer(vmbuffer);
5425
5426 /*
5427 * Don't update the visibility map here. Locking a tuple doesn't change
5428 * visibility info.
5429 */
5430
5431 /*
5432 * Now that we have successfully marked the tuple as locked, we can
5433 * release the lmgr tuple lock, if we had it.
5434 */
5435 if (have_tuple_lock)
5436 UnlockTupleTuplock(relation, tid, mode);
5437
5438 return result;
5439}
5440
5441/*
5442 * Acquire heavyweight lock on the given tuple, in preparation for acquiring
5443 * its normal, Xmax-based tuple lock.
5444 *
5445 * have_tuple_lock is an input and output parameter: on input, it indicates
5446 * whether the lock has previously been acquired (and this function does
5447 * nothing in that case). If this function returns success, have_tuple_lock
5448 * has been flipped to true.
5449 *
5450 * Returns false if it was unable to obtain the lock; this can only happen if
5451 * wait_policy is Skip.
5452 */
5453static bool
5456{
5457 if (*have_tuple_lock)
5458 return true;
5459
5460 switch (wait_policy)
5461 {
5462 case LockWaitBlock:
5463 LockTupleTuplock(relation, tid, mode);
5464 break;
5465
5466 case LockWaitSkip:
5467 if (!ConditionalLockTupleTuplock(relation, tid, mode, false))
5468 return false;
5469 break;
5470
5471 case LockWaitError:
5473 ereport(ERROR,
5475 errmsg("could not obtain lock on row in relation \"%s\"",
5476 RelationGetRelationName(relation))));
5477 break;
5478 }
5479 *have_tuple_lock = true;
5480
5481 return true;
5482}
5483
5484/*
5485 * Given an original set of Xmax and infomask, and a transaction (identified by
5486 * add_to_xmax) acquiring a new lock of some mode, compute the new Xmax and
5487 * corresponding infomasks to use on the tuple.
5488 *
5489 * Note that this might have side effects such as creating a new MultiXactId.
5490 *
5491 * Most callers will have called HeapTupleSatisfiesUpdate before this function;
5492 * that will have set the HEAP_XMAX_INVALID bit if the xmax was a MultiXactId
5493 * but it was not running anymore. There is a race condition, which is that the
5494 * MultiXactId may have finished since then, but that uncommon case is handled
5495 * either here, or within MultiXactIdExpand.
5496 *
5497 * There is a similar race condition possible when the old xmax was a regular
5498 * TransactionId. We test TransactionIdIsInProgress again just to narrow the
5499 * window, but it's still possible to end up creating an unnecessary
5500 * MultiXactId. Fortunately this is harmless.
5501 */
5502static void
5508{
5509 TransactionId new_xmax;
5512
5514
5515l5:
5516 new_infomask = 0;
5517 new_infomask2 = 0;
5519 {
5520 /*
5521 * No previous locker; we just insert our own TransactionId.
5522 *
5523 * Note that it's critical that this case be the first one checked,
5524 * because there are several blocks below that come back to this one
5525 * to implement certain optimizations; old_infomask might contain
5526 * other dirty bits in those cases, but we don't really care.
5527 */
5528 if (is_update)
5529 {
5530 new_xmax = add_to_xmax;
5531 if (mode == LockTupleExclusive)
5533 }
5534 else
5535 {
5537 switch (mode)
5538 {
5539 case LockTupleKeyShare:
5540 new_xmax = add_to_xmax;
5542 break;
5543 case LockTupleShare:
5544 new_xmax = add_to_xmax;
5546 break;
5548 new_xmax = add_to_xmax;
5550 break;
5551 case LockTupleExclusive:
5552 new_xmax = add_to_xmax;
5555 break;
5556 default:
5557 new_xmax = InvalidTransactionId; /* silence compiler */
5558 elog(ERROR, "invalid lock mode");
5559 }
5560 }
5561 }
5563 {
5565
5566 /*
5567 * Currently we don't allow XMAX_COMMITTED to be set for multis, so
5568 * cross-check.
5569 */
5571
5572 /*
5573 * A multixact together with LOCK_ONLY set but neither lock bit set
5574 * (i.e. a pg_upgraded share locked tuple) cannot possibly be running
5575 * anymore. This check is critical for databases upgraded by
5576 * pg_upgrade; both MultiXactIdIsRunning and MultiXactIdExpand assume
5577 * that such multis are never passed.
5578 */
5580 {
5583 goto l5;
5584 }
5585
5586 /*
5587 * If the XMAX is already a MultiXactId, then we need to expand it to
5588 * include add_to_xmax; but if all the members were lockers and are
5589 * all gone, we can do away with the IS_MULTI bit and just set
5590 * add_to_xmax as the only locker/updater. If all lockers are gone
5591 * and we have an updater that aborted, we can also do without a
5592 * multi.
5593 *
5594 * The cost of doing GetMultiXactIdMembers would be paid by
5595 * MultiXactIdExpand if we weren't to do this, so this check is not
5596 * incurring extra work anyhow.
5597 */
5599 {
5602 old_infomask)))
5603 {
5604 /*
5605 * Reset these bits and restart; otherwise fall through to
5606 * create a new multi below.
5607 */
5610 goto l5;
5611 }
5612 }
5613
5615
5616 new_xmax = MultiXactIdExpand((MultiXactId) xmax, add_to_xmax,
5617 new_status);
5619 }
5621 {
5622 /*
5623 * It's a committed update, so we need to preserve him as updater of
5624 * the tuple.
5625 */
5626 MultiXactStatus status;
5628
5630 status = MultiXactStatusUpdate;
5631 else
5633
5635
5636 /*
5637 * since it's not running, it's obviously impossible for the old
5638 * updater to be identical to the current one, so we need not check
5639 * for that case as we do in the block above.
5640 */
5641 new_xmax = MultiXactIdCreate(xmax, status, add_to_xmax, new_status);
5643 }
5644 else if (TransactionIdIsInProgress(xmax))
5645 {
5646 /*
5647 * If the XMAX is a valid, in-progress TransactionId, then we need to
5648 * create a new MultiXactId that includes both the old locker or
5649 * updater and our own TransactionId.
5650 */
5654
5656 {
5662 {
5665 else
5667 }
5668 else
5669 {
5670 /*
5671 * LOCK_ONLY can be present alone only when a page has been
5672 * upgraded by pg_upgrade. But in that case,
5673 * TransactionIdIsInProgress() should have returned false. We
5674 * assume it's no longer locked in this case.
5675 */
5676 elog(WARNING, "LOCK_ONLY found for Xid in progress %u", xmax);
5679 goto l5;
5680 }
5681 }
5682 else
5683 {
5684 /* it's an update, but which kind? */
5687 else
5689 }
5690
5692
5693 /*
5694 * If the lock to be acquired is for the same TransactionId as the
5695 * existing lock, there's an optimization possible: consider only the
5696 * strongest of both locks as the only one present, and restart.
5697 */
5698 if (xmax == add_to_xmax)
5699 {
5700 /*
5701 * Note that it's not possible for the original tuple to be
5702 * updated: we wouldn't be here because the tuple would have been
5703 * invisible and we wouldn't try to update it. As a subtlety,
5704 * this code can also run when traversing an update chain to lock
5705 * future versions of a tuple. But we wouldn't be here either,
5706 * because the add_to_xmax would be different from the original
5707 * updater.
5708 */
5710
5711 /* acquire the strongest of both */
5712 if (mode < old_mode)
5713 mode = old_mode;
5714 /* mustn't touch is_update */
5715
5717 goto l5;
5718 }
5719
5720 /* otherwise, just fall back to creating a new multixact */
5722 new_xmax = MultiXactIdCreate(xmax, old_status,
5725 }
5728 {
5729 /*
5730 * It's a committed update, so we gotta preserve him as updater of the
5731 * tuple.
5732 */
5733 MultiXactStatus status;
5735
5737 status = MultiXactStatusUpdate;
5738 else
5740
5742
5743 /*
5744 * since it's not running, it's obviously impossible for the old
5745 * updater to be identical to the current one, so we need not check
5746 * for that case as we do in the block above.
5747 */
5748 new_xmax = MultiXactIdCreate(xmax, status, add_to_xmax, new_status);
5750 }
5751 else
5752 {
5753 /*
5754 * Can get here iff the locking/updating transaction was running when
5755 * the infomask was extracted from the tuple, but finished before
5756 * TransactionIdIsInProgress got to run. Deal with it as if there was
5757 * no locker at all in the first place.
5758 */
5760 goto l5;
5761 }
5762
5765 *result_xmax = new_xmax;
5766}
5767
5768/*
5769 * Subroutine for heap_lock_updated_tuple_rec.
5770 *
5771 * Given a hypothetical multixact status held by the transaction identified
5772 * with the given xid, does the current transaction need to wait, fail, or can
5773 * it continue if it wanted to acquire a lock of the given mode? "needwait"
5774 * is set to true if waiting is necessary; if it can continue, then TM_Ok is
5775 * returned. If the lock is already held by the current transaction, return
5776 * TM_SelfModified. In case of a conflict with another transaction, a
5777 * different HeapTupleSatisfiesUpdate return code is returned.
5778 *
5779 * The held status is said to be hypothetical because it might correspond to a
5780 * lock held by a single Xid, i.e. not a real MultiXactId; we express it this
5781 * way for simplicity of API.
5782 */
5783static TM_Result
5786 bool *needwait)
5787{
5789
5790 *needwait = false;
5792
5793 /*
5794 * Note: we *must* check TransactionIdIsInProgress before
5795 * TransactionIdDidAbort/Commit; see comment at top of heapam_visibility.c
5796 * for an explanation.
5797 */
5799 {
5800 /*
5801 * The tuple has already been locked by our own transaction. This is
5802 * very rare but can happen if multiple transactions are trying to
5803 * lock an ancient version of the same tuple.
5804 */
5805 return TM_SelfModified;
5806 }
5807 else if (TransactionIdIsInProgress(xid))
5808 {
5809 /*
5810 * If the locking transaction is running, what we do depends on
5811 * whether the lock modes conflict: if they do, then we must wait for
5812 * it to finish; otherwise we can fall through to lock this tuple
5813 * version without waiting.
5814 */
5817 {
5818 *needwait = true;
5819 }
5820
5821 /*
5822 * If we set needwait above, then this value doesn't matter;
5823 * otherwise, this value signals to caller that it's okay to proceed.
5824 */
5825 return TM_Ok;
5826 }
5827 else if (TransactionIdDidAbort(xid))
5828 return TM_Ok;
5829 else if (TransactionIdDidCommit(xid))
5830 {
5831 /*
5832 * The other transaction committed. If it was only a locker, then the
5833 * lock is completely gone now and we can return success; but if it
5834 * was an update, then what we do depends on whether the two lock
5835 * modes conflict. If they conflict, then we must report error to
5836 * caller. But if they don't, we can fall through to allow the current
5837 * transaction to lock the tuple.
5838 *
5839 * Note: the reason we worry about ISUPDATE here is because as soon as
5840 * a transaction ends, all its locks are gone and meaningless, and
5841 * thus we can ignore them; whereas its updates persist. In the
5842 * TransactionIdIsInProgress case, above, we don't need to check
5843 * because we know the lock is still "alive" and thus a conflict needs
5844 * always be checked.
5845 */
5846 if (!ISUPDATE_from_mxstatus(status))
5847 return TM_Ok;
5848
5851 {
5852 /* bummer */
5853 if (!ItemPointerEquals(&tup->t_self, &tup->t_data->t_ctid))
5854 return TM_Updated;
5855 else
5856 return TM_Deleted;
5857 }
5858
5859 return TM_Ok;
5860 }
5861
5862 /* Not in progress, not aborted, not committed -- must have crashed */
5863 return TM_Ok;
5864}
5865
5866
5867/*
5868 * Recursive part of heap_lock_updated_tuple
5869 *
5870 * Fetch the tuple pointed to by tid in rel, and mark it as locked by the given
5871 * xid with the given mode; if this tuple is updated, recurse to lock the new
5872 * version as well.
5873 */
5874static TM_Result
5876 const ItemPointerData *tid, TransactionId xid,
5878{
5882 Buffer buf;
5883 Page page;
5888 TransactionId xmax,
5889 new_xmax;
5890 bool cleared_all_frozen = false;
5892 Buffer vmbuffer = InvalidBuffer;
5893 bool unlock_vmbuffer = false;
5894 BlockNumber block;
5895
5896 ItemPointerCopy(tid, &tupid);
5897
5898 for (;;)
5899 {
5900 new_infomask = 0;
5901 new_xmax = InvalidTransactionId;
5902 cleared_all_frozen = false;
5904 ItemPointerCopy(&tupid, &(mytup.t_self));
5905
5906 if (!heap_fetch(rel, SnapshotAny, &mytup, &buf, false))
5907 {
5908 /*
5909 * if we fail to find the updated version of the tuple, it's
5910 * because it was vacuumed/pruned away after its creator
5911 * transaction aborted. So behave as if we got to the end of the
5912 * chain, and there's no further tuple to lock: return success to
5913 * caller.
5914 */
5915 result = TM_Ok;
5916 goto out_unlocked;
5917 }
5918
5919l4:
5921
5922 page = BufferGetPage(buf);
5923
5924 /*
5925 * Before locking the buffer, pin the visibility map page if it
5926 * appears to be necessary. Since we haven't got the lock yet,
5927 * someone else might be in the middle of changing this, so we'll need
5928 * to recheck after we have the lock.
5929 */
5930 if (PageIsAllVisible(page))
5931 {
5932 visibilitymap_pin(rel, block, &vmbuffer);
5933 pinned_desired_page = true;
5934 }
5935 else
5936 pinned_desired_page = false;
5937
5939
5940 /*
5941 * If we didn't pin the visibility map page and the page has become
5942 * all visible while we were busy locking the buffer, we'll have to
5943 * unlock and re-lock, to avoid holding the buffer lock across I/O.
5944 * That's a bit unfortunate, but hopefully shouldn't happen often.
5945 *
5946 * Note: in some paths through this function, we will reach here
5947 * holding a pin on a vm page that may or may not be the one matching
5948 * this page. If this page isn't all-visible, we won't use the vm
5949 * page, but we hold onto such a pin till the end of the function.
5950 */
5952 {
5954 visibilitymap_pin(rel, block, &vmbuffer);
5956 }
5957
5958 /*
5959 * Check the tuple XMIN against prior XMAX, if any. If we reached the
5960 * end of the chain, we're done, so return success.
5961 */
5964 priorXmax))
5965 {
5966 result = TM_Ok;
5967 goto out_locked;
5968 }
5969
5970 /*
5971 * Also check Xmin: if this tuple was created by an aborted
5972 * (sub)transaction, then we already locked the last live one in the
5973 * chain, thus we're done, so return success.
5974 */
5976 {
5977 result = TM_Ok;
5978 goto out_locked;
5979 }
5980
5981 old_infomask = mytup.t_data->t_infomask;
5982 old_infomask2 = mytup.t_data->t_infomask2;
5983 xmax = HeapTupleHeaderGetRawXmax(mytup.t_data);
5984
5985 /*
5986 * If this tuple version has been updated or locked by some concurrent
5987 * transaction(s), what we do depends on whether our lock mode
5988 * conflicts with what those other transactions hold, and also on the
5989 * status of them.
5990 */
5992 {
5994 bool needwait;
5995
5998 {
5999 int nmembers;
6000 int i;
6001 MultiXactMember *members;
6002
6003 /*
6004 * We don't need a test for pg_upgrade'd tuples: this is only
6005 * applied to tuples after the first in an update chain. Said
6006 * first tuple in the chain may well be locked-in-9.2-and-
6007 * pg_upgraded, but that one was already locked by our caller,
6008 * not us; and any subsequent ones cannot be because our
6009 * caller must necessarily have obtained a snapshot later than
6010 * the pg_upgrade itself.
6011 */
6012 Assert(!HEAP_LOCKED_UPGRADED(mytup.t_data->t_infomask));
6013
6014 nmembers = GetMultiXactIdMembers(rawxmax, &members, false,
6016 for (i = 0; i < nmembers; i++)
6017 {
6018 result = test_lockmode_for_conflict(members[i].status,
6019 members[i].xid,
6020 mode,
6021 &mytup,
6022 &needwait);
6023
6024 /*
6025 * If the tuple was already locked by ourselves in a
6026 * previous iteration of this (say heap_lock_tuple was
6027 * forced to restart the locking loop because of a change
6028 * in xmax), then we hold the lock already on this tuple
6029 * version and we don't need to do anything; and this is
6030 * not an error condition either. We just need to skip
6031 * this tuple and continue locking the next version in the
6032 * update chain.
6033 */
6034 if (result == TM_SelfModified)
6035 {
6036 pfree(members);
6037 goto next;
6038 }
6039
6040 if (needwait)
6041 {
6043 XactLockTableWait(members[i].xid, rel,
6044 &mytup.t_self,
6046 pfree(members);
6047 goto l4;
6048 }
6049 if (result != TM_Ok)
6050 {
6051 pfree(members);
6052 goto out_locked;
6053 }
6054 }
6055 if (members)
6056 pfree(members);
6057 }
6058 else
6059 {
6060 MultiXactStatus status;
6061
6062 /*
6063 * For a non-multi Xmax, we first need to compute the
6064 * corresponding MultiXactStatus by using the infomask bits.
6065 */
6067 {
6071 status = MultiXactStatusForShare;
6073 {
6075 status = MultiXactStatusForUpdate;
6076 else
6078 }
6079 else
6080 {
6081 /*
6082 * LOCK_ONLY present alone (a pg_upgraded tuple marked
6083 * as share-locked in the old cluster) shouldn't be
6084 * seen in the middle of an update chain.
6085 */
6086 elog(ERROR, "invalid lock status in tuple");
6087 }
6088 }
6089 else
6090 {
6091 /* it's an update, but which kind? */
6093 status = MultiXactStatusUpdate;
6094 else
6096 }
6097
6099 &mytup, &needwait);
6100
6101 /*
6102 * If the tuple was already locked by ourselves in a previous
6103 * iteration of this (say heap_lock_tuple was forced to
6104 * restart the locking loop because of a change in xmax), then
6105 * we hold the lock already on this tuple version and we don't
6106 * need to do anything; and this is not an error condition
6107 * either. We just need to skip this tuple and continue
6108 * locking the next version in the update chain.
6109 */
6110 if (result == TM_SelfModified)
6111 goto next;
6112
6113 if (needwait)
6114 {
6116 XactLockTableWait(rawxmax, rel, &mytup.t_self,
6118 goto l4;
6119 }
6120 if (result != TM_Ok)
6121 {
6122 goto out_locked;
6123 }
6124 }
6125 }
6126
6127 /* compute the new Xmax and infomask values for the tuple ... */
6128 compute_new_xmax_infomask(xmax, old_infomask, mytup.t_data->t_infomask2,
6129 xid, mode, false,
6130 &new_xmax, &new_infomask, &new_infomask2);
6131
6132 if (PageIsAllVisible(page))
6133 {
6135 unlock_vmbuffer = true;
6136 }
6137
6139
6140 /* ... and set them */
6141 HeapTupleHeaderSetXmax(mytup.t_data, new_xmax);
6142 mytup.t_data->t_infomask &= ~HEAP_XMAX_BITS;
6143 mytup.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
6144 mytup.t_data->t_infomask |= new_infomask;
6145 mytup.t_data->t_infomask2 |= new_infomask2;
6146
6148
6149 if (PageIsAllVisible(page))
6150 {
6151 /* It's possible all-frozen was already clear */
6152 if (visibilitymap_clear(rel->rd_locator, block, vmbuffer,
6154 cleared_all_frozen = true;
6155 }
6156
6157 /* XLOG stuff */
6158 if (RelationNeedsWAL(rel))
6159 {
6162
6165
6166 xlrec.offnum = ItemPointerGetOffsetNumber(&mytup.t_self);
6167 xlrec.xmax = new_xmax;
6169 xlrec.flags =
6171
6173
6176
6178
6179 PageSetLSN(page, recptr);
6180
6182 PageSetLSN(BufferGetPage(vmbuffer), recptr);
6183 }
6184
6186
6187 /* release VM lock first, since it covers many heap blocks */
6188 if (unlock_vmbuffer)
6189 {
6190 LockBuffer(vmbuffer, BUFFER_LOCK_UNLOCK);
6191 unlock_vmbuffer = false;
6192 }
6193
6194next:
6195 /* if we find the end of update chain, we're done. */
6196 if (mytup.t_data->t_infomask & HEAP_XMAX_INVALID ||
6198 ItemPointerEquals(&mytup.t_self, &mytup.t_data->t_ctid) ||
6200 {
6201 result = TM_Ok;
6202 goto out_locked;
6203 }
6204
6205 /* tail recursion */
6207 ItemPointerCopy(&(mytup.t_data->t_ctid), &tupid);
6209 }
6210
6211 result = TM_Ok;
6212
6215
6217 if (vmbuffer != InvalidBuffer)
6218 ReleaseBuffer(vmbuffer);
6220
6221 return result;
6222}
6223
6224/*
6225 * heap_lock_updated_tuple
6226 * Follow update chain when locking an updated tuple, acquiring locks (row
6227 * marks) on the updated versions.
6228 *
6229 * 'prior_infomask', 'prior_raw_xmax' and 'prior_ctid' are the corresponding
6230 * fields from the initial tuple. We will lock the tuples starting from the
6231 * one that 'prior_ctid' points to. Note: This function does not lock the
6232 * initial tuple itself.
6233 *
6234 * This function doesn't check visibility, it just unconditionally marks the
6235 * tuple(s) as locked. If any tuple in the updated chain is being deleted
6236 * concurrently (or updated with the key being modified), sleep until the
6237 * transaction doing it is finished.
6238 *
6239 * Note that we don't acquire heavyweight tuple locks on the tuples we walk
6240 * when we have to wait for other transactions to release them, as opposed to
6241 * what heap_lock_tuple does. The reason is that having more than one
6242 * transaction walking the chain is probably uncommon enough that risk of
6243 * starvation is not likely: one of the preconditions for being here is that
6244 * the snapshot in use predates the update that created this tuple (because we
6245 * started at an earlier version of the tuple), but at the same time such a
6246 * transaction cannot be using repeatable read or serializable isolation
6247 * levels, because that would lead to a serializability failure.
6248 */
6249static TM_Result
6255{
6256 INJECTION_POINT("heap_lock_updated_tuple", NULL);
6257
6258 /*
6259 * If the tuple has moved into another partition (effectively a delete)
6260 * stop here.
6261 */
6263 {
6265
6266 /*
6267 * If this is the first possibly-multixact-able operation in the
6268 * current transaction, set my per-backend OldestMemberMXactId
6269 * setting. We can be certain that the transaction will never become a
6270 * member of any older MultiXactIds than that. (We have to do this
6271 * even if we end up just using our own TransactionId below, since
6272 * some other backend could incorporate our XID into a MultiXact
6273 * immediately afterwards.)
6274 */
6276
6280 }
6281
6282 /* nothing to lock */
6283 return TM_Ok;
6284}
6285
6286/*
6287 * heap_finish_speculative - mark speculative insertion as successful
6288 *
6289 * To successfully finish a speculative insertion we have to clear speculative
6290 * token from tuple. To do so the t_ctid field, which will contain a
6291 * speculative token value, is modified in place to point to the tuple itself,
6292 * which is characteristic of a newly inserted ordinary tuple.
6293 *
6294 * NB: It is not ok to commit without either finishing or aborting a
6295 * speculative insertion. We could treat speculative tuples of committed
6296 * transactions implicitly as completed, but then we would have to be prepared
6297 * to deal with speculative tokens on committed tuples. That wouldn't be
6298 * difficult - no-one looks at the ctid field of a tuple with invalid xmax -
6299 * but clearing the token at completion isn't very expensive either.
6300 * An explicit confirmation WAL record also makes logical decoding simpler.
6301 */
6302void
6304{
6305 Buffer buffer;
6306 Page page;
6307 OffsetNumber offnum;
6308 ItemId lp;
6309 HeapTupleHeader htup;
6310
6311 buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
6313 page = BufferGetPage(buffer);
6314
6315 offnum = ItemPointerGetOffsetNumber(tid);
6317 elog(ERROR, "offnum out of range");
6318 lp = PageGetItemId(page, offnum);
6319 if (!ItemIdIsNormal(lp))
6320 elog(ERROR, "invalid lp");
6321
6322 htup = (HeapTupleHeader) PageGetItem(page, lp);
6323
6324 /* NO EREPORT(ERROR) from here till changes are logged */
6326
6328
6329 MarkBufferDirty(buffer);
6330
6331 /*
6332 * Replace the speculative insertion token with a real t_ctid, pointing to
6333 * itself like it does on regular tuples.
6334 */
6335 htup->t_ctid = *tid;
6336
6337 /* XLOG stuff */
6338 if (RelationNeedsWAL(relation))
6339 {
6342
6344
6346
6347 /* We want the same filtering on this as on a plain insert */
6349
6352
6354
6355 PageSetLSN(page, recptr);
6356 }
6357
6359
6360 UnlockReleaseBuffer(buffer);
6361}
6362
6363/*
6364 * heap_abort_speculative - kill a speculatively inserted tuple
6365 *
6366 * Marks a tuple that was speculatively inserted in the same command as dead,
6367 * by setting its xmin as invalid. That makes it immediately appear as dead
6368 * to all transactions, including our own. In particular, it makes
6369 * HeapTupleSatisfiesDirty() regard the tuple as dead, so that another backend
6370 * inserting a duplicate key value won't unnecessarily wait for our whole
6371 * transaction to finish (it'll just wait for our speculative insertion to
6372 * finish).
6373 *
6374 * Killing the tuple prevents "unprincipled deadlocks", which are deadlocks
6375 * that arise due to a mutual dependency that is not user visible. By
6376 * definition, unprincipled deadlocks cannot be prevented by the user
6377 * reordering lock acquisition in client code, because the implementation level
6378 * lock acquisitions are not under the user's direct control. If speculative
6379 * inserters did not take this precaution, then under high concurrency they
6380 * could deadlock with each other, which would not be acceptable.
6381 *
6382 * This is somewhat redundant with heap_delete, but we prefer to have a
6383 * dedicated routine with stripped down requirements. Note that this is also
6384 * used to delete the TOAST tuples created during speculative insertion.
6385 *
6386 * This routine does not affect logical decoding as it only looks at
6387 * confirmation records.
6388 */
6389void
6391{
6393 ItemId lp;
6394 HeapTupleData tp;
6395 Page page;
6396 BlockNumber block;
6397 Buffer buffer;
6398
6400
6401 block = ItemPointerGetBlockNumber(tid);
6402 buffer = ReadBuffer(relation, block);
6403 page = BufferGetPage(buffer);
6404
6406
6407 /*
6408 * Page can't be all visible, we just inserted into it, and are still
6409 * running.
6410 */
6411 Assert(!PageIsAllVisible(page));
6412
6415
6416 tp.t_tableOid = RelationGetRelid(relation);
6417 tp.t_data = (HeapTupleHeader) PageGetItem(page, lp);
6418 tp.t_len = ItemIdGetLength(lp);
6419 tp.t_self = *tid;
6420
6421 /*
6422 * Sanity check that the tuple really is a speculatively inserted tuple,
6423 * inserted by us.
6424 */
6425 if (tp.t_data->t_choice.t_heap.t_xmin != xid)
6426 elog(ERROR, "attempted to kill a tuple inserted by another transaction");
6427 if (!(IsToastRelation(relation) || HeapTupleHeaderIsSpeculative(tp.t_data)))
6428 elog(ERROR, "attempted to kill a non-speculative tuple");
6430
6431 /*
6432 * No need to check for serializable conflicts here. There is never a
6433 * need for a combo CID, either. No need to extract replica identity, or
6434 * do anything special with infomask bits.
6435 */
6436
6438
6439 /*
6440 * The tuple will become DEAD immediately. Flag that this page is a
6441 * candidate for pruning by setting xmin to TransactionXmin. While not
6442 * immediately prunable, it is the oldest xid we can cheaply determine
6443 * that's safe against wraparound / being older than the table's
6444 * relfrozenxid. To defend against the unlikely case of a new relation
6445 * having a newer relfrozenxid than our TransactionXmin, use relfrozenxid
6446 * if so (vacuum can't subsequently move relfrozenxid to beyond
6447 * TransactionXmin, so there's no race here).
6448 */
6450 {
6451 TransactionId relfrozenxid = relation->rd_rel->relfrozenxid;
6453
6454 if (TransactionIdPrecedes(TransactionXmin, relfrozenxid))
6455 prune_xid = relfrozenxid;
6456 else
6459 }
6460
6461 /* store transaction information of xact deleting the tuple */
6464
6465 /*
6466 * Set the tuple header xmin to InvalidTransactionId. This makes the
6467 * tuple immediately invisible everyone. (In particular, to any
6468 * transactions waiting on the speculative token, woken up later.)
6469 */
6471
6472 /* Clear the speculative insertion token too */
6473 tp.t_data->t_ctid = tp.t_self;
6474
6475 MarkBufferDirty(buffer);
6476
6477 /*
6478 * XLOG stuff
6479 *
6480 * The WAL records generated here match heap_delete(). The same recovery
6481 * routines are used.
6482 */
6483 if (RelationNeedsWAL(relation))
6484 {
6487
6489 xlrec.infobits_set = compute_infobits(tp.t_data->t_infomask,
6490 tp.t_data->t_infomask2);
6492 xlrec.xmax = xid;
6493
6497
6498 /* No replica identity & replication origin logged */
6499
6501
6502 PageSetLSN(page, recptr);
6503 }
6504
6506
6508
6509 if (HeapTupleHasExternal(&tp))
6510 {
6511 Assert(!IsToastRelation(relation));
6512 heap_toast_delete(relation, &tp, true);
6513 }
6514
6515 /*
6516 * Never need to mark tuple for invalidation, since catalogs don't support
6517 * speculative insertion
6518 */
6519
6520 /* Now we can release the buffer */
6521 ReleaseBuffer(buffer);
6522
6523 /* count deletion, as we counted the insertion too */
6524 pgstat_count_heap_delete(relation);
6525}
6526
6527/*
6528 * heap_inplace_lock - protect inplace update from concurrent heap_update()
6529 *
6530 * Evaluate whether the tuple's state is compatible with a no-key update.
6531 * Current transaction rowmarks are fine, as is KEY SHARE from any
6532 * transaction. If compatible, return true with the buffer exclusive-locked,
6533 * and the caller must release that by calling
6534 * heap_inplace_update_and_unlock(), calling heap_inplace_unlock(), or raising
6535 * an error. Otherwise, call release_callback(arg), wait for blocking
6536 * transactions to end, and return false.
6537 *
6538 * Since this is intended for system catalogs and SERIALIZABLE doesn't cover
6539 * DDL, this doesn't guarantee any particular predicate locking.
6540 *
6541 * heap_delete() is a rarer source of blocking transactions (xwait). We'll
6542 * wait for such a transaction just like for the normal heap_update() case.
6543 * Normal concurrent DROP commands won't cause that, because all inplace
6544 * updaters take some lock that conflicts with DROP. An explicit SQL "DELETE
6545 * FROM pg_class" can cause it. By waiting, if the concurrent transaction
6546 * executed both "DELETE FROM pg_class" and "INSERT INTO pg_class", our caller
6547 * can find the successor tuple.
6548 *
6549 * Readers of inplace-updated fields expect changes to those fields are
6550 * durable. For example, vac_truncate_clog() reads datfrozenxid from
6551 * pg_database tuples via catalog snapshots. A future snapshot must not
6552 * return a lower datfrozenxid for the same database OID (lower in the
6553 * FullTransactionIdPrecedes() sense). We achieve that since no update of a
6554 * tuple can start while we hold a lock on its buffer. In cases like
6555 * BEGIN;GRANT;CREATE INDEX;COMMIT we're inplace-updating a tuple visible only
6556 * to this transaction. ROLLBACK then is one case where it's okay to lose
6557 * inplace updates. (Restoring relhasindex=false on ROLLBACK is fine, since
6558 * any concurrent CREATE INDEX would have blocked, then inplace-updated the
6559 * committed tuple.)
6560 *
6561 * In principle, we could avoid waiting by overwriting every tuple in the
6562 * updated tuple chain. Reader expectations permit updating a tuple only if
6563 * it's aborted, is the tail of the chain, or we already updated the tuple
6564 * referenced in its t_ctid. Hence, we would need to overwrite the tuples in
6565 * order from tail to head. That would imply either (a) mutating all tuples
6566 * in one critical section or (b) accepting a chance of partial completion.
6567 * Partial completion of a relfrozenxid update would have the weird
6568 * consequence that the table's next VACUUM could see the table's relfrozenxid
6569 * move forward between vacuum_get_cutoffs() and finishing.
6570 */
6571bool
6573 HeapTuple oldtup_ptr, Buffer buffer,
6574 void (*release_callback) (void *), void *arg)
6575{
6576 HeapTupleData oldtup = *oldtup_ptr; /* minimize diff vs. heap_update() */
6578 bool ret;
6579
6580#ifdef USE_ASSERT_CHECKING
6581 if (RelationGetRelid(relation) == RelationRelationId)
6583#endif
6584
6585 Assert(BufferIsValid(buffer));
6586
6587 /*
6588 * Register shared cache invals if necessary. Other sessions may finish
6589 * inplace updates of this tuple between this step and LockTuple(). Since
6590 * inplace updates don't change cache keys, that's harmless.
6591 *
6592 * While it's tempting to register invals only after confirming we can
6593 * return true, the following obstacle precludes reordering steps that
6594 * way. Registering invals might reach a CatalogCacheInitializeCache()
6595 * that locks "buffer". That would hang indefinitely if running after our
6596 * own LockBuffer(). Hence, we must register invals before LockBuffer().
6597 */
6599
6600 LockTuple(relation, &oldtup.t_self, InplaceUpdateTupleLock);
6602
6603 /*----------
6604 * Interpret HeapTupleSatisfiesUpdate() like heap_update() does, except:
6605 *
6606 * - wait unconditionally
6607 * - already locked tuple above, since inplace needs that unconditionally
6608 * - don't recheck header after wait: simpler to defer to next iteration
6609 * - don't try to continue even if the updater aborts: likewise
6610 * - no crosscheck
6611 */
6613 buffer);
6614
6615 if (result == TM_Invisible)
6616 {
6617 /* no known way this can happen */
6618 ereport(ERROR,
6620 errmsg_internal("attempted to overwrite invisible tuple")));
6621 }
6622 else if (result == TM_SelfModified)
6623 {
6624 /*
6625 * CREATE INDEX might reach this if an expression is silly enough to
6626 * call e.g. SELECT ... FROM pg_class FOR SHARE. C code of other SQL
6627 * statements might get here after a heap_update() of the same row, in
6628 * the absence of an intervening CommandCounterIncrement().
6629 */
6630 ereport(ERROR,
6632 errmsg("tuple to be updated was already modified by an operation triggered by the current command")));
6633 }
6634 else if (result == TM_BeingModified)
6635 {
6638
6640 infomask = oldtup.t_data->t_infomask;
6641
6643 {
6646 int remain;
6647
6649 lockmode, NULL))
6650 {
6653 ret = false;
6655 relation, &oldtup.t_self, XLTW_Update,
6656 &remain);
6657 }
6658 else
6659 ret = true;
6660 }
6662 ret = true;
6664 ret = true;
6665 else
6666 {
6669 ret = false;
6670 XactLockTableWait(xwait, relation, &oldtup.t_self,
6671 XLTW_Update);
6672 }
6673 }
6674 else
6675 {
6676 ret = (result == TM_Ok);
6677 if (!ret)
6678 {
6681 }
6682 }
6683
6684 /*
6685 * GetCatalogSnapshot() relies on invalidation messages to know when to
6686 * take a new snapshot. COMMIT of xwait is responsible for sending the
6687 * invalidation. We're not acquiring heavyweight locks sufficient to
6688 * block if not yet sent, so we must take a new snapshot to ensure a later
6689 * attempt has a fair chance. While we don't need this if xwait aborted,
6690 * don't bother optimizing that.
6691 */
6692 if (!ret)
6693 {
6694 UnlockTuple(relation, &oldtup.t_self, InplaceUpdateTupleLock);
6697 }
6698 return ret;
6699}
6700
6701/*
6702 * heap_inplace_update_and_unlock - core of systable_inplace_update_finish
6703 *
6704 * The tuple cannot change size, and therefore its header fields and null
6705 * bitmap (if any) don't change either.
6706 *
6707 * Since we hold LOCKTAG_TUPLE, no updater has a local copy of this tuple.
6708 */
6709void
6711 HeapTuple oldtup, HeapTuple tuple,
6712 Buffer buffer)
6713{
6714 HeapTupleHeader htup = oldtup->t_data;
6715 uint32 oldlen;
6716 uint32 newlen;
6717 char *dst;
6718 char *src;
6719 int nmsgs = 0;
6721 bool RelcacheInitFileInval = false;
6722
6723 Assert(ItemPointerEquals(&oldtup->t_self, &tuple->t_self));
6724 oldlen = oldtup->t_len - htup->t_hoff;
6725 newlen = tuple->t_len - tuple->t_data->t_hoff;
6726 if (oldlen != newlen || htup->t_hoff != tuple->t_data->t_hoff)
6727 elog(ERROR, "wrong tuple length");
6728
6729 dst = (char *) htup + htup->t_hoff;
6730 src = (char *) tuple->t_data + tuple->t_data->t_hoff;
6731
6732 /* Like RecordTransactionCommit(), log only if needed */
6735 &RelcacheInitFileInval);
6736
6737 /*
6738 * Unlink relcache init files as needed. If unlinking, acquire
6739 * RelCacheInitLock until after associated invalidations. By doing this
6740 * in advance, if we checkpoint and then crash between inplace
6741 * XLogInsert() and inval, we don't rely on StartupXLOG() ->
6742 * RelationCacheInitFileRemove(). That uses elevel==LOG, so replay would
6743 * neglect to PANIC on EIO.
6744 */
6746
6747 /*----------
6748 * NO EREPORT(ERROR) from here till changes are complete
6749 *
6750 * Our exclusive buffer lock won't stop a reader having already pinned and
6751 * checked visibility for this tuple. With the usual order of changes
6752 * (i.e. updating the buffer contents before WAL logging), a reader could
6753 * observe our not-yet-persistent update to relfrozenxid and update
6754 * datfrozenxid based on that. A crash in that moment could allow
6755 * datfrozenxid to overtake relfrozenxid:
6756 *
6757 * ["D" is a VACUUM (ONLY_DATABASE_STATS)]
6758 * ["R" is a VACUUM tbl]
6759 * D: vac_update_datfrozenxid() -> systable_beginscan(pg_class)
6760 * D: systable_getnext() returns pg_class tuple of tbl
6761 * R: memcpy() into pg_class tuple of tbl
6762 * D: raise pg_database.datfrozenxid, XLogInsert(), finish
6763 * [crash]
6764 * [recovery restores datfrozenxid w/o relfrozenxid]
6765 *
6766 * We avoid that by using a temporary copy of the buffer to hide our
6767 * change from other backends until the change has been WAL-logged. We
6768 * apply our change to the temporary copy and WAL-log it, before modifying
6769 * the real page. That way any action a reader of the in-place-updated
6770 * value takes will be WAL logged after this change.
6771 */
6773
6774 MarkBufferDirty(buffer);
6775
6776 /* XLOG stuff */
6777 if (RelationNeedsWAL(relation))
6778 {
6781 char *origdata = (char *) BufferGetBlock(buffer);
6782 Page page = BufferGetPage(buffer);
6783 uint16 lower = ((PageHeader) page)->pd_lower;
6784 uint16 upper = ((PageHeader) page)->pd_upper;
6786 RelFileLocator rlocator;
6787 ForkNumber forkno;
6788 BlockNumber blkno;
6790
6791 xlrec.offnum = ItemPointerGetOffsetNumber(&tuple->t_self);
6792 xlrec.dbId = MyDatabaseId;
6794 xlrec.relcacheInitFileInval = RelcacheInitFileInval;
6795 xlrec.nmsgs = nmsgs;
6796
6799 if (nmsgs != 0)
6801 nmsgs * sizeof(SharedInvalidationMessage));
6802
6803 /* register block matching what buffer will look like after changes */
6808 BufferGetTag(buffer, &rlocator, &forkno, &blkno);
6809 Assert(forkno == MAIN_FORKNUM);
6810 XLogRegisterBlock(0, &rlocator, forkno, blkno, copied_buffer.data,
6812 XLogRegisterBufData(0, src, newlen);
6813
6814 /* inplace updates aren't decoded atm, don't log the origin */
6815
6817
6818 PageSetLSN(page, recptr);
6819 }
6820
6821 memcpy(dst, src, newlen);
6822
6824
6825 /*
6826 * Send invalidations to shared queue. SearchSysCacheLocked1() assumes we
6827 * do this before UnlockTuple().
6828 */
6830
6832 UnlockTuple(relation, &tuple->t_self, InplaceUpdateTupleLock);
6833
6834 AcceptInvalidationMessages(); /* local processing of just-sent inval */
6835
6836 /*
6837 * Queue a transactional inval, for logical decoding and for third-party
6838 * code that might have been relying on it since long before inplace
6839 * update adopted immediate invalidation. See README.tuplock section
6840 * "Reading inplace-updated columns" for logical decoding details.
6841 */
6843 CacheInvalidateHeapTuple(relation, tuple, NULL);
6844}
6845
6846/*
6847 * heap_inplace_unlock - reverse of heap_inplace_lock
6848 */
6849void
6851 HeapTuple oldtup, Buffer buffer)
6852{
6854 UnlockTuple(relation, &oldtup->t_self, InplaceUpdateTupleLock);
6856}
6857
6858#define FRM_NOOP 0x0001
6859#define FRM_INVALIDATE_XMAX 0x0002
6860#define FRM_RETURN_IS_XID 0x0004
6861#define FRM_RETURN_IS_MULTI 0x0008
6862#define FRM_MARK_COMMITTED 0x0010
6863
6864/*
6865 * FreezeMultiXactId
6866 * Determine what to do during freezing when a tuple is marked by a
6867 * MultiXactId.
6868 *
6869 * "flags" is an output value; it's used to tell caller what to do on return.
6870 * "pagefrz" is an input/output value, used to manage page level freezing.
6871 *
6872 * Possible values that we can set in "flags":
6873 * FRM_NOOP
6874 * don't do anything -- keep existing Xmax
6875 * FRM_INVALIDATE_XMAX
6876 * mark Xmax as InvalidTransactionId and set XMAX_INVALID flag.
6877 * FRM_RETURN_IS_XID
6878 * The Xid return value is a single update Xid to set as xmax.
6879 * FRM_MARK_COMMITTED
6880 * Xmax can be marked as HEAP_XMAX_COMMITTED
6881 * FRM_RETURN_IS_MULTI
6882 * The return value is a new MultiXactId to set as new Xmax.
6883 * (caller must obtain proper infomask bits using GetMultiXactIdHintBits)
6884 *
6885 * Caller delegates control of page freezing to us. In practice we always
6886 * force freezing of caller's page unless FRM_NOOP processing is indicated.
6887 * We help caller ensure that XIDs < FreezeLimit and MXIDs < MultiXactCutoff
6888 * can never be left behind. We freely choose when and how to process each
6889 * Multi, without ever violating the cutoff postconditions for freezing.
6890 *
6891 * It's useful to remove Multis on a proactive timeline (relative to freezing
6892 * XIDs) to keep MultiXact member SLRU buffer misses to a minimum. It can also
6893 * be cheaper in the short run, for us, since we too can avoid SLRU buffer
6894 * misses through eager processing.
6895 *
6896 * NB: Creates a _new_ MultiXactId when FRM_RETURN_IS_MULTI is set, though only
6897 * when FreezeLimit and/or MultiXactCutoff cutoffs leave us with no choice.
6898 * This can usually be put off, which is usually enough to avoid it altogether.
6899 * Allocating new multis during VACUUM should be avoided on general principle;
6900 * only VACUUM can advance relminmxid, so allocating new Multis here comes with
6901 * its own special risks.
6902 *
6903 * NB: Caller must maintain "no freeze" NewRelfrozenXid/NewRelminMxid trackers
6904 * using heap_tuple_should_freeze when we haven't forced page-level freezing.
6905 *
6906 * NB: Caller should avoid needlessly calling heap_tuple_should_freeze when we
6907 * have already forced page-level freezing, since that might incur the same
6908 * SLRU buffer misses that we specifically intended to avoid by freezing.
6909 */
6910static TransactionId
6912 const struct VacuumCutoffs *cutoffs, uint16 *flags,
6913 HeapPageFreeze *pagefrz)
6914{
6916 MultiXactMember *members;
6917 int nmembers;
6918 bool need_replace;
6919 int nnewmembers;
6921 bool has_lockers;
6923 bool update_committed;
6924 TransactionId FreezePageRelfrozenXid;
6925
6926 *flags = 0;
6927
6928 /* We should only be called in Multis */
6929 Assert(t_infomask & HEAP_XMAX_IS_MULTI);
6930
6931 if (!MultiXactIdIsValid(multi) ||
6932 HEAP_LOCKED_UPGRADED(t_infomask))
6933 {
6934 *flags |= FRM_INVALIDATE_XMAX;
6935 pagefrz->freeze_required = true;
6936 return InvalidTransactionId;
6937 }
6938 else if (MultiXactIdPrecedes(multi, cutoffs->relminmxid))
6939 ereport(ERROR,
6941 errmsg_internal("found multixact %u from before relminmxid %u",
6942 multi, cutoffs->relminmxid)));
6943 else if (MultiXactIdPrecedes(multi, cutoffs->OldestMxact))
6944 {
6946
6947 /*
6948 * This old multi cannot possibly have members still running, but
6949 * verify just in case. If it was a locker only, it can be removed
6950 * without any further consideration; but if it contained an update,
6951 * we might need to preserve it.
6952 */
6953 if (MultiXactIdIsRunning(multi,
6954 HEAP_XMAX_IS_LOCKED_ONLY(t_infomask)))
6955 ereport(ERROR,
6957 errmsg_internal("multixact %u from before multi freeze cutoff %u found to be still running",
6958 multi, cutoffs->OldestMxact)));
6959
6960 if (HEAP_XMAX_IS_LOCKED_ONLY(t_infomask))
6961 {
6962 *flags |= FRM_INVALIDATE_XMAX;
6963 pagefrz->freeze_required = true;
6964 return InvalidTransactionId;
6965 }
6966
6967 /* replace multi with single XID for its updater? */
6968 update_xact = MultiXactIdGetUpdateXid(multi, t_infomask);
6970 ereport(ERROR,
6972 errmsg_internal("multixact %u contains update XID %u from before relfrozenxid %u",
6973 multi, update_xact,
6974 cutoffs->relfrozenxid)));
6975 else if (TransactionIdPrecedes(update_xact, cutoffs->OldestXmin))
6976 {
6977 /*
6978 * Updater XID has to have aborted (otherwise the tuple would have
6979 * been pruned away instead, since updater XID is < OldestXmin).
6980 * Just remove xmax.
6981 */
6983 ereport(ERROR,
6985 errmsg_internal("multixact %u contains committed update XID %u from before removable cutoff %u",
6986 multi, update_xact,
6987 cutoffs->OldestXmin)));
6988 *flags |= FRM_INVALIDATE_XMAX;
6989 pagefrz->freeze_required = true;
6990 return InvalidTransactionId;
6991 }
6992
6993 /* Have to keep updater XID as new xmax */
6994 *flags |= FRM_RETURN_IS_XID;
6995 pagefrz->freeze_required = true;
6996 return update_xact;
6997 }
6998
6999 /*
7000 * Some member(s) of this Multi may be below FreezeLimit xid cutoff, so we
7001 * need to walk the whole members array to figure out what to do, if
7002 * anything.
7003 */
7004 nmembers =
7005 GetMultiXactIdMembers(multi, &members, false,
7006 HEAP_XMAX_IS_LOCKED_ONLY(t_infomask));
7007 if (nmembers <= 0)
7008 {
7009 /* Nothing worth keeping */
7010 *flags |= FRM_INVALIDATE_XMAX;
7011 pagefrz->freeze_required = true;
7012 return InvalidTransactionId;
7013 }
7014
7015 /*
7016 * The FRM_NOOP case is the only case where we might need to ratchet back
7017 * FreezePageRelfrozenXid or FreezePageRelminMxid. It is also the only
7018 * case where our caller might ratchet back its NoFreezePageRelfrozenXid
7019 * or NoFreezePageRelminMxid "no freeze" trackers to deal with a multi.
7020 * FRM_NOOP handling should result in the NewRelfrozenXid/NewRelminMxid
7021 * trackers managed by VACUUM being ratcheting back by xmax to the degree
7022 * required to make it safe to leave xmax undisturbed, independent of
7023 * whether or not page freezing is triggered somewhere else.
7024 *
7025 * Our policy is to force freezing in every case other than FRM_NOOP,
7026 * which obviates the need to maintain either set of trackers, anywhere.
7027 * Every other case will reliably execute a freeze plan for xmax that
7028 * either replaces xmax with an XID/MXID >= OldestXmin/OldestMxact, or
7029 * sets xmax to an InvalidTransactionId XID, rendering xmax fully frozen.
7030 * (VACUUM's NewRelfrozenXid/NewRelminMxid trackers are initialized with
7031 * OldestXmin/OldestMxact, so later values never need to be tracked here.)
7032 */
7033 need_replace = false;
7034 FreezePageRelfrozenXid = pagefrz->FreezePageRelfrozenXid;
7035 for (int i = 0; i < nmembers; i++)
7036 {
7037 TransactionId xid = members[i].xid;
7038
7039 Assert(!TransactionIdPrecedes(xid, cutoffs->relfrozenxid));
7040
7041 if (TransactionIdPrecedes(xid, cutoffs->FreezeLimit))
7042 {
7043 /* Can't violate the FreezeLimit postcondition */
7044 need_replace = true;
7045 break;
7046 }
7047 if (TransactionIdPrecedes(xid, FreezePageRelfrozenXid))
7048 FreezePageRelfrozenXid = xid;
7049 }
7050
7051 /* Can't violate the MultiXactCutoff postcondition, either */
7052 if (!need_replace)
7054
7055 if (!need_replace)
7056 {
7057 /*
7058 * vacuumlazy.c might ratchet back NewRelminMxid, NewRelfrozenXid, or
7059 * both together to make it safe to retain this particular multi after
7060 * freezing its page
7061 */
7062 *flags |= FRM_NOOP;
7063 pagefrz->FreezePageRelfrozenXid = FreezePageRelfrozenXid;
7064 if (MultiXactIdPrecedes(multi, pagefrz->FreezePageRelminMxid))
7065 pagefrz->FreezePageRelminMxid = multi;
7066 pfree(members);
7067 return multi;
7068 }
7069
7070 /*
7071 * Do a more thorough second pass over the multi to figure out which
7072 * member XIDs actually need to be kept. Checking the precise status of
7073 * individual members might even show that we don't need to keep anything.
7074 * That is quite possible even though the Multi must be >= OldestMxact,
7075 * since our second pass only keeps member XIDs when it's truly necessary;
7076 * even member XIDs >= OldestXmin often won't be kept by second pass.
7077 */
7078 nnewmembers = 0;
7080 has_lockers = false;
7082 update_committed = false;
7083
7084 /*
7085 * Determine whether to keep each member xid, or to ignore it instead
7086 */
7087 for (int i = 0; i < nmembers; i++)
7088 {
7089 TransactionId xid = members[i].xid;
7090 MultiXactStatus mstatus = members[i].status;
7091
7092 Assert(!TransactionIdPrecedes(xid, cutoffs->relfrozenxid));
7093
7094 if (!ISUPDATE_from_mxstatus(mstatus))
7095 {
7096 /*
7097 * Locker XID (not updater XID). We only keep lockers that are
7098 * still running.
7099 */
7102 {
7103 if (TransactionIdPrecedes(xid, cutoffs->OldestXmin))
7104 ereport(ERROR,
7106 errmsg_internal("multixact %u contains running locker XID %u from before removable cutoff %u",
7107 multi, xid,
7108 cutoffs->OldestXmin)));
7109 newmembers[nnewmembers++] = members[i];
7110 has_lockers = true;
7111 }
7112
7113 continue;
7114 }
7115
7116 /*
7117 * Updater XID (not locker XID). Should we keep it?
7118 *
7119 * Since the tuple wasn't totally removed when vacuum pruned, the
7120 * update Xid cannot possibly be older than OldestXmin cutoff unless
7121 * the updater XID aborted. If the updater transaction is known
7122 * aborted or crashed then it's okay to ignore it, otherwise not.
7123 *
7124 * In any case the Multi should never contain two updaters, whatever
7125 * their individual commit status. Check for that first, in passing.
7126 */
7128 ereport(ERROR,
7130 errmsg_internal("multixact %u has two or more updating members",
7131 multi),
7132 errdetail_internal("First updater XID=%u second updater XID=%u.",
7133 update_xid, xid)));
7134
7135 /*
7136 * As with all tuple visibility routines, it's critical to test
7137 * TransactionIdIsInProgress before TransactionIdDidCommit, because of
7138 * race conditions explained in detail in heapam_visibility.c.
7139 */
7142 update_xid = xid;
7143 else if (TransactionIdDidCommit(xid))
7144 {
7145 /*
7146 * The transaction committed, so we can tell caller to set
7147 * HEAP_XMAX_COMMITTED. (We can only do this because we know the
7148 * transaction is not running.)
7149 */
7150 update_committed = true;
7151 update_xid = xid;
7152 }
7153 else
7154 {
7155 /*
7156 * Not in progress, not committed -- must be aborted or crashed;
7157 * we can ignore it.
7158 */
7159 continue;
7160 }
7161
7162 /*
7163 * We determined that updater must be kept -- add it to pending new
7164 * members list
7165 */
7166 if (TransactionIdPrecedes(xid, cutoffs->OldestXmin))
7167 ereport(ERROR,
7169 errmsg_internal("multixact %u contains committed update XID %u from before removable cutoff %u",
7170 multi, xid, cutoffs->OldestXmin)));
7171 newmembers[nnewmembers++] = members[i];
7172 }
7173
7174 pfree(members);
7175
7176 /*
7177 * Determine what to do with caller's multi based on information gathered
7178 * during our second pass
7179 */
7180 if (nnewmembers == 0)
7181 {
7182 /* Nothing worth keeping */
7183 *flags |= FRM_INVALIDATE_XMAX;
7185 }
7187 {
7188 /*
7189 * If there's a single member and it's an update, pass it back alone
7190 * without creating a new Multi. (XXX we could do this when there's a
7191 * single remaining locker, too, but that would complicate the API too
7192 * much; moreover, the case with the single updater is more
7193 * interesting, because those are longer-lived.)
7194 */
7195 Assert(nnewmembers == 1);
7196 *flags |= FRM_RETURN_IS_XID;
7197 if (update_committed)
7198 *flags |= FRM_MARK_COMMITTED;
7200 }
7201 else
7202 {
7203 /*
7204 * Create a new multixact with the surviving members of the previous
7205 * one, to set as new Xmax in the tuple
7206 */
7208 *flags |= FRM_RETURN_IS_MULTI;
7209 }
7210
7212
7213 pagefrz->freeze_required = true;
7214 return newxmax;
7215}
7216
7217/*
7218 * heap_prepare_freeze_tuple
7219 *
7220 * Check to see whether any of the XID fields of a tuple (xmin, xmax, xvac)
7221 * are older than the OldestXmin and/or OldestMxact freeze cutoffs. If so,
7222 * setup enough state (in the *frz output argument) to enable caller to
7223 * process this tuple as part of freezing its page, and return true. Return
7224 * false if nothing can be changed about the tuple right now.
7225 *
7226 * FreezePageConflictXid is advanced only for xmin/xvac freezing, not for xmax
7227 * changes. We only remove xmax state here when it is lock-only, or when the
7228 * updater XID (including an updater member of a MultiXact) must be aborted;
7229 * otherwise, the tuple would already be removable. Neither case affects
7230 * visibility on a standby.
7231 *
7232 * Also sets *totally_frozen to true if the tuple will be totally frozen once
7233 * caller executes returned freeze plan (or if the tuple was already totally
7234 * frozen by an earlier VACUUM). This indicates that there are no remaining
7235 * XIDs or MultiXactIds that will need to be processed by a future VACUUM.
7236 *
7237 * VACUUM caller must assemble HeapTupleFreeze freeze plan entries for every
7238 * tuple that we returned true for, and then execute freezing. Caller must
7239 * initialize pagefrz fields for page as a whole before first call here for
7240 * each heap page.
7241 *
7242 * VACUUM caller decides on whether or not to freeze the page as a whole.
7243 * We'll often prepare freeze plans for a page that caller just discards.
7244 * However, VACUUM doesn't always get to make a choice; it must freeze when
7245 * pagefrz.freeze_required is set, to ensure that any XIDs < FreezeLimit (and
7246 * MXIDs < MultiXactCutoff) can never be left behind. We help to make sure
7247 * that VACUUM always follows that rule.
7248 *
7249 * We sometimes force freezing of xmax MultiXactId values long before it is
7250 * strictly necessary to do so just to ensure the FreezeLimit postcondition.
7251 * It's worth processing MultiXactIds proactively when it is cheap to do so,
7252 * and it's convenient to make that happen by piggy-backing it on the "force
7253 * freezing" mechanism. Conversely, we sometimes delay freezing MultiXactIds
7254 * because it is expensive right now (though only when it's still possible to
7255 * do so without violating the FreezeLimit/MultiXactCutoff postcondition).
7256 *
7257 * It is assumed that the caller has checked the tuple with
7258 * HeapTupleSatisfiesVacuum() and determined that it is not HEAPTUPLE_DEAD
7259 * (else we should be removing the tuple, not freezing it).
7260 *
7261 * NB: This function has side effects: it might allocate a new MultiXactId.
7262 * It will be set as tuple's new xmax when our *frz output is processed within
7263 * heap_execute_freeze_tuple later on. If the tuple is in a shared buffer
7264 * then caller had better have an exclusive lock on it already.
7265 */
7266bool
7268 const struct VacuumCutoffs *cutoffs,
7269 HeapPageFreeze *pagefrz,
7271{
7272 bool xmin_already_frozen = false,
7273 xmax_already_frozen = false;
7274 bool freeze_xmin = false,
7275 replace_xvac = false,
7276 replace_xmax = false,
7277 freeze_xmax = false;
7278 TransactionId xid;
7279
7280 frz->xmax = HeapTupleHeaderGetRawXmax(tuple);
7281 frz->t_infomask2 = tuple->t_infomask2;
7282 frz->t_infomask = tuple->t_infomask;
7283 frz->frzflags = 0;
7284 frz->checkflags = 0;
7285
7286 /*
7287 * Process xmin, while keeping track of whether it's already frozen, or
7288 * will become frozen iff our freeze plan is executed by caller (could be
7289 * neither).
7290 */
7291 xid = HeapTupleHeaderGetXmin(tuple);
7292 if (!TransactionIdIsNormal(xid))
7293 xmin_already_frozen = true;
7294 else
7295 {
7296 if (TransactionIdPrecedes(xid, cutoffs->relfrozenxid))
7297 ereport(ERROR,
7299 errmsg_internal("found xmin %u from before relfrozenxid %u",
7300 xid, cutoffs->relfrozenxid)));
7301
7302 /* Will set freeze_xmin flags in freeze plan below */
7304
7305 /* Verify that xmin committed if and when freeze plan is executed */
7306 if (freeze_xmin)
7307 {
7310 pagefrz->FreezePageConflictXid = xid;
7311 }
7312 }
7313
7314 /*
7315 * Old-style VACUUM FULL is gone, but we have to process xvac for as long
7316 * as we support having MOVED_OFF/MOVED_IN tuples in the database
7317 */
7318 xid = HeapTupleHeaderGetXvac(tuple);
7319 if (TransactionIdIsNormal(xid))
7320 {
7322 Assert(TransactionIdPrecedes(xid, cutoffs->OldestXmin));
7323
7324 /*
7325 * For Xvac, we always freeze proactively. This allows totally_frozen
7326 * tracking to ignore xvac.
7327 */
7328 replace_xvac = pagefrz->freeze_required = true;
7329
7331 pagefrz->FreezePageConflictXid = xid;
7332
7333 /* Will set replace_xvac flags in freeze plan below */
7334 }
7335
7336 /* Now process xmax */
7337 xid = frz->xmax;
7338 if (tuple->t_infomask & HEAP_XMAX_IS_MULTI)
7339 {
7340 /* Raw xmax is a MultiXactId */
7342 uint16 flags;
7343
7344 /*
7345 * We will either remove xmax completely (in the "freeze_xmax" path),
7346 * process xmax by replacing it (in the "replace_xmax" path), or
7347 * perform no-op xmax processing. The only constraint is that the
7348 * FreezeLimit/MultiXactCutoff postcondition must never be violated.
7349 */
7350 newxmax = FreezeMultiXactId(xid, tuple->t_infomask, cutoffs,
7351 &flags, pagefrz);
7352
7353 if (flags & FRM_NOOP)
7354 {
7355 /*
7356 * xmax is a MultiXactId, and nothing about it changes for now.
7357 * This is the only case where 'freeze_required' won't have been
7358 * set for us by FreezeMultiXactId, as well as the only case where
7359 * neither freeze_xmax nor replace_xmax are set (given a multi).
7360 *
7361 * This is a no-op, but the call to FreezeMultiXactId might have
7362 * ratcheted back NewRelfrozenXid and/or NewRelminMxid trackers
7363 * for us (the "freeze page" variants, specifically). That'll
7364 * make it safe for our caller to freeze the page later on, while
7365 * leaving this particular xmax undisturbed.
7366 *
7367 * FreezeMultiXactId is _not_ responsible for the "no freeze"
7368 * NewRelfrozenXid/NewRelminMxid trackers, though -- that's our
7369 * job. A call to heap_tuple_should_freeze for this same tuple
7370 * will take place below if 'freeze_required' isn't set already.
7371 * (This repeats work from FreezeMultiXactId, but allows "no
7372 * freeze" tracker maintenance to happen in only one place.)
7373 */
7376 }
7377 else if (flags & FRM_RETURN_IS_XID)
7378 {
7379 /*
7380 * xmax will become an updater Xid (original MultiXact's updater
7381 * member Xid will be carried forward as a simple Xid in Xmax).
7382 */
7384
7385 /*
7386 * NB -- some of these transformations are only valid because we
7387 * know the return Xid is a tuple updater (i.e. not merely a
7388 * locker.) Also note that the only reason we don't explicitly
7389 * worry about HEAP_KEYS_UPDATED is because it lives in
7390 * t_infomask2 rather than t_infomask.
7391 */
7392 frz->t_infomask &= ~HEAP_XMAX_BITS;
7393 frz->xmax = newxmax;
7394 if (flags & FRM_MARK_COMMITTED)
7395 frz->t_infomask |= HEAP_XMAX_COMMITTED;
7396 replace_xmax = true;
7397 }
7398 else if (flags & FRM_RETURN_IS_MULTI)
7399 {
7402
7403 /*
7404 * xmax is an old MultiXactId that we have to replace with a new
7405 * MultiXactId, to carry forward two or more original member XIDs.
7406 */
7408
7409 /*
7410 * We can't use GetMultiXactIdHintBits directly on the new multi
7411 * here; that routine initializes the masks to all zeroes, which
7412 * would lose other bits we need. Doing it this way ensures all
7413 * unrelated bits remain untouched.
7414 */
7415 frz->t_infomask &= ~HEAP_XMAX_BITS;
7416 frz->t_infomask2 &= ~HEAP_KEYS_UPDATED;
7418 frz->t_infomask |= newbits;
7419 frz->t_infomask2 |= newbits2;
7420 frz->xmax = newxmax;
7421 replace_xmax = true;
7422 }
7423 else
7424 {
7425 /*
7426 * Freeze plan for tuple "freezes xmax" in the strictest sense:
7427 * it'll leave nothing in xmax (neither an Xid nor a MultiXactId).
7428 */
7429 Assert(flags & FRM_INVALIDATE_XMAX);
7431
7432 /* Will set freeze_xmax flags in freeze plan below */
7433 freeze_xmax = true;
7434 }
7435
7436 /* MultiXactId processing forces freezing (barring FRM_NOOP case) */
7437 Assert(pagefrz->freeze_required || (!freeze_xmax && !replace_xmax));
7438 }
7439 else if (TransactionIdIsNormal(xid))
7440 {
7441 /* Raw xmax is normal XID */
7442 if (TransactionIdPrecedes(xid, cutoffs->relfrozenxid))
7443 ereport(ERROR,
7445 errmsg_internal("found xmax %u from before relfrozenxid %u",
7446 xid, cutoffs->relfrozenxid)));
7447
7448 /* Will set freeze_xmax flags in freeze plan below */
7450
7451 /*
7452 * Verify that xmax aborted if and when freeze plan is executed,
7453 * provided it's from an update. (A lock-only xmax can be removed
7454 * independent of this, since the lock is released at xact end.)
7455 */
7457 frz->checkflags |= HEAP_FREEZE_CHECK_XMAX_ABORTED;
7458 }
7459 else if (!TransactionIdIsValid(xid))
7460 {
7461 /* Raw xmax is InvalidTransactionId XID */
7462 Assert((tuple->t_infomask & HEAP_XMAX_IS_MULTI) == 0);
7463 xmax_already_frozen = true;
7464 }
7465 else
7466 ereport(ERROR,
7468 errmsg_internal("found raw xmax %u (infomask 0x%04x) not invalid and not multi",
7469 xid, tuple->t_infomask)));
7470
7471 if (freeze_xmin)
7472 {
7474
7475 frz->t_infomask |= HEAP_XMIN_FROZEN;
7476 }
7477 if (replace_xvac)
7478 {
7479 /*
7480 * If a MOVED_OFF tuple is not dead, the xvac transaction must have
7481 * failed; whereas a non-dead MOVED_IN tuple must mean the xvac
7482 * transaction succeeded.
7483 */
7484 Assert(pagefrz->freeze_required);
7485 if (tuple->t_infomask & HEAP_MOVED_OFF)
7486 frz->frzflags |= XLH_INVALID_XVAC;
7487 else
7488 frz->frzflags |= XLH_FREEZE_XVAC;
7489 }
7490 if (replace_xmax)
7491 {
7493 Assert(pagefrz->freeze_required);
7494
7495 /* Already set replace_xmax flags in freeze plan earlier */
7496 }
7497 if (freeze_xmax)
7498 {
7500
7501 frz->xmax = InvalidTransactionId;
7502
7503 /*
7504 * The tuple might be marked either XMAX_INVALID or XMAX_COMMITTED +
7505 * LOCKED. Normalize to INVALID just to be sure no one gets confused.
7506 * Also get rid of the HEAP_KEYS_UPDATED bit.
7507 */
7508 frz->t_infomask &= ~HEAP_XMAX_BITS;
7509 frz->t_infomask |= HEAP_XMAX_INVALID;
7510 frz->t_infomask2 &= ~HEAP_HOT_UPDATED;
7511 frz->t_infomask2 &= ~HEAP_KEYS_UPDATED;
7512 }
7513
7514 /*
7515 * Determine if this tuple is already totally frozen, or will become
7516 * totally frozen (provided caller executes freeze plans for the page)
7517 */
7520
7521 if (!pagefrz->freeze_required && !(xmin_already_frozen &&
7523 {
7524 /*
7525 * So far no previous tuple from the page made freezing mandatory.
7526 * Does this tuple force caller to freeze the entire page?
7527 */
7528 pagefrz->freeze_required =
7529 heap_tuple_should_freeze(tuple, cutoffs,
7530 &pagefrz->NoFreezePageRelfrozenXid,
7531 &pagefrz->NoFreezePageRelminMxid);
7532 }
7533
7534 /* Tell caller if this tuple has a usable freeze plan set in *frz */
7536}
7537
7538/*
7539 * Perform xmin/xmax XID status sanity checks before actually executing freeze
7540 * plans.
7541 *
7542 * heap_prepare_freeze_tuple doesn't perform these checks directly because
7543 * pg_xact lookups are relatively expensive. They shouldn't be repeated by
7544 * successive VACUUMs that each decide against freezing the same page.
7545 */
7546void
7548 HeapTupleFreeze *tuples, int ntuples)
7549{
7550 Page page = BufferGetPage(buffer);
7551
7552 for (int i = 0; i < ntuples; i++)
7553 {
7554 HeapTupleFreeze *frz = tuples + i;
7555 ItemId itemid = PageGetItemId(page, frz->offset);
7556 HeapTupleHeader htup;
7557
7558 htup = (HeapTupleHeader) PageGetItem(page, itemid);
7559
7560 /* Deliberately avoid relying on tuple hint bits here */
7561 if (frz->checkflags & HEAP_FREEZE_CHECK_XMIN_COMMITTED)
7562 {
7564
7566 if (unlikely(!TransactionIdDidCommit(xmin)))
7567 ereport(ERROR,
7569 errmsg_internal("uncommitted xmin %u needs to be frozen",
7570 xmin)));
7571 }
7572
7573 /*
7574 * TransactionIdDidAbort won't work reliably in the presence of XIDs
7575 * left behind by transactions that were in progress during a crash,
7576 * so we can only check that xmax didn't commit
7577 */
7578 if (frz->checkflags & HEAP_FREEZE_CHECK_XMAX_ABORTED)
7579 {
7581
7584 ereport(ERROR,
7586 errmsg_internal("cannot freeze committed xmax %u",
7587 xmax)));
7588 }
7589 }
7590}
7591
7592/*
7593 * Helper which executes freezing of one or more heap tuples on a page on
7594 * behalf of caller. Caller passes an array of tuple plans from
7595 * heap_prepare_freeze_tuple. Caller must set 'offset' in each plan for us.
7596 * Must be called in a critical section that also marks the buffer dirty and,
7597 * if needed, emits WAL.
7598 */
7599void
7601{
7602 Page page = BufferGetPage(buffer);
7603
7604 for (int i = 0; i < ntuples; i++)
7605 {
7606 HeapTupleFreeze *frz = tuples + i;
7607 ItemId itemid = PageGetItemId(page, frz->offset);
7608 HeapTupleHeader htup;
7609
7610 htup = (HeapTupleHeader) PageGetItem(page, itemid);
7612 }
7613}
7614
7615/*
7616 * heap_freeze_tuple
7617 * Freeze tuple in place, without WAL logging.
7618 *
7619 * Useful for callers like CLUSTER that perform their own WAL logging.
7620 */
7621bool
7623 TransactionId relfrozenxid, TransactionId relminmxid,
7624 TransactionId FreezeLimit, TransactionId MultiXactCutoff)
7625{
7627 bool do_freeze;
7628 bool totally_frozen;
7629 struct VacuumCutoffs cutoffs;
7630 HeapPageFreeze pagefrz;
7631
7632 cutoffs.relfrozenxid = relfrozenxid;
7633 cutoffs.relminmxid = relminmxid;
7634 cutoffs.OldestXmin = FreezeLimit;
7635 cutoffs.OldestMxact = MultiXactCutoff;
7636 cutoffs.FreezeLimit = FreezeLimit;
7638
7639 pagefrz.freeze_required = true;
7640 pagefrz.FreezePageRelfrozenXid = FreezeLimit;
7641 pagefrz.FreezePageRelminMxid = MultiXactCutoff;
7642 pagefrz.FreezePageConflictXid = InvalidTransactionId;
7643 pagefrz.NoFreezePageRelfrozenXid = FreezeLimit;
7644 pagefrz.NoFreezePageRelminMxid = MultiXactCutoff;
7645
7646 do_freeze = heap_prepare_freeze_tuple(tuple, &cutoffs,
7647 &pagefrz, &frz, &totally_frozen);
7648
7649 /*
7650 * Note that because this is not a WAL-logged operation, we don't need to
7651 * fill in the offset in the freeze record.
7652 */
7653
7654 if (do_freeze)
7656 return do_freeze;
7657}
7658
7659/*
7660 * For a given MultiXactId, return the hint bits that should be set in the
7661 * tuple's infomask.
7662 *
7663 * Normally this should be called for a multixact that was just created, and
7664 * so is on our local cache, so the GetMembers call is fast.
7665 */
7666static void
7669{
7670 int nmembers;
7671 MultiXactMember *members;
7672 int i;
7674 uint16 bits2 = 0;
7675 bool has_update = false;
7677
7678 /*
7679 * We only use this in multis we just created, so they cannot be values
7680 * pre-pg_upgrade.
7681 */
7682 nmembers = GetMultiXactIdMembers(multi, &members, false, false);
7683
7684 for (i = 0; i < nmembers; i++)
7685 {
7687
7688 /*
7689 * Remember the strongest lock mode held by any member of the
7690 * multixact.
7691 */
7692 mode = TUPLOCK_from_mxstatus(members[i].status);
7693 if (mode > strongest)
7694 strongest = mode;
7695
7696 /* See what other bits we need */
7697 switch (members[i].status)
7698 {
7702 break;
7703
7706 break;
7707
7709 has_update = true;
7710 break;
7711
7714 has_update = true;
7715 break;
7716 }
7717 }
7718
7721 bits |= HEAP_XMAX_EXCL_LOCK;
7722 else if (strongest == LockTupleShare)
7723 bits |= HEAP_XMAX_SHR_LOCK;
7724 else if (strongest == LockTupleKeyShare)
7725 bits |= HEAP_XMAX_KEYSHR_LOCK;
7726
7727 if (!has_update)
7728 bits |= HEAP_XMAX_LOCK_ONLY;
7729
7730 if (nmembers > 0)
7731 pfree(members);
7732
7733 *new_infomask = bits;
7735}
7736
7737/*
7738 * MultiXactIdGetUpdateXid
7739 *
7740 * Given a multixact Xmax and corresponding infomask, which does not have the
7741 * HEAP_XMAX_LOCK_ONLY bit set, obtain and return the Xid of the updating
7742 * transaction.
7743 *
7744 * Caller is expected to check the status of the updating transaction, if
7745 * necessary.
7746 */
7747static TransactionId
7749{
7751 MultiXactMember *members;
7752 int nmembers;
7753
7754 Assert(!(t_infomask & HEAP_XMAX_LOCK_ONLY));
7755 Assert(t_infomask & HEAP_XMAX_IS_MULTI);
7756
7757 /*
7758 * Since we know the LOCK_ONLY bit is not set, this cannot be a multi from
7759 * pre-pg_upgrade.
7760 */
7761 nmembers = GetMultiXactIdMembers(xmax, &members, false, false);
7762
7763 if (nmembers > 0)
7764 {
7765 int i;
7766
7767 for (i = 0; i < nmembers; i++)
7768 {
7769 /* Ignore lockers */
7770 if (!ISUPDATE_from_mxstatus(members[i].status))
7771 continue;
7772
7773 /* there can be at most one updater */
7775 update_xact = members[i].xid;
7776#ifndef USE_ASSERT_CHECKING
7777
7778 /*
7779 * in an assert-enabled build, walk the whole array to ensure
7780 * there's no other updater.
7781 */
7782 break;
7783#endif
7784 }
7785
7786 pfree(members);
7787 }
7788
7789 return update_xact;
7790}
7791
7792/*
7793 * HeapTupleGetUpdateXid
7794 * As above, but use a HeapTupleHeader
7795 *
7796 * See also HeapTupleHeaderGetUpdateXid, which can be used without previously
7797 * checking the hint bits.
7798 */
7805
7806/*
7807 * Does the given multixact conflict with the current transaction grabbing a
7808 * tuple lock of the given strength?
7809 *
7810 * The passed infomask pairs up with the given multixact in the tuple header.
7811 *
7812 * If current_is_member is not NULL, it is set to 'true' if the current
7813 * transaction is a member of the given multixact.
7814 */
7815static bool
7817 LockTupleMode lockmode, bool *current_is_member)
7818{
7819 int nmembers;
7820 MultiXactMember *members;
7821 bool result = false;
7822 LOCKMODE wanted = tupleLockExtraInfo[lockmode].hwlock;
7823
7825 return false;
7826
7827 nmembers = GetMultiXactIdMembers(multi, &members, false,
7829 if (nmembers >= 0)
7830 {
7831 int i;
7832
7833 for (i = 0; i < nmembers; i++)
7834 {
7837
7839 break;
7840
7841 memlockmode = LOCKMODE_from_mxstatus(members[i].status);
7842
7843 /* ignore members from current xact (but track their presence) */
7844 memxid = members[i].xid;
7846 {
7847 if (current_is_member != NULL)
7848 *current_is_member = true;
7849 continue;
7850 }
7851 else if (result)
7852 continue;
7853
7854 /* ignore members that don't conflict with the lock we want */
7856 continue;
7857
7858 if (ISUPDATE_from_mxstatus(members[i].status))
7859 {
7860 /* ignore aborted updaters */
7862 continue;
7863 }
7864 else
7865 {
7866 /* ignore lockers-only that are no longer in progress */
7868 continue;
7869 }
7870
7871 /*
7872 * Whatever remains are either live lockers that conflict with our
7873 * wanted lock, and updaters that are not aborted. Those conflict
7874 * with what we want. Set up to return true, but keep going to
7875 * look for the current transaction among the multixact members,
7876 * if needed.
7877 */
7878 result = true;
7879 }
7880 pfree(members);
7881 }
7882
7883 return result;
7884}
7885
7886/*
7887 * Do_MultiXactIdWait
7888 * Actual implementation for the two functions below.
7889 *
7890 * 'multi', 'status' and 'infomask' indicate what to sleep on (the status is
7891 * needed to ensure we only sleep on conflicting members, and the infomask is
7892 * used to optimize multixact access in case it's a lock-only multi); 'nowait'
7893 * indicates whether to use conditional lock acquisition, to allow callers to
7894 * fail if lock is unavailable. 'rel', 'ctid' and 'oper' are used to set up
7895 * context information for error messages. 'remaining', if not NULL, receives
7896 * the number of members that are still running, including any (non-aborted)
7897 * subtransactions of our own transaction. 'logLockFailure' indicates whether
7898 * to log details when a lock acquisition fails with 'nowait' enabled.
7899 *
7900 * We do this by sleeping on each member using XactLockTableWait. Any
7901 * members that belong to the current backend are *not* waited for, however;
7902 * this would not merely be useless but would lead to Assert failure inside
7903 * XactLockTableWait. By the time this returns, it is certain that all
7904 * transactions *of other backends* that were members of the MultiXactId
7905 * that conflict with the requested status are dead (and no new ones can have
7906 * been added, since it is not legal to add members to an existing
7907 * MultiXactId).
7908 *
7909 * But by the time we finish sleeping, someone else may have changed the Xmax
7910 * of the containing tuple, so the caller needs to iterate on us somehow.
7911 *
7912 * Note that in case we return false, the number of remaining members is
7913 * not to be trusted.
7914 */
7915static bool
7917 uint16 infomask, bool nowait,
7918 Relation rel, const ItemPointerData *ctid, XLTW_Oper oper,
7919 int *remaining, bool logLockFailure)
7920{
7921 bool result = true;
7922 MultiXactMember *members;
7923 int nmembers;
7924 int remain = 0;
7925
7926 /* for pre-pg_upgrade tuples, no need to sleep at all */
7927 nmembers = HEAP_LOCKED_UPGRADED(infomask) ? -1 :
7928 GetMultiXactIdMembers(multi, &members, false,
7930
7931 if (nmembers >= 0)
7932 {
7933 int i;
7934
7935 for (i = 0; i < nmembers; i++)
7936 {
7937 TransactionId memxid = members[i].xid;
7938 MultiXactStatus memstatus = members[i].status;
7939
7941 {
7942 remain++;
7943 continue;
7944 }
7945
7947 LOCKMODE_from_mxstatus(status)))
7948 {
7950 remain++;
7951 continue;
7952 }
7953
7954 /*
7955 * This member conflicts with our multi, so we have to sleep (or
7956 * return failure, if asked to avoid waiting.)
7957 *
7958 * Note that we don't set up an error context callback ourselves,
7959 * but instead we pass the info down to XactLockTableWait. This
7960 * might seem a bit wasteful because the context is set up and
7961 * tore down for each member of the multixact, but in reality it
7962 * should be barely noticeable, and it avoids duplicate code.
7963 */
7964 if (nowait)
7965 {
7967 if (!result)
7968 break;
7969 }
7970 else
7971 XactLockTableWait(memxid, rel, ctid, oper);
7972 }
7973
7974 pfree(members);
7975 }
7976
7977 if (remaining)
7978 *remaining = remain;
7979
7980 return result;
7981}
7982
7983/*
7984 * MultiXactIdWait
7985 * Sleep on a MultiXactId.
7986 *
7987 * By the time we finish sleeping, someone else may have changed the Xmax
7988 * of the containing tuple, so the caller needs to iterate on us somehow.
7989 *
7990 * We return (in *remaining, if not NULL) the number of members that are still
7991 * running, including any (non-aborted) subtransactions of our own transaction.
7992 */
7993static void
7995 Relation rel, const ItemPointerData *ctid, XLTW_Oper oper,
7996 int *remaining)
7997{
7998 (void) Do_MultiXactIdWait(multi, status, infomask, false,
7999 rel, ctid, oper, remaining, false);
8000}
8001
8002/*
8003 * ConditionalMultiXactIdWait
8004 * As above, but only lock if we can get the lock without blocking.
8005 *
8006 * By the time we finish sleeping, someone else may have changed the Xmax
8007 * of the containing tuple, so the caller needs to iterate on us somehow.
8008 *
8009 * If the multixact is now all gone, return true. Returns false if some
8010 * transactions might still be running.
8011 *
8012 * We return (in *remaining, if not NULL) the number of members that are still
8013 * running, including any (non-aborted) subtransactions of our own transaction.
8014 */
8015static bool
8017 uint16 infomask, Relation rel, int *remaining,
8018 bool logLockFailure)
8019{
8020 return Do_MultiXactIdWait(multi, status, infomask, true,
8022}
8023
8024/*
8025 * heap_tuple_needs_eventual_freeze
8026 *
8027 * Check to see whether any of the XID fields of a tuple (xmin, xmax, xvac)
8028 * will eventually require freezing (if tuple isn't removed by pruning first).
8029 */
8030bool
8032{
8033 TransactionId xid;
8034
8035 /*
8036 * If xmin is a normal transaction ID, this tuple is definitely not
8037 * frozen.
8038 */
8039 xid = HeapTupleHeaderGetXmin(tuple);
8040 if (TransactionIdIsNormal(xid))
8041 return true;
8042
8043 /*
8044 * If xmax is a valid xact or multixact, this tuple is also not frozen.
8045 */
8046 if (tuple->t_infomask & HEAP_XMAX_IS_MULTI)
8047 {
8048 MultiXactId multi;
8049
8050 multi = HeapTupleHeaderGetRawXmax(tuple);
8051 if (MultiXactIdIsValid(multi))
8052 return true;
8053 }
8054 else
8055 {
8056 xid = HeapTupleHeaderGetRawXmax(tuple);
8057 if (TransactionIdIsNormal(xid))
8058 return true;
8059 }
8060
8061 if (tuple->t_infomask & HEAP_MOVED)
8062 {
8063 xid = HeapTupleHeaderGetXvac(tuple);
8064 if (TransactionIdIsNormal(xid))
8065 return true;
8066 }
8067
8068 return false;
8069}
8070
8071/*
8072 * heap_tuple_should_freeze
8073 *
8074 * Return value indicates if heap_prepare_freeze_tuple sibling function would
8075 * (or should) force freezing of the heap page that contains caller's tuple.
8076 * Tuple header XIDs/MXIDs < FreezeLimit/MultiXactCutoff trigger freezing.
8077 * This includes (xmin, xmax, xvac) fields, as well as MultiXact member XIDs.
8078 *
8079 * The *NoFreezePageRelfrozenXid and *NoFreezePageRelminMxid input/output
8080 * arguments help VACUUM track the oldest extant XID/MXID remaining in rel.
8081 * Our working assumption is that caller won't decide to freeze this tuple.
8082 * It's up to caller to only ratchet back its own top-level trackers after the
8083 * point that it fully commits to not freezing the tuple/page in question.
8084 */
8085bool
8087 const struct VacuumCutoffs *cutoffs,
8088 TransactionId *NoFreezePageRelfrozenXid,
8089 MultiXactId *NoFreezePageRelminMxid)
8090{
8091 TransactionId xid;
8092 MultiXactId multi;
8093 bool freeze = false;
8094
8095 /* First deal with xmin */
8096 xid = HeapTupleHeaderGetXmin(tuple);
8097 if (TransactionIdIsNormal(xid))
8098 {
8100 if (TransactionIdPrecedes(xid, *NoFreezePageRelfrozenXid))
8101 *NoFreezePageRelfrozenXid = xid;
8102 if (TransactionIdPrecedes(xid, cutoffs->FreezeLimit))
8103 freeze = true;
8104 }
8105
8106 /* Now deal with xmax */
8108 multi = InvalidMultiXactId;
8109 if (tuple->t_infomask & HEAP_XMAX_IS_MULTI)
8110 multi = HeapTupleHeaderGetRawXmax(tuple);
8111 else
8112 xid = HeapTupleHeaderGetRawXmax(tuple);
8113
8114 if (TransactionIdIsNormal(xid))
8115 {
8117 /* xmax is a non-permanent XID */
8118 if (TransactionIdPrecedes(xid, *NoFreezePageRelfrozenXid))
8119 *NoFreezePageRelfrozenXid = xid;
8120 if (TransactionIdPrecedes(xid, cutoffs->FreezeLimit))
8121 freeze = true;
8122 }
8123 else if (!MultiXactIdIsValid(multi))
8124 {
8125 /* xmax is a permanent XID or invalid MultiXactId/XID */
8126 }
8127 else if (HEAP_LOCKED_UPGRADED(tuple->t_infomask))
8128 {
8129 /* xmax is a pg_upgrade'd MultiXact, which can't have updater XID */
8130 if (MultiXactIdPrecedes(multi, *NoFreezePageRelminMxid))
8131 *NoFreezePageRelminMxid = multi;
8132 /* heap_prepare_freeze_tuple always freezes pg_upgrade'd xmax */
8133 freeze = true;
8134 }
8135 else
8136 {
8137 /* xmax is a MultiXactId that may have an updater XID */
8138 MultiXactMember *members;
8139 int nmembers;
8140
8142 if (MultiXactIdPrecedes(multi, *NoFreezePageRelminMxid))
8143 *NoFreezePageRelminMxid = multi;
8144 if (MultiXactIdPrecedes(multi, cutoffs->MultiXactCutoff))
8145 freeze = true;
8146
8147 /* need to check whether any member of the mxact is old */
8148 nmembers = GetMultiXactIdMembers(multi, &members, false,
8150
8151 for (int i = 0; i < nmembers; i++)
8152 {
8153 xid = members[i].xid;
8155 if (TransactionIdPrecedes(xid, *NoFreezePageRelfrozenXid))
8156 *NoFreezePageRelfrozenXid = xid;
8157 if (TransactionIdPrecedes(xid, cutoffs->FreezeLimit))
8158 freeze = true;
8159 }
8160 if (nmembers > 0)
8161 pfree(members);
8162 }
8163
8164 if (tuple->t_infomask & HEAP_MOVED)
8165 {
8166 xid = HeapTupleHeaderGetXvac(tuple);
8167 if (TransactionIdIsNormal(xid))
8168 {
8170 if (TransactionIdPrecedes(xid, *NoFreezePageRelfrozenXid))
8171 *NoFreezePageRelfrozenXid = xid;
8172 /* heap_prepare_freeze_tuple forces xvac freezing */
8173 freeze = true;
8174 }
8175 }
8176
8177 return freeze;
8178}
8179
8180/*
8181 * Maintain snapshotConflictHorizon for caller by ratcheting forward its value
8182 * using any committed XIDs contained in 'tuple', an obsolescent heap tuple
8183 * that caller is in the process of physically removing, e.g. via HOT pruning
8184 * or index deletion.
8185 *
8186 * Caller must initialize its value to InvalidTransactionId, which is
8187 * generally interpreted as "definitely no need for a recovery conflict".
8188 * Final value must reflect all heap tuples that caller will physically remove
8189 * (or remove TID references to) via its ongoing pruning/deletion operation.
8190 * ResolveRecoveryConflictWithSnapshot() is passed the final value (taken from
8191 * caller's WAL record) by REDO routine when it replays caller's operation.
8192 */
8193void
8195 TransactionId *snapshotConflictHorizon)
8196{
8200
8201 if (tuple->t_infomask & HEAP_MOVED)
8202 {
8203 if (TransactionIdPrecedes(*snapshotConflictHorizon, xvac))
8204 *snapshotConflictHorizon = xvac;
8205 }
8206
8207 /*
8208 * Ignore tuples inserted by an aborted transaction or if the tuple was
8209 * updated/deleted by the inserting transaction.
8210 *
8211 * Look for a committed hint bit, or if no xmin bit is set, check clog.
8212 */
8213 if (HeapTupleHeaderXminCommitted(tuple) ||
8215 {
8216 if (xmax != xmin &&
8217 TransactionIdFollows(xmax, *snapshotConflictHorizon))
8218 *snapshotConflictHorizon = xmax;
8219 }
8220}
8221
8222#ifdef USE_PREFETCH
8223/*
8224 * Helper function for heap_index_delete_tuples. Issues prefetch requests for
8225 * prefetch_count buffers. The prefetch_state keeps track of all the buffers
8226 * we can prefetch, and which have already been prefetched; each call to this
8227 * function picks up where the previous call left off.
8228 *
8229 * Note: we expect the deltids array to be sorted in an order that groups TIDs
8230 * by heap block, with all TIDs for each block appearing together in exactly
8231 * one group.
8232 */
8233static void
8236 int prefetch_count)
8237{
8239 int count = 0;
8240 int i;
8241 int ndeltids = prefetch_state->ndeltids;
8242 TM_IndexDelete *deltids = prefetch_state->deltids;
8243
8244 for (i = prefetch_state->next_item;
8245 i < ndeltids && count < prefetch_count;
8246 i++)
8247 {
8248 ItemPointer htid = &deltids[i].tid;
8249
8252 {
8255 count++;
8256 }
8257 }
8258
8259 /*
8260 * Save the prefetch position so that next time we can continue from that
8261 * position.
8262 */
8263 prefetch_state->next_item = i;
8264 prefetch_state->cur_hblkno = cur_hblkno;
8265}
8266#endif
8267
8268/*
8269 * Helper function for heap_index_delete_tuples. Checks for index corruption
8270 * involving an invalid TID in index AM caller's index page.
8271 *
8272 * This is an ideal place for these checks. The index AM must hold a buffer
8273 * lock on the index page containing the TIDs we examine here, so we don't
8274 * have to worry about concurrent VACUUMs at all. We can be sure that the
8275 * index is corrupt when htid points directly to an LP_UNUSED item or
8276 * heap-only tuple, which is not the case during standard index scans.
8277 */
8278static inline void
8280 Page page, OffsetNumber maxoff,
8282{
8284 ItemId iid;
8285
8286 Assert(OffsetNumberIsValid(istatus->idxoffnum));
8287
8288 if (unlikely(indexpagehoffnum > maxoff))
8289 ereport(ERROR,
8291 errmsg_internal("heap tid from index tuple (%u,%u) points past end of heap page line pointer array at offset %u of block %u in index \"%s\"",
8294 istatus->idxoffnum, delstate->iblknum,
8296
8298 if (unlikely(!ItemIdIsUsed(iid)))
8299 ereport(ERROR,
8301 errmsg_internal("heap tid from index tuple (%u,%u) points to unused heap page item at offset %u of block %u in index \"%s\"",
8304 istatus->idxoffnum, delstate->iblknum,
8306
8307 if (ItemIdHasStorage(iid))
8308 {
8309 HeapTupleHeader htup;
8310
8312 htup = (HeapTupleHeader) PageGetItem(page, iid);
8313
8315 ereport(ERROR,
8317 errmsg_internal("heap tid from index tuple (%u,%u) points to heap-only tuple at offset %u of block %u in index \"%s\"",
8320 istatus->idxoffnum, delstate->iblknum,
8322 }
8323}
8324
8325/*
8326 * heapam implementation of tableam's index_delete_tuples interface.
8327 *
8328 * This helper function is called by index AMs during index tuple deletion.
8329 * See tableam header comments for an explanation of the interface implemented
8330 * here and a general theory of operation. Note that each call here is either
8331 * a simple index deletion call, or a bottom-up index deletion call.
8332 *
8333 * It's possible for this to generate a fair amount of I/O, since we may be
8334 * deleting hundreds of tuples from a single index block. To amortize that
8335 * cost to some degree, this uses prefetching and combines repeat accesses to
8336 * the same heap block.
8337 */
8340{
8341 /* Initial assumption is that earlier pruning took care of conflict */
8342 TransactionId snapshotConflictHorizon = InvalidTransactionId;
8345 Page page = NULL;
8348#ifdef USE_PREFETCH
8351#endif
8353 int finalndeltids = 0,
8354 nblocksaccessed = 0;
8355
8356 /* State that's only used in bottom-up index deletion case */
8357 int nblocksfavorable = 0;
8358 int curtargetfreespace = delstate->bottomupfreespace,
8359 lastfreespace = 0,
8360 actualfreespace = 0;
8361 bool bottomup_final_block = false;
8362
8364
8365 /* Sort caller's deltids array by TID for further processing */
8367
8368 /*
8369 * Bottom-up case: resort deltids array in an order attuned to where the
8370 * greatest number of promising TIDs are to be found, and determine how
8371 * many blocks from the start of sorted array should be considered
8372 * favorable. This will also shrink the deltids array in order to
8373 * eliminate completely unfavorable blocks up front.
8374 */
8375 if (delstate->bottomup)
8377
8378#ifdef USE_PREFETCH
8379 /* Initialize prefetch state. */
8381 prefetch_state.next_item = 0;
8382 prefetch_state.ndeltids = delstate->ndeltids;
8383 prefetch_state.deltids = delstate->deltids;
8384
8385 /*
8386 * Determine the prefetch distance that we will attempt to maintain.
8387 *
8388 * Since the caller holds a buffer lock somewhere in rel, we'd better make
8389 * sure that isn't a catalog relation before we call code that does
8390 * syscache lookups, to avoid risk of deadlock.
8391 */
8392 if (IsCatalogRelation(rel))
8394 else
8397
8398 /* Cap initial prefetch distance for bottom-up deletion caller */
8399 if (delstate->bottomup)
8400 {
8404 }
8405
8406 /* Start prefetching. */
8408#endif
8409
8410 /* Iterate over deltids, determine which to delete, check their horizon */
8411 Assert(delstate->ndeltids > 0);
8412 for (int i = 0; i < delstate->ndeltids; i++)
8413 {
8414 TM_IndexDelete *ideltid = &delstate->deltids[i];
8415 TM_IndexStatus *istatus = delstate->status + ideltid->id;
8416 ItemPointer htid = &ideltid->tid;
8417 OffsetNumber offnum;
8418
8419 /*
8420 * Read buffer, and perform required extra steps each time a new block
8421 * is encountered. Avoid refetching if it's the same block as the one
8422 * from the last htid.
8423 */
8424 if (blkno == InvalidBlockNumber ||
8426 {
8427 /*
8428 * Consider giving up early for bottom-up index deletion caller
8429 * first. (Only prefetch next-next block afterwards, when it
8430 * becomes clear that we're at least going to access the next
8431 * block in line.)
8432 *
8433 * Sometimes the first block frees so much space for bottom-up
8434 * caller that the deletion process can end without accessing any
8435 * more blocks. It is usually necessary to access 2 or 3 blocks
8436 * per bottom-up deletion operation, though.
8437 */
8438 if (delstate->bottomup)
8439 {
8440 /*
8441 * We often allow caller to delete a few additional items
8442 * whose entries we reached after the point that space target
8443 * from caller was satisfied. The cost of accessing the page
8444 * was already paid at that point, so it made sense to finish
8445 * it off. When that happened, we finalize everything here
8446 * (by finishing off the whole bottom-up deletion operation
8447 * without needlessly paying the cost of accessing any more
8448 * blocks).
8449 */
8451 break;
8452
8453 /*
8454 * Give up when we didn't enable our caller to free any
8455 * additional space as a result of processing the page that we
8456 * just finished up with. This rule is the main way in which
8457 * we keep the cost of bottom-up deletion under control.
8458 */
8460 break;
8461 lastfreespace = actualfreespace; /* for next time */
8462
8463 /*
8464 * Deletion operation (which is bottom-up) will definitely
8465 * access the next block in line. Prepare for that now.
8466 *
8467 * Decay target free space so that we don't hang on for too
8468 * long with a marginal case. (Space target is only truly
8469 * helpful when it allows us to recognize that we don't need
8470 * to access more than 1 or 2 blocks to satisfy caller due to
8471 * agreeable workload characteristics.)
8472 *
8473 * We are a bit more patient when we encounter contiguous
8474 * blocks, though: these are treated as favorable blocks. The
8475 * decay process is only applied when the next block in line
8476 * is not a favorable/contiguous block. This is not an
8477 * exception to the general rule; we still insist on finding
8478 * at least one deletable item per block accessed. See
8479 * bottomup_nblocksfavorable() for full details of the theory
8480 * behind favorable blocks and heap block locality in general.
8481 *
8482 * Note: The first block in line is always treated as a
8483 * favorable block, so the earliest possible point that the
8484 * decay can be applied is just before we access the second
8485 * block in line. The Assert() verifies this for us.
8486 */
8488 if (nblocksfavorable > 0)
8490 else
8491 curtargetfreespace /= 2;
8492 }
8493
8494 /* release old buffer */
8495 if (BufferIsValid(buf))
8497
8499 buf = ReadBuffer(rel, blkno);
8501 Assert(!delstate->bottomup ||
8503
8504#ifdef USE_PREFETCH
8505
8506 /*
8507 * To maintain the prefetch distance, prefetch one more page for
8508 * each page we read.
8509 */
8511#endif
8512
8514
8515 page = BufferGetPage(buf);
8516 maxoff = PageGetMaxOffsetNumber(page);
8517 }
8518
8519 /*
8520 * In passing, detect index corruption involving an index page with a
8521 * TID that points to a location in the heap that couldn't possibly be
8522 * correct. We only do this with actual TIDs from caller's index page
8523 * (not items reached by traversing through a HOT chain).
8524 */
8526
8527 if (istatus->knowndeletable)
8528 Assert(!delstate->bottomup && !istatus->promising);
8529 else
8530 {
8531 ItemPointerData tmp = *htid;
8533
8534 /* Are any tuples from this HOT chain non-vacuumable? */
8536 &heapTuple, NULL, true))
8537 continue; /* can't delete entry */
8538
8539 /* Caller will delete, since whole HOT chain is vacuumable */
8540 istatus->knowndeletable = true;
8541
8542 /* Maintain index free space info for bottom-up deletion case */
8543 if (delstate->bottomup)
8544 {
8545 Assert(istatus->freespace > 0);
8546 actualfreespace += istatus->freespace;
8548 bottomup_final_block = true;
8549 }
8550 }
8551
8552 /*
8553 * Maintain snapshotConflictHorizon value for deletion operation as a
8554 * whole by advancing current value using heap tuple headers. This is
8555 * loosely based on the logic for pruning a HOT chain.
8556 */
8558 priorXmax = InvalidTransactionId; /* cannot check first XMIN */
8559 for (;;)
8560 {
8561 ItemId lp;
8562 HeapTupleHeader htup;
8563
8564 /* Sanity check (pure paranoia) */
8565 if (offnum < FirstOffsetNumber)
8566 break;
8567
8568 /*
8569 * An offset past the end of page's line pointer array is possible
8570 * when the array was truncated
8571 */
8572 if (offnum > maxoff)
8573 break;
8574
8575 lp = PageGetItemId(page, offnum);
8577 {
8578 offnum = ItemIdGetRedirect(lp);
8579 continue;
8580 }
8581
8582 /*
8583 * We'll often encounter LP_DEAD line pointers (especially with an
8584 * entry marked knowndeletable by our caller up front). No heap
8585 * tuple headers get examined for an htid that leads us to an
8586 * LP_DEAD item. This is okay because the earlier pruning
8587 * operation that made the line pointer LP_DEAD in the first place
8588 * must have considered the original tuple header as part of
8589 * generating its own snapshotConflictHorizon value.
8590 *
8591 * Relying on XLOG_HEAP2_PRUNE_VACUUM_SCAN records like this is
8592 * the same strategy that index vacuuming uses in all cases. Index
8593 * VACUUM WAL records don't even have a snapshotConflictHorizon
8594 * field of their own for this reason.
8595 */
8596 if (!ItemIdIsNormal(lp))
8597 break;
8598
8599 htup = (HeapTupleHeader) PageGetItem(page, lp);
8600
8601 /*
8602 * Check the tuple XMIN against prior XMAX, if any
8603 */
8606 break;
8607
8609 &snapshotConflictHorizon);
8610
8611 /*
8612 * If the tuple is not HOT-updated, then we are at the end of this
8613 * HOT-chain. No need to visit later tuples from the same update
8614 * chain (they get their own index entries) -- just move on to
8615 * next htid from index AM caller.
8616 */
8617 if (!HeapTupleHeaderIsHotUpdated(htup))
8618 break;
8619
8620 /* Advance to next HOT chain member */
8621 Assert(ItemPointerGetBlockNumber(&htup->t_ctid) == blkno);
8622 offnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
8624 }
8625
8626 /* Enable further/final shrinking of deltids for caller */
8627 finalndeltids = i + 1;
8628 }
8629
8631
8632 /*
8633 * Shrink deltids array to exclude non-deletable entries at the end. This
8634 * is not just a minor optimization. Final deltids array size might be
8635 * zero for a bottom-up caller. Index AM is explicitly allowed to rely on
8636 * ndeltids being zero in all cases with zero total deletable entries.
8637 */
8638 Assert(finalndeltids > 0 || delstate->bottomup);
8639 delstate->ndeltids = finalndeltids;
8640
8641 return snapshotConflictHorizon;
8642}
8643
8644/*
8645 * Specialized inlineable comparison function for index_delete_sort()
8646 */
8647static inline int
8649{
8650 ItemPointer tid1 = &deltid1->tid;
8651 ItemPointer tid2 = &deltid2->tid;
8652
8653 {
8656
8657 if (blk1 != blk2)
8658 return (blk1 < blk2) ? -1 : 1;
8659 }
8660 {
8663
8664 if (pos1 != pos2)
8665 return (pos1 < pos2) ? -1 : 1;
8666 }
8667
8668 Assert(false);
8669
8670 return 0;
8671}
8672
8673/*
8674 * Sort deltids array from delstate by TID. This prepares it for further
8675 * processing by heap_index_delete_tuples().
8676 *
8677 * This operation becomes a noticeable consumer of CPU cycles with some
8678 * workloads, so we go to the trouble of specialization/micro optimization.
8679 * We use shellsort for this because it's easy to specialize, compiles to
8680 * relatively few instructions, and is adaptive to presorted inputs/subsets
8681 * (which are typical here).
8682 */
8683static void
8685{
8686 TM_IndexDelete *deltids = delstate->deltids;
8687 int ndeltids = delstate->ndeltids;
8688
8689 /*
8690 * Shellsort gap sequence (taken from Sedgewick-Incerpi paper).
8691 *
8692 * This implementation is fast with array sizes up to ~4500. This covers
8693 * all supported BLCKSZ values.
8694 */
8695 const int gaps[9] = {1968, 861, 336, 112, 48, 21, 7, 3, 1};
8696
8697 /* Think carefully before changing anything here -- keep swaps cheap */
8698 StaticAssertDecl(sizeof(TM_IndexDelete) <= 8,
8699 "element size exceeds 8 bytes");
8700
8701 for (size_t g = 0; g < lengthof(gaps); g++)
8702 {
8703 for (int hi = gaps[g], i = hi; i < ndeltids; i++)
8704 {
8705 TM_IndexDelete d = deltids[i];
8706 int j = i;
8707
8708 while (j >= hi && index_delete_sort_cmp(&deltids[j - hi], &d) >= 0)
8709 {
8710 deltids[j] = deltids[j - hi];
8711 j -= hi;
8712 }
8713 deltids[j] = d;
8714 }
8715 }
8716}
8717
8718/*
8719 * Returns how many blocks should be considered favorable/contiguous for a
8720 * bottom-up index deletion pass. This is a number of heap blocks that starts
8721 * from and includes the first block in line.
8722 *
8723 * There is always at least one favorable block during bottom-up index
8724 * deletion. In the worst case (i.e. with totally random heap blocks) the
8725 * first block in line (the only favorable block) can be thought of as a
8726 * degenerate array of contiguous blocks that consists of a single block.
8727 * heap_index_delete_tuples() will expect this.
8728 *
8729 * Caller passes blockgroups, a description of the final order that deltids
8730 * will be sorted in for heap_index_delete_tuples() bottom-up index deletion
8731 * processing. Note that deltids need not actually be sorted just yet (caller
8732 * only passes deltids to us so that we can interpret blockgroups).
8733 *
8734 * You might guess that the existence of contiguous blocks cannot matter much,
8735 * since in general the main factor that determines which blocks we visit is
8736 * the number of promising TIDs, which is a fixed hint from the index AM.
8737 * We're not really targeting the general case, though -- the actual goal is
8738 * to adapt our behavior to a wide variety of naturally occurring conditions.
8739 * The effects of most of the heuristics we apply are only noticeable in the
8740 * aggregate, over time and across many _related_ bottom-up index deletion
8741 * passes.
8742 *
8743 * Deeming certain blocks favorable allows heapam to recognize and adapt to
8744 * workloads where heap blocks visited during bottom-up index deletion can be
8745 * accessed contiguously, in the sense that each newly visited block is the
8746 * neighbor of the block that bottom-up deletion just finished processing (or
8747 * close enough to it). It will likely be cheaper to access more favorable
8748 * blocks sooner rather than later (e.g. in this pass, not across a series of
8749 * related bottom-up passes). Either way it is probably only a matter of time
8750 * (or a matter of further correlated version churn) before all blocks that
8751 * appear together as a single large batch of favorable blocks get accessed by
8752 * _some_ bottom-up pass. Large batches of favorable blocks tend to either
8753 * appear almost constantly or not even once (it all depends on per-index
8754 * workload characteristics).
8755 *
8756 * Note that the blockgroups sort order applies a power-of-two bucketing
8757 * scheme that creates opportunities for contiguous groups of blocks to get
8758 * batched together, at least with workloads that are naturally amenable to
8759 * being driven by heap block locality. This doesn't just enhance the spatial
8760 * locality of bottom-up heap block processing in the obvious way. It also
8761 * enables temporal locality of access, since sorting by heap block number
8762 * naturally tends to make the bottom-up processing order deterministic.
8763 *
8764 * Consider the following example to get a sense of how temporal locality
8765 * might matter: There is a heap relation with several indexes, each of which
8766 * is low to medium cardinality. It is subject to constant non-HOT updates.
8767 * The updates are skewed (in one part of the primary key, perhaps). None of
8768 * the indexes are logically modified by the UPDATE statements (if they were
8769 * then bottom-up index deletion would not be triggered in the first place).
8770 * Naturally, each new round of index tuples (for each heap tuple that gets a
8771 * heap_update() call) will have the same heap TID in each and every index.
8772 * Since these indexes are low cardinality and never get logically modified,
8773 * heapam processing during bottom-up deletion passes will access heap blocks
8774 * in approximately sequential order. Temporal locality of access occurs due
8775 * to bottom-up deletion passes behaving very similarly across each of the
8776 * indexes at any given moment. This keeps the number of buffer misses needed
8777 * to visit heap blocks to a minimum.
8778 */
8779static int
8781 TM_IndexDelete *deltids)
8782{
8783 int64 lastblock = -1;
8784 int nblocksfavorable = 0;
8785
8786 Assert(nblockgroups >= 1);
8788
8789 /*
8790 * We tolerate heap blocks that will be accessed only slightly out of
8791 * physical order. Small blips occur when a pair of almost-contiguous
8792 * blocks happen to fall into different buckets (perhaps due only to a
8793 * small difference in npromisingtids that the bucketing scheme didn't
8794 * quite manage to ignore). We effectively ignore these blips by applying
8795 * a small tolerance. The precise tolerance we use is a little arbitrary,
8796 * but it works well enough in practice.
8797 */
8798 for (int b = 0; b < nblockgroups; b++)
8799 {
8800 IndexDeleteCounts *group = blockgroups + b;
8801 TM_IndexDelete *firstdtid = deltids + group->ifirsttid;
8803
8804 if (lastblock != -1 &&
8807 break;
8808
8810 lastblock = block;
8811 }
8812
8813 /* Always indicate that there is at least 1 favorable block */
8815
8816 return nblocksfavorable;
8817}
8818
8819/*
8820 * qsort comparison function for bottomup_sort_and_shrink()
8821 */
8822static int
8823bottomup_sort_and_shrink_cmp(const void *arg1, const void *arg2)
8824{
8827
8828 /*
8829 * Most significant field is npromisingtids (which we invert the order of
8830 * so as to sort in desc order).
8831 *
8832 * Caller should have already normalized npromisingtids fields into
8833 * power-of-two values (buckets).
8834 */
8835 if (group1->npromisingtids > group2->npromisingtids)
8836 return -1;
8837 if (group1->npromisingtids < group2->npromisingtids)
8838 return 1;
8839
8840 /*
8841 * Tiebreak: desc ntids sort order.
8842 *
8843 * We cannot expect power-of-two values for ntids fields. We should
8844 * behave as if they were already rounded up for us instead.
8845 */
8846 if (group1->ntids != group2->ntids)
8847 {
8850
8851 if (ntids1 > ntids2)
8852 return -1;
8853 if (ntids1 < ntids2)
8854 return 1;
8855 }
8856
8857 /*
8858 * Tiebreak: asc offset-into-deltids-for-block (offset to first TID for
8859 * block in deltids array) order.
8860 *
8861 * This is equivalent to sorting in ascending heap block number order
8862 * (among otherwise equal subsets of the array). This approach allows us
8863 * to avoid accessing the out-of-line TID. (We rely on the assumption
8864 * that the deltids array was sorted in ascending heap TID order when
8865 * these offsets to the first TID from each heap block group were formed.)
8866 */
8867 if (group1->ifirsttid > group2->ifirsttid)
8868 return 1;
8869 if (group1->ifirsttid < group2->ifirsttid)
8870 return -1;
8871
8873
8874 return 0;
8875}
8876
8877/*
8878 * heap_index_delete_tuples() helper function for bottom-up deletion callers.
8879 *
8880 * Sorts deltids array in the order needed for useful processing by bottom-up
8881 * deletion. The array should already be sorted in TID order when we're
8882 * called. The sort process groups heap TIDs from deltids into heap block
8883 * groupings. Earlier/more-promising groups/blocks are usually those that are
8884 * known to have the most "promising" TIDs.
8885 *
8886 * Sets new size of deltids array (ndeltids) in state. deltids will only have
8887 * TIDs from the BOTTOMUP_MAX_NBLOCKS most promising heap blocks when we
8888 * return. This often means that deltids will be shrunk to a small fraction
8889 * of its original size (we eliminate many heap blocks from consideration for
8890 * caller up front).
8891 *
8892 * Returns the number of "favorable" blocks. See bottomup_nblocksfavorable()
8893 * for a definition and full details.
8894 */
8895static int
8897{
8901 int nblockgroups = 0;
8902 int ncopied = 0;
8903 int nblocksfavorable = 0;
8904
8905 Assert(delstate->bottomup);
8906 Assert(delstate->ndeltids > 0);
8907
8908 /* Calculate per-heap-block count of TIDs */
8910 for (int i = 0; i < delstate->ndeltids; i++)
8911 {
8912 TM_IndexDelete *ideltid = &delstate->deltids[i];
8913 TM_IndexStatus *istatus = delstate->status + ideltid->id;
8914 ItemPointer htid = &ideltid->tid;
8915 bool promising = istatus->promising;
8916
8917 if (curblock != ItemPointerGetBlockNumber(htid))
8918 {
8919 /* New block group */
8920 nblockgroups++;
8921
8923 !BlockNumberIsValid(curblock));
8924
8925 curblock = ItemPointerGetBlockNumber(htid);
8926 blockgroups[nblockgroups - 1].ifirsttid = i;
8927 blockgroups[nblockgroups - 1].ntids = 1;
8928 blockgroups[nblockgroups - 1].npromisingtids = 0;
8929 }
8930 else
8931 {
8932 blockgroups[nblockgroups - 1].ntids++;
8933 }
8934
8935 if (promising)
8936 blockgroups[nblockgroups - 1].npromisingtids++;
8937 }
8938
8939 /*
8940 * We're about ready to sort block groups to determine the optimal order
8941 * for visiting heap blocks. But before we do, round the number of
8942 * promising tuples for each block group up to the next power-of-two,
8943 * unless it is very low (less than 4), in which case we round up to 4.
8944 * npromisingtids is far too noisy to trust when choosing between a pair
8945 * of block groups that both have very low values.
8946 *
8947 * This scheme divides heap blocks/block groups into buckets. Each bucket
8948 * contains blocks that have _approximately_ the same number of promising
8949 * TIDs as each other. The goal is to ignore relatively small differences
8950 * in the total number of promising entries, so that the whole process can
8951 * give a little weight to heapam factors (like heap block locality)
8952 * instead. This isn't a trade-off, really -- we have nothing to lose. It
8953 * would be foolish to interpret small differences in npromisingtids
8954 * values as anything more than noise.
8955 *
8956 * We tiebreak on nhtids when sorting block group subsets that have the
8957 * same npromisingtids, but this has the same issues as npromisingtids,
8958 * and so nhtids is subject to the same power-of-two bucketing scheme. The
8959 * only reason that we don't fix nhtids in the same way here too is that
8960 * we'll need accurate nhtids values after the sort. We handle nhtids
8961 * bucketization dynamically instead (in the sort comparator).
8962 *
8963 * See bottomup_nblocksfavorable() for a full explanation of when and how
8964 * heap locality/favorable blocks can significantly influence when and how
8965 * heap blocks are accessed.
8966 */
8967 for (int b = 0; b < nblockgroups; b++)
8968 {
8969 IndexDeleteCounts *group = blockgroups + b;
8970
8971 /* Better off falling back on nhtids with low npromisingtids */
8972 if (group->npromisingtids <= 4)
8973 group->npromisingtids = 4;
8974 else
8975 group->npromisingtids =
8977 }
8978
8979 /* Sort groups and rearrange caller's deltids array */
8982 reordereddeltids = palloc(delstate->ndeltids * sizeof(TM_IndexDelete));
8983
8985 /* Determine number of favorable blocks at the start of final deltids */
8987 delstate->deltids);
8988
8989 for (int b = 0; b < nblockgroups; b++)
8990 {
8991 IndexDeleteCounts *group = blockgroups + b;
8992 TM_IndexDelete *firstdtid = delstate->deltids + group->ifirsttid;
8993
8995 sizeof(TM_IndexDelete) * group->ntids);
8996 ncopied += group->ntids;
8997 }
8998
8999 /* Copy final grouped and sorted TIDs back into start of caller's array */
9001 sizeof(TM_IndexDelete) * ncopied);
9002 delstate->ndeltids = ncopied;
9003
9006
9007 return nblocksfavorable;
9008}
9009
9010/*
9011 * Perform XLogInsert for a heap-update operation. Caller must already
9012 * have modified the buffer(s) and marked them dirty.
9013 */
9014static XLogRecPtr
9020 bool walLogical)
9021{
9025 uint8 info;
9027 uint16 prefixlen = 0,
9028 suffixlen = 0;
9030 Page page = BufferGetPage(newbuf);
9032 bool init;
9033 int bufflags;
9034
9035 /* Caller should not call me on a non-WAL-logged relation */
9037
9039
9041 info = XLOG_HEAP_HOT_UPDATE;
9042 else
9043 info = XLOG_HEAP_UPDATE;
9044
9045 /*
9046 * If the old and new tuple are on the same page, we only need to log the
9047 * parts of the new tuple that were changed. That saves on the amount of
9048 * WAL we need to write. Currently, we just count any unchanged bytes in
9049 * the beginning and end of the tuple. That's quick to check, and
9050 * perfectly covers the common case that only one field is updated.
9051 *
9052 * We could do this even if the old and new tuple are on different pages,
9053 * but only if we don't make a full-page image of the old page, which is
9054 * difficult to know in advance. Also, if the old tuple is corrupt for
9055 * some reason, it would allow the corruption to propagate the new page,
9056 * so it seems best to avoid. Under the general assumption that most
9057 * updates tend to create the new tuple version on the same page, there
9058 * isn't much to be gained by doing this across pages anyway.
9059 *
9060 * Skip this if we're taking a full-page image of the new page, as we
9061 * don't include the new tuple in the WAL record in that case. Also
9062 * disable if effective_wal_level='logical', as logical decoding needs to
9063 * be able to read the new tuple in whole from the WAL record alone.
9064 */
9065 if (oldbuf == newbuf && !need_tuple_data &&
9067 {
9068 char *oldp = (char *) oldtup->t_data + oldtup->t_data->t_hoff;
9069 char *newp = (char *) newtup->t_data + newtup->t_data->t_hoff;
9070 int oldlen = oldtup->t_len - oldtup->t_data->t_hoff;
9071 int newlen = newtup->t_len - newtup->t_data->t_hoff;
9072
9073 /* Check for common prefix between old and new tuple */
9074 for (prefixlen = 0; prefixlen < Min(oldlen, newlen); prefixlen++)
9075 {
9076 if (newp[prefixlen] != oldp[prefixlen])
9077 break;
9078 }
9079
9080 /*
9081 * Storing the length of the prefix takes 2 bytes, so we need to save
9082 * at least 3 bytes or there's no point.
9083 */
9084 if (prefixlen < 3)
9085 prefixlen = 0;
9086
9087 /* Same for suffix */
9089 {
9090 if (newp[newlen - suffixlen - 1] != oldp[oldlen - suffixlen - 1])
9091 break;
9092 }
9093 if (suffixlen < 3)
9094 suffixlen = 0;
9095 }
9096
9097 /* Prepare main WAL data chain */
9098 xlrec.flags = 0;
9103 if (prefixlen > 0)
9105 if (suffixlen > 0)
9107 if (need_tuple_data)
9108 {
9110 if (old_key_tuple)
9111 {
9112 if (reln->rd_rel->relreplident == REPLICA_IDENTITY_FULL)
9114 else
9116 }
9117 }
9118
9119 /* If new tuple is the single and first tuple on page... */
9122 {
9123 info |= XLOG_HEAP_INIT_PAGE;
9124 init = true;
9125 }
9126 else
9127 init = false;
9128
9129 /* Prepare WAL data for the old page */
9130 xlrec.old_offnum = ItemPointerGetOffsetNumber(&oldtup->t_self);
9131 xlrec.old_xmax = HeapTupleHeaderGetRawXmax(oldtup->t_data);
9132 xlrec.old_infobits_set = compute_infobits(oldtup->t_data->t_infomask,
9133 oldtup->t_data->t_infomask2);
9134
9135 /* Prepare WAL data for the new page */
9136 xlrec.new_offnum = ItemPointerGetOffsetNumber(&newtup->t_self);
9137 xlrec.new_xmax = HeapTupleHeaderGetRawXmax(newtup->t_data);
9138
9140 if (init)
9142 if (need_tuple_data)
9144
9146 if (oldbuf != newbuf)
9148
9150
9151 /*
9152 * Prepare WAL data for the new tuple.
9153 */
9154 if (prefixlen > 0 || suffixlen > 0)
9155 {
9156 if (prefixlen > 0 && suffixlen > 0)
9157 {
9161 sizeof(uint16) * 2);
9162 }
9163 else if (prefixlen > 0)
9164 {
9166 sizeof(uint16));
9167 }
9168 else
9169 {
9171 sizeof(uint16));
9172 }
9173 }
9174
9175 xlhdr.t_infomask2 = newtup->t_data->t_infomask2;
9176 xlhdr.t_infomask = newtup->t_data->t_infomask;
9177 xlhdr.t_hoff = newtup->t_data->t_hoff;
9179
9180 /*
9181 * PG73FORMAT: write bitmap [+ padding] [+ oid] + data
9182 *
9183 * The 'data' doesn't include the common prefix or suffix.
9184 */
9186 if (prefixlen == 0)
9187 {
9189 (char *) newtup->t_data + SizeofHeapTupleHeader,
9191 }
9192 else
9193 {
9194 /*
9195 * Have to write the null bitmap and data after the common prefix as
9196 * two separate rdata entries.
9197 */
9198 /* bitmap [+ padding] [+ oid] */
9199 if (newtup->t_data->t_hoff - SizeofHeapTupleHeader > 0)
9200 {
9202 (char *) newtup->t_data + SizeofHeapTupleHeader,
9203 newtup->t_data->t_hoff - SizeofHeapTupleHeader);
9204 }
9205
9206 /* data after common prefix */
9208 (char *) newtup->t_data + newtup->t_data->t_hoff + prefixlen,
9209 newtup->t_len - newtup->t_data->t_hoff - prefixlen - suffixlen);
9210 }
9211
9212 /* We need to log a tuple identity */
9214 {
9215 /* don't really need this, but its more comfy to decode */
9216 xlhdr_idx.t_infomask2 = old_key_tuple->t_data->t_infomask2;
9217 xlhdr_idx.t_infomask = old_key_tuple->t_data->t_infomask;
9218 xlhdr_idx.t_hoff = old_key_tuple->t_data->t_hoff;
9219
9221
9222 /* PG73FORMAT: write bitmap [+ padding] [+ oid] + data */
9225 }
9226
9227 /*
9228 * Register VM buffers. If the old and new heap pages' VM bits are on the
9229 * same VM page and both their VM bits were cleared, the caller passes
9230 * only vmbuffer_new (mirroring the heap page convention where block 0 =
9231 * new is always registered).
9232 */
9235
9238
9241
9242 /* filtering by origin on a row level is much more efficient */
9244
9245 recptr = XLogInsert(RM_HEAP_ID, info);
9246
9247 return recptr;
9248}
9249
9250/*
9251 * Perform XLogInsert of an XLOG_HEAP2_NEW_CID record
9252 *
9253 * This is only used when effective_wal_level is logical, and only for
9254 * catalog tuples.
9255 */
9256static XLogRecPtr
9258{
9260
9262 HeapTupleHeader hdr = tup->t_data;
9263
9264 Assert(ItemPointerIsValid(&tup->t_self));
9265 Assert(tup->t_tableOid != InvalidOid);
9266
9267 xlrec.top_xid = GetTopTransactionId();
9268 xlrec.target_locator = relation->rd_locator;
9269 xlrec.target_tid = tup->t_self;
9270
9271 /*
9272 * If the tuple got inserted & deleted in the same TX we definitely have a
9273 * combo CID, set cmin and cmax.
9274 */
9275 if (hdr->t_infomask & HEAP_COMBOCID)
9276 {
9279 xlrec.cmin = HeapTupleHeaderGetCmin(hdr);
9280 xlrec.cmax = HeapTupleHeaderGetCmax(hdr);
9281 xlrec.combocid = HeapTupleHeaderGetRawCommandId(hdr);
9282 }
9283 /* No combo CID, so only cmin or cmax can be set by this TX */
9284 else
9285 {
9286 /*
9287 * Tuple inserted.
9288 *
9289 * We need to check for LOCK ONLY because multixacts might be
9290 * transferred to the new tuple in case of FOR KEY SHARE updates in
9291 * which case there will be an xmax, although the tuple just got
9292 * inserted.
9293 */
9294 if (hdr->t_infomask & HEAP_XMAX_INVALID ||
9296 {
9298 xlrec.cmax = InvalidCommandId;
9299 }
9300 /* Tuple from a different tx updated or deleted. */
9301 else
9302 {
9303 xlrec.cmin = InvalidCommandId;
9305 }
9306 xlrec.combocid = InvalidCommandId;
9307 }
9308
9309 /*
9310 * Note that we don't need to register the buffer here, because this
9311 * operation does not modify the page. The insert/update/delete that
9312 * called us certainly did, but that's WAL-logged separately.
9313 */
9316
9317 /* will be looked at irrespective of origin */
9318
9320
9321 return recptr;
9322}
9323
9324/*
9325 * Build a heap tuple representing the configured REPLICA IDENTITY to represent
9326 * the old tuple in an UPDATE or DELETE.
9327 *
9328 * Returns NULL if there's no need to log an identity or if there's no suitable
9329 * key defined.
9330 *
9331 * Pass key_required true if any replica identity columns changed value, or if
9332 * any of them have any external data. Delete must always pass true.
9333 *
9334 * *copy is set to true if the returned tuple is a modified copy rather than
9335 * the same tuple that was passed in.
9336 */
9337static HeapTuple
9339 bool *copy)
9340{
9341 TupleDesc desc = RelationGetDescr(relation);
9342 char replident = relation->rd_rel->relreplident;
9345 bool nulls[MaxHeapAttributeNumber];
9347
9348 *copy = false;
9349
9350 if (!RelationIsLogicallyLogged(relation))
9351 return NULL;
9352
9353 if (replident == REPLICA_IDENTITY_NOTHING)
9354 return NULL;
9355
9356 if (replident == REPLICA_IDENTITY_FULL)
9357 {
9358 /*
9359 * When logging the entire old tuple, it very well could contain
9360 * toasted columns. If so, force them to be inlined.
9361 */
9362 if (HeapTupleHasExternal(tp))
9363 {
9364 *copy = true;
9365 tp = toast_flatten_tuple(tp, desc);
9366 }
9367 return tp;
9368 }
9369
9370 /* if the key isn't required and we're only logging the key, we're done */
9371 if (!key_required)
9372 return NULL;
9373
9374 /* find out the replica identity columns */
9377
9378 /*
9379 * If there's no defined replica identity columns, treat as !key_required.
9380 * (This case should not be reachable from heap_update, since that should
9381 * calculate key_required accurately. But heap_delete just passes
9382 * constant true for key_required, so we can hit this case in deletes.)
9383 */
9384 if (bms_is_empty(idattrs))
9385 return NULL;
9386
9387 /*
9388 * Construct a new tuple containing only the replica identity columns,
9389 * with nulls elsewhere. While we're at it, assert that the replica
9390 * identity columns aren't null.
9391 */
9392 heap_deform_tuple(tp, desc, values, nulls);
9393
9394 for (int i = 0; i < desc->natts; i++)
9395 {
9397 idattrs))
9398 Assert(!nulls[i]);
9399 else
9400 nulls[i] = true;
9401 }
9402
9403 key_tuple = heap_form_tuple(desc, values, nulls);
9404 *copy = true;
9405
9407
9408 /*
9409 * If the tuple, which by here only contains indexed columns, still has
9410 * toasted columns, force them to be inlined. This is somewhat unlikely
9411 * since there's limits on the size of indexed columns, so we don't
9412 * duplicate toast_flatten_tuple()s functionality in the above loop over
9413 * the indexed columns, even if it would be more efficient.
9414 */
9416 {
9418
9421 }
9422
9423 return key_tuple;
9424}
9425
9426/*
9427 * HeapCheckForSerializableConflictOut
9428 * We are reading a tuple. If it's not visible, there may be a
9429 * rw-conflict out with the inserter. Otherwise, if it is visible to us
9430 * but has been deleted, there may be a rw-conflict out with the deleter.
9431 *
9432 * We will determine the top level xid of the writing transaction with which
9433 * we may be in conflict, and ask CheckForSerializableConflictOut() to check
9434 * for overlap with our own transaction.
9435 *
9436 * This function should be called just about anywhere in heapam.c where a
9437 * tuple has been read. The caller must hold at least a shared lock on the
9438 * buffer, because this function might set hint bits on the tuple. There is
9439 * currently no known reason to call this function from an index AM.
9440 */
9441void
9443 HeapTuple tuple, Buffer buffer,
9444 Snapshot snapshot)
9445{
9446 TransactionId xid;
9448
9449 if (!CheckForSerializableConflictOutNeeded(relation, snapshot))
9450 return;
9451
9452 /*
9453 * Check to see whether the tuple has been written to by a concurrent
9454 * transaction, either to create it not visible to us, or to delete it
9455 * while it is visible to us. The "visible" bool indicates whether the
9456 * tuple is visible to us, while HeapTupleSatisfiesVacuum checks what else
9457 * is going on with it.
9458 *
9459 * In the event of a concurrently inserted tuple that also happens to have
9460 * been concurrently updated (by a separate transaction), the xmin of the
9461 * tuple will be used -- not the updater's xid.
9462 */
9464 switch (htsvResult)
9465 {
9466 case HEAPTUPLE_LIVE:
9467 if (visible)
9468 return;
9469 xid = HeapTupleHeaderGetXmin(tuple->t_data);
9470 break;
9473 if (visible)
9474 xid = HeapTupleHeaderGetUpdateXid(tuple->t_data);
9475 else
9476 xid = HeapTupleHeaderGetXmin(tuple->t_data);
9477
9479 {
9480 /* This is like the HEAPTUPLE_DEAD case */
9481 Assert(!visible);
9482 return;
9483 }
9484 break;
9486 xid = HeapTupleHeaderGetXmin(tuple->t_data);
9487 break;
9488 case HEAPTUPLE_DEAD:
9489 Assert(!visible);
9490 return;
9491 default:
9492
9493 /*
9494 * The only way to get to this default clause is if a new value is
9495 * added to the enum type without adding it to this switch
9496 * statement. That's a bug, so elog.
9497 */
9498 elog(ERROR, "unrecognized return value from HeapTupleSatisfiesVacuum: %u", htsvResult);
9499
9500 /*
9501 * In spite of having all enum values covered and calling elog on
9502 * this default, some compilers think this is a code path which
9503 * allows xid to be used below without initialization. Silence
9504 * that warning.
9505 */
9507 }
9508
9511
9512 /*
9513 * Find top level xid. Bail out if xid is too early to be a conflict, or
9514 * if it's our own xid.
9515 */
9517 return;
9520 return;
9521
9522 CheckForSerializableConflictOut(relation, xid, snapshot);
9523}
int16 AttrNumber
Definition attnum.h:21
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1425
void bms_free(Bitmapset *a)
Definition bitmapset.c:240
bool bms_is_member(int x, const Bitmapset *a)
Definition bitmapset.c:645
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:934
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:1036
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:710
#define bms_is_empty(a)
Definition bitmapset.h:119
uint32 BlockNumber
Definition block.h:31
#define InvalidBlockNumber
Definition block.h:33
static bool BlockNumberIsValid(BlockNumber blockNumber)
Definition block.h:71
static int32 next
Definition blutils.c:225
static Datum values[MAXATTR]
Definition bootstrap.c:190
int Buffer
Definition buf.h:23
#define BufferIsInvalid(buffer)
Definition buf.h:31
#define InvalidBuffer
Definition buf.h:25
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition bufmgr.c:4469
PrefetchBufferResult PrefetchBuffer(Relation reln, ForkNumber forkNum, BlockNumber blockNum)
Definition bufmgr.c:787
void BufferGetTag(Buffer buffer, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum)
Definition bufmgr.c:4490
bool BufferIsDirty(Buffer buffer)
Definition bufmgr.c:3137
void ReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5609
void UnlockReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5626
void MarkBufferDirty(Buffer buffer)
Definition bufmgr.c:3170
int maintenance_io_concurrency
Definition bufmgr.c:207
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition bufmgr.c:879
@ BAS_BULKREAD
Definition bufmgr.h:37
@ BAS_BULKWRITE
Definition bufmgr.h:39
#define RelationGetNumberOfBlocks(reln)
Definition bufmgr.h:309
static Page BufferGetPage(Buffer buffer)
Definition bufmgr.h:468
static Block BufferGetBlock(Buffer buffer)
Definition bufmgr.h:435
@ BUFFER_LOCK_SHARE
Definition bufmgr.h:212
@ BUFFER_LOCK_EXCLUSIVE
Definition bufmgr.h:222
@ BUFFER_LOCK_UNLOCK
Definition bufmgr.h:207
static void LockBuffer(Buffer buffer, BufferLockMode mode)
Definition bufmgr.h:334
static bool BufferIsValid(Buffer bufnum)
Definition bufmgr.h:419
Size PageGetHeapFreeSpace(const PageData *page)
Definition bufpage.c:1000
PageHeaderData * PageHeader
Definition bufpage.h:199
static bool PageIsAllVisible(const PageData *page)
Definition bufpage.h:454
static void PageClearAllVisible(Page page)
Definition bufpage.h:464
#define SizeOfPageHeaderData
Definition bufpage.h:241
static void PageSetAllVisible(Page page)
Definition bufpage.h:459
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition bufpage.h:268
static void * PageGetItem(PageData *page, const ItemIdData *itemId)
Definition bufpage.h:378
static void PageSetFull(Page page)
Definition bufpage.h:443
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition bufpage.h:416
PageData * Page
Definition bufpage.h:81
#define PageClearPrunable(page)
Definition bufpage.h:485
#define PageSetPrunable(page, xid)
Definition bufpage.h:478
static OffsetNumber PageGetMaxOffsetNumber(const PageData *page)
Definition bufpage.h:396
#define NameStr(name)
Definition c.h:894
#define InvalidCommandId
Definition c.h:812
#define pg_always_inline
Definition c.h:364
#define pg_noinline
Definition c.h:380
#define Min(x, y)
Definition c.h:1131
#define pg_attribute_unused()
Definition c.h:208
#define likely(x)
Definition c.h:496
#define MAXALIGN(LEN)
Definition c.h:955
uint8_t uint8
Definition c.h:681
#define Assert(condition)
Definition c.h:1002
int64_t int64
Definition c.h:680
TransactionId MultiXactId
Definition c.h:805
int16_t int16
Definition c.h:678
#define SHORTALIGN(LEN)
Definition c.h:951
uint16_t uint16
Definition c.h:682
#define pg_unreachable()
Definition c.h:426
#define unlikely(x)
Definition c.h:497
uint32_t uint32
Definition c.h:683
#define lengthof(array)
Definition c.h:932
#define StaticAssertDecl(condition, errmessage)
Definition c.h:1067
uint32 CommandId
Definition c.h:809
uint32 TransactionId
Definition c.h:795
#define OidIsValid(objectId)
Definition c.h:917
size_t Size
Definition c.h:748
bool IsToastRelation(Relation relation)
Definition catalog.c:208
bool IsCatalogRelation(Relation relation)
Definition catalog.c:106
bool IsSharedRelation(Oid relationId)
Definition catalog.c:331
bool IsInplaceUpdateRelation(Relation relation)
Definition catalog.c:185
uint32 result
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
CommandId HeapTupleHeaderGetCmin(const HeapTupleHeaderData *tup)
Definition combocid.c:104
void HeapTupleHeaderAdjustCmax(const HeapTupleHeaderData *tup, CommandId *cmax, bool *iscombo)
Definition combocid.c:153
CommandId HeapTupleHeaderGetCmax(const HeapTupleHeaderData *tup)
Definition combocid.c:118
bool datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen)
Definition datum.c:223
Datum arg
Definition elog.c:1323
int errcode(int sqlerrcode)
Definition elog.c:875
int int errdetail_internal(const char *fmt,...) pg_attribute_printf(1
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define WARNING
Definition elog.h:37
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
HeapTuple ExecFetchSlotHeapTuple(TupleTableSlot *slot, bool materialize, bool *shouldFree)
TupleTableSlot * ExecStoreBufferHeapTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer)
#define palloc_object(type)
Definition fe_memutils.h:89
#define palloc_array(type, count)
Definition fe_memutils.h:91
#define palloc0_object(type)
Definition fe_memutils.h:90
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition freelist.c:426
void FreeAccessStrategy(BufferAccessStrategy strategy)
Definition freelist.c:608
int NBuffers
Definition globals.c:144
Oid MyDatabaseTableSpace
Definition globals.c:98
Oid MyDatabaseId
Definition globals.c:96
void simple_heap_update(Relation relation, const ItemPointerData *otid, HeapTuple tup, TU_UpdateIndexes *update_indexes)
Definition heapam.c:4639
static bool DoesMultiXactIdConflict(MultiXactId multi, uint16 infomask, LockTupleMode lockmode, bool *current_is_member)
Definition heapam.c:7816
static XLogRecPtr log_heap_new_cid(Relation relation, HeapTuple tup)
Definition heapam.c:9257
static XLogRecPtr log_heap_update(Relation reln, Buffer oldbuf, Buffer vmbuffer_old, Buffer newbuf, Buffer vmbuffer_new, HeapTuple oldtup, HeapTuple newtup, HeapTuple old_key_tuple, bool all_visible_cleared, bool new_all_visible_cleared, bool walLogical)
Definition heapam.c:9015
static void compute_new_xmax_infomask(TransactionId xmax, uint16 old_infomask, uint16 old_infomask2, TransactionId add_to_xmax, LockTupleMode mode, bool is_update, TransactionId *result_xmax, uint16 *result_infomask, uint16 *result_infomask2)
Definition heapam.c:5503
static HeapTuple heap_prepare_insert(Relation relation, HeapTuple tup, TransactionId xid, CommandId cid, uint32 options)
Definition heapam.c:2228
static TM_Result heap_lock_updated_tuple_rec(Relation rel, TransactionId priorXmax, const ItemPointerData *tid, TransactionId xid, LockTupleMode mode)
Definition heapam.c:5875
static void heap_fetch_next_buffer(HeapScanDesc scan, ScanDirection dir)
Definition heapam.c:711
bool heap_inplace_lock(Relation relation, HeapTuple oldtup_ptr, Buffer buffer, void(*release_callback)(void *), void *arg)
Definition heapam.c:6572
bool heap_fetch(Relation relation, Snapshot snapshot, HeapTuple tuple, Buffer *userbuf, bool keep_buf)
Definition heapam.c:1685
#define BOTTOMUP_TOLERANCE_NBLOCKS
Definition heapam.c:193
static BlockNumber heap_scan_stream_read_next_parallel(ReadStream *stream, void *callback_private_data, void *per_buffer_data)
Definition heapam.c:255
int updstatus
Definition heapam.c:133
static int bottomup_sort_and_shrink(TM_IndexDeleteOp *delstate)
Definition heapam.c:8896
static bool heap_acquire_tuplock(Relation relation, const ItemPointerData *tid, LockTupleMode mode, LockWaitPolicy wait_policy, bool *have_tuple_lock)
Definition heapam.c:5454
static int heap_multi_insert_pages(HeapTuple *heaptuples, int done, int ntuples, Size saveFreeSpace)
Definition heapam.c:2276
static BlockNumber heap_scan_stream_read_next_serial(ReadStream *stream, void *callback_private_data, void *per_buffer_data)
Definition heapam.c:295
static pg_always_inline int page_collect_tuples(HeapScanDesc scan, Snapshot snapshot, Page page, Buffer buffer, BlockNumber block, int lines, bool all_visible, bool check_serializable)
Definition heapam.c:525
static void GetMultiXactIdHintBits(MultiXactId multi, uint16 *new_infomask, uint16 *new_infomask2)
Definition heapam.c:7667
void heap_insert(Relation relation, HeapTuple tup, CommandId cid, uint32 options, BulkInsertState bistate)
Definition heapam.c:2005
void heap_finish_speculative(Relation relation, const ItemPointerData *tid)
Definition heapam.c:6303
void HeapTupleHeaderAdvanceConflictHorizon(HeapTupleHeader tuple, TransactionId *snapshotConflictHorizon)
Definition heapam.c:8194
bool heap_getnextslot(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
Definition heapam.c:1475
#define LOCKMODE_from_mxstatus(status)
Definition heapam.c:162
void heap_endscan(TableScanDesc sscan)
Definition heapam.c:1391
#define FRM_RETURN_IS_XID
Definition heapam.c:6860
#define TUPLOCK_from_mxstatus(status)
Definition heapam.c:221
void heap_rescan(TableScanDesc sscan, ScanKey key, bool set_params, bool allow_strat, bool allow_sync, bool allow_pagemode)
Definition heapam.c:1332
void heap_inplace_unlock(Relation relation, HeapTuple oldtup, Buffer buffer)
Definition heapam.c:6850
static int index_delete_sort_cmp(TM_IndexDelete *deltid1, TM_IndexDelete *deltid2)
Definition heapam.c:8648
static bool ConditionalMultiXactIdWait(MultiXactId multi, MultiXactStatus status, uint16 infomask, Relation rel, int *remaining, bool logLockFailure)
Definition heapam.c:8016
bool heap_tuple_needs_eventual_freeze(HeapTupleHeader tuple)
Definition heapam.c:8031
static TransactionId FreezeMultiXactId(MultiXactId multi, uint16 t_infomask, const struct VacuumCutoffs *cutoffs, uint16 *flags, HeapPageFreeze *pagefrz)
Definition heapam.c:6911
static HeapTuple ExtractReplicaIdentity(Relation relation, HeapTuple tp, bool key_required, bool *copy)
Definition heapam.c:9338
static pg_noinline BlockNumber heapgettup_initial_block(HeapScanDesc scan, ScanDirection dir)
Definition heapam.c:756
static TM_Result heap_lock_updated_tuple(Relation rel, uint16 prior_infomask, TransactionId prior_raw_xmax, const ItemPointerData *prior_ctid, TransactionId xid, LockTupleMode mode)
Definition heapam.c:6250
#define LockTupleTuplock(rel, tup, mode)
Definition heapam.c:170
bool heap_tuple_should_freeze(HeapTupleHeader tuple, const struct VacuumCutoffs *cutoffs, TransactionId *NoFreezePageRelfrozenXid, MultiXactId *NoFreezePageRelminMxid)
Definition heapam.c:8086
bool heap_freeze_tuple(HeapTupleHeader tuple, TransactionId relfrozenxid, TransactionId relminmxid, TransactionId FreezeLimit, TransactionId MultiXactCutoff)
Definition heapam.c:7622
void heap_inplace_update_and_unlock(Relation relation, HeapTuple oldtup, HeapTuple tuple, Buffer buffer)
Definition heapam.c:6710
static BlockNumber heapgettup_advance_block(HeapScanDesc scan, BlockNumber block, ScanDirection dir)
Definition heapam.c:880
static TransactionId MultiXactIdGetUpdateXid(TransactionId xmax, uint16 t_infomask)
Definition heapam.c:7748
#define BOTTOMUP_MAX_NBLOCKS
Definition heapam.c:192
void ReleaseBulkInsertStatePin(BulkInsertState bistate)
Definition heapam.c:1967
#define FRM_MARK_COMMITTED
Definition heapam.c:6862
#define FRM_NOOP
Definition heapam.c:6858
static void index_delete_check_htid(TM_IndexDeleteOp *delstate, Page page, OffsetNumber maxoff, const ItemPointerData *htid, TM_IndexStatus *istatus)
Definition heapam.c:8279
HeapTuple heap_getnext(TableScanDesc sscan, ScanDirection direction)
Definition heapam.c:1436
int lockstatus
Definition heapam.c:132
void heap_freeze_prepared_tuples(Buffer buffer, HeapTupleFreeze *tuples, int ntuples)
Definition heapam.c:7600
bool heap_getnextslot_tidrange(TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
Definition heapam.c:1578
static void MultiXactIdWait(MultiXactId multi, MultiXactStatus status, uint16 infomask, Relation rel, const ItemPointerData *ctid, XLTW_Oper oper, int *remaining)
Definition heapam.c:7994
void heap_set_tidrange(TableScanDesc sscan, ItemPointer mintid, ItemPointer maxtid)
Definition heapam.c:1505
void heap_abort_speculative(Relation relation, const ItemPointerData *tid)
Definition heapam.c:6390
void heap_multi_insert(Relation relation, TupleTableSlot **slots, int ntuples, CommandId cid, uint32 options, BulkInsertState bistate)
Definition heapam.c:2308
static BlockNumber bitmapheap_stream_read_next(ReadStream *pgsr, void *private_data, void *per_buffer_data)
Definition heapam.c:320
TableScanDesc heap_beginscan(Relation relation, Snapshot snapshot, int nkeys, ScanKey key, ParallelTableScanDesc parallel_scan, uint32 flags)
Definition heapam.c:1168
static void heapgettup(HeapScanDesc scan, ScanDirection dir, int nkeys, ScanKey key)
Definition heapam.c:964
static Page heapgettup_continue_page(HeapScanDesc scan, ScanDirection dir, int *linesleft, OffsetNumber *lineoff)
Definition heapam.c:834
static uint8 compute_infobits(uint16 infomask, uint16 infomask2)
Definition heapam.c:2714
#define FRM_RETURN_IS_MULTI
Definition heapam.c:6861
LOCKMODE hwlock
Definition heapam.c:131
#define FRM_INVALIDATE_XMAX
Definition heapam.c:6859
static bool heap_attr_equals(TupleDesc tupdesc, int attrnum, Datum value1, Datum value2, bool isnull1, bool isnull2)
Definition heapam.c:4498
static void index_delete_sort(TM_IndexDeleteOp *delstate)
Definition heapam.c:8684
void heap_prepare_pagescan(TableScanDesc sscan)
Definition heapam.c:619
static Bitmapset * HeapDetermineColumnsInfo(Relation relation, Bitmapset *interesting_cols, Bitmapset *external_cols, HeapTuple oldtup, HeapTuple newtup, bool *has_external)
Definition heapam.c:4549
static const int MultiXactStatusLock[MaxMultiXactStatus+1]
Definition heapam.c:210
void simple_heap_insert(Relation relation, HeapTuple tup)
Definition heapam.c:2701
static bool xmax_infomask_changed(uint16 new_infomask, uint16 old_infomask)
Definition heapam.c:2736
#define UnlockTupleTuplock(rel, tup, mode)
Definition heapam.c:172
TM_Result heap_update(Relation relation, const ItemPointerData *otid, HeapTuple newtup, CommandId cid, uint32 options pg_attribute_unused(), Snapshot crosscheck, bool wait, TM_FailureData *tmfd, LockTupleMode *lockmode, TU_UpdateIndexes *update_indexes)
Definition heapam.c:3267
static TM_Result test_lockmode_for_conflict(MultiXactStatus status, TransactionId xid, LockTupleMode mode, HeapTuple tup, bool *needwait)
Definition heapam.c:5784
bool heap_prepare_freeze_tuple(HeapTupleHeader tuple, const struct VacuumCutoffs *cutoffs, HeapPageFreeze *pagefrz, HeapTupleFreeze *frz, bool *totally_frozen)
Definition heapam.c:7267
static void AssertHasSnapshotForToast(Relation rel)
Definition heapam.c:228
void simple_heap_delete(Relation relation, const ItemPointerData *tid)
Definition heapam.c:3219
static const struct @15 tupleLockExtraInfo[]
TransactionId HeapTupleGetUpdateXid(const HeapTupleHeaderData *tup)
Definition heapam.c:7800
TransactionId heap_index_delete_tuples(Relation rel, TM_IndexDeleteOp *delstate)
Definition heapam.c:8339
#define ConditionalLockTupleTuplock(rel, tup, mode, log)
Definition heapam.c:174
static void initscan(HeapScanDesc scan, ScanKey key, bool keep_startblock)
Definition heapam.c:360
static int bottomup_nblocksfavorable(IndexDeleteCounts *blockgroups, int nblockgroups, TM_IndexDelete *deltids)
Definition heapam.c:8780
static void heapgettup_pagemode(HeapScanDesc scan, ScanDirection dir, int nkeys, ScanKey key)
Definition heapam.c:1074
TM_Result heap_lock_tuple(Relation relation, HeapTuple tuple, CommandId cid, LockTupleMode mode, LockWaitPolicy wait_policy, bool follow_updates, Buffer *buffer, TM_FailureData *tmfd)
Definition heapam.c:4728
static void UpdateXmaxHintBits(HeapTupleHeader tuple, Buffer buffer, TransactionId xid)
Definition heapam.c:1916
static bool Do_MultiXactIdWait(MultiXactId multi, MultiXactStatus status, uint16 infomask, bool nowait, Relation rel, const ItemPointerData *ctid, XLTW_Oper oper, int *remaining, bool logLockFailure)
Definition heapam.c:7916
static int bottomup_sort_and_shrink_cmp(const void *arg1, const void *arg2)
Definition heapam.c:8823
void heap_get_latest_tid(TableScanDesc sscan, ItemPointer tid)
Definition heapam.c:1794
void heap_setscanlimits(TableScanDesc sscan, BlockNumber startBlk, BlockNumber numBlks)
Definition heapam.c:503
void HeapCheckForSerializableConflictOut(bool visible, Relation relation, HeapTuple tuple, Buffer buffer, Snapshot snapshot)
Definition heapam.c:9442
static Page heapgettup_start_page(HeapScanDesc scan, ScanDirection dir, int *linesleft, OffsetNumber *lineoff)
Definition heapam.c:803
static MultiXactStatus get_mxact_status_for_lock(LockTupleMode mode, bool is_update)
Definition heapam.c:4681
void heap_pre_freeze_checks(Buffer buffer, HeapTupleFreeze *tuples, int ntuples)
Definition heapam.c:7547
BulkInsertState GetBulkInsertState(void)
Definition heapam.c:1938
TM_Result heap_delete(Relation relation, const ItemPointerData *tid, CommandId cid, uint32 options, Snapshot crosscheck, bool wait, TM_FailureData *tmfd)
Definition heapam.c:2759
void FreeBulkInsertState(BulkInsertState bistate)
Definition heapam.c:1955
#define HEAP_INSERT_SPECULATIVE
Definition heapam.h:39
#define HEAP_FREEZE_CHECK_XMAX_ABORTED
Definition heapam.h:150
struct HeapScanDescData * HeapScanDesc
Definition heapam.h:107
HTSV_Result
Definition heapam.h:137
@ HEAPTUPLE_RECENTLY_DEAD
Definition heapam.h:140
@ HEAPTUPLE_INSERT_IN_PROGRESS
Definition heapam.h:141
@ HEAPTUPLE_LIVE
Definition heapam.h:139
@ HEAPTUPLE_DELETE_IN_PROGRESS
Definition heapam.h:142
@ HEAPTUPLE_DEAD
Definition heapam.h:138
struct BitmapHeapScanDescData * BitmapHeapScanDesc
Definition heapam.h:115
#define HEAP_INSERT_FROZEN
Definition heapam.h:37
static void heap_execute_freeze_tuple(HeapTupleHeader tuple, HeapTupleFreeze *frz)
Definition heapam.h:533
#define HEAP_FREEZE_CHECK_XMIN_COMMITTED
Definition heapam.h:149
#define HEAP_INSERT_NO_LOGICAL
Definition heapam.h:38
struct BulkInsertStateData * BulkInsertState
Definition heapam.h:47
const TableAmRoutine * GetHeapamTableAmRoutine(void)
bool heap_hot_search_buffer(ItemPointer tid, Relation relation, Buffer buffer, Snapshot snapshot, HeapTuple heapTuple, bool *all_dead, bool first_call)
void HeapTupleSetHintBits(HeapTupleHeader tuple, Buffer buffer, uint16 infomask, TransactionId xid)
bool HeapTupleSatisfiesVisibility(HeapTuple htup, Snapshot snapshot, Buffer buffer)
HTSV_Result HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin, Buffer buffer)
int HeapTupleSatisfiesMVCCBatch(Snapshot snapshot, Buffer buffer, int ntups, BatchMVCCState *batchmvcc, OffsetNumber *vistuples_dense)
bool HeapTupleHeaderIsOnlyLocked(HeapTupleHeader tuple)
TM_Result HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid, Buffer buffer)
#define XLH_INSERT_ON_TOAST_RELATION
Definition heapam_xlog.h:76
#define HEAP_DELETE_BLKREF_HEAP
#define SizeOfHeapMultiInsert
#define XLOG_HEAP2_MULTI_INSERT
Definition heapam_xlog.h:64
#define SizeOfHeapUpdate
#define XLH_INVALID_XVAC
#define XLH_UPDATE_NEW_ALL_VISIBLE_CLEARED
Definition heapam_xlog.h:87
#define XLOG_HEAP_HOT_UPDATE
Definition heapam_xlog.h:37
#define XLOG_HEAP_DELETE
Definition heapam_xlog.h:34
#define XLH_INSERT_IS_SPECULATIVE
Definition heapam_xlog.h:74
#define XLH_LOCK_ALL_FROZEN_CLEARED
#define XLH_DELETE_CONTAINS_OLD_KEY
#define HEAP_MULTI_INSERT_BLKREF_HEAP
#define XLH_UPDATE_CONTAINS_NEW_TUPLE
Definition heapam_xlog.h:90
#define XLH_INSERT_LAST_IN_MULTI
Definition heapam_xlog.h:73
#define HEAP_LOCK_BLKREF_VM
#define XLH_INSERT_ALL_FROZEN_SET
Definition heapam_xlog.h:79
#define XLH_FREEZE_XVAC
#define XLOG_HEAP_UPDATE
Definition heapam_xlog.h:35
#define XLHL_XMAX_KEYSHR_LOCK
#define XLH_DELETE_ALL_VISIBLE_CLEARED
#define XLH_UPDATE_CONTAINS_OLD_TUPLE
Definition heapam_xlog.h:88
#define HEAP_INSERT_BLKREF_VM
#define HEAP_UPDATE_BLKREF_HEAP_NEW
#define HEAP_LOCK_BLKREF_HEAP
#define HEAP_DELETE_BLKREF_VM
#define SizeOfHeapNewCid
#define SizeOfHeapLockUpdated
#define XLHL_XMAX_IS_MULTI
#define XLH_INSERT_ALL_VISIBLE_CLEARED
Definition heapam_xlog.h:72
#define SizeOfHeapHeader
#define XLH_DELETE_IS_PARTITION_MOVE
#define MinSizeOfHeapInplace
#define XLH_UPDATE_OLD_ALL_VISIBLE_CLEARED
Definition heapam_xlog.h:85
#define XLHL_XMAX_LOCK_ONLY
#define XLOG_HEAP_INPLACE
Definition heapam_xlog.h:40
#define XLOG_HEAP2_LOCK_UPDATED
Definition heapam_xlog.h:65
#define XLH_UPDATE_SUFFIX_FROM_OLD
Definition heapam_xlog.h:92
#define HEAP_UPDATE_BLKREF_HEAP_OLD
#define XLH_UPDATE_PREFIX_FROM_OLD
Definition heapam_xlog.h:91
#define SizeOfMultiInsertTuple
#define XLHL_XMAX_EXCL_LOCK
#define XLOG_HEAP2_NEW_CID
Definition heapam_xlog.h:66
#define XLH_DELETE_CONTAINS_OLD_TUPLE
#define HEAP_INSERT_BLKREF_HEAP
#define HEAP_UPDATE_BLKREF_VM_OLD
#define HEAP_MULTI_INSERT_BLKREF_VM
#define XLOG_HEAP_LOCK
Definition heapam_xlog.h:39
#define XLOG_HEAP_INSERT
Definition heapam_xlog.h:33
#define SizeOfHeapInsert
#define SizeOfHeapDelete
#define HEAP_UPDATE_BLKREF_VM_NEW
#define XLH_DELETE_IS_SUPER
#define XLH_UPDATE_CONTAINS_OLD_KEY
Definition heapam_xlog.h:89
#define XLH_DELETE_NO_LOGICAL
#define XLHL_KEYS_UPDATED
#define XLH_INSERT_CONTAINS_NEW_TUPLE
Definition heapam_xlog.h:75
#define XLOG_HEAP_INIT_PAGE
Definition heapam_xlog.h:47
#define SizeOfHeapConfirm
#define SizeOfHeapLock
#define XLOG_HEAP_CONFIRM
Definition heapam_xlog.h:38
void heap_toast_delete(Relation rel, HeapTuple oldtup, bool is_speculative)
Definition heaptoast.c:43
HeapTuple heap_toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup, uint32 options)
Definition heaptoast.c:96
HeapTuple toast_flatten_tuple(HeapTuple tup, TupleDesc tupleDesc)
Definition heaptoast.c:350
#define TOAST_TUPLE_THRESHOLD
Definition heaptoast.h:48
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition heaptuple.c:1025
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition heaptuple.c:1254
void heap_freetuple(HeapTuple htup)
Definition heaptuple.c:1372
void RelationPutHeapTuple(Relation relation, Buffer buffer, HeapTuple tuple, bool token)
Definition hio.c:35
Buffer RelationGetBufferForTuple(Relation relation, Size len, Buffer otherBuffer, uint32 options, BulkInsertState bistate, Buffer *vmbuffer, Buffer *vmbuffer_other, int num_pages)
Definition hio.c:500
HeapTupleHeaderData * HeapTupleHeader
Definition htup.h:23
#define HEAP_MOVED_OFF
#define HEAP_XMAX_SHR_LOCK
#define HEAP_XMIN_FROZEN
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
static bool HeapTupleHeaderXminFrozen(const HeapTupleHeaderData *tup)
#define HeapTupleHeaderGetNatts(tup)
static void HeapTupleHeaderSetXminFrozen(HeapTupleHeaderData *tup)
#define SizeofHeapTupleHeader
#define HEAP_KEYS_UPDATED
static bool HEAP_XMAX_IS_SHR_LOCKED(uint16 infomask)
static bool HEAP_XMAX_IS_LOCKED_ONLY(uint16 infomask)
static bool HeapTupleHeaderXminInvalid(const HeapTupleHeaderData *tup)
static void HeapTupleClearHotUpdated(const HeapTupleData *tuple)
static bool HeapTupleHasExternal(const HeapTupleData *tuple)
static TransactionId HeapTupleHeaderGetXvac(const HeapTupleHeaderData *tup)
#define HEAP2_XACT_MASK
static void HeapTupleHeaderSetCmax(HeapTupleHeaderData *tup, CommandId cid, bool iscombo)
#define HEAP_XMAX_LOCK_ONLY
static void HeapTupleHeaderClearHotUpdated(HeapTupleHeaderData *tup)
static void HeapTupleHeaderSetCmin(HeapTupleHeaderData *tup, CommandId cid)
#define HEAP_XMAX_BITS
#define HEAP_LOCK_MASK
static CommandId HeapTupleHeaderGetRawCommandId(const HeapTupleHeaderData *tup)
static TransactionId HeapTupleHeaderGetRawXmax(const HeapTupleHeaderData *tup)
static bool HeapTupleHeaderIsHeapOnly(const HeapTupleHeaderData *tup)
static bool HeapTupleIsHeapOnly(const HeapTupleData *tuple)
#define HEAP_MOVED
static void HeapTupleSetHeapOnly(const HeapTupleData *tuple)
#define HEAP_XMAX_IS_MULTI
static bool HEAP_XMAX_IS_KEYSHR_LOCKED(uint16 infomask)
#define HEAP_XMAX_COMMITTED
static TransactionId HeapTupleHeaderGetXmin(const HeapTupleHeaderData *tup)
#define HEAP_COMBOCID
#define HEAP_XACT_MASK
static bool HeapTupleHeaderIndicatesMovedPartitions(const HeapTupleHeaderData *tup)
static void HeapTupleSetHotUpdated(const HeapTupleData *tuple)
#define HEAP_XMAX_EXCL_LOCK
static bool HeapTupleHeaderIsHotUpdated(const HeapTupleHeaderData *tup)
#define HEAP_XMAX_INVALID
static TransactionId HeapTupleHeaderGetRawXmin(const HeapTupleHeaderData *tup)
static void * GETSTRUCT(const HeapTupleData *tuple)
static void HeapTupleClearHeapOnly(const HeapTupleData *tuple)
#define MaxHeapAttributeNumber
static bool HeapTupleHeaderIsSpeculative(const HeapTupleHeaderData *tup)
static TransactionId HeapTupleHeaderGetUpdateXid(const HeapTupleHeaderData *tup)
#define MaxHeapTuplesPerPage
static bool HEAP_XMAX_IS_EXCL_LOCKED(uint16 infomask)
static void HeapTupleHeaderSetXmin(HeapTupleHeaderData *tup, TransactionId xid)
static bool HEAP_LOCKED_UPGRADED(uint16 infomask)
#define HEAP_UPDATED
#define HEAP_XMAX_KEYSHR_LOCK
static void HeapTupleHeaderSetMovedPartitions(HeapTupleHeaderData *tup)
static void HeapTupleHeaderSetXmax(HeapTupleHeaderData *tup, TransactionId xid)
static bool HeapTupleHeaderXminCommitted(const HeapTupleHeaderData *tup)
#define IsParallelWorker()
Definition parallel.h:62
void index_close(Relation relation, LOCKMODE lockmode)
Definition indexam.c:178
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition indexam.c:134
int remaining
Definition informix.c:692
#define INJECTION_POINT(name, arg)
void AcceptInvalidationMessages(void)
Definition inval.c:930
int inplaceGetInvalidationMessages(SharedInvalidationMessage **msgs, bool *RelcacheInitFileInval)
Definition inval.c:1088
void PreInplace_Inval(void)
Definition inval.c:1247
void CacheInvalidateHeapTupleInplace(Relation relation, HeapTuple key_equivalent_tuple)
Definition inval.c:1590
void AtInplace_Inval(void)
Definition inval.c:1260
void ForgetInplace_Inval(void)
Definition inval.c:1283
void CacheInvalidateHeapTuple(Relation relation, HeapTuple tuple, HeapTuple newtuple)
Definition inval.c:1568
int b
Definition isn.c:74
int j
Definition isn.c:78
int i
Definition isn.c:77
#define ItemIdGetLength(itemId)
Definition itemid.h:59
#define ItemIdIsNormal(itemId)
Definition itemid.h:99
#define ItemIdGetRedirect(itemId)
Definition itemid.h:78
#define ItemIdIsUsed(itemId)
Definition itemid.h:92
#define ItemIdIsRedirected(itemId)
Definition itemid.h:106
#define ItemIdHasStorage(itemId)
Definition itemid.h:120
int32 ItemPointerCompare(const ItemPointerData *arg1, const ItemPointerData *arg2)
Definition itemptr.c:51
bool ItemPointerEquals(const ItemPointerData *pointer1, const ItemPointerData *pointer2)
Definition itemptr.c:35
static void ItemPointerSet(ItemPointerData *pointer, BlockNumber blockNumber, OffsetNumber offNum)
Definition itemptr.h:135
static void ItemPointerSetInvalid(ItemPointerData *pointer)
Definition itemptr.h:184
static void ItemPointerSetOffsetNumber(ItemPointerData *pointer, OffsetNumber offsetNumber)
Definition itemptr.h:158
static void ItemPointerSetBlockNumber(ItemPointerData *pointer, BlockNumber blockNumber)
Definition itemptr.h:147
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
Definition itemptr.h:124
static bool ItemPointerIndicatesMovedPartitions(const ItemPointerData *pointer)
Definition itemptr.h:197
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition itemptr.h:103
static BlockNumber ItemPointerGetBlockNumberNoCheck(const ItemPointerData *pointer)
Definition itemptr.h:93
static void ItemPointerCopy(const ItemPointerData *fromPointer, ItemPointerData *toPointer)
Definition itemptr.h:172
static bool ItemPointerIsValid(const ItemPointerData *pointer)
Definition itemptr.h:83
void UnlockTuple(Relation relation, const ItemPointerData *tid, LOCKMODE lockmode)
Definition lmgr.c:601
bool ConditionalXactLockTableWait(TransactionId xid, bool logLockFailure)
Definition lmgr.c:739
void LockTuple(Relation relation, const ItemPointerData *tid, LOCKMODE lockmode)
Definition lmgr.c:562
void XactLockTableWait(TransactionId xid, Relation rel, const ItemPointerData *ctid, XLTW_Oper oper)
Definition lmgr.c:663
XLTW_Oper
Definition lmgr.h:25
@ XLTW_None
Definition lmgr.h:26
@ XLTW_Lock
Definition lmgr.h:29
@ XLTW_Delete
Definition lmgr.h:28
@ XLTW_LockUpdated
Definition lmgr.h:30
@ XLTW_Update
Definition lmgr.h:27
bool LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode, bool orstronger)
Definition lock.c:640
bool DoLockModesConflict(LOCKMODE mode1, LOCKMODE mode2)
Definition lock.c:620
bool log_lock_failures
Definition lock.c:57
int LOCKMODE
Definition lockdefs.h:26
#define AccessExclusiveLock
Definition lockdefs.h:43
#define ShareRowExclusiveLock
Definition lockdefs.h:41
#define AccessShareLock
Definition lockdefs.h:36
#define InplaceUpdateTupleLock
Definition lockdefs.h:48
#define ShareUpdateExclusiveLock
Definition lockdefs.h:39
#define ExclusiveLock
Definition lockdefs.h:42
#define RowShareLock
Definition lockdefs.h:37
LockWaitPolicy
Definition lockoptions.h:38
@ LockWaitSkip
Definition lockoptions.h:42
@ LockWaitBlock
Definition lockoptions.h:40
@ LockWaitError
Definition lockoptions.h:44
LockTupleMode
Definition lockoptions.h:51
@ LockTupleExclusive
Definition lockoptions.h:59
@ LockTupleNoKeyExclusive
Definition lockoptions.h:57
@ LockTupleShare
Definition lockoptions.h:55
@ LockTupleKeyShare
Definition lockoptions.h:53
#define SET_LOCKTAG_RELATION(locktag, dboid, reloid)
Definition locktag.h:81
#define SET_LOCKTAG_TUPLE(locktag, dboid, reloid, blocknum, offnum)
Definition locktag.h:117
void pfree(void *pointer)
Definition mcxt.c:1619
void * palloc(Size size)
Definition mcxt.c:1390
#define IsBootstrapProcessingMode()
Definition miscadmin.h:486
#define START_CRIT_SECTION()
Definition miscadmin.h:152
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:125
#define IsNormalProcessingMode()
Definition miscadmin.h:488
#define END_CRIT_SECTION()
Definition miscadmin.h:154
MultiXactId MultiXactIdExpand(MultiXactId multi, TransactionId xid, MultiXactStatus status)
Definition multixact.c:411
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition multixact.c:2865
bool MultiXactIdPrecedesOrEquals(MultiXactId multi1, MultiXactId multi2)
Definition multixact.c:2879
bool MultiXactIdIsRunning(MultiXactId multi, bool isLockOnly)
Definition multixact.c:522
void MultiXactIdSetOldestMember(void)
Definition multixact.c:596
MultiXactId MultiXactIdCreateFromMembers(int nmembers, MultiXactMember *members)
Definition multixact.c:715
MultiXactId MultiXactIdCreate(TransactionId xid1, MultiXactStatus status1, TransactionId xid2, MultiXactStatus status2)
Definition multixact.c:358
int GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members, bool from_pgupgrade, bool isLockOnly)
Definition multixact.c:1172
#define MultiXactIdIsValid(multi)
Definition multixact.h:29
MultiXactStatus
Definition multixact.h:37
@ MultiXactStatusForShare
Definition multixact.h:39
@ MultiXactStatusForNoKeyUpdate
Definition multixact.h:40
@ MultiXactStatusNoKeyUpdate
Definition multixact.h:43
@ MultiXactStatusUpdate
Definition multixact.h:45
@ MultiXactStatusForUpdate
Definition multixact.h:41
@ MultiXactStatusForKeyShare
Definition multixact.h:38
#define ISUPDATE_from_mxstatus(status)
Definition multixact.h:51
#define InvalidMultiXactId
Definition multixact.h:25
#define MaxMultiXactStatus
Definition multixact.h:48
static char * errmsg
#define InvalidOffsetNumber
Definition off.h:26
#define OffsetNumberIsValid(offsetNumber)
Definition off.h:39
#define OffsetNumberNext(offsetNumber)
Definition off.h:52
uint16 OffsetNumber
Definition off.h:24
#define FirstOffsetNumber
Definition off.h:27
#define OffsetNumberPrev(offsetNumber)
Definition off.h:54
#define MaxOffsetNumber
Definition off.h:28
Datum lower(PG_FUNCTION_ARGS)
Datum upper(PG_FUNCTION_ARGS)
Operator oper(ParseState *pstate, List *opname, Oid ltypeId, Oid rtypeId, bool noError, int location)
Definition parse_oper.c:376
int16 attlen
#define ERRCODE_DATA_CORRUPTED
static uint32 pg_nextpower2_32(uint32 num)
static PgChecksumMode mode
FormData_pg_class * Form_pg_class
Definition pg_class.h:160
END_CATALOG_STRUCT typedef FormData_pg_database * Form_pg_database
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define pgstat_count_heap_getnext(rel)
Definition pgstat.h:735
#define pgstat_count_heap_scan(rel)
Definition pgstat.h:730
void pgstat_count_heap_update(Relation rel, bool hot, bool newpage)
void pgstat_count_heap_delete(Relation rel)
void pgstat_count_heap_insert(Relation rel, PgStat_Counter n)
#define qsort(a, b, c, d)
Definition port.h:496
static Oid DatumGetObjectId(Datum X)
Definition postgres.h:242
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
#define InvalidOid
unsigned int Oid
void CheckForSerializableConflictIn(Relation relation, const ItemPointerData *tid, BlockNumber blkno)
Definition predicate.c:4265
void CheckForSerializableConflictOut(Relation relation, TransactionId xid, Snapshot snapshot)
Definition predicate.c:3952
void PredicateLockRelation(Relation relation, Snapshot snapshot)
Definition predicate.c:2505
void PredicateLockTID(Relation relation, const ItemPointerData *tid, Snapshot snapshot, TransactionId tuple_xid)
Definition predicate.c:2550
bool CheckForSerializableConflictOutNeeded(Relation relation, Snapshot snapshot)
Definition predicate.c:3920
static int fb(int x)
GlobalVisState * GlobalVisTestFor(Relation rel)
Definition procarray.c:4114
bool TransactionIdIsInProgress(TransactionId xid)
Definition procarray.c:1393
void heap_page_prune_opt(Relation relation, Buffer buffer, Buffer *vmbuffer, bool rel_read_only)
Definition pruneheap.c:272
void read_stream_reset(ReadStream *stream)
Buffer read_stream_next_buffer(ReadStream *stream, void **per_buffer_data)
ReadStream * read_stream_begin_relation(int flags, BufferAccessStrategy strategy, Relation rel, ForkNumber forknum, ReadStreamBlockNumberCB callback, void *callback_private_data, size_t per_buffer_data_size)
void read_stream_enable_stats(ReadStream *stream, IOStats *stats)
void read_stream_end(ReadStream *stream)
#define READ_STREAM_USE_BATCHING
Definition read_stream.h:64
BlockNumber(* ReadStreamBlockNumberCB)(ReadStream *stream, void *callback_private_data, void *per_buffer_data)
Definition read_stream.h:78
#define READ_STREAM_DEFAULT
Definition read_stream.h:21
#define READ_STREAM_SEQUENTIAL
Definition read_stream.h:36
#define RelationGetRelid(relation)
Definition rel.h:516
#define RelationIsLogicallyLogged(relation)
Definition rel.h:721
#define RelationGetTargetPageFreeSpace(relation, defaultff)
Definition rel.h:391
#define RelationGetDescr(relation)
Definition rel.h:542
#define RelationGetNumberOfAttributes(relation)
Definition rel.h:522
#define RelationGetRelationName(relation)
Definition rel.h:550
#define RelationIsAccessibleInLogicalDecoding(relation)
Definition rel.h:704
#define RelationNeedsWAL(relation)
Definition rel.h:639
#define RelationUsesLocalBuffers(relation)
Definition rel.h:648
#define HEAP_DEFAULT_FILLFACTOR
Definition rel.h:362
void RelationDecrementReferenceCount(Relation rel)
Definition relcache.c:2204
Bitmapset * RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
Definition relcache.c:5315
void RelationIncrementReferenceCount(Relation rel)
Definition relcache.c:2191
@ INDEX_ATTR_BITMAP_KEY
Definition relcache.h:70
@ INDEX_ATTR_BITMAP_HOT_BLOCKING
Definition relcache.h:73
@ INDEX_ATTR_BITMAP_SUMMARIZED
Definition relcache.h:74
@ INDEX_ATTR_BITMAP_IDENTITY_KEY
Definition relcache.h:72
ForkNumber
Definition relpath.h:56
@ MAIN_FORKNUM
Definition relpath.h:58
struct ParallelBlockTableScanDescData * ParallelBlockTableScanDesc
Definition relscan.h:109
#define ScanDirectionIsForward(direction)
Definition sdir.h:64
#define ScanDirectionIsBackward(direction)
Definition sdir.h:50
ScanDirection
Definition sdir.h:25
@ ForwardScanDirection
Definition sdir.h:28
void UnregisterSnapshot(Snapshot snapshot)
Definition snapmgr.c:866
TransactionId TransactionXmin
Definition snapmgr.c:159
bool HaveRegisteredOrActiveSnapshot(void)
Definition snapmgr.c:1643
void InvalidateCatalogSnapshot(void)
Definition snapmgr.c:455
#define IsHistoricMVCCSnapshot(snapshot)
Definition snapmgr.h:67
#define SnapshotAny
Definition snapmgr.h:33
#define InitNonVacuumableSnapshot(snapshotdata, vistestp)
Definition snapmgr.h:50
#define IsMVCCSnapshot(snapshot)
Definition snapmgr.h:59
#define InvalidSnapshot
Definition snapshot.h:119
int get_tablespace_maintenance_io_concurrency(Oid spcid)
Definition spccache.c:230
#define init()
BlockNumber last_free
Definition hio.h:49
BufferAccessStrategy strategy
Definition hio.h:31
uint32 already_extended_by
Definition hio.h:50
BlockNumber next_free
Definition hio.h:48
Buffer current_buf
Definition hio.h:32
MultiXactId NoFreezePageRelminMxid
Definition heapam.h:244
TransactionId FreezePageConflictXid
Definition heapam.h:233
TransactionId FreezePageRelfrozenXid
Definition heapam.h:220
bool freeze_required
Definition heapam.h:194
MultiXactId FreezePageRelminMxid
Definition heapam.h:221
TransactionId NoFreezePageRelfrozenXid
Definition heapam.h:243
Buffer rs_vmbuffer
Definition heapam.h:100
BufferAccessStrategy rs_strategy
Definition heapam.h:75
ScanDirection rs_dir
Definition heapam.h:90
uint32 rs_ntuples
Definition heapam.h:104
OffsetNumber rs_coffset
Definition heapam.h:70
Buffer rs_cbuf
Definition heapam.h:72
ParallelBlockTableScanWorkerData * rs_parallelworkerdata
Definition heapam.h:97
BlockNumber rs_startblock
Definition heapam.h:64
HeapTupleData rs_ctup
Definition heapam.h:77
OffsetNumber rs_vistuples[MaxHeapTuplesPerPage]
Definition heapam.h:105
BlockNumber rs_numblocks
Definition heapam.h:65
BlockNumber rs_nblocks
Definition heapam.h:63
ReadStream * rs_read_stream
Definition heapam.h:80
uint32 rs_cindex
Definition heapam.h:103
BlockNumber rs_prefetch_block
Definition heapam.h:91
BlockNumber rs_cblock
Definition heapam.h:71
TableScanDescData rs_base
Definition heapam.h:60
ItemPointerData t_self
Definition htup.h:65
uint32 t_len
Definition htup.h:64
HeapTupleHeader t_data
Definition htup.h:68
Oid t_tableOid
Definition htup.h:66
TransactionId t_xmin
union HeapTupleHeaderData::@52 t_choice
ItemPointerData t_ctid
HeapTupleFields t_heap
int16 npromisingtids
Definition heapam.c:201
LockRelId lockRelId
Definition rel.h:46
Oid relId
Definition rel.h:40
Oid dbId
Definition rel.h:41
TransactionId xid
Definition multixact.h:57
MultiXactStatus status
Definition multixact.h:58
LockInfoData rd_lockInfo
Definition rel.h:114
Form_pg_index rd_index
Definition rel.h:192
RelFileLocator rd_locator
Definition rel.h:57
Form_pg_class rd_rel
Definition rel.h:111
bool takenDuringRecovery
Definition snapshot.h:180
TransactionId xmax
Definition tableam.h:172
CommandId cmax
Definition tableam.h:173
ItemPointerData ctid
Definition tableam.h:171
ItemPointerData tid
Definition tableam.h:234
Relation rs_rd
Definition relscan.h:36
struct TableScanInstrumentation * rs_instrument
Definition relscan.h:72
uint32 rs_flags
Definition relscan.h:64
struct ScanKeyData * rs_key
Definition relscan.h:39
struct SnapshotData * rs_snapshot
Definition relscan.h:37
struct ParallelTableScanDescData * rs_parallel
Definition relscan.h:66
TransactionId FreezeLimit
Definition vacuum.h:288
TransactionId OldestXmin
Definition vacuum.h:278
TransactionId relfrozenxid
Definition vacuum.h:262
MultiXactId relminmxid
Definition vacuum.h:263
MultiXactId MultiXactCutoff
Definition vacuum.h:289
MultiXactId OldestMxact
Definition vacuum.h:279
Definition c.h:835
OffsetNumber offnum
TransactionId SubTransGetTopmostTransaction(TransactionId xid)
Definition subtrans.c:170
void ss_report_location(Relation rel, BlockNumber location)
Definition syncscan.c:287
BlockNumber ss_get_location(Relation rel, BlockNumber relnblocks)
Definition syncscan.c:252
#define FirstLowInvalidHeapAttributeNumber
Definition sysattr.h:27
#define TableOidAttributeNumber
Definition sysattr.h:26
bool RelationSupportsSysCache(Oid relid)
Definition syscache.c:763
void table_block_parallelscan_startblock_init(Relation rel, ParallelBlockTableScanWorker pbscanwork, ParallelBlockTableScanDesc pbscan, BlockNumber startblock, BlockNumber numblocks)
Definition tableam.c:453
BlockNumber table_block_parallelscan_nextpage(Relation rel, ParallelBlockTableScanWorker pbscanwork, ParallelBlockTableScanDesc pbscan)
Definition tableam.c:548
bool synchronize_seqscans
Definition tableam.c:50
@ SO_ALLOW_STRAT
Definition tableam.h:61
@ SO_TYPE_TIDRANGESCAN
Definition tableam.h:56
@ SO_TEMP_SNAPSHOT
Definition tableam.h:68
@ SO_HINT_REL_READ_ONLY
Definition tableam.h:71
@ SO_ALLOW_PAGEMODE
Definition tableam.h:65
@ SO_TYPE_SAMPLESCAN
Definition tableam.h:54
@ SO_ALLOW_SYNC
Definition tableam.h:63
@ SO_TYPE_SEQSCAN
Definition tableam.h:52
@ SO_SCAN_INSTRUMENT
Definition tableam.h:74
@ SO_TYPE_BITMAPSCAN
Definition tableam.h:53
TU_UpdateIndexes
Definition tableam.h:133
@ TU_Summarizing
Definition tableam.h:141
@ TU_All
Definition tableam.h:138
@ TU_None
Definition tableam.h:135
TM_Result
Definition tableam.h:95
@ TM_Ok
Definition tableam.h:100
@ TM_BeingModified
Definition tableam.h:122
@ TM_Deleted
Definition tableam.h:115
@ TM_WouldBlock
Definition tableam.h:125
@ TM_Updated
Definition tableam.h:112
@ TM_SelfModified
Definition tableam.h:106
@ TM_Invisible
Definition tableam.h:103
#define TABLE_DELETE_CHANGING_PARTITION
Definition tableam.h:289
#define TABLE_DELETE_NO_LOGICAL
Definition tableam.h:290
#define TABLE_UPDATE_NO_LOGICAL
Definition tableam.h:293
bool tbm_iterate(TBMIterator *iterator, TBMIterateResult *tbmres)
Definition tidbitmap.c:1614
bool TransactionIdDidCommit(TransactionId transactionId)
Definition transam.c:126
bool TransactionIdDidAbort(TransactionId transactionId)
Definition transam.c:188
static bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition transam.h:297
#define InvalidTransactionId
Definition transam.h:31
static bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition transam.h:282
static bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
Definition transam.h:312
#define TransactionIdEquals(id1, id2)
Definition transam.h:43
#define TransactionIdIsValid(xid)
Definition transam.h:41
#define TransactionIdIsNormal(xid)
Definition transam.h:42
static bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition transam.h:263
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:195
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition tuptable.h:476
static bool HeapKeyTest(HeapTuple tuple, TupleDesc tupdesc, int nkeys, ScanKey keys)
Definition valid.h:28
static bool VARATT_IS_EXTERNAL(const void *PTR)
Definition varatt.h:354
bool visibilitymap_clear(RelFileLocator rlocator, BlockNumber heapBlk, Buffer vmbuf, uint8 flags)
void visibilitymap_pin(Relation rel, BlockNumber heapBlk, Buffer *vmbuf)
void visibilitymap_set(BlockNumber heapBlk, Buffer vmBuf, uint8 flags, RelFileLocator rlocator)
#define VISIBILITYMAP_VALID_BITS
#define VISIBILITYMAP_ALL_FROZEN
#define VISIBILITYMAP_ALL_VISIBLE
TransactionId GetTopTransactionId(void)
Definition xact.c:428
TransactionId GetTopTransactionIdIfAny(void)
Definition xact.c:443
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition xact.c:943
bool IsInParallelMode(void)
Definition xact.c:1119
TransactionId GetCurrentTransactionId(void)
Definition xact.c:456
CommandId GetCurrentCommandId(bool used)
Definition xact.c:831
#define IsolationIsSerializable()
Definition xact.h:53
#define XLOG_INCLUDE_ORIGIN
Definition xlog.h:166
#define XLogStandbyInfoActive()
Definition xlog.h:126
uint64 XLogRecPtr
Definition xlogdefs.h:21
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition xloginsert.c:482
void XLogRegisterBufData(uint8 block_id, const void *data, uint32 len)
Definition xloginsert.c:413
bool XLogCheckBufferNeedsBackup(Buffer buffer)
void XLogRegisterData(const void *data, uint32 len)
Definition xloginsert.c:372
void XLogSetRecordFlags(uint8 flags)
Definition xloginsert.c:464
void XLogRegisterBlock(uint8 block_id, RelFileLocator *rlocator, ForkNumber forknum, BlockNumber blknum, const PageData *page, uint8 flags)
Definition xloginsert.c:317
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition xloginsert.c:246
void XLogBeginInsert(void)
Definition xloginsert.c:153
#define REGBUF_STANDARD
Definition xloginsert.h:35
#define REGBUF_KEEP_DATA
Definition xloginsert.h:36
#define REGBUF_WILL_INIT
Definition xloginsert.h:34