PostgreSQL Source Code  git master
heapam.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * heapam.h
4  * POSTGRES heap access method definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/access/heapam.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef HEAPAM_H
15 #define HEAPAM_H
16 
17 #include "access/relation.h" /* for backward compatibility */
18 #include "access/relscan.h"
19 #include "access/sdir.h"
20 #include "access/skey.h"
21 #include "access/table.h" /* for backward compatibility */
22 #include "access/tableam.h"
23 #include "nodes/lockoptions.h"
24 #include "nodes/primnodes.h"
25 #include "storage/bufpage.h"
26 #include "storage/dsm.h"
27 #include "storage/lockdefs.h"
28 #include "storage/shm_toc.h"
29 #include "utils/relcache.h"
30 #include "utils/snapshot.h"
31 
32 
33 /* "options" flag bits for heap_insert */
34 #define HEAP_INSERT_SKIP_WAL TABLE_INSERT_SKIP_WAL
35 #define HEAP_INSERT_SKIP_FSM TABLE_INSERT_SKIP_FSM
36 #define HEAP_INSERT_FROZEN TABLE_INSERT_FROZEN
37 #define HEAP_INSERT_NO_LOGICAL TABLE_INSERT_NO_LOGICAL
38 #define HEAP_INSERT_SPECULATIVE 0x0010
39 
41 struct TupleTableSlot;
42 
43 #define MaxLockTupleMode LockTupleExclusive
44 
45 /*
46  * Descriptor for heap table scans.
47  */
48 typedef struct HeapScanDescData
49 {
50  TableScanDescData rs_base; /* AM independent part of the descriptor */
51 
52  /* state set up at initscan time */
53  BlockNumber rs_nblocks; /* total number of blocks in rel */
54  BlockNumber rs_startblock; /* block # to start at */
55  BlockNumber rs_numblocks; /* max number of blocks to scan */
56  /* rs_numblocks is usually InvalidBlockNumber, meaning "scan whole rel" */
57 
58  /* scan current state */
59  bool rs_inited; /* false = scan not init'd yet */
60  BlockNumber rs_cblock; /* current block # in scan, if any */
61  Buffer rs_cbuf; /* current buffer in scan, if any */
62  /* NB: if rs_cbuf is not InvalidBuffer, we hold a pin on that buffer */
63 
64  /* rs_numblocks is usually InvalidBlockNumber, meaning "scan whole rel" */
65  BufferAccessStrategy rs_strategy; /* access strategy for reads */
66 
67  HeapTupleData rs_ctup; /* current tuple in scan, if any */
68 
69  /* these fields only used in page-at-a-time mode and for bitmap scans */
70  int rs_cindex; /* current tuple's index in vistuples */
71  int rs_ntuples; /* number of visible tuples on page */
75 
76 /*
77  * Descriptor for fetches from heap via an index.
78  */
79 typedef struct IndexFetchHeapData
80 {
81  IndexFetchTableData xs_base; /* AM independent part of the descriptor */
82 
83  Buffer xs_cbuf; /* current heap buffer in scan, if any */
84  /* NB: if xs_cbuf is not InvalidBuffer, we hold a pin on that buffer */
86 
87 /* Result codes for HeapTupleSatisfiesVacuum */
88 typedef enum
89 {
90  HEAPTUPLE_DEAD, /* tuple is dead and deletable */
91  HEAPTUPLE_LIVE, /* tuple is live (committed, no deleter) */
92  HEAPTUPLE_RECENTLY_DEAD, /* tuple is dead, but not deletable yet */
93  HEAPTUPLE_INSERT_IN_PROGRESS, /* inserting xact is still in progress */
94  HEAPTUPLE_DELETE_IN_PROGRESS /* deleting xact is still in progress */
95 } HTSV_Result;
96 
97 /* ----------------
98  * function prototypes for heap access method
99  *
100  * heap_create, heap_create_with_catalog, and heap_drop_with_catalog
101  * are declared in catalog/heap.h
102  * ----------------
103  */
104 
105 
106 /*
107  * HeapScanIsValid
108  * True iff the heap scan is valid.
109  */
110 #define HeapScanIsValid(scan) PointerIsValid(scan)
111 
112 extern TableScanDesc heap_beginscan(Relation relation, Snapshot snapshot,
113  int nkeys, ScanKey key,
114  ParallelTableScanDesc parallel_scan,
115  uint32 flags);
116 extern void heap_setscanlimits(TableScanDesc scan, BlockNumber startBlk,
117  BlockNumber numBlks);
118 extern void heapgetpage(TableScanDesc scan, BlockNumber page);
119 extern void heap_rescan(TableScanDesc scan, ScanKey key, bool set_params,
120  bool allow_strat, bool allow_sync, bool allow_pagemode);
121 extern void heap_endscan(TableScanDesc scan);
122 extern HeapTuple heap_getnext(TableScanDesc scan, ScanDirection direction);
123 extern bool heap_getnextslot(TableScanDesc sscan,
124  ScanDirection direction, struct TupleTableSlot *slot);
125 
126 extern bool heap_fetch(Relation relation, Snapshot snapshot,
127  HeapTuple tuple, Buffer *userbuf);
128 extern bool heap_hot_search_buffer(ItemPointer tid, Relation relation,
129  Buffer buffer, Snapshot snapshot, HeapTuple heapTuple,
130  bool *all_dead, bool first_call);
131 
132 extern void heap_get_latest_tid(TableScanDesc scan, ItemPointer tid);
133 extern void setLastTid(const ItemPointer tid);
134 
135 extern BulkInsertState GetBulkInsertState(void);
136 extern void FreeBulkInsertState(BulkInsertState);
137 extern void ReleaseBulkInsertStatePin(BulkInsertState bistate);
138 
139 extern void heap_insert(Relation relation, HeapTuple tup, CommandId cid,
140  int options, BulkInsertState bistate);
141 extern void heap_multi_insert(Relation relation, struct TupleTableSlot **slots,
142  int ntuples, CommandId cid, int options,
143  BulkInsertState bistate);
144 extern TM_Result heap_delete(Relation relation, ItemPointer tid,
145  CommandId cid, Snapshot crosscheck, bool wait,
146  struct TM_FailureData *tmfd, bool changingPart);
147 extern void heap_finish_speculative(Relation relation, ItemPointer tid);
148 extern void heap_abort_speculative(Relation relation, ItemPointer tid);
149 extern TM_Result heap_update(Relation relation, ItemPointer otid,
150  HeapTuple newtup,
151  CommandId cid, Snapshot crosscheck, bool wait,
152  struct TM_FailureData *tmfd, LockTupleMode *lockmode);
153 extern TM_Result heap_lock_tuple(Relation relation, HeapTuple tuple,
154  CommandId cid, LockTupleMode mode, LockWaitPolicy wait_policy,
155  bool follow_update,
156  Buffer *buffer, struct TM_FailureData *tmfd);
157 
158 extern void heap_inplace_update(Relation relation, HeapTuple tuple);
159 extern bool heap_freeze_tuple(HeapTupleHeader tuple,
161  TransactionId cutoff_xid, TransactionId cutoff_multi);
162 extern bool heap_tuple_needs_freeze(HeapTupleHeader tuple, TransactionId cutoff_xid,
163  MultiXactId cutoff_multi, Buffer buf);
165 
166 extern void simple_heap_insert(Relation relation, HeapTuple tup);
167 extern void simple_heap_delete(Relation relation, ItemPointer tid);
168 extern void simple_heap_update(Relation relation, ItemPointer otid,
169  HeapTuple tup);
170 
171 extern void heap_sync(Relation relation);
172 
174  ItemPointerData *items,
175  int nitems);
176 
177 /* in heap/pruneheap.c */
178 extern void heap_page_prune_opt(Relation relation, Buffer buffer);
179 extern int heap_page_prune(Relation relation, Buffer buffer,
181  bool report_stats, TransactionId *latestRemovedXid);
182 extern void heap_page_prune_execute(Buffer buffer,
183  OffsetNumber *redirected, int nredirected,
184  OffsetNumber *nowdead, int ndead,
185  OffsetNumber *nowunused, int nunused);
186 extern void heap_get_root_tuples(Page page, OffsetNumber *root_offsets);
187 
188 /* in heap/syncscan.c */
189 extern void ss_report_location(Relation rel, BlockNumber location);
190 extern BlockNumber ss_get_location(Relation rel, BlockNumber relnblocks);
191 extern void SyncScanShmemInit(void);
192 extern Size SyncScanShmemSize(void);
193 
194 /* in heap/vacuumlazy.c */
195 struct VacuumParams;
196 extern void heap_vacuum_rel(Relation onerel,
197  struct VacuumParams *params, BufferAccessStrategy bstrategy);
198 extern void parallel_vacuum_main(dsm_segment *seg, shm_toc *toc);
199 
200 /* in heap/heapam_visibility.c */
201 extern bool HeapTupleSatisfiesVisibility(HeapTuple stup, Snapshot snapshot,
202  Buffer buffer);
204  Buffer buffer);
206  Buffer buffer);
207 extern void HeapTupleSetHintBits(HeapTupleHeader tuple, Buffer buffer,
208  uint16 infomask, TransactionId xid);
210 extern bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot);
212 
213 /*
214  * To avoid leaking too much knowledge about reorderbuffer implementation
215  * details this is implemented in reorderbuffer.c not heapam_visibility.c
216  */
217 struct HTAB;
219  Snapshot snapshot,
220  HeapTuple htup,
221  Buffer buffer,
222  CommandId *cmin, CommandId *cmax);
223 
224 #endif /* HEAPAM_H */
uint32 CommandId
Definition: c.h:528
bool heap_fetch(Relation relation, Snapshot snapshot, HeapTuple tuple, Buffer *userbuf)
Definition: heapam.c:1412
static PgChecksumMode mode
Definition: pg_checksums.c:61
BlockNumber rs_cblock
Definition: heapam.h:60
LockTupleMode
Definition: lockoptions.h:49
void SyncScanShmemInit(void)
Definition: syncscan.c:135
void heap_sync(Relation relation)
Definition: heapam.c:8938
uint32 TransactionId
Definition: c.h:514
bool HeapTupleHeaderIsOnlyLocked(HeapTupleHeader tuple)
TableScanDescData rs_base
Definition: heapam.h:50
void ReleaseBulkInsertStatePin(BulkInsertState bistate)
Definition: heapam.c:1841
#define MaxHeapTuplesPerPage
Definition: htup_details.h:574
bool heap_tuple_needs_eventual_freeze(HeapTupleHeader tuple)
Definition: heapam.c:6763
void heapgetpage(TableScanDesc scan, BlockNumber page)
Definition: heapam.c:352
uint32 BlockNumber
Definition: block.h:31
void HeapTupleSetHintBits(HeapTupleHeader tuple, Buffer buffer, uint16 infomask, TransactionId xid)
void parallel_vacuum_main(dsm_segment *seg, shm_toc *toc)
Definition: vacuumlazy.c:3313
struct HeapScanDescData * HeapScanDesc
Definition: heapam.h:74
HeapTupleData rs_ctup
Definition: heapam.h:67
void heap_get_root_tuples(Page page, OffsetNumber *root_offsets)
Definition: pruneheap.c:745
uint16 OffsetNumber
Definition: off.h:24
bool HeapTupleIsSurelyDead(HeapTuple htup, TransactionId OldestXmin)
void heap_multi_insert(Relation relation, struct TupleTableSlot **slots, int ntuples, CommandId cid, int options, BulkInsertState bistate)
Definition: heapam.c:2105
TM_Result heap_lock_tuple(Relation relation, HeapTuple tuple, CommandId cid, LockTupleMode mode, LockWaitPolicy wait_policy, bool follow_update, Buffer *buffer, struct TM_FailureData *tmfd)
Definition: heapam.c:3973
void simple_heap_delete(Relation relation, ItemPointer tid)
Definition: heapam.c:2847
Definition: dynahash.c:208
bool HeapTupleSatisfiesVisibility(HeapTuple stup, Snapshot snapshot, Buffer buffer)
unsigned short uint16
Definition: c.h:358
bool heap_freeze_tuple(HeapTupleHeader tuple, TransactionId relfrozenxid, TransactionId relminmxid, TransactionId cutoff_xid, TransactionId cutoff_multi)
Definition: heapam.c:6368
BulkInsertState GetBulkInsertState(void)
Definition: heapam.c:1815
void heap_insert(Relation relation, HeapTuple tup, CommandId cid, int options, BulkInsertState bistate)
Definition: heapam.c:1868
BlockNumber rs_numblocks
Definition: heapam.h:55
bool XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
Definition: snapmgr.c:2241
int heap_page_prune(Relation relation, Buffer buffer, TransactionId OldestXmin, bool report_stats, TransactionId *latestRemovedXid)
Definition: pruneheap.c:180
void heap_endscan(TableScanDesc scan)
Definition: heapam.c:1245
struct HeapScanDescData HeapScanDescData
Buffer xs_cbuf
Definition: heapam.h:83
static char * buf
Definition: pg_test_fsync.c:67
ScanDirection
Definition: sdir.h:22
static TransactionId OldestXmin
Definition: vacuumlazy.c:299
void setLastTid(const ItemPointer tid)
Definition: tid.c:280
unsigned int uint32
Definition: c.h:359
bool rs_inited
Definition: heapam.h:59
void simple_heap_insert(Relation relation, HeapTuple tup)
Definition: heapam.c:2384
BlockNumber rs_startblock
Definition: heapam.h:54
TM_Result heap_update(Relation relation, ItemPointer otid, HeapTuple newtup, CommandId cid, Snapshot crosscheck, bool wait, struct TM_FailureData *tmfd, LockTupleMode *lockmode)
Definition: heapam.c:2893
int rs_ntuples
Definition: heapam.h:71
void heap_finish_speculative(Relation relation, ItemPointer tid)
Definition: heapam.c:5476
static HTAB * tuplecid_data
Definition: snapmgr.c:172
bool heap_hot_search_buffer(ItemPointer tid, Relation relation, Buffer buffer, Snapshot snapshot, HeapTuple heapTuple, bool *all_dead, bool first_call)
Definition: heapam.c:1526
TM_Result
Definition: tableam.h:68
BlockNumber rs_nblocks
Definition: heapam.h:53
BufferAccessStrategy rs_strategy
Definition: heapam.h:65
TransactionId relminmxid
Definition: pg_class.h:126
Size SyncScanShmemSize(void)
Definition: syncscan.c:126
Buffer rs_cbuf
Definition: heapam.h:61
struct IndexFetchHeapData IndexFetchHeapData
TransactionId MultiXactId
Definition: c.h:524
void ss_report_location(Relation rel, BlockNumber location)
Definition: syncscan.c:288
OffsetNumber rs_vistuples[MaxHeapTuplesPerPage]
Definition: heapam.h:72
size_t Size
Definition: c.h:467
void heap_setscanlimits(TableScanDesc scan, BlockNumber startBlk, BlockNumber numBlks)
Definition: heapam.c:329
TM_Result heap_delete(Relation relation, ItemPointer tid, CommandId cid, Snapshot crosscheck, bool wait, struct TM_FailureData *tmfd, bool changingPart)
Definition: heapam.c:2442
struct BulkInsertStateData * BulkInsertState
Definition: heapam.h:40
bool heap_tuple_needs_freeze(HeapTupleHeader tuple, TransactionId cutoff_xid, MultiXactId cutoff_multi, Buffer buf)
Definition: heapam.c:6816
void FreeBulkInsertState(BulkInsertState)
Definition: heapam.c:1829
void heap_get_latest_tid(TableScanDesc scan, ItemPointer tid)
Definition: heapam.c:1670
TransactionId heap_compute_xid_horizon_for_tuples(Relation rel, ItemPointerData *items, int nitems)
Definition: heapam.c:6986
void heap_abort_speculative(Relation relation, ItemPointer tid)
Definition: heapam.c:5567
TransactionId relfrozenxid
Definition: pg_class.h:123
HeapTuple heap_getnext(TableScanDesc scan, ScanDirection direction)
Definition: heapam.c:1290
bool heap_getnextslot(TableScanDesc sscan, ScanDirection direction, struct TupleTableSlot *slot)
Definition: heapam.c:1349
void heap_page_prune_execute(Buffer buffer, OffsetNumber *redirected, int nredirected, OffsetNumber *nowdead, int ndead, OffsetNumber *nowunused, int nunused)
Definition: pruneheap.c:681
HTSV_Result
Definition: heapam.h:88
HTSV_Result HeapTupleSatisfiesVacuum(HeapTuple stup, TransactionId OldestXmin, Buffer buffer)
void heap_inplace_update(Relation relation, HeapTuple tuple)
Definition: heapam.c:5707
void heap_vacuum_rel(Relation onerel, struct VacuumParams *params, BufferAccessStrategy bstrategy)
Definition: vacuumlazy.c:376
void simple_heap_update(Relation relation, ItemPointer otid, HeapTuple tup)
Definition: heapam.c:3885
int Buffer
Definition: buf.h:23
TableScanDesc heap_beginscan(Relation relation, Snapshot snapshot, int nkeys, ScanKey key, ParallelTableScanDesc parallel_scan, uint32 flags)
Definition: heapam.c:1132
LockWaitPolicy
Definition: lockoptions.h:36
void heap_rescan(TableScanDesc scan, ScanKey key, bool set_params, bool allow_strat, bool allow_sync, bool allow_pagemode)
Definition: heapam.c:1208
Pointer Page
Definition: bufpage.h:78
IndexFetchTableData xs_base
Definition: heapam.h:81
bool ResolveCminCmaxDuringDecoding(struct HTAB *tuplecid_data, Snapshot snapshot, HeapTuple htup, Buffer buffer, CommandId *cmin, CommandId *cmax)
TM_Result HeapTupleSatisfiesUpdate(HeapTuple stup, CommandId curcid, Buffer buffer)
BlockNumber ss_get_location(Relation rel, BlockNumber relnblocks)
Definition: syncscan.c:253
void heap_page_prune_opt(Relation relation, Buffer buffer)
Definition: pruneheap.c:73