OSDN Git Service

Use Snapshot in heap access methods.
[pg-rex/syncrep.git] / src / include / access / heapam.h
1 /*-------------------------------------------------------------------------
2  *
3  * heapam.h--
4  *        POSTGRES heap access method definitions.
5  *
6  *
7  * Copyright (c) 1994, Regents of the University of California
8  *
9  * $Id: heapam.h,v 1.34 1998/07/27 19:38:29 vadim Exp $
10  *
11  *-------------------------------------------------------------------------
12  */
13 #ifndef HEAPAM_H
14 #define HEAPAM_H
15
16 #include <access/tupmacs.h>
17 #include <access/htup.h>
18 #include <access/relscan.h>
19 #include <storage/block.h>
20 #include <utils/rel.h>
21 #include <utils/tqual.h>
22
23 /* ----------------------------------------------------------------
24  *                              heap access method statistics
25  * ----------------------------------------------------------------
26  */
27
28 typedef struct HeapAccessStatisticsData
29 {
30         time_t          init_global_timestamp;  /* time global statistics started */
31         time_t          local_reset_timestamp;  /* last time local reset was done */
32         time_t          last_request_timestamp; /* last time stats were requested */
33
34         int                     global_open;
35         int                     global_openr;
36         int                     global_close;
37         int                     global_beginscan;
38         int                     global_rescan;
39         int                     global_endscan;
40         int                     global_getnext;
41         int                     global_fetch;
42         int                     global_insert;
43         int                     global_delete;
44         int                     global_replace;
45         int                     global_markpos;
46         int                     global_restrpos;
47         int                     global_BufferGetRelation;
48         int                     global_RelationIdGetRelation;
49         int                     global_RelationIdGetRelation_Buf;
50         int                     global_RelationNameGetRelation;
51         int                     global_getreldesc;
52         int                     global_heapgettup;
53         int                     global_RelationPutHeapTuple;
54         int                     global_RelationPutLongHeapTuple;
55
56         int                     local_open;
57         int                     local_openr;
58         int                     local_close;
59         int                     local_beginscan;
60         int                     local_rescan;
61         int                     local_endscan;
62         int                     local_getnext;
63         int                     local_fetch;
64         int                     local_insert;
65         int                     local_delete;
66         int                     local_replace;
67         int                     local_markpos;
68         int                     local_restrpos;
69         int                     local_BufferGetRelation;
70         int                     local_RelationIdGetRelation;
71         int                     local_RelationIdGetRelation_Buf;
72         int                     local_RelationNameGetRelation;
73         int                     local_getreldesc;
74         int                     local_heapgettup;
75         int                     local_RelationPutHeapTuple;
76         int                     local_RelationPutLongHeapTuple;
77 } HeapAccessStatisticsData;
78
79 typedef HeapAccessStatisticsData *HeapAccessStatistics;
80
81 #define IncrHeapAccessStat(x) \
82         (heap_access_stats == NULL ? 0 : (heap_access_stats->x)++)
83
84 /* ----------------
85  *              fastgetattr
86  *
87  *              This gets called many times, so we macro the cacheable and NULL
88  *              lookups, and call noncachegetattr() for the rest.
89  *
90  * ----------------
91  */
92 #if !defined(DISABLE_COMPLEX_MACRO)
93
94 #define fastgetattr(tup, attnum, tupleDesc, isnull) \
95 ( \
96         AssertMacro((attnum) > 0), \
97         ((isnull) ? (*(isnull) = false) : (dummyret)NULL), \
98         HeapTupleNoNulls(tup) ? \
99         ( \
100                 ((tupleDesc)->attrs[(attnum)-1]->attcacheoff != -1 || \
101                  (attnum) == 1) ? \
102                 ( \
103                         (Datum)fetchatt(&((tupleDesc)->attrs[(attnum)-1]), \
104                                 (char *) (tup) + (tup)->t_hoff + \
105                                 ( \
106                                         ((attnum) != 1) ? \
107                                                 (tupleDesc)->attrs[(attnum)-1]->attcacheoff \
108                                         : \
109                                                 0 \
110                                 ) \
111                         ) \
112                 ) \
113                 : \
114                         nocachegetattr((tup), (attnum), (tupleDesc), (isnull)) \
115         ) \
116         : \
117         ( \
118                 att_isnull((attnum)-1, (tup)->t_bits) ? \
119                 ( \
120                         ((isnull) ? (*(isnull) = true) : (dummyret)NULL), \
121                         (Datum)NULL \
122                 ) \
123                 : \
124                 ( \
125                         nocachegetattr((tup), (attnum), (tupleDesc), (isnull)) \
126                 ) \
127         ) \
128 )
129
130 #else /* !defined(DISABLE_COMPLEX_MACRO) */
131
132 extern Datum nocachegetattr(HeapTuple tup, int attnum,
133                                                  TupleDesc att, bool *isnull);
134
135 static Datum fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc,
136                                                  bool *isnull)
137 {
138     return (
139                 (attnum) > 0 ?
140                 (
141                         ((isnull) ? (*(isnull) = false) : (dummyret)NULL),
142                         HeapTupleNoNulls(tup) ?
143                         (
144                                 ((tupleDesc)->attrs[(attnum)-1]->attcacheoff != -1 ||
145                                  (attnum) == 1) ?
146                                 (
147                                         (Datum)fetchatt(&((tupleDesc)->attrs[(attnum)-1]),
148                                                 (char *) (tup) + (tup)->t_hoff +
149                                                 (
150                                                         ((attnum) != 1) ?
151                                                                 (tupleDesc)->attrs[(attnum)-1]->attcacheoff
152                                                         :
153                                                                 0
154                                                 )
155                                         )
156                                 )
157                                 :
158                                         nocachegetattr((tup), (attnum), (tupleDesc), (isnull))
159                         )
160                         :
161                         (
162                                 att_isnull((attnum)-1, (tup)->t_bits) ?
163                                 (
164                                         ((isnull) ? (*(isnull) = true) : (dummyret)NULL),
165                                         (Datum)NULL
166                                 )
167                                 :
168                                 (
169                                         nocachegetattr((tup), (attnum), (tupleDesc), (isnull))
170                                 )
171                         )
172                 )
173                 :
174                 (
175                          (Datum)NULL
176                 )
177         );
178 }
179
180 #endif
181
182 /* ----------------
183  *              heap_getattr
184  *
185  *              Find a particular field in a row represented as a heap tuple.
186  *              We return a pointer into that heap tuple, which points to the
187  *              first byte of the value of the field in question.
188  *
189  *              If the field in question has a NULL value, we return a null
190  *              pointer and return <*isnull> == true.  Otherwise, we return
191  *              <*isnull> == false.
192  *
193  *              <tup> is the pointer to the heap tuple.  <attnum> is the attribute
194  *              number of the column (field) caller wants.      <tupleDesc> is a
195  *              pointer to the structure describing the row and all its fields.
196  *
197  *              Because this macro is often called with constants, it generates
198  *              compiler warnings about 'left-hand comma expression has no effect.
199  *
200  * ----------------
201  */
202 #define heap_getattr(tup, attnum, tupleDesc, isnull) \
203 ( \
204         AssertMacro((tup) != NULL && \
205                 (attnum) > FirstLowInvalidHeapAttributeNumber && \
206                 (attnum) != 0), \
207         ((attnum) > (int) (tup)->t_natts) ? \
208         ( \
209                 ((isnull) ? (*(isnull) = true) : (dummyret)NULL), \
210                 (Datum)NULL \
211         ) \
212         : \
213         ( \
214                 ((attnum) > 0) ? \
215                 ( \
216                         fastgetattr((tup), (attnum), (tupleDesc), (isnull)) \
217                 ) \
218                 : \
219                 ( \
220                         ((isnull) ? (*(isnull) = false) : (dummyret)NULL), \
221                         ((attnum) == SelfItemPointerAttributeNumber) ? \
222                         ( \
223                                 (Datum)((char *)(tup) + \
224                                         heap_sysoffset[-SelfItemPointerAttributeNumber-1]) \
225                         ) \
226                         : \
227                         ( \
228                                 (Datum)*(unsigned int *) \
229                                         ((char *)(tup) + heap_sysoffset[-(attnum)-1]) \
230                         ) \
231                 ) \
232         ) \
233 )
234
235 extern HeapAccessStatistics heap_access_stats;  /* in stats.c */
236
237 /* ----------------
238  *              function prototypes for heap access method
239  * ----------------
240  */
241 /* heap_create, heap_creatr, and heap_destroy are declared in catalog/heap.h */
242
243 /* heapam.c */
244 extern void doinsert(Relation relation, HeapTuple tup);
245
246 extern Relation heap_open(Oid relationId);
247 extern Relation heap_openr(char *relationName);
248 extern void heap_close(Relation relation);
249 extern HeapScanDesc
250 heap_beginscan(Relation relation, int atend,
251                            Snapshot snapshot, unsigned nkeys, ScanKey key);
252 extern void heap_rescan(HeapScanDesc sdesc, bool scanFromEnd, ScanKey key);
253 extern void heap_endscan(HeapScanDesc sdesc);
254 extern HeapTuple heap_getnext(HeapScanDesc scandesc, int backw, Buffer *b);
255 extern HeapTuple heap_fetch(Relation relation, Snapshot snapshot, ItemPointer tid, Buffer *b);
256 extern Oid      heap_insert(Relation relation, HeapTuple tup);
257 extern int      heap_delete(Relation relation, ItemPointer tid);
258 extern int
259 heap_replace(Relation relation, ItemPointer otid,
260                          HeapTuple tup);
261 extern void heap_markpos(HeapScanDesc sdesc);
262 extern void heap_restrpos(HeapScanDesc sdesc);
263
264 /* in common/heaptuple.c */
265 extern Size ComputeDataSize(TupleDesc tupleDesc, Datum value[], char nulls[]);
266 extern void
267 DataFill(char *data, TupleDesc tupleDesc,
268                  Datum value[], char nulls[], uint16 *infomask,
269                  bits8 *bit);
270 extern int      heap_attisnull(HeapTuple tup, int attnum);
271 extern int      heap_sysattrlen(AttrNumber attno);
272 extern bool heap_sysattrbyval(AttrNumber attno);
273 extern Datum heap_getsysattr(HeapTuple tup, Buffer b, int attnum);
274 extern Datum
275 nocachegetattr(HeapTuple tup, int attnum,
276                            TupleDesc att, bool *isnull);
277 extern HeapTuple heap_copytuple(HeapTuple tuple);
278 extern HeapTuple
279 heap_formtuple(TupleDesc tupleDescriptor,
280                            Datum value[], char nulls[]);
281 extern HeapTuple
282 heap_modifytuple(HeapTuple tuple, Buffer buffer,
283          Relation relation, Datum replValue[], char replNull[], char repl[]);
284 HeapTuple       heap_addheader(uint32 natts, int structlen, char *structure);
285
286 /* in common/heap/stats.c */
287 extern void PrintHeapAccessStatistics(HeapAccessStatistics stats);
288 extern void initam(void);
289
290 /* hio.c */
291 extern void
292 RelationPutHeapTuple(Relation relation, BlockNumber blockIndex,
293                                          HeapTuple tuple);
294 extern void RelationPutHeapTupleAtEnd(Relation relation, HeapTuple tuple);
295
296 #endif                                                  /* HEAPAM_H */