1 /*-------------------------------------------------------------------------
2  *
3  * heapam.h
4  *	  POSTGRES heap access method definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2016, 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/lock.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 HeapScanDesc heap_beginscan_parallel(Relation, ParallelHeapScanDesc);
135 
136 extern bool heap_fetch(Relation relation, Snapshot snapshot,
137 		   HeapTuple tuple, Buffer *userbuf, bool keep_buf,
138 		   Relation stats_relation);
139 extern bool heap_hot_search_buffer(ItemPointer tid, Relation relation,
140 					   Buffer buffer, Snapshot snapshot, HeapTuple heapTuple,
141 					   bool *all_dead, bool first_call);
142 extern bool heap_hot_search(ItemPointer tid, Relation relation,
143 				Snapshot snapshot, bool *all_dead);
144 
145 extern void heap_get_latest_tid(Relation relation, Snapshot snapshot,
146 					ItemPointer tid);
147 extern void setLastTid(const ItemPointer tid);
148 
149 extern BulkInsertState GetBulkInsertState(void);
150 extern void FreeBulkInsertState(BulkInsertState);
151 
152 extern Oid heap_insert(Relation relation, HeapTuple tup, CommandId cid,
153 			int options, BulkInsertState bistate);
154 extern void heap_multi_insert(Relation relation, HeapTuple *tuples, int ntuples,
155 				  CommandId cid, int options, BulkInsertState bistate);
156 extern HTSU_Result heap_delete(Relation relation, ItemPointer tid,
157 			CommandId cid, Snapshot crosscheck, bool wait,
158 			HeapUpdateFailureData *hufd);
159 extern void heap_finish_speculative(Relation relation, HeapTuple tuple);
160 extern void heap_abort_speculative(Relation relation, HeapTuple tuple);
161 extern HTSU_Result heap_update(Relation relation, ItemPointer otid,
162 			HeapTuple newtup,
163 			CommandId cid, Snapshot crosscheck, bool wait,
164 			HeapUpdateFailureData *hufd, LockTupleMode *lockmode);
165 extern HTSU_Result heap_lock_tuple(Relation relation, HeapTuple tuple,
166 				CommandId cid, LockTupleMode mode, LockWaitPolicy wait_policy,
167 				bool follow_update,
168 				Buffer *buffer, HeapUpdateFailureData *hufd);
169 extern void heap_inplace_update(Relation relation, HeapTuple tuple);
170 extern bool heap_freeze_tuple(HeapTupleHeader tuple,
171 				  TransactionId relfrozenxid, TransactionId relminmxid,
172 				  TransactionId cutoff_xid, TransactionId cutoff_multi);
173 extern bool heap_tuple_needs_freeze(HeapTupleHeader tuple, TransactionId cutoff_xid,
174 						MultiXactId cutoff_multi, Buffer buf);
175 extern bool heap_tuple_needs_eventual_freeze(HeapTupleHeader tuple);
176 
177 extern Oid	simple_heap_insert(Relation relation, HeapTuple tup);
178 extern void simple_heap_delete(Relation relation, ItemPointer tid);
179 extern void simple_heap_update(Relation relation, ItemPointer otid,
180 				   HeapTuple tup);
181 
182 extern void heap_sync(Relation relation);
183 
184 /* in heap/pruneheap.c */
185 extern void heap_page_prune_opt(Relation relation, Buffer buffer);
186 extern int heap_page_prune(Relation relation, Buffer buffer,
187 				TransactionId OldestXmin,
188 				bool report_stats, TransactionId *latestRemovedXid);
189 extern void heap_page_prune_execute(Buffer buffer,
190 						OffsetNumber *redirected, int nredirected,
191 						OffsetNumber *nowdead, int ndead,
192 						OffsetNumber *nowunused, int nunused);
193 extern void heap_get_root_tuples(Page page, OffsetNumber *root_offsets);
194 
195 /* in heap/syncscan.c */
196 extern void ss_report_location(Relation rel, BlockNumber location);
197 extern BlockNumber ss_get_location(Relation rel, BlockNumber relnblocks);
198 extern void SyncScanShmemInit(void);
199 extern Size SyncScanShmemSize(void);
200 
201 #endif   /* HEAPAM_H */
202