1 /*-------------------------------------------------------------------------
2  *
3  * heapam.h
4  *	  POSTGRES heap access method definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2018, 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/sdir.h"
18 #include "access/skey.h"
19 #include "nodes/lockoptions.h"
20 #include "nodes/primnodes.h"
21 #include "storage/bufpage.h"
22 #include "storage/lockdefs.h"
23 #include "utils/relcache.h"
24 #include "utils/snapshot.h"
25 
26 
27 /* "options" flag bits for heap_insert */
28 #define HEAP_INSERT_SKIP_WAL	0x0001
29 #define HEAP_INSERT_SKIP_FSM	0x0002
30 #define HEAP_INSERT_FROZEN		0x0004
31 #define HEAP_INSERT_SPECULATIVE 0x0008
32 #define HEAP_INSERT_NO_LOGICAL	0x0010
33 
34 typedef struct BulkInsertStateData *BulkInsertState;
35 
36 /*
37  * Possible lock modes for a tuple.
38  */
39 typedef enum LockTupleMode
40 {
41 	/* SELECT FOR KEY SHARE */
42 	LockTupleKeyShare,
43 	/* SELECT FOR SHARE */
44 	LockTupleShare,
45 	/* SELECT FOR NO KEY UPDATE, and UPDATEs that don't modify key columns */
46 	LockTupleNoKeyExclusive,
47 	/* SELECT FOR UPDATE, UPDATEs that modify key columns, and DELETE */
48 	LockTupleExclusive
49 } LockTupleMode;
50 
51 #define MaxLockTupleMode	LockTupleExclusive
52 
53 /*
54  * When heap_update, heap_delete, or heap_lock_tuple fail because the target
55  * tuple is already outdated, they fill in this struct to provide information
56  * to the caller about what happened.
57  * ctid is the target's ctid link: it is the same as the target's TID if the
58  * target was deleted, or the location of the replacement tuple if the target
59  * was updated.
60  * xmax is the outdating transaction's XID.  If the caller wants to visit the
61  * replacement tuple, it must check that this matches before believing the
62  * replacement is really a match.
63  * cmax is the outdating command's CID, but only when the failure code is
64  * HeapTupleSelfUpdated (i.e., something in the current transaction outdated
65  * the tuple); otherwise cmax is zero.  (We make this restriction because
66  * HeapTupleHeaderGetCmax doesn't work for tuples outdated in other
67  * transactions.)
68  */
69 typedef struct HeapUpdateFailureData
70 {
71 	ItemPointerData ctid;
72 	TransactionId xmax;
73 	CommandId	cmax;
74 } HeapUpdateFailureData;
75 
76 
77 /* ----------------
78  *		function prototypes for heap access method
79  *
80  * heap_create, heap_create_with_catalog, and heap_drop_with_catalog
81  * are declared in catalog/heap.h
82  * ----------------
83  */
84 
85 /* in heap/heapam.c */
86 extern Relation relation_open(Oid relationId, LOCKMODE lockmode);
87 extern Relation try_relation_open(Oid relationId, LOCKMODE lockmode);
88 extern Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode);
89 extern Relation relation_openrv_extended(const RangeVar *relation,
90 						 LOCKMODE lockmode, bool missing_ok);
91 extern void relation_close(Relation relation, LOCKMODE lockmode);
92 
93 extern Relation heap_open(Oid relationId, LOCKMODE lockmode);
94 extern Relation heap_openrv(const RangeVar *relation, LOCKMODE lockmode);
95 extern Relation heap_openrv_extended(const RangeVar *relation,
96 					 LOCKMODE lockmode, bool missing_ok);
97 
98 #define heap_close(r,l)  relation_close(r,l)
99 
100 /* struct definitions appear in relscan.h */
101 typedef struct HeapScanDescData *HeapScanDesc;
102 typedef struct ParallelHeapScanDescData *ParallelHeapScanDesc;
103 
104 /*
105  * HeapScanIsValid
106  *		True iff the heap scan is valid.
107  */
108 #define HeapScanIsValid(scan) PointerIsValid(scan)
109 
110 extern HeapScanDesc heap_beginscan(Relation relation, Snapshot snapshot,
111 			   int nkeys, ScanKey key);
112 extern HeapScanDesc heap_beginscan_catalog(Relation relation, int nkeys,
113 					   ScanKey key);
114 extern HeapScanDesc heap_beginscan_strat(Relation relation, Snapshot snapshot,
115 					 int nkeys, ScanKey key,
116 					 bool allow_strat, bool allow_sync);
117 extern HeapScanDesc heap_beginscan_bm(Relation relation, Snapshot snapshot,
118 				  int nkeys, ScanKey key);
119 extern HeapScanDesc heap_beginscan_sampling(Relation relation,
120 						Snapshot snapshot, int nkeys, ScanKey key,
121 						bool allow_strat, bool allow_sync, bool allow_pagemode);
122 extern void heap_setscanlimits(HeapScanDesc scan, BlockNumber startBlk,
123 				   BlockNumber endBlk);
124 extern void heapgetpage(HeapScanDesc scan, BlockNumber page);
125 extern void heap_rescan(HeapScanDesc scan, ScanKey key);
126 extern void heap_rescan_set_params(HeapScanDesc scan, ScanKey key,
127 					   bool allow_strat, bool allow_sync, bool allow_pagemode);
128 extern void heap_endscan(HeapScanDesc scan);
129 extern HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction);
130 
131 extern Size heap_parallelscan_estimate(Snapshot snapshot);
132 extern void heap_parallelscan_initialize(ParallelHeapScanDesc target,
133 							 Relation relation, Snapshot snapshot);
134 extern void heap_parallelscan_reinitialize(ParallelHeapScanDesc parallel_scan);
135 extern HeapScanDesc heap_beginscan_parallel(Relation, ParallelHeapScanDesc);
136 
137 extern bool heap_fetch(Relation relation, Snapshot snapshot,
138 		   HeapTuple tuple, Buffer *userbuf, bool keep_buf,
139 		   Relation stats_relation);
140 extern bool heap_hot_search_buffer(ItemPointer tid, Relation relation,
141 					   Buffer buffer, Snapshot snapshot, HeapTuple heapTuple,
142 					   bool *all_dead, bool first_call);
143 extern bool heap_hot_search(ItemPointer tid, Relation relation,
144 				Snapshot snapshot, bool *all_dead);
145 
146 extern void heap_get_latest_tid(Relation relation, Snapshot snapshot,
147 					ItemPointer tid);
148 extern void setLastTid(const ItemPointer tid);
149 
150 extern BulkInsertState GetBulkInsertState(void);
151 extern void FreeBulkInsertState(BulkInsertState);
152 extern void ReleaseBulkInsertStatePin(BulkInsertState bistate);
153 
154 extern Oid heap_insert(Relation relation, HeapTuple tup, CommandId cid,
155 			int options, BulkInsertState bistate);
156 extern void heap_multi_insert(Relation relation, HeapTuple *tuples, int ntuples,
157 				  CommandId cid, int options, BulkInsertState bistate);
158 extern HTSU_Result heap_delete(Relation relation, ItemPointer tid,
159 			CommandId cid, Snapshot crosscheck, bool wait,
160 			HeapUpdateFailureData *hufd, bool changingPart);
161 extern void heap_finish_speculative(Relation relation, HeapTuple tuple);
162 extern void heap_abort_speculative(Relation relation, HeapTuple tuple);
163 extern HTSU_Result heap_update(Relation relation, ItemPointer otid,
164 			HeapTuple newtup,
165 			CommandId cid, Snapshot crosscheck, bool wait,
166 			HeapUpdateFailureData *hufd, LockTupleMode *lockmode);
167 extern HTSU_Result heap_lock_tuple(Relation relation, HeapTuple tuple,
168 				CommandId cid, LockTupleMode mode, LockWaitPolicy wait_policy,
169 				bool follow_update,
170 				Buffer *buffer, HeapUpdateFailureData *hufd);
171 extern void heap_inplace_update(Relation relation, HeapTuple tuple);
172 extern bool heap_freeze_tuple(HeapTupleHeader tuple,
173 				  TransactionId relfrozenxid, TransactionId relminmxid,
174 				  TransactionId cutoff_xid, TransactionId cutoff_multi);
175 extern bool heap_tuple_needs_freeze(HeapTupleHeader tuple, TransactionId cutoff_xid,
176 						MultiXactId cutoff_multi, Buffer buf);
177 extern bool heap_tuple_needs_eventual_freeze(HeapTupleHeader tuple);
178 
179 extern Oid	simple_heap_insert(Relation relation, HeapTuple tup);
180 extern void simple_heap_delete(Relation relation, ItemPointer tid);
181 extern void simple_heap_update(Relation relation, ItemPointer otid,
182 				   HeapTuple tup);
183 
184 extern void heap_sync(Relation relation);
185 extern void heap_update_snapshot(HeapScanDesc scan, Snapshot snapshot);
186 
187 /* in heap/pruneheap.c */
188 extern void heap_page_prune_opt(Relation relation, Buffer buffer);
189 extern int heap_page_prune(Relation relation, Buffer buffer,
190 				TransactionId OldestXmin,
191 				bool report_stats, TransactionId *latestRemovedXid);
192 extern void heap_page_prune_execute(Buffer buffer,
193 						OffsetNumber *redirected, int nredirected,
194 						OffsetNumber *nowdead, int ndead,
195 						OffsetNumber *nowunused, int nunused);
196 extern void heap_get_root_tuples(Page page, OffsetNumber *root_offsets);
197 
198 /* in heap/syncscan.c */
199 extern void ss_report_location(Relation rel, BlockNumber location);
200 extern BlockNumber ss_get_location(Relation rel, BlockNumber relnblocks);
201 extern void SyncScanShmemInit(void);
202 extern Size SyncScanShmemSize(void);
203 
204 #endif							/* HEAPAM_H */
205