PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
heapam.c File Reference
#include "postgres.h"
#include "access/heapam.h"
#include "access/heaptoast.h"
#include "access/hio.h"
#include "access/multixact.h"
#include "access/subtrans.h"
#include "access/syncscan.h"
#include "access/valid.h"
#include "access/visibilitymap.h"
#include "access/xloginsert.h"
#include "catalog/pg_database.h"
#include "catalog/pg_database_d.h"
#include "commands/vacuum.h"
#include "pgstat.h"
#include "port/pg_bitutils.h"
#include "storage/lmgr.h"
#include "storage/predicate.h"
#include "storage/procarray.h"
#include "utils/datum.h"
#include "utils/injection_point.h"
#include "utils/inval.h"
#include "utils/spccache.h"
#include "utils/syscache.h"
Include dependency graph for heapam.c:

Go to the source code of this file.

Data Structures

struct  IndexDeleteCounts
 

Macros

#define LOCKMODE_from_mxstatus(status)    (tupleLockExtraInfo[TUPLOCK_from_mxstatus((status))].hwlock)
 
#define LockTupleTuplock(rel, tup, mode)    LockTuple((rel), (tup), tupleLockExtraInfo[mode].hwlock)
 
#define UnlockTupleTuplock(rel, tup, mode)    UnlockTuple((rel), (tup), tupleLockExtraInfo[mode].hwlock)
 
#define ConditionalLockTupleTuplock(rel, tup, mode, log)    ConditionalLockTuple((rel), (tup), tupleLockExtraInfo[mode].hwlock, (log))
 
#define BOTTOMUP_MAX_NBLOCKS   6
 
#define BOTTOMUP_TOLERANCE_NBLOCKS   3
 
#define TUPLOCK_from_mxstatus(status)    (MultiXactStatusLock[(status)])
 
#define FRM_NOOP   0x0001
 
#define FRM_INVALIDATE_XMAX   0x0002
 
#define FRM_RETURN_IS_XID   0x0004
 
#define FRM_RETURN_IS_MULTI   0x0008
 
#define FRM_MARK_COMMITTED   0x0010
 

Typedefs

typedef struct IndexDeleteCounts IndexDeleteCounts
 

Functions

static HeapTuple heap_prepare_insert (Relation relation, HeapTuple tup, TransactionId xid, CommandId cid, int options)
 
static XLogRecPtr log_heap_update (Relation reln, Buffer oldbuf, Buffer newbuf, HeapTuple oldtup, HeapTuple newtup, HeapTuple old_key_tuple, bool all_visible_cleared, bool new_all_visible_cleared)
 
static BitmapsetHeapDetermineColumnsInfo (Relation relation, Bitmapset *interesting_cols, Bitmapset *external_cols, HeapTuple oldtup, HeapTuple newtup, bool *has_external)
 
static bool heap_acquire_tuplock (Relation relation, ItemPointer tid, LockTupleMode mode, LockWaitPolicy wait_policy, bool *have_tuple_lock)
 
static BlockNumber heapgettup_advance_block (HeapScanDesc scan, BlockNumber block, ScanDirection dir)
 
static pg_noinline BlockNumber heapgettup_initial_block (HeapScanDesc scan, ScanDirection dir)
 
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)
 
static TM_Result heap_lock_updated_tuple (Relation rel, HeapTuple tuple, ItemPointer ctid, TransactionId xid, LockTupleMode mode)
 
static void GetMultiXactIdHintBits (MultiXactId multi, uint16 *new_infomask, uint16 *new_infomask2)
 
static TransactionId MultiXactIdGetUpdateXid (TransactionId xmax, uint16 t_infomask)
 
static bool DoesMultiXactIdConflict (MultiXactId multi, uint16 infomask, LockTupleMode lockmode, bool *current_is_member)
 
static void MultiXactIdWait (MultiXactId multi, MultiXactStatus status, uint16 infomask, Relation rel, ItemPointer ctid, XLTW_Oper oper, int *remaining)
 
static bool ConditionalMultiXactIdWait (MultiXactId multi, MultiXactStatus status, uint16 infomask, Relation rel, int *remaining, bool logLockFailure)
 
static void index_delete_sort (TM_IndexDeleteOp *delstate)
 
static int bottomup_sort_and_shrink (TM_IndexDeleteOp *delstate)
 
static XLogRecPtr log_heap_new_cid (Relation relation, HeapTuple tup)
 
static HeapTuple ExtractReplicaIdentity (Relation relation, HeapTuple tp, bool key_required, bool *copy)
 
static BlockNumber heap_scan_stream_read_next_parallel (ReadStream *stream, void *callback_private_data, void *per_buffer_data)
 
static BlockNumber heap_scan_stream_read_next_serial (ReadStream *stream, void *callback_private_data, void *per_buffer_data)
 
static BlockNumber bitmapheap_stream_read_next (ReadStream *pgsr, void *private_data, void *per_buffer_data)
 
static void initscan (HeapScanDesc scan, ScanKey key, bool keep_startblock)
 
void heap_setscanlimits (TableScanDesc sscan, BlockNumber startBlk, BlockNumber numBlks)
 
static pg_attribute_always_inline int page_collect_tuples (HeapScanDesc scan, Snapshot snapshot, Page page, Buffer buffer, BlockNumber block, int lines, bool all_visible, bool check_serializable)
 
void heap_prepare_pagescan (TableScanDesc sscan)
 
static void heap_fetch_next_buffer (HeapScanDesc scan, ScanDirection dir)
 
static Page heapgettup_start_page (HeapScanDesc scan, ScanDirection dir, int *linesleft, OffsetNumber *lineoff)
 
static Page heapgettup_continue_page (HeapScanDesc scan, ScanDirection dir, int *linesleft, OffsetNumber *lineoff)
 
static void heapgettup (HeapScanDesc scan, ScanDirection dir, int nkeys, ScanKey key)
 
static void heapgettup_pagemode (HeapScanDesc scan, ScanDirection dir, int nkeys, ScanKey key)
 
TableScanDesc heap_beginscan (Relation relation, Snapshot snapshot, int nkeys, ScanKey key, ParallelTableScanDesc parallel_scan, uint32 flags)
 
void heap_rescan (TableScanDesc sscan, ScanKey key, bool set_params, bool allow_strat, bool allow_sync, bool allow_pagemode)
 
void heap_endscan (TableScanDesc sscan)
 
HeapTuple heap_getnext (TableScanDesc sscan, ScanDirection direction)
 
bool heap_getnextslot (TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
 
void heap_set_tidrange (TableScanDesc sscan, ItemPointer mintid, ItemPointer maxtid)
 
bool heap_getnextslot_tidrange (TableScanDesc sscan, ScanDirection direction, TupleTableSlot *slot)
 
bool heap_fetch (Relation relation, Snapshot snapshot, HeapTuple tuple, Buffer *userbuf, bool keep_buf)
 
bool heap_hot_search_buffer (ItemPointer tid, Relation relation, Buffer buffer, Snapshot snapshot, HeapTuple heapTuple, bool *all_dead, bool first_call)
 
void heap_get_latest_tid (TableScanDesc sscan, ItemPointer tid)
 
static void UpdateXmaxHintBits (HeapTupleHeader tuple, Buffer buffer, TransactionId xid)
 
BulkInsertState GetBulkInsertState (void)
 
void FreeBulkInsertState (BulkInsertState bistate)
 
void ReleaseBulkInsertStatePin (BulkInsertState bistate)
 
void heap_insert (Relation relation, HeapTuple tup, CommandId cid, int options, BulkInsertState bistate)
 
static int heap_multi_insert_pages (HeapTuple *heaptuples, int done, int ntuples, Size saveFreeSpace)
 
void heap_multi_insert (Relation relation, TupleTableSlot **slots, int ntuples, CommandId cid, int options, BulkInsertState bistate)
 
void simple_heap_insert (Relation relation, HeapTuple tup)
 
static uint8 compute_infobits (uint16 infomask, uint16 infomask2)
 
static bool xmax_infomask_changed (uint16 new_infomask, uint16 old_infomask)
 
TM_Result heap_delete (Relation relation, ItemPointer tid, CommandId cid, Snapshot crosscheck, bool wait, TM_FailureData *tmfd, bool changingPart)
 
void simple_heap_delete (Relation relation, ItemPointer tid)
 
TM_Result heap_update (Relation relation, ItemPointer otid, HeapTuple newtup, CommandId cid, Snapshot crosscheck, bool wait, TM_FailureData *tmfd, LockTupleMode *lockmode, TU_UpdateIndexes *update_indexes)
 
static bool heap_attr_equals (TupleDesc tupdesc, int attrnum, Datum value1, Datum value2, bool isnull1, bool isnull2)
 
void simple_heap_update (Relation relation, ItemPointer otid, HeapTuple tup, TU_UpdateIndexes *update_indexes)
 
static MultiXactStatus get_mxact_status_for_lock (LockTupleMode mode, bool is_update)
 
TM_Result heap_lock_tuple (Relation relation, HeapTuple tuple, CommandId cid, LockTupleMode mode, LockWaitPolicy wait_policy, bool follow_updates, Buffer *buffer, TM_FailureData *tmfd)
 
static TM_Result test_lockmode_for_conflict (MultiXactStatus status, TransactionId xid, LockTupleMode mode, HeapTuple tup, bool *needwait)
 
static TM_Result heap_lock_updated_tuple_rec (Relation rel, ItemPointer tid, TransactionId xid, LockTupleMode mode)
 
void heap_finish_speculative (Relation relation, ItemPointer tid)
 
void heap_abort_speculative (Relation relation, ItemPointer tid)
 
bool heap_inplace_lock (Relation relation, HeapTuple oldtup_ptr, Buffer buffer, void(*release_callback)(void *), void *arg)
 
void heap_inplace_update_and_unlock (Relation relation, HeapTuple oldtup, HeapTuple tuple, Buffer buffer)
 
void heap_inplace_unlock (Relation relation, HeapTuple oldtup, Buffer buffer)
 
static TransactionId FreezeMultiXactId (MultiXactId multi, uint16 t_infomask, const struct VacuumCutoffs *cutoffs, uint16 *flags, HeapPageFreeze *pagefrz)
 
bool heap_prepare_freeze_tuple (HeapTupleHeader tuple, const struct VacuumCutoffs *cutoffs, HeapPageFreeze *pagefrz, HeapTupleFreeze *frz, bool *totally_frozen)
 
void heap_pre_freeze_checks (Buffer buffer, HeapTupleFreeze *tuples, int ntuples)
 
void heap_freeze_prepared_tuples (Buffer buffer, HeapTupleFreeze *tuples, int ntuples)
 
bool heap_freeze_tuple (HeapTupleHeader tuple, TransactionId relfrozenxid, TransactionId relminmxid, TransactionId FreezeLimit, TransactionId MultiXactCutoff)
 
TransactionId HeapTupleGetUpdateXid (const HeapTupleHeaderData *tup)
 
static bool Do_MultiXactIdWait (MultiXactId multi, MultiXactStatus status, uint16 infomask, bool nowait, Relation rel, ItemPointer ctid, XLTW_Oper oper, int *remaining, bool logLockFailure)
 
bool heap_tuple_needs_eventual_freeze (HeapTupleHeader tuple)
 
bool heap_tuple_should_freeze (HeapTupleHeader tuple, const struct VacuumCutoffs *cutoffs, TransactionId *NoFreezePageRelfrozenXid, MultiXactId *NoFreezePageRelminMxid)
 
void HeapTupleHeaderAdvanceConflictHorizon (HeapTupleHeader tuple, TransactionId *snapshotConflictHorizon)
 
static void index_delete_check_htid (TM_IndexDeleteOp *delstate, Page page, OffsetNumber maxoff, ItemPointer htid, TM_IndexStatus *istatus)
 
TransactionId heap_index_delete_tuples (Relation rel, TM_IndexDeleteOp *delstate)
 
static int index_delete_sort_cmp (TM_IndexDelete *deltid1, TM_IndexDelete *deltid2)
 
static int bottomup_nblocksfavorable (IndexDeleteCounts *blockgroups, int nblockgroups, TM_IndexDelete *deltids)
 
static int bottomup_sort_and_shrink_cmp (const void *arg1, const void *arg2)
 
XLogRecPtr log_heap_visible (Relation rel, Buffer heap_buffer, Buffer vm_buffer, TransactionId snapshotConflictHorizon, uint8 vmflags)
 
void HeapCheckForSerializableConflictOut (bool visible, Relation relation, HeapTuple tuple, Buffer buffer, Snapshot snapshot)
 

Variables

struct {
   LOCKMODE   hwlock
 
   int   lockstatus
 
   int   updstatus
 
tupleLockExtraInfo [MaxLockTupleMode+1]
 
static const int MultiXactStatusLock [MaxMultiXactStatus+1]
 

Macro Definition Documentation

◆ BOTTOMUP_MAX_NBLOCKS

#define BOTTOMUP_MAX_NBLOCKS   6

Definition at line 184 of file heapam.c.

◆ BOTTOMUP_TOLERANCE_NBLOCKS

#define BOTTOMUP_TOLERANCE_NBLOCKS   3

Definition at line 185 of file heapam.c.

◆ ConditionalLockTupleTuplock

#define ConditionalLockTupleTuplock (   rel,
  tup,
  mode,
  log 
)     ConditionalLockTuple((rel), (tup), tupleLockExtraInfo[mode].hwlock, (log))

Definition at line 166 of file heapam.c.

◆ FRM_INVALIDATE_XMAX

#define FRM_INVALIDATE_XMAX   0x0002

Definition at line 6608 of file heapam.c.

◆ FRM_MARK_COMMITTED

#define FRM_MARK_COMMITTED   0x0010

Definition at line 6611 of file heapam.c.

◆ FRM_NOOP

#define FRM_NOOP   0x0001

Definition at line 6607 of file heapam.c.

◆ FRM_RETURN_IS_MULTI

#define FRM_RETURN_IS_MULTI   0x0008

Definition at line 6610 of file heapam.c.

◆ FRM_RETURN_IS_XID

#define FRM_RETURN_IS_XID   0x0004

Definition at line 6609 of file heapam.c.

◆ LOCKMODE_from_mxstatus

#define LOCKMODE_from_mxstatus (   status)     (tupleLockExtraInfo[TUPLOCK_from_mxstatus((status))].hwlock)

Definition at line 154 of file heapam.c.

◆ LockTupleTuplock

#define LockTupleTuplock (   rel,
  tup,
  mode 
)     LockTuple((rel), (tup), tupleLockExtraInfo[mode].hwlock)

Definition at line 162 of file heapam.c.

◆ TUPLOCK_from_mxstatus

#define TUPLOCK_from_mxstatus (   status)     (MultiXactStatusLock[(status)])

Definition at line 213 of file heapam.c.

◆ UnlockTupleTuplock

#define UnlockTupleTuplock (   rel,
  tup,
  mode 
)     UnlockTuple((rel), (tup), tupleLockExtraInfo[mode].hwlock)

Definition at line 164 of file heapam.c.

Typedef Documentation

◆ IndexDeleteCounts

Function Documentation

◆ bitmapheap_stream_read_next()

static BlockNumber bitmapheap_stream_read_next ( ReadStream pgsr,
void *  private_data,
void *  per_buffer_data 
)
static

Definition at line 289 of file heapam.c.

291{
292 TBMIterateResult *tbmres = per_buffer_data;
293 BitmapHeapScanDesc bscan = (BitmapHeapScanDesc) private_data;
294 HeapScanDesc hscan = (HeapScanDesc) bscan;
295 TableScanDesc sscan = &hscan->rs_base;
296
297 for (;;)
298 {
300
301 /* no more entries in the bitmap */
302 if (!tbm_iterate(&sscan->st.rs_tbmiterator, tbmres))
303 return InvalidBlockNumber;
304
305 /*
306 * Ignore any claimed entries past what we think is the end of the
307 * relation. It may have been extended after the start of our scan (we
308 * only hold an AccessShareLock, and it could be inserts from this
309 * backend). We don't take this optimization in SERIALIZABLE
310 * isolation though, as we need to examine all invisible tuples
311 * reachable by the index.
312 */
314 tbmres->blockno >= hscan->rs_nblocks)
315 continue;
316
317 return tbmres->blockno;
318 }
319
320 /* not reachable */
321 Assert(false);
322}
#define InvalidBlockNumber
Definition: block.h:33
Assert(PointerIsAligned(start, uint64))
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:100
struct BitmapHeapScanDescData * BitmapHeapScanDesc
Definition: heapam.h:108
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
BlockNumber rs_nblocks
Definition: heapam.h:59
TableScanDescData rs_base
Definition: heapam.h:56
BlockNumber blockno
Definition: tidbitmap.h:64
TBMIterator rs_tbmiterator
Definition: relscan.h:47
union TableScanDescData::@49 st
bool tbm_iterate(TBMIterator *iterator, TBMIterateResult *tbmres)
Definition: tidbitmap.c:1617
#define IsolationIsSerializable()
Definition: xact.h:52

References Assert(), TBMIterateResult::blockno, CHECK_FOR_INTERRUPTS, InvalidBlockNumber, IsolationIsSerializable, HeapScanDescData::rs_base, HeapScanDescData::rs_nblocks, TableScanDescData::rs_tbmiterator, TableScanDescData::st, and tbm_iterate().

Referenced by heap_beginscan().

◆ bottomup_nblocksfavorable()

static int bottomup_nblocksfavorable ( IndexDeleteCounts blockgroups,
int  nblockgroups,
TM_IndexDelete deltids 
)
static

Definition at line 8515 of file heapam.c.

8517{
8518 int64 lastblock = -1;
8519 int nblocksfavorable = 0;
8520
8521 Assert(nblockgroups >= 1);
8522 Assert(nblockgroups <= BOTTOMUP_MAX_NBLOCKS);
8523
8524 /*
8525 * We tolerate heap blocks that will be accessed only slightly out of
8526 * physical order. Small blips occur when a pair of almost-contiguous
8527 * blocks happen to fall into different buckets (perhaps due only to a
8528 * small difference in npromisingtids that the bucketing scheme didn't
8529 * quite manage to ignore). We effectively ignore these blips by applying
8530 * a small tolerance. The precise tolerance we use is a little arbitrary,
8531 * but it works well enough in practice.
8532 */
8533 for (int b = 0; b < nblockgroups; b++)
8534 {
8535 IndexDeleteCounts *group = blockgroups + b;
8536 TM_IndexDelete *firstdtid = deltids + group->ifirsttid;
8537 BlockNumber block = ItemPointerGetBlockNumber(&firstdtid->tid);
8538
8539 if (lastblock != -1 &&
8540 ((int64) block < lastblock - BOTTOMUP_TOLERANCE_NBLOCKS ||
8541 (int64) block > lastblock + BOTTOMUP_TOLERANCE_NBLOCKS))
8542 break;
8543
8544 nblocksfavorable++;
8545 lastblock = block;
8546 }
8547
8548 /* Always indicate that there is at least 1 favorable block */
8549 Assert(nblocksfavorable >= 1);
8550
8551 return nblocksfavorable;
8552}
uint32 BlockNumber
Definition: block.h:31
int64_t int64
Definition: c.h:499
#define BOTTOMUP_TOLERANCE_NBLOCKS
Definition: heapam.c:185
#define BOTTOMUP_MAX_NBLOCKS
Definition: heapam.c:184
int b
Definition: isn.c:74
static BlockNumber ItemPointerGetBlockNumber(const ItemPointerData *pointer)
Definition: itemptr.h:103
int16 ifirsttid
Definition: heapam.c:195
ItemPointerData tid
Definition: tableam.h:206

References Assert(), b, BOTTOMUP_MAX_NBLOCKS, BOTTOMUP_TOLERANCE_NBLOCKS, IndexDeleteCounts::ifirsttid, ItemPointerGetBlockNumber(), and TM_IndexDelete::tid.

Referenced by bottomup_sort_and_shrink().

◆ bottomup_sort_and_shrink()

static int bottomup_sort_and_shrink ( TM_IndexDeleteOp delstate)
static

Definition at line 8631 of file heapam.c.

8632{
8633 IndexDeleteCounts *blockgroups;
8634 TM_IndexDelete *reordereddeltids;
8636 int nblockgroups = 0;
8637 int ncopied = 0;
8638 int nblocksfavorable = 0;
8639
8640 Assert(delstate->bottomup);
8641 Assert(delstate->ndeltids > 0);
8642
8643 /* Calculate per-heap-block count of TIDs */
8644 blockgroups = palloc(sizeof(IndexDeleteCounts) * delstate->ndeltids);
8645 for (int i = 0; i < delstate->ndeltids; i++)
8646 {
8647 TM_IndexDelete *ideltid = &delstate->deltids[i];
8648 TM_IndexStatus *istatus = delstate->status + ideltid->id;
8649 ItemPointer htid = &ideltid->tid;
8650 bool promising = istatus->promising;
8651
8652 if (curblock != ItemPointerGetBlockNumber(htid))
8653 {
8654 /* New block group */
8655 nblockgroups++;
8656
8657 Assert(curblock < ItemPointerGetBlockNumber(htid) ||
8658 !BlockNumberIsValid(curblock));
8659
8660 curblock = ItemPointerGetBlockNumber(htid);
8661 blockgroups[nblockgroups - 1].ifirsttid = i;
8662 blockgroups[nblockgroups - 1].ntids = 1;
8663 blockgroups[nblockgroups - 1].npromisingtids = 0;
8664 }
8665 else
8666 {
8667 blockgroups[nblockgroups - 1].ntids++;
8668 }
8669
8670 if (promising)
8671 blockgroups[nblockgroups - 1].npromisingtids++;
8672 }
8673
8674 /*
8675 * We're about ready to sort block groups to determine the optimal order
8676 * for visiting heap blocks. But before we do, round the number of
8677 * promising tuples for each block group up to the next power-of-two,
8678 * unless it is very low (less than 4), in which case we round up to 4.
8679 * npromisingtids is far too noisy to trust when choosing between a pair
8680 * of block groups that both have very low values.
8681 *
8682 * This scheme divides heap blocks/block groups into buckets. Each bucket
8683 * contains blocks that have _approximately_ the same number of promising
8684 * TIDs as each other. The goal is to ignore relatively small differences
8685 * in the total number of promising entries, so that the whole process can
8686 * give a little weight to heapam factors (like heap block locality)
8687 * instead. This isn't a trade-off, really -- we have nothing to lose. It
8688 * would be foolish to interpret small differences in npromisingtids
8689 * values as anything more than noise.
8690 *
8691 * We tiebreak on nhtids when sorting block group subsets that have the
8692 * same npromisingtids, but this has the same issues as npromisingtids,
8693 * and so nhtids is subject to the same power-of-two bucketing scheme. The
8694 * only reason that we don't fix nhtids in the same way here too is that
8695 * we'll need accurate nhtids values after the sort. We handle nhtids
8696 * bucketization dynamically instead (in the sort comparator).
8697 *
8698 * See bottomup_nblocksfavorable() for a full explanation of when and how
8699 * heap locality/favorable blocks can significantly influence when and how
8700 * heap blocks are accessed.
8701 */
8702 for (int b = 0; b < nblockgroups; b++)
8703 {
8704 IndexDeleteCounts *group = blockgroups + b;
8705
8706 /* Better off falling back on nhtids with low npromisingtids */
8707 if (group->npromisingtids <= 4)
8708 group->npromisingtids = 4;
8709 else
8710 group->npromisingtids =
8712 }
8713
8714 /* Sort groups and rearrange caller's deltids array */
8715 qsort(blockgroups, nblockgroups, sizeof(IndexDeleteCounts),
8717 reordereddeltids = palloc(delstate->ndeltids * sizeof(TM_IndexDelete));
8718
8719 nblockgroups = Min(BOTTOMUP_MAX_NBLOCKS, nblockgroups);
8720 /* Determine number of favorable blocks at the start of final deltids */
8721 nblocksfavorable = bottomup_nblocksfavorable(blockgroups, nblockgroups,
8722 delstate->deltids);
8723
8724 for (int b = 0; b < nblockgroups; b++)
8725 {
8726 IndexDeleteCounts *group = blockgroups + b;
8727 TM_IndexDelete *firstdtid = delstate->deltids + group->ifirsttid;
8728
8729 memcpy(reordereddeltids + ncopied, firstdtid,
8730 sizeof(TM_IndexDelete) * group->ntids);
8731 ncopied += group->ntids;
8732 }
8733
8734 /* Copy final grouped and sorted TIDs back into start of caller's array */
8735 memcpy(delstate->deltids, reordereddeltids,
8736 sizeof(TM_IndexDelete) * ncopied);
8737 delstate->ndeltids = ncopied;
8738
8739 pfree(reordereddeltids);
8740 pfree(blockgroups);
8741
8742 return nblocksfavorable;
8743}
static bool BlockNumberIsValid(BlockNumber blockNumber)
Definition: block.h:71
#define Min(x, y)
Definition: c.h:975
uint32_t uint32
Definition: c.h:502
static int bottomup_nblocksfavorable(IndexDeleteCounts *blockgroups, int nblockgroups, TM_IndexDelete *deltids)
Definition: heapam.c:8515
static int bottomup_sort_and_shrink_cmp(const void *arg1, const void *arg2)
Definition: heapam.c:8558
int i
Definition: isn.c:77
void pfree(void *pointer)
Definition: mcxt.c:2147
void * palloc(Size size)
Definition: mcxt.c:1940
static uint32 pg_nextpower2_32(uint32 num)
Definition: pg_bitutils.h:189
#define qsort(a, b, c, d)
Definition: port.h:479
int16 npromisingtids
Definition: heapam.c:193
TM_IndexStatus * status
Definition: tableam.h:248
TM_IndexDelete * deltids
Definition: tableam.h:247
bool promising
Definition: tableam.h:216

References Assert(), b, BlockNumberIsValid(), TM_IndexDeleteOp::bottomup, BOTTOMUP_MAX_NBLOCKS, bottomup_nblocksfavorable(), bottomup_sort_and_shrink_cmp(), TM_IndexDeleteOp::deltids, i, TM_IndexDelete::id, IndexDeleteCounts::ifirsttid, InvalidBlockNumber, ItemPointerGetBlockNumber(), Min, TM_IndexDeleteOp::ndeltids, IndexDeleteCounts::npromisingtids, IndexDeleteCounts::ntids, palloc(), pfree(), pg_nextpower2_32(), TM_IndexStatus::promising, qsort, TM_IndexDeleteOp::status, and TM_IndexDelete::tid.

Referenced by heap_index_delete_tuples().

◆ bottomup_sort_and_shrink_cmp()

static int bottomup_sort_and_shrink_cmp ( const void *  arg1,
const void *  arg2 
)
static

Definition at line 8558 of file heapam.c.

8559{
8560 const IndexDeleteCounts *group1 = (const IndexDeleteCounts *) arg1;
8561 const IndexDeleteCounts *group2 = (const IndexDeleteCounts *) arg2;
8562
8563 /*
8564 * Most significant field is npromisingtids (which we invert the order of
8565 * so as to sort in desc order).
8566 *
8567 * Caller should have already normalized npromisingtids fields into
8568 * power-of-two values (buckets).
8569 */
8570 if (group1->npromisingtids > group2->npromisingtids)
8571 return -1;
8572 if (group1->npromisingtids < group2->npromisingtids)
8573 return 1;
8574
8575 /*
8576 * Tiebreak: desc ntids sort order.
8577 *
8578 * We cannot expect power-of-two values for ntids fields. We should
8579 * behave as if they were already rounded up for us instead.
8580 */
8581 if (group1->ntids != group2->ntids)
8582 {
8583 uint32 ntids1 = pg_nextpower2_32((uint32) group1->ntids);
8584 uint32 ntids2 = pg_nextpower2_32((uint32) group2->ntids);
8585
8586 if (ntids1 > ntids2)
8587 return -1;
8588 if (ntids1 < ntids2)
8589 return 1;
8590 }
8591
8592 /*
8593 * Tiebreak: asc offset-into-deltids-for-block (offset to first TID for
8594 * block in deltids array) order.
8595 *
8596 * This is equivalent to sorting in ascending heap block number order
8597 * (among otherwise equal subsets of the array). This approach allows us
8598 * to avoid accessing the out-of-line TID. (We rely on the assumption
8599 * that the deltids array was sorted in ascending heap TID order when
8600 * these offsets to the first TID from each heap block group were formed.)
8601 */
8602 if (group1->ifirsttid > group2->ifirsttid)
8603 return 1;
8604 if (group1->ifirsttid < group2->ifirsttid)
8605 return -1;
8606
8608
8609 return 0;
8610}
#define pg_unreachable()
Definition: c.h:332

References IndexDeleteCounts::ifirsttid, IndexDeleteCounts::npromisingtids, IndexDeleteCounts::ntids, pg_nextpower2_32(), and pg_unreachable.

Referenced by bottomup_sort_and_shrink().

◆ compute_infobits()

static uint8 compute_infobits ( uint16  infomask,
uint16  infomask2 
)
static

Definition at line 2700 of file heapam.c.

2701{
2702 return
2703 ((infomask & HEAP_XMAX_IS_MULTI) != 0 ? XLHL_XMAX_IS_MULTI : 0) |
2704 ((infomask & HEAP_XMAX_LOCK_ONLY) != 0 ? XLHL_XMAX_LOCK_ONLY : 0) |
2705 ((infomask & HEAP_XMAX_EXCL_LOCK) != 0 ? XLHL_XMAX_EXCL_LOCK : 0) |
2706 /* note we ignore HEAP_XMAX_SHR_LOCK here */
2707 ((infomask & HEAP_XMAX_KEYSHR_LOCK) != 0 ? XLHL_XMAX_KEYSHR_LOCK : 0) |
2708 ((infomask2 & HEAP_KEYS_UPDATED) != 0 ?
2709 XLHL_KEYS_UPDATED : 0);
2710}
#define XLHL_XMAX_KEYSHR_LOCK
Definition: heapam_xlog.h:389
#define XLHL_XMAX_IS_MULTI
Definition: heapam_xlog.h:386
#define XLHL_XMAX_LOCK_ONLY
Definition: heapam_xlog.h:387
#define XLHL_XMAX_EXCL_LOCK
Definition: heapam_xlog.h:388
#define XLHL_KEYS_UPDATED
Definition: heapam_xlog.h:390
#define HEAP_KEYS_UPDATED
Definition: htup_details.h:289
#define HEAP_XMAX_LOCK_ONLY
Definition: htup_details.h:197
#define HEAP_XMAX_IS_MULTI
Definition: htup_details.h:209
#define HEAP_XMAX_EXCL_LOCK
Definition: htup_details.h:196
#define HEAP_XMAX_KEYSHR_LOCK
Definition: htup_details.h:194

References HEAP_KEYS_UPDATED, HEAP_XMAX_EXCL_LOCK, HEAP_XMAX_IS_MULTI, HEAP_XMAX_KEYSHR_LOCK, HEAP_XMAX_LOCK_ONLY, XLHL_KEYS_UPDATED, XLHL_XMAX_EXCL_LOCK, XLHL_XMAX_IS_MULTI, XLHL_XMAX_KEYSHR_LOCK, and XLHL_XMAX_LOCK_ONLY.

Referenced by heap_abort_speculative(), heap_delete(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_update(), and log_heap_update().

◆ compute_new_xmax_infomask()

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 
)
static

Definition at line 5290 of file heapam.c.

5295{
5296 TransactionId new_xmax;
5297 uint16 new_infomask,
5298 new_infomask2;
5299
5301
5302l5:
5303 new_infomask = 0;
5304 new_infomask2 = 0;
5305 if (old_infomask & HEAP_XMAX_INVALID)
5306 {
5307 /*
5308 * No previous locker; we just insert our own TransactionId.
5309 *
5310 * Note that it's critical that this case be the first one checked,
5311 * because there are several blocks below that come back to this one
5312 * to implement certain optimizations; old_infomask might contain
5313 * other dirty bits in those cases, but we don't really care.
5314 */
5315 if (is_update)
5316 {
5317 new_xmax = add_to_xmax;
5318 if (mode == LockTupleExclusive)
5319 new_infomask2 |= HEAP_KEYS_UPDATED;
5320 }
5321 else
5322 {
5323 new_infomask |= HEAP_XMAX_LOCK_ONLY;
5324 switch (mode)
5325 {
5326 case LockTupleKeyShare:
5327 new_xmax = add_to_xmax;
5328 new_infomask |= HEAP_XMAX_KEYSHR_LOCK;
5329 break;
5330 case LockTupleShare:
5331 new_xmax = add_to_xmax;
5332 new_infomask |= HEAP_XMAX_SHR_LOCK;
5333 break;
5335 new_xmax = add_to_xmax;
5336 new_infomask |= HEAP_XMAX_EXCL_LOCK;
5337 break;
5338 case LockTupleExclusive:
5339 new_xmax = add_to_xmax;
5340 new_infomask |= HEAP_XMAX_EXCL_LOCK;
5341 new_infomask2 |= HEAP_KEYS_UPDATED;
5342 break;
5343 default:
5344 new_xmax = InvalidTransactionId; /* silence compiler */
5345 elog(ERROR, "invalid lock mode");
5346 }
5347 }
5348 }
5349 else if (old_infomask & HEAP_XMAX_IS_MULTI)
5350 {
5351 MultiXactStatus new_status;
5352
5353 /*
5354 * Currently we don't allow XMAX_COMMITTED to be set for multis, so
5355 * cross-check.
5356 */
5357 Assert(!(old_infomask & HEAP_XMAX_COMMITTED));
5358
5359 /*
5360 * A multixact together with LOCK_ONLY set but neither lock bit set
5361 * (i.e. a pg_upgraded share locked tuple) cannot possibly be running
5362 * anymore. This check is critical for databases upgraded by
5363 * pg_upgrade; both MultiXactIdIsRunning and MultiXactIdExpand assume
5364 * that such multis are never passed.
5365 */
5366 if (HEAP_LOCKED_UPGRADED(old_infomask))
5367 {
5368 old_infomask &= ~HEAP_XMAX_IS_MULTI;
5369 old_infomask |= HEAP_XMAX_INVALID;
5370 goto l5;
5371 }
5372
5373 /*
5374 * If the XMAX is already a MultiXactId, then we need to expand it to
5375 * include add_to_xmax; but if all the members were lockers and are
5376 * all gone, we can do away with the IS_MULTI bit and just set
5377 * add_to_xmax as the only locker/updater. If all lockers are gone
5378 * and we have an updater that aborted, we can also do without a
5379 * multi.
5380 *
5381 * The cost of doing GetMultiXactIdMembers would be paid by
5382 * MultiXactIdExpand if we weren't to do this, so this check is not
5383 * incurring extra work anyhow.
5384 */
5385 if (!MultiXactIdIsRunning(xmax, HEAP_XMAX_IS_LOCKED_ONLY(old_infomask)))
5386 {
5387 if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask) ||
5389 old_infomask)))
5390 {
5391 /*
5392 * Reset these bits and restart; otherwise fall through to
5393 * create a new multi below.
5394 */
5395 old_infomask &= ~HEAP_XMAX_IS_MULTI;
5396 old_infomask |= HEAP_XMAX_INVALID;
5397 goto l5;
5398 }
5399 }
5400
5401 new_status = get_mxact_status_for_lock(mode, is_update);
5402
5403 new_xmax = MultiXactIdExpand((MultiXactId) xmax, add_to_xmax,
5404 new_status);
5405 GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2);
5406 }
5407 else if (old_infomask & HEAP_XMAX_COMMITTED)
5408 {
5409 /*
5410 * It's a committed update, so we need to preserve him as updater of
5411 * the tuple.
5412 */
5413 MultiXactStatus status;
5414 MultiXactStatus new_status;
5415
5416 if (old_infomask2 & HEAP_KEYS_UPDATED)
5417 status = MultiXactStatusUpdate;
5418 else
5420
5421 new_status = get_mxact_status_for_lock(mode, is_update);
5422
5423 /*
5424 * since it's not running, it's obviously impossible for the old
5425 * updater to be identical to the current one, so we need not check
5426 * for that case as we do in the block above.
5427 */
5428 new_xmax = MultiXactIdCreate(xmax, status, add_to_xmax, new_status);
5429 GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2);
5430 }
5431 else if (TransactionIdIsInProgress(xmax))
5432 {
5433 /*
5434 * If the XMAX is a valid, in-progress TransactionId, then we need to
5435 * create a new MultiXactId that includes both the old locker or
5436 * updater and our own TransactionId.
5437 */
5438 MultiXactStatus new_status;
5439 MultiXactStatus old_status;
5440 LockTupleMode old_mode;
5441
5442 if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask))
5443 {
5444 if (HEAP_XMAX_IS_KEYSHR_LOCKED(old_infomask))
5445 old_status = MultiXactStatusForKeyShare;
5446 else if (HEAP_XMAX_IS_SHR_LOCKED(old_infomask))
5447 old_status = MultiXactStatusForShare;
5448 else if (HEAP_XMAX_IS_EXCL_LOCKED(old_infomask))
5449 {
5450 if (old_infomask2 & HEAP_KEYS_UPDATED)
5451 old_status = MultiXactStatusForUpdate;
5452 else
5453 old_status = MultiXactStatusForNoKeyUpdate;
5454 }
5455 else
5456 {
5457 /*
5458 * LOCK_ONLY can be present alone only when a page has been
5459 * upgraded by pg_upgrade. But in that case,
5460 * TransactionIdIsInProgress() should have returned false. We
5461 * assume it's no longer locked in this case.
5462 */
5463 elog(WARNING, "LOCK_ONLY found for Xid in progress %u", xmax);
5464 old_infomask |= HEAP_XMAX_INVALID;
5465 old_infomask &= ~HEAP_XMAX_LOCK_ONLY;
5466 goto l5;
5467 }
5468 }
5469 else
5470 {
5471 /* it's an update, but which kind? */
5472 if (old_infomask2 & HEAP_KEYS_UPDATED)
5473 old_status = MultiXactStatusUpdate;
5474 else
5475 old_status = MultiXactStatusNoKeyUpdate;
5476 }
5477
5478 old_mode = TUPLOCK_from_mxstatus(old_status);
5479
5480 /*
5481 * If the lock to be acquired is for the same TransactionId as the
5482 * existing lock, there's an optimization possible: consider only the
5483 * strongest of both locks as the only one present, and restart.
5484 */
5485 if (xmax == add_to_xmax)
5486 {
5487 /*
5488 * Note that it's not possible for the original tuple to be
5489 * updated: we wouldn't be here because the tuple would have been
5490 * invisible and we wouldn't try to update it. As a subtlety,
5491 * this code can also run when traversing an update chain to lock
5492 * future versions of a tuple. But we wouldn't be here either,
5493 * because the add_to_xmax would be different from the original
5494 * updater.
5495 */
5496 Assert(HEAP_XMAX_IS_LOCKED_ONLY(old_infomask));
5497
5498 /* acquire the strongest of both */
5499 if (mode < old_mode)
5500 mode = old_mode;
5501 /* mustn't touch is_update */
5502
5503 old_infomask |= HEAP_XMAX_INVALID;
5504 goto l5;
5505 }
5506
5507 /* otherwise, just fall back to creating a new multixact */
5508 new_status = get_mxact_status_for_lock(mode, is_update);
5509 new_xmax = MultiXactIdCreate(xmax, old_status,
5510 add_to_xmax, new_status);
5511 GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2);
5512 }
5513 else if (!HEAP_XMAX_IS_LOCKED_ONLY(old_infomask) &&
5515 {
5516 /*
5517 * It's a committed update, so we gotta preserve him as updater of the
5518 * tuple.
5519 */
5520 MultiXactStatus status;
5521 MultiXactStatus new_status;
5522
5523 if (old_infomask2 & HEAP_KEYS_UPDATED)
5524 status = MultiXactStatusUpdate;
5525 else
5527
5528 new_status = get_mxact_status_for_lock(mode, is_update);
5529
5530 /*
5531 * since it's not running, it's obviously impossible for the old
5532 * updater to be identical to the current one, so we need not check
5533 * for that case as we do in the block above.
5534 */
5535 new_xmax = MultiXactIdCreate(xmax, status, add_to_xmax, new_status);
5536 GetMultiXactIdHintBits(new_xmax, &new_infomask, &new_infomask2);
5537 }
5538 else
5539 {
5540 /*
5541 * Can get here iff the locking/updating transaction was running when
5542 * the infomask was extracted from the tuple, but finished before
5543 * TransactionIdIsInProgress got to run. Deal with it as if there was
5544 * no locker at all in the first place.
5545 */
5546 old_infomask |= HEAP_XMAX_INVALID;
5547 goto l5;
5548 }
5549
5550 *result_infomask = new_infomask;
5551 *result_infomask2 = new_infomask2;
5552 *result_xmax = new_xmax;
5553}
TransactionId MultiXactId
Definition: c.h:633
uint16_t uint16
Definition: c.h:501
uint32 TransactionId
Definition: c.h:623
#define WARNING
Definition: elog.h:36
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
static void GetMultiXactIdHintBits(MultiXactId multi, uint16 *new_infomask, uint16 *new_infomask2)
Definition: heapam.c:7402
#define TUPLOCK_from_mxstatus(status)
Definition: heapam.c:213
static TransactionId MultiXactIdGetUpdateXid(TransactionId xmax, uint16 t_infomask)
Definition: heapam.c:7483
static MultiXactStatus get_mxact_status_for_lock(LockTupleMode mode, bool is_update)
Definition: heapam.c:4495
static bool HEAP_XMAX_IS_SHR_LOCKED(int16 infomask)
Definition: htup_details.h:263
#define HEAP_XMAX_SHR_LOCK
Definition: htup_details.h:200
static bool HEAP_XMAX_IS_LOCKED_ONLY(uint16 infomask)
Definition: htup_details.h:226
static bool HEAP_XMAX_IS_KEYSHR_LOCKED(int16 infomask)
Definition: htup_details.h:275
#define HEAP_XMAX_COMMITTED
Definition: htup_details.h:207
#define HEAP_XMAX_INVALID
Definition: htup_details.h:208
static bool HEAP_LOCKED_UPGRADED(uint16 infomask)
Definition: htup_details.h:251
static bool HEAP_XMAX_IS_EXCL_LOCKED(int16 infomask)
Definition: htup_details.h:269
LockTupleMode
Definition: lockoptions.h:50
@ LockTupleExclusive
Definition: lockoptions.h:58
@ LockTupleNoKeyExclusive
Definition: lockoptions.h:56
@ LockTupleShare
Definition: lockoptions.h:54
@ LockTupleKeyShare
Definition: lockoptions.h:52
MultiXactId MultiXactIdExpand(MultiXactId multi, TransactionId xid, MultiXactStatus status)
Definition: multixact.c:487
bool MultiXactIdIsRunning(MultiXactId multi, bool isLockOnly)
Definition: multixact.c:599
MultiXactId MultiXactIdCreate(TransactionId xid1, MultiXactStatus status1, TransactionId xid2, MultiXactStatus status2)
Definition: multixact.c:434
MultiXactStatus
Definition: multixact.h:38
@ MultiXactStatusForShare
Definition: multixact.h:40
@ MultiXactStatusForNoKeyUpdate
Definition: multixact.h:41
@ MultiXactStatusNoKeyUpdate
Definition: multixact.h:44
@ MultiXactStatusUpdate
Definition: multixact.h:46
@ MultiXactStatusForUpdate
Definition: multixact.h:42
@ MultiXactStatusForKeyShare
Definition: multixact.h:39
static PgChecksumMode mode
Definition: pg_checksums.c:55
bool TransactionIdIsInProgress(TransactionId xid)
Definition: procarray.c:1402
bool TransactionIdDidCommit(TransactionId transactionId)
Definition: transam.c:126
#define InvalidTransactionId
Definition: transam.h:31
bool TransactionIdIsCurrentTransactionId(TransactionId xid)
Definition: xact.c:941

References Assert(), elog, ERROR, get_mxact_status_for_lock(), GetMultiXactIdHintBits(), HEAP_KEYS_UPDATED, HEAP_LOCKED_UPGRADED(), HEAP_XMAX_COMMITTED, HEAP_XMAX_EXCL_LOCK, HEAP_XMAX_INVALID, HEAP_XMAX_IS_EXCL_LOCKED(), HEAP_XMAX_IS_KEYSHR_LOCKED(), HEAP_XMAX_IS_LOCKED_ONLY(), HEAP_XMAX_IS_MULTI, HEAP_XMAX_IS_SHR_LOCKED(), HEAP_XMAX_KEYSHR_LOCK, HEAP_XMAX_LOCK_ONLY, HEAP_XMAX_SHR_LOCK, InvalidTransactionId, LockTupleExclusive, LockTupleKeyShare, LockTupleNoKeyExclusive, LockTupleShare, mode, MultiXactIdCreate(), MultiXactIdExpand(), MultiXactIdGetUpdateXid(), MultiXactIdIsRunning(), MultiXactStatusForKeyShare, MultiXactStatusForNoKeyUpdate, MultiXactStatusForShare, MultiXactStatusForUpdate, MultiXactStatusNoKeyUpdate, MultiXactStatusUpdate, TransactionIdDidCommit(), TransactionIdIsCurrentTransactionId(), TransactionIdIsInProgress(), TUPLOCK_from_mxstatus, and WARNING.

Referenced by heap_delete(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), and heap_update().

◆ ConditionalMultiXactIdWait()

static bool ConditionalMultiXactIdWait ( MultiXactId  multi,
MultiXactStatus  status,
uint16  infomask,
Relation  rel,
int *  remaining,
bool  logLockFailure 
)
static

Definition at line 7751 of file heapam.c.

7754{
7755 return Do_MultiXactIdWait(multi, status, infomask, true,
7756 rel, NULL, XLTW_None, remaining, logLockFailure);
7757}
static bool Do_MultiXactIdWait(MultiXactId multi, MultiXactStatus status, uint16 infomask, bool nowait, Relation rel, ItemPointer ctid, XLTW_Oper oper, int *remaining, bool logLockFailure)
Definition: heapam.c:7651
int remaining
Definition: informix.c:692
@ XLTW_None
Definition: lmgr.h:26

References Do_MultiXactIdWait(), remaining, and XLTW_None.

Referenced by heap_lock_tuple().

◆ Do_MultiXactIdWait()

static bool Do_MultiXactIdWait ( MultiXactId  multi,
MultiXactStatus  status,
uint16  infomask,
bool  nowait,
Relation  rel,
ItemPointer  ctid,
XLTW_Oper  oper,
int *  remaining,
bool  logLockFailure 
)
static

Definition at line 7651 of file heapam.c.

7655{
7656 bool result = true;
7657 MultiXactMember *members;
7658 int nmembers;
7659 int remain = 0;
7660
7661 /* for pre-pg_upgrade tuples, no need to sleep at all */
7662 nmembers = HEAP_LOCKED_UPGRADED(infomask) ? -1 :
7663 GetMultiXactIdMembers(multi, &members, false,
7664 HEAP_XMAX_IS_LOCKED_ONLY(infomask));
7665
7666 if (nmembers >= 0)
7667 {
7668 int i;
7669
7670 for (i = 0; i < nmembers; i++)
7671 {
7672 TransactionId memxid = members[i].xid;
7673 MultiXactStatus memstatus = members[i].status;
7674
7676 {
7677 remain++;
7678 continue;
7679 }
7680
7682 LOCKMODE_from_mxstatus(status)))
7683 {
7684 if (remaining && TransactionIdIsInProgress(memxid))
7685 remain++;
7686 continue;
7687 }
7688
7689 /*
7690 * This member conflicts with our multi, so we have to sleep (or
7691 * return failure, if asked to avoid waiting.)
7692 *
7693 * Note that we don't set up an error context callback ourselves,
7694 * but instead we pass the info down to XactLockTableWait. This
7695 * might seem a bit wasteful because the context is set up and
7696 * tore down for each member of the multixact, but in reality it
7697 * should be barely noticeable, and it avoids duplicate code.
7698 */
7699 if (nowait)
7700 {
7701 result = ConditionalXactLockTableWait(memxid, logLockFailure);
7702 if (!result)
7703 break;
7704 }
7705 else
7706 XactLockTableWait(memxid, rel, ctid, oper);
7707 }
7708
7709 pfree(members);
7710 }
7711
7712 if (remaining)
7713 *remaining = remain;
7714
7715 return result;
7716}
#define LOCKMODE_from_mxstatus(status)
Definition: heapam.c:154
void XactLockTableWait(TransactionId xid, Relation rel, ItemPointer ctid, XLTW_Oper oper)
Definition: lmgr.c:663
bool ConditionalXactLockTableWait(TransactionId xid, bool logLockFailure)
Definition: lmgr.c:736
bool DoLockModesConflict(LOCKMODE mode1, LOCKMODE mode2)
Definition: lock.c:619
int GetMultiXactIdMembers(MultiXactId multi, MultiXactMember **members, bool from_pgupgrade, bool isLockOnly)
Definition: multixact.c:1299
Operator oper(ParseState *pstate, List *opname, Oid ltypeId, Oid rtypeId, bool noError, int location)
Definition: parse_oper.c:370
TransactionId xid
Definition: multixact.h:58
MultiXactStatus status
Definition: multixact.h:59

References ConditionalXactLockTableWait(), DoLockModesConflict(), GetMultiXactIdMembers(), HEAP_LOCKED_UPGRADED(), HEAP_XMAX_IS_LOCKED_ONLY(), i, LOCKMODE_from_mxstatus, oper(), pfree(), remaining, MultiXactMember::status, TransactionIdIsCurrentTransactionId(), TransactionIdIsInProgress(), XactLockTableWait(), and MultiXactMember::xid.

Referenced by ConditionalMultiXactIdWait(), and MultiXactIdWait().

◆ DoesMultiXactIdConflict()

static bool DoesMultiXactIdConflict ( MultiXactId  multi,
uint16  infomask,
LockTupleMode  lockmode,
bool *  current_is_member 
)
static

Definition at line 7551 of file heapam.c.

7553{
7554 int nmembers;
7555 MultiXactMember *members;
7556 bool result = false;
7557 LOCKMODE wanted = tupleLockExtraInfo[lockmode].hwlock;
7558
7559 if (HEAP_LOCKED_UPGRADED(infomask))
7560 return false;
7561
7562 nmembers = GetMultiXactIdMembers(multi, &members, false,
7563 HEAP_XMAX_IS_LOCKED_ONLY(infomask));
7564 if (nmembers >= 0)
7565 {
7566 int i;
7567
7568 for (i = 0; i < nmembers; i++)
7569 {
7570 TransactionId memxid;
7571 LOCKMODE memlockmode;
7572
7573 if (result && (current_is_member == NULL || *current_is_member))
7574 break;
7575
7576 memlockmode = LOCKMODE_from_mxstatus(members[i].status);
7577
7578 /* ignore members from current xact (but track their presence) */
7579 memxid = members[i].xid;
7581 {
7582 if (current_is_member != NULL)
7583 *current_is_member = true;
7584 continue;
7585 }
7586 else if (result)
7587 continue;
7588
7589 /* ignore members that don't conflict with the lock we want */
7590 if (!DoLockModesConflict(memlockmode, wanted))
7591 continue;
7592
7593 if (ISUPDATE_from_mxstatus(members[i].status))
7594 {
7595 /* ignore aborted updaters */
7596 if (TransactionIdDidAbort(memxid))
7597 continue;
7598 }
7599 else
7600 {
7601 /* ignore lockers-only that are no longer in progress */
7602 if (!TransactionIdIsInProgress(memxid))
7603 continue;
7604 }
7605
7606 /*
7607 * Whatever remains are either live lockers that conflict with our
7608 * wanted lock, and updaters that are not aborted. Those conflict
7609 * with what we want. Set up to return true, but keep going to
7610 * look for the current transaction among the multixact members,
7611 * if needed.
7612 */
7613 result = true;
7614 }
7615 pfree(members);
7616 }
7617
7618 return result;
7619}
static const struct @15 tupleLockExtraInfo[MaxLockTupleMode+1]
int LOCKMODE
Definition: lockdefs.h:26
#define ISUPDATE_from_mxstatus(status)
Definition: multixact.h:52
bool TransactionIdDidAbort(TransactionId transactionId)
Definition: transam.c:188

References DoLockModesConflict(), GetMultiXactIdMembers(), HEAP_LOCKED_UPGRADED(), HEAP_XMAX_IS_LOCKED_ONLY(), i, ISUPDATE_from_mxstatus, LOCKMODE_from_mxstatus, pfree(), TransactionIdDidAbort(), TransactionIdIsCurrentTransactionId(), TransactionIdIsInProgress(), tupleLockExtraInfo, and MultiXactMember::xid.

Referenced by heap_delete(), heap_inplace_lock(), heap_lock_tuple(), and heap_update().

◆ ExtractReplicaIdentity()

static HeapTuple ExtractReplicaIdentity ( Relation  relation,
HeapTuple  tp,
bool  key_required,
bool *  copy 
)
static

Definition at line 9097 of file heapam.c.

9099{
9100 TupleDesc desc = RelationGetDescr(relation);
9101 char replident = relation->rd_rel->relreplident;
9102 Bitmapset *idattrs;
9103 HeapTuple key_tuple;
9104 bool nulls[MaxHeapAttributeNumber];
9106
9107 *copy = false;
9108
9109 if (!RelationIsLogicallyLogged(relation))
9110 return NULL;
9111
9112 if (replident == REPLICA_IDENTITY_NOTHING)
9113 return NULL;
9114
9115 if (replident == REPLICA_IDENTITY_FULL)
9116 {
9117 /*
9118 * When logging the entire old tuple, it very well could contain
9119 * toasted columns. If so, force them to be inlined.
9120 */
9121 if (HeapTupleHasExternal(tp))
9122 {
9123 *copy = true;
9124 tp = toast_flatten_tuple(tp, desc);
9125 }
9126 return tp;
9127 }
9128
9129 /* if the key isn't required and we're only logging the key, we're done */
9130 if (!key_required)
9131 return NULL;
9132
9133 /* find out the replica identity columns */
9134 idattrs = RelationGetIndexAttrBitmap(relation,
9136
9137 /*
9138 * If there's no defined replica identity columns, treat as !key_required.
9139 * (This case should not be reachable from heap_update, since that should
9140 * calculate key_required accurately. But heap_delete just passes
9141 * constant true for key_required, so we can hit this case in deletes.)
9142 */
9143 if (bms_is_empty(idattrs))
9144 return NULL;
9145
9146 /*
9147 * Construct a new tuple containing only the replica identity columns,
9148 * with nulls elsewhere. While we're at it, assert that the replica
9149 * identity columns aren't null.
9150 */
9151 heap_deform_tuple(tp, desc, values, nulls);
9152
9153 for (int i = 0; i < desc->natts; i++)
9154 {
9156 idattrs))
9157 Assert(!nulls[i]);
9158 else
9159 nulls[i] = true;
9160 }
9161
9162 key_tuple = heap_form_tuple(desc, values, nulls);
9163 *copy = true;
9164
9165 bms_free(idattrs);
9166
9167 /*
9168 * If the tuple, which by here only contains indexed columns, still has
9169 * toasted columns, force them to be inlined. This is somewhat unlikely
9170 * since there's limits on the size of indexed columns, so we don't
9171 * duplicate toast_flatten_tuple()s functionality in the above loop over
9172 * the indexed columns, even if it would be more efficient.
9173 */
9174 if (HeapTupleHasExternal(key_tuple))
9175 {
9176 HeapTuple oldtup = key_tuple;
9177
9178 key_tuple = toast_flatten_tuple(oldtup, desc);
9179 heap_freetuple(oldtup);
9180 }
9181
9182 return key_tuple;
9183}
void bms_free(Bitmapset *a)
Definition: bitmapset.c:239
bool bms_is_member(int x, const Bitmapset *a)
Definition: bitmapset.c:510
#define bms_is_empty(a)
Definition: bitmapset.h:118
static Datum values[MAXATTR]
Definition: bootstrap.c:151
HeapTuple toast_flatten_tuple(HeapTuple tup, TupleDesc tupleDesc)
Definition: heaptoast.c:350
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *values, bool *isnull)
Definition: heaptuple.c:1346
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1435
static bool HeapTupleHasExternal(const HeapTupleData *tuple)
Definition: htup_details.h:762
#define MaxHeapAttributeNumber
Definition: htup_details.h:48
uintptr_t Datum
Definition: postgres.h:69
#define RelationIsLogicallyLogged(relation)
Definition: rel.h:712
#define RelationGetDescr(relation)
Definition: rel.h:542
Bitmapset * RelationGetIndexAttrBitmap(Relation relation, IndexAttrBitmapKind attrKind)
Definition: relcache.c:5286
@ INDEX_ATTR_BITMAP_IDENTITY_KEY
Definition: relcache.h:63
Form_pg_class rd_rel
Definition: rel.h:111
#define FirstLowInvalidHeapAttributeNumber
Definition: sysattr.h:27

References Assert(), bms_free(), bms_is_empty, bms_is_member(), FirstLowInvalidHeapAttributeNumber, heap_deform_tuple(), heap_form_tuple(), heap_freetuple(), HeapTupleHasExternal(), i, INDEX_ATTR_BITMAP_IDENTITY_KEY, MaxHeapAttributeNumber, TupleDescData::natts, RelationData::rd_rel, RelationGetDescr, RelationGetIndexAttrBitmap(), RelationIsLogicallyLogged, toast_flatten_tuple(), and values.

Referenced by heap_delete(), and heap_update().

◆ FreeBulkInsertState()

void FreeBulkInsertState ( BulkInsertState  bistate)

Definition at line 2006 of file heapam.c.

2007{
2008 if (bistate->current_buf != InvalidBuffer)
2009 ReleaseBuffer(bistate->current_buf);
2010 FreeAccessStrategy(bistate->strategy);
2011 pfree(bistate);
2012}
#define InvalidBuffer
Definition: buf.h:25
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:5303
void FreeAccessStrategy(BufferAccessStrategy strategy)
Definition: freelist.c:723
BufferAccessStrategy strategy
Definition: hio.h:31
Buffer current_buf
Definition: hio.h:32

References BulkInsertStateData::current_buf, FreeAccessStrategy(), InvalidBuffer, pfree(), ReleaseBuffer(), and BulkInsertStateData::strategy.

Referenced by ATRewriteTable(), CopyFrom(), CopyMultiInsertBufferCleanup(), intorel_shutdown(), and transientrel_shutdown().

◆ FreezeMultiXactId()

static TransactionId FreezeMultiXactId ( MultiXactId  multi,
uint16  t_infomask,
const struct VacuumCutoffs cutoffs,
uint16 flags,
HeapPageFreeze pagefrz 
)
static

Definition at line 6660 of file heapam.c.

6663{
6664 TransactionId newxmax;
6665 MultiXactMember *members;
6666 int nmembers;
6667 bool need_replace;
6668 int nnewmembers;
6669 MultiXactMember *newmembers;
6670 bool has_lockers;
6671 TransactionId update_xid;
6672 bool update_committed;
6673 TransactionId FreezePageRelfrozenXid;
6674
6675 *flags = 0;
6676
6677 /* We should only be called in Multis */
6678 Assert(t_infomask & HEAP_XMAX_IS_MULTI);
6679
6680 if (!MultiXactIdIsValid(multi) ||
6681 HEAP_LOCKED_UPGRADED(t_infomask))
6682 {
6683 *flags |= FRM_INVALIDATE_XMAX;
6684 pagefrz->freeze_required = true;
6685 return InvalidTransactionId;
6686 }
6687 else if (MultiXactIdPrecedes(multi, cutoffs->relminmxid))
6688 ereport(ERROR,
6690 errmsg_internal("found multixact %u from before relminmxid %u",
6691 multi, cutoffs->relminmxid)));
6692 else if (MultiXactIdPrecedes(multi, cutoffs->OldestMxact))
6693 {
6694 TransactionId update_xact;
6695
6696 /*
6697 * This old multi cannot possibly have members still running, but
6698 * verify just in case. If it was a locker only, it can be removed
6699 * without any further consideration; but if it contained an update,
6700 * we might need to preserve it.
6701 */
6702 if (MultiXactIdIsRunning(multi,
6703 HEAP_XMAX_IS_LOCKED_ONLY(t_infomask)))
6704 ereport(ERROR,
6706 errmsg_internal("multixact %u from before multi freeze cutoff %u found to be still running",
6707 multi, cutoffs->OldestMxact)));
6708
6709 if (HEAP_XMAX_IS_LOCKED_ONLY(t_infomask))
6710 {
6711 *flags |= FRM_INVALIDATE_XMAX;
6712 pagefrz->freeze_required = true;
6713 return InvalidTransactionId;
6714 }
6715
6716 /* replace multi with single XID for its updater? */
6717 update_xact = MultiXactIdGetUpdateXid(multi, t_infomask);
6718 if (TransactionIdPrecedes(update_xact, cutoffs->relfrozenxid))
6719 ereport(ERROR,
6721 errmsg_internal("multixact %u contains update XID %u from before relfrozenxid %u",
6722 multi, update_xact,
6723 cutoffs->relfrozenxid)));
6724 else if (TransactionIdPrecedes(update_xact, cutoffs->OldestXmin))
6725 {
6726 /*
6727 * Updater XID has to have aborted (otherwise the tuple would have
6728 * been pruned away instead, since updater XID is < OldestXmin).
6729 * Just remove xmax.
6730 */
6731 if (TransactionIdDidCommit(update_xact))
6732 ereport(ERROR,
6734 errmsg_internal("multixact %u contains committed update XID %u from before removable cutoff %u",
6735 multi, update_xact,
6736 cutoffs->OldestXmin)));
6737 *flags |= FRM_INVALIDATE_XMAX;
6738 pagefrz->freeze_required = true;
6739 return InvalidTransactionId;
6740 }
6741
6742 /* Have to keep updater XID as new xmax */
6743 *flags |= FRM_RETURN_IS_XID;
6744 pagefrz->freeze_required = true;
6745 return update_xact;
6746 }
6747
6748 /*
6749 * Some member(s) of this Multi may be below FreezeLimit xid cutoff, so we
6750 * need to walk the whole members array to figure out what to do, if
6751 * anything.
6752 */
6753 nmembers =
6754 GetMultiXactIdMembers(multi, &members, false,
6755 HEAP_XMAX_IS_LOCKED_ONLY(t_infomask));
6756 if (nmembers <= 0)
6757 {
6758 /* Nothing worth keeping */
6759 *flags |= FRM_INVALIDATE_XMAX;
6760 pagefrz->freeze_required = true;
6761 return InvalidTransactionId;
6762 }
6763
6764 /*
6765 * The FRM_NOOP case is the only case where we might need to ratchet back
6766 * FreezePageRelfrozenXid or FreezePageRelminMxid. It is also the only
6767 * case where our caller might ratchet back its NoFreezePageRelfrozenXid
6768 * or NoFreezePageRelminMxid "no freeze" trackers to deal with a multi.
6769 * FRM_NOOP handling should result in the NewRelfrozenXid/NewRelminMxid
6770 * trackers managed by VACUUM being ratcheting back by xmax to the degree
6771 * required to make it safe to leave xmax undisturbed, independent of
6772 * whether or not page freezing is triggered somewhere else.
6773 *
6774 * Our policy is to force freezing in every case other than FRM_NOOP,
6775 * which obviates the need to maintain either set of trackers, anywhere.
6776 * Every other case will reliably execute a freeze plan for xmax that
6777 * either replaces xmax with an XID/MXID >= OldestXmin/OldestMxact, or
6778 * sets xmax to an InvalidTransactionId XID, rendering xmax fully frozen.
6779 * (VACUUM's NewRelfrozenXid/NewRelminMxid trackers are initialized with
6780 * OldestXmin/OldestMxact, so later values never need to be tracked here.)
6781 */
6782 need_replace = false;
6783 FreezePageRelfrozenXid = pagefrz->FreezePageRelfrozenXid;
6784 for (int i = 0; i < nmembers; i++)
6785 {
6786 TransactionId xid = members[i].xid;
6787
6788 Assert(!TransactionIdPrecedes(xid, cutoffs->relfrozenxid));
6789
6790 if (TransactionIdPrecedes(xid, cutoffs->FreezeLimit))
6791 {
6792 /* Can't violate the FreezeLimit postcondition */
6793 need_replace = true;
6794 break;
6795 }
6796 if (TransactionIdPrecedes(xid, FreezePageRelfrozenXid))
6797 FreezePageRelfrozenXid = xid;
6798 }
6799
6800 /* Can't violate the MultiXactCutoff postcondition, either */
6801 if (!need_replace)
6802 need_replace = MultiXactIdPrecedes(multi, cutoffs->MultiXactCutoff);
6803
6804 if (!need_replace)
6805 {
6806 /*
6807 * vacuumlazy.c might ratchet back NewRelminMxid, NewRelfrozenXid, or
6808 * both together to make it safe to retain this particular multi after
6809 * freezing its page
6810 */
6811 *flags |= FRM_NOOP;
6812 pagefrz->FreezePageRelfrozenXid = FreezePageRelfrozenXid;
6813 if (MultiXactIdPrecedes(multi, pagefrz->FreezePageRelminMxid))
6814 pagefrz->FreezePageRelminMxid = multi;
6815 pfree(members);
6816 return multi;
6817 }
6818
6819 /*
6820 * Do a more thorough second pass over the multi to figure out which
6821 * member XIDs actually need to be kept. Checking the precise status of
6822 * individual members might even show that we don't need to keep anything.
6823 * That is quite possible even though the Multi must be >= OldestMxact,
6824 * since our second pass only keeps member XIDs when it's truly necessary;
6825 * even member XIDs >= OldestXmin often won't be kept by second pass.
6826 */
6827 nnewmembers = 0;
6828 newmembers = palloc(sizeof(MultiXactMember) * nmembers);
6829 has_lockers = false;
6830 update_xid = InvalidTransactionId;
6831 update_committed = false;
6832
6833 /*
6834 * Determine whether to keep each member xid, or to ignore it instead
6835 */
6836 for (int i = 0; i < nmembers; i++)
6837 {
6838 TransactionId xid = members[i].xid;
6839 MultiXactStatus mstatus = members[i].status;
6840
6841 Assert(!TransactionIdPrecedes(xid, cutoffs->relfrozenxid));
6842
6843 if (!ISUPDATE_from_mxstatus(mstatus))
6844 {
6845 /*
6846 * Locker XID (not updater XID). We only keep lockers that are
6847 * still running.
6848 */
6851 {
6852 if (TransactionIdPrecedes(xid, cutoffs->OldestXmin))
6853 ereport(ERROR,
6855 errmsg_internal("multixact %u contains running locker XID %u from before removable cutoff %u",
6856 multi, xid,
6857 cutoffs->OldestXmin)));
6858 newmembers[nnewmembers++] = members[i];
6859 has_lockers = true;
6860 }
6861
6862 continue;
6863 }
6864
6865 /*
6866 * Updater XID (not locker XID). Should we keep it?
6867 *
6868 * Since the tuple wasn't totally removed when vacuum pruned, the
6869 * update Xid cannot possibly be older than OldestXmin cutoff unless
6870 * the updater XID aborted. If the updater transaction is known
6871 * aborted or crashed then it's okay to ignore it, otherwise not.
6872 *
6873 * In any case the Multi should never contain two updaters, whatever
6874 * their individual commit status. Check for that first, in passing.
6875 */
6876 if (TransactionIdIsValid(update_xid))
6877 ereport(ERROR,
6879 errmsg_internal("multixact %u has two or more updating members",
6880 multi),
6881 errdetail_internal("First updater XID=%u second updater XID=%u.",
6882 update_xid, xid)));
6883
6884 /*
6885 * As with all tuple visibility routines, it's critical to test
6886 * TransactionIdIsInProgress before TransactionIdDidCommit, because of
6887 * race conditions explained in detail in heapam_visibility.c.
6888 */
6891 update_xid = xid;
6892 else if (TransactionIdDidCommit(xid))
6893 {
6894 /*
6895 * The transaction committed, so we can tell caller to set
6896 * HEAP_XMAX_COMMITTED. (We can only do this because we know the
6897 * transaction is not running.)
6898 */
6899 update_committed = true;
6900 update_xid = xid;
6901 }
6902 else
6903 {
6904 /*
6905 * Not in progress, not committed -- must be aborted or crashed;
6906 * we can ignore it.
6907 */
6908 continue;
6909 }
6910
6911 /*
6912 * We determined that updater must be kept -- add it to pending new
6913 * members list
6914 */
6915 if (TransactionIdPrecedes(xid, cutoffs->OldestXmin))
6916 ereport(ERROR,
6918 errmsg_internal("multixact %u contains committed update XID %u from before removable cutoff %u",
6919 multi, xid, cutoffs->OldestXmin)));
6920 newmembers[nnewmembers++] = members[i];
6921 }
6922
6923 pfree(members);
6924
6925 /*
6926 * Determine what to do with caller's multi based on information gathered
6927 * during our second pass
6928 */
6929 if (nnewmembers == 0)
6930 {
6931 /* Nothing worth keeping */
6932 *flags |= FRM_INVALIDATE_XMAX;
6933 newxmax = InvalidTransactionId;
6934 }
6935 else if (TransactionIdIsValid(update_xid) && !has_lockers)
6936 {
6937 /*
6938 * If there's a single member and it's an update, pass it back alone
6939 * without creating a new Multi. (XXX we could do this when there's a
6940 * single remaining locker, too, but that would complicate the API too
6941 * much; moreover, the case with the single updater is more
6942 * interesting, because those are longer-lived.)
6943 */
6944 Assert(nnewmembers == 1);
6945 *flags |= FRM_RETURN_IS_XID;
6946 if (update_committed)
6947 *flags |= FRM_MARK_COMMITTED;
6948 newxmax = update_xid;
6949 }
6950 else
6951 {
6952 /*
6953 * Create a new multixact with the surviving members of the previous
6954 * one, to set as new Xmax in the tuple
6955 */
6956 newxmax = MultiXactIdCreateFromMembers(nnewmembers, newmembers);
6957 *flags |= FRM_RETURN_IS_MULTI;
6958 }
6959
6960 pfree(newmembers);
6961
6962 pagefrz->freeze_required = true;
6963 return newxmax;
6964}
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1158
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1231
int errcode(int sqlerrcode)
Definition: elog.c:854
#define ereport(elevel,...)
Definition: elog.h:149
#define FRM_RETURN_IS_XID
Definition: heapam.c:6609
#define FRM_MARK_COMMITTED
Definition: heapam.c:6611
#define FRM_NOOP
Definition: heapam.c:6607
#define FRM_RETURN_IS_MULTI
Definition: heapam.c:6610
#define FRM_INVALIDATE_XMAX
Definition: heapam.c:6608
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3317
MultiXactId MultiXactIdCreateFromMembers(int nmembers, MultiXactMember *members)
Definition: multixact.c:815
#define MultiXactIdIsValid(multi)
Definition: multixact.h:28
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:41
TransactionId FreezePageRelfrozenXid
Definition: heapam.h:206
bool freeze_required
Definition: heapam.h:180
MultiXactId FreezePageRelminMxid
Definition: heapam.h:207
TransactionId FreezeLimit
Definition: vacuum.h:284
TransactionId OldestXmin
Definition: vacuum.h:274
TransactionId relfrozenxid
Definition: vacuum.h:258
MultiXactId relminmxid
Definition: vacuum.h:259
MultiXactId MultiXactCutoff
Definition: vacuum.h:285
MultiXactId OldestMxact
Definition: vacuum.h:275
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:280
#define TransactionIdIsValid(xid)
Definition: transam.h:41

References Assert(), ereport, errcode(), ERRCODE_DATA_CORRUPTED, errdetail_internal(), errmsg_internal(), ERROR, HeapPageFreeze::freeze_required, VacuumCutoffs::FreezeLimit, HeapPageFreeze::FreezePageRelfrozenXid, HeapPageFreeze::FreezePageRelminMxid, FRM_INVALIDATE_XMAX, FRM_MARK_COMMITTED, FRM_NOOP, FRM_RETURN_IS_MULTI, FRM_RETURN_IS_XID, GetMultiXactIdMembers(), HEAP_LOCKED_UPGRADED(), HEAP_XMAX_IS_LOCKED_ONLY(), HEAP_XMAX_IS_MULTI, i, InvalidTransactionId, ISUPDATE_from_mxstatus, VacuumCutoffs::MultiXactCutoff, MultiXactIdCreateFromMembers(), MultiXactIdGetUpdateXid(), MultiXactIdIsRunning(), MultiXactIdIsValid, MultiXactIdPrecedes(), VacuumCutoffs::OldestMxact, VacuumCutoffs::OldestXmin, palloc(), pfree(), VacuumCutoffs::relfrozenxid, VacuumCutoffs::relminmxid, MultiXactMember::status, TransactionIdDidCommit(), TransactionIdIsCurrentTransactionId(), TransactionIdIsInProgress(), TransactionIdIsValid, TransactionIdPrecedes(), and MultiXactMember::xid.

Referenced by heap_prepare_freeze_tuple().

◆ get_mxact_status_for_lock()

static MultiXactStatus get_mxact_status_for_lock ( LockTupleMode  mode,
bool  is_update 
)
static

Definition at line 4495 of file heapam.c.

4496{
4497 int retval;
4498
4499 if (is_update)
4500 retval = tupleLockExtraInfo[mode].updstatus;
4501 else
4502 retval = tupleLockExtraInfo[mode].lockstatus;
4503
4504 if (retval == -1)
4505 elog(ERROR, "invalid lock tuple mode %d/%s", mode,
4506 is_update ? "true" : "false");
4507
4508 return (MultiXactStatus) retval;
4509}

References elog, ERROR, mode, and tupleLockExtraInfo.

Referenced by compute_new_xmax_infomask(), heap_lock_tuple(), and test_lockmode_for_conflict().

◆ GetBulkInsertState()

BulkInsertState GetBulkInsertState ( void  )

Definition at line 1989 of file heapam.c.

1990{
1991 BulkInsertState bistate;
1992
1993 bistate = (BulkInsertState) palloc(sizeof(BulkInsertStateData));
1995 bistate->current_buf = InvalidBuffer;
1996 bistate->next_free = InvalidBlockNumber;
1997 bistate->last_free = InvalidBlockNumber;
1998 bistate->already_extended_by = 0;
1999 return bistate;
2000}
@ BAS_BULKWRITE
Definition: bufmgr.h:39
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:541
struct BulkInsertStateData * BulkInsertState
Definition: heapam.h:45
BlockNumber last_free
Definition: hio.h:49
uint32 already_extended_by
Definition: hio.h:50
BlockNumber next_free
Definition: hio.h:48

References BulkInsertStateData::already_extended_by, BAS_BULKWRITE, BulkInsertStateData::current_buf, GetAccessStrategy(), InvalidBlockNumber, InvalidBuffer, BulkInsertStateData::last_free, BulkInsertStateData::next_free, palloc(), and BulkInsertStateData::strategy.

Referenced by ATRewriteTable(), CopyFrom(), CopyMultiInsertBufferInit(), intorel_startup(), and transientrel_startup().

◆ GetMultiXactIdHintBits()

static void GetMultiXactIdHintBits ( MultiXactId  multi,
uint16 new_infomask,
uint16 new_infomask2 
)
static

Definition at line 7402 of file heapam.c.

7404{
7405 int nmembers;
7406 MultiXactMember *members;
7407 int i;
7409 uint16 bits2 = 0;
7410 bool has_update = false;
7411 LockTupleMode strongest = LockTupleKeyShare;
7412
7413 /*
7414 * We only use this in multis we just created, so they cannot be values
7415 * pre-pg_upgrade.
7416 */
7417 nmembers = GetMultiXactIdMembers(multi, &members, false, false);
7418
7419 for (i = 0; i < nmembers; i++)
7420 {
7422
7423 /*
7424 * Remember the strongest lock mode held by any member of the
7425 * multixact.
7426 */
7427 mode = TUPLOCK_from_mxstatus(members[i].status);
7428 if (mode > strongest)
7429 strongest = mode;
7430
7431 /* See what other bits we need */
7432 switch (members[i].status)
7433 {
7437 break;
7438
7440 bits2 |= HEAP_KEYS_UPDATED;
7441 break;
7442
7444 has_update = true;
7445 break;
7446
7448 bits2 |= HEAP_KEYS_UPDATED;
7449 has_update = true;
7450 break;
7451 }
7452 }
7453
7454 if (strongest == LockTupleExclusive ||
7455 strongest == LockTupleNoKeyExclusive)
7456 bits |= HEAP_XMAX_EXCL_LOCK;
7457 else if (strongest == LockTupleShare)
7458 bits |= HEAP_XMAX_SHR_LOCK;
7459 else if (strongest == LockTupleKeyShare)
7460 bits |= HEAP_XMAX_KEYSHR_LOCK;
7461
7462 if (!has_update)
7463 bits |= HEAP_XMAX_LOCK_ONLY;
7464
7465 if (nmembers > 0)
7466 pfree(members);
7467
7468 *new_infomask = bits;
7469 *new_infomask2 = bits2;
7470}

References GetMultiXactIdMembers(), HEAP_KEYS_UPDATED, HEAP_XMAX_EXCL_LOCK, HEAP_XMAX_IS_MULTI, HEAP_XMAX_KEYSHR_LOCK, HEAP_XMAX_LOCK_ONLY, HEAP_XMAX_SHR_LOCK, i, LockTupleExclusive, LockTupleKeyShare, LockTupleNoKeyExclusive, LockTupleShare, mode, MultiXactStatusForKeyShare, MultiXactStatusForNoKeyUpdate, MultiXactStatusForShare, MultiXactStatusForUpdate, MultiXactStatusNoKeyUpdate, MultiXactStatusUpdate, pfree(), and TUPLOCK_from_mxstatus.

Referenced by compute_new_xmax_infomask(), heap_prepare_freeze_tuple(), and heap_update().

◆ heap_abort_speculative()

void heap_abort_speculative ( Relation  relation,
ItemPointer  tid 
)

Definition at line 6139 of file heapam.c.

6140{
6142 ItemId lp;
6143 HeapTupleData tp;
6144 Page page;
6145 BlockNumber block;
6146 Buffer buffer;
6147
6149
6150 block = ItemPointerGetBlockNumber(tid);
6151 buffer = ReadBuffer(relation, block);
6152 page = BufferGetPage(buffer);
6153
6155
6156 /*
6157 * Page can't be all visible, we just inserted into it, and are still
6158 * running.
6159 */
6160 Assert(!PageIsAllVisible(page));
6161
6164
6165 tp.t_tableOid = RelationGetRelid(relation);
6166 tp.t_data = (HeapTupleHeader) PageGetItem(page, lp);
6167 tp.t_len = ItemIdGetLength(lp);
6168 tp.t_self = *tid;
6169
6170 /*
6171 * Sanity check that the tuple really is a speculatively inserted tuple,
6172 * inserted by us.
6173 */
6174 if (tp.t_data->t_choice.t_heap.t_xmin != xid)
6175 elog(ERROR, "attempted to kill a tuple inserted by another transaction");
6176 if (!(IsToastRelation(relation) || HeapTupleHeaderIsSpeculative(tp.t_data)))
6177 elog(ERROR, "attempted to kill a non-speculative tuple");
6179
6180 /*
6181 * No need to check for serializable conflicts here. There is never a
6182 * need for a combo CID, either. No need to extract replica identity, or
6183 * do anything special with infomask bits.
6184 */
6185
6187
6188 /*
6189 * The tuple will become DEAD immediately. Flag that this page is a
6190 * candidate for pruning by setting xmin to TransactionXmin. While not
6191 * immediately prunable, it is the oldest xid we can cheaply determine
6192 * that's safe against wraparound / being older than the table's
6193 * relfrozenxid. To defend against the unlikely case of a new relation
6194 * having a newer relfrozenxid than our TransactionXmin, use relfrozenxid
6195 * if so (vacuum can't subsequently move relfrozenxid to beyond
6196 * TransactionXmin, so there's no race here).
6197 */
6199 {
6200 TransactionId relfrozenxid = relation->rd_rel->relfrozenxid;
6201 TransactionId prune_xid;
6202
6203 if (TransactionIdPrecedes(TransactionXmin, relfrozenxid))
6204 prune_xid = relfrozenxid;
6205 else
6206 prune_xid = TransactionXmin;
6207 PageSetPrunable(page, prune_xid);
6208 }
6209
6210 /* store transaction information of xact deleting the tuple */
6212 tp.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
6213
6214 /*
6215 * Set the tuple header xmin to InvalidTransactionId. This makes the
6216 * tuple immediately invisible everyone. (In particular, to any
6217 * transactions waiting on the speculative token, woken up later.)
6218 */
6220
6221 /* Clear the speculative insertion token too */
6222 tp.t_data->t_ctid = tp.t_self;
6223
6224 MarkBufferDirty(buffer);
6225
6226 /*
6227 * XLOG stuff
6228 *
6229 * The WAL records generated here match heap_delete(). The same recovery
6230 * routines are used.
6231 */
6232 if (RelationNeedsWAL(relation))
6233 {
6234 xl_heap_delete xlrec;
6235 XLogRecPtr recptr;
6236
6237 xlrec.flags = XLH_DELETE_IS_SUPER;
6239 tp.t_data->t_infomask2);
6241 xlrec.xmax = xid;
6242
6246
6247 /* No replica identity & replication origin logged */
6248
6249 recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_DELETE);
6250
6251 PageSetLSN(page, recptr);
6252 }
6253
6255
6257
6258 if (HeapTupleHasExternal(&tp))
6259 {
6260 Assert(!IsToastRelation(relation));
6261 heap_toast_delete(relation, &tp, true);
6262 }
6263
6264 /*
6265 * Never need to mark tuple for invalidation, since catalogs don't support
6266 * speculative insertion
6267 */
6268
6269 /* Now we can release the buffer */
6270 ReleaseBuffer(buffer);
6271
6272 /* count deletion, as we counted the insertion too */
6273 pgstat_count_heap_delete(relation);
6274}
int Buffer
Definition: buf.h:23
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:2945
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:5537
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition: bufmgr.c:751
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:196
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:414
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:198
static bool PageIsAllVisible(const PageData *page)
Definition: bufpage.h:429
static Item PageGetItem(const PageData *page, const ItemIdData *itemId)
Definition: bufpage.h:354
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:244
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition: bufpage.h:391
PageData * Page
Definition: bufpage.h:82
#define PageSetPrunable(page, xid)
Definition: bufpage.h:447
bool IsToastRelation(Relation relation)
Definition: catalog.c:175
static uint8 compute_infobits(uint16 infomask, uint16 infomask2)
Definition: heapam.c:2700
#define XLOG_HEAP_DELETE
Definition: heapam_xlog.h:34
#define SizeOfHeapDelete
Definition: heapam_xlog.h:121
#define XLH_DELETE_IS_SUPER
Definition: heapam_xlog.h:105
void heap_toast_delete(Relation rel, HeapTuple oldtup, bool is_speculative)
Definition: heaptoast.c:43
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
#define HEAP_XMAX_BITS
Definition: htup_details.h:281
static bool HeapTupleHeaderIsHeapOnly(const HeapTupleHeaderData *tup)
Definition: htup_details.h:555
#define HEAP_MOVED
Definition: htup_details.h:213
static bool HeapTupleHeaderIsSpeculative(const HeapTupleHeaderData *tup)
Definition: htup_details.h:461
static void HeapTupleHeaderSetXmin(HeapTupleHeaderData *tup, TransactionId xid)
Definition: htup_details.h:331
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
#define ItemIdIsNormal(itemId)
Definition: itemid.h:99
static OffsetNumber ItemPointerGetOffsetNumber(const ItemPointerData *pointer)
Definition: itemptr.h:124
static bool ItemPointerIsValid(const ItemPointerData *pointer)
Definition: itemptr.h:83
#define START_CRIT_SECTION()
Definition: miscadmin.h:150
#define END_CRIT_SECTION()
Definition: miscadmin.h:152
void pgstat_count_heap_delete(Relation rel)
#define RelationGetRelid(relation)
Definition: rel.h:516
#define RelationNeedsWAL(relation)
Definition: rel.h:639
TransactionId TransactionXmin
Definition: snapmgr.c:158
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
Definition: htup_details.h:124
union HeapTupleHeaderData::@47 t_choice
ItemPointerData t_ctid
Definition: htup_details.h:161
HeapTupleFields t_heap
Definition: htup_details.h:157
TransactionId xmax
Definition: heapam_xlog.h:115
OffsetNumber offnum
Definition: heapam_xlog.h:116
uint8 infobits_set
Definition: heapam_xlog.h:117
TransactionId GetCurrentTransactionId(void)
Definition: xact.c:454
uint64 XLogRecPtr
Definition: xlogdefs.h:21
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:474
void XLogRegisterData(const void *data, uint32 len)
Definition: xloginsert.c:364
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:242
void XLogBeginInsert(void)
Definition: xloginsert.c:149
#define REGBUF_STANDARD
Definition: xloginsert.h:35

References Assert(), BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetPage(), compute_infobits(), elog, END_CRIT_SECTION, ERROR, xl_heap_delete::flags, GetCurrentTransactionId(), HEAP_MOVED, heap_toast_delete(), HEAP_XMAX_BITS, HeapTupleHasExternal(), HeapTupleHeaderIsHeapOnly(), HeapTupleHeaderIsSpeculative(), HeapTupleHeaderSetXmin(), xl_heap_delete::infobits_set, InvalidTransactionId, IsToastRelation(), ItemIdGetLength, ItemIdIsNormal, ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), ItemPointerIsValid(), LockBuffer(), MarkBufferDirty(), xl_heap_delete::offnum, PageGetItem(), PageGetItemId(), PageIsAllVisible(), PageSetLSN(), PageSetPrunable, pgstat_count_heap_delete(), RelationData::rd_rel, ReadBuffer(), REGBUF_STANDARD, RelationGetRelid, RelationNeedsWAL, ReleaseBuffer(), SizeOfHeapDelete, START_CRIT_SECTION, HeapTupleHeaderData::t_choice, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleHeaderData::t_heap, HeapTupleHeaderData::t_infomask, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, HeapTupleFields::t_xmin, TransactionIdIsValid, TransactionIdPrecedes(), TransactionXmin, XLH_DELETE_IS_SUPER, XLOG_HEAP_DELETE, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), XLogRegisterData(), and xl_heap_delete::xmax.

Referenced by heapam_tuple_complete_speculative(), and toast_delete_datum().

◆ heap_acquire_tuplock()

static bool heap_acquire_tuplock ( Relation  relation,
ItemPointer  tid,
LockTupleMode  mode,
LockWaitPolicy  wait_policy,
bool *  have_tuple_lock 
)
static

Definition at line 5241 of file heapam.c.

5243{
5244 if (*have_tuple_lock)
5245 return true;
5246
5247 switch (wait_policy)
5248 {
5249 case LockWaitBlock:
5250 LockTupleTuplock(relation, tid, mode);
5251 break;
5252
5253 case LockWaitSkip:
5254 if (!ConditionalLockTupleTuplock(relation, tid, mode, false))
5255 return false;
5256 break;
5257
5258 case LockWaitError:
5260 ereport(ERROR,
5261 (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
5262 errmsg("could not obtain lock on row in relation \"%s\"",
5263 RelationGetRelationName(relation))));
5264 break;
5265 }
5266 *have_tuple_lock = true;
5267
5268 return true;
5269}
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define LockTupleTuplock(rel, tup, mode)
Definition: heapam.c:162
#define ConditionalLockTupleTuplock(rel, tup, mode, log)
Definition: heapam.c:166
bool log_lock_failure
Definition: lock.c:54
@ LockWaitSkip
Definition: lockoptions.h:41
@ LockWaitBlock
Definition: lockoptions.h:39
@ LockWaitError
Definition: lockoptions.h:43
#define RelationGetRelationName(relation)
Definition: rel.h:550

References ConditionalLockTupleTuplock, ereport, errcode(), errmsg(), ERROR, LockTupleTuplock, LockWaitBlock, LockWaitError, LockWaitSkip, log_lock_failure, mode, and RelationGetRelationName.

Referenced by heap_delete(), heap_lock_tuple(), and heap_update().

◆ heap_attr_equals()

static bool heap_attr_equals ( TupleDesc  tupdesc,
int  attrnum,
Datum  value1,
Datum  value2,
bool  isnull1,
bool  isnull2 
)
static

Definition at line 4313 of file heapam.c.

4315{
4316 /*
4317 * If one value is NULL and other is not, then they are certainly not
4318 * equal
4319 */
4320 if (isnull1 != isnull2)
4321 return false;
4322
4323 /*
4324 * If both are NULL, they can be considered equal.
4325 */
4326 if (isnull1)
4327 return true;
4328
4329 /*
4330 * We do simple binary comparison of the two datums. This may be overly
4331 * strict because there can be multiple binary representations for the
4332 * same logical value. But we should be OK as long as there are no false
4333 * positives. Using a type-specific equality operator is messy because
4334 * there could be multiple notions of equality in different operator
4335 * classes; furthermore, we cannot safely invoke user-defined functions
4336 * while holding exclusive buffer lock.
4337 */
4338 if (attrnum <= 0)
4339 {
4340 /* The only allowed system columns are OIDs, so do this */
4341 return (DatumGetObjectId(value1) == DatumGetObjectId(value2));
4342 }
4343 else
4344 {
4345 CompactAttribute *att;
4346
4347 Assert(attrnum <= tupdesc->natts);
4348 att = TupleDescCompactAttr(tupdesc, attrnum - 1);
4349 return datumIsEqual(value1, value2, att->attbyval, att->attlen);
4350 }
4351}
bool datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen)
Definition: datum.c:223
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:247
int16 attlen
Definition: tupdesc.h:71
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:175

References Assert(), CompactAttribute::attbyval, CompactAttribute::attlen, DatumGetObjectId(), datumIsEqual(), and TupleDescCompactAttr().

Referenced by HeapDetermineColumnsInfo().

◆ heap_beginscan()

TableScanDesc heap_beginscan ( Relation  relation,
Snapshot  snapshot,
int  nkeys,
ScanKey  key,
ParallelTableScanDesc  parallel_scan,
uint32  flags 
)

Definition at line 1079 of file heapam.c.

1083{
1084 HeapScanDesc scan;
1085
1086 /*
1087 * increment relation ref count while scanning relation
1088 *
1089 * This is just to make really sure the relcache entry won't go away while
1090 * the scan has a pointer to it. Caller should be holding the rel open
1091 * anyway, so this is redundant in all normal scenarios...
1092 */
1094
1095 /*
1096 * allocate and initialize scan descriptor
1097 */
1098 if (flags & SO_TYPE_BITMAPSCAN)
1099 {
1101
1102 /*
1103 * Bitmap Heap scans do not have any fields that a normal Heap Scan
1104 * does not have, so no special initializations required here.
1105 */
1106 scan = (HeapScanDesc) bscan;
1107 }
1108 else
1109 scan = (HeapScanDesc) palloc(sizeof(HeapScanDescData));
1110
1111 scan->rs_base.rs_rd = relation;
1112 scan->rs_base.rs_snapshot = snapshot;
1113 scan->rs_base.rs_nkeys = nkeys;
1114 scan->rs_base.rs_flags = flags;
1115 scan->rs_base.rs_parallel = parallel_scan;
1116 scan->rs_strategy = NULL; /* set in initscan */
1117 scan->rs_cbuf = InvalidBuffer;
1118
1119 /*
1120 * Disable page-at-a-time mode if it's not a MVCC-safe snapshot.
1121 */
1122 if (!(snapshot && IsMVCCSnapshot(snapshot)))
1123 scan->rs_base.rs_flags &= ~SO_ALLOW_PAGEMODE;
1124
1125 /*
1126 * For seqscan and sample scans in a serializable transaction, acquire a
1127 * predicate lock on the entire relation. This is required not only to
1128 * lock all the matching tuples, but also to conflict with new insertions
1129 * into the table. In an indexscan, we take page locks on the index pages
1130 * covering the range specified in the scan qual, but in a heap scan there
1131 * is nothing more fine-grained to lock. A bitmap scan is a different
1132 * story, there we have already scanned the index and locked the index
1133 * pages covering the predicate. But in that case we still have to lock
1134 * any matching heap tuples. For sample scan we could optimize the locking
1135 * to be at least page-level granularity, but we'd need to add per-tuple
1136 * locking for that.
1137 */
1139 {
1140 /*
1141 * Ensure a missing snapshot is noticed reliably, even if the
1142 * isolation mode means predicate locking isn't performed (and
1143 * therefore the snapshot isn't used here).
1144 */
1145 Assert(snapshot);
1146 PredicateLockRelation(relation, snapshot);
1147 }
1148
1149 /* we only need to set this up once */
1150 scan->rs_ctup.t_tableOid = RelationGetRelid(relation);
1151
1152 /*
1153 * Allocate memory to keep track of page allocation for parallel workers
1154 * when doing a parallel scan.
1155 */
1156 if (parallel_scan != NULL)
1158 else
1159 scan->rs_parallelworkerdata = NULL;
1160
1161 /*
1162 * we do this here instead of in initscan() because heap_rescan also calls
1163 * initscan() and we don't want to allocate memory again
1164 */
1165 if (nkeys > 0)
1166 scan->rs_base.rs_key = (ScanKey) palloc(sizeof(ScanKeyData) * nkeys);
1167 else
1168 scan->rs_base.rs_key = NULL;
1169
1170 initscan(scan, key, false);
1171
1172 scan->rs_read_stream = NULL;
1173
1174 /*
1175 * Set up a read stream for sequential scans and TID range scans. This
1176 * should be done after initscan() because initscan() allocates the
1177 * BufferAccessStrategy object passed to the read stream API.
1178 */
1179 if (scan->rs_base.rs_flags & SO_TYPE_SEQSCAN ||
1181 {
1183
1184 if (scan->rs_base.rs_parallel)
1186 else
1188
1189 /* ---
1190 * It is safe to use batchmode as the only locks taken by `cb`
1191 * are never taken while waiting for IO:
1192 * - SyncScanLock is used in the non-parallel case
1193 * - in the parallel case, only spinlocks and atomics are used
1194 * ---
1195 */
1198 scan->rs_strategy,
1199 scan->rs_base.rs_rd,
1201 cb,
1202 scan,
1203 0);
1204 }
1205 else if (scan->rs_base.rs_flags & SO_TYPE_BITMAPSCAN)
1206 {
1209 scan->rs_strategy,
1210 scan->rs_base.rs_rd,
1213 scan,
1214 sizeof(TBMIterateResult));
1215 }
1216
1217
1218 return (TableScanDesc) scan;
1219}
static BlockNumber heap_scan_stream_read_next_parallel(ReadStream *stream, void *callback_private_data, void *per_buffer_data)
Definition: heapam.c:226
static BlockNumber heap_scan_stream_read_next_serial(ReadStream *stream, void *callback_private_data, void *per_buffer_data)
Definition: heapam.c:264
static BlockNumber bitmapheap_stream_read_next(ReadStream *pgsr, void *private_data, void *per_buffer_data)
Definition: heapam.c:289
static void initscan(HeapScanDesc scan, ScanKey key, bool keep_startblock)
Definition: heapam.c:329
void PredicateLockRelation(Relation relation, Snapshot snapshot)
Definition: predicate.c:2576
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)
Definition: read_stream.c:716
#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:77
#define READ_STREAM_DEFAULT
Definition: read_stream.h:21
#define READ_STREAM_SEQUENTIAL
Definition: read_stream.h:36
void RelationIncrementReferenceCount(Relation rel)
Definition: relcache.c:2171
@ MAIN_FORKNUM
Definition: relpath.h:58
ScanKeyData * ScanKey
Definition: skey.h:75
#define IsMVCCSnapshot(snapshot)
Definition: snapmgr.h:55
BufferAccessStrategy rs_strategy
Definition: heapam.h:71
Buffer rs_cbuf
Definition: heapam.h:68
ParallelBlockTableScanWorkerData * rs_parallelworkerdata
Definition: heapam.h:93
HeapTupleData rs_ctup
Definition: heapam.h:73
ReadStream * rs_read_stream
Definition: heapam.h:76
Relation rs_rd
Definition: relscan.h:36
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
@ SO_TYPE_TIDRANGESCAN
Definition: tableam.h:52
@ SO_TYPE_SAMPLESCAN
Definition: tableam.h:50
@ SO_TYPE_SEQSCAN
Definition: tableam.h:48
@ SO_TYPE_BITMAPSCAN
Definition: tableam.h:49

References Assert(), bitmapheap_stream_read_next(), heap_scan_stream_read_next_parallel(), heap_scan_stream_read_next_serial(), initscan(), InvalidBuffer, IsMVCCSnapshot, sort-test::key, MAIN_FORKNUM, palloc(), PredicateLockRelation(), read_stream_begin_relation(), READ_STREAM_DEFAULT, READ_STREAM_SEQUENTIAL, READ_STREAM_USE_BATCHING, RelationGetRelid, RelationIncrementReferenceCount(), HeapScanDescData::rs_base, HeapScanDescData::rs_cbuf, HeapScanDescData::rs_ctup, TableScanDescData::rs_flags, TableScanDescData::rs_key, TableScanDescData::rs_nkeys, TableScanDescData::rs_parallel, HeapScanDescData::rs_parallelworkerdata, TableScanDescData::rs_rd, HeapScanDescData::rs_read_stream, TableScanDescData::rs_snapshot, HeapScanDescData::rs_strategy, SO_TYPE_BITMAPSCAN, SO_TYPE_SAMPLESCAN, SO_TYPE_SEQSCAN, SO_TYPE_TIDRANGESCAN, and HeapTupleData::t_tableOid.

◆ heap_delete()

TM_Result heap_delete ( Relation  relation,
ItemPointer  tid,
CommandId  cid,
Snapshot  crosscheck,
bool  wait,
TM_FailureData tmfd,
bool  changingPart 
)

Definition at line 2745 of file heapam.c.

2748{
2749 TM_Result result;
2751 ItemId lp;
2752 HeapTupleData tp;
2753 Page page;
2754 BlockNumber block;
2755 Buffer buffer;
2756 Buffer vmbuffer = InvalidBuffer;
2757 TransactionId new_xmax;
2758 uint16 new_infomask,
2759 new_infomask2;
2760 bool have_tuple_lock = false;
2761 bool iscombo;
2762 bool all_visible_cleared = false;
2763 HeapTuple old_key_tuple = NULL; /* replica identity of the tuple */
2764 bool old_key_copied = false;
2765
2767
2768 /*
2769 * Forbid this during a parallel operation, lest it allocate a combo CID.
2770 * Other workers might need that combo CID for visibility checks, and we
2771 * have no provision for broadcasting it to them.
2772 */
2773 if (IsInParallelMode())
2774 ereport(ERROR,
2775 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
2776 errmsg("cannot delete tuples during a parallel operation")));
2777
2778 block = ItemPointerGetBlockNumber(tid);
2779 buffer = ReadBuffer(relation, block);
2780 page = BufferGetPage(buffer);
2781
2782 /*
2783 * Before locking the buffer, pin the visibility map page if it appears to
2784 * be necessary. Since we haven't got the lock yet, someone else might be
2785 * in the middle of changing this, so we'll need to recheck after we have
2786 * the lock.
2787 */
2788 if (PageIsAllVisible(page))
2789 visibilitymap_pin(relation, block, &vmbuffer);
2790
2792
2795
2796 tp.t_tableOid = RelationGetRelid(relation);
2797 tp.t_data = (HeapTupleHeader) PageGetItem(page, lp);
2798 tp.t_len = ItemIdGetLength(lp);
2799 tp.t_self = *tid;
2800
2801l1:
2802
2803 /*
2804 * If we didn't pin the visibility map page and the page has become all
2805 * visible while we were busy locking the buffer, we'll have to unlock and
2806 * re-lock, to avoid holding the buffer lock across an I/O. That's a bit
2807 * unfortunate, but hopefully shouldn't happen often.
2808 */
2809 if (vmbuffer == InvalidBuffer && PageIsAllVisible(page))
2810 {
2812 visibilitymap_pin(relation, block, &vmbuffer);
2814 }
2815
2816 result = HeapTupleSatisfiesUpdate(&tp, cid, buffer);
2817
2818 if (result == TM_Invisible)
2819 {
2820 UnlockReleaseBuffer(buffer);
2821 ereport(ERROR,
2822 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
2823 errmsg("attempted to delete invisible tuple")));
2824 }
2825 else if (result == TM_BeingModified && wait)
2826 {
2827 TransactionId xwait;
2828 uint16 infomask;
2829
2830 /* must copy state data before unlocking buffer */
2832 infomask = tp.t_data->t_infomask;
2833
2834 /*
2835 * Sleep until concurrent transaction ends -- except when there's a
2836 * single locker and it's our own transaction. Note we don't care
2837 * which lock mode the locker has, because we need the strongest one.
2838 *
2839 * Before sleeping, we need to acquire tuple lock to establish our
2840 * priority for the tuple (see heap_lock_tuple). LockTuple will
2841 * release us when we are next-in-line for the tuple.
2842 *
2843 * If we are forced to "start over" below, we keep the tuple lock;
2844 * this arranges that we stay at the head of the line while rechecking
2845 * tuple state.
2846 */
2847 if (infomask & HEAP_XMAX_IS_MULTI)
2848 {
2849 bool current_is_member = false;
2850
2851 if (DoesMultiXactIdConflict((MultiXactId) xwait, infomask,
2852 LockTupleExclusive, &current_is_member))
2853 {
2855
2856 /*
2857 * Acquire the lock, if necessary (but skip it when we're
2858 * requesting a lock and already have one; avoids deadlock).
2859 */
2860 if (!current_is_member)
2862 LockWaitBlock, &have_tuple_lock);
2863
2864 /* wait for multixact */
2866 relation, &(tp.t_self), XLTW_Delete,
2867 NULL);
2869
2870 /*
2871 * If xwait had just locked the tuple then some other xact
2872 * could update this tuple before we get to this point. Check
2873 * for xmax change, and start over if so.
2874 *
2875 * We also must start over if we didn't pin the VM page, and
2876 * the page has become all visible.
2877 */
2878 if ((vmbuffer == InvalidBuffer && PageIsAllVisible(page)) ||
2879 xmax_infomask_changed(tp.t_data->t_infomask, infomask) ||
2881 xwait))
2882 goto l1;
2883 }
2884
2885 /*
2886 * You might think the multixact is necessarily done here, but not
2887 * so: it could have surviving members, namely our own xact or
2888 * other subxacts of this backend. It is legal for us to delete
2889 * the tuple in either case, however (the latter case is
2890 * essentially a situation of upgrading our former shared lock to
2891 * exclusive). We don't bother changing the on-disk hint bits
2892 * since we are about to overwrite the xmax altogether.
2893 */
2894 }
2895 else if (!TransactionIdIsCurrentTransactionId(xwait))
2896 {
2897 /*
2898 * Wait for regular transaction to end; but first, acquire tuple
2899 * lock.
2900 */
2903 LockWaitBlock, &have_tuple_lock);
2904 XactLockTableWait(xwait, relation, &(tp.t_self), XLTW_Delete);
2906
2907 /*
2908 * xwait is done, but if xwait had just locked the tuple then some
2909 * other xact could update this tuple before we get to this point.
2910 * Check for xmax change, and start over if so.
2911 *
2912 * We also must start over if we didn't pin the VM page, and the
2913 * page has become all visible.
2914 */
2915 if ((vmbuffer == InvalidBuffer && PageIsAllVisible(page)) ||
2916 xmax_infomask_changed(tp.t_data->t_infomask, infomask) ||
2918 xwait))
2919 goto l1;
2920
2921 /* Otherwise check if it committed or aborted */
2922 UpdateXmaxHintBits(tp.t_data, buffer, xwait);
2923 }
2924
2925 /*
2926 * We may overwrite if previous xmax aborted, or if it committed but
2927 * only locked the tuple without updating it.
2928 */
2929 if ((tp.t_data->t_infomask & HEAP_XMAX_INVALID) ||
2932 result = TM_Ok;
2933 else if (!ItemPointerEquals(&tp.t_self, &tp.t_data->t_ctid))
2934 result = TM_Updated;
2935 else
2936 result = TM_Deleted;
2937 }
2938
2939 /* sanity check the result HeapTupleSatisfiesUpdate() and the logic above */
2940 if (result != TM_Ok)
2941 {
2942 Assert(result == TM_SelfModified ||
2943 result == TM_Updated ||
2944 result == TM_Deleted ||
2945 result == TM_BeingModified);
2947 Assert(result != TM_Updated ||
2949 }
2950
2951 if (crosscheck != InvalidSnapshot && result == TM_Ok)
2952 {
2953 /* Perform additional check for transaction-snapshot mode RI updates */
2954 if (!HeapTupleSatisfiesVisibility(&tp, crosscheck, buffer))
2955 result = TM_Updated;
2956 }
2957
2958 if (result != TM_Ok)
2959 {
2960 tmfd->ctid = tp.t_data->t_ctid;
2962 if (result == TM_SelfModified)
2964 else
2965 tmfd->cmax = InvalidCommandId;
2966 UnlockReleaseBuffer(buffer);
2967 if (have_tuple_lock)
2969 if (vmbuffer != InvalidBuffer)
2970 ReleaseBuffer(vmbuffer);
2971 return result;
2972 }
2973
2974 /*
2975 * We're about to do the actual delete -- check for conflict first, to
2976 * avoid possibly having to roll back work we've just done.
2977 *
2978 * This is safe without a recheck as long as there is no possibility of
2979 * another process scanning the page between this check and the delete
2980 * being visible to the scan (i.e., an exclusive buffer content lock is
2981 * continuously held from this point until the tuple delete is visible).
2982 */
2984
2985 /* replace cid with a combo CID if necessary */
2986 HeapTupleHeaderAdjustCmax(tp.t_data, &cid, &iscombo);
2987
2988 /*
2989 * Compute replica identity tuple before entering the critical section so
2990 * we don't PANIC upon a memory allocation failure.
2991 */
2992 old_key_tuple = ExtractReplicaIdentity(relation, &tp, true, &old_key_copied);
2993
2994 /*
2995 * If this is the first possibly-multixact-able operation in the current
2996 * transaction, set my per-backend OldestMemberMXactId setting. We can be
2997 * certain that the transaction will never become a member of any older
2998 * MultiXactIds than that. (We have to do this even if we end up just
2999 * using our own TransactionId below, since some other backend could
3000 * incorporate our XID into a MultiXact immediately afterwards.)
3001 */
3003
3006 xid, LockTupleExclusive, true,
3007 &new_xmax, &new_infomask, &new_infomask2);
3008
3010
3011 /*
3012 * If this transaction commits, the tuple will become DEAD sooner or
3013 * later. Set flag that this page is a candidate for pruning once our xid
3014 * falls below the OldestXmin horizon. If the transaction finally aborts,
3015 * the subsequent page pruning will be a no-op and the hint will be
3016 * cleared.
3017 */
3018 PageSetPrunable(page, xid);
3019
3020 if (PageIsAllVisible(page))
3021 {
3022 all_visible_cleared = true;
3023 PageClearAllVisible(page);
3024 visibilitymap_clear(relation, BufferGetBlockNumber(buffer),
3025 vmbuffer, VISIBILITYMAP_VALID_BITS);
3026 }
3027
3028 /* store transaction information of xact deleting the tuple */
3030 tp.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
3031 tp.t_data->t_infomask |= new_infomask;
3032 tp.t_data->t_infomask2 |= new_infomask2;
3034 HeapTupleHeaderSetXmax(tp.t_data, new_xmax);
3035 HeapTupleHeaderSetCmax(tp.t_data, cid, iscombo);
3036 /* Make sure there is no forward chain link in t_ctid */
3037 tp.t_data->t_ctid = tp.t_self;
3038
3039 /* Signal that this is actually a move into another partition */
3040 if (changingPart)
3042
3043 MarkBufferDirty(buffer);
3044
3045 /*
3046 * XLOG stuff
3047 *
3048 * NB: heap_abort_speculative() uses the same xlog record and replay
3049 * routines.
3050 */
3051 if (RelationNeedsWAL(relation))
3052 {
3053 xl_heap_delete xlrec;
3054 xl_heap_header xlhdr;
3055 XLogRecPtr recptr;
3056
3057 /*
3058 * For logical decode we need combo CIDs to properly decode the
3059 * catalog
3060 */
3062 log_heap_new_cid(relation, &tp);
3063
3064 xlrec.flags = 0;
3065 if (all_visible_cleared)
3067 if (changingPart)
3070 tp.t_data->t_infomask2);
3072 xlrec.xmax = new_xmax;
3073
3074 if (old_key_tuple != NULL)
3075 {
3076 if (relation->rd_rel->relreplident == REPLICA_IDENTITY_FULL)
3078 else
3080 }
3081
3084
3086
3087 /*
3088 * Log replica identity of the deleted tuple if there is one
3089 */
3090 if (old_key_tuple != NULL)
3091 {
3092 xlhdr.t_infomask2 = old_key_tuple->t_data->t_infomask2;
3093 xlhdr.t_infomask = old_key_tuple->t_data->t_infomask;
3094 xlhdr.t_hoff = old_key_tuple->t_data->t_hoff;
3095
3097 XLogRegisterData((char *) old_key_tuple->t_data
3099 old_key_tuple->t_len
3101 }
3102
3103 /* filtering by origin on a row level is much more efficient */
3105
3106 recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_DELETE);
3107
3108 PageSetLSN(page, recptr);
3109 }
3110
3112
3114
3115 if (vmbuffer != InvalidBuffer)
3116 ReleaseBuffer(vmbuffer);
3117
3118 /*
3119 * If the tuple has toasted out-of-line attributes, we need to delete
3120 * those items too. We have to do this before releasing the buffer
3121 * because we need to look at the contents of the tuple, but it's OK to
3122 * release the content lock on the buffer first.
3123 */
3124 if (relation->rd_rel->relkind != RELKIND_RELATION &&
3125 relation->rd_rel->relkind != RELKIND_MATVIEW)
3126 {
3127 /* toast table entries should never be recursively toasted */
3129 }
3130 else if (HeapTupleHasExternal(&tp))
3131 heap_toast_delete(relation, &tp, false);
3132
3133 /*
3134 * Mark tuple for invalidation from system caches at next command
3135 * boundary. We have to do this before releasing the buffer because we
3136 * need to look at the contents of the tuple.
3137 */
3138 CacheInvalidateHeapTuple(relation, &tp, NULL);
3139
3140 /* Now we can release the buffer */
3141 ReleaseBuffer(buffer);
3142
3143 /*
3144 * Release the lmgr tuple lock, if we had it.
3145 */
3146 if (have_tuple_lock)
3148
3149 pgstat_count_heap_delete(relation);
3150
3151 if (old_key_tuple != NULL && old_key_copied)
3152 heap_freetuple(old_key_tuple);
3153
3154 return TM_Ok;
3155}
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:4161
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:5320
static void PageClearAllVisible(Page page)
Definition: bufpage.h:439
#define InvalidCommandId
Definition: c.h:640
void HeapTupleHeaderAdjustCmax(const HeapTupleHeaderData *tup, CommandId *cmax, bool *iscombo)
Definition: combocid.c:153
CommandId HeapTupleHeaderGetCmax(const HeapTupleHeaderData *tup)
Definition: combocid.c:118
static bool DoesMultiXactIdConflict(MultiXactId multi, uint16 infomask, LockTupleMode lockmode, bool *current_is_member)
Definition: heapam.c:7551
static XLogRecPtr log_heap_new_cid(Relation relation, HeapTuple tup)
Definition: heapam.c:9016
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:5290
static HeapTuple ExtractReplicaIdentity(Relation relation, HeapTuple tp, bool key_required, bool *copy)
Definition: heapam.c:9097
static bool heap_acquire_tuplock(Relation relation, ItemPointer tid, LockTupleMode mode, LockWaitPolicy wait_policy, bool *have_tuple_lock)
Definition: heapam.c:5241
static void MultiXactIdWait(MultiXactId multi, MultiXactStatus status, uint16 infomask, Relation rel, ItemPointer ctid, XLTW_Oper oper, int *remaining)
Definition: heapam.c:7729
static bool xmax_infomask_changed(uint16 new_infomask, uint16 old_infomask)
Definition: heapam.c:2722
#define UnlockTupleTuplock(rel, tup, mode)
Definition: heapam.c:164
static void UpdateXmaxHintBits(HeapTupleHeader tuple, Buffer buffer, TransactionId xid)
Definition: heapam.c:1967
bool HeapTupleSatisfiesVisibility(HeapTuple htup, Snapshot snapshot, Buffer buffer)
bool HeapTupleHeaderIsOnlyLocked(HeapTupleHeader tuple)
TM_Result HeapTupleSatisfiesUpdate(HeapTuple htup, CommandId curcid, Buffer buffer)
#define XLH_DELETE_CONTAINS_OLD_KEY
Definition: heapam_xlog.h:104
#define XLH_DELETE_ALL_VISIBLE_CLEARED
Definition: heapam_xlog.h:102
#define SizeOfHeapHeader
Definition: heapam_xlog.h:157
#define XLH_DELETE_IS_PARTITION_MOVE
Definition: heapam_xlog.h:106
#define XLH_DELETE_CONTAINS_OLD_TUPLE
Definition: heapam_xlog.h:103
#define SizeofHeapTupleHeader
Definition: htup_details.h:185
static void HeapTupleHeaderSetCmax(HeapTupleHeaderData *tup, CommandId cid, bool iscombo)
Definition: htup_details.h:431
static void HeapTupleHeaderClearHotUpdated(HeapTupleHeaderData *tup)
Definition: htup_details.h:549
static TransactionId HeapTupleHeaderGetRawXmax(const HeapTupleHeaderData *tup)
Definition: htup_details.h:377
static TransactionId HeapTupleHeaderGetUpdateXid(const HeapTupleHeaderData *tup)
Definition: htup_details.h:397
static void HeapTupleHeaderSetMovedPartitions(HeapTupleHeaderData *tup)
Definition: htup_details.h:486
static void HeapTupleHeaderSetXmax(HeapTupleHeaderData *tup, TransactionId xid)
Definition: htup_details.h:383
void CacheInvalidateHeapTuple(Relation relation, HeapTuple tuple, HeapTuple newtuple)
Definition: inval.c:1561
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:35
@ XLTW_Delete
Definition: lmgr.h:28
void MultiXactIdSetOldestMember(void)
Definition: multixact.c:673
void CheckForSerializableConflictIn(Relation relation, ItemPointer tid, BlockNumber blkno)
Definition: predicate.c:4336
#define RelationIsAccessibleInLogicalDecoding(relation)
Definition: rel.h:695
#define InvalidSnapshot
Definition: snapshot.h:119
TransactionId xmax
Definition: tableam.h:144
CommandId cmax
Definition: tableam.h:145
ItemPointerData ctid
Definition: tableam.h:143
uint16 t_infomask
Definition: heapam_xlog.h:153
uint16 t_infomask2
Definition: heapam_xlog.h:152
TM_Result
Definition: tableam.h:72
@ TM_Ok
Definition: tableam.h:77
@ TM_BeingModified
Definition: tableam.h:99
@ TM_Deleted
Definition: tableam.h:92
@ TM_Updated
Definition: tableam.h:89
@ TM_SelfModified
Definition: tableam.h:83
@ TM_Invisible
Definition: tableam.h:80
#define TransactionIdEquals(id1, id2)
Definition: transam.h:43
bool visibilitymap_clear(Relation rel, BlockNumber heapBlk, Buffer vmbuf, uint8 flags)
void visibilitymap_pin(Relation rel, BlockNumber heapBlk, Buffer *vmbuf)
#define VISIBILITYMAP_VALID_BITS
bool IsInParallelMode(void)
Definition: xact.c:1089
#define XLOG_INCLUDE_ORIGIN
Definition: xlog.h:154
void XLogSetRecordFlags(uint8 flags)
Definition: xloginsert.c:456

References Assert(), BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage(), CacheInvalidateHeapTuple(), CheckForSerializableConflictIn(), TM_FailureData::cmax, compute_infobits(), compute_new_xmax_infomask(), TM_FailureData::ctid, DoesMultiXactIdConflict(), END_CRIT_SECTION, ereport, errcode(), errmsg(), ERROR, ExtractReplicaIdentity(), xl_heap_delete::flags, GetCurrentTransactionId(), heap_acquire_tuplock(), heap_freetuple(), HEAP_MOVED, heap_toast_delete(), HEAP_XMAX_BITS, HEAP_XMAX_INVALID, HEAP_XMAX_IS_LOCKED_ONLY(), HEAP_XMAX_IS_MULTI, HeapTupleHasExternal(), HeapTupleHeaderAdjustCmax(), HeapTupleHeaderClearHotUpdated(), HeapTupleHeaderGetCmax(), HeapTupleHeaderGetRawXmax(), HeapTupleHeaderGetUpdateXid(), HeapTupleHeaderIsOnlyLocked(), HeapTupleHeaderSetCmax(), HeapTupleHeaderSetMovedPartitions(), HeapTupleHeaderSetXmax(), HeapTupleSatisfiesUpdate(), HeapTupleSatisfiesVisibility(), xl_heap_delete::infobits_set, InvalidBuffer, InvalidCommandId, InvalidSnapshot, IsInParallelMode(), ItemIdGetLength, ItemIdIsNormal, ItemPointerEquals(), ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), ItemPointerIsValid(), LockBuffer(), LockTupleExclusive, LockWaitBlock, log_heap_new_cid(), MarkBufferDirty(), MultiXactIdSetOldestMember(), MultiXactIdWait(), MultiXactStatusUpdate, xl_heap_delete::offnum, PageClearAllVisible(), PageGetItem(), PageGetItemId(), PageIsAllVisible(), PageSetLSN(), PageSetPrunable, pgstat_count_heap_delete(), RelationData::rd_rel, ReadBuffer(), REGBUF_STANDARD, RelationGetRelid, RelationIsAccessibleInLogicalDecoding, RelationNeedsWAL, ReleaseBuffer(), SizeOfHeapDelete, SizeOfHeapHeader, SizeofHeapTupleHeader, START_CRIT_SECTION, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, xl_heap_header::t_hoff, HeapTupleHeaderData::t_hoff, xl_heap_header::t_infomask, HeapTupleHeaderData::t_infomask, xl_heap_header::t_infomask2, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TM_BeingModified, TM_Deleted, TM_Invisible, TM_Ok, TM_SelfModified, TM_Updated, TransactionIdEquals, TransactionIdIsCurrentTransactionId(), UnlockReleaseBuffer(), UnlockTupleTuplock, UpdateXmaxHintBits(), visibilitymap_clear(), visibilitymap_pin(), VISIBILITYMAP_VALID_BITS, XactLockTableWait(), XLH_DELETE_ALL_VISIBLE_CLEARED, XLH_DELETE_CONTAINS_OLD_KEY, XLH_DELETE_CONTAINS_OLD_TUPLE, XLH_DELETE_IS_PARTITION_MOVE, XLOG_HEAP_DELETE, XLOG_INCLUDE_ORIGIN, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), XLogRegisterData(), XLogSetRecordFlags(), XLTW_Delete, xl_heap_delete::xmax, TM_FailureData::xmax, and xmax_infomask_changed().

Referenced by heapam_tuple_delete(), and simple_heap_delete().

◆ heap_endscan()

void heap_endscan ( TableScanDesc  sscan)

Definition at line 1275 of file heapam.c.

1276{
1277 HeapScanDesc scan = (HeapScanDesc) sscan;
1278
1279 /* Note: no locking manipulations needed */
1280
1281 /*
1282 * unpin scan buffers
1283 */
1284 if (BufferIsValid(scan->rs_cbuf))
1285 ReleaseBuffer(scan->rs_cbuf);
1286
1287 /*
1288 * Must free the read stream before freeing the BufferAccessStrategy.
1289 */
1290 if (scan->rs_read_stream)
1292
1293 /*
1294 * decrement relation reference count and free scan descriptor storage
1295 */
1297
1298 if (scan->rs_base.rs_key)
1299 pfree(scan->rs_base.rs_key);
1300
1301 if (scan->rs_strategy != NULL)
1303
1304 if (scan->rs_parallelworkerdata != NULL)
1306
1307 if (scan->rs_base.rs_flags & SO_TEMP_SNAPSHOT)
1309
1310 pfree(scan);
1311}
static bool BufferIsValid(Buffer bufnum)
Definition: bufmgr.h:365
void read_stream_end(ReadStream *stream)
Definition: read_stream.c:1055
void RelationDecrementReferenceCount(Relation rel)
Definition: relcache.c:2184
void UnregisterSnapshot(Snapshot snapshot)
Definition: snapmgr.c:853
@ SO_TEMP_SNAPSHOT
Definition: tableam.h:64

References BufferIsValid(), FreeAccessStrategy(), pfree(), read_stream_end(), RelationDecrementReferenceCount(), ReleaseBuffer(), HeapScanDescData::rs_base, HeapScanDescData::rs_cbuf, TableScanDescData::rs_flags, TableScanDescData::rs_key, HeapScanDescData::rs_parallelworkerdata, TableScanDescData::rs_rd, HeapScanDescData::rs_read_stream, TableScanDescData::rs_snapshot, HeapScanDescData::rs_strategy, SO_TEMP_SNAPSHOT, and UnregisterSnapshot().

◆ heap_fetch()

bool heap_fetch ( Relation  relation,
Snapshot  snapshot,
HeapTuple  tuple,
Buffer userbuf,
bool  keep_buf 
)

Definition at line 1573 of file heapam.c.

1578{
1579 ItemPointer tid = &(tuple->t_self);
1580 ItemId lp;
1581 Buffer buffer;
1582 Page page;
1583 OffsetNumber offnum;
1584 bool valid;
1585
1586 /*
1587 * Fetch and pin the appropriate page of the relation.
1588 */
1589 buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
1590
1591 /*
1592 * Need share lock on buffer to examine tuple commit status.
1593 */
1595 page = BufferGetPage(buffer);
1596
1597 /*
1598 * We'd better check for out-of-range offnum in case of VACUUM since the
1599 * TID was obtained.
1600 */
1601 offnum = ItemPointerGetOffsetNumber(tid);
1602 if (offnum < FirstOffsetNumber || offnum > PageGetMaxOffsetNumber(page))
1603 {
1605 ReleaseBuffer(buffer);
1606 *userbuf = InvalidBuffer;
1607 tuple->t_data = NULL;
1608 return false;
1609 }
1610
1611 /*
1612 * get the item line pointer corresponding to the requested tid
1613 */
1614 lp = PageGetItemId(page, offnum);
1615
1616 /*
1617 * Must check for deleted tuple.
1618 */
1619 if (!ItemIdIsNormal(lp))
1620 {
1622 ReleaseBuffer(buffer);
1623 *userbuf = InvalidBuffer;
1624 tuple->t_data = NULL;
1625 return false;
1626 }
1627
1628 /*
1629 * fill in *tuple fields
1630 */
1631 tuple->t_data = (HeapTupleHeader) PageGetItem(page, lp);
1632 tuple->t_len = ItemIdGetLength(lp);
1633 tuple->t_tableOid = RelationGetRelid(relation);
1634
1635 /*
1636 * check tuple visibility, then release lock
1637 */
1638 valid = HeapTupleSatisfiesVisibility(tuple, snapshot, buffer);
1639
1640 if (valid)
1641 PredicateLockTID(relation, &(tuple->t_self), snapshot,
1643
1644 HeapCheckForSerializableConflictOut(valid, relation, tuple, buffer, snapshot);
1645
1647
1648 if (valid)
1649 {
1650 /*
1651 * All checks passed, so return the tuple as valid. Caller is now
1652 * responsible for releasing the buffer.
1653 */
1654 *userbuf = buffer;
1655
1656 return true;
1657 }
1658
1659 /* Tuple failed time qual, but maybe caller wants to see it anyway. */
1660 if (keep_buf)
1661 *userbuf = buffer;
1662 else
1663 {
1664 ReleaseBuffer(buffer);
1665 *userbuf = InvalidBuffer;
1666 tuple->t_data = NULL;
1667 }
1668
1669 return false;
1670}
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:197
static OffsetNumber PageGetMaxOffsetNumber(const PageData *page)
Definition: bufpage.h:372
void HeapCheckForSerializableConflictOut(bool visible, Relation relation, HeapTuple tuple, Buffer buffer, Snapshot snapshot)
Definition: heapam.c:9201
static TransactionId HeapTupleHeaderGetXmin(const HeapTupleHeaderData *tup)
Definition: htup_details.h:324
uint16 OffsetNumber
Definition: off.h:24
void PredicateLockTID(Relation relation, ItemPointer tid, Snapshot snapshot, TransactionId tuple_xid)
Definition: predicate.c:2621

References BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage(), HeapCheckForSerializableConflictOut(), HeapTupleHeaderGetXmin(), HeapTupleSatisfiesVisibility(), InvalidBuffer, ItemIdGetLength, ItemIdIsNormal, ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), LockBuffer(), PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), PredicateLockTID(), ReadBuffer(), RelationGetRelid, ReleaseBuffer(), HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, and HeapTupleData::t_tableOid.

Referenced by heap_lock_updated_tuple_rec(), heapam_fetch_row_version(), and heapam_tuple_lock().

◆ heap_fetch_next_buffer()

static void heap_fetch_next_buffer ( HeapScanDesc  scan,
ScanDirection  dir 
)
inlinestatic

Definition at line 622 of file heapam.c.

623{
624 Assert(scan->rs_read_stream);
625
626 /* release previous scan buffer, if any */
627 if (BufferIsValid(scan->rs_cbuf))
628 {
629 ReleaseBuffer(scan->rs_cbuf);
630 scan->rs_cbuf = InvalidBuffer;
631 }
632
633 /*
634 * Be sure to check for interrupts at least once per page. Checks at
635 * higher code levels won't be able to stop a seqscan that encounters many
636 * pages' worth of consecutive dead tuples.
637 */
639
640 /*
641 * If the scan direction is changing, reset the prefetch block to the
642 * current block. Otherwise, we will incorrectly prefetch the blocks
643 * between the prefetch block and the current block again before
644 * prefetching blocks in the new, correct scan direction.
645 */
646 if (unlikely(scan->rs_dir != dir))
647 {
648 scan->rs_prefetch_block = scan->rs_cblock;
650 }
651
652 scan->rs_dir = dir;
653
655 if (BufferIsValid(scan->rs_cbuf))
657}
#define unlikely(x)
Definition: c.h:347
void read_stream_reset(ReadStream *stream)
Definition: read_stream.c:1010
Buffer read_stream_next_buffer(ReadStream *stream, void **per_buffer_data)
Definition: read_stream.c:770
ScanDirection rs_dir
Definition: heapam.h:86
BlockNumber rs_prefetch_block
Definition: heapam.h:87
BlockNumber rs_cblock
Definition: heapam.h:67

References Assert(), BufferGetBlockNumber(), BufferIsValid(), CHECK_FOR_INTERRUPTS, InvalidBuffer, read_stream_next_buffer(), read_stream_reset(), ReleaseBuffer(), HeapScanDescData::rs_cblock, HeapScanDescData::rs_cbuf, HeapScanDescData::rs_dir, HeapScanDescData::rs_prefetch_block, HeapScanDescData::rs_read_stream, and unlikely.

Referenced by heapgettup(), and heapgettup_pagemode().

◆ heap_finish_speculative()

void heap_finish_speculative ( Relation  relation,
ItemPointer  tid 
)

Definition at line 6052 of file heapam.c.

6053{
6054 Buffer buffer;
6055 Page page;
6056 OffsetNumber offnum;
6057 ItemId lp = NULL;
6058 HeapTupleHeader htup;
6059
6060 buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
6062 page = (Page) BufferGetPage(buffer);
6063
6064 offnum = ItemPointerGetOffsetNumber(tid);
6065 if (PageGetMaxOffsetNumber(page) >= offnum)
6066 lp = PageGetItemId(page, offnum);
6067
6068 if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsNormal(lp))
6069 elog(ERROR, "invalid lp");
6070
6071 htup = (HeapTupleHeader) PageGetItem(page, lp);
6072
6073 /* NO EREPORT(ERROR) from here till changes are logged */
6075
6077
6078 MarkBufferDirty(buffer);
6079
6080 /*
6081 * Replace the speculative insertion token with a real t_ctid, pointing to
6082 * itself like it does on regular tuples.
6083 */
6084 htup->t_ctid = *tid;
6085
6086 /* XLOG stuff */
6087 if (RelationNeedsWAL(relation))
6088 {
6089 xl_heap_confirm xlrec;
6090 XLogRecPtr recptr;
6091
6093
6095
6096 /* We want the same filtering on this as on a plain insert */
6098
6101
6102 recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_CONFIRM);
6103
6104 PageSetLSN(page, recptr);
6105 }
6106
6108
6109 UnlockReleaseBuffer(buffer);
6110}
#define SizeOfHeapConfirm
Definition: heapam_xlog.h:423
#define XLOG_HEAP_CONFIRM
Definition: heapam_xlog.h:38
OffsetNumber offnum
Definition: heapam_xlog.h:420

References Assert(), BUFFER_LOCK_EXCLUSIVE, BufferGetPage(), elog, END_CRIT_SECTION, ERROR, HeapTupleHeaderIsSpeculative(), ItemIdIsNormal, ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), LockBuffer(), MarkBufferDirty(), xl_heap_confirm::offnum, PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), PageSetLSN(), ReadBuffer(), REGBUF_STANDARD, RelationNeedsWAL, SizeOfHeapConfirm, START_CRIT_SECTION, HeapTupleHeaderData::t_ctid, UnlockReleaseBuffer(), XLOG_HEAP_CONFIRM, XLOG_INCLUDE_ORIGIN, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), XLogRegisterData(), and XLogSetRecordFlags().

Referenced by heapam_tuple_complete_speculative().

◆ heap_freeze_prepared_tuples()

void heap_freeze_prepared_tuples ( Buffer  buffer,
HeapTupleFreeze tuples,
int  ntuples 
)

Definition at line 7336 of file heapam.c.

7337{
7338 Page page = BufferGetPage(buffer);
7339
7340 for (int i = 0; i < ntuples; i++)
7341 {
7342 HeapTupleFreeze *frz = tuples + i;
7343 ItemId itemid = PageGetItemId(page, frz->offset);
7344 HeapTupleHeader htup;
7345
7346 htup = (HeapTupleHeader) PageGetItem(page, itemid);
7347 heap_execute_freeze_tuple(htup, frz);
7348 }
7349}
static void heap_execute_freeze_tuple(HeapTupleHeader tuple, HeapTupleFreeze *frz)
Definition: heapam.h:441
OffsetNumber offset
Definition: heapam.h:150

References BufferGetPage(), heap_execute_freeze_tuple(), i, HeapTupleFreeze::offset, PageGetItem(), and PageGetItemId().

Referenced by heap_page_prune_and_freeze().

◆ heap_freeze_tuple()

bool heap_freeze_tuple ( HeapTupleHeader  tuple,
TransactionId  relfrozenxid,
TransactionId  relminmxid,
TransactionId  FreezeLimit,
TransactionId  MultiXactCutoff 
)

Definition at line 7358 of file heapam.c.

7361{
7362 HeapTupleFreeze frz;
7363 bool do_freeze;
7364 bool totally_frozen;
7365 struct VacuumCutoffs cutoffs;
7366 HeapPageFreeze pagefrz;
7367
7368 cutoffs.relfrozenxid = relfrozenxid;
7369 cutoffs.relminmxid = relminmxid;
7370 cutoffs.OldestXmin = FreezeLimit;
7371 cutoffs.OldestMxact = MultiXactCutoff;
7372 cutoffs.FreezeLimit = FreezeLimit;
7373 cutoffs.MultiXactCutoff = MultiXactCutoff;
7374
7375 pagefrz.freeze_required = true;
7376 pagefrz.FreezePageRelfrozenXid = FreezeLimit;
7377 pagefrz.FreezePageRelminMxid = MultiXactCutoff;
7378 pagefrz.NoFreezePageRelfrozenXid = FreezeLimit;
7379 pagefrz.NoFreezePageRelminMxid = MultiXactCutoff;
7380
7381 do_freeze = heap_prepare_freeze_tuple(tuple, &cutoffs,
7382 &pagefrz, &frz, &totally_frozen);
7383
7384 /*
7385 * Note that because this is not a WAL-logged operation, we don't need to
7386 * fill in the offset in the freeze record.
7387 */
7388
7389 if (do_freeze)
7390 heap_execute_freeze_tuple(tuple, &frz);
7391 return do_freeze;
7392}
bool heap_prepare_freeze_tuple(HeapTupleHeader tuple, const struct VacuumCutoffs *cutoffs, HeapPageFreeze *pagefrz, HeapTupleFreeze *frz, bool *totally_frozen)
Definition: heapam.c:7010

References VacuumCutoffs::FreezeLimit, heap_execute_freeze_tuple(), heap_prepare_freeze_tuple(), VacuumCutoffs::MultiXactCutoff, VacuumCutoffs::OldestMxact, VacuumCutoffs::OldestXmin, VacuumCutoffs::relfrozenxid, and VacuumCutoffs::relminmxid.

Referenced by rewrite_heap_tuple().

◆ heap_get_latest_tid()

void heap_get_latest_tid ( TableScanDesc  sscan,
ItemPointer  tid 
)

Definition at line 1845 of file heapam.c.

1847{
1848 Relation relation = sscan->rs_rd;
1849 Snapshot snapshot = sscan->rs_snapshot;
1850 ItemPointerData ctid;
1851 TransactionId priorXmax;
1852
1853 /*
1854 * table_tuple_get_latest_tid() verified that the passed in tid is valid.
1855 * Assume that t_ctid links are valid however - there shouldn't be invalid
1856 * ones in the table.
1857 */
1859
1860 /*
1861 * Loop to chase down t_ctid links. At top of loop, ctid is the tuple we
1862 * need to examine, and *tid is the TID we will return if ctid turns out
1863 * to be bogus.
1864 *
1865 * Note that we will loop until we reach the end of the t_ctid chain.
1866 * Depending on the snapshot passed, there might be at most one visible
1867 * version of the row, but we don't try to optimize for that.
1868 */
1869 ctid = *tid;
1870 priorXmax = InvalidTransactionId; /* cannot check first XMIN */
1871 for (;;)
1872 {
1873 Buffer buffer;
1874 Page page;
1875 OffsetNumber offnum;
1876 ItemId lp;
1877 HeapTupleData tp;
1878 bool valid;
1879
1880 /*
1881 * Read, pin, and lock the page.
1882 */
1883 buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(&ctid));
1885 page = BufferGetPage(buffer);
1886
1887 /*
1888 * Check for bogus item number. This is not treated as an error
1889 * condition because it can happen while following a t_ctid link. We
1890 * just assume that the prior tid is OK and return it unchanged.
1891 */
1892 offnum = ItemPointerGetOffsetNumber(&ctid);
1893 if (offnum < FirstOffsetNumber || offnum > PageGetMaxOffsetNumber(page))
1894 {
1895 UnlockReleaseBuffer(buffer);
1896 break;
1897 }
1898 lp = PageGetItemId(page, offnum);
1899 if (!ItemIdIsNormal(lp))
1900 {
1901 UnlockReleaseBuffer(buffer);
1902 break;
1903 }
1904
1905 /* OK to access the tuple */
1906 tp.t_self = ctid;
1907 tp.t_data = (HeapTupleHeader) PageGetItem(page, lp);
1908 tp.t_len = ItemIdGetLength(lp);
1909 tp.t_tableOid = RelationGetRelid(relation);
1910
1911 /*
1912 * After following a t_ctid link, we might arrive at an unrelated
1913 * tuple. Check for XMIN match.
1914 */
1915 if (TransactionIdIsValid(priorXmax) &&
1917 {
1918 UnlockReleaseBuffer(buffer);
1919 break;
1920 }
1921
1922 /*
1923 * Check tuple visibility; if visible, set it as the new result
1924 * candidate.
1925 */
1926 valid = HeapTupleSatisfiesVisibility(&tp, snapshot, buffer);
1927 HeapCheckForSerializableConflictOut(valid, relation, &tp, buffer, snapshot);
1928 if (valid)
1929 *tid = ctid;
1930
1931 /*
1932 * If there's a valid t_ctid link, follow it, else we're done.
1933 */
1934 if ((tp.t_data->t_infomask & HEAP_XMAX_INVALID) ||
1938 {
1939 UnlockReleaseBuffer(buffer);
1940 break;
1941 }
1942
1943 ctid = tp.t_data->t_ctid;
1944 priorXmax = HeapTupleHeaderGetUpdateXid(tp.t_data);
1945 UnlockReleaseBuffer(buffer);
1946 } /* end of loop */
1947}
static bool HeapTupleHeaderIndicatesMovedPartitions(const HeapTupleHeaderData *tup)
Definition: htup_details.h:480

References Assert(), BUFFER_LOCK_SHARE, BufferGetPage(), HEAP_XMAX_INVALID, HeapCheckForSerializableConflictOut(), HeapTupleHeaderGetUpdateXid(), HeapTupleHeaderGetXmin(), HeapTupleHeaderIndicatesMovedPartitions(), HeapTupleHeaderIsOnlyLocked(), HeapTupleSatisfiesVisibility(), InvalidTransactionId, ItemIdGetLength, ItemIdIsNormal, ItemPointerEquals(), ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), ItemPointerIsValid(), LockBuffer(), PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), ReadBuffer(), RelationGetRelid, TableScanDescData::rs_rd, TableScanDescData::rs_snapshot, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleHeaderData::t_infomask, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TransactionIdEquals, TransactionIdIsValid, and UnlockReleaseBuffer().

◆ heap_getnext()

HeapTuple heap_getnext ( TableScanDesc  sscan,
ScanDirection  direction 
)

Definition at line 1314 of file heapam.c.

1315{
1316 HeapScanDesc scan = (HeapScanDesc) sscan;
1317
1318 /*
1319 * This is still widely used directly, without going through table AM, so
1320 * add a safety check. It's possible we should, at a later point,
1321 * downgrade this to an assert. The reason for checking the AM routine,
1322 * rather than the AM oid, is that this allows to write regression tests
1323 * that create another AM reusing the heap handler.
1324 */
1326 ereport(ERROR,
1327 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1328 errmsg_internal("only heap AM is supported")));
1329
1330 /*
1331 * We don't expect direct calls to heap_getnext with valid CheckXidAlive
1332 * for catalog or regular tables. See detailed comments in xact.c where
1333 * these variables are declared. Normally we have such a check at tableam
1334 * level API but this is called from many places so we need to ensure it
1335 * here.
1336 */
1338 elog(ERROR, "unexpected heap_getnext call during logical decoding");
1339
1340 /* Note: no locking manipulations needed */
1341
1343 heapgettup_pagemode(scan, direction,
1344 scan->rs_base.rs_nkeys, scan->rs_base.rs_key);
1345 else
1346 heapgettup(scan, direction,
1347 scan->rs_base.rs_nkeys, scan->rs_base.rs_key);
1348
1349 if (scan->rs_ctup.t_data == NULL)
1350 return NULL;
1351
1352 /*
1353 * if we get here it means we have a new current scan tuple, so point to
1354 * the proper return buffer and return the tuple.
1355 */
1356
1358
1359 return &scan->rs_ctup;
1360}
static void heapgettup(HeapScanDesc scan, ScanDirection dir, int nkeys, ScanKey key)
Definition: heapam.c:875
static void heapgettup_pagemode(HeapScanDesc scan, ScanDirection dir, int nkeys, ScanKey key)
Definition: heapam.c:985
const TableAmRoutine * GetHeapamTableAmRoutine(void)
#define pgstat_count_heap_getnext(rel)
Definition: pgstat.h:684
const struct TableAmRoutine * rd_tableam
Definition: rel.h:189
@ SO_ALLOW_PAGEMODE
Definition: tableam.h:61
bool bsysscan
Definition: xact.c:100
TransactionId CheckXidAlive
Definition: xact.c:99

References bsysscan, CheckXidAlive, elog, ereport, errcode(), errmsg_internal(), ERROR, GetHeapamTableAmRoutine(), heapgettup(), heapgettup_pagemode(), pgstat_count_heap_getnext, RelationData::rd_tableam, HeapScanDescData::rs_base, HeapScanDescData::rs_ctup, TableScanDescData::rs_flags, TableScanDescData::rs_key, TableScanDescData::rs_nkeys, TableScanDescData::rs_rd, SO_ALLOW_PAGEMODE, HeapTupleData::t_data, TransactionIdIsValid, and unlikely.

Referenced by AlterTableMoveAll(), AlterTableSpaceOptions(), check_db_file_conflict(), CreateDatabaseUsingFileCopy(), do_autovacuum(), DropSetting(), DropTableSpace(), find_typed_table_dependencies(), get_all_vacuum_rels(), get_database_list(), get_subscription_list(), get_tables_to_cluster(), get_tablespace_name(), get_tablespace_oid(), GetAllTablesPublicationRelations(), getRelationsInNamespace(), GetSchemaPublicationRelations(), heapam_index_build_range_scan(), heapam_index_validate_scan(), objectsInSchemaToOids(), pgrowlocks(), pgstat_heap(), populate_typ_list(), ReindexMultipleTables(), remove_dbtablespaces(), RemoveSubscriptionRel(), RenameTableSpace(), ThereIsAtLeastOneRole(), and vac_truncate_clog().

◆ heap_getnextslot()

bool heap_getnextslot ( TableScanDesc  sscan,
ScanDirection  direction,
TupleTableSlot slot 
)

Definition at line 1363 of file heapam.c.

1364{
1365 HeapScanDesc scan = (HeapScanDesc) sscan;
1366
1367 /* Note: no locking manipulations needed */
1368
1369 if (sscan->rs_flags & SO_ALLOW_PAGEMODE)
1370 heapgettup_pagemode(scan, direction, sscan->rs_nkeys, sscan->rs_key);
1371 else
1372 heapgettup(scan, direction, sscan->rs_nkeys, sscan->rs_key);
1373
1374 if (scan->rs_ctup.t_data == NULL)
1375 {
1376 ExecClearTuple(slot);
1377 return false;
1378 }
1379
1380 /*
1381 * if we get here it means we have a new current scan tuple, so point to
1382 * the proper return buffer and return the tuple.
1383 */
1384
1386
1387 ExecStoreBufferHeapTuple(&scan->rs_ctup, slot,
1388 scan->rs_cbuf);
1389 return true;
1390}
TupleTableSlot * ExecStoreBufferHeapTuple(HeapTuple tuple, TupleTableSlot *slot, Buffer buffer)
Definition: execTuples.c:1581
static TupleTableSlot * ExecClearTuple(TupleTableSlot *slot)
Definition: tuptable.h:458

References ExecClearTuple(), ExecStoreBufferHeapTuple(), heapgettup(), heapgettup_pagemode(), pgstat_count_heap_getnext, HeapScanDescData::rs_base, HeapScanDescData::rs_cbuf, HeapScanDescData::rs_ctup, TableScanDescData::rs_flags, TableScanDescData::rs_key, TableScanDescData::rs_nkeys, TableScanDescData::rs_rd, SO_ALLOW_PAGEMODE, and HeapTupleData::t_data.

◆ heap_getnextslot_tidrange()

bool heap_getnextslot_tidrange ( TableScanDesc  sscan,
ScanDirection  direction,
TupleTableSlot slot 
)

Definition at line 1466 of file heapam.c.

1468{
1469 HeapScanDesc scan = (HeapScanDesc) sscan;
1470 ItemPointer mintid = &sscan->st.tidrange.rs_mintid;
1471 ItemPointer maxtid = &sscan->st.tidrange.rs_maxtid;
1472
1473 /* Note: no locking manipulations needed */
1474 for (;;)
1475 {
1476 if (sscan->rs_flags & SO_ALLOW_PAGEMODE)
1477 heapgettup_pagemode(scan, direction, sscan->rs_nkeys, sscan->rs_key);
1478 else
1479 heapgettup(scan, direction, sscan->rs_nkeys, sscan->rs_key);
1480
1481 if (scan->rs_ctup.t_data == NULL)
1482 {
1483 ExecClearTuple(slot);
1484 return false;
1485 }
1486
1487 /*
1488 * heap_set_tidrange will have used heap_setscanlimits to limit the
1489 * range of pages we scan to only ones that can contain the TID range
1490 * we're scanning for. Here we must filter out any tuples from these
1491 * pages that are outside of that range.
1492 */
1493 if (ItemPointerCompare(&scan->rs_ctup.t_self, mintid) < 0)
1494 {
1495 ExecClearTuple(slot);
1496
1497 /*
1498 * When scanning backwards, the TIDs will be in descending order.
1499 * Future tuples in this direction will be lower still, so we can
1500 * just return false to indicate there will be no more tuples.
1501 */
1502 if (ScanDirectionIsBackward(direction))
1503 return false;
1504
1505 continue;
1506 }
1507
1508 /*
1509 * Likewise for the final page, we must filter out TIDs greater than
1510 * maxtid.
1511 */
1512 if (ItemPointerCompare(&scan->rs_ctup.t_self, maxtid) > 0)
1513 {
1514 ExecClearTuple(slot);
1515
1516 /*
1517 * When scanning forward, the TIDs will be in ascending order.
1518 * Future tuples in this direction will be higher still, so we can
1519 * just return false to indicate there will be no more tuples.
1520 */
1521 if (ScanDirectionIsForward(direction))
1522 return false;
1523 continue;
1524 }
1525
1526 break;
1527 }
1528
1529 /*
1530 * if we get here it means we have a new current scan tuple, so point to
1531 * the proper return buffer and return the tuple.
1532 */
1534
1535 ExecStoreBufferHeapTuple(&scan->rs_ctup, slot, scan->rs_cbuf);
1536 return true;
1537}
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
Definition: itemptr.c:51
#define ScanDirectionIsForward(direction)
Definition: sdir.h:64
#define ScanDirectionIsBackward(direction)
Definition: sdir.h:50
ItemPointerData rs_mintid
Definition: relscan.h:55
ItemPointerData rs_maxtid
Definition: relscan.h:56
struct TableScanDescData::@49::@50 tidrange

References ExecClearTuple(), ExecStoreBufferHeapTuple(), heapgettup(), heapgettup_pagemode(), ItemPointerCompare(), pgstat_count_heap_getnext, HeapScanDescData::rs_base, HeapScanDescData::rs_cbuf, HeapScanDescData::rs_ctup, TableScanDescData::rs_flags, TableScanDescData::rs_key, TableScanDescData::rs_maxtid, TableScanDescData::rs_mintid, TableScanDescData::rs_nkeys, TableScanDescData::rs_rd, ScanDirectionIsBackward, ScanDirectionIsForward, SO_ALLOW_PAGEMODE, TableScanDescData::st, HeapTupleData::t_data, HeapTupleData::t_self, and TableScanDescData::tidrange.

◆ heap_hot_search_buffer()

bool heap_hot_search_buffer ( ItemPointer  tid,
Relation  relation,
Buffer  buffer,
Snapshot  snapshot,
HeapTuple  heapTuple,
bool *  all_dead,
bool  first_call 
)

Definition at line 1693 of file heapam.c.

1696{
1697 Page page = BufferGetPage(buffer);
1699 BlockNumber blkno;
1700 OffsetNumber offnum;
1701 bool at_chain_start;
1702 bool valid;
1703 bool skip;
1704 GlobalVisState *vistest = NULL;
1705
1706 /* If this is not the first call, previous call returned a (live!) tuple */
1707 if (all_dead)
1708 *all_dead = first_call;
1709
1710 blkno = ItemPointerGetBlockNumber(tid);
1711 offnum = ItemPointerGetOffsetNumber(tid);
1712 at_chain_start = first_call;
1713 skip = !first_call;
1714
1715 /* XXX: we should assert that a snapshot is pushed or registered */
1717 Assert(BufferGetBlockNumber(buffer) == blkno);
1718
1719 /* Scan through possible multiple members of HOT-chain */
1720 for (;;)
1721 {
1722 ItemId lp;
1723
1724 /* check for bogus TID */
1725 if (offnum < FirstOffsetNumber || offnum > PageGetMaxOffsetNumber(page))
1726 break;
1727
1728 lp = PageGetItemId(page, offnum);
1729
1730 /* check for unused, dead, or redirected items */
1731 if (!ItemIdIsNormal(lp))
1732 {
1733 /* We should only see a redirect at start of chain */
1734 if (ItemIdIsRedirected(lp) && at_chain_start)
1735 {
1736 /* Follow the redirect */
1737 offnum = ItemIdGetRedirect(lp);
1738 at_chain_start = false;
1739 continue;
1740 }
1741 /* else must be end of chain */
1742 break;
1743 }
1744
1745 /*
1746 * Update heapTuple to point to the element of the HOT chain we're
1747 * currently investigating. Having t_self set correctly is important
1748 * because the SSI checks and the *Satisfies routine for historical
1749 * MVCC snapshots need the correct tid to decide about the visibility.
1750 */
1751 heapTuple->t_data = (HeapTupleHeader) PageGetItem(page, lp);
1752 heapTuple->t_len = ItemIdGetLength(lp);
1753 heapTuple->t_tableOid = RelationGetRelid(relation);
1754 ItemPointerSet(&heapTuple->t_self, blkno, offnum);
1755
1756 /*
1757 * Shouldn't see a HEAP_ONLY tuple at chain start.
1758 */
1759 if (at_chain_start && HeapTupleIsHeapOnly(heapTuple))
1760 break;
1761
1762 /*
1763 * The xmin should match the previous xmax value, else chain is
1764 * broken.
1765 */
1766 if (TransactionIdIsValid(prev_xmax) &&
1767 !TransactionIdEquals(prev_xmax,
1768 HeapTupleHeaderGetXmin(heapTuple->t_data)))
1769 break;
1770
1771 /*
1772 * When first_call is true (and thus, skip is initially false) we'll
1773 * return the first tuple we find. But on later passes, heapTuple
1774 * will initially be pointing to the tuple we returned last time.
1775 * Returning it again would be incorrect (and would loop forever), so
1776 * we skip it and return the next match we find.
1777 */
1778 if (!skip)
1779 {
1780 /* If it's visible per the snapshot, we must return it */
1781 valid = HeapTupleSatisfiesVisibility(heapTuple, snapshot, buffer);
1782 HeapCheckForSerializableConflictOut(valid, relation, heapTuple,
1783 buffer, snapshot);
1784
1785 if (valid)
1786 {
1787 ItemPointerSetOffsetNumber(tid, offnum);
1788 PredicateLockTID(relation, &heapTuple->t_self, snapshot,
1789 HeapTupleHeaderGetXmin(heapTuple->t_data));
1790 if (all_dead)
1791 *all_dead = false;
1792 return true;
1793 }
1794 }
1795 skip = false;
1796
1797 /*
1798 * If we can't see it, maybe no one else can either. At caller
1799 * request, check whether all chain members are dead to all
1800 * transactions.
1801 *
1802 * Note: if you change the criterion here for what is "dead", fix the
1803 * planner's get_actual_variable_range() function to match.
1804 */
1805 if (all_dead && *all_dead)
1806 {
1807 if (!vistest)
1808 vistest = GlobalVisTestFor(relation);
1809
1810 if (!HeapTupleIsSurelyDead(heapTuple, vistest))
1811 *all_dead = false;
1812 }
1813
1814 /*
1815 * Check to see if HOT chain continues past this tuple; if so fetch
1816 * the next offnum and loop around.
1817 */
1818 if (HeapTupleIsHotUpdated(heapTuple))
1819 {
1821 blkno);
1822 offnum = ItemPointerGetOffsetNumber(&heapTuple->t_data->t_ctid);
1823 at_chain_start = false;
1824 prev_xmax = HeapTupleHeaderGetUpdateXid(heapTuple->t_data);
1825 }
1826 else
1827 break; /* end of chain */
1828 }
1829
1830 return false;
1831}
bool HeapTupleIsSurelyDead(HeapTuple htup, GlobalVisState *vistest)
static bool HeapTupleIsHotUpdated(const HeapTupleData *tuple)
Definition: htup_details.h:768
static bool HeapTupleIsHeapOnly(const HeapTupleData *tuple)
Definition: htup_details.h:786
#define ItemIdGetRedirect(itemId)
Definition: itemid.h:78
#define ItemIdIsRedirected(itemId)
Definition: itemid.h:106
static void ItemPointerSet(ItemPointerData *pointer, BlockNumber blockNumber, OffsetNumber offNum)
Definition: itemptr.h:135
static void ItemPointerSetOffsetNumber(ItemPointerData *pointer, OffsetNumber offsetNumber)
Definition: itemptr.h:158
static const struct exclude_list_item skip[]
Definition: pg_checksums.c:107
GlobalVisState * GlobalVisTestFor(Relation rel)
Definition: procarray.c:4107
TransactionId RecentXmin
Definition: snapmgr.c:159

References Assert(), BufferGetBlockNumber(), BufferGetPage(), GlobalVisTestFor(), HeapCheckForSerializableConflictOut(), HeapTupleHeaderGetUpdateXid(), HeapTupleHeaderGetXmin(), HeapTupleIsHeapOnly(), HeapTupleIsHotUpdated(), HeapTupleIsSurelyDead(), HeapTupleSatisfiesVisibility(), InvalidTransactionId, ItemIdGetLength, ItemIdGetRedirect, ItemIdIsNormal, ItemIdIsRedirected, ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), ItemPointerSet(), ItemPointerSetOffsetNumber(), PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), PredicateLockTID(), RecentXmin, RelationGetRelid, skip, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TransactionIdEquals, and TransactionIdIsValid.

Referenced by BitmapHeapScanNextBlock(), heap_index_delete_tuples(), and heapam_index_fetch_tuple().

◆ heap_index_delete_tuples()

TransactionId heap_index_delete_tuples ( Relation  rel,
TM_IndexDeleteOp delstate 
)

Definition at line 8074 of file heapam.c.

8075{
8076 /* Initial assumption is that earlier pruning took care of conflict */
8077 TransactionId snapshotConflictHorizon = InvalidTransactionId;
8080 Page page = NULL;
8082 TransactionId priorXmax;
8083#ifdef USE_PREFETCH
8084 IndexDeletePrefetchState prefetch_state;
8085 int prefetch_distance;
8086#endif
8087 SnapshotData SnapshotNonVacuumable;
8088 int finalndeltids = 0,
8089 nblocksaccessed = 0;
8090
8091 /* State that's only used in bottom-up index deletion case */
8092 int nblocksfavorable = 0;
8093 int curtargetfreespace = delstate->bottomupfreespace,
8094 lastfreespace = 0,
8095 actualfreespace = 0;
8096 bool bottomup_final_block = false;
8097
8098 InitNonVacuumableSnapshot(SnapshotNonVacuumable, GlobalVisTestFor(rel));
8099
8100 /* Sort caller's deltids array by TID for further processing */
8101 index_delete_sort(delstate);
8102
8103 /*
8104 * Bottom-up case: resort deltids array in an order attuned to where the
8105 * greatest number of promising TIDs are to be found, and determine how
8106 * many blocks from the start of sorted array should be considered
8107 * favorable. This will also shrink the deltids array in order to
8108 * eliminate completely unfavorable blocks up front.
8109 */
8110 if (delstate->bottomup)
8111 nblocksfavorable = bottomup_sort_and_shrink(delstate);
8112
8113#ifdef USE_PREFETCH
8114 /* Initialize prefetch state. */
8115 prefetch_state.cur_hblkno = InvalidBlockNumber;
8116 prefetch_state.next_item = 0;
8117 prefetch_state.ndeltids = delstate->ndeltids;
8118 prefetch_state.deltids = delstate->deltids;
8119
8120 /*
8121 * Determine the prefetch distance that we will attempt to maintain.
8122 *
8123 * Since the caller holds a buffer lock somewhere in rel, we'd better make
8124 * sure that isn't a catalog relation before we call code that does
8125 * syscache lookups, to avoid risk of deadlock.
8126 */
8127 if (IsCatalogRelation(rel))
8128 prefetch_distance = maintenance_io_concurrency;
8129 else
8130 prefetch_distance =
8132
8133 /* Cap initial prefetch distance for bottom-up deletion caller */
8134 if (delstate->bottomup)
8135 {
8136 Assert(nblocksfavorable >= 1);
8137 Assert(nblocksfavorable <= BOTTOMUP_MAX_NBLOCKS);
8138 prefetch_distance = Min(prefetch_distance, nblocksfavorable);
8139 }
8140
8141 /* Start prefetching. */
8142 index_delete_prefetch_buffer(rel, &prefetch_state, prefetch_distance);
8143#endif
8144
8145 /* Iterate over deltids, determine which to delete, check their horizon */
8146 Assert(delstate->ndeltids > 0);
8147 for (int i = 0; i < delstate->ndeltids; i++)
8148 {
8149 TM_IndexDelete *ideltid = &delstate->deltids[i];
8150 TM_IndexStatus *istatus = delstate->status + ideltid->id;
8151 ItemPointer htid = &ideltid->tid;
8152 OffsetNumber offnum;
8153
8154 /*
8155 * Read buffer, and perform required extra steps each time a new block
8156 * is encountered. Avoid refetching if it's the same block as the one
8157 * from the last htid.
8158 */
8159 if (blkno == InvalidBlockNumber ||
8160 ItemPointerGetBlockNumber(htid) != blkno)
8161 {
8162 /*
8163 * Consider giving up early for bottom-up index deletion caller
8164 * first. (Only prefetch next-next block afterwards, when it
8165 * becomes clear that we're at least going to access the next
8166 * block in line.)
8167 *
8168 * Sometimes the first block frees so much space for bottom-up
8169 * caller that the deletion process can end without accessing any
8170 * more blocks. It is usually necessary to access 2 or 3 blocks
8171 * per bottom-up deletion operation, though.
8172 */
8173 if (delstate->bottomup)
8174 {
8175 /*
8176 * We often allow caller to delete a few additional items
8177 * whose entries we reached after the point that space target
8178 * from caller was satisfied. The cost of accessing the page
8179 * was already paid at that point, so it made sense to finish
8180 * it off. When that happened, we finalize everything here
8181 * (by finishing off the whole bottom-up deletion operation
8182 * without needlessly paying the cost of accessing any more
8183 * blocks).
8184 */
8185 if (bottomup_final_block)
8186 break;
8187
8188 /*
8189 * Give up when we didn't enable our caller to free any
8190 * additional space as a result of processing the page that we
8191 * just finished up with. This rule is the main way in which
8192 * we keep the cost of bottom-up deletion under control.
8193 */
8194 if (nblocksaccessed >= 1 && actualfreespace == lastfreespace)
8195 break;
8196 lastfreespace = actualfreespace; /* for next time */
8197
8198 /*
8199 * Deletion operation (which is bottom-up) will definitely
8200 * access the next block in line. Prepare for that now.
8201 *
8202 * Decay target free space so that we don't hang on for too
8203 * long with a marginal case. (Space target is only truly
8204 * helpful when it allows us to recognize that we don't need
8205 * to access more than 1 or 2 blocks to satisfy caller due to
8206 * agreeable workload characteristics.)
8207 *
8208 * We are a bit more patient when we encounter contiguous
8209 * blocks, though: these are treated as favorable blocks. The
8210 * decay process is only applied when the next block in line
8211 * is not a favorable/contiguous block. This is not an
8212 * exception to the general rule; we still insist on finding
8213 * at least one deletable item per block accessed. See
8214 * bottomup_nblocksfavorable() for full details of the theory
8215 * behind favorable blocks and heap block locality in general.
8216 *
8217 * Note: The first block in line is always treated as a
8218 * favorable block, so the earliest possible point that the
8219 * decay can be applied is just before we access the second
8220 * block in line. The Assert() verifies this for us.
8221 */
8222 Assert(nblocksaccessed > 0 || nblocksfavorable > 0);
8223 if (nblocksfavorable > 0)
8224 nblocksfavorable--;
8225 else
8226 curtargetfreespace /= 2;
8227 }
8228
8229 /* release old buffer */
8230 if (BufferIsValid(buf))
8232
8233 blkno = ItemPointerGetBlockNumber(htid);
8234 buf = ReadBuffer(rel, blkno);
8235 nblocksaccessed++;
8236 Assert(!delstate->bottomup ||
8237 nblocksaccessed <= BOTTOMUP_MAX_NBLOCKS);
8238
8239#ifdef USE_PREFETCH
8240
8241 /*
8242 * To maintain the prefetch distance, prefetch one more page for
8243 * each page we read.
8244 */
8245 index_delete_prefetch_buffer(rel, &prefetch_state, 1);
8246#endif
8247
8249
8250 page = BufferGetPage(buf);
8251 maxoff = PageGetMaxOffsetNumber(page);
8252 }
8253
8254 /*
8255 * In passing, detect index corruption involving an index page with a
8256 * TID that points to a location in the heap that couldn't possibly be
8257 * correct. We only do this with actual TIDs from caller's index page
8258 * (not items reached by traversing through a HOT chain).
8259 */
8260 index_delete_check_htid(delstate, page, maxoff, htid, istatus);
8261
8262 if (istatus->knowndeletable)
8263 Assert(!delstate->bottomup && !istatus->promising);
8264 else
8265 {
8266 ItemPointerData tmp = *htid;
8267 HeapTupleData heapTuple;
8268
8269 /* Are any tuples from this HOT chain non-vacuumable? */
8270 if (heap_hot_search_buffer(&tmp, rel, buf, &SnapshotNonVacuumable,
8271 &heapTuple, NULL, true))
8272 continue; /* can't delete entry */
8273
8274 /* Caller will delete, since whole HOT chain is vacuumable */
8275 istatus->knowndeletable = true;
8276
8277 /* Maintain index free space info for bottom-up deletion case */
8278 if (delstate->bottomup)
8279 {
8280 Assert(istatus->freespace > 0);
8281 actualfreespace += istatus->freespace;
8282 if (actualfreespace >= curtargetfreespace)
8283 bottomup_final_block = true;
8284 }
8285 }
8286
8287 /*
8288 * Maintain snapshotConflictHorizon value for deletion operation as a
8289 * whole by advancing current value using heap tuple headers. This is
8290 * loosely based on the logic for pruning a HOT chain.
8291 */
8292 offnum = ItemPointerGetOffsetNumber(htid);
8293 priorXmax = InvalidTransactionId; /* cannot check first XMIN */
8294 for (;;)
8295 {
8296 ItemId lp;
8297 HeapTupleHeader htup;
8298
8299 /* Sanity check (pure paranoia) */
8300 if (offnum < FirstOffsetNumber)
8301 break;
8302
8303 /*
8304 * An offset past the end of page's line pointer array is possible
8305 * when the array was truncated
8306 */
8307 if (offnum > maxoff)
8308 break;
8309
8310 lp = PageGetItemId(page, offnum);
8311 if (ItemIdIsRedirected(lp))
8312 {
8313 offnum = ItemIdGetRedirect(lp);
8314 continue;
8315 }
8316
8317 /*
8318 * We'll often encounter LP_DEAD line pointers (especially with an
8319 * entry marked knowndeletable by our caller up front). No heap
8320 * tuple headers get examined for an htid that leads us to an
8321 * LP_DEAD item. This is okay because the earlier pruning
8322 * operation that made the line pointer LP_DEAD in the first place
8323 * must have considered the original tuple header as part of
8324 * generating its own snapshotConflictHorizon value.
8325 *
8326 * Relying on XLOG_HEAP2_PRUNE_VACUUM_SCAN records like this is
8327 * the same strategy that index vacuuming uses in all cases. Index
8328 * VACUUM WAL records don't even have a snapshotConflictHorizon
8329 * field of their own for this reason.
8330 */
8331 if (!ItemIdIsNormal(lp))
8332 break;
8333
8334 htup = (HeapTupleHeader) PageGetItem(page, lp);
8335
8336 /*
8337 * Check the tuple XMIN against prior XMAX, if any
8338 */
8339 if (TransactionIdIsValid(priorXmax) &&
8341 break;
8342
8344 &snapshotConflictHorizon);
8345
8346 /*
8347 * If the tuple is not HOT-updated, then we are at the end of this
8348 * HOT-chain. No need to visit later tuples from the same update
8349 * chain (they get their own index entries) -- just move on to
8350 * next htid from index AM caller.
8351 */
8352 if (!HeapTupleHeaderIsHotUpdated(htup))
8353 break;
8354
8355 /* Advance to next HOT chain member */
8356 Assert(ItemPointerGetBlockNumber(&htup->t_ctid) == blkno);
8357 offnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
8358 priorXmax = HeapTupleHeaderGetUpdateXid(htup);
8359 }
8360
8361 /* Enable further/final shrinking of deltids for caller */
8362 finalndeltids = i + 1;
8363 }
8364
8366
8367 /*
8368 * Shrink deltids array to exclude non-deletable entries at the end. This
8369 * is not just a minor optimization. Final deltids array size might be
8370 * zero for a bottom-up caller. Index AM is explicitly allowed to rely on
8371 * ndeltids being zero in all cases with zero total deletable entries.
8372 */
8373 Assert(finalndeltids > 0 || delstate->bottomup);
8374 delstate->ndeltids = finalndeltids;
8375
8376 return snapshotConflictHorizon;
8377}
int maintenance_io_concurrency
Definition: bufmgr.c:159
bool IsCatalogRelation(Relation relation)
Definition: catalog.c:103
static int bottomup_sort_and_shrink(TM_IndexDeleteOp *delstate)
Definition: heapam.c:8631
void HeapTupleHeaderAdvanceConflictHorizon(HeapTupleHeader tuple, TransactionId *snapshotConflictHorizon)
Definition: heapam.c:7929
static void index_delete_check_htid(TM_IndexDeleteOp *delstate, Page page, OffsetNumber maxoff, ItemPointer htid, TM_IndexStatus *istatus)
Definition: heapam.c:8014
bool heap_hot_search_buffer(ItemPointer tid, Relation relation, Buffer buffer, Snapshot snapshot, HeapTuple heapTuple, bool *all_dead, bool first_call)
Definition: heapam.c:1693
static void index_delete_sort(TM_IndexDeleteOp *delstate)
Definition: heapam.c:8419
static bool HeapTupleHeaderIsHotUpdated(const HeapTupleHeaderData *tup)
Definition: htup_details.h:534
#define InvalidOffsetNumber
Definition: off.h:26
#define FirstOffsetNumber
Definition: off.h:27
static char * buf
Definition: pg_test_fsync.c:72
#define InitNonVacuumableSnapshot(snapshotdata, vistestp)
Definition: snapmgr.h:50
int get_tablespace_maintenance_io_concurrency(Oid spcid)
Definition: spccache.c:229
int bottomupfreespace
Definition: tableam.h:243
bool knowndeletable
Definition: tableam.h:213
int16 freespace
Definition: tableam.h:217

References Assert(), TM_IndexDeleteOp::bottomup, BOTTOMUP_MAX_NBLOCKS, bottomup_sort_and_shrink(), TM_IndexDeleteOp::bottomupfreespace, buf, BUFFER_LOCK_SHARE, BufferGetPage(), BufferIsValid(), TM_IndexDeleteOp::deltids, FirstOffsetNumber, TM_IndexStatus::freespace, get_tablespace_maintenance_io_concurrency(), GlobalVisTestFor(), heap_hot_search_buffer(), HeapTupleHeaderAdvanceConflictHorizon(), HeapTupleHeaderGetUpdateXid(), HeapTupleHeaderGetXmin(), HeapTupleHeaderIsHotUpdated(), i, TM_IndexDelete::id, index_delete_check_htid(), index_delete_sort(), InitNonVacuumableSnapshot, InvalidBlockNumber, InvalidBuffer, InvalidOffsetNumber, InvalidTransactionId, IsCatalogRelation(), ItemIdGetRedirect, ItemIdIsNormal, ItemIdIsRedirected, ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), TM_IndexStatus::knowndeletable, LockBuffer(), maintenance_io_concurrency, Min, TM_IndexDeleteOp::ndeltids, PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), TM_IndexStatus::promising, RelationData::rd_rel, ReadBuffer(), TM_IndexDeleteOp::status, HeapTupleHeaderData::t_ctid, TM_IndexDelete::tid, TransactionIdEquals, TransactionIdIsValid, and UnlockReleaseBuffer().

◆ heap_inplace_lock()

bool heap_inplace_lock ( Relation  relation,
HeapTuple  oldtup_ptr,
Buffer  buffer,
void(*)(void *)  release_callback,
void *  arg 
)

Definition at line 6318 of file heapam.c.

6321{
6322 HeapTupleData oldtup = *oldtup_ptr; /* minimize diff vs. heap_update() */
6323 TM_Result result;
6324 bool ret;
6325
6326#ifdef USE_ASSERT_CHECKING
6327 if (RelationGetRelid(relation) == RelationRelationId)
6328 check_inplace_rel_lock(oldtup_ptr);
6329#endif
6330
6331 Assert(BufferIsValid(buffer));
6332
6333 /*
6334 * Construct shared cache inval if necessary. Because we pass a tuple
6335 * version without our own inplace changes or inplace changes other
6336 * sessions complete while we wait for locks, inplace update mustn't
6337 * change catcache lookup keys. But we aren't bothering with index
6338 * updates either, so that's true a fortiori. After LockBuffer(), it
6339 * would be too late, because this might reach a
6340 * CatalogCacheInitializeCache() that locks "buffer".
6341 */
6342 CacheInvalidateHeapTupleInplace(relation, oldtup_ptr, NULL);
6343
6344 LockTuple(relation, &oldtup.t_self, InplaceUpdateTupleLock);
6346
6347 /*----------
6348 * Interpret HeapTupleSatisfiesUpdate() like heap_update() does, except:
6349 *
6350 * - wait unconditionally
6351 * - already locked tuple above, since inplace needs that unconditionally
6352 * - don't recheck header after wait: simpler to defer to next iteration
6353 * - don't try to continue even if the updater aborts: likewise
6354 * - no crosscheck
6355 */
6356 result = HeapTupleSatisfiesUpdate(&oldtup, GetCurrentCommandId(false),
6357 buffer);
6358
6359 if (result == TM_Invisible)
6360 {
6361 /* no known way this can happen */
6362 ereport(ERROR,
6363 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
6364 errmsg_internal("attempted to overwrite invisible tuple")));
6365 }
6366 else if (result == TM_SelfModified)
6367 {
6368 /*
6369 * CREATE INDEX might reach this if an expression is silly enough to
6370 * call e.g. SELECT ... FROM pg_class FOR SHARE. C code of other SQL
6371 * statements might get here after a heap_update() of the same row, in
6372 * the absence of an intervening CommandCounterIncrement().
6373 */
6374 ereport(ERROR,
6375 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
6376 errmsg("tuple to be updated was already modified by an operation triggered by the current command")));
6377 }
6378 else if (result == TM_BeingModified)
6379 {
6380 TransactionId xwait;
6381 uint16 infomask;
6382
6383 xwait = HeapTupleHeaderGetRawXmax(oldtup.t_data);
6384 infomask = oldtup.t_data->t_infomask;
6385
6386 if (infomask & HEAP_XMAX_IS_MULTI)
6387 {
6390 int remain;
6391
6392 if (DoesMultiXactIdConflict((MultiXactId) xwait, infomask,
6393 lockmode, NULL))
6394 {
6396 release_callback(arg);
6397 ret = false;
6398 MultiXactIdWait((MultiXactId) xwait, mxact_status, infomask,
6399 relation, &oldtup.t_self, XLTW_Update,
6400 &remain);
6401 }
6402 else
6403 ret = true;
6404 }
6406 ret = true;
6407 else if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask))
6408 ret = true;
6409 else
6410 {
6412 release_callback(arg);
6413 ret = false;
6414 XactLockTableWait(xwait, relation, &oldtup.t_self,
6415 XLTW_Update);
6416 }
6417 }
6418 else
6419 {
6420 ret = (result == TM_Ok);
6421 if (!ret)
6422 {
6424 release_callback(arg);
6425 }
6426 }
6427
6428 /*
6429 * GetCatalogSnapshot() relies on invalidation messages to know when to
6430 * take a new snapshot. COMMIT of xwait is responsible for sending the
6431 * invalidation. We're not acquiring heavyweight locks sufficient to
6432 * block if not yet sent, so we must take a new snapshot to ensure a later
6433 * attempt has a fair chance. While we don't need this if xwait aborted,
6434 * don't bother optimizing that.
6435 */
6436 if (!ret)
6437 {
6438 UnlockTuple(relation, &oldtup.t_self, InplaceUpdateTupleLock);
6441 }
6442 return ret;
6443}
void CacheInvalidateHeapTupleInplace(Relation relation, HeapTuple tuple, HeapTuple newtuple)
Definition: inval.c:1578
void ForgetInplace_Inval(void)
Definition: inval.c:1279
void LockTuple(Relation relation, ItemPointer tid, LOCKMODE lockmode)
Definition: lmgr.c:562
void UnlockTuple(Relation relation, ItemPointer tid, LOCKMODE lockmode)
Definition: lmgr.c:601
@ XLTW_Update
Definition: lmgr.h:27
#define InplaceUpdateTupleLock
Definition: lockdefs.h:48
void * arg
void InvalidateCatalogSnapshot(void)
Definition: snapmgr.c:443
CommandId GetCurrentCommandId(bool used)
Definition: xact.c:829

References arg, Assert(), BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferIsValid(), CacheInvalidateHeapTupleInplace(), DoesMultiXactIdConflict(), ereport, errcode(), errmsg(), errmsg_internal(), ERROR, ForgetInplace_Inval(), GetCurrentCommandId(), HEAP_XMAX_IS_KEYSHR_LOCKED(), HEAP_XMAX_IS_MULTI, HeapTupleHeaderGetRawXmax(), HeapTupleSatisfiesUpdate(), InplaceUpdateTupleLock, InvalidateCatalogSnapshot(), LockBuffer(), LockTuple(), LockTupleNoKeyExclusive, MultiXactIdWait(), MultiXactStatusNoKeyUpdate, RelationGetRelid, HeapTupleData::t_data, HeapTupleHeaderData::t_infomask, HeapTupleData::t_self, TM_BeingModified, TM_Invisible, TM_Ok, TM_SelfModified, TransactionIdIsCurrentTransactionId(), UnlockTuple(), XactLockTableWait(), and XLTW_Update.

Referenced by systable_inplace_update_begin().

◆ heap_inplace_unlock()

void heap_inplace_unlock ( Relation  relation,
HeapTuple  oldtup,
Buffer  buffer 
)

◆ heap_inplace_update_and_unlock()

void heap_inplace_update_and_unlock ( Relation  relation,
HeapTuple  oldtup,
HeapTuple  tuple,
Buffer  buffer 
)

Definition at line 6454 of file heapam.c.

6457{
6458 HeapTupleHeader htup = oldtup->t_data;
6459 uint32 oldlen;
6460 uint32 newlen;
6461 char *dst;
6462 char *src;
6463 int nmsgs = 0;
6464 SharedInvalidationMessage *invalMessages = NULL;
6465 bool RelcacheInitFileInval = false;
6466
6467 Assert(ItemPointerEquals(&oldtup->t_self, &tuple->t_self));
6468 oldlen = oldtup->t_len - htup->t_hoff;
6469 newlen = tuple->t_len - tuple->t_data->t_hoff;
6470 if (oldlen != newlen || htup->t_hoff != tuple->t_data->t_hoff)
6471 elog(ERROR, "wrong tuple length");
6472
6473 dst = (char *) htup + htup->t_hoff;
6474 src = (char *) tuple->t_data + tuple->t_data->t_hoff;
6475
6476 /* Like RecordTransactionCommit(), log only if needed */
6478 nmsgs = inplaceGetInvalidationMessages(&invalMessages,
6479 &RelcacheInitFileInval);
6480
6481 /*
6482 * Unlink relcache init files as needed. If unlinking, acquire
6483 * RelCacheInitLock until after associated invalidations. By doing this
6484 * in advance, if we checkpoint and then crash between inplace
6485 * XLogInsert() and inval, we don't rely on StartupXLOG() ->
6486 * RelationCacheInitFileRemove(). That uses elevel==LOG, so replay would
6487 * neglect to PANIC on EIO.
6488 */
6490
6491 /*----------
6492 * NO EREPORT(ERROR) from here till changes are complete
6493 *
6494 * Our buffer lock won't stop a reader having already pinned and checked
6495 * visibility for this tuple. Hence, we write WAL first, then mutate the
6496 * buffer. Like in MarkBufferDirtyHint() or RecordTransactionCommit(),
6497 * checkpoint delay makes that acceptable. With the usual order of
6498 * changes, a crash after memcpy() and before XLogInsert() could allow
6499 * datfrozenxid to overtake relfrozenxid:
6500 *
6501 * ["D" is a VACUUM (ONLY_DATABASE_STATS)]
6502 * ["R" is a VACUUM tbl]
6503 * D: vac_update_datfrozenxid() -> systable_beginscan(pg_class)
6504 * D: systable_getnext() returns pg_class tuple of tbl
6505 * R: memcpy() into pg_class tuple of tbl
6506 * D: raise pg_database.datfrozenxid, XLogInsert(), finish
6507 * [crash]
6508 * [recovery restores datfrozenxid w/o relfrozenxid]
6509 *
6510 * Like in MarkBufferDirtyHint() subroutine XLogSaveBufferForHint(), copy
6511 * the buffer to the stack before logging. Here, that facilitates a FPI
6512 * of the post-mutation block before we accept other sessions seeing it.
6513 */
6517
6518 /* XLOG stuff */
6519 if (RelationNeedsWAL(relation))
6520 {
6521 xl_heap_inplace xlrec;
6522 PGAlignedBlock copied_buffer;
6523 char *origdata = (char *) BufferGetBlock(buffer);
6524 Page page = BufferGetPage(buffer);
6525 uint16 lower = ((PageHeader) page)->pd_lower;
6526 uint16 upper = ((PageHeader) page)->pd_upper;
6527 uintptr_t dst_offset_in_block;
6528 RelFileLocator rlocator;
6529 ForkNumber forkno;
6530 BlockNumber blkno;
6531 XLogRecPtr recptr;
6532
6533 xlrec.offnum = ItemPointerGetOffsetNumber(&tuple->t_self);
6534 xlrec.dbId = MyDatabaseId;
6535 xlrec.tsId = MyDatabaseTableSpace;
6536 xlrec.relcacheInitFileInval = RelcacheInitFileInval;
6537 xlrec.nmsgs = nmsgs;
6538
6541 if (nmsgs != 0)
6542 XLogRegisterData(invalMessages,
6543 nmsgs * sizeof(SharedInvalidationMessage));
6544
6545 /* register block matching what buffer will look like after changes */
6546 memcpy(copied_buffer.data, origdata, lower);
6547 memcpy(copied_buffer.data + upper, origdata + upper, BLCKSZ - upper);
6548 dst_offset_in_block = dst - origdata;
6549 memcpy(copied_buffer.data + dst_offset_in_block, src, newlen);
6550 BufferGetTag(buffer, &rlocator, &forkno, &blkno);
6551 Assert(forkno == MAIN_FORKNUM);
6552 XLogRegisterBlock(0, &rlocator, forkno, blkno, copied_buffer.data,
6554 XLogRegisterBufData(0, src, newlen);
6555
6556 /* inplace updates aren't decoded atm, don't log the origin */
6557
6558 recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_INPLACE);
6559
6560 PageSetLSN(page, recptr);
6561 }
6562
6563 memcpy(dst, src, newlen);
6564
6565 MarkBufferDirty(buffer);
6566
6568
6569 /*
6570 * Send invalidations to shared queue. SearchSysCacheLocked1() assumes we
6571 * do this before UnlockTuple().
6572 *
6573 * If we're mutating a tuple visible only to this transaction, there's an
6574 * equivalent transactional inval from the action that created the tuple,
6575 * and this inval is superfluous.
6576 */
6578
6579 MyProc->delayChkptFlags &= ~DELAY_CHKPT_START;
6581 UnlockTuple(relation, &tuple->t_self, InplaceUpdateTupleLock);
6582
6583 AcceptInvalidationMessages(); /* local processing of just-sent inval */
6584
6585 /*
6586 * Queue a transactional inval. The immediate invalidation we just sent
6587 * is the only one known to be necessary. To reduce risk from the
6588 * transition to immediate invalidation, continue sending a transactional
6589 * invalidation like we've long done. Third-party code might rely on it.
6590 */
6592 CacheInvalidateHeapTuple(relation, tuple, NULL);
6593}
void BufferGetTag(Buffer buffer, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum)
Definition: bufmgr.c:4182
static Block BufferGetBlock(Buffer buffer)
Definition: bufmgr.h:381
PageHeaderData * PageHeader
Definition: bufpage.h:174
Oid MyDatabaseTableSpace
Definition: globals.c:97
Oid MyDatabaseId
Definition: globals.c:95
#define MinSizeOfHeapInplace
Definition: heapam_xlog.h:436
#define XLOG_HEAP_INPLACE
Definition: heapam_xlog.h:40
void AcceptInvalidationMessages(void)
Definition: inval.c:929
int inplaceGetInvalidationMessages(SharedInvalidationMessage **msgs, bool *RelcacheInitFileInval)
Definition: inval.c:1081
void PreInplace_Inval(void)
Definition: inval.c:1243
void AtInplace_Inval(void)
Definition: inval.c:1256
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:477
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:49
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:80
#define DELAY_CHKPT_START
Definition: proc.h:120
ForkNumber
Definition: relpath.h:56
PGPROC * MyProc
Definition: proc.c:67
int delayChkptFlags
Definition: proc.h:241
OffsetNumber offnum
Definition: heapam_xlog.h:428
bool relcacheInitFileInval
Definition: heapam_xlog.h:431
char data[BLCKSZ]
Definition: c.h:1090
#define XLogStandbyInfoActive()
Definition: xlog.h:123
void XLogRegisterBufData(uint8 block_id, const void *data, uint32 len)
Definition: xloginsert.c:405
void XLogRegisterBlock(uint8 block_id, RelFileLocator *rlocator, ForkNumber forknum, BlockNumber blknum, const PageData *page, uint8 flags)
Definition: xloginsert.c:309

References AcceptInvalidationMessages(), Assert(), AtInplace_Inval(), BUFFER_LOCK_UNLOCK, BufferGetBlock(), BufferGetPage(), BufferGetTag(), CacheInvalidateHeapTuple(), PGAlignedBlock::data, xl_heap_inplace::dbId, DELAY_CHKPT_START, PGPROC::delayChkptFlags, elog, END_CRIT_SECTION, ERROR, inplaceGetInvalidationMessages(), InplaceUpdateTupleLock, IsBootstrapProcessingMode, ItemPointerEquals(), ItemPointerGetOffsetNumber(), LockBuffer(), lower(), MAIN_FORKNUM, MarkBufferDirty(), MinSizeOfHeapInplace, MyDatabaseId, MyDatabaseTableSpace, MyProc, xl_heap_inplace::nmsgs, xl_heap_inplace::offnum, PageSetLSN(), PreInplace_Inval(), REGBUF_STANDARD, RelationNeedsWAL, xl_heap_inplace::relcacheInitFileInval, START_CRIT_SECTION, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, HeapTupleData::t_len, HeapTupleData::t_self, xl_heap_inplace::tsId, UnlockTuple(), upper(), XLOG_HEAP_INPLACE, XLogBeginInsert(), XLogInsert(), XLogRegisterBlock(), XLogRegisterBufData(), XLogRegisterData(), and XLogStandbyInfoActive.

Referenced by systable_inplace_update_finish().

◆ heap_insert()

void heap_insert ( Relation  relation,
HeapTuple  tup,
CommandId  cid,
int  options,
BulkInsertState  bistate 
)

Definition at line 2056 of file heapam.c.

2058{
2060 HeapTuple heaptup;
2061 Buffer buffer;
2062 Buffer vmbuffer = InvalidBuffer;
2063 bool all_visible_cleared = false;
2064
2065 /* Cheap, simplistic check that the tuple matches the rel's rowtype. */
2068
2069 /*
2070 * Fill in tuple header fields and toast the tuple if necessary.
2071 *
2072 * Note: below this point, heaptup is the data we actually intend to store
2073 * into the relation; tup is the caller's original untoasted data.
2074 */
2075 heaptup = heap_prepare_insert(relation, tup, xid, cid, options);
2076
2077 /*
2078 * Find buffer to insert this tuple into. If the page is all visible,
2079 * this will also pin the requisite visibility map page.
2080 */
2081 buffer = RelationGetBufferForTuple(relation, heaptup->t_len,
2082 InvalidBuffer, options, bistate,
2083 &vmbuffer, NULL,
2084 0);
2085
2086 /*
2087 * We're about to do the actual insert -- but check for conflict first, to
2088 * avoid possibly having to roll back work we've just done.
2089 *
2090 * This is safe without a recheck as long as there is no possibility of
2091 * another process scanning the page between this check and the insert
2092 * being visible to the scan (i.e., an exclusive buffer content lock is
2093 * continuously held from this point until the tuple insert is visible).
2094 *
2095 * For a heap insert, we only need to check for table-level SSI locks. Our
2096 * new tuple can't possibly conflict with existing tuple locks, and heap
2097 * page locks are only consolidated versions of tuple locks; they do not
2098 * lock "gaps" as index page locks do. So we don't need to specify a
2099 * buffer when making the call, which makes for a faster check.
2100 */
2102
2103 /* NO EREPORT(ERROR) from here till changes are logged */
2105
2106 RelationPutHeapTuple(relation, buffer, heaptup,
2108
2109 if (PageIsAllVisible(BufferGetPage(buffer)))
2110 {
2111 all_visible_cleared = true;
2113 visibilitymap_clear(relation,
2114 ItemPointerGetBlockNumber(&(heaptup->t_self)),
2115 vmbuffer, VISIBILITYMAP_VALID_BITS);
2116 }
2117
2118 /*
2119 * XXX Should we set PageSetPrunable on this page ?
2120 *
2121 * The inserting transaction may eventually abort thus making this tuple
2122 * DEAD and hence available for pruning. Though we don't want to optimize
2123 * for aborts, if no other tuple in this page is UPDATEd/DELETEd, the
2124 * aborted tuple will never be pruned until next vacuum is triggered.
2125 *
2126 * If you do add PageSetPrunable here, add it in heap_xlog_insert too.
2127 */
2128
2129 MarkBufferDirty(buffer);
2130
2131 /* XLOG stuff */
2132 if (RelationNeedsWAL(relation))
2133 {
2134 xl_heap_insert xlrec;
2135 xl_heap_header xlhdr;
2136 XLogRecPtr recptr;
2137 Page page = BufferGetPage(buffer);
2138 uint8 info = XLOG_HEAP_INSERT;
2139 int bufflags = 0;
2140
2141 /*
2142 * If this is a catalog, we need to transmit combo CIDs to properly
2143 * decode, so log that as well.
2144 */
2146 log_heap_new_cid(relation, heaptup);
2147
2148 /*
2149 * If this is the single and first tuple on page, we can reinit the
2150 * page instead of restoring the whole thing. Set flag, and hide
2151 * buffer references from XLogInsert.
2152 */
2155 {
2156 info |= XLOG_HEAP_INIT_PAGE;
2157 bufflags |= REGBUF_WILL_INIT;
2158 }
2159
2160 xlrec.offnum = ItemPointerGetOffsetNumber(&heaptup->t_self);
2161 xlrec.flags = 0;
2162 if (all_visible_cleared)
2167
2168 /*
2169 * For logical decoding, we need the tuple even if we're doing a full
2170 * page write, so make sure it's included even if we take a full-page
2171 * image. (XXX We could alternatively store a pointer into the FPW).
2172 */
2173 if (RelationIsLogicallyLogged(relation) &&
2175 {
2177 bufflags |= REGBUF_KEEP_DATA;
2178
2179 if (IsToastRelation(relation))
2181 }
2182
2185
2186 xlhdr.t_infomask2 = heaptup->t_data->t_infomask2;
2187 xlhdr.t_infomask = heaptup->t_data->t_infomask;
2188 xlhdr.t_hoff = heaptup->t_data->t_hoff;
2189
2190 /*
2191 * note we mark xlhdr as belonging to buffer; if XLogInsert decides to
2192 * write the whole page to the xlog, we don't need to store
2193 * xl_heap_header in the xlog.
2194 */
2195 XLogRegisterBuffer(0, buffer, REGBUF_STANDARD | bufflags);
2197 /* PG73FORMAT: write bitmap [+ padding] [+ oid] + data */
2199 (char *) heaptup->t_data + SizeofHeapTupleHeader,
2200 heaptup->t_len - SizeofHeapTupleHeader);
2201
2202 /* filtering by origin on a row level is much more efficient */
2204
2205 recptr = XLogInsert(RM_HEAP_ID, info);
2206
2207 PageSetLSN(page, recptr);
2208 }
2209
2211
2212 UnlockReleaseBuffer(buffer);
2213 if (vmbuffer != InvalidBuffer)
2214 ReleaseBuffer(vmbuffer);
2215
2216 /*
2217 * If tuple is cachable, mark it for invalidation from the caches in case
2218 * we abort. Note it is OK to do this after releasing the buffer, because
2219 * the heaptup data structure is all in local memory, not in the shared
2220 * buffer.
2221 */
2222 CacheInvalidateHeapTuple(relation, heaptup, NULL);
2223
2224 /* Note: speculative insertions are counted too, even if aborted later */
2225 pgstat_count_heap_insert(relation, 1);
2226
2227 /*
2228 * If heaptup is a private copy, release it. Don't forget to copy t_self
2229 * back to the caller's image, too.
2230 */
2231 if (heaptup != tup)
2232 {
2233 tup->t_self = heaptup->t_self;
2234 heap_freetuple(heaptup);
2235 }
2236}
uint8_t uint8
Definition: c.h:500
static HeapTuple heap_prepare_insert(Relation relation, HeapTuple tup, TransactionId xid, CommandId cid, int options)
Definition: heapam.c:2245
#define HEAP_INSERT_SPECULATIVE
Definition: heapam.h:39
#define HEAP_INSERT_NO_LOGICAL
Definition: heapam.h:38
#define XLH_INSERT_ON_TOAST_RELATION
Definition: heapam_xlog.h:76
#define XLH_INSERT_IS_SPECULATIVE
Definition: heapam_xlog.h:74
#define XLH_INSERT_ALL_VISIBLE_CLEARED
Definition: heapam_xlog.h:72
#define XLOG_HEAP_INSERT
Definition: heapam_xlog.h:33
#define SizeOfHeapInsert
Definition: heapam_xlog.h:168
#define XLH_INSERT_CONTAINS_NEW_TUPLE
Definition: heapam_xlog.h:75
#define XLOG_HEAP_INIT_PAGE
Definition: heapam_xlog.h:47
void RelationPutHeapTuple(Relation relation, Buffer buffer, HeapTuple tuple, bool token)
Definition: hio.c:35
Buffer RelationGetBufferForTuple(Relation relation, Size len, Buffer otherBuffer, int options, BulkInsertState bistate, Buffer *vmbuffer, Buffer *vmbuffer_other, int num_pages)
Definition: hio.c:502
#define HeapTupleHeaderGetNatts(tup)
Definition: htup_details.h:577
void pgstat_count_heap_insert(Relation rel, PgStat_Counter n)
#define RelationGetNumberOfAttributes(relation)
Definition: rel.h:522
OffsetNumber offnum
Definition: heapam_xlog.h:162
#define REGBUF_KEEP_DATA
Definition: xloginsert.h:36
#define REGBUF_WILL_INIT
Definition: xloginsert.h:34

References Assert(), BufferGetBlockNumber(), BufferGetPage(), CacheInvalidateHeapTuple(), CheckForSerializableConflictIn(), END_CRIT_SECTION, FirstOffsetNumber, xl_heap_insert::flags, GetCurrentTransactionId(), heap_freetuple(), HEAP_INSERT_NO_LOGICAL, HEAP_INSERT_SPECULATIVE, heap_prepare_insert(), HeapTupleHeaderGetNatts, InvalidBlockNumber, InvalidBuffer, IsToastRelation(), ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), log_heap_new_cid(), MarkBufferDirty(), xl_heap_insert::offnum, PageClearAllVisible(), PageGetMaxOffsetNumber(), PageIsAllVisible(), PageSetLSN(), pgstat_count_heap_insert(), REGBUF_KEEP_DATA, REGBUF_STANDARD, REGBUF_WILL_INIT, RelationGetBufferForTuple(), RelationGetNumberOfAttributes, RelationIsAccessibleInLogicalDecoding, RelationIsLogicallyLogged, RelationNeedsWAL, RelationPutHeapTuple(), ReleaseBuffer(), SizeOfHeapHeader, SizeOfHeapInsert, SizeofHeapTupleHeader, START_CRIT_SECTION, HeapTupleData::t_data, xl_heap_header::t_hoff, HeapTupleHeaderData::t_hoff, xl_heap_header::t_infomask, HeapTupleHeaderData::t_infomask, xl_heap_header::t_infomask2, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_self, UnlockReleaseBuffer(), visibilitymap_clear(), VISIBILITYMAP_VALID_BITS, XLH_INSERT_ALL_VISIBLE_CLEARED, XLH_INSERT_CONTAINS_NEW_TUPLE, XLH_INSERT_IS_SPECULATIVE, XLH_INSERT_ON_TOAST_RELATION, XLOG_HEAP_INIT_PAGE, XLOG_HEAP_INSERT, XLOG_INCLUDE_ORIGIN, XLogBeginInsert(), XLogInsert(), XLogRegisterBufData(), XLogRegisterBuffer(), XLogRegisterData(), and XLogSetRecordFlags().

Referenced by heapam_tuple_insert(), heapam_tuple_insert_speculative(), simple_heap_insert(), and toast_save_datum().

◆ heap_lock_tuple()

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 at line 4543 of file heapam.c.

4547{
4548 TM_Result result;
4549 ItemPointer tid = &(tuple->t_self);
4550 ItemId lp;
4551 Page page;
4552 Buffer vmbuffer = InvalidBuffer;
4553 BlockNumber block;
4554 TransactionId xid,
4555 xmax;
4556 uint16 old_infomask,
4557 new_infomask,
4558 new_infomask2;
4559 bool first_time = true;
4560 bool skip_tuple_lock = false;
4561 bool have_tuple_lock = false;
4562 bool cleared_all_frozen = false;
4563
4564 *buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
4565 block = ItemPointerGetBlockNumber(tid);
4566
4567 /*
4568 * Before locking the buffer, pin the visibility map page if it appears to
4569 * be necessary. Since we haven't got the lock yet, someone else might be
4570 * in the middle of changing this, so we'll need to recheck after we have
4571 * the lock.
4572 */
4573 if (PageIsAllVisible(BufferGetPage(*buffer)))
4574 visibilitymap_pin(relation, block, &vmbuffer);
4575
4577
4578 page = BufferGetPage(*buffer);
4581
4582 tuple->t_data = (HeapTupleHeader) PageGetItem(page, lp);
4583 tuple->t_len = ItemIdGetLength(lp);
4584 tuple->t_tableOid = RelationGetRelid(relation);
4585
4586l3:
4587 result = HeapTupleSatisfiesUpdate(tuple, cid, *buffer);
4588
4589 if (result == TM_Invisible)
4590 {
4591 /*
4592 * This is possible, but only when locking a tuple for ON CONFLICT
4593 * UPDATE. We return this value here rather than throwing an error in
4594 * order to give that case the opportunity to throw a more specific
4595 * error.
4596 */
4597 result = TM_Invisible;
4598 goto out_locked;
4599 }
4600 else if (result == TM_BeingModified ||
4601 result == TM_Updated ||
4602 result == TM_Deleted)
4603 {
4604 TransactionId xwait;
4605 uint16 infomask;
4606 uint16 infomask2;
4607 bool require_sleep;
4608 ItemPointerData t_ctid;
4609
4610 /* must copy state data before unlocking buffer */
4611 xwait = HeapTupleHeaderGetRawXmax(tuple->t_data);
4612 infomask = tuple->t_data->t_infomask;
4613 infomask2 = tuple->t_data->t_infomask2;
4614 ItemPointerCopy(&tuple->t_data->t_ctid, &t_ctid);
4615
4617
4618 /*
4619 * If any subtransaction of the current top transaction already holds
4620 * a lock as strong as or stronger than what we're requesting, we
4621 * effectively hold the desired lock already. We *must* succeed
4622 * without trying to take the tuple lock, else we will deadlock
4623 * against anyone wanting to acquire a stronger lock.
4624 *
4625 * Note we only do this the first time we loop on the HTSU result;
4626 * there is no point in testing in subsequent passes, because
4627 * evidently our own transaction cannot have acquired a new lock after
4628 * the first time we checked.
4629 */
4630 if (first_time)
4631 {
4632 first_time = false;
4633
4634 if (infomask & HEAP_XMAX_IS_MULTI)
4635 {
4636 int i;
4637 int nmembers;
4638 MultiXactMember *members;
4639
4640 /*
4641 * We don't need to allow old multixacts here; if that had
4642 * been the case, HeapTupleSatisfiesUpdate would have returned
4643 * MayBeUpdated and we wouldn't be here.
4644 */
4645 nmembers =
4646 GetMultiXactIdMembers(xwait, &members, false,
4647 HEAP_XMAX_IS_LOCKED_ONLY(infomask));
4648
4649 for (i = 0; i < nmembers; i++)
4650 {
4651 /* only consider members of our own transaction */
4652 if (!TransactionIdIsCurrentTransactionId(members[i].xid))
4653 continue;
4654
4655 if (TUPLOCK_from_mxstatus(members[i].status) >= mode)
4656 {
4657 pfree(members);
4658 result = TM_Ok;
4659 goto out_unlocked;
4660 }
4661 else
4662 {
4663 /*
4664 * Disable acquisition of the heavyweight tuple lock.
4665 * Otherwise, when promoting a weaker lock, we might
4666 * deadlock with another locker that has acquired the
4667 * heavyweight tuple lock and is waiting for our
4668 * transaction to finish.
4669 *
4670 * Note that in this case we still need to wait for
4671 * the multixact if required, to avoid acquiring
4672 * conflicting locks.
4673 */
4674 skip_tuple_lock = true;
4675 }
4676 }
4677
4678 if (members)
4679 pfree(members);
4680 }
4682 {
4683 switch (mode)
4684 {
4685 case LockTupleKeyShare:
4687 HEAP_XMAX_IS_SHR_LOCKED(infomask) ||
4688 HEAP_XMAX_IS_EXCL_LOCKED(infomask));
4689 result = TM_Ok;
4690 goto out_unlocked;
4691 case LockTupleShare:
4692 if (HEAP_XMAX_IS_SHR_LOCKED(infomask) ||
4693 HEAP_XMAX_IS_EXCL_LOCKED(infomask))
4694 {
4695 result = TM_Ok;
4696 goto out_unlocked;
4697 }
4698 break;
4700 if (HEAP_XMAX_IS_EXCL_LOCKED(infomask))
4701 {
4702 result = TM_Ok;
4703 goto out_unlocked;
4704 }
4705 break;
4706 case LockTupleExclusive:
4707 if (HEAP_XMAX_IS_EXCL_LOCKED(infomask) &&
4708 infomask2 & HEAP_KEYS_UPDATED)
4709 {
4710 result = TM_Ok;
4711 goto out_unlocked;
4712 }
4713 break;
4714 }
4715 }
4716 }
4717
4718 /*
4719 * Initially assume that we will have to wait for the locking
4720 * transaction(s) to finish. We check various cases below in which
4721 * this can be turned off.
4722 */
4723 require_sleep = true;
4724 if (mode == LockTupleKeyShare)
4725 {
4726 /*
4727 * If we're requesting KeyShare, and there's no update present, we
4728 * don't need to wait. Even if there is an update, we can still
4729 * continue if the key hasn't been modified.
4730 *
4731 * However, if there are updates, we need to walk the update chain
4732 * to mark future versions of the row as locked, too. That way,
4733 * if somebody deletes that future version, we're protected
4734 * against the key going away. This locking of future versions
4735 * could block momentarily, if a concurrent transaction is
4736 * deleting a key; or it could return a value to the effect that
4737 * the transaction deleting the key has already committed. So we
4738 * do this before re-locking the buffer; otherwise this would be
4739 * prone to deadlocks.
4740 *
4741 * Note that the TID we're locking was grabbed before we unlocked
4742 * the buffer. For it to change while we're not looking, the
4743 * other properties we're testing for below after re-locking the
4744 * buffer would also change, in which case we would restart this
4745 * loop above.
4746 */
4747 if (!(infomask2 & HEAP_KEYS_UPDATED))
4748 {
4749 bool updated;
4750
4751 updated = !HEAP_XMAX_IS_LOCKED_ONLY(infomask);
4752
4753 /*
4754 * If there are updates, follow the update chain; bail out if
4755 * that cannot be done.
4756 */
4757 if (follow_updates && updated)
4758 {
4759 TM_Result res;
4760
4761 res = heap_lock_updated_tuple(relation, tuple, &t_ctid,
4763 mode);
4764 if (res != TM_Ok)
4765 {
4766 result = res;
4767 /* recovery code expects to have buffer lock held */
4769 goto failed;
4770 }
4771 }
4772
4774
4775 /*
4776 * Make sure it's still an appropriate lock, else start over.
4777 * Also, if it wasn't updated before we released the lock, but
4778 * is updated now, we start over too; the reason is that we
4779 * now need to follow the update chain to lock the new
4780 * versions.
4781 */
4782 if (!HeapTupleHeaderIsOnlyLocked(tuple->t_data) &&
4783 ((tuple->t_data->t_infomask2 & HEAP_KEYS_UPDATED) ||
4784 !updated))
4785 goto l3;
4786
4787 /* Things look okay, so we can skip sleeping */
4788 require_sleep = false;
4789
4790 /*
4791 * Note we allow Xmax to change here; other updaters/lockers
4792 * could have modified it before we grabbed the buffer lock.
4793 * However, this is not a problem, because with the recheck we
4794 * just did we ensure that they still don't conflict with the
4795 * lock we want.
4796 */
4797 }
4798 }
4799 else if (mode == LockTupleShare)
4800 {
4801 /*
4802 * If we're requesting Share, we can similarly avoid sleeping if
4803 * there's no update and no exclusive lock present.
4804 */
4805 if (HEAP_XMAX_IS_LOCKED_ONLY(infomask) &&
4806 !HEAP_XMAX_IS_EXCL_LOCKED(infomask))
4807 {
4809
4810 /*
4811 * Make sure it's still an appropriate lock, else start over.
4812 * See above about allowing xmax to change.
4813 */
4816 goto l3;
4817 require_sleep = false;
4818 }
4819 }
4820 else if (mode == LockTupleNoKeyExclusive)
4821 {
4822 /*
4823 * If we're requesting NoKeyExclusive, we might also be able to
4824 * avoid sleeping; just ensure that there no conflicting lock
4825 * already acquired.
4826 */
4827 if (infomask & HEAP_XMAX_IS_MULTI)
4828 {
4829 if (!DoesMultiXactIdConflict((MultiXactId) xwait, infomask,
4830 mode, NULL))
4831 {
4832 /*
4833 * No conflict, but if the xmax changed under us in the
4834 * meantime, start over.
4835 */
4837 if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) ||
4839 xwait))
4840 goto l3;
4841
4842 /* otherwise, we're good */
4843 require_sleep = false;
4844 }
4845 }
4846 else if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask))
4847 {
4849
4850 /* if the xmax changed in the meantime, start over */
4851 if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) ||
4853 xwait))
4854 goto l3;
4855 /* otherwise, we're good */
4856 require_sleep = false;
4857 }
4858 }
4859
4860 /*
4861 * As a check independent from those above, we can also avoid sleeping
4862 * if the current transaction is the sole locker of the tuple. Note
4863 * that the strength of the lock already held is irrelevant; this is
4864 * not about recording the lock in Xmax (which will be done regardless
4865 * of this optimization, below). Also, note that the cases where we
4866 * hold a lock stronger than we are requesting are already handled
4867 * above by not doing anything.
4868 *
4869 * Note we only deal with the non-multixact case here; MultiXactIdWait
4870 * is well equipped to deal with this situation on its own.
4871 */
4872 if (require_sleep && !(infomask & HEAP_XMAX_IS_MULTI) &&
4874 {
4875 /* ... but if the xmax changed in the meantime, start over */
4877 if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) ||
4879 xwait))
4880 goto l3;
4882 require_sleep = false;
4883 }
4884
4885 /*
4886 * Time to sleep on the other transaction/multixact, if necessary.
4887 *
4888 * If the other transaction is an update/delete that's already
4889 * committed, then sleeping cannot possibly do any good: if we're
4890 * required to sleep, get out to raise an error instead.
4891 *
4892 * By here, we either have already acquired the buffer exclusive lock,
4893 * or we must wait for the locking transaction or multixact; so below
4894 * we ensure that we grab buffer lock after the sleep.
4895 */
4896 if (require_sleep && (result == TM_Updated || result == TM_Deleted))
4897 {
4899 goto failed;
4900 }
4901 else if (require_sleep)
4902 {
4903 /*
4904 * Acquire tuple lock to establish our priority for the tuple, or
4905 * die trying. LockTuple will release us when we are next-in-line
4906 * for the tuple. We must do this even if we are share-locking,
4907 * but not if we already have a weaker lock on the tuple.
4908 *
4909 * If we are forced to "start over" below, we keep the tuple lock;
4910 * this arranges that we stay at the head of the line while
4911 * rechecking tuple state.
4912 */
4913 if (!skip_tuple_lock &&
4914 !heap_acquire_tuplock(relation, tid, mode, wait_policy,
4915 &have_tuple_lock))
4916 {
4917 /*
4918 * This can only happen if wait_policy is Skip and the lock
4919 * couldn't be obtained.
4920 */
4921 result = TM_WouldBlock;
4922 /* recovery code expects to have buffer lock held */
4924 goto failed;
4925 }
4926
4927 if (infomask & HEAP_XMAX_IS_MULTI)
4928 {
4930
4931 /* We only ever lock tuples, never update them */
4932 if (status >= MultiXactStatusNoKeyUpdate)
4933 elog(ERROR, "invalid lock mode in heap_lock_tuple");
4934
4935 /* wait for multixact to end, or die trying */
4936 switch (wait_policy)
4937 {
4938 case LockWaitBlock:
4939 MultiXactIdWait((MultiXactId) xwait, status, infomask,
4940 relation, &tuple->t_self, XLTW_Lock, NULL);
4941 break;
4942 case LockWaitSkip:
4944 status, infomask, relation,
4945 NULL, false))
4946 {
4947 result = TM_WouldBlock;
4948 /* recovery code expects to have buffer lock held */
4950 goto failed;
4951 }
4952 break;
4953 case LockWaitError:
4955 status, infomask, relation,
4956 NULL, log_lock_failure))
4957 ereport(ERROR,
4958 (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
4959 errmsg("could not obtain lock on row in relation \"%s\"",
4960 RelationGetRelationName(relation))));
4961
4962 break;
4963 }
4964
4965 /*
4966 * Of course, the multixact might not be done here: if we're
4967 * requesting a light lock mode, other transactions with light
4968 * locks could still be alive, as well as locks owned by our
4969 * own xact or other subxacts of this backend. We need to
4970 * preserve the surviving MultiXact members. Note that it
4971 * isn't absolutely necessary in the latter case, but doing so
4972 * is simpler.
4973 */
4974 }
4975 else
4976 {
4977 /* wait for regular transaction to end, or die trying */
4978 switch (wait_policy)
4979 {
4980 case LockWaitBlock:
4981 XactLockTableWait(xwait, relation, &tuple->t_self,
4982 XLTW_Lock);
4983 break;
4984 case LockWaitSkip:
4985 if (!ConditionalXactLockTableWait(xwait, false))
4986 {
4987 result = TM_WouldBlock;
4988 /* recovery code expects to have buffer lock held */
4990 goto failed;
4991 }
4992 break;
4993 case LockWaitError:
4995 ereport(ERROR,
4996 (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
4997 errmsg("could not obtain lock on row in relation \"%s\"",
4998 RelationGetRelationName(relation))));
4999 break;
5000 }
5001 }
5002
5003 /* if there are updates, follow the update chain */
5004 if (follow_updates && !HEAP_XMAX_IS_LOCKED_ONLY(infomask))
5005 {
5006 TM_Result res;
5007
5008 res = heap_lock_updated_tuple(relation, tuple, &t_ctid,
5010 mode);
5011 if (res != TM_Ok)
5012 {
5013 result = res;
5014 /* recovery code expects to have buffer lock held */
5016 goto failed;
5017 }
5018 }
5019
5021
5022 /*
5023 * xwait is done, but if xwait had just locked the tuple then some
5024 * other xact could update this tuple before we get to this point.
5025 * Check for xmax change, and start over if so.
5026 */
5027 if (xmax_infomask_changed(tuple->t_data->t_infomask, infomask) ||
5029 xwait))
5030 goto l3;
5031
5032 if (!(infomask & HEAP_XMAX_IS_MULTI))
5033 {
5034 /*
5035 * Otherwise check if it committed or aborted. Note we cannot
5036 * be here if the tuple was only locked by somebody who didn't
5037 * conflict with us; that would have been handled above. So
5038 * that transaction must necessarily be gone by now. But
5039 * don't check for this in the multixact case, because some
5040 * locker transactions might still be running.
5041 */
5042 UpdateXmaxHintBits(tuple->t_data, *buffer, xwait);
5043 }
5044 }
5045
5046 /* By here, we're certain that we hold buffer exclusive lock again */
5047
5048 /*
5049 * We may lock if previous xmax aborted, or if it committed but only
5050 * locked the tuple without updating it; or if we didn't have to wait
5051 * at all for whatever reason.
5052 */
5053 if (!require_sleep ||
5054 (tuple->t_data->t_infomask & HEAP_XMAX_INVALID) ||
5057 result = TM_Ok;
5058 else if (!ItemPointerEquals(&tuple->t_self, &tuple->t_data->t_ctid))
5059 result = TM_Updated;
5060 else
5061 result = TM_Deleted;
5062 }
5063
5064failed:
5065 if (result != TM_Ok)
5066 {
5067 Assert(result == TM_SelfModified || result == TM_Updated ||
5068 result == TM_Deleted || result == TM_WouldBlock);
5069
5070 /*
5071 * When locking a tuple under LockWaitSkip semantics and we fail with
5072 * TM_WouldBlock above, it's possible for concurrent transactions to
5073 * release the lock and set HEAP_XMAX_INVALID in the meantime. So
5074 * this assert is slightly different from the equivalent one in
5075 * heap_delete and heap_update.
5076 */
5077 Assert((result == TM_WouldBlock) ||
5078 !(tuple->t_data->t_infomask & HEAP_XMAX_INVALID));
5079 Assert(result != TM_Updated ||
5080 !ItemPointerEquals(&tuple->t_self, &tuple->t_data->t_ctid));
5081 tmfd->ctid = tuple->t_data->t_ctid;
5082 tmfd->xmax = HeapTupleHeaderGetUpdateXid(tuple->t_data);
5083 if (result == TM_SelfModified)
5084 tmfd->cmax = HeapTupleHeaderGetCmax(tuple->t_data);
5085 else
5086 tmfd->cmax = InvalidCommandId;
5087 goto out_locked;
5088 }
5089
5090 /*
5091 * If we didn't pin the visibility map page and the page has become all
5092 * visible while we were busy locking the buffer, or during some
5093 * subsequent window during which we had it unlocked, we'll have to unlock
5094 * and re-lock, to avoid holding the buffer lock across I/O. That's a bit
5095 * unfortunate, especially since we'll now have to recheck whether the
5096 * tuple has been locked or updated under us, but hopefully it won't
5097 * happen very often.
5098 */
5099 if (vmbuffer == InvalidBuffer && PageIsAllVisible(page))
5100 {
5102 visibilitymap_pin(relation, block, &vmbuffer);
5104 goto l3;
5105 }
5106
5107 xmax = HeapTupleHeaderGetRawXmax(tuple->t_data);
5108 old_infomask = tuple->t_data->t_infomask;
5109
5110 /*
5111 * If this is the first possibly-multixact-able operation in the current
5112 * transaction, set my per-backend OldestMemberMXactId setting. We can be
5113 * certain that the transaction will never become a member of any older
5114 * MultiXactIds than that. (We have to do this even if we end up just
5115 * using our own TransactionId below, since some other backend could
5116 * incorporate our XID into a MultiXact immediately afterwards.)
5117 */
5119
5120 /*
5121 * Compute the new xmax and infomask to store into the tuple. Note we do
5122 * not modify the tuple just yet, because that would leave it in the wrong
5123 * state if multixact.c elogs.
5124 */
5125 compute_new_xmax_infomask(xmax, old_infomask, tuple->t_data->t_infomask2,
5126 GetCurrentTransactionId(), mode, false,
5127 &xid, &new_infomask, &new_infomask2);
5128
5130
5131 /*
5132 * Store transaction information of xact locking the tuple.
5133 *
5134 * Note: Cmax is meaningless in this context, so don't set it; this avoids
5135 * possibly generating a useless combo CID. Moreover, if we're locking a
5136 * previously updated tuple, it's important to preserve the Cmax.
5137 *
5138 * Also reset the HOT UPDATE bit, but only if there's no update; otherwise
5139 * we would break the HOT chain.
5140 */
5141 tuple->t_data->t_infomask &= ~HEAP_XMAX_BITS;
5142 tuple->t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
5143 tuple->t_data->t_infomask |= new_infomask;
5144 tuple->t_data->t_infomask2 |= new_infomask2;
5145 if (HEAP_XMAX_IS_LOCKED_ONLY(new_infomask))
5147 HeapTupleHeaderSetXmax(tuple->t_data, xid);
5148
5149 /*
5150 * Make sure there is no forward chain link in t_ctid. Note that in the
5151 * cases where the tuple has been updated, we must not overwrite t_ctid,
5152 * because it was set by the updater. Moreover, if the tuple has been
5153 * updated, we need to follow the update chain to lock the new versions of
5154 * the tuple as well.
5155 */
5156 if (HEAP_XMAX_IS_LOCKED_ONLY(new_infomask))
5157 tuple->t_data->t_ctid = *tid;
5158
5159 /* Clear only the all-frozen bit on visibility map if needed */
5160 if (PageIsAllVisible(page) &&
5161 visibilitymap_clear(relation, block, vmbuffer,
5163 cleared_all_frozen = true;
5164
5165
5166 MarkBufferDirty(*buffer);
5167
5168 /*
5169 * XLOG stuff. You might think that we don't need an XLOG record because
5170 * there is no state change worth restoring after a crash. You would be
5171 * wrong however: we have just written either a TransactionId or a
5172 * MultiXactId that may never have been seen on disk before, and we need
5173 * to make sure that there are XLOG entries covering those ID numbers.
5174 * Else the same IDs might be re-used after a crash, which would be
5175 * disastrous if this page made it to disk before the crash. Essentially
5176 * we have to enforce the WAL log-before-data rule even in this case.
5177 * (Also, in a PITR log-shipping or 2PC environment, we have to have XLOG
5178 * entries for everything anyway.)
5179 */
5180 if (RelationNeedsWAL(relation))
5181 {
5182 xl_heap_lock xlrec;
5183 XLogRecPtr recptr;
5184
5187
5188 xlrec.offnum = ItemPointerGetOffsetNumber(&tuple->t_self);
5189 xlrec.xmax = xid;
5190 xlrec.infobits_set = compute_infobits(new_infomask,
5191 tuple->t_data->t_infomask2);
5192 xlrec.flags = cleared_all_frozen ? XLH_LOCK_ALL_FROZEN_CLEARED : 0;
5194
5195 /* we don't decode row locks atm, so no need to log the origin */
5196
5197 recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_LOCK);
5198
5199 PageSetLSN(page, recptr);
5200 }
5201
5203
5204 result = TM_Ok;
5205
5206out_locked:
5208
5209out_unlocked:
5210 if (BufferIsValid(vmbuffer))
5211 ReleaseBuffer(vmbuffer);
5212
5213 /*
5214 * Don't update the visibility map here. Locking a tuple doesn't change
5215 * visibility info.
5216 */
5217
5218 /*
5219 * Now that we have successfully marked the tuple as locked, we can
5220 * release the lmgr tuple lock, if we had it.
5221 */
5222 if (have_tuple_lock)
5223 UnlockTupleTuplock(relation, tid, mode);
5224
5225 return result;
5226}
static bool ConditionalMultiXactIdWait(MultiXactId multi, MultiXactStatus status, uint16 infomask, Relation rel, int *remaining, bool logLockFailure)
Definition: heapam.c:7751
static TM_Result heap_lock_updated_tuple(Relation rel, HeapTuple tuple, ItemPointer ctid, TransactionId xid, LockTupleMode mode)
Definition: heapam.c:6007
#define XLH_LOCK_ALL_FROZEN_CLEARED
Definition: heapam_xlog.h:393
#define XLOG_HEAP_LOCK
Definition: heapam_xlog.h:39
#define SizeOfHeapLock
Definition: heapam_xlog.h:404
static void ItemPointerCopy(const ItemPointerData *fromPointer, ItemPointerData *toPointer)
Definition: itemptr.h:172
@ XLTW_Lock
Definition: lmgr.h:29
uint8 infobits_set
Definition: heapam_xlog.h:400
OffsetNumber offnum
Definition: heapam_xlog.h:399
TransactionId xmax
Definition: heapam_xlog.h:398
@ TM_WouldBlock
Definition: tableam.h:102
#define VISIBILITYMAP_ALL_FROZEN

References Assert(), BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetPage(), BufferIsValid(), TM_FailureData::cmax, compute_infobits(), compute_new_xmax_infomask(), ConditionalMultiXactIdWait(), ConditionalXactLockTableWait(), TM_FailureData::ctid, DoesMultiXactIdConflict(), elog, END_CRIT_SECTION, ereport, errcode(), errmsg(), ERROR, xl_heap_lock::flags, get_mxact_status_for_lock(), GetCurrentTransactionId(), GetMultiXactIdMembers(), heap_acquire_tuplock(), HEAP_KEYS_UPDATED, heap_lock_updated_tuple(), HEAP_XMAX_INVALID, HEAP_XMAX_IS_EXCL_LOCKED(), HEAP_XMAX_IS_KEYSHR_LOCKED(), HEAP_XMAX_IS_LOCKED_ONLY(), HEAP_XMAX_IS_MULTI, HEAP_XMAX_IS_SHR_LOCKED(), HeapTupleHeaderClearHotUpdated(), HeapTupleHeaderGetCmax(), HeapTupleHeaderGetRawXmax(), HeapTupleHeaderGetUpdateXid(), HeapTupleHeaderIsOnlyLocked(), HeapTupleHeaderSetXmax(), HeapTupleSatisfiesUpdate(), i, xl_heap_lock::infobits_set, InvalidBuffer, InvalidCommandId, ItemIdGetLength, ItemIdIsNormal, ItemPointerCopy(), ItemPointerEquals(), ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), LockBuffer(), LockTupleExclusive, LockTupleKeyShare, LockTupleNoKeyExclusive, LockTupleShare, LockWaitBlock, LockWaitError, LockWaitSkip, log_lock_failure, MarkBufferDirty(), mode, MultiXactIdSetOldestMember(), MultiXactIdWait(), MultiXactStatusNoKeyUpdate, xl_heap_lock::offnum, PageGetItem(), PageGetItemId(), PageIsAllVisible(), PageSetLSN(), pfree(), ReadBuffer(), REGBUF_STANDARD, RelationGetRelationName, RelationGetRelid, RelationNeedsWAL, ReleaseBuffer(), SizeOfHeapLock, START_CRIT_SECTION, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleHeaderData::t_infomask, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TM_BeingModified, TM_Deleted, TM_Invisible, TM_Ok, TM_SelfModified, TM_Updated, TM_WouldBlock, TransactionIdEquals, TransactionIdIsCurrentTransactionId(), TUPLOCK_from_mxstatus, UnlockTupleTuplock, UpdateXmaxHintBits(), VISIBILITYMAP_ALL_FROZEN, visibilitymap_clear(), visibilitymap_pin(), XactLockTableWait(), XLH_LOCK_ALL_FROZEN_CLEARED, XLOG_HEAP_LOCK, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), XLogRegisterData(), XLTW_Lock, xl_heap_lock::xmax, TM_FailureData::xmax, and xmax_infomask_changed().

Referenced by heapam_tuple_lock().

◆ heap_lock_updated_tuple()

static TM_Result heap_lock_updated_tuple ( Relation  rel,
HeapTuple  tuple,
ItemPointer  ctid,
TransactionId  xid,
LockTupleMode  mode 
)
static

Definition at line 6007 of file heapam.c.

6009{
6010 /*
6011 * If the tuple has not been updated, or has moved into another partition
6012 * (effectively a delete) stop here.
6013 */
6015 !ItemPointerEquals(&tuple->t_self, ctid))
6016 {
6017 /*
6018 * If this is the first possibly-multixact-able operation in the
6019 * current transaction, set my per-backend OldestMemberMXactId
6020 * setting. We can be certain that the transaction will never become a
6021 * member of any older MultiXactIds than that. (We have to do this
6022 * even if we end up just using our own TransactionId below, since
6023 * some other backend could incorporate our XID into a MultiXact
6024 * immediately afterwards.)
6025 */
6027
6028 return heap_lock_updated_tuple_rec(rel, ctid, xid, mode);
6029 }
6030
6031 /* nothing to lock */
6032 return TM_Ok;
6033}
static TM_Result heap_lock_updated_tuple_rec(Relation rel, ItemPointer tid, TransactionId xid, LockTupleMode mode)
Definition: heapam.c:5662

References heap_lock_updated_tuple_rec(), HeapTupleHeaderIndicatesMovedPartitions(), ItemPointerEquals(), mode, MultiXactIdSetOldestMember(), HeapTupleData::t_data, HeapTupleData::t_self, and TM_Ok.

Referenced by heap_lock_tuple().

◆ heap_lock_updated_tuple_rec()

static TM_Result heap_lock_updated_tuple_rec ( Relation  rel,
ItemPointer  tid,
TransactionId  xid,
LockTupleMode  mode 
)
static

Definition at line 5662 of file heapam.c.

5664{
5665 TM_Result result;
5666 ItemPointerData tupid;
5667 HeapTupleData mytup;
5668 Buffer buf;
5669 uint16 new_infomask,
5670 new_infomask2,
5671 old_infomask,
5672 old_infomask2;
5673 TransactionId xmax,
5674 new_xmax;
5676 bool cleared_all_frozen = false;
5677 bool pinned_desired_page;
5678 Buffer vmbuffer = InvalidBuffer;
5679 BlockNumber block;
5680
5681 ItemPointerCopy(tid, &tupid);
5682
5683 for (;;)
5684 {
5685 new_infomask = 0;
5686 new_xmax = InvalidTransactionId;
5687 block = ItemPointerGetBlockNumber(&tupid);
5688 ItemPointerCopy(&tupid, &(mytup.t_self));
5689
5690 if (!heap_fetch(rel, SnapshotAny, &mytup, &buf, false))
5691 {
5692 /*
5693 * if we fail to find the updated version of the tuple, it's
5694 * because it was vacuumed/pruned away after its creator
5695 * transaction aborted. So behave as if we got to the end of the
5696 * chain, and there's no further tuple to lock: return success to
5697 * caller.
5698 */
5699 result = TM_Ok;
5700 goto out_unlocked;
5701 }
5702
5703l4:
5705
5706 /*
5707 * Before locking the buffer, pin the visibility map page if it
5708 * appears to be necessary. Since we haven't got the lock yet,
5709 * someone else might be in the middle of changing this, so we'll need
5710 * to recheck after we have the lock.
5711 */
5713 {
5714 visibilitymap_pin(rel, block, &vmbuffer);
5715 pinned_desired_page = true;
5716 }
5717 else
5718 pinned_desired_page = false;
5719
5721
5722 /*
5723 * If we didn't pin the visibility map page and the page has become
5724 * all visible while we were busy locking the buffer, we'll have to
5725 * unlock and re-lock, to avoid holding the buffer lock across I/O.
5726 * That's a bit unfortunate, but hopefully shouldn't happen often.
5727 *
5728 * Note: in some paths through this function, we will reach here
5729 * holding a pin on a vm page that may or may not be the one matching
5730 * this page. If this page isn't all-visible, we won't use the vm
5731 * page, but we hold onto such a pin till the end of the function.
5732 */
5733 if (!pinned_desired_page && PageIsAllVisible(BufferGetPage(buf)))
5734 {
5736 visibilitymap_pin(rel, block, &vmbuffer);
5738 }
5739
5740 /*
5741 * Check the tuple XMIN against prior XMAX, if any. If we reached the
5742 * end of the chain, we're done, so return success.
5743 */
5744 if (TransactionIdIsValid(priorXmax) &&
5746 priorXmax))
5747 {
5748 result = TM_Ok;
5749 goto out_locked;
5750 }
5751
5752 /*
5753 * Also check Xmin: if this tuple was created by an aborted
5754 * (sub)transaction, then we already locked the last live one in the
5755 * chain, thus we're done, so return success.
5756 */
5758 {
5759 result = TM_Ok;
5760 goto out_locked;
5761 }
5762
5763 old_infomask = mytup.t_data->t_infomask;
5764 old_infomask2 = mytup.t_data->t_infomask2;
5765 xmax = HeapTupleHeaderGetRawXmax(mytup.t_data);
5766
5767 /*
5768 * If this tuple version has been updated or locked by some concurrent
5769 * transaction(s), what we do depends on whether our lock mode
5770 * conflicts with what those other transactions hold, and also on the
5771 * status of them.
5772 */
5773 if (!(old_infomask & HEAP_XMAX_INVALID))
5774 {
5775 TransactionId rawxmax;
5776 bool needwait;
5777
5778 rawxmax = HeapTupleHeaderGetRawXmax(mytup.t_data);
5779 if (old_infomask & HEAP_XMAX_IS_MULTI)
5780 {
5781 int nmembers;
5782 int i;
5783 MultiXactMember *members;
5784
5785 /*
5786 * We don't need a test for pg_upgrade'd tuples: this is only
5787 * applied to tuples after the first in an update chain. Said
5788 * first tuple in the chain may well be locked-in-9.2-and-
5789 * pg_upgraded, but that one was already locked by our caller,
5790 * not us; and any subsequent ones cannot be because our
5791 * caller must necessarily have obtained a snapshot later than
5792 * the pg_upgrade itself.
5793 */
5795
5796 nmembers = GetMultiXactIdMembers(rawxmax, &members, false,
5797 HEAP_XMAX_IS_LOCKED_ONLY(old_infomask));
5798 for (i = 0; i < nmembers; i++)
5799 {
5800 result = test_lockmode_for_conflict(members[i].status,
5801 members[i].xid,
5802 mode,
5803 &mytup,
5804 &needwait);
5805
5806 /*
5807 * If the tuple was already locked by ourselves in a
5808 * previous iteration of this (say heap_lock_tuple was
5809 * forced to restart the locking loop because of a change
5810 * in xmax), then we hold the lock already on this tuple
5811 * version and we don't need to do anything; and this is
5812 * not an error condition either. We just need to skip
5813 * this tuple and continue locking the next version in the
5814 * update chain.
5815 */
5816 if (result == TM_SelfModified)
5817 {
5818 pfree(members);
5819 goto next;
5820 }
5821
5822 if (needwait)
5823 {
5825 XactLockTableWait(members[i].xid, rel,
5826 &mytup.t_self,
5828 pfree(members);
5829 goto l4;
5830 }
5831 if (result != TM_Ok)
5832 {
5833 pfree(members);
5834 goto out_locked;
5835 }
5836 }
5837 if (members)
5838 pfree(members);
5839 }
5840 else
5841 {
5842 MultiXactStatus status;
5843
5844 /*
5845 * For a non-multi Xmax, we first need to compute the
5846 * corresponding MultiXactStatus by using the infomask bits.
5847 */
5848 if (HEAP_XMAX_IS_LOCKED_ONLY(old_infomask))
5849 {
5850 if (HEAP_XMAX_IS_KEYSHR_LOCKED(old_infomask))
5852 else if (HEAP_XMAX_IS_SHR_LOCKED(old_infomask))
5853 status = MultiXactStatusForShare;
5854 else if (HEAP_XMAX_IS_EXCL_LOCKED(old_infomask))
5855 {
5856 if (old_infomask2 & HEAP_KEYS_UPDATED)
5857 status = MultiXactStatusForUpdate;
5858 else
5860 }
5861 else
5862 {
5863 /*
5864 * LOCK_ONLY present alone (a pg_upgraded tuple marked
5865 * as share-locked in the old cluster) shouldn't be
5866 * seen in the middle of an update chain.
5867 */
5868 elog(ERROR, "invalid lock status in tuple");
5869 }
5870 }
5871 else
5872 {
5873 /* it's an update, but which kind? */
5874 if (old_infomask2 & HEAP_KEYS_UPDATED)
5875 status = MultiXactStatusUpdate;
5876 else
5878 }
5879
5880 result = test_lockmode_for_conflict(status, rawxmax, mode,
5881 &mytup, &needwait);
5882
5883 /*
5884 * If the tuple was already locked by ourselves in a previous
5885 * iteration of this (say heap_lock_tuple was forced to
5886 * restart the locking loop because of a change in xmax), then
5887 * we hold the lock already on this tuple version and we don't
5888 * need to do anything; and this is not an error condition
5889 * either. We just need to skip this tuple and continue
5890 * locking the next version in the update chain.
5891 */
5892 if (result == TM_SelfModified)
5893 goto next;
5894
5895 if (needwait)
5896 {
5898 XactLockTableWait(rawxmax, rel, &mytup.t_self,
5900 goto l4;
5901 }
5902 if (result != TM_Ok)
5903 {
5904 goto out_locked;
5905 }
5906 }
5907 }
5908
5909 /* compute the new Xmax and infomask values for the tuple ... */
5910 compute_new_xmax_infomask(xmax, old_infomask, mytup.t_data->t_infomask2,
5911 xid, mode, false,
5912 &new_xmax, &new_infomask, &new_infomask2);
5913
5915 visibilitymap_clear(rel, block, vmbuffer,
5917 cleared_all_frozen = true;
5918
5920
5921 /* ... and set them */
5922 HeapTupleHeaderSetXmax(mytup.t_data, new_xmax);
5923 mytup.t_data->t_infomask &= ~HEAP_XMAX_BITS;
5924 mytup.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
5925 mytup.t_data->t_infomask |= new_infomask;
5926 mytup.t_data->t_infomask2 |= new_infomask2;
5927
5929
5930 /* XLOG stuff */
5931 if (RelationNeedsWAL(rel))
5932 {
5934 XLogRecPtr recptr;
5935 Page page = BufferGetPage(buf);
5936
5939
5941 xlrec.xmax = new_xmax;
5942 xlrec.infobits_set = compute_infobits(new_infomask, new_infomask2);
5943 xlrec.flags =
5944 cleared_all_frozen ? XLH_LOCK_ALL_FROZEN_CLEARED : 0;
5945
5947
5948 recptr = XLogInsert(RM_HEAP2_ID, XLOG_HEAP2_LOCK_UPDATED);
5949
5950 PageSetLSN(page, recptr);
5951 }
5952
5954
5955next:
5956 /* if we find the end of update chain, we're done. */
5957 if (mytup.t_data->t_infomask & HEAP_XMAX_INVALID ||
5959 ItemPointerEquals(&mytup.t_self, &mytup.t_data->t_ctid) ||
5961 {
5962 result = TM_Ok;
5963 goto out_locked;
5964 }
5965
5966 /* tail recursion */
5967 priorXmax = HeapTupleHeaderGetUpdateXid(mytup.t_data);
5968 ItemPointerCopy(&(mytup.t_data->t_ctid), &tupid);
5970 }
5971
5972 result = TM_Ok;
5973
5974out_locked:
5976
5977out_unlocked:
5978 if (vmbuffer != InvalidBuffer)
5979 ReleaseBuffer(vmbuffer);
5980
5981 return result;
5982}
static int32 next
Definition: blutils.c:224
bool heap_fetch(Relation relation, Snapshot snapshot, HeapTuple tuple, Buffer *userbuf, bool keep_buf)
Definition: heapam.c:1573
static TM_Result test_lockmode_for_conflict(MultiXactStatus status, TransactionId xid, LockTupleMode mode, HeapTuple tup, bool *needwait)
Definition: heapam.c:5571
#define SizeOfHeapLockUpdated
Definition: heapam_xlog.h:415
#define XLOG_HEAP2_LOCK_UPDATED
Definition: heapam_xlog.h:65
@ XLTW_LockUpdated
Definition: lmgr.h:30
#define SnapshotAny
Definition: snapmgr.h:33
TransactionId xmax
Definition: heapam_xlog.h:409
OffsetNumber offnum
Definition: heapam_xlog.h:410

References Assert(), buf, BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetPage(), CHECK_FOR_INTERRUPTS, compute_infobits(), compute_new_xmax_infomask(), elog, END_CRIT_SECTION, ERROR, xl_heap_lock_updated::flags, GetMultiXactIdMembers(), heap_fetch(), HEAP_KEYS_UPDATED, HEAP_LOCKED_UPGRADED(), HEAP_XMAX_INVALID, HEAP_XMAX_IS_EXCL_LOCKED(), HEAP_XMAX_IS_KEYSHR_LOCKED(), HEAP_XMAX_IS_LOCKED_ONLY(), HEAP_XMAX_IS_MULTI, HEAP_XMAX_IS_SHR_LOCKED(), HeapTupleHeaderGetRawXmax(), HeapTupleHeaderGetUpdateXid(), HeapTupleHeaderGetXmin(), HeapTupleHeaderIndicatesMovedPartitions(), HeapTupleHeaderIsOnlyLocked(), HeapTupleHeaderSetXmax(), i, xl_heap_lock_updated::infobits_set, InvalidBuffer, InvalidTransactionId, ItemPointerCopy(), ItemPointerEquals(), ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), LockBuffer(), MarkBufferDirty(), mode, MultiXactStatusForKeyShare, MultiXactStatusForNoKeyUpdate, MultiXactStatusForShare, MultiXactStatusForUpdate, MultiXactStatusNoKeyUpdate, MultiXactStatusUpdate, next, xl_heap_lock_updated::offnum, PageIsAllVisible(), PageSetLSN(), pfree(), REGBUF_STANDARD, RelationNeedsWAL, ReleaseBuffer(), SizeOfHeapLockUpdated, SnapshotAny, START_CRIT_SECTION, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleHeaderData::t_infomask, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_self, test_lockmode_for_conflict(), TM_Ok, TM_SelfModified, TransactionIdDidAbort(), TransactionIdEquals, TransactionIdIsValid, UnlockReleaseBuffer(), VISIBILITYMAP_ALL_FROZEN, visibilitymap_clear(), visibilitymap_pin(), XactLockTableWait(), XLH_LOCK_ALL_FROZEN_CLEARED, XLOG_HEAP2_LOCK_UPDATED, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), XLogRegisterData(), XLTW_LockUpdated, and xl_heap_lock_updated::xmax.

Referenced by heap_lock_updated_tuple().

◆ heap_multi_insert()

void heap_multi_insert ( Relation  relation,
TupleTableSlot **  slots,
int  ntuples,
CommandId  cid,
int  options,
BulkInsertState  bistate 
)

Definition at line 2325 of file heapam.c.

2327{
2329 HeapTuple *heaptuples;
2330 int i;
2331 int ndone;
2332 PGAlignedBlock scratch;
2333 Page page;
2334 Buffer vmbuffer = InvalidBuffer;
2335 bool needwal;
2336 Size saveFreeSpace;
2337 bool need_tuple_data = RelationIsLogicallyLogged(relation);
2338 bool need_cids = RelationIsAccessibleInLogicalDecoding(relation);
2339 bool starting_with_empty_page = false;
2340 int npages = 0;
2341 int npages_used = 0;
2342
2343 /* currently not needed (thus unsupported) for heap_multi_insert() */
2345
2346 needwal = RelationNeedsWAL(relation);
2347 saveFreeSpace = RelationGetTargetPageFreeSpace(relation,
2349
2350 /* Toast and set header data in all the slots */
2351 heaptuples = palloc(ntuples * sizeof(HeapTuple));
2352 for (i = 0; i < ntuples; i++)
2353 {
2354 HeapTuple tuple;
2355
2356 tuple = ExecFetchSlotHeapTuple(slots[i], true, NULL);
2357 slots[i]->tts_tableOid = RelationGetRelid(relation);
2358 tuple->t_tableOid = slots[i]->tts_tableOid;
2359 heaptuples[i] = heap_prepare_insert(relation, tuple, xid, cid,
2360 options);
2361 }
2362
2363 /*
2364 * We're about to do the actual inserts -- but check for conflict first,
2365 * to minimize the possibility of having to roll back work we've just
2366 * done.
2367 *
2368 * A check here does not definitively prevent a serialization anomaly;
2369 * that check MUST be done at least past the point of acquiring an
2370 * exclusive buffer content lock on every buffer that will be affected,
2371 * and MAY be done after all inserts are reflected in the buffers and
2372 * those locks are released; otherwise there is a race condition. Since
2373 * multiple buffers can be locked and unlocked in the loop below, and it
2374 * would not be feasible to identify and lock all of those buffers before
2375 * the loop, we must do a final check at the end.
2376 *
2377 * The check here could be omitted with no loss of correctness; it is
2378 * present strictly as an optimization.
2379 *
2380 * For heap inserts, we only need to check for table-level SSI locks. Our
2381 * new tuples can't possibly conflict with existing tuple locks, and heap
2382 * page locks are only consolidated versions of tuple locks; they do not
2383 * lock "gaps" as index page locks do. So we don't need to specify a
2384 * buffer when making the call, which makes for a faster check.
2385 */
2387
2388 ndone = 0;
2389 while (ndone < ntuples)
2390 {
2391 Buffer buffer;
2392 bool all_visible_cleared = false;
2393 bool all_frozen_set = false;
2394 int nthispage;
2395
2397
2398 /*
2399 * Compute number of pages needed to fit the to-be-inserted tuples in
2400 * the worst case. This will be used to determine how much to extend
2401 * the relation by in RelationGetBufferForTuple(), if needed. If we
2402 * filled a prior page from scratch, we can just update our last
2403 * computation, but if we started with a partially filled page,
2404 * recompute from scratch, the number of potentially required pages
2405 * can vary due to tuples needing to fit onto the page, page headers
2406 * etc.
2407 */
2408 if (ndone == 0 || !starting_with_empty_page)
2409 {
2410 npages = heap_multi_insert_pages(heaptuples, ndone, ntuples,
2411 saveFreeSpace);
2412 npages_used = 0;
2413 }
2414 else
2415 npages_used++;
2416
2417 /*
2418 * Find buffer where at least the next tuple will fit. If the page is
2419 * all-visible, this will also pin the requisite visibility map page.
2420 *
2421 * Also pin visibility map page if COPY FREEZE inserts tuples into an
2422 * empty page. See all_frozen_set below.
2423 */
2424 buffer = RelationGetBufferForTuple(relation, heaptuples[ndone]->t_len,
2425 InvalidBuffer, options, bistate,
2426 &vmbuffer, NULL,
2427 npages - npages_used);
2428 page = BufferGetPage(buffer);
2429
2430 starting_with_empty_page = PageGetMaxOffsetNumber(page) == 0;
2431
2432 if (starting_with_empty_page && (options & HEAP_INSERT_FROZEN))
2433 all_frozen_set = true;
2434
2435 /* NO EREPORT(ERROR) from here till changes are logged */
2437
2438 /*
2439 * RelationGetBufferForTuple has ensured that the first tuple fits.
2440 * Put that on the page, and then as many other tuples as fit.
2441 */
2442 RelationPutHeapTuple(relation, buffer, heaptuples[ndone], false);
2443
2444 /*
2445 * For logical decoding we need combo CIDs to properly decode the
2446 * catalog.
2447 */
2448 if (needwal && need_cids)
2449 log_heap_new_cid(relation, heaptuples[ndone]);
2450
2451 for (nthispage = 1; ndone + nthispage < ntuples; nthispage++)
2452 {
2453 HeapTuple heaptup = heaptuples[ndone + nthispage];
2454
2455 if (PageGetHeapFreeSpace(page) < MAXALIGN(heaptup->t_len) + saveFreeSpace)
2456 break;
2457
2458 RelationPutHeapTuple(relation, buffer, heaptup, false);
2459
2460 /*
2461 * For logical decoding we need combo CIDs to properly decode the
2462 * catalog.
2463 */
2464 if (needwal && need_cids)
2465 log_heap_new_cid(relation, heaptup);
2466 }
2467
2468 /*
2469 * If the page is all visible, need to clear that, unless we're only
2470 * going to add further frozen rows to it.
2471 *
2472 * If we're only adding already frozen rows to a previously empty
2473 * page, mark it as all-visible.
2474 */
2476 {
2477 all_visible_cleared = true;
2478 PageClearAllVisible(page);
2479 visibilitymap_clear(relation,
2480 BufferGetBlockNumber(buffer),
2481 vmbuffer, VISIBILITYMAP_VALID_BITS);
2482 }
2483 else if (all_frozen_set)
2484 PageSetAllVisible(page);
2485
2486 /*
2487 * XXX Should we set PageSetPrunable on this page ? See heap_insert()
2488 */
2489
2490 MarkBufferDirty(buffer);
2491
2492 /* XLOG stuff */
2493 if (needwal)
2494 {
2495 XLogRecPtr recptr;
2496 xl_heap_multi_insert *xlrec;
2498 char *tupledata;
2499 int totaldatalen;
2500 char *scratchptr = scratch.data;
2501 bool init;
2502 int bufflags = 0;
2503
2504 /*
2505 * If the page was previously empty, we can reinit the page
2506 * instead of restoring the whole thing.
2507 */
2508 init = starting_with_empty_page;
2509
2510 /* allocate xl_heap_multi_insert struct from the scratch area */
2511 xlrec = (xl_heap_multi_insert *) scratchptr;
2512 scratchptr += SizeOfHeapMultiInsert;
2513
2514 /*
2515 * Allocate offsets array. Unless we're reinitializing the page,
2516 * in that case the tuples are stored in order starting at
2517 * FirstOffsetNumber and we don't need to store the offsets
2518 * explicitly.
2519 */
2520 if (!init)
2521 scratchptr += nthispage * sizeof(OffsetNumber);
2522
2523 /* the rest of the scratch space is used for tuple data */
2524 tupledata = scratchptr;
2525
2526 /* check that the mutually exclusive flags are not both set */
2527 Assert(!(all_visible_cleared && all_frozen_set));
2528
2529 xlrec->flags = 0;
2530 if (all_visible_cleared)
2532 if (all_frozen_set)
2534
2535 xlrec->ntuples = nthispage;
2536
2537 /*
2538 * Write out an xl_multi_insert_tuple and the tuple data itself
2539 * for each tuple.
2540 */
2541 for (i = 0; i < nthispage; i++)
2542 {
2543 HeapTuple heaptup = heaptuples[ndone + i];
2544 xl_multi_insert_tuple *tuphdr;
2545 int datalen;
2546
2547 if (!init)
2548 xlrec->offsets[i] = ItemPointerGetOffsetNumber(&heaptup->t_self);
2549 /* xl_multi_insert_tuple needs two-byte alignment. */
2550 tuphdr = (xl_multi_insert_tuple *) SHORTALIGN(scratchptr);
2551 scratchptr = ((char *) tuphdr) + SizeOfMultiInsertTuple;
2552
2553 tuphdr->t_infomask2 = heaptup->t_data->t_infomask2;
2554 tuphdr->t_infomask = heaptup->t_data->t_infomask;
2555 tuphdr->t_hoff = heaptup->t_data->t_hoff;
2556
2557 /* write bitmap [+ padding] [+ oid] + data */
2558 datalen = heaptup->t_len - SizeofHeapTupleHeader;
2559 memcpy(scratchptr,
2560 (char *) heaptup->t_data + SizeofHeapTupleHeader,
2561 datalen);
2562 tuphdr->datalen = datalen;
2563 scratchptr += datalen;
2564 }
2565 totaldatalen = scratchptr - tupledata;
2566 Assert((scratchptr - scratch.data) < BLCKSZ);
2567
2568 if (need_tuple_data)
2570
2571 /*
2572 * Signal that this is the last xl_heap_multi_insert record
2573 * emitted by this call to heap_multi_insert(). Needed for logical
2574 * decoding so it knows when to cleanup temporary data.
2575 */
2576 if (ndone + nthispage == ntuples)
2578
2579 if (init)
2580 {
2581 info |= XLOG_HEAP_INIT_PAGE;
2582 bufflags |= REGBUF_WILL_INIT;
2583 }
2584
2585 /*
2586 * If we're doing logical decoding, include the new tuple data
2587 * even if we take a full-page image of the page.
2588 */
2589 if (need_tuple_data)
2590 bufflags |= REGBUF_KEEP_DATA;
2591
2593 XLogRegisterData(xlrec, tupledata - scratch.data);
2594 XLogRegisterBuffer(0, buffer, REGBUF_STANDARD | bufflags);
2595
2596 XLogRegisterBufData(0, tupledata, totaldatalen);
2597
2598 /* filtering by origin on a row level is much more efficient */
2600
2601 recptr = XLogInsert(RM_HEAP2_ID, info);
2602
2603 PageSetLSN(page, recptr);
2604 }
2605
2607
2608 /*
2609 * If we've frozen everything on the page, update the visibilitymap.
2610 * We're already holding pin on the vmbuffer.
2611 */
2612 if (all_frozen_set)
2613 {
2614 Assert(PageIsAllVisible(page));
2616
2617 /*
2618 * It's fine to use InvalidTransactionId here - this is only used
2619 * when HEAP_INSERT_FROZEN is specified, which intentionally
2620 * violates visibility rules.
2621 */
2622 visibilitymap_set(relation, BufferGetBlockNumber(buffer), buffer,
2623 InvalidXLogRecPtr, vmbuffer,
2626 }
2627
2628 UnlockReleaseBuffer(buffer);
2629 ndone += nthispage;
2630
2631 /*
2632 * NB: Only release vmbuffer after inserting all tuples - it's fairly
2633 * likely that we'll insert into subsequent heap pages that are likely
2634 * to use the same vm page.
2635 */
2636 }
2637
2638 /* We're done with inserting all tuples, so release the last vmbuffer. */
2639 if (vmbuffer != InvalidBuffer)
2640 ReleaseBuffer(vmbuffer);
2641
2642 /*
2643 * We're done with the actual inserts. Check for conflicts again, to
2644 * ensure that all rw-conflicts in to these inserts are detected. Without
2645 * this final check, a sequential scan of the heap may have locked the
2646 * table after the "before" check, missing one opportunity to detect the
2647 * conflict, and then scanned the table before the new tuples were there,
2648 * missing the other chance to detect the conflict.
2649 *
2650 * For heap inserts, we only need to check for table-level SSI locks. Our
2651 * new tuples can't possibly conflict with existing tuple locks, and heap
2652 * page locks are only consolidated versions of tuple locks; they do not
2653 * lock "gaps" as index page locks do. So we don't need to specify a
2654 * buffer when making the call.
2655 */
2657
2658 /*
2659 * If tuples are cachable, mark them for invalidation from the caches in
2660 * case we abort. Note it is OK to do this after releasing the buffer,
2661 * because the heaptuples data structure is all in local memory, not in
2662 * the shared buffer.
2663 */
2664 if (IsCatalogRelation(relation))
2665 {
2666 for (i = 0; i < ntuples; i++)
2667 CacheInvalidateHeapTuple(relation, heaptuples[i], NULL);
2668 }
2669
2670 /* copy t_self fields back to the caller's slots */
2671 for (i = 0; i < ntuples; i++)
2672 slots[i]->tts_tid = heaptuples[i]->t_self;
2673
2674 pgstat_count_heap_insert(relation, ntuples);
2675}
Size PageGetHeapFreeSpace(const PageData *page)
Definition: bufpage.c:990
static void PageSetAllVisible(Page page)
Definition: bufpage.h:434
#define MAXALIGN(LEN)
Definition: c.h:782
#define SHORTALIGN(LEN)
Definition: c.h:778
size_t Size
Definition: c.h:576
HeapTuple ExecFetchSlotHeapTuple(TupleTableSlot *slot, bool materialize, bool *shouldFree)
Definition: execTuples.c:1833
static int heap_multi_insert_pages(HeapTuple *heaptuples, int done, int ntuples, Size saveFreeSpace)
Definition: heapam.c:2293
#define HEAP_INSERT_FROZEN
Definition: heapam.h:37
#define SizeOfHeapMultiInsert
Definition: heapam_xlog.h:188
#define XLOG_HEAP2_MULTI_INSERT
Definition: heapam_xlog.h:64
#define XLH_INSERT_LAST_IN_MULTI
Definition: heapam_xlog.h:73
#define XLH_INSERT_ALL_FROZEN_SET
Definition: heapam_xlog.h:79
#define SizeOfMultiInsertTuple
Definition: heapam_xlog.h:199
int init
Definition: isn.c:79
#define RelationGetTargetPageFreeSpace(relation, defaultff)
Definition: rel.h:389
#define HEAP_DEFAULT_FILLFACTOR
Definition: rel.h:360
Oid tts_tableOid
Definition: tuptable.h:130
OffsetNumber offsets[FLEXIBLE_ARRAY_MEMBER]
Definition: heapam_xlog.h:185
bool visibilitymap_pin_ok(BlockNumber heapBlk, Buffer vmbuf)
uint8 visibilitymap_set(Relation rel, BlockNumber heapBlk, Buffer heapBuf, XLogRecPtr recptr, Buffer vmBuf, TransactionId cutoff_xid, uint8 flags)
#define VISIBILITYMAP_ALL_VISIBLE
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

References Assert(), BufferGetBlockNumber(), BufferGetPage(), CacheInvalidateHeapTuple(), CHECK_FOR_INTERRUPTS, CheckForSerializableConflictIn(), PGAlignedBlock::data, xl_multi_insert_tuple::datalen, END_CRIT_SECTION, ExecFetchSlotHeapTuple(), xl_heap_multi_insert::flags, GetCurrentTransactionId(), HEAP_DEFAULT_FILLFACTOR, HEAP_INSERT_FROZEN, HEAP_INSERT_NO_LOGICAL, heap_multi_insert_pages(), heap_prepare_insert(), i, init, InvalidBlockNumber, InvalidBuffer, InvalidTransactionId, InvalidXLogRecPtr, IsCatalogRelation(), ItemPointerGetOffsetNumber(), log_heap_new_cid(), MarkBufferDirty(), MAXALIGN, xl_heap_multi_insert::ntuples, xl_heap_multi_insert::offsets, PageClearAllVisible(), PageGetHeapFreeSpace(), PageGetMaxOffsetNumber(), PageIsAllVisible(), PageSetAllVisible(), PageSetLSN(), palloc(), pgstat_count_heap_insert(), REGBUF_KEEP_DATA, REGBUF_STANDARD, REGBUF_WILL_INIT, RelationGetBufferForTuple(), RelationGetRelid, RelationGetTargetPageFreeSpace, RelationIsAccessibleInLogicalDecoding, RelationIsLogicallyLogged, RelationNeedsWAL, RelationPutHeapTuple(), ReleaseBuffer(), SHORTALIGN, SizeOfHeapMultiInsert, SizeofHeapTupleHeader, SizeOfMultiInsertTuple, START_CRIT_SECTION, HeapTupleData::t_data, xl_multi_insert_tuple::t_hoff, HeapTupleHeaderData::t_hoff, xl_multi_insert_tuple::t_infomask, HeapTupleHeaderData::t_infomask, xl_multi_insert_tuple::t_infomask2, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TupleTableSlot::tts_tableOid, UnlockReleaseBuffer(), VISIBILITYMAP_ALL_FROZEN, VISIBILITYMAP_ALL_VISIBLE, visibilitymap_clear(), visibilitymap_pin_ok(), visibilitymap_set(), VISIBILITYMAP_VALID_BITS, XLH_INSERT_ALL_FROZEN_SET, XLH_INSERT_ALL_VISIBLE_CLEARED, XLH_INSERT_CONTAINS_NEW_TUPLE, XLH_INSERT_LAST_IN_MULTI, XLOG_HEAP2_MULTI_INSERT, XLOG_HEAP_INIT_PAGE, XLOG_INCLUDE_ORIGIN, XLogBeginInsert(), XLogInsert(), XLogRegisterBufData(), XLogRegisterBuffer(), XLogRegisterData(), and XLogSetRecordFlags().

Referenced by CatalogTuplesMultiInsertWithInfo().

◆ heap_multi_insert_pages()

static int heap_multi_insert_pages ( HeapTuple heaptuples,
int  done,
int  ntuples,
Size  saveFreeSpace 
)
static

Definition at line 2293 of file heapam.c.

2294{
2295 size_t page_avail = BLCKSZ - SizeOfPageHeaderData - saveFreeSpace;
2296 int npages = 1;
2297
2298 for (int i = done; i < ntuples; i++)
2299 {
2300 size_t tup_sz = sizeof(ItemIdData) + MAXALIGN(heaptuples[i]->t_len);
2301
2302 if (page_avail < tup_sz)
2303 {
2304 npages++;
2305 page_avail = BLCKSZ - SizeOfPageHeaderData - saveFreeSpace;
2306 }
2307 page_avail -= tup_sz;
2308 }
2309
2310 return npages;
2311}
#define SizeOfPageHeaderData
Definition: bufpage.h:217
struct ItemIdData ItemIdData

References i, MAXALIGN, and SizeOfPageHeaderData.

Referenced by heap_multi_insert().

◆ heap_pre_freeze_checks()

void heap_pre_freeze_checks ( Buffer  buffer,
HeapTupleFreeze tuples,
int  ntuples 
)

Definition at line 7283 of file heapam.c.

7285{
7286 Page page = BufferGetPage(buffer);
7287
7288 for (int i = 0; i < ntuples; i++)
7289 {
7290 HeapTupleFreeze *frz = tuples + i;
7291 ItemId itemid = PageGetItemId(page, frz->offset);
7292 HeapTupleHeader htup;
7293
7294 htup = (HeapTupleHeader) PageGetItem(page, itemid);
7295
7296 /* Deliberately avoid relying on tuple hint bits here */
7298 {
7300
7302 if (unlikely(!TransactionIdDidCommit(xmin)))
7303 ereport(ERROR,
7305 errmsg_internal("uncommitted xmin %u needs to be frozen",
7306 xmin)));
7307 }
7308
7309 /*
7310 * TransactionIdDidAbort won't work reliably in the presence of XIDs
7311 * left behind by transactions that were in progress during a crash,
7312 * so we can only check that xmax didn't commit
7313 */
7315 {
7317
7320 ereport(ERROR,
7322 errmsg_internal("cannot freeze committed xmax %u",
7323 xmax)));
7324 }
7325 }
7326}
#define HEAP_FREEZE_CHECK_XMAX_ABORTED
Definition: heapam.h:136
#define HEAP_FREEZE_CHECK_XMIN_COMMITTED
Definition: heapam.h:135
static bool HeapTupleHeaderXminFrozen(const HeapTupleHeaderData *tup)
Definition: htup_details.h:350
static TransactionId HeapTupleHeaderGetRawXmin(const HeapTupleHeaderData *tup)
Definition: htup_details.h:318
uint8 checkflags
Definition: heapam.h:148
#define TransactionIdIsNormal(xid)
Definition: transam.h:42

References Assert(), BufferGetPage(), HeapTupleFreeze::checkflags, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg_internal(), ERROR, HEAP_FREEZE_CHECK_XMAX_ABORTED, HEAP_FREEZE_CHECK_XMIN_COMMITTED, HeapTupleHeaderGetRawXmax(), HeapTupleHeaderGetRawXmin(), HeapTupleHeaderXminFrozen(), i, HeapTupleFreeze::offset, PageGetItem(), PageGetItemId(), TransactionIdDidCommit(), TransactionIdIsNormal, and unlikely.

Referenced by heap_page_prune_and_freeze().

◆ heap_prepare_freeze_tuple()

bool heap_prepare_freeze_tuple ( HeapTupleHeader  tuple,
const struct VacuumCutoffs cutoffs,
HeapPageFreeze pagefrz,
HeapTupleFreeze frz,
bool *  totally_frozen 
)

Definition at line 7010 of file heapam.c.

7014{
7015 bool xmin_already_frozen = false,
7016 xmax_already_frozen = false;
7017 bool freeze_xmin = false,
7018 replace_xvac = false,
7019 replace_xmax = false,
7020 freeze_xmax = false;
7021 TransactionId xid;
7022
7023 frz->xmax = HeapTupleHeaderGetRawXmax(tuple);
7024 frz->t_infomask2 = tuple->t_infomask2;
7025 frz->t_infomask = tuple->t_infomask;
7026 frz->frzflags = 0;
7027 frz->checkflags = 0;
7028
7029 /*
7030 * Process xmin, while keeping track of whether it's already frozen, or
7031 * will become frozen iff our freeze plan is executed by caller (could be
7032 * neither).
7033 */
7034 xid = HeapTupleHeaderGetXmin(tuple);
7035 if (!TransactionIdIsNormal(xid))
7036 xmin_already_frozen = true;
7037 else
7038 {
7039 if (TransactionIdPrecedes(xid, cutoffs->relfrozenxid))
7040 ereport(ERROR,
7042 errmsg_internal("found xmin %u from before relfrozenxid %u",
7043 xid, cutoffs->relfrozenxid)));
7044
7045 /* Will set freeze_xmin flags in freeze plan below */
7046 freeze_xmin = TransactionIdPrecedes(xid, cutoffs->OldestXmin);
7047
7048 /* Verify that xmin committed if and when freeze plan is executed */
7049 if (freeze_xmin)
7051 }
7052
7053 /*
7054 * Old-style VACUUM FULL is gone, but we have to process xvac for as long
7055 * as we support having MOVED_OFF/MOVED_IN tuples in the database
7056 */
7057 xid = HeapTupleHeaderGetXvac(tuple);
7058 if (TransactionIdIsNormal(xid))
7059 {
7061 Assert(TransactionIdPrecedes(xid, cutoffs->OldestXmin));
7062
7063 /*
7064 * For Xvac, we always freeze proactively. This allows totally_frozen
7065 * tracking to ignore xvac.
7066 */
7067 replace_xvac = pagefrz->freeze_required = true;
7068
7069 /* Will set replace_xvac flags in freeze plan below */
7070 }
7071
7072 /* Now process xmax */
7073 xid = frz->xmax;
7074 if (tuple->t_infomask & HEAP_XMAX_IS_MULTI)
7075 {
7076 /* Raw xmax is a MultiXactId */
7077 TransactionId newxmax;
7078 uint16 flags;
7079
7080 /*
7081 * We will either remove xmax completely (in the "freeze_xmax" path),
7082 * process xmax by replacing it (in the "replace_xmax" path), or
7083 * perform no-op xmax processing. The only constraint is that the
7084 * FreezeLimit/MultiXactCutoff postcondition must never be violated.
7085 */
7086 newxmax = FreezeMultiXactId(xid, tuple->t_infomask, cutoffs,
7087 &flags, pagefrz);
7088
7089 if (flags & FRM_NOOP)
7090 {
7091 /*
7092 * xmax is a MultiXactId, and nothing about it changes for now.
7093 * This is the only case where 'freeze_required' won't have been
7094 * set for us by FreezeMultiXactId, as well as the only case where
7095 * neither freeze_xmax nor replace_xmax are set (given a multi).
7096 *
7097 * This is a no-op, but the call to FreezeMultiXactId might have
7098 * ratcheted back NewRelfrozenXid and/or NewRelminMxid trackers
7099 * for us (the "freeze page" variants, specifically). That'll
7100 * make it safe for our caller to freeze the page later on, while
7101 * leaving this particular xmax undisturbed.
7102 *
7103 * FreezeMultiXactId is _not_ responsible for the "no freeze"
7104 * NewRelfrozenXid/NewRelminMxid trackers, though -- that's our
7105 * job. A call to heap_tuple_should_freeze for this same tuple
7106 * will take place below if 'freeze_required' isn't set already.
7107 * (This repeats work from FreezeMultiXactId, but allows "no
7108 * freeze" tracker maintenance to happen in only one place.)
7109 */
7110 Assert(!MultiXactIdPrecedes(newxmax, cutoffs->MultiXactCutoff));
7111 Assert(MultiXactIdIsValid(newxmax) && xid == newxmax);
7112 }
7113 else if (flags & FRM_RETURN_IS_XID)
7114 {
7115 /*
7116 * xmax will become an updater Xid (original MultiXact's updater
7117 * member Xid will be carried forward as a simple Xid in Xmax).
7118 */
7119 Assert(!TransactionIdPrecedes(newxmax, cutoffs->OldestXmin));
7120
7121 /*
7122 * NB -- some of these transformations are only valid because we
7123 * know the return Xid is a tuple updater (i.e. not merely a
7124 * locker.) Also note that the only reason we don't explicitly
7125 * worry about HEAP_KEYS_UPDATED is because it lives in
7126 * t_infomask2 rather than t_infomask.
7127 */
7128 frz->t_infomask &= ~HEAP_XMAX_BITS;
7129 frz->xmax = newxmax;
7130 if (flags & FRM_MARK_COMMITTED)
7132 replace_xmax = true;
7133 }
7134 else if (flags & FRM_RETURN_IS_MULTI)
7135 {
7136 uint16 newbits;
7137 uint16 newbits2;
7138
7139 /*
7140 * xmax is an old MultiXactId that we have to replace with a new
7141 * MultiXactId, to carry forward two or more original member XIDs.
7142 */
7143 Assert(!MultiXactIdPrecedes(newxmax, cutoffs->OldestMxact));
7144
7145 /*
7146 * We can't use GetMultiXactIdHintBits directly on the new multi
7147 * here; that routine initializes the masks to all zeroes, which
7148 * would lose other bits we need. Doing it this way ensures all
7149 * unrelated bits remain untouched.
7150 */
7151 frz->t_infomask &= ~HEAP_XMAX_BITS;
7152 frz->t_infomask2 &= ~HEAP_KEYS_UPDATED;
7153 GetMultiXactIdHintBits(newxmax, &newbits, &newbits2);
7154 frz->t_infomask |= newbits;
7155 frz->t_infomask2 |= newbits2;
7156 frz->xmax = newxmax;
7157 replace_xmax = true;
7158 }
7159 else
7160 {
7161 /*
7162 * Freeze plan for tuple "freezes xmax" in the strictest sense:
7163 * it'll leave nothing in xmax (neither an Xid nor a MultiXactId).
7164 */
7165 Assert(flags & FRM_INVALIDATE_XMAX);
7166 Assert(!TransactionIdIsValid(newxmax));
7167
7168 /* Will set freeze_xmax flags in freeze plan below */
7169 freeze_xmax = true;
7170 }
7171
7172 /* MultiXactId processing forces freezing (barring FRM_NOOP case) */
7173 Assert(pagefrz->freeze_required || (!freeze_xmax && !replace_xmax));
7174 }
7175 else if (TransactionIdIsNormal(xid))
7176 {
7177 /* Raw xmax is normal XID */
7178 if (TransactionIdPrecedes(xid, cutoffs->relfrozenxid))
7179 ereport(ERROR,
7181 errmsg_internal("found xmax %u from before relfrozenxid %u",
7182 xid, cutoffs->relfrozenxid)));
7183
7184 /* Will set freeze_xmax flags in freeze plan below */
7185 freeze_xmax = TransactionIdPrecedes(xid, cutoffs->OldestXmin);
7186
7187 /*
7188 * Verify that xmax aborted if and when freeze plan is executed,
7189 * provided it's from an update. (A lock-only xmax can be removed
7190 * independent of this, since the lock is released at xact end.)
7191 */
7192 if (freeze_xmax && !HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_infomask))
7194 }
7195 else if (!TransactionIdIsValid(xid))
7196 {
7197 /* Raw xmax is InvalidTransactionId XID */
7198 Assert((tuple->t_infomask & HEAP_XMAX_IS_MULTI) == 0);
7199 xmax_already_frozen = true;
7200 }
7201 else
7202 ereport(ERROR,
7204 errmsg_internal("found raw xmax %u (infomask 0x%04x) not invalid and not multi",
7205 xid, tuple->t_infomask)));
7206
7207 if (freeze_xmin)
7208 {
7209 Assert(!xmin_already_frozen);
7210
7212 }
7213 if (replace_xvac)
7214 {
7215 /*
7216 * If a MOVED_OFF tuple is not dead, the xvac transaction must have
7217 * failed; whereas a non-dead MOVED_IN tuple must mean the xvac
7218 * transaction succeeded.
7219 */
7220 Assert(pagefrz->freeze_required);
7221 if (tuple->t_infomask & HEAP_MOVED_OFF)
7222 frz->frzflags |= XLH_INVALID_XVAC;
7223 else
7224 frz->frzflags |= XLH_FREEZE_XVAC;
7225 }
7226 if (replace_xmax)
7227 {
7228 Assert(!xmax_already_frozen && !freeze_xmax);
7229 Assert(pagefrz->freeze_required);
7230
7231 /* Already set replace_xmax flags in freeze plan earlier */
7232 }
7233 if (freeze_xmax)
7234 {
7235 Assert(!xmax_already_frozen && !replace_xmax);
7236
7238
7239 /*
7240 * The tuple might be marked either XMAX_INVALID or XMAX_COMMITTED +
7241 * LOCKED. Normalize to INVALID just to be sure no one gets confused.
7242 * Also get rid of the HEAP_KEYS_UPDATED bit.
7243 */
7244 frz->t_infomask &= ~HEAP_XMAX_BITS;
7246 frz->t_infomask2 &= ~HEAP_HOT_UPDATED;
7247 frz->t_infomask2 &= ~HEAP_KEYS_UPDATED;
7248 }
7249
7250 /*
7251 * Determine if this tuple is already totally frozen, or will become
7252 * totally frozen (provided caller executes freeze plans for the page)
7253 */
7254 *totally_frozen = ((freeze_xmin || xmin_already_frozen) &&
7255 (freeze_xmax || xmax_already_frozen));
7256
7257 if (!pagefrz->freeze_required && !(xmin_already_frozen &&
7258 xmax_already_frozen))
7259 {
7260 /*
7261 * So far no previous tuple from the page made freezing mandatory.
7262 * Does this tuple force caller to freeze the entire page?
7263 */
7264 pagefrz->freeze_required =
7265 heap_tuple_should_freeze(tuple, cutoffs,
7266 &pagefrz->NoFreezePageRelfrozenXid,
7267 &pagefrz->NoFreezePageRelminMxid);
7268 }
7269
7270 /* Tell caller if this tuple has a usable freeze plan set in *frz */
7271 return freeze_xmin || replace_xvac || replace_xmax || freeze_xmax;
7272}
static TransactionId FreezeMultiXactId(MultiXactId multi, uint16 t_infomask, const struct VacuumCutoffs *cutoffs, uint16 *flags, HeapPageFreeze *pagefrz)
Definition: heapam.c:6660
bool heap_tuple_should_freeze(HeapTupleHeader tuple, const struct VacuumCutoffs *cutoffs, TransactionId *NoFreezePageRelfrozenXid, MultiXactId *NoFreezePageRelminMxid)
Definition: heapam.c:7821
#define XLH_INVALID_XVAC
Definition: heapam_xlog.h:340
#define XLH_FREEZE_XVAC
Definition: heapam_xlog.h:339
#define HEAP_MOVED_OFF
Definition: htup_details.h:211
#define HEAP_XMIN_FROZEN
Definition: htup_details.h:206
static TransactionId HeapTupleHeaderGetXvac(const HeapTupleHeaderData *tup)
Definition: htup_details.h:442
MultiXactId NoFreezePageRelminMxid
Definition: heapam.h:218
TransactionId NoFreezePageRelfrozenXid
Definition: heapam.h:217
uint8 frzflags
Definition: heapam.h:145
uint16 t_infomask2
Definition: heapam.h:143
TransactionId xmax
Definition: heapam.h:142
uint16 t_infomask
Definition: heapam.h:144
bool TransactionIdPrecedesOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:299

References Assert(), HeapTupleFreeze::checkflags, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg_internal(), ERROR, HeapPageFreeze::freeze_required, FreezeMultiXactId(), FRM_INVALIDATE_XMAX, FRM_MARK_COMMITTED, FRM_NOOP, FRM_RETURN_IS_MULTI, FRM_RETURN_IS_XID, HeapTupleFreeze::frzflags, GetMultiXactIdHintBits(), HEAP_FREEZE_CHECK_XMAX_ABORTED, HEAP_FREEZE_CHECK_XMIN_COMMITTED, HEAP_MOVED_OFF, heap_tuple_should_freeze(), HEAP_XMAX_COMMITTED, HEAP_XMAX_INVALID, HEAP_XMAX_IS_LOCKED_ONLY(), HEAP_XMAX_IS_MULTI, HEAP_XMIN_FROZEN, HeapTupleHeaderGetRawXmax(), HeapTupleHeaderGetXmin(), HeapTupleHeaderGetXvac(), InvalidTransactionId, VacuumCutoffs::MultiXactCutoff, MultiXactIdIsValid, MultiXactIdPrecedes(), HeapPageFreeze::NoFreezePageRelfrozenXid, HeapPageFreeze::NoFreezePageRelminMxid, VacuumCutoffs::OldestMxact, VacuumCutoffs::OldestXmin, VacuumCutoffs::relfrozenxid, HeapTupleFreeze::t_infomask, HeapTupleHeaderData::t_infomask, HeapTupleFreeze::t_infomask2, HeapTupleHeaderData::t_infomask2, TransactionIdIsNormal, TransactionIdIsValid, TransactionIdPrecedes(), TransactionIdPrecedesOrEquals(), XLH_FREEZE_XVAC, XLH_INVALID_XVAC, and HeapTupleFreeze::xmax.

Referenced by heap_freeze_tuple(), and heap_prune_record_unchanged_lp_normal().

◆ heap_prepare_insert()

static HeapTuple heap_prepare_insert ( Relation  relation,
HeapTuple  tup,
TransactionId  xid,
CommandId  cid,
int  options 
)
static

Definition at line 2245 of file heapam.c.

2247{
2248 /*
2249 * To allow parallel inserts, we need to ensure that they are safe to be
2250 * performed in workers. We have the infrastructure to allow parallel
2251 * inserts in general except for the cases where inserts generate a new
2252 * CommandId (eg. inserts into a table having a foreign key column).
2253 */
2254 if (IsParallelWorker())
2255 ereport(ERROR,
2256 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
2257 errmsg("cannot insert tuples in a parallel worker")));
2258
2259 tup->t_data->t_infomask &= ~(HEAP_XACT_MASK);
2262 HeapTupleHeaderSetXmin(tup->t_data, xid);
2265
2266 HeapTupleHeaderSetCmin(tup->t_data, cid);
2267 HeapTupleHeaderSetXmax(tup->t_data, 0); /* for cleanliness */
2268 tup->t_tableOid = RelationGetRelid(relation);
2269
2270 /*
2271 * If the new tuple is too big for storage or contains already toasted
2272 * out-of-line attributes from some other relation, invoke the toaster.
2273 */
2274 if (relation->rd_rel->relkind != RELKIND_RELATION &&
2275 relation->rd_rel->relkind != RELKIND_MATVIEW)
2276 {
2277 /* toast table entries should never be recursively toasted */
2279 return tup;
2280 }
2281 else if (HeapTupleHasExternal(tup) || tup->t_len > TOAST_TUPLE_THRESHOLD)
2282 return heap_toast_insert_or_update(relation, tup, NULL, options);
2283 else
2284 return tup;
2285}
HeapTuple heap_toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup, int options)
Definition: heaptoast.c:96
#define TOAST_TUPLE_THRESHOLD
Definition: heaptoast.h:48
static void HeapTupleHeaderSetXminFrozen(HeapTupleHeaderData *tup)
Definition: htup_details.h:370
#define HEAP2_XACT_MASK
Definition: htup_details.h:293
static void HeapTupleHeaderSetCmin(HeapTupleHeaderData *tup, CommandId cid)
Definition: htup_details.h:422
#define HEAP_XACT_MASK
Definition: htup_details.h:215
#define IsParallelWorker()
Definition: parallel.h:60

References Assert(), ereport, errcode(), errmsg(), ERROR, HEAP2_XACT_MASK, HEAP_INSERT_FROZEN, heap_toast_insert_or_update(), HEAP_XACT_MASK, HEAP_XMAX_INVALID, HeapTupleHasExternal(), HeapTupleHeaderSetCmin(), HeapTupleHeaderSetXmax(), HeapTupleHeaderSetXmin(), HeapTupleHeaderSetXminFrozen(), IsParallelWorker, RelationData::rd_rel, RelationGetRelid, HeapTupleData::t_data, HeapTupleHeaderData::t_infomask, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_tableOid, and TOAST_TUPLE_THRESHOLD.

Referenced by heap_insert(), and heap_multi_insert().

◆ heap_prepare_pagescan()

void heap_prepare_pagescan ( TableScanDesc  sscan)

Definition at line 531 of file heapam.c.

532{
533 HeapScanDesc scan = (HeapScanDesc) sscan;
534 Buffer buffer = scan->rs_cbuf;
535 BlockNumber block = scan->rs_cblock;
536 Snapshot snapshot;
537 Page page;
538 int lines;
539 bool all_visible;
540 bool check_serializable;
541
542 Assert(BufferGetBlockNumber(buffer) == block);
543
544 /* ensure we're not accidentally being used when not in pagemode */
546 snapshot = scan->rs_base.rs_snapshot;
547
548 /*
549 * Prune and repair fragmentation for the whole page, if possible.
550 */
551 heap_page_prune_opt(scan->rs_base.rs_rd, buffer);
552
553 /*
554 * We must hold share lock on the buffer content while examining tuple
555 * visibility. Afterwards, however, the tuples we have found to be
556 * visible are guaranteed good as long as we hold the buffer pin.
557 */
559
560 page = BufferGetPage(buffer);
561 lines = PageGetMaxOffsetNumber(page);
562
563 /*
564 * If the all-visible flag indicates that all tuples on the page are
565 * visible to everyone, we can skip the per-tuple visibility tests.
566 *
567 * Note: In hot standby, a tuple that's already visible to all
568 * transactions on the primary might still be invisible to a read-only
569 * transaction in the standby. We partly handle this problem by tracking
570 * the minimum xmin of visible tuples as the cut-off XID while marking a
571 * page all-visible on the primary and WAL log that along with the
572 * visibility map SET operation. In hot standby, we wait for (or abort)
573 * all transactions that can potentially may not see one or more tuples on
574 * the page. That's how index-only scans work fine in hot standby. A
575 * crucial difference between index-only scans and heap scans is that the
576 * index-only scan completely relies on the visibility map where as heap
577 * scan looks at the page-level PD_ALL_VISIBLE flag. We are not sure if
578 * the page-level flag can be trusted in the same way, because it might
579 * get propagated somehow without being explicitly WAL-logged, e.g. via a
580 * full page write. Until we can prove that beyond doubt, let's check each
581 * tuple for visibility the hard way.
582 */
583 all_visible = PageIsAllVisible(page) && !snapshot->takenDuringRecovery;
584 check_serializable =
586
587 /*
588 * We call page_collect_tuples() with constant arguments, to get the
589 * compiler to constant fold the constant arguments. Separate calls with
590 * constant arguments, rather than variables, are needed on several
591 * compilers to actually perform constant folding.
592 */
593 if (likely(all_visible))
594 {
595 if (likely(!check_serializable))
596 scan->rs_ntuples = page_collect_tuples(scan, snapshot, page, buffer,
597 block, lines, true, false);
598 else
599 scan->rs_ntuples = page_collect_tuples(scan, snapshot, page, buffer,
600 block, lines, true, true);
601 }
602 else
603 {
604 if (likely(!check_serializable))
605 scan->rs_ntuples = page_collect_tuples(scan, snapshot, page, buffer,
606 block, lines, false, false);
607 else
608 scan->rs_ntuples = page_collect_tuples(scan, snapshot, page, buffer,
609 block, lines, false, true);
610 }
611
613}
#define likely(x)
Definition: c.h:346
static pg_attribute_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:481
bool CheckForSerializableConflictOutNeeded(Relation relation, Snapshot snapshot)
Definition: predicate.c:3991
void heap_page_prune_opt(Relation relation, Buffer buffer)
Definition: pruneheap.c:193
uint32 rs_ntuples
Definition: heapam.h:97
bool takenDuringRecovery
Definition: snapshot.h:180

References Assert(), BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage(), CheckForSerializableConflictOutNeeded(), heap_page_prune_opt(), likely, LockBuffer(), page_collect_tuples(), PageGetMaxOffsetNumber(), PageIsAllVisible(), HeapScanDescData::rs_base, HeapScanDescData::rs_cblock, HeapScanDescData::rs_cbuf, TableScanDescData::rs_flags, HeapScanDescData::rs_ntuples, TableScanDescData::rs_rd, TableScanDescData::rs_snapshot, SO_ALLOW_PAGEMODE, and SnapshotData::takenDuringRecovery.

Referenced by heapam_scan_sample_next_block(), and heapgettup_pagemode().

◆ heap_rescan()

void heap_rescan ( TableScanDesc  sscan,
ScanKey  key,
bool  set_params,
bool  allow_strat,
bool  allow_sync,
bool  allow_pagemode 
)

Definition at line 1222 of file heapam.c.

1224{
1225 HeapScanDesc scan = (HeapScanDesc) sscan;
1226
1227 if (set_params)
1228 {
1229 if (allow_strat)
1231 else
1232 scan->rs_base.rs_flags &= ~SO_ALLOW_STRAT;
1233
1234 if (allow_sync)
1236 else
1237 scan->rs_base.rs_flags &= ~SO_ALLOW_SYNC;
1238
1239 if (allow_pagemode && scan->rs_base.rs_snapshot &&
1242 else
1244 }
1245
1246 /*
1247 * unpin scan buffers
1248 */
1249 if (BufferIsValid(scan->rs_cbuf))
1250 {
1251 ReleaseBuffer(scan->rs_cbuf);
1252 scan->rs_cbuf = InvalidBuffer;
1253 }
1254
1255 /*
1256 * SO_TYPE_BITMAPSCAN would be cleaned up here, but it does not hold any
1257 * additional data vs a normal HeapScan
1258 */
1259
1260 /*
1261 * The read stream is reset on rescan. This must be done before
1262 * initscan(), as some state referred to by read_stream_reset() is reset
1263 * in initscan().
1264 */
1265 if (scan->rs_read_stream)
1267
1268 /*
1269 * reinitialize scan descriptor
1270 */
1271 initscan(scan, key, true);
1272}
@ SO_ALLOW_STRAT
Definition: tableam.h:57
@ SO_ALLOW_SYNC
Definition: tableam.h:59

References BufferIsValid(), initscan(), InvalidBuffer, IsMVCCSnapshot, sort-test::key, read_stream_reset(), ReleaseBuffer(), HeapScanDescData::rs_base, HeapScanDescData::rs_cbuf, TableScanDescData::rs_flags, HeapScanDescData::rs_read_stream, TableScanDescData::rs_snapshot, SO_ALLOW_PAGEMODE, SO_ALLOW_STRAT, and SO_ALLOW_SYNC.

◆ heap_scan_stream_read_next_parallel()

static BlockNumber heap_scan_stream_read_next_parallel ( ReadStream stream,
void *  callback_private_data,
void *  per_buffer_data 
)
static

Definition at line 226 of file heapam.c.

229{
230 HeapScanDesc scan = (HeapScanDesc) callback_private_data;
231
234
235 if (unlikely(!scan->rs_inited))
236 {
237 /* parallel scan */
241
242 /* may return InvalidBlockNumber if there are no more blocks */
246 scan->rs_inited = true;
247 }
248 else
249 {
252 scan->rs_base.rs_parallel);
253 }
254
255 return scan->rs_prefetch_block;
256}
bool rs_inited
Definition: heapam.h:65
void table_block_parallelscan_startblock_init(Relation rel, ParallelBlockTableScanWorker pbscanwork, ParallelBlockTableScanDesc pbscan)
Definition: tableam.c:422
BlockNumber table_block_parallelscan_nextpage(Relation rel, ParallelBlockTableScanWorker pbscanwork, ParallelBlockTableScanDesc pbscan)
Definition: tableam.c:492

References Assert(), HeapScanDescData::rs_base, HeapScanDescData::rs_dir, HeapScanDescData::rs_inited, TableScanDescData::rs_parallel, HeapScanDescData::rs_parallelworkerdata, HeapScanDescData::rs_prefetch_block, TableScanDescData::rs_rd, ScanDirectionIsForward, table_block_parallelscan_nextpage(), table_block_parallelscan_startblock_init(), and unlikely.

Referenced by heap_beginscan().

◆ heap_scan_stream_read_next_serial()

static BlockNumber heap_scan_stream_read_next_serial ( ReadStream stream,
void *  callback_private_data,
void *  per_buffer_data 
)
static

Definition at line 264 of file heapam.c.

267{
268 HeapScanDesc scan = (HeapScanDesc) callback_private_data;
269
270 if (unlikely(!scan->rs_inited))
271 {
273 scan->rs_inited = true;
274 }
275 else
277 scan->rs_prefetch_block,
278 scan->rs_dir);
279
280 return scan->rs_prefetch_block;
281}
static pg_noinline BlockNumber heapgettup_initial_block(HeapScanDesc scan, ScanDirection dir)
Definition: heapam.c:667
static BlockNumber heapgettup_advance_block(HeapScanDesc scan, BlockNumber block, ScanDirection dir)
Definition: heapam.c:791

References heapgettup_advance_block(), heapgettup_initial_block(), HeapScanDescData::rs_dir, HeapScanDescData::rs_inited, HeapScanDescData::rs_prefetch_block, and unlikely.

Referenced by heap_beginscan().

◆ heap_set_tidrange()

void heap_set_tidrange ( TableScanDesc  sscan,
ItemPointer  mintid,
ItemPointer  maxtid 
)

Definition at line 1393 of file heapam.c.

1395{
1396 HeapScanDesc scan = (HeapScanDesc) sscan;
1397 BlockNumber startBlk;
1398 BlockNumber numBlks;
1399 ItemPointerData highestItem;
1400 ItemPointerData lowestItem;
1401
1402 /*
1403 * For relations without any pages, we can simply leave the TID range
1404 * unset. There will be no tuples to scan, therefore no tuples outside
1405 * the given TID range.
1406 */
1407 if (scan->rs_nblocks == 0)
1408 return;
1409
1410 /*
1411 * Set up some ItemPointers which point to the first and last possible
1412 * tuples in the heap.
1413 */
1414 ItemPointerSet(&highestItem, scan->rs_nblocks - 1, MaxOffsetNumber);
1415 ItemPointerSet(&lowestItem, 0, FirstOffsetNumber);
1416
1417 /*
1418 * If the given maximum TID is below the highest possible TID in the
1419 * relation, then restrict the range to that, otherwise we scan to the end
1420 * of the relation.
1421 */
1422 if (ItemPointerCompare(maxtid, &highestItem) < 0)
1423 ItemPointerCopy(maxtid, &highestItem);
1424
1425 /*
1426 * If the given minimum TID is above the lowest possible TID in the
1427 * relation, then restrict the range to only scan for TIDs above that.
1428 */
1429 if (ItemPointerCompare(mintid, &lowestItem) > 0)
1430 ItemPointerCopy(mintid, &lowestItem);
1431
1432 /*
1433 * Check for an empty range and protect from would be negative results
1434 * from the numBlks calculation below.
1435 */
1436 if (ItemPointerCompare(&highestItem, &lowestItem) < 0)
1437 {
1438 /* Set an empty range of blocks to scan */
1439 heap_setscanlimits(sscan, 0, 0);
1440 return;
1441 }
1442
1443 /*
1444 * Calculate the first block and the number of blocks we must scan. We
1445 * could be more aggressive here and perform some more validation to try
1446 * and further narrow the scope of blocks to scan by checking if the
1447 * lowestItem has an offset above MaxOffsetNumber. In this case, we could
1448 * advance startBlk by one. Likewise, if highestItem has an offset of 0
1449 * we could scan one fewer blocks. However, such an optimization does not
1450 * seem worth troubling over, currently.
1451 */
1452 startBlk = ItemPointerGetBlockNumberNoCheck(&lowestItem);
1453
1454 numBlks = ItemPointerGetBlockNumberNoCheck(&highestItem) -
1455 ItemPointerGetBlockNumberNoCheck(&lowestItem) + 1;
1456
1457 /* Set the start block and number of blocks to scan */
1458 heap_setscanlimits(sscan, startBlk, numBlks);
1459
1460 /* Finally, set the TID range in sscan */
1461 ItemPointerCopy(&lowestItem, &sscan->st.tidrange.rs_mintid);
1462 ItemPointerCopy(&highestItem, &sscan->st.tidrange.rs_maxtid);
1463}
void heap_setscanlimits(TableScanDesc sscan, BlockNumber startBlk, BlockNumber numBlks)
Definition: heapam.c:459
static BlockNumber ItemPointerGetBlockNumberNoCheck(const ItemPointerData *pointer)
Definition: itemptr.h:93
#define MaxOffsetNumber
Definition: off.h:28

References FirstOffsetNumber, heap_setscanlimits(), ItemPointerCompare(), ItemPointerCopy(), ItemPointerGetBlockNumberNoCheck(), ItemPointerSet(), MaxOffsetNumber, TableScanDescData::rs_maxtid, TableScanDescData::rs_mintid, HeapScanDescData::rs_nblocks, TableScanDescData::st, and TableScanDescData::tidrange.

◆ heap_setscanlimits()

void heap_setscanlimits ( TableScanDesc  sscan,
BlockNumber  startBlk,
BlockNumber  numBlks 
)

Definition at line 459 of file heapam.c.

460{
461 HeapScanDesc scan = (HeapScanDesc) sscan;
462
463 Assert(!scan->rs_inited); /* else too late to change */
464 /* else rs_startblock is significant */
466
467 /* Check startBlk is valid (but allow case of zero blocks...) */
468 Assert(startBlk == 0 || startBlk < scan->rs_nblocks);
469
470 scan->rs_startblock = startBlk;
471 scan->rs_numblocks = numBlks;
472}
BlockNumber rs_startblock
Definition: heapam.h:60
BlockNumber rs_numblocks
Definition: heapam.h:61

References Assert(), HeapScanDescData::rs_base, TableScanDescData::rs_flags, HeapScanDescData::rs_inited, HeapScanDescData::rs_numblocks, HeapScanDescData::rs_startblock, and SO_ALLOW_SYNC.

Referenced by heap_set_tidrange(), and heapam_index_build_range_scan().

◆ heap_tuple_needs_eventual_freeze()

bool heap_tuple_needs_eventual_freeze ( HeapTupleHeader  tuple)

Definition at line 7766 of file heapam.c.

7767{
7768 TransactionId xid;
7769
7770 /*
7771 * If xmin is a normal transaction ID, this tuple is definitely not
7772 * frozen.
7773 */
7774 xid = HeapTupleHeaderGetXmin(tuple);
7775 if (TransactionIdIsNormal(xid))
7776 return true;
7777
7778 /*
7779 * If xmax is a valid xact or multixact, this tuple is also not frozen.
7780 */
7781 if (tuple->t_infomask & HEAP_XMAX_IS_MULTI)
7782 {
7783 MultiXactId multi;
7784
7785 multi = HeapTupleHeaderGetRawXmax(tuple);
7786 if (MultiXactIdIsValid(multi))
7787 return true;
7788 }
7789 else
7790 {
7791 xid = HeapTupleHeaderGetRawXmax(tuple);
7792 if (TransactionIdIsNormal(xid))
7793 return true;
7794 }
7795
7796 if (tuple->t_infomask & HEAP_MOVED)
7797 {
7798 xid = HeapTupleHeaderGetXvac(tuple);
7799 if (TransactionIdIsNormal(xid))
7800 return true;
7801 }
7802
7803 return false;
7804}

References HEAP_MOVED, HEAP_XMAX_IS_MULTI, HeapTupleHeaderGetRawXmax(), HeapTupleHeaderGetXmin(), HeapTupleHeaderGetXvac(), MultiXactIdIsValid, HeapTupleHeaderData::t_infomask, and TransactionIdIsNormal.

Referenced by collect_corrupt_items(), and heap_page_is_all_visible().

◆ heap_tuple_should_freeze()

bool heap_tuple_should_freeze ( HeapTupleHeader  tuple,
const struct VacuumCutoffs cutoffs,
TransactionId NoFreezePageRelfrozenXid,
MultiXactId NoFreezePageRelminMxid 
)

Definition at line 7821 of file heapam.c.

7825{
7826 TransactionId xid;
7827 MultiXactId multi;
7828 bool freeze = false;
7829
7830 /* First deal with xmin */
7831 xid = HeapTupleHeaderGetXmin(tuple);
7832 if (TransactionIdIsNormal(xid))
7833 {
7835 if (TransactionIdPrecedes(xid, *NoFreezePageRelfrozenXid))
7836 *NoFreezePageRelfrozenXid = xid;
7837 if (TransactionIdPrecedes(xid, cutoffs->FreezeLimit))
7838 freeze = true;
7839 }
7840
7841 /* Now deal with xmax */
7843 multi = InvalidMultiXactId;
7844 if (tuple->t_infomask & HEAP_XMAX_IS_MULTI)
7845 multi = HeapTupleHeaderGetRawXmax(tuple);
7846 else
7847 xid = HeapTupleHeaderGetRawXmax(tuple);
7848
7849 if (TransactionIdIsNormal(xid))
7850 {
7852 /* xmax is a non-permanent XID */
7853 if (TransactionIdPrecedes(xid, *NoFreezePageRelfrozenXid))
7854 *NoFreezePageRelfrozenXid = xid;
7855 if (TransactionIdPrecedes(xid, cutoffs->FreezeLimit))
7856 freeze = true;
7857 }
7858 else if (!MultiXactIdIsValid(multi))
7859 {
7860 /* xmax is a permanent XID or invalid MultiXactId/XID */
7861 }
7862 else if (HEAP_LOCKED_UPGRADED(tuple->t_infomask))
7863 {
7864 /* xmax is a pg_upgrade'd MultiXact, which can't have updater XID */
7865 if (MultiXactIdPrecedes(multi, *NoFreezePageRelminMxid))
7866 *NoFreezePageRelminMxid = multi;
7867 /* heap_prepare_freeze_tuple always freezes pg_upgrade'd xmax */
7868 freeze = true;
7869 }
7870 else
7871 {
7872 /* xmax is a MultiXactId that may have an updater XID */
7873 MultiXactMember *members;
7874 int nmembers;
7875
7877 if (MultiXactIdPrecedes(multi, *NoFreezePageRelminMxid))
7878 *NoFreezePageRelminMxid = multi;
7879 if (MultiXactIdPrecedes(multi, cutoffs->MultiXactCutoff))
7880 freeze = true;
7881
7882 /* need to check whether any member of the mxact is old */
7883 nmembers = GetMultiXactIdMembers(multi, &members, false,
7885
7886 for (int i = 0; i < nmembers; i++)
7887 {
7888 xid = members[i].xid;
7890 if (TransactionIdPrecedes(xid, *NoFreezePageRelfrozenXid))
7891 *NoFreezePageRelfrozenXid = xid;
7892 if (TransactionIdPrecedes(xid, cutoffs->FreezeLimit))
7893 freeze = true;
7894 }
7895 if (nmembers > 0)
7896 pfree(members);
7897 }
7898
7899 if (tuple->t_infomask & HEAP_MOVED)
7900 {
7901 xid = HeapTupleHeaderGetXvac(tuple);
7902 if (TransactionIdIsNormal(xid))
7903 {
7905 if (TransactionIdPrecedes(xid, *NoFreezePageRelfrozenXid))
7906 *NoFreezePageRelfrozenXid = xid;
7907 /* heap_prepare_freeze_tuple forces xvac freezing */
7908 freeze = true;
7909 }
7910 }
7911
7912 return freeze;
7913}
bool MultiXactIdPrecedesOrEquals(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3331
#define InvalidMultiXactId
Definition: multixact.h:24

References Assert(), VacuumCutoffs::FreezeLimit, GetMultiXactIdMembers(), HEAP_LOCKED_UPGRADED(), HEAP_MOVED, HEAP_XMAX_IS_LOCKED_ONLY(), HEAP_XMAX_IS_MULTI, HeapTupleHeaderGetRawXmax(), HeapTupleHeaderGetXmin(), HeapTupleHeaderGetXvac(), i, InvalidMultiXactId, InvalidTransactionId, VacuumCutoffs::MultiXactCutoff, MultiXactIdIsValid, MultiXactIdPrecedes(), MultiXactIdPrecedesOrEquals(), pfree(), VacuumCutoffs::relfrozenxid, VacuumCutoffs::relminmxid, HeapTupleHeaderData::t_infomask, TransactionIdIsNormal, TransactionIdPrecedes(), TransactionIdPrecedesOrEquals(), and MultiXactMember::xid.

Referenced by heap_prepare_freeze_tuple(), and lazy_scan_noprune().

◆ heap_update()

TM_Result heap_update ( Relation  relation,
ItemPointer  otid,
HeapTuple  newtup,
CommandId  cid,
Snapshot  crosscheck,
bool  wait,
TM_FailureData tmfd,
LockTupleMode lockmode,
TU_UpdateIndexes update_indexes 
)

Definition at line 3212 of file heapam.c.

3216{
3217 TM_Result result;
3219 Bitmapset *hot_attrs;
3220 Bitmapset *sum_attrs;
3221 Bitmapset *key_attrs;
3222 Bitmapset *id_attrs;
3223 Bitmapset *interesting_attrs;
3224 Bitmapset *modified_attrs;
3225 ItemId lp;
3226 HeapTupleData oldtup;
3227 HeapTuple heaptup;
3228 HeapTuple old_key_tuple = NULL;
3229 bool old_key_copied = false;
3230 Page page;
3231 BlockNumber block;
3232 MultiXactStatus mxact_status;
3233 Buffer buffer,
3234 newbuf,
3235 vmbuffer = InvalidBuffer,
3236 vmbuffer_new = InvalidBuffer;
3237 bool need_toast;
3238 Size newtupsize,
3239 pagefree;
3240 bool have_tuple_lock = false;
3241 bool iscombo;
3242 bool use_hot_update = false;
3243 bool summarized_update = false;
3244 bool key_intact;
3245 bool all_visible_cleared = false;
3246 bool all_visible_cleared_new = false;
3247 bool checked_lockers;
3248 bool locker_remains;
3249 bool id_has_external = false;
3250 TransactionId xmax_new_tuple,
3251 xmax_old_tuple;
3252 uint16 infomask_old_tuple,
3253 infomask2_old_tuple,
3254 infomask_new_tuple,
3255 infomask2_new_tuple;
3256
3258
3259 /* Cheap, simplistic check that the tuple matches the rel's rowtype. */
3262
3263 /*
3264 * Forbid this during a parallel operation, lest it allocate a combo CID.
3265 * Other workers might need that combo CID for visibility checks, and we
3266 * have no provision for broadcasting it to them.
3267 */
3268 if (IsInParallelMode())
3269 ereport(ERROR,
3270 (errcode(ERRCODE_INVALID_TRANSACTION_STATE),
3271 errmsg("cannot update tuples during a parallel operation")));
3272
3273#ifdef USE_ASSERT_CHECKING
3274 check_lock_if_inplace_updateable_rel(relation, otid, newtup);
3275#endif
3276
3277 /*
3278 * Fetch the list of attributes to be checked for various operations.
3279 *
3280 * For HOT considerations, this is wasted effort if we fail to update or
3281 * have to put the new tuple on a different page. But we must compute the
3282 * list before obtaining buffer lock --- in the worst case, if we are
3283 * doing an update on one of the relevant system catalogs, we could
3284 * deadlock if we try to fetch the list later. In any case, the relcache
3285 * caches the data so this is usually pretty cheap.
3286 *
3287 * We also need columns used by the replica identity and columns that are
3288 * considered the "key" of rows in the table.
3289 *
3290 * Note that we get copies of each bitmap, so we need not worry about
3291 * relcache flush happening midway through.
3292 */
3293 hot_attrs = RelationGetIndexAttrBitmap(relation,
3295 sum_attrs = RelationGetIndexAttrBitmap(relation,
3298 id_attrs = RelationGetIndexAttrBitmap(relation,
3300 interesting_attrs = NULL;
3301 interesting_attrs = bms_add_members(interesting_attrs, hot_attrs);
3302 interesting_attrs = bms_add_members(interesting_attrs, sum_attrs);
3303 interesting_attrs = bms_add_members(interesting_attrs, key_attrs);
3304 interesting_attrs = bms_add_members(interesting_attrs, id_attrs);
3305
3306 block = ItemPointerGetBlockNumber(otid);
3307 INJECTION_POINT("heap_update-before-pin");
3308 buffer = ReadBuffer(relation, block);
3309 page = BufferGetPage(buffer);
3310
3311 /*
3312 * Before locking the buffer, pin the visibility map page if it appears to
3313 * be necessary. Since we haven't got the lock yet, someone else might be
3314 * in the middle of changing this, so we'll need to recheck after we have
3315 * the lock.
3316 */
3317 if (PageIsAllVisible(page))
3318 visibilitymap_pin(relation, block, &vmbuffer);
3319
3321
3322 lp = PageGetItemId(page, ItemPointerGetOffsetNumber(otid));
3323
3324 /*
3325 * Usually, a buffer pin and/or snapshot blocks pruning of otid, ensuring
3326 * we see LP_NORMAL here. When the otid origin is a syscache, we may have
3327 * neither a pin nor a snapshot. Hence, we may see other LP_ states, each
3328 * of which indicates concurrent pruning.
3329 *
3330 * Failing with TM_Updated would be most accurate. However, unlike other
3331 * TM_Updated scenarios, we don't know the successor ctid in LP_UNUSED and
3332 * LP_DEAD cases. While the distinction between TM_Updated and TM_Deleted
3333 * does matter to SQL statements UPDATE and MERGE, those SQL statements
3334 * hold a snapshot that ensures LP_NORMAL. Hence, the choice between
3335 * TM_Updated and TM_Deleted affects only the wording of error messages.
3336 * Settle on TM_Deleted, for two reasons. First, it avoids complicating
3337 * the specification of when tmfd->ctid is valid. Second, it creates
3338 * error log evidence that we took this branch.
3339 *
3340 * Since it's possible to see LP_UNUSED at otid, it's also possible to see
3341 * LP_NORMAL for a tuple that replaced LP_UNUSED. If it's a tuple for an
3342 * unrelated row, we'll fail with "duplicate key value violates unique".
3343 * XXX if otid is the live, newer version of the newtup row, we'll discard
3344 * changes originating in versions of this catalog row after the version
3345 * the caller got from syscache. See syscache-update-pruned.spec.
3346 */
3347 if (!ItemIdIsNormal(lp))
3348 {
3350
3351 UnlockReleaseBuffer(buffer);
3352 Assert(!have_tuple_lock);
3353 if (vmbuffer != InvalidBuffer)
3354 ReleaseBuffer(vmbuffer);
3355 tmfd->ctid = *otid;
3356 tmfd->xmax = InvalidTransactionId;
3357 tmfd->cmax = InvalidCommandId;
3358 *update_indexes = TU_None;
3359
3360 bms_free(hot_attrs);
3361 bms_free(sum_attrs);
3362 bms_free(key_attrs);
3363 bms_free(id_attrs);
3364 /* modified_attrs not yet initialized */
3365 bms_free(interesting_attrs);
3366 return TM_Deleted;
3367 }
3368
3369 /*
3370 * Fill in enough data in oldtup for HeapDetermineColumnsInfo to work
3371 * properly.
3372 */
3373 oldtup.t_tableOid = RelationGetRelid(relation);
3374 oldtup.t_data = (HeapTupleHeader) PageGetItem(page, lp);
3375 oldtup.t_len = ItemIdGetLength(lp);
3376 oldtup.t_self = *otid;
3377
3378 /* the new tuple is ready, except for this: */
3379 newtup->t_tableOid = RelationGetRelid(relation);
3380
3381 /*
3382 * Determine columns modified by the update. Additionally, identify
3383 * whether any of the unmodified replica identity key attributes in the
3384 * old tuple is externally stored or not. This is required because for
3385 * such attributes the flattened value won't be WAL logged as part of the
3386 * new tuple so we must include it as part of the old_key_tuple. See
3387 * ExtractReplicaIdentity.
3388 */
3389 modified_attrs = HeapDetermineColumnsInfo(relation, interesting_attrs,
3390 id_attrs, &oldtup,
3391 newtup, &id_has_external);
3392
3393 /*
3394 * If we're not updating any "key" column, we can grab a weaker lock type.
3395 * This allows for more concurrency when we are running simultaneously
3396 * with foreign key checks.
3397 *
3398 * Note that if a column gets detoasted while executing the update, but
3399 * the value ends up being the same, this test will fail and we will use
3400 * the stronger lock. This is acceptable; the important case to optimize
3401 * is updates that don't manipulate key columns, not those that
3402 * serendipitously arrive at the same key values.
3403 */
3404 if (!bms_overlap(modified_attrs, key_attrs))
3405 {
3406 *lockmode = LockTupleNoKeyExclusive;
3407 mxact_status = MultiXactStatusNoKeyUpdate;
3408 key_intact = true;
3409
3410 /*
3411 * If this is the first possibly-multixact-able operation in the
3412 * current transaction, set my per-backend OldestMemberMXactId
3413 * setting. We can be certain that the transaction will never become a
3414 * member of any older MultiXactIds than that. (We have to do this
3415 * even if we end up just using our own TransactionId below, since
3416 * some other backend could incorporate our XID into a MultiXact
3417 * immediately afterwards.)
3418 */
3420 }
3421 else
3422 {
3423 *lockmode = LockTupleExclusive;
3424 mxact_status = MultiXactStatusUpdate;
3425 key_intact = false;
3426 }
3427
3428 /*
3429 * Note: beyond this point, use oldtup not otid to refer to old tuple.
3430 * otid may very well point at newtup->t_self, which we will overwrite
3431 * with the new tuple's location, so there's great risk of confusion if we
3432 * use otid anymore.
3433 */
3434
3435l2:
3436 checked_lockers = false;
3437 locker_remains = false;
3438 result = HeapTupleSatisfiesUpdate(&oldtup, cid, buffer);
3439
3440 /* see below about the "no wait" case */
3441 Assert(result != TM_BeingModified || wait);
3442
3443 if (result == TM_Invisible)
3444 {
3445 UnlockReleaseBuffer(buffer);
3446 ereport(ERROR,
3447 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
3448 errmsg("attempted to update invisible tuple")));
3449 }
3450 else if (result == TM_BeingModified && wait)
3451 {
3452 TransactionId xwait;
3453 uint16 infomask;
3454 bool can_continue = false;
3455
3456 /*
3457 * XXX note that we don't consider the "no wait" case here. This
3458 * isn't a problem currently because no caller uses that case, but it
3459 * should be fixed if such a caller is introduced. It wasn't a
3460 * problem previously because this code would always wait, but now
3461 * that some tuple locks do not conflict with one of the lock modes we
3462 * use, it is possible that this case is interesting to handle
3463 * specially.
3464 *
3465 * This may cause failures with third-party code that calls
3466 * heap_update directly.
3467 */
3468
3469 /* must copy state data before unlocking buffer */
3470 xwait = HeapTupleHeaderGetRawXmax(oldtup.t_data);
3471 infomask = oldtup.t_data->t_infomask;
3472
3473 /*
3474 * Now we have to do something about the existing locker. If it's a
3475 * multi, sleep on it; we might be awakened before it is completely
3476 * gone (or even not sleep at all in some cases); we need to preserve
3477 * it as locker, unless it is gone completely.
3478 *
3479 * If it's not a multi, we need to check for sleeping conditions
3480 * before actually going to sleep. If the update doesn't conflict
3481 * with the locks, we just continue without sleeping (but making sure
3482 * it is preserved).
3483 *
3484 * Before sleeping, we need to acquire tuple lock to establish our
3485 * priority for the tuple (see heap_lock_tuple). LockTuple will
3486 * release us when we are next-in-line for the tuple. Note we must
3487 * not acquire the tuple lock until we're sure we're going to sleep;
3488 * otherwise we're open for race conditions with other transactions
3489 * holding the tuple lock which sleep on us.
3490 *
3491 * If we are forced to "start over" below, we keep the tuple lock;
3492 * this arranges that we stay at the head of the line while rechecking
3493 * tuple state.
3494 */
3495 if (infomask & HEAP_XMAX_IS_MULTI)
3496 {
3497 TransactionId update_xact;
3498 int remain;
3499 bool current_is_member = false;
3500
3501 if (DoesMultiXactIdConflict((MultiXactId) xwait, infomask,
3502 *lockmode, &current_is_member))
3503 {
3505
3506 /*
3507 * Acquire the lock, if necessary (but skip it when we're
3508 * requesting a lock and already have one; avoids deadlock).
3509 */
3510 if (!current_is_member)
3511 heap_acquire_tuplock(relation, &(oldtup.t_self), *lockmode,
3512 LockWaitBlock, &have_tuple_lock);
3513
3514 /* wait for multixact */
3515 MultiXactIdWait((MultiXactId) xwait, mxact_status, infomask,
3516 relation, &oldtup.t_self, XLTW_Update,
3517 &remain);
3518 checked_lockers = true;
3519 locker_remains = remain != 0;
3521
3522 /*
3523 * If xwait had just locked the tuple then some other xact
3524 * could update this tuple before we get to this point. Check
3525 * for xmax change, and start over if so.
3526 */
3528 infomask) ||
3530 xwait))
3531 goto l2;
3532 }
3533
3534 /*
3535 * Note that the multixact may not be done by now. It could have
3536 * surviving members; our own xact or other subxacts of this
3537 * backend, and also any other concurrent transaction that locked
3538 * the tuple with LockTupleKeyShare if we only got
3539 * LockTupleNoKeyExclusive. If this is the case, we have to be
3540 * careful to mark the updated tuple with the surviving members in
3541 * Xmax.
3542 *
3543 * Note that there could have been another update in the
3544 * MultiXact. In that case, we need to check whether it committed
3545 * or aborted. If it aborted we are safe to update it again;
3546 * otherwise there is an update conflict, and we have to return
3547 * TableTuple{Deleted, Updated} below.
3548 *
3549 * In the LockTupleExclusive case, we still need to preserve the
3550 * surviving members: those would include the tuple locks we had
3551 * before this one, which are important to keep in case this
3552 * subxact aborts.
3553 */
3555 update_xact = HeapTupleGetUpdateXid(oldtup.t_data);
3556 else
3557 update_xact = InvalidTransactionId;
3558
3559 /*
3560 * There was no UPDATE in the MultiXact; or it aborted. No
3561 * TransactionIdIsInProgress() call needed here, since we called
3562 * MultiXactIdWait() above.
3563 */
3564 if (!TransactionIdIsValid(update_xact) ||
3565 TransactionIdDidAbort(update_xact))
3566 can_continue = true;
3567 }
3569 {
3570 /*
3571 * The only locker is ourselves; we can avoid grabbing the tuple
3572 * lock here, but must preserve our locking information.
3573 */
3574 checked_lockers = true;
3575 locker_remains = true;
3576 can_continue = true;
3577 }
3578 else if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask) && key_intact)
3579 {
3580 /*
3581 * If it's just a key-share locker, and we're not changing the key
3582 * columns, we don't need to wait for it to end; but we need to
3583 * preserve it as locker.
3584 */
3585 checked_lockers = true;
3586 locker_remains = true;
3587 can_continue = true;
3588 }
3589 else
3590 {
3591 /*
3592 * Wait for regular transaction to end; but first, acquire tuple
3593 * lock.
3594 */
3596 heap_acquire_tuplock(relation, &(oldtup.t_self), *lockmode,
3597 LockWaitBlock, &have_tuple_lock);
3598 XactLockTableWait(xwait, relation, &oldtup.t_self,
3599 XLTW_Update);
3600 checked_lockers = true;
3602
3603 /*
3604 * xwait is done, but if xwait had just locked the tuple then some
3605 * other xact could update this tuple before we get to this point.
3606 * Check for xmax change, and start over if so.
3607 */
3608 if (xmax_infomask_changed(oldtup.t_data->t_infomask, infomask) ||
3609 !TransactionIdEquals(xwait,
3611 goto l2;
3612
3613 /* Otherwise check if it committed or aborted */
3614 UpdateXmaxHintBits(oldtup.t_data, buffer, xwait);
3615 if (oldtup.t_data->t_infomask & HEAP_XMAX_INVALID)
3616 can_continue = true;
3617 }
3618
3619 if (can_continue)
3620 result = TM_Ok;
3621 else if (!ItemPointerEquals(&oldtup.t_self, &oldtup.t_data->t_ctid))
3622 result = TM_Updated;
3623 else
3624 result = TM_Deleted;
3625 }
3626
3627 /* Sanity check the result HeapTupleSatisfiesUpdate() and the logic above */
3628 if (result != TM_Ok)
3629 {
3630 Assert(result == TM_SelfModified ||
3631 result == TM_Updated ||
3632 result == TM_Deleted ||
3633 result == TM_BeingModified);
3635 Assert(result != TM_Updated ||
3636 !ItemPointerEquals(&oldtup.t_self, &oldtup.t_data->t_ctid));
3637 }
3638
3639 if (crosscheck != InvalidSnapshot && result == TM_Ok)
3640 {
3641 /* Perform additional check for transaction-snapshot mode RI updates */
3642 if (!HeapTupleSatisfiesVisibility(&oldtup, crosscheck, buffer))
3643 result = TM_Updated;
3644 }
3645
3646 if (result != TM_Ok)
3647 {
3648 tmfd->ctid = oldtup.t_data->t_ctid;
3649 tmfd->xmax = HeapTupleHeaderGetUpdateXid(oldtup.t_data);
3650 if (result == TM_SelfModified)
3651 tmfd->cmax = HeapTupleHeaderGetCmax(oldtup.t_data);
3652 else
3653 tmfd->cmax = InvalidCommandId;
3654 UnlockReleaseBuffer(buffer);
3655 if (have_tuple_lock)
3656 UnlockTupleTuplock(relation, &(oldtup.t_self), *lockmode);
3657 if (vmbuffer != InvalidBuffer)
3658 ReleaseBuffer(vmbuffer);
3659 *update_indexes = TU_None;
3660
3661 bms_free(hot_attrs);
3662 bms_free(sum_attrs);
3663 bms_free(key_attrs);
3664 bms_free(id_attrs);
3665 bms_free(modified_attrs);
3666 bms_free(interesting_attrs);
3667 return result;
3668 }
3669
3670 /*
3671 * If we didn't pin the visibility map page and the page has become all
3672 * visible while we were busy locking the buffer, or during some
3673 * subsequent window during which we had it unlocked, we'll have to unlock
3674 * and re-lock, to avoid holding the buffer lock across an I/O. That's a
3675 * bit unfortunate, especially since we'll now have to recheck whether the
3676 * tuple has been locked or updated under us, but hopefully it won't
3677 * happen very often.
3678 */
3679 if (vmbuffer == InvalidBuffer && PageIsAllVisible(page))
3680 {
3682 visibilitymap_pin(relation, block, &vmbuffer);
3684 goto l2;
3685 }
3686
3687 /* Fill in transaction status data */
3688
3689 /*
3690 * If the tuple we're updating is locked, we need to preserve the locking
3691 * info in the old tuple's Xmax. Prepare a new Xmax value for this.
3692 */
3694 oldtup.t_data->t_infomask,
3695 oldtup.t_data->t_infomask2,
3696 xid, *lockmode, true,
3697 &xmax_old_tuple, &infomask_old_tuple,
3698 &infomask2_old_tuple);
3699
3700 /*
3701 * And also prepare an Xmax value for the new copy of the tuple. If there
3702 * was no xmax previously, or there was one but all lockers are now gone,
3703 * then use InvalidTransactionId; otherwise, get the xmax from the old
3704 * tuple. (In rare cases that might also be InvalidTransactionId and yet
3705 * not have the HEAP_XMAX_INVALID bit set; that's fine.)
3706 */
3707 if ((oldtup.t_data->t_infomask & HEAP_XMAX_INVALID) ||
3709 (checked_lockers && !locker_remains))
3710 xmax_new_tuple = InvalidTransactionId;
3711 else
3712 xmax_new_tuple = HeapTupleHeaderGetRawXmax(oldtup.t_data);
3713
3714 if (!TransactionIdIsValid(xmax_new_tuple))
3715 {
3716 infomask_new_tuple = HEAP_XMAX_INVALID;
3717 infomask2_new_tuple = 0;
3718 }
3719 else
3720 {
3721 /*
3722 * If we found a valid Xmax for the new tuple, then the infomask bits
3723 * to use on the new tuple depend on what was there on the old one.
3724 * Note that since we're doing an update, the only possibility is that
3725 * the lockers had FOR KEY SHARE lock.
3726 */
3727 if (oldtup.t_data->t_infomask & HEAP_XMAX_IS_MULTI)
3728 {
3729 GetMultiXactIdHintBits(xmax_new_tuple, &infomask_new_tuple,
3730 &infomask2_new_tuple);
3731 }
3732 else
3733 {
3734 infomask_new_tuple = HEAP_XMAX_KEYSHR_LOCK | HEAP_XMAX_LOCK_ONLY;
3735 infomask2_new_tuple = 0;
3736 }
3737 }
3738
3739 /*
3740 * Prepare the new tuple with the appropriate initial values of Xmin and
3741 * Xmax, as well as initial infomask bits as computed above.
3742 */
3743 newtup->t_data->t_infomask &= ~(HEAP_XACT_MASK);
3744 newtup->t_data->t_infomask2 &= ~(HEAP2_XACT_MASK);
3745 HeapTupleHeaderSetXmin(newtup->t_data, xid);
3746 HeapTupleHeaderSetCmin(newtup->t_data, cid);
3747 newtup->t_data->t_infomask |= HEAP_UPDATED | infomask_new_tuple;
3748 newtup->t_data->t_infomask2 |= infomask2_new_tuple;
3749 HeapTupleHeaderSetXmax(newtup->t_data, xmax_new_tuple);
3750
3751 /*
3752 * Replace cid with a combo CID if necessary. Note that we already put
3753 * the plain cid into the new tuple.
3754 */
3755 HeapTupleHeaderAdjustCmax(oldtup.t_data, &cid, &iscombo);
3756
3757 /*
3758 * If the toaster needs to be activated, OR if the new tuple will not fit
3759 * on the same page as the old, then we need to release the content lock
3760 * (but not the pin!) on the old tuple's buffer while we are off doing
3761 * TOAST and/or table-file-extension work. We must mark the old tuple to
3762 * show that it's locked, else other processes may try to update it
3763 * themselves.
3764 *
3765 * We need to invoke the toaster if there are already any out-of-line
3766 * toasted values present, or if the new tuple is over-threshold.
3767 */
3768 if (relation->rd_rel->relkind != RELKIND_RELATION &&
3769 relation->rd_rel->relkind != RELKIND_MATVIEW)
3770 {
3771 /* toast table entries should never be recursively toasted */
3772 Assert(!HeapTupleHasExternal(&oldtup));
3773 Assert(!HeapTupleHasExternal(newtup));
3774 need_toast = false;
3775 }
3776 else
3777 need_toast = (HeapTupleHasExternal(&oldtup) ||
3778 HeapTupleHasExternal(newtup) ||
3779 newtup->t_len > TOAST_TUPLE_THRESHOLD);
3780
3781 pagefree = PageGetHeapFreeSpace(page);
3782
3783 newtupsize = MAXALIGN(newtup->t_len);
3784
3785 if (need_toast || newtupsize > pagefree)
3786 {
3787 TransactionId xmax_lock_old_tuple;
3788 uint16 infomask_lock_old_tuple,
3789 infomask2_lock_old_tuple;
3790 bool cleared_all_frozen = false;
3791
3792 /*
3793 * To prevent concurrent sessions from updating the tuple, we have to
3794 * temporarily mark it locked, while we release the page-level lock.
3795 *
3796 * To satisfy the rule that any xid potentially appearing in a buffer
3797 * written out to disk, we unfortunately have to WAL log this
3798 * temporary modification. We can reuse xl_heap_lock for this
3799 * purpose. If we crash/error before following through with the
3800 * actual update, xmax will be of an aborted transaction, allowing
3801 * other sessions to proceed.
3802 */
3803
3804 /*
3805 * Compute xmax / infomask appropriate for locking the tuple. This has
3806 * to be done separately from the combo that's going to be used for
3807 * updating, because the potentially created multixact would otherwise
3808 * be wrong.
3809 */
3811 oldtup.t_data->t_infomask,
3812 oldtup.t_data->t_infomask2,
3813 xid, *lockmode, false,
3814 &xmax_lock_old_tuple, &infomask_lock_old_tuple,
3815 &infomask2_lock_old_tuple);
3816
3817 Assert(HEAP_XMAX_IS_LOCKED_ONLY(infomask_lock_old_tuple));
3818
3820
3821 /* Clear obsolete visibility flags ... */
3823 oldtup.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
3824 HeapTupleClearHotUpdated(&oldtup);
3825 /* ... and store info about transaction updating this tuple */
3826 Assert(TransactionIdIsValid(xmax_lock_old_tuple));
3827 HeapTupleHeaderSetXmax(oldtup.t_data, xmax_lock_old_tuple);
3828 oldtup.t_data->t_infomask |= infomask_lock_old_tuple;
3829 oldtup.t_data->t_infomask2 |= infomask2_lock_old_tuple;
3830 HeapTupleHeaderSetCmax(oldtup.t_data, cid, iscombo);
3831
3832 /* temporarily make it look not-updated, but locked */
3833 oldtup.t_data->t_ctid = oldtup.t_self;
3834
3835 /*
3836 * Clear all-frozen bit on visibility map if needed. We could
3837 * immediately reset ALL_VISIBLE, but given that the WAL logging
3838 * overhead would be unchanged, that doesn't seem necessarily
3839 * worthwhile.
3840 */
3841 if (PageIsAllVisible(page) &&
3842 visibilitymap_clear(relation, block, vmbuffer,
3844 cleared_all_frozen = true;
3845
3846 MarkBufferDirty(buffer);
3847
3848 if (RelationNeedsWAL(relation))
3849 {
3850 xl_heap_lock xlrec;
3851 XLogRecPtr recptr;
3852
3855
3856 xlrec.offnum = ItemPointerGetOffsetNumber(&oldtup.t_self);
3857 xlrec.xmax = xmax_lock_old_tuple;
3859 oldtup.t_data->t_infomask2);
3860 xlrec.flags =
3861 cleared_all_frozen ? XLH_LOCK_ALL_FROZEN_CLEARED : 0;
3863 recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_LOCK);
3864 PageSetLSN(page, recptr);
3865 }
3866
3868
3870
3871 /*
3872 * Let the toaster do its thing, if needed.
3873 *
3874 * Note: below this point, heaptup is the data we actually intend to
3875 * store into the relation; newtup is the caller's original untoasted
3876 * data.
3877 */
3878 if (need_toast)
3879 {
3880 /* Note we always use WAL and FSM during updates */
3881 heaptup = heap_toast_insert_or_update(relation, newtup, &oldtup, 0);
3882 newtupsize = MAXALIGN(heaptup->t_len);
3883 }
3884 else
3885 heaptup = newtup;
3886
3887 /*
3888 * Now, do we need a new page for the tuple, or not? This is a bit
3889 * tricky since someone else could have added tuples to the page while
3890 * we weren't looking. We have to recheck the available space after
3891 * reacquiring the buffer lock. But don't bother to do that if the
3892 * former amount of free space is still not enough; it's unlikely
3893 * there's more free now than before.
3894 *
3895 * What's more, if we need to get a new page, we will need to acquire
3896 * buffer locks on both old and new pages. To avoid deadlock against
3897 * some other backend trying to get the same two locks in the other
3898 * order, we must be consistent about the order we get the locks in.
3899 * We use the rule "lock the lower-numbered page of the relation
3900 * first". To implement this, we must do RelationGetBufferForTuple
3901 * while not holding the lock on the old page, and we must rely on it
3902 * to get the locks on both pages in the correct order.
3903 *
3904 * Another consideration is that we need visibility map page pin(s) if
3905 * we will have to clear the all-visible flag on either page. If we
3906 * call RelationGetBufferForTuple, we rely on it to acquire any such
3907 * pins; but if we don't, we have to handle that here. Hence we need
3908 * a loop.
3909 */
3910 for (;;)
3911 {
3912 if (newtupsize > pagefree)
3913 {
3914 /* It doesn't fit, must use RelationGetBufferForTuple. */
3915 newbuf = RelationGetBufferForTuple(relation, heaptup->t_len,
3916 buffer, 0, NULL,
3917 &vmbuffer_new, &vmbuffer,
3918 0);
3919 /* We're all done. */
3920 break;
3921 }
3922 /* Acquire VM page pin if needed and we don't have it. */
3923 if (vmbuffer == InvalidBuffer && PageIsAllVisible(page))
3924 visibilitymap_pin(relation, block, &vmbuffer);
3925 /* Re-acquire the lock on the old tuple's page. */
3927 /* Re-check using the up-to-date free space */
3928 pagefree = PageGetHeapFreeSpace(page);
3929 if (newtupsize > pagefree ||
3930 (vmbuffer == InvalidBuffer && PageIsAllVisible(page)))
3931 {
3932 /*
3933 * Rats, it doesn't fit anymore, or somebody just now set the
3934 * all-visible flag. We must now unlock and loop to avoid
3935 * deadlock. Fortunately, this path should seldom be taken.
3936 */
3938 }
3939 else
3940 {
3941 /* We're all done. */
3942 newbuf = buffer;
3943 break;
3944 }
3945 }
3946 }
3947 else
3948 {
3949 /* No TOAST work needed, and it'll fit on same page */
3950 newbuf = buffer;
3951 heaptup = newtup;
3952 }
3953
3954 /*
3955 * We're about to do the actual update -- check for conflict first, to
3956 * avoid possibly having to roll back work we've just done.
3957 *
3958 * This is safe without a recheck as long as there is no possibility of
3959 * another process scanning the pages between this check and the update
3960 * being visible to the scan (i.e., exclusive buffer content lock(s) are
3961 * continuously held from this point until the tuple update is visible).
3962 *
3963 * For the new tuple the only check needed is at the relation level, but
3964 * since both tuples are in the same relation and the check for oldtup
3965 * will include checking the relation level, there is no benefit to a
3966 * separate check for the new tuple.
3967 */
3968 CheckForSerializableConflictIn(relation, &oldtup.t_self,
3969 BufferGetBlockNumber(buffer));
3970
3971 /*
3972 * At this point newbuf and buffer are both pinned and locked, and newbuf
3973 * has enough space for the new tuple. If they are the same buffer, only
3974 * one pin is held.
3975 */
3976
3977 if (newbuf == buffer)
3978 {
3979 /*
3980 * Since the new tuple is going into the same page, we might be able
3981 * to do a HOT update. Check if any of the index columns have been
3982 * changed.
3983 */
3984 if (!bms_overlap(modified_attrs, hot_attrs))
3985 {
3986 use_hot_update = true;
3987
3988 /*
3989 * If none of the columns that are used in hot-blocking indexes
3990 * were updated, we can apply HOT, but we do still need to check
3991 * if we need to update the summarizing indexes, and update those
3992 * indexes if the columns were updated, or we may fail to detect
3993 * e.g. value bound changes in BRIN minmax indexes.
3994 */
3995 if (bms_overlap(modified_attrs, sum_attrs))
3996 summarized_update = true;
3997 }
3998 }
3999 else
4000 {
4001 /* Set a hint that the old page could use prune/defrag */
4002 PageSetFull(page);
4003 }
4004
4005 /*
4006 * Compute replica identity tuple before entering the critical section so
4007 * we don't PANIC upon a memory allocation failure.
4008 * ExtractReplicaIdentity() will return NULL if nothing needs to be
4009 * logged. Pass old key required as true only if the replica identity key
4010 * columns are modified or it has external data.
4011 */
4012 old_key_tuple = ExtractReplicaIdentity(relation, &oldtup,
4013 bms_overlap(modified_attrs, id_attrs) ||
4014 id_has_external,
4015 &old_key_copied);
4016
4017 /* NO EREPORT(ERROR) from here till changes are logged */
4019
4020 /*
4021 * If this transaction commits, the old tuple will become DEAD sooner or
4022 * later. Set flag that this page is a candidate for pruning once our xid
4023 * falls below the OldestXmin horizon. If the transaction finally aborts,
4024 * the subsequent page pruning will be a no-op and the hint will be
4025 * cleared.
4026 *
4027 * XXX Should we set hint on newbuf as well? If the transaction aborts,
4028 * there would be a prunable tuple in the newbuf; but for now we choose
4029 * not to optimize for aborts. Note that heap_xlog_update must be kept in
4030 * sync if this decision changes.
4031 */
4032 PageSetPrunable(page, xid);
4033
4034 if (use_hot_update)
4035 {
4036 /* Mark the old tuple as HOT-updated */
4037 HeapTupleSetHotUpdated(&oldtup);
4038 /* And mark the new tuple as heap-only */
4039 HeapTupleSetHeapOnly(heaptup);
4040 /* Mark the caller's copy too, in case different from heaptup */
4041 HeapTupleSetHeapOnly(newtup);
4042 }
4043 else
4044 {
4045 /* Make sure tuples are correctly marked as not-HOT */
4046 HeapTupleClearHotUpdated(&oldtup);
4047 HeapTupleClearHeapOnly(heaptup);
4048 HeapTupleClearHeapOnly(newtup);
4049 }
4050
4051 RelationPutHeapTuple(relation, newbuf, heaptup, false); /* insert new tuple */
4052
4053
4054 /* Clear obsolete visibility flags, possibly set by ourselves above... */
4056 oldtup.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
4057 /* ... and store info about transaction updating this tuple */
4058 Assert(TransactionIdIsValid(xmax_old_tuple));
4059 HeapTupleHeaderSetXmax(oldtup.t_data, xmax_old_tuple);
4060 oldtup.t_data->t_infomask |= infomask_old_tuple;
4061 oldtup.t_data->t_infomask2 |= infomask2_old_tuple;
4062 HeapTupleHeaderSetCmax(oldtup.t_data, cid, iscombo);
4063
4064 /* record address of new tuple in t_ctid of old one */
4065 oldtup.t_data->t_ctid = heaptup->t_self;
4066
4067 /* clear PD_ALL_VISIBLE flags, reset all visibilitymap bits */
4068 if (PageIsAllVisible(BufferGetPage(buffer)))
4069 {
4070 all_visible_cleared = true;
4072 visibilitymap_clear(relation, BufferGetBlockNumber(buffer),
4073 vmbuffer, VISIBILITYMAP_VALID_BITS);
4074 }
4075 if (newbuf != buffer && PageIsAllVisible(BufferGetPage(newbuf)))
4076 {
4077 all_visible_cleared_new = true;
4079 visibilitymap_clear(relation, BufferGetBlockNumber(newbuf),
4080 vmbuffer_new, VISIBILITYMAP_VALID_BITS);
4081 }
4082
4083 if (newbuf != buffer)
4084 MarkBufferDirty(newbuf);
4085 MarkBufferDirty(buffer);
4086
4087 /* XLOG stuff */
4088 if (RelationNeedsWAL(relation))
4089 {
4090 XLogRecPtr recptr;
4091
4092 /*
4093 * For logical decoding we need combo CIDs to properly decode the
4094 * catalog.
4095 */
4097 {
4098 log_heap_new_cid(relation, &oldtup);
4099 log_heap_new_cid(relation, heaptup);
4100 }
4101
4102 recptr = log_heap_update(relation, buffer,
4103 newbuf, &oldtup, heaptup,
4104 old_key_tuple,
4105 all_visible_cleared,
4106 all_visible_cleared_new);
4107 if (newbuf != buffer)
4108 {
4109 PageSetLSN(BufferGetPage(newbuf), recptr);
4110 }
4111 PageSetLSN(BufferGetPage(buffer), recptr);
4112 }
4113
4115
4116 if (newbuf != buffer)
4119
4120 /*
4121 * Mark old tuple for invalidation from system caches at next command
4122 * boundary, and mark the new tuple for invalidation in case we abort. We
4123 * have to do this before releasing the buffer because oldtup is in the
4124 * buffer. (heaptup is all in local memory, but it's necessary to process
4125 * both tuple versions in one call to inval.c so we can avoid redundant
4126 * sinval messages.)
4127 */
4128 CacheInvalidateHeapTuple(relation, &oldtup, heaptup);
4129
4130 /* Now we can release the buffer(s) */
4131 if (newbuf != buffer)
4132 ReleaseBuffer(newbuf);
4133 ReleaseBuffer(buffer);
4134 if (BufferIsValid(vmbuffer_new))
4135 ReleaseBuffer(vmbuffer_new);
4136 if (BufferIsValid(vmbuffer))
4137 ReleaseBuffer(vmbuffer);
4138
4139 /*
4140 * Release the lmgr tuple lock, if we had it.
4141 */
4142 if (have_tuple_lock)
4143 UnlockTupleTuplock(relation, &(oldtup.t_self), *lockmode);
4144
4145 pgstat_count_heap_update(relation, use_hot_update, newbuf != buffer);
4146
4147 /*
4148 * If heaptup is a private copy, release it. Don't forget to copy t_self
4149 * back to the caller's image, too.
4150 */
4151 if (heaptup != newtup)
4152 {
4153 newtup->t_self = heaptup->t_self;
4154 heap_freetuple(heaptup);
4155 }
4156
4157 /*
4158 * If it is a HOT update, the update may still need to update summarized
4159 * indexes, lest we fail to update those summaries and get incorrect
4160 * results (for example, minmax bounds of the block may change with this
4161 * update).
4162 */
4163 if (use_hot_update)
4164 {
4165 if (summarized_update)
4166 *update_indexes = TU_Summarizing;
4167 else
4168 *update_indexes = TU_None;
4169 }
4170 else
4171 *update_indexes = TU_All;
4172
4173 if (old_key_tuple != NULL && old_key_copied)
4174 heap_freetuple(old_key_tuple);
4175
4176 bms_free(hot_attrs);
4177 bms_free(sum_attrs);
4178 bms_free(key_attrs);
4179 bms_free(id_attrs);
4180 bms_free(modified_attrs);
4181 bms_free(interesting_attrs);
4182
4183 return TM_Ok;
4184}
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:917
bool bms_overlap(const Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:582
static void PageSetFull(Page page)
Definition: bufpage.h:418
static Bitmapset * HeapDetermineColumnsInfo(Relation relation, Bitmapset *interesting_cols, Bitmapset *external_cols, HeapTuple oldtup, HeapTuple newtup, bool *has_external)
Definition: heapam.c:4364
static XLogRecPtr log_heap_update(Relation reln, Buffer oldbuf, Buffer newbuf, HeapTuple oldtup, HeapTuple newtup, HeapTuple old_key_tuple, bool all_visible_cleared, bool new_all_visible_cleared)
Definition: heapam.c:8794
TransactionId HeapTupleGetUpdateXid(const HeapTupleHeaderData *tup)
Definition: heapam.c:7535
static void HeapTupleClearHotUpdated(const HeapTupleData *tuple)
Definition: htup_details.h:780
static void HeapTupleSetHeapOnly(const HeapTupleData *tuple)
Definition: htup_details.h:792
static void HeapTupleSetHotUpdated(const HeapTupleData *tuple)
Definition: htup_details.h:774
static void HeapTupleClearHeapOnly(const HeapTupleData *tuple)
Definition: htup_details.h:798
#define HEAP_UPDATED
Definition: htup_details.h:210
#define INJECTION_POINT(name)
void pgstat_count_heap_update(Relation rel, bool hot, bool newpage)
@ INDEX_ATTR_BITMAP_KEY
Definition: relcache.h:61
@ INDEX_ATTR_BITMAP_HOT_BLOCKING
Definition: relcache.h:64
@ INDEX_ATTR_BITMAP_SUMMARIZED
Definition: relcache.h:65
bool RelationSupportsSysCache(Oid relid)
Definition: syscache.c:770
@ TU_Summarizing
Definition: tableam.h:118
@ TU_All
Definition: tableam.h:115
@ TU_None
Definition: tableam.h:112

References Assert(), bms_add_members(), bms_free(), bms_overlap(), BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage(), BufferIsValid(), CacheInvalidateHeapTuple(), CheckForSerializableConflictIn(), TM_FailureData::cmax, compute_infobits(), compute_new_xmax_infomask(), TM_FailureData::ctid, DoesMultiXactIdConflict(), END_CRIT_SECTION, ereport, errcode(), errmsg(), ERROR, ExtractReplicaIdentity(), xl_heap_lock::flags, GetCurrentTransactionId(), GetMultiXactIdHintBits(), HEAP2_XACT_MASK, heap_acquire_tuplock(), heap_freetuple(), HEAP_LOCKED_UPGRADED(), HEAP_MOVED, heap_toast_insert_or_update(), HEAP_UPDATED, HEAP_XACT_MASK, HEAP_XMAX_BITS, HEAP_XMAX_INVALID, HEAP_XMAX_IS_KEYSHR_LOCKED(), HEAP_XMAX_IS_LOCKED_ONLY(), HEAP_XMAX_IS_MULTI, HEAP_XMAX_KEYSHR_LOCK, HEAP_XMAX_LOCK_ONLY, HeapDetermineColumnsInfo(), HeapTupleClearHeapOnly(), HeapTupleClearHotUpdated(), HeapTupleGetUpdateXid(), HeapTupleHasExternal(), HeapTupleHeaderAdjustCmax(), HeapTupleHeaderGetCmax(), HeapTupleHeaderGetNatts, HeapTupleHeaderGetRawXmax(), HeapTupleHeaderGetUpdateXid(), HeapTupleHeaderSetCmax(), HeapTupleHeaderSetCmin(), HeapTupleHeaderSetXmax(), HeapTupleHeaderSetXmin(), HeapTupleSatisfiesUpdate(), HeapTupleSatisfiesVisibility(), HeapTupleSetHeapOnly(), HeapTupleSetHotUpdated(), INDEX_ATTR_BITMAP_HOT_BLOCKING, INDEX_ATTR_BITMAP_IDENTITY_KEY, INDEX_ATTR_BITMAP_KEY, INDEX_ATTR_BITMAP_SUMMARIZED, xl_heap_lock::infobits_set, INJECTION_POINT, InvalidBuffer, InvalidCommandId, InvalidSnapshot, InvalidTransactionId, IsInParallelMode(), ItemIdGetLength, ItemIdIsNormal, ItemPointerEquals(), ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), ItemPointerIsValid(), LockBuffer(), LockTupleExclusive, LockTupleNoKeyExclusive, LockWaitBlock, log_heap_new_cid(), log_heap_update(), MarkBufferDirty(), MAXALIGN, MultiXactIdSetOldestMember(), MultiXactIdWait(), MultiXactStatusNoKeyUpdate, MultiXactStatusUpdate, xl_heap_lock::offnum, PageClearAllVisible(), PageGetHeapFreeSpace(), PageGetItem(), PageGetItemId(), PageIsAllVisible(), PageSetFull(), PageSetLSN(), PageSetPrunable, pgstat_count_heap_update(), RelationData::rd_rel, ReadBuffer(), REGBUF_STANDARD, RelationGetBufferForTuple(), RelationGetIndexAttrBitmap(), RelationGetNumberOfAttributes, RelationGetRelid, RelationIsAccessibleInLogicalDecoding, RelationNeedsWAL, RelationPutHeapTuple(), RelationSupportsSysCache(), ReleaseBuffer(), SizeOfHeapLock, START_CRIT_SECTION, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleHeaderData::t_infomask, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TM_BeingModified, TM_Deleted, TM_Invisible, TM_Ok, TM_SelfModified, TM_Updated, TOAST_TUPLE_THRESHOLD, TransactionIdDidAbort(), TransactionIdEquals, TransactionIdIsCurrentTransactionId(), TransactionIdIsValid, TU_All, TU_None, TU_Summarizing, UnlockReleaseBuffer(), UnlockTupleTuplock, UpdateXmaxHintBits(), VISIBILITYMAP_ALL_FROZEN, visibilitymap_clear(), visibilitymap_pin(), VISIBILITYMAP_VALID_BITS, XactLockTableWait(), XLH_LOCK_ALL_FROZEN_CLEARED, XLOG_HEAP_LOCK, XLogBeginInsert(), XLogInsert(), XLogRegisterBuffer(), XLogRegisterData(), XLTW_Update, xl_heap_lock::xmax, TM_FailureData::xmax, and xmax_infomask_changed().

Referenced by heapam_tuple_update(), and simple_heap_update().

◆ HeapCheckForSerializableConflictOut()

void HeapCheckForSerializableConflictOut ( bool  visible,
Relation  relation,
HeapTuple  tuple,
Buffer  buffer,
Snapshot  snapshot 
)

Definition at line 9201 of file heapam.c.

9204{
9205 TransactionId xid;
9206 HTSV_Result htsvResult;
9207
9208 if (!CheckForSerializableConflictOutNeeded(relation, snapshot))
9209 return;
9210
9211 /*
9212 * Check to see whether the tuple has been written to by a concurrent
9213 * transaction, either to create it not visible to us, or to delete it
9214 * while it is visible to us. The "visible" bool indicates whether the
9215 * tuple is visible to us, while HeapTupleSatisfiesVacuum checks what else
9216 * is going on with it.
9217 *
9218 * In the event of a concurrently inserted tuple that also happens to have
9219 * been concurrently updated (by a separate transaction), the xmin of the
9220 * tuple will be used -- not the updater's xid.
9221 */
9222 htsvResult = HeapTupleSatisfiesVacuum(tuple, TransactionXmin, buffer);
9223 switch (htsvResult)
9224 {
9225 case HEAPTUPLE_LIVE:
9226 if (visible)
9227 return;
9228 xid = HeapTupleHeaderGetXmin(tuple->t_data);
9229 break;
9232 if (visible)
9233 xid = HeapTupleHeaderGetUpdateXid(tuple->t_data);
9234 else
9235 xid = HeapTupleHeaderGetXmin(tuple->t_data);
9236
9238 {
9239 /* This is like the HEAPTUPLE_DEAD case */
9240 Assert(!visible);
9241 return;
9242 }
9243 break;
9245 xid = HeapTupleHeaderGetXmin(tuple->t_data);
9246 break;
9247 case HEAPTUPLE_DEAD:
9248 Assert(!visible);
9249 return;
9250 default:
9251
9252 /*
9253 * The only way to get to this default clause is if a new value is
9254 * added to the enum type without adding it to this switch
9255 * statement. That's a bug, so elog.
9256 */
9257 elog(ERROR, "unrecognized return value from HeapTupleSatisfiesVacuum: %u", htsvResult);
9258
9259 /*
9260 * In spite of having all enum values covered and calling elog on
9261 * this default, some compilers think this is a code path which
9262 * allows xid to be used below without initialization. Silence
9263 * that warning.
9264 */
9266 }
9267
9270
9271 /*
9272 * Find top level xid. Bail out if xid is too early to be a conflict, or
9273 * if it's our own xid.
9274 */
9276 return;
9279 return;
9280
9281 CheckForSerializableConflictOut(relation, xid, snapshot);
9282}
HTSV_Result
Definition: heapam.h:123
@ HEAPTUPLE_RECENTLY_DEAD
Definition: heapam.h:126
@ HEAPTUPLE_INSERT_IN_PROGRESS
Definition: heapam.h:127
@ HEAPTUPLE_LIVE
Definition: heapam.h:125
@ HEAPTUPLE_DELETE_IN_PROGRESS
Definition: heapam.h:128
@ HEAPTUPLE_DEAD
Definition: heapam.h:124
HTSV_Result HeapTupleSatisfiesVacuum(HeapTuple htup, TransactionId OldestXmin, Buffer buffer)
void CheckForSerializableConflictOut(Relation relation, TransactionId xid, Snapshot snapshot)
Definition: predicate.c:4023
TransactionId SubTransGetTopmostTransaction(TransactionId xid)
Definition: subtrans.c:163
bool TransactionIdFollowsOrEquals(TransactionId id1, TransactionId id2)
Definition: transam.c:329
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:441

References Assert(), CheckForSerializableConflictOut(), CheckForSerializableConflictOutNeeded(), elog, ERROR, GetTopTransactionIdIfAny(), HEAPTUPLE_DEAD, HEAPTUPLE_DELETE_IN_PROGRESS, HEAPTUPLE_INSERT_IN_PROGRESS, HEAPTUPLE_LIVE, HEAPTUPLE_RECENTLY_DEAD, HeapTupleHeaderGetUpdateXid(), HeapTupleHeaderGetXmin(), HeapTupleSatisfiesVacuum(), InvalidTransactionId, SubTransGetTopmostTransaction(), HeapTupleData::t_data, TransactionIdEquals, TransactionIdFollowsOrEquals(), TransactionIdIsValid, TransactionIdPrecedes(), and TransactionXmin.

Referenced by BitmapHeapScanNextBlock(), heap_fetch(), heap_get_latest_tid(), heap_hot_search_buffer(), heapam_scan_sample_next_tuple(), heapgettup(), and page_collect_tuples().

◆ HeapDetermineColumnsInfo()

static Bitmapset * HeapDetermineColumnsInfo ( Relation  relation,
Bitmapset interesting_cols,
Bitmapset external_cols,
HeapTuple  oldtup,
HeapTuple  newtup,
bool *  has_external 
)
static

Definition at line 4364 of file heapam.c.

4369{
4370 int attidx;
4371 Bitmapset *modified = NULL;
4372 TupleDesc tupdesc = RelationGetDescr(relation);
4373
4374 attidx = -1;
4375 while ((attidx = bms_next_member(interesting_cols, attidx)) >= 0)
4376 {
4377 /* attidx is zero-based, attrnum is the normal attribute number */
4379 Datum value1,
4380 value2;
4381 bool isnull1,
4382 isnull2;
4383
4384 /*
4385 * If it's a whole-tuple reference, say "not equal". It's not really
4386 * worth supporting this case, since it could only succeed after a
4387 * no-op update, which is hardly a case worth optimizing for.
4388 */
4389 if (attrnum == 0)
4390 {
4391 modified = bms_add_member(modified, attidx);
4392 continue;
4393 }
4394
4395 /*
4396 * Likewise, automatically say "not equal" for any system attribute
4397 * other than tableOID; we cannot expect these to be consistent in a
4398 * HOT chain, or even to be set correctly yet in the new tuple.
4399 */
4400 if (attrnum < 0)
4401 {
4402 if (attrnum != TableOidAttributeNumber)
4403 {
4404 modified = bms_add_member(modified, attidx);
4405 continue;
4406 }
4407 }
4408
4409 /*
4410 * Extract the corresponding values. XXX this is pretty inefficient
4411 * if there are many indexed columns. Should we do a single
4412 * heap_deform_tuple call on each tuple, instead? But that doesn't
4413 * work for system columns ...
4414 */
4415 value1 = heap_getattr(oldtup, attrnum, tupdesc, &isnull1);
4416 value2 = heap_getattr(newtup, attrnum, tupdesc, &isnull2);
4417
4418 if (!heap_attr_equals(tupdesc, attrnum, value1,
4419 value2, isnull1, isnull2))
4420 {
4421 modified = bms_add_member(modified, attidx);
4422 continue;
4423 }
4424
4425 /*
4426 * No need to check attributes that can't be stored externally. Note
4427 * that system attributes can't be stored externally.
4428 */
4429 if (attrnum < 0 || isnull1 ||
4430 TupleDescCompactAttr(tupdesc, attrnum - 1)->attlen != -1)
4431 continue;
4432
4433 /*
4434 * Check if the old tuple's attribute is stored externally and is a
4435 * member of external_cols.
4436 */
4437 if (VARATT_IS_EXTERNAL((struct varlena *) DatumGetPointer(value1)) &&
4438 bms_is_member(attidx, external_cols))
4439 *has_external = true;
4440 }
4441
4442 return modified;
4443}
int16 AttrNumber
Definition: attnum.h:21
int bms_next_member(const Bitmapset *a, int prevbit)
Definition: bitmapset.c:1306
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:815
static bool heap_attr_equals(TupleDesc tupdesc, int attrnum, Datum value1, Datum value2, bool isnull1, bool isnull2)
Definition: heapam.c:4313
static Datum heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
Definition: htup_details.h:904
int16 attlen
Definition: pg_attribute.h:59
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
Definition: c.h:658
#define TableOidAttributeNumber
Definition: sysattr.h:26
#define VARATT_IS_EXTERNAL(PTR)
Definition: varatt.h:289

References attlen, bms_add_member(), bms_is_member(), bms_next_member(), DatumGetPointer(), FirstLowInvalidHeapAttributeNumber, heap_attr_equals(), heap_getattr(), RelationGetDescr, TableOidAttributeNumber, TupleDescCompactAttr(), and VARATT_IS_EXTERNAL.

Referenced by heap_update().

◆ heapgettup()

static void heapgettup ( HeapScanDesc  scan,
ScanDirection  dir,
int  nkeys,
ScanKey  key 
)
static

Definition at line 875 of file heapam.c.

879{
880 HeapTuple tuple = &(scan->rs_ctup);
881 Page page;
882 OffsetNumber lineoff;
883 int linesleft;
884
885 if (likely(scan->rs_inited))
886 {
887 /* continue from previously returned page/tuple */
889 page = heapgettup_continue_page(scan, dir, &linesleft, &lineoff);
890 goto continue_page;
891 }
892
893 /*
894 * advance the scan until we find a qualifying tuple or run out of stuff
895 * to scan
896 */
897 while (true)
898 {
899 heap_fetch_next_buffer(scan, dir);
900
901 /* did we run out of blocks to scan? */
902 if (!BufferIsValid(scan->rs_cbuf))
903 break;
904
906
908 page = heapgettup_start_page(scan, dir, &linesleft, &lineoff);
909continue_page:
910
911 /*
912 * Only continue scanning the page while we have lines left.
913 *
914 * Note that this protects us from accessing line pointers past
915 * PageGetMaxOffsetNumber(); both for forward scans when we resume the
916 * table scan, and for when we start scanning a new page.
917 */
918 for (; linesleft > 0; linesleft--, lineoff += dir)
919 {
920 bool visible;
921 ItemId lpp = PageGetItemId(page, lineoff);
922
923 if (!ItemIdIsNormal(lpp))
924 continue;
925
926 tuple->t_data = (HeapTupleHeader) PageGetItem(page, lpp);
927 tuple->t_len = ItemIdGetLength(lpp);
928 ItemPointerSet(&(tuple->t_self), scan->rs_cblock, lineoff);
929
930 visible = HeapTupleSatisfiesVisibility(tuple,
931 scan->rs_base.rs_snapshot,
932 scan->rs_cbuf);
933
935 tuple, scan->rs_cbuf,
936 scan->rs_base.rs_snapshot);
937
938 /* skip tuples not visible to this snapshot */
939 if (!visible)
940 continue;
941
942 /* skip any tuples that don't match the scan key */
943 if (key != NULL &&
945 nkeys, key))
946 continue;
947
949 scan->rs_coffset = lineoff;
950 return;
951 }
952
953 /*
954 * if we get here, it means we've exhausted the items on this page and
955 * it's time to move to the next.
956 */
958 }
959
960 /* end of scan */
961 if (BufferIsValid(scan->rs_cbuf))
962 ReleaseBuffer(scan->rs_cbuf);
963
964 scan->rs_cbuf = InvalidBuffer;
967 tuple->t_data = NULL;
968 scan->rs_inited = false;
969}
static void heap_fetch_next_buffer(HeapScanDesc scan, ScanDirection dir)
Definition: heapam.c:622
static Page heapgettup_continue_page(HeapScanDesc scan, ScanDirection dir, int *linesleft, OffsetNumber *lineoff)
Definition: heapam.c:745
static Page heapgettup_start_page(HeapScanDesc scan, ScanDirection dir, int *linesleft, OffsetNumber *lineoff)
Definition: heapam.c:714
OffsetNumber rs_coffset
Definition: heapam.h:66
static bool HeapKeyTest(HeapTuple tuple, TupleDesc tupdesc, int nkeys, ScanKey keys)
Definition: valid.h:28

References Assert(), BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferIsValid(), heap_fetch_next_buffer(), HeapCheckForSerializableConflictOut(), heapgettup_continue_page(), heapgettup_start_page(), HeapKeyTest(), HeapTupleSatisfiesVisibility(), InvalidBlockNumber, InvalidBuffer, ItemIdGetLength, ItemIdIsNormal, ItemPointerSet(), sort-test::key, likely, LockBuffer(), PageGetItem(), PageGetItemId(), RelationGetDescr, ReleaseBuffer(), HeapScanDescData::rs_base, HeapScanDescData::rs_cblock, HeapScanDescData::rs_cbuf, HeapScanDescData::rs_coffset, HeapScanDescData::rs_ctup, HeapScanDescData::rs_inited, HeapScanDescData::rs_prefetch_block, TableScanDescData::rs_rd, TableScanDescData::rs_snapshot, HeapTupleData::t_data, HeapTupleData::t_len, and HeapTupleData::t_self.

Referenced by heap_getnext(), heap_getnextslot(), and heap_getnextslot_tidrange().

◆ heapgettup_advance_block()

static BlockNumber heapgettup_advance_block ( HeapScanDesc  scan,
BlockNumber  block,
ScanDirection  dir 
)
inlinestatic

Definition at line 791 of file heapam.c.

792{
793 Assert(scan->rs_base.rs_parallel == NULL);
794
796 {
797 block++;
798
799 /* wrap back to the start of the heap */
800 if (block >= scan->rs_nblocks)
801 block = 0;
802
803 /*
804 * Report our new scan position for synchronization purposes. We don't
805 * do that when moving backwards, however. That would just mess up any
806 * other forward-moving scanners.
807 *
808 * Note: we do this before checking for end of scan so that the final
809 * state of the position hint is back at the start of the rel. That's
810 * not strictly necessary, but otherwise when you run the same query
811 * multiple times the starting position would shift a little bit
812 * backwards on every invocation, which is confusing. We don't
813 * guarantee any specific ordering in general, though.
814 */
815 if (scan->rs_base.rs_flags & SO_ALLOW_SYNC)
816 ss_report_location(scan->rs_base.rs_rd, block);
817
818 /* we're done if we're back at where we started */
819 if (block == scan->rs_startblock)
820 return InvalidBlockNumber;
821
822 /* check if the limit imposed by heap_setscanlimits() is met */
823 if (scan->rs_numblocks != InvalidBlockNumber)
824 {
825 if (--scan->rs_numblocks == 0)
826 return InvalidBlockNumber;
827 }
828
829 return block;
830 }
831 else
832 {
833 /* we're done if the last block is the start position */
834 if (block == scan->rs_startblock)
835 return InvalidBlockNumber;
836
837 /* check if the limit imposed by heap_setscanlimits() is met */
838 if (scan->rs_numblocks != InvalidBlockNumber)
839 {
840 if (--scan->rs_numblocks == 0)
841 return InvalidBlockNumber;
842 }
843
844 /* wrap to the end of the heap when the last page was page 0 */
845 if (block == 0)
846 block = scan->rs_nblocks;
847
848 block--;
849
850 return block;
851 }
852}
void ss_report_location(Relation rel, BlockNumber location)
Definition: syncscan.c:289

References Assert(), InvalidBlockNumber, likely, HeapScanDescData::rs_base, TableScanDescData::rs_flags, HeapScanDescData::rs_nblocks, HeapScanDescData::rs_numblocks, TableScanDescData::rs_parallel, TableScanDescData::rs_rd, HeapScanDescData::rs_startblock, ScanDirectionIsForward, SO_ALLOW_SYNC, and ss_report_location().

Referenced by heap_scan_stream_read_next_serial().

◆ heapgettup_continue_page()

static Page heapgettup_continue_page ( HeapScanDesc  scan,
ScanDirection  dir,
int *  linesleft,
OffsetNumber lineoff 
)
inlinestatic

Definition at line 745 of file heapam.c.

747{
748 Page page;
749
750 Assert(scan->rs_inited);
752
753 /* Caller is responsible for ensuring buffer is locked if needed */
754 page = BufferGetPage(scan->rs_cbuf);
755
756 if (ScanDirectionIsForward(dir))
757 {
758 *lineoff = OffsetNumberNext(scan->rs_coffset);
759 *linesleft = PageGetMaxOffsetNumber(page) - (*lineoff) + 1;
760 }
761 else
762 {
763 /*
764 * The previous returned tuple may have been vacuumed since the
765 * previous scan when we use a non-MVCC snapshot, so we must
766 * re-establish the lineoff <= PageGetMaxOffsetNumber(page) invariant
767 */
768 *lineoff = Min(PageGetMaxOffsetNumber(page), OffsetNumberPrev(scan->rs_coffset));
769 *linesleft = *lineoff;
770 }
771
772 /* lineoff now references the physically previous or next tid */
773 return page;
774}
#define OffsetNumberNext(offsetNumber)
Definition: off.h:52
#define OffsetNumberPrev(offsetNumber)
Definition: off.h:54

References Assert(), BufferGetPage(), BufferIsValid(), Min, OffsetNumberNext, OffsetNumberPrev, PageGetMaxOffsetNumber(), HeapScanDescData::rs_cbuf, HeapScanDescData::rs_coffset, HeapScanDescData::rs_inited, and ScanDirectionIsForward.

Referenced by heapgettup().

◆ heapgettup_initial_block()

static pg_noinline BlockNumber heapgettup_initial_block ( HeapScanDesc  scan,
ScanDirection  dir 
)
static

Definition at line 667 of file heapam.c.

668{
669 Assert(!scan->rs_inited);
670 Assert(scan->rs_base.rs_parallel == NULL);
671
672 /* When there are no pages to scan, return InvalidBlockNumber */
673 if (scan->rs_nblocks == 0 || scan->rs_numblocks == 0)
674 return InvalidBlockNumber;
675
676 if (ScanDirectionIsForward(dir))
677 {
678 return scan->rs_startblock;
679 }
680 else
681 {
682 /*
683 * Disable reporting to syncscan logic in a backwards scan; it's not
684 * very likely anyone else is doing the same thing at the same time,
685 * and much more likely that we'll just bollix things for forward
686 * scanners.
687 */
688 scan->rs_base.rs_flags &= ~SO_ALLOW_SYNC;
689
690 /*
691 * Start from last page of the scan. Ensure we take into account
692 * rs_numblocks if it's been adjusted by heap_setscanlimits().
693 */
694 if (scan->rs_numblocks != InvalidBlockNumber)
695 return (scan->rs_startblock + scan->rs_numblocks - 1) % scan->rs_nblocks;
696
697 if (scan->rs_startblock > 0)
698 return scan->rs_startblock - 1;
699
700 return scan->rs_nblocks - 1;
701 }
702}

References Assert(), InvalidBlockNumber, HeapScanDescData::rs_base, TableScanDescData::rs_flags, HeapScanDescData::rs_inited, HeapScanDescData::rs_nblocks, HeapScanDescData::rs_numblocks, TableScanDescData::rs_parallel, HeapScanDescData::rs_startblock, and ScanDirectionIsForward.

Referenced by heap_scan_stream_read_next_serial().

◆ heapgettup_pagemode()

static void heapgettup_pagemode ( HeapScanDesc  scan,
ScanDirection  dir,
int  nkeys,
ScanKey  key 
)
static

Definition at line 985 of file heapam.c.

989{
990 HeapTuple tuple = &(scan->rs_ctup);
991 Page page;
992 uint32 lineindex;
993 uint32 linesleft;
994
995 if (likely(scan->rs_inited))
996 {
997 /* continue from previously returned page/tuple */
998 page = BufferGetPage(scan->rs_cbuf);
999
1000 lineindex = scan->rs_cindex + dir;
1001 if (ScanDirectionIsForward(dir))
1002 linesleft = scan->rs_ntuples - lineindex;
1003 else
1004 linesleft = scan->rs_cindex;
1005 /* lineindex now references the next or previous visible tid */
1006
1007 goto continue_page;
1008 }
1009
1010 /*
1011 * advance the scan until we find a qualifying tuple or run out of stuff
1012 * to scan
1013 */
1014 while (true)
1015 {
1016 heap_fetch_next_buffer(scan, dir);
1017
1018 /* did we run out of blocks to scan? */
1019 if (!BufferIsValid(scan->rs_cbuf))
1020 break;
1021
1023
1024 /* prune the page and determine visible tuple offsets */
1026 page = BufferGetPage(scan->rs_cbuf);
1027 linesleft = scan->rs_ntuples;
1028 lineindex = ScanDirectionIsForward(dir) ? 0 : linesleft - 1;
1029
1030 /* block is the same for all tuples, set it once outside the loop */
1032
1033 /* lineindex now references the next or previous visible tid */
1034continue_page:
1035
1036 for (; linesleft > 0; linesleft--, lineindex += dir)
1037 {
1038 ItemId lpp;
1039 OffsetNumber lineoff;
1040
1041 Assert(lineindex <= scan->rs_ntuples);
1042 lineoff = scan->rs_vistuples[lineindex];
1043 lpp = PageGetItemId(page, lineoff);
1044 Assert(ItemIdIsNormal(lpp));
1045
1046 tuple->t_data = (HeapTupleHeader) PageGetItem(page, lpp);
1047 tuple->t_len = ItemIdGetLength(lpp);
1048 ItemPointerSetOffsetNumber(&tuple->t_self, lineoff);
1049
1050 /* skip any tuples that don't match the scan key */
1051 if (key != NULL &&
1053 nkeys, key))
1054 continue;
1055
1056 scan->rs_cindex = lineindex;
1057 return;
1058 }
1059 }
1060
1061 /* end of scan */
1062 if (BufferIsValid(scan->rs_cbuf))
1063 ReleaseBuffer(scan->rs_cbuf);
1064 scan->rs_cbuf = InvalidBuffer;
1067 tuple->t_data = NULL;
1068 scan->rs_inited = false;
1069}
void heap_prepare_pagescan(TableScanDesc sscan)
Definition: heapam.c:531
static void ItemPointerSetBlockNumber(ItemPointerData *pointer, BlockNumber blockNumber)
Definition: itemptr.h:147
OffsetNumber rs_vistuples[MaxHeapTuplesPerPage]
Definition: heapam.h:98
uint32 rs_cindex
Definition: heapam.h:96

References Assert(), BufferGetBlockNumber(), BufferGetPage(), BufferIsValid(), heap_fetch_next_buffer(), heap_prepare_pagescan(), HeapKeyTest(), InvalidBlockNumber, InvalidBuffer, ItemIdGetLength, ItemIdIsNormal, ItemPointerSetBlockNumber(), ItemPointerSetOffsetNumber(), sort-test::key, likely, PageGetItem(), PageGetItemId(), RelationGetDescr, ReleaseBuffer(), HeapScanDescData::rs_cblock, HeapScanDescData::rs_cbuf, HeapScanDescData::rs_cindex, HeapScanDescData::rs_ctup, HeapScanDescData::rs_inited, HeapScanDescData::rs_ntuples, HeapScanDescData::rs_prefetch_block, TableScanDescData::rs_rd, ScanDirectionIsForward, HeapTupleData::t_data, HeapTupleData::t_len, and HeapTupleData::t_self.

Referenced by heap_getnext(), heap_getnextslot(), and heap_getnextslot_tidrange().

◆ heapgettup_start_page()

static Page heapgettup_start_page ( HeapScanDesc  scan,
ScanDirection  dir,
int *  linesleft,
OffsetNumber lineoff 
)
static

Definition at line 714 of file heapam.c.

716{
717 Page page;
718
719 Assert(scan->rs_inited);
721
722 /* Caller is responsible for ensuring buffer is locked if needed */
723 page = BufferGetPage(scan->rs_cbuf);
724
725 *linesleft = PageGetMaxOffsetNumber(page) - FirstOffsetNumber + 1;
726
727 if (ScanDirectionIsForward(dir))
728 *lineoff = FirstOffsetNumber;
729 else
730 *lineoff = (OffsetNumber) (*linesleft);
731
732 /* lineoff now references the physically previous or next tid */
733 return page;
734}

References Assert(), BufferGetPage(), BufferIsValid(), FirstOffsetNumber, PageGetMaxOffsetNumber(), HeapScanDescData::rs_cbuf, HeapScanDescData::rs_inited, and ScanDirectionIsForward.

Referenced by heapgettup().

◆ HeapTupleGetUpdateXid()

◆ HeapTupleHeaderAdvanceConflictHorizon()

void HeapTupleHeaderAdvanceConflictHorizon ( HeapTupleHeader  tuple,
TransactionId snapshotConflictHorizon 
)

Definition at line 7929 of file heapam.c.

7931{
7935
7936 if (tuple->t_infomask & HEAP_MOVED)
7937 {
7938 if (TransactionIdPrecedes(*snapshotConflictHorizon, xvac))
7939 *snapshotConflictHorizon = xvac;
7940 }
7941
7942 /*
7943 * Ignore tuples inserted by an aborted transaction or if the tuple was
7944 * updated/deleted by the inserting transaction.
7945 *
7946 * Look for a committed hint bit, or if no xmin bit is set, check clog.
7947 */
7948 if (HeapTupleHeaderXminCommitted(tuple) ||
7950 {
7951 if (xmax != xmin &&
7952 TransactionIdFollows(xmax, *snapshotConflictHorizon))
7953 *snapshotConflictHorizon = xmax;
7954 }
7955}
static bool HeapTupleHeaderXminInvalid(const HeapTupleHeaderData *tup)
Definition: htup_details.h:343
static bool HeapTupleHeaderXminCommitted(const HeapTupleHeaderData *tup)
Definition: htup_details.h:337
bool TransactionIdFollows(TransactionId id1, TransactionId id2)
Definition: transam.c:314

References HEAP_MOVED, HeapTupleHeaderGetUpdateXid(), HeapTupleHeaderGetXmin(), HeapTupleHeaderGetXvac(), HeapTupleHeaderXminCommitted(), HeapTupleHeaderXminInvalid(), HeapTupleHeaderData::t_infomask, TransactionIdDidCommit(), TransactionIdFollows(), and TransactionIdPrecedes().

Referenced by heap_index_delete_tuples(), heap_page_prune_and_freeze(), and heap_prune_chain().

◆ index_delete_check_htid()

static void index_delete_check_htid ( TM_IndexDeleteOp delstate,
Page  page,
OffsetNumber  maxoff,
ItemPointer  htid,
TM_IndexStatus istatus 
)
inlinestatic

Definition at line 8014 of file heapam.c.

8017{
8018 OffsetNumber indexpagehoffnum = ItemPointerGetOffsetNumber(htid);
8019 ItemId iid;
8020
8022
8023 if (unlikely(indexpagehoffnum > maxoff))
8024 ereport(ERROR,
8025 (errcode(ERRCODE_INDEX_CORRUPTED),
8026 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\"",
8028 indexpagehoffnum,
8029 istatus->idxoffnum, delstate->iblknum,
8030 RelationGetRelationName(delstate->irel))));
8031
8032 iid = PageGetItemId(page, indexpagehoffnum);
8033 if (unlikely(!ItemIdIsUsed(iid)))
8034 ereport(ERROR,
8035 (errcode(ERRCODE_INDEX_CORRUPTED),
8036 errmsg_internal("heap tid from index tuple (%u,%u) points to unused heap page item at offset %u of block %u in index \"%s\"",
8038 indexpagehoffnum,
8039 istatus->idxoffnum, delstate->iblknum,
8040 RelationGetRelationName(delstate->irel))));
8041
8042 if (ItemIdHasStorage(iid))
8043 {
8044 HeapTupleHeader htup;
8045
8046 Assert(ItemIdIsNormal(iid));
8047 htup = (HeapTupleHeader) PageGetItem(page, iid);
8048
8050 ereport(ERROR,
8051 (errcode(ERRCODE_INDEX_CORRUPTED),
8052 errmsg_internal("heap tid from index tuple (%u,%u) points to heap-only tuple at offset %u of block %u in index \"%s\"",
8054 indexpagehoffnum,
8055 istatus->idxoffnum, delstate->iblknum,
8056 RelationGetRelationName(delstate->irel))));
8057 }
8058}
#define ItemIdIsUsed(itemId)
Definition: itemid.h:92
#define ItemIdHasStorage(itemId)
Definition: itemid.h:120
#define OffsetNumberIsValid(offsetNumber)
Definition: off.h:39
Relation irel
Definition: tableam.h:240
BlockNumber iblknum
Definition: tableam.h:241
OffsetNumber idxoffnum
Definition: tableam.h:212

References Assert(), ereport, errcode(), errmsg_internal(), ERROR, HeapTupleHeaderIsHeapOnly(), TM_IndexDeleteOp::iblknum, TM_IndexStatus::idxoffnum, TM_IndexDeleteOp::irel, ItemIdHasStorage, ItemIdIsNormal, ItemIdIsUsed, ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), OffsetNumberIsValid, PageGetItem(), PageGetItemId(), RelationGetRelationName, and unlikely.

Referenced by heap_index_delete_tuples().

◆ index_delete_sort()

static void index_delete_sort ( TM_IndexDeleteOp delstate)
static

Definition at line 8419 of file heapam.c.

8420{
8421 TM_IndexDelete *deltids = delstate->deltids;
8422 int ndeltids = delstate->ndeltids;
8423
8424 /*
8425 * Shellsort gap sequence (taken from Sedgewick-Incerpi paper).
8426 *
8427 * This implementation is fast with array sizes up to ~4500. This covers
8428 * all supported BLCKSZ values.
8429 */
8430 const int gaps[9] = {1968, 861, 336, 112, 48, 21, 7, 3, 1};
8431
8432 /* Think carefully before changing anything here -- keep swaps cheap */
8433 StaticAssertDecl(sizeof(TM_IndexDelete) <= 8,
8434 "element size exceeds 8 bytes");
8435
8436 for (int g = 0; g < lengthof(gaps); g++)
8437 {
8438 for (int hi = gaps[g], i = hi; i < ndeltids; i++)
8439 {
8440 TM_IndexDelete d = deltids[i];
8441 int j = i;
8442
8443 while (j >= hi && index_delete_sort_cmp(&deltids[j - hi], &d) >= 0)
8444 {
8445 deltids[j] = deltids[j - hi];
8446 j -= hi;
8447 }
8448 deltids[j] = d;
8449 }
8450 }
8451}
#define lengthof(array)
Definition: c.h:759
#define StaticAssertDecl(condition, errmessage)
Definition: c.h:907
static int index_delete_sort_cmp(TM_IndexDelete *deltid1, TM_IndexDelete *deltid2)
Definition: heapam.c:8383
int j
Definition: isn.c:78

References TM_IndexDeleteOp::deltids, i, index_delete_sort_cmp(), j, lengthof, TM_IndexDeleteOp::ndeltids, and StaticAssertDecl.

Referenced by heap_index_delete_tuples().

◆ index_delete_sort_cmp()

static int index_delete_sort_cmp ( TM_IndexDelete deltid1,
TM_IndexDelete deltid2 
)
inlinestatic

Definition at line 8383 of file heapam.c.

8384{
8385 ItemPointer tid1 = &deltid1->tid;
8386 ItemPointer tid2 = &deltid2->tid;
8387
8388 {
8391
8392 if (blk1 != blk2)
8393 return (blk1 < blk2) ? -1 : 1;
8394 }
8395 {
8398
8399 if (pos1 != pos2)
8400 return (pos1 < pos2) ? -1 : 1;
8401 }
8402
8403 Assert(false);
8404
8405 return 0;
8406}

References Assert(), ItemPointerGetBlockNumber(), ItemPointerGetOffsetNumber(), and TM_IndexDelete::tid.

Referenced by index_delete_sort().

◆ initscan()

static void initscan ( HeapScanDesc  scan,
ScanKey  key,
bool  keep_startblock 
)
static

Definition at line 329 of file heapam.c.

330{
331 ParallelBlockTableScanDesc bpscan = NULL;
332 bool allow_strat;
333 bool allow_sync;
334
335 /*
336 * Determine the number of blocks we have to scan.
337 *
338 * It is sufficient to do this once at scan start, since any tuples added
339 * while the scan is in progress will be invisible to my snapshot anyway.
340 * (That is not true when using a non-MVCC snapshot. However, we couldn't
341 * guarantee to return tuples added after scan start anyway, since they
342 * might go into pages we already scanned. To guarantee consistent
343 * results for a non-MVCC snapshot, the caller must hold some higher-level
344 * lock that ensures the interesting tuple(s) won't change.)
345 */
346 if (scan->rs_base.rs_parallel != NULL)
347 {
349 scan->rs_nblocks = bpscan->phs_nblocks;
350 }
351 else
353
354 /*
355 * If the table is large relative to NBuffers, use a bulk-read access
356 * strategy and enable synchronized scanning (see syncscan.c). Although
357 * the thresholds for these features could be different, we make them the
358 * same so that there are only two behaviors to tune rather than four.
359 * (However, some callers need to be able to disable one or both of these
360 * behaviors, independently of the size of the table; also there is a GUC
361 * variable that can disable synchronized scanning.)
362 *
363 * Note that table_block_parallelscan_initialize has a very similar test;
364 * if you change this, consider changing that one, too.
365 */
367 scan->rs_nblocks > NBuffers / 4)
368 {
369 allow_strat = (scan->rs_base.rs_flags & SO_ALLOW_STRAT) != 0;
370 allow_sync = (scan->rs_base.rs_flags & SO_ALLOW_SYNC) != 0;
371 }
372 else
373 allow_strat = allow_sync = false;
374
375 if (allow_strat)
376 {
377 /* During a rescan, keep the previous strategy object. */
378 if (scan->rs_strategy == NULL)
380 }
381 else
382 {
383 if (scan->rs_strategy != NULL)
385 scan->rs_strategy = NULL;
386 }
387
388 if (scan->rs_base.rs_parallel != NULL)
389 {
390 /* For parallel scan, believe whatever ParallelTableScanDesc says. */
393 else
394 scan->rs_base.rs_flags &= ~SO_ALLOW_SYNC;
395 }
396 else if (keep_startblock)
397 {
398 /*
399 * When rescanning, we want to keep the previous startblock setting,
400 * so that rewinding a cursor doesn't generate surprising results.
401 * Reset the active syncscan setting, though.
402 */
403 if (allow_sync && synchronize_seqscans)
405 else
406 scan->rs_base.rs_flags &= ~SO_ALLOW_SYNC;
407 }
408 else if (allow_sync && synchronize_seqscans)
409 {
412 }
413 else
414 {
415 scan->rs_base.rs_flags &= ~SO_ALLOW_SYNC;
416 scan->rs_startblock = 0;
417 }
418
420 scan->rs_inited = false;
421 scan->rs_ctup.t_data = NULL;
423 scan->rs_cbuf = InvalidBuffer;
425 scan->rs_ntuples = 0;
426 scan->rs_cindex = 0;
427
428 /*
429 * Initialize to ForwardScanDirection because it is most common and
430 * because heap scans go forward before going backward (e.g. CURSORs).
431 */
434
435 /* page-at-a-time fields are always invalid when not rs_inited */
436
437 /*
438 * copy the scan key, if appropriate
439 */
440 if (key != NULL && scan->rs_base.rs_nkeys > 0)
441 memcpy(scan->rs_base.rs_key, key, scan->rs_base.rs_nkeys * sizeof(ScanKeyData));
442
443 /*
444 * Currently, we only have a stats counter for sequential heap scans (but
445 * e.g for bitmap scans the underlying bitmap index scans will be counted,
446 * and for sample scans we update stats for tuple fetches).
447 */
448 if (scan->rs_base.rs_flags & SO_TYPE_SEQSCAN)
450}
@ BAS_BULKREAD
Definition: bufmgr.h:37
#define RelationGetNumberOfBlocks(reln)
Definition: bufmgr.h:280
int NBuffers
Definition: globals.c:143
static void ItemPointerSetInvalid(ItemPointerData *pointer)
Definition: itemptr.h:184
#define pgstat_count_heap_scan(rel)
Definition: pgstat.h:679
#define RelationUsesLocalBuffers(relation)
Definition: rel.h:648
struct ParallelBlockTableScanDescData * ParallelBlockTableScanDesc
Definition: relscan.h:102
@ ForwardScanDirection
Definition: sdir.h:28
BlockNumber ss_get_location(Relation rel, BlockNumber relnblocks)
Definition: syncscan.c:254
bool synchronize_seqscans
Definition: tableam.c:50

References BAS_BULKREAD, ForwardScanDirection, FreeAccessStrategy(), GetAccessStrategy(), InvalidBlockNumber, InvalidBuffer, ItemPointerSetInvalid(), sort-test::key, NBuffers, pgstat_count_heap_scan, ParallelBlockTableScanDescData::phs_nblocks, ParallelTableScanDescData::phs_syncscan, RelationGetNumberOfBlocks, RelationUsesLocalBuffers, HeapScanDescData::rs_base, HeapScanDescData::rs_cblock, HeapScanDescData::rs_cbuf, HeapScanDescData::rs_cindex, HeapScanDescData::rs_ctup, HeapScanDescData::rs_dir, TableScanDescData::rs_flags, HeapScanDescData::rs_inited, TableScanDescData::rs_key, HeapScanDescData::rs_nblocks, TableScanDescData::rs_nkeys, HeapScanDescData::rs_ntuples, HeapScanDescData::rs_numblocks, TableScanDescData::rs_parallel, HeapScanDescData::rs_prefetch_block, TableScanDescData::rs_rd, HeapScanDescData::rs_startblock, HeapScanDescData::rs_strategy, SO_ALLOW_STRAT, SO_ALLOW_SYNC, SO_TYPE_SEQSCAN, ss_get_location(), synchronize_seqscans, HeapTupleData::t_data, and HeapTupleData::t_self.

Referenced by heap_beginscan(), and heap_rescan().

◆ log_heap_new_cid()

static XLogRecPtr log_heap_new_cid ( Relation  relation,
HeapTuple  tup 
)
static

Definition at line 9016 of file heapam.c.

9017{
9018 xl_heap_new_cid xlrec;
9019
9020 XLogRecPtr recptr;
9021 HeapTupleHeader hdr = tup->t_data;
9022
9024 Assert(tup->t_tableOid != InvalidOid);
9025
9026 xlrec.top_xid = GetTopTransactionId();
9027 xlrec.target_locator = relation->rd_locator;
9028 xlrec.target_tid = tup->t_self;
9029
9030 /*
9031 * If the tuple got inserted & deleted in the same TX we definitely have a
9032 * combo CID, set cmin and cmax.
9033 */
9034 if (hdr->t_infomask & HEAP_COMBOCID)
9035 {
9038 xlrec.cmin = HeapTupleHeaderGetCmin(hdr);
9039 xlrec.cmax = HeapTupleHeaderGetCmax(hdr);
9041 }
9042 /* No combo CID, so only cmin or cmax can be set by this TX */
9043 else
9044 {
9045 /*
9046 * Tuple inserted.
9047 *
9048 * We need to check for LOCK ONLY because multixacts might be
9049 * transferred to the new tuple in case of FOR KEY SHARE updates in
9050 * which case there will be an xmax, although the tuple just got
9051 * inserted.
9052 */
9053 if (hdr->t_infomask & HEAP_XMAX_INVALID ||
9055 {
9057 xlrec.cmax = InvalidCommandId;
9058 }
9059 /* Tuple from a different tx updated or deleted. */
9060 else
9061 {
9062 xlrec.cmin = InvalidCommandId;
9064 }
9065 xlrec.combocid = InvalidCommandId;
9066 }
9067
9068 /*
9069 * Note that we don't need to register the buffer here, because this
9070 * operation does not modify the page. The insert/update/delete that
9071 * called us certainly did, but that's WAL-logged separately.
9072 */
9075
9076 /* will be looked at irrespective of origin */
9077
9078 recptr = XLogInsert(RM_HEAP2_ID, XLOG_HEAP2_NEW_CID);
9079
9080 return recptr;
9081}
CommandId HeapTupleHeaderGetCmin(const HeapTupleHeaderData *tup)
Definition: combocid.c:104
#define SizeOfHeapNewCid
Definition: heapam_xlog.h:470
#define XLOG_HEAP2_NEW_CID
Definition: heapam_xlog.h:66
static CommandId HeapTupleHeaderGetRawCommandId(const HeapTupleHeaderData *tup)
Definition: htup_details.h:415
#define HEAP_COMBOCID
Definition: htup_details.h:195
#define InvalidOid
Definition: postgres_ext.h:35
RelFileLocator rd_locator
Definition: rel.h:57
CommandId cmin
Definition: heapam_xlog.h:459
CommandId combocid
Definition: heapam_xlog.h:461
ItemPointerData target_tid
Definition: heapam_xlog.h:467
TransactionId top_xid
Definition: heapam_xlog.h:458
CommandId cmax
Definition: heapam_xlog.h:460
RelFileLocator target_locator
Definition: heapam_xlog.h:466
TransactionId GetTopTransactionId(void)
Definition: xact.c:426

References Assert(), xl_heap_new_cid::cmax, xl_heap_new_cid::cmin, xl_heap_new_cid::combocid, GetTopTransactionId(), HEAP_COMBOCID, HEAP_XMAX_INVALID, HEAP_XMAX_IS_LOCKED_ONLY(), HeapTupleHeaderGetCmax(), HeapTupleHeaderGetCmin(), HeapTupleHeaderGetRawCommandId(), HeapTupleHeaderXminInvalid(), InvalidCommandId, InvalidOid, ItemPointerIsValid(), RelationData::rd_locator, SizeOfHeapNewCid, HeapTupleData::t_data, HeapTupleHeaderData::t_infomask, HeapTupleData::t_self, HeapTupleData::t_tableOid, xl_heap_new_cid::target_locator, xl_heap_new_cid::target_tid, xl_heap_new_cid::top_xid, XLOG_HEAP2_NEW_CID, XLogBeginInsert(), XLogInsert(), and XLogRegisterData().

Referenced by heap_delete(), heap_insert(), heap_multi_insert(), and heap_update().

◆ log_heap_update()

static XLogRecPtr log_heap_update ( Relation  reln,
Buffer  oldbuf,
Buffer  newbuf,
HeapTuple  oldtup,
HeapTuple  newtup,
HeapTuple  old_key_tuple,
bool  all_visible_cleared,
bool  new_all_visible_cleared 
)
static

Definition at line 8794 of file heapam.c.

8798{
8799 xl_heap_update xlrec;
8800 xl_heap_header xlhdr;
8801 xl_heap_header xlhdr_idx;
8802 uint8 info;
8803 uint16 prefix_suffix[2];
8804 uint16 prefixlen = 0,
8805 suffixlen = 0;
8806 XLogRecPtr recptr;
8807 Page page = BufferGetPage(newbuf);
8808 bool need_tuple_data = RelationIsLogicallyLogged(reln);
8809 bool init;
8810 int bufflags;
8811
8812 /* Caller should not call me on a non-WAL-logged relation */
8813 Assert(RelationNeedsWAL(reln));
8814
8816
8817 if (HeapTupleIsHeapOnly(newtup))
8818 info = XLOG_HEAP_HOT_UPDATE;
8819 else
8820 info = XLOG_HEAP_UPDATE;
8821
8822 /*
8823 * If the old and new tuple are on the same page, we only need to log the
8824 * parts of the new tuple that were changed. That saves on the amount of
8825 * WAL we need to write. Currently, we just count any unchanged bytes in
8826 * the beginning and end of the tuple. That's quick to check, and
8827 * perfectly covers the common case that only one field is updated.
8828 *
8829 * We could do this even if the old and new tuple are on different pages,
8830 * but only if we don't make a full-page image of the old page, which is
8831 * difficult to know in advance. Also, if the old tuple is corrupt for
8832 * some reason, it would allow the corruption to propagate the new page,
8833 * so it seems best to avoid. Under the general assumption that most
8834 * updates tend to create the new tuple version on the same page, there
8835 * isn't much to be gained by doing this across pages anyway.
8836 *
8837 * Skip this if we're taking a full-page image of the new page, as we
8838 * don't include the new tuple in the WAL record in that case. Also
8839 * disable if wal_level='logical', as logical decoding needs to be able to
8840 * read the new tuple in whole from the WAL record alone.
8841 */
8842 if (oldbuf == newbuf && !need_tuple_data &&
8844 {
8845 char *oldp = (char *) oldtup->t_data + oldtup->t_data->t_hoff;
8846 char *newp = (char *) newtup->t_data + newtup->t_data->t_hoff;
8847 int oldlen = oldtup->t_len - oldtup->t_data->t_hoff;
8848 int newlen = newtup->t_len - newtup->t_data->t_hoff;
8849
8850 /* Check for common prefix between old and new tuple */
8851 for (prefixlen = 0; prefixlen < Min(oldlen, newlen); prefixlen++)
8852 {
8853 if (newp[prefixlen] != oldp[prefixlen])
8854 break;
8855 }
8856
8857 /*
8858 * Storing the length of the prefix takes 2 bytes, so we need to save
8859 * at least 3 bytes or there's no point.
8860 */
8861 if (prefixlen < 3)
8862 prefixlen = 0;
8863
8864 /* Same for suffix */
8865 for (suffixlen = 0; suffixlen < Min(oldlen, newlen) - prefixlen; suffixlen++)
8866 {
8867 if (newp[newlen - suffixlen - 1] != oldp[oldlen - suffixlen - 1])
8868 break;
8869 }
8870 if (suffixlen < 3)
8871 suffixlen = 0;
8872 }
8873
8874 /* Prepare main WAL data chain */
8875 xlrec.flags = 0;
8876 if (all_visible_cleared)
8878 if (new_all_visible_cleared)
8880 if (prefixlen > 0)
8882 if (suffixlen > 0)
8884 if (need_tuple_data)
8885 {
8887 if (old_key_tuple)
8888 {
8889 if (reln->rd_rel->relreplident == REPLICA_IDENTITY_FULL)
8891 else
8893 }
8894 }
8895
8896 /* If new tuple is the single and first tuple on page... */
8899 {
8900 info |= XLOG_HEAP_INIT_PAGE;
8901 init = true;
8902 }
8903 else
8904 init = false;
8905
8906 /* Prepare WAL data for the old page */
8908 xlrec.old_xmax = HeapTupleHeaderGetRawXmax(oldtup->t_data);
8910 oldtup->t_data->t_infomask2);
8911
8912 /* Prepare WAL data for the new page */
8914 xlrec.new_xmax = HeapTupleHeaderGetRawXmax(newtup->t_data);
8915
8916 bufflags = REGBUF_STANDARD;
8917 if (init)
8918 bufflags |= REGBUF_WILL_INIT;
8919 if (need_tuple_data)
8920 bufflags |= REGBUF_KEEP_DATA;
8921
8922 XLogRegisterBuffer(0, newbuf, bufflags);
8923 if (oldbuf != newbuf)
8925
8927
8928 /*
8929 * Prepare WAL data for the new tuple.
8930 */
8931 if (prefixlen > 0 || suffixlen > 0)
8932 {
8933 if (prefixlen > 0 && suffixlen > 0)
8934 {
8935 prefix_suffix[0] = prefixlen;
8936 prefix_suffix[1] = suffixlen;
8937 XLogRegisterBufData(0, &prefix_suffix, sizeof(uint16) * 2);
8938 }
8939 else if (prefixlen > 0)
8940 {
8941 XLogRegisterBufData(0, &prefixlen, sizeof(uint16));
8942 }
8943 else
8944 {
8945 XLogRegisterBufData(0, &suffixlen, sizeof(uint16));
8946 }
8947 }
8948
8949 xlhdr.t_infomask2 = newtup->t_data->t_infomask2;
8950 xlhdr.t_infomask = newtup->t_data->t_infomask;
8951 xlhdr.t_hoff = newtup->t_data->t_hoff;
8952 Assert(SizeofHeapTupleHeader + prefixlen + suffixlen <= newtup->t_len);
8953
8954 /*
8955 * PG73FORMAT: write bitmap [+ padding] [+ oid] + data
8956 *
8957 * The 'data' doesn't include the common prefix or suffix.
8958 */
8960 if (prefixlen == 0)
8961 {
8963 (char *) newtup->t_data + SizeofHeapTupleHeader,
8964 newtup->t_len - SizeofHeapTupleHeader - suffixlen);
8965 }
8966 else
8967 {
8968 /*
8969 * Have to write the null bitmap and data after the common prefix as
8970 * two separate rdata entries.
8971 */
8972 /* bitmap [+ padding] [+ oid] */
8973 if (newtup->t_data->t_hoff - SizeofHeapTupleHeader > 0)
8974 {
8976 (char *) newtup->t_data + SizeofHeapTupleHeader,
8978 }
8979
8980 /* data after common prefix */
8982 (char *) newtup->t_data + newtup->t_data->t_hoff + prefixlen,
8983 newtup->t_len - newtup->t_data->t_hoff - prefixlen - suffixlen);
8984 }
8985
8986 /* We need to log a tuple identity */
8987 if (need_tuple_data && old_key_tuple)
8988 {
8989 /* don't really need this, but its more comfy to decode */
8990 xlhdr_idx.t_infomask2 = old_key_tuple->t_data->t_infomask2;
8991 xlhdr_idx.t_infomask = old_key_tuple->t_data->t_infomask;
8992 xlhdr_idx.t_hoff = old_key_tuple->t_data->t_hoff;
8993
8995
8996 /* PG73FORMAT: write bitmap [+ padding] [+ oid] + data */
8997 XLogRegisterData((char *) old_key_tuple->t_data + SizeofHeapTupleHeader,
8998 old_key_tuple->t_len - SizeofHeapTupleHeader);
8999 }
9000
9001 /* filtering by origin on a row level is much more efficient */
9003
9004 recptr = XLogInsert(RM_HEAP_ID, info);
9005
9006 return recptr;
9007}
#define SizeOfHeapUpdate
Definition: heapam_xlog.h:233
#define XLH_UPDATE_NEW_ALL_VISIBLE_CLEARED
Definition: heapam_xlog.h:87
#define XLOG_HEAP_HOT_UPDATE
Definition: heapam_xlog.h:37
#define XLH_UPDATE_CONTAINS_NEW_TUPLE
Definition: heapam_xlog.h:90
#define XLOG_HEAP_UPDATE
Definition: heapam_xlog.h:35
#define XLH_UPDATE_CONTAINS_OLD_TUPLE
Definition: heapam_xlog.h:88
#define XLH_UPDATE_OLD_ALL_VISIBLE_CLEARED
Definition: heapam_xlog.h:85
#define XLH_UPDATE_SUFFIX_FROM_OLD
Definition: heapam_xlog.h:92
#define XLH_UPDATE_PREFIX_FROM_OLD
Definition: heapam_xlog.h:91
#define XLH_UPDATE_CONTAINS_OLD_KEY
Definition: heapam_xlog.h:89
TransactionId new_xmax
Definition: heapam_xlog.h:224
uint8 old_infobits_set
Definition: heapam_xlog.h:222
TransactionId old_xmax
Definition: heapam_xlog.h:220
OffsetNumber old_offnum
Definition: heapam_xlog.h:221
OffsetNumber new_offnum
Definition: heapam_xlog.h:225
bool XLogCheckBufferNeedsBackup(Buffer buffer)
Definition: xloginsert.c:1027

References Assert(), BufferGetPage(), compute_infobits(), FirstOffsetNumber, xl_heap_update::flags, HeapTupleHeaderGetRawXmax(), HeapTupleIsHeapOnly(), init, ItemPointerGetOffsetNumber(), Min, xl_heap_update::new_offnum, xl_heap_update::new_xmax, xl_heap_update::old_infobits_set, xl_heap_update::old_offnum, xl_heap_update::old_xmax, PageGetMaxOffsetNumber(), RelationData::rd_rel, REGBUF_KEEP_DATA, REGBUF_STANDARD, REGBUF_WILL_INIT, RelationIsLogicallyLogged, RelationNeedsWAL, SizeOfHeapHeader, SizeofHeapTupleHeader, SizeOfHeapUpdate, HeapTupleData::t_data, xl_heap_header::t_hoff, HeapTupleHeaderData::t_hoff, xl_heap_header::t_infomask, HeapTupleHeaderData::t_infomask, xl_heap_header::t_infomask2, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_self, XLH_UPDATE_CONTAINS_NEW_TUPLE, XLH_UPDATE_CONTAINS_OLD_KEY, XLH_UPDATE_CONTAINS_OLD_TUPLE, XLH_UPDATE_NEW_ALL_VISIBLE_CLEARED, XLH_UPDATE_OLD_ALL_VISIBLE_CLEARED, XLH_UPDATE_PREFIX_FROM_OLD, XLH_UPDATE_SUFFIX_FROM_OLD, XLOG_HEAP_HOT_UPDATE, XLOG_HEAP_INIT_PAGE, XLOG_HEAP_UPDATE, XLOG_INCLUDE_ORIGIN, XLogBeginInsert(), XLogCheckBufferNeedsBackup(), XLogInsert(), XLogRegisterBufData(), XLogRegisterBuffer(), XLogRegisterData(), and XLogSetRecordFlags().

Referenced by heap_update().

◆ log_heap_visible()

XLogRecPtr log_heap_visible ( Relation  rel,
Buffer  heap_buffer,
Buffer  vm_buffer,
TransactionId  snapshotConflictHorizon,
uint8  vmflags 
)

Definition at line 8760 of file heapam.c.

8762{
8763 xl_heap_visible xlrec;
8764 XLogRecPtr recptr;
8765 uint8 flags;
8766
8767 Assert(BufferIsValid(heap_buffer));
8768 Assert(BufferIsValid(vm_buffer));
8769
8770 xlrec.snapshotConflictHorizon = snapshotConflictHorizon;
8771 xlrec.flags = vmflags;
8776
8777 XLogRegisterBuffer(0, vm_buffer, 0);
8778
8779 flags = REGBUF_STANDARD;
8780 if (!XLogHintBitIsNeeded())
8781 flags |= REGBUF_NO_IMAGE;
8782 XLogRegisterBuffer(1, heap_buffer, flags);
8783
8784 recptr = XLogInsert(RM_HEAP2_ID, XLOG_HEAP2_VISIBLE);
8785
8786 return recptr;
8787}
#define SizeOfHeapVisible
Definition: heapam_xlog.h:450
#define XLOG_HEAP2_VISIBLE
Definition: heapam_xlog.h:63
TransactionId snapshotConflictHorizon
Definition: heapam_xlog.h:446
#define VISIBILITYMAP_XLOG_CATALOG_REL
#define XLogHintBitIsNeeded()
Definition: xlog.h:120
#define REGBUF_NO_IMAGE
Definition: xloginsert.h:33

References Assert(), BufferIsValid(), xl_heap_visible::flags, REGBUF_NO_IMAGE, REGBUF_STANDARD, RelationIsAccessibleInLogicalDecoding, SizeOfHeapVisible, xl_heap_visible::snapshotConflictHorizon, VISIBILITYMAP_XLOG_CATALOG_REL, XLOG_HEAP2_VISIBLE, XLogBeginInsert(), XLogHintBitIsNeeded, XLogInsert(), XLogRegisterBuffer(), and XLogRegisterData().

Referenced by visibilitymap_set().

◆ MultiXactIdGetUpdateXid()

static TransactionId MultiXactIdGetUpdateXid ( TransactionId  xmax,
uint16  t_infomask 
)
static

Definition at line 7483 of file heapam.c.

7484{
7485 TransactionId update_xact = InvalidTransactionId;
7486 MultiXactMember *members;
7487 int nmembers;
7488
7489 Assert(!(t_infomask & HEAP_XMAX_LOCK_ONLY));
7490 Assert(t_infomask & HEAP_XMAX_IS_MULTI);
7491
7492 /*
7493 * Since we know the LOCK_ONLY bit is not set, this cannot be a multi from
7494 * pre-pg_upgrade.
7495 */
7496 nmembers = GetMultiXactIdMembers(xmax, &members, false, false);
7497
7498 if (nmembers > 0)
7499 {
7500 int i;
7501
7502 for (i = 0; i < nmembers; i++)
7503 {
7504 /* Ignore lockers */
7505 if (!ISUPDATE_from_mxstatus(members[i].status))
7506 continue;
7507
7508 /* there can be at most one updater */
7509 Assert(update_xact == InvalidTransactionId);
7510 update_xact = members[i].xid;
7511#ifndef USE_ASSERT_CHECKING
7512
7513 /*
7514 * in an assert-enabled build, walk the whole array to ensure
7515 * there's no other updater.
7516 */
7517 break;
7518#endif
7519 }
7520
7521 pfree(members);
7522 }
7523
7524 return update_xact;
7525}

References Assert(), GetMultiXactIdMembers(), HEAP_XMAX_IS_MULTI, HEAP_XMAX_LOCK_ONLY, i, InvalidTransactionId, ISUPDATE_from_mxstatus, pfree(), and MultiXactMember::xid.

Referenced by compute_new_xmax_infomask(), FreezeMultiXactId(), and HeapTupleGetUpdateXid().

◆ MultiXactIdWait()

static void MultiXactIdWait ( MultiXactId  multi,
MultiXactStatus  status,
uint16  infomask,
Relation  rel,
ItemPointer  ctid,
XLTW_Oper  oper,
int *  remaining 
)
static

Definition at line 7729 of file heapam.c.

7732{
7733 (void) Do_MultiXactIdWait(multi, status, infomask, false,
7734 rel, ctid, oper, remaining, false);
7735}

References Do_MultiXactIdWait(), oper(), and remaining.

Referenced by heap_delete(), heap_inplace_lock(), heap_lock_tuple(), and heap_update().

◆ page_collect_tuples()

static pg_attribute_always_inline int page_collect_tuples ( HeapScanDesc  scan,
Snapshot  snapshot,
Page  page,
Buffer  buffer,
BlockNumber  block,
int  lines,
bool  all_visible,
bool  check_serializable 
)
static

Definition at line 481 of file heapam.c.

485{
486 int ntup = 0;
487 OffsetNumber lineoff;
488
489 for (lineoff = FirstOffsetNumber; lineoff <= lines; lineoff++)
490 {
491 ItemId lpp = PageGetItemId(page, lineoff);
492 HeapTupleData loctup;
493 bool valid;
494
495 if (!ItemIdIsNormal(lpp))
496 continue;
497
498 loctup.t_data = (HeapTupleHeader) PageGetItem(page, lpp);
499 loctup.t_len = ItemIdGetLength(lpp);
501 ItemPointerSet(&(loctup.t_self), block, lineoff);
502
503 if (all_visible)
504 valid = true;
505 else
506 valid = HeapTupleSatisfiesVisibility(&loctup, snapshot, buffer);
507
508 if (check_serializable)
510 &loctup, buffer, snapshot);
511
512 if (valid)
513 {
514 scan->rs_vistuples[ntup] = lineoff;
515 ntup++;
516 }
517 }
518
520
521 return ntup;
522}
#define MaxHeapTuplesPerPage
Definition: htup_details.h:624

References Assert(), FirstOffsetNumber, HeapCheckForSerializableConflictOut(), HeapTupleSatisfiesVisibility(), ItemIdGetLength, ItemIdIsNormal, ItemPointerSet(), MaxHeapTuplesPerPage, PageGetItem(), PageGetItemId(), RelationGetRelid, HeapScanDescData::rs_base, TableScanDescData::rs_rd, HeapScanDescData::rs_vistuples, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, and HeapTupleData::t_tableOid.

Referenced by heap_prepare_pagescan().

◆ ReleaseBulkInsertStatePin()

void ReleaseBulkInsertStatePin ( BulkInsertState  bistate)

Definition at line 2018 of file heapam.c.

2019{
2020 if (bistate->current_buf != InvalidBuffer)
2021 ReleaseBuffer(bistate->current_buf);
2022 bistate->current_buf = InvalidBuffer;
2023
2024 /*
2025 * Despite the name, we also reset bulk relation extension state.
2026 * Otherwise we can end up erroring out due to looking for free space in
2027 * ->next_free of one partition, even though ->next_free was set when
2028 * extending another partition. It could obviously also be bad for
2029 * efficiency to look at existing blocks at offsets from another
2030 * partition, even if we don't error out.
2031 */
2032 bistate->next_free = InvalidBlockNumber;
2033 bistate->last_free = InvalidBlockNumber;
2034}

References BulkInsertStateData::current_buf, InvalidBlockNumber, InvalidBuffer, BulkInsertStateData::last_free, BulkInsertStateData::next_free, and ReleaseBuffer().

Referenced by CopyFrom().

◆ simple_heap_delete()

void simple_heap_delete ( Relation  relation,
ItemPointer  tid 
)

Definition at line 3166 of file heapam.c.

3167{
3168 TM_Result result;
3169 TM_FailureData tmfd;
3170
3171 result = heap_delete(relation, tid,
3173 true /* wait for commit */ ,
3174 &tmfd, false /* changingPart */ );
3175 switch (result)
3176 {
3177 case TM_SelfModified:
3178 /* Tuple was already updated in current command? */
3179 elog(ERROR, "tuple already updated by self");
3180 break;
3181
3182 case TM_Ok:
3183 /* done successfully */
3184 break;
3185
3186 case TM_Updated:
3187 elog(ERROR, "tuple concurrently updated");
3188 break;
3189
3190 case TM_Deleted:
3191 elog(ERROR, "tuple concurrently deleted");
3192 break;
3193
3194 default:
3195 elog(ERROR, "unrecognized heap_delete status: %u", result);
3196 break;
3197 }
3198}
TM_Result heap_delete(Relation relation, ItemPointer tid, CommandId cid, Snapshot crosscheck, bool wait, TM_FailureData *tmfd, bool changingPart)
Definition: heapam.c:2745

References elog, ERROR, GetCurrentCommandId(), heap_delete(), InvalidSnapshot, TM_Deleted, TM_Ok, TM_SelfModified, and TM_Updated.

Referenced by CatalogTupleDelete(), and toast_delete_datum().

◆ simple_heap_insert()

void simple_heap_insert ( Relation  relation,
HeapTuple  tup 
)

Definition at line 2687 of file heapam.c.

2688{
2689 heap_insert(relation, tup, GetCurrentCommandId(true), 0, NULL);
2690}
void heap_insert(Relation relation, HeapTuple tup, CommandId cid, int options, BulkInsertState bistate)
Definition: heapam.c:2056

References GetCurrentCommandId(), and heap_insert().

Referenced by CatalogTupleInsert(), CatalogTupleInsertWithInfo(), and InsertOneTuple().

◆ simple_heap_update()

void simple_heap_update ( Relation  relation,
ItemPointer  otid,
HeapTuple  tup,
TU_UpdateIndexes update_indexes 
)

Definition at line 4454 of file heapam.c.

4456{
4457 TM_Result result;
4458 TM_FailureData tmfd;
4459 LockTupleMode lockmode;
4460
4461 result = heap_update(relation, otid, tup,
4463 true /* wait for commit */ ,
4464 &tmfd, &lockmode, update_indexes);
4465 switch (result)
4466 {
4467 case TM_SelfModified:
4468 /* Tuple was already updated in current command? */
4469 elog(ERROR, "tuple already updated by self");
4470 break;
4471
4472 case TM_Ok:
4473 /* done successfully */
4474 break;
4475
4476 case TM_Updated:
4477 elog(ERROR, "tuple concurrently updated");
4478 break;
4479
4480 case TM_Deleted:
4481 elog(ERROR, "tuple concurrently deleted");
4482 break;
4483
4484 default:
4485 elog(ERROR, "unrecognized heap_update status: %u", result);
4486 break;
4487 }
4488}
TM_Result heap_update(Relation relation, ItemPointer otid, HeapTuple newtup, CommandId cid, Snapshot crosscheck, bool wait, TM_FailureData *tmfd, LockTupleMode *lockmode, TU_UpdateIndexes *update_indexes)
Definition: heapam.c:3212

References elog, ERROR, GetCurrentCommandId(), heap_update(), InvalidSnapshot, TM_Deleted, TM_Ok, TM_SelfModified, and TM_Updated.

Referenced by CatalogTupleUpdate(), and CatalogTupleUpdateWithInfo().

◆ test_lockmode_for_conflict()

static TM_Result test_lockmode_for_conflict ( MultiXactStatus  status,
TransactionId  xid,
LockTupleMode  mode,
HeapTuple  tup,
bool *  needwait 
)
static

Definition at line 5571 of file heapam.c.

5574{
5575 MultiXactStatus wantedstatus;
5576
5577 *needwait = false;
5578 wantedstatus = get_mxact_status_for_lock(mode, false);
5579
5580 /*
5581 * Note: we *must* check TransactionIdIsInProgress before
5582 * TransactionIdDidAbort/Commit; see comment at top of heapam_visibility.c
5583 * for an explanation.
5584 */
5586 {
5587 /*
5588 * The tuple has already been locked by our own transaction. This is
5589 * very rare but can happen if multiple transactions are trying to
5590 * lock an ancient version of the same tuple.
5591 */
5592 return TM_SelfModified;
5593 }
5594 else if (TransactionIdIsInProgress(xid))
5595 {
5596 /*
5597 * If the locking transaction is running, what we do depends on
5598 * whether the lock modes conflict: if they do, then we must wait for
5599 * it to finish; otherwise we can fall through to lock this tuple
5600 * version without waiting.
5601 */
5603 LOCKMODE_from_mxstatus(wantedstatus)))
5604 {
5605 *needwait = true;
5606 }
5607
5608 /*
5609 * If we set needwait above, then this value doesn't matter;
5610 * otherwise, this value signals to caller that it's okay to proceed.
5611 */
5612 return TM_Ok;
5613 }
5614 else if (TransactionIdDidAbort(xid))
5615 return TM_Ok;
5616 else if (TransactionIdDidCommit(xid))
5617 {
5618 /*
5619 * The other transaction committed. If it was only a locker, then the
5620 * lock is completely gone now and we can return success; but if it
5621 * was an update, then what we do depends on whether the two lock
5622 * modes conflict. If they conflict, then we must report error to
5623 * caller. But if they don't, we can fall through to allow the current
5624 * transaction to lock the tuple.
5625 *
5626 * Note: the reason we worry about ISUPDATE here is because as soon as
5627 * a transaction ends, all its locks are gone and meaningless, and
5628 * thus we can ignore them; whereas its updates persist. In the
5629 * TransactionIdIsInProgress case, above, we don't need to check
5630 * because we know the lock is still "alive" and thus a conflict needs
5631 * always be checked.
5632 */
5633 if (!ISUPDATE_from_mxstatus(status))
5634 return TM_Ok;
5635
5637 LOCKMODE_from_mxstatus(wantedstatus)))
5638 {
5639 /* bummer */
5640 if (!ItemPointerEquals(&tup->t_self, &tup->t_data->t_ctid))
5641 return TM_Updated;
5642 else
5643 return TM_Deleted;
5644 }
5645
5646 return TM_Ok;
5647 }
5648
5649 /* Not in progress, not aborted, not committed -- must have crashed */
5650 return TM_Ok;
5651}

References DoLockModesConflict(), get_mxact_status_for_lock(), ISUPDATE_from_mxstatus, ItemPointerEquals(), LOCKMODE_from_mxstatus, mode, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleData::t_self, TM_Deleted, TM_Ok, TM_SelfModified, TM_Updated, TransactionIdDidAbort(), TransactionIdDidCommit(), TransactionIdIsCurrentTransactionId(), and TransactionIdIsInProgress().

Referenced by heap_lock_updated_tuple_rec().

◆ UpdateXmaxHintBits()

static void UpdateXmaxHintBits ( HeapTupleHeader  tuple,
Buffer  buffer,
TransactionId  xid 
)
static

Definition at line 1967 of file heapam.c.

1968{
1971
1973 {
1974 if (!HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_infomask) &&
1977 xid);
1978 else
1981 }
1982}
void HeapTupleSetHintBits(HeapTupleHeader tuple, Buffer buffer, uint16 infomask, TransactionId xid)

References Assert(), HEAP_XMAX_COMMITTED, HEAP_XMAX_INVALID, HEAP_XMAX_IS_LOCKED_ONLY(), HEAP_XMAX_IS_MULTI, HeapTupleHeaderGetRawXmax(), HeapTupleSetHintBits(), InvalidTransactionId, HeapTupleHeaderData::t_infomask, TransactionIdDidCommit(), and TransactionIdEquals.

Referenced by heap_delete(), heap_lock_tuple(), and heap_update().

◆ xmax_infomask_changed()

static bool xmax_infomask_changed ( uint16  new_infomask,
uint16  old_infomask 
)
inlinestatic

Definition at line 2722 of file heapam.c.

2723{
2724 const uint16 interesting =
2726
2727 if ((new_infomask & interesting) != (old_infomask & interesting))
2728 return true;
2729
2730 return false;
2731}
#define HEAP_LOCK_MASK
Definition: htup_details.h:202

References HEAP_LOCK_MASK, HEAP_XMAX_IS_MULTI, and HEAP_XMAX_LOCK_ONLY.

Referenced by heap_delete(), heap_lock_tuple(), and heap_update().

Variable Documentation

◆ hwlock

LOCKMODE hwlock

Definition at line 124 of file heapam.c.

◆ lockstatus

int lockstatus

Definition at line 125 of file heapam.c.

◆ MultiXactStatusLock

const int MultiXactStatusLock[MaxMultiXactStatus+1]
static

◆ 

const struct { ... } tupleLockExtraInfo[MaxLockTupleMode + 1]

◆ updstatus

int updstatus

Definition at line 126 of file heapam.c.