]> granicus.if.org Git - postgresql/blob - src/include/access/htup.h
c0258354e6f91c92559bf36be752011b9ba8bf32
[postgresql] / src / include / access / htup.h
1 /*-------------------------------------------------------------------------
2  *
3  * htup.h
4  *        POSTGRES heap tuple definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/access/htup.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef HTUP_H
15 #define HTUP_H
16
17 #include "access/tupdesc.h"
18 #include "access/tupmacs.h"
19 #include "storage/bufpage.h"
20 #include "storage/itemptr.h"
21 #include "storage/relfilenode.h"
22
23 /*
24  * MaxTupleAttributeNumber limits the number of (user) columns in a tuple.
25  * The key limit on this value is that the size of the fixed overhead for
26  * a tuple, plus the size of the null-values bitmap (at 1 bit per column),
27  * plus MAXALIGN alignment, must fit into t_hoff which is uint8.  On most
28  * machines the upper limit without making t_hoff wider would be a little
29  * over 1700.  We use round numbers here and for MaxHeapAttributeNumber
30  * so that alterations in HeapTupleHeaderData layout won't change the
31  * supported max number of columns.
32  */
33 #define MaxTupleAttributeNumber 1664    /* 8 * 208 */
34
35 /*
36  * MaxHeapAttributeNumber limits the number of (user) columns in a table.
37  * This should be somewhat less than MaxTupleAttributeNumber.  It must be
38  * at least one less, else we will fail to do UPDATEs on a maximal-width
39  * table (because UPDATE has to form working tuples that include CTID).
40  * In practice we want some additional daylight so that we can gracefully
41  * support operations that add hidden "resjunk" columns, for example
42  * SELECT * FROM wide_table ORDER BY foo, bar, baz.
43  * In any case, depending on column data types you will likely be running
44  * into the disk-block-based limit on overall tuple size if you have more
45  * than a thousand or so columns.  TOAST won't help.
46  */
47 #define MaxHeapAttributeNumber  1600    /* 8 * 200 */
48
49 /*
50  * Heap tuple header.  To avoid wasting space, the fields should be
51  * laid out in such a way as to avoid structure padding.
52  *
53  * Datums of composite types (row types) share the same general structure
54  * as on-disk tuples, so that the same routines can be used to build and
55  * examine them.  However the requirements are slightly different: a Datum
56  * does not need any transaction visibility information, and it does need
57  * a length word and some embedded type information.  We can achieve this
58  * by overlaying the xmin/cmin/xmax/cmax/xvac fields of a heap tuple
59  * with the fields needed in the Datum case.  Typically, all tuples built
60  * in-memory will be initialized with the Datum fields; but when a tuple is
61  * about to be inserted in a table, the transaction fields will be filled,
62  * overwriting the datum fields.
63  *
64  * The overall structure of a heap tuple looks like:
65  *                      fixed fields (HeapTupleHeaderData struct)
66  *                      nulls bitmap (if HEAP_HASNULL is set in t_infomask)
67  *                      alignment padding (as needed to make user data MAXALIGN'd)
68  *                      object ID (if HEAP_HASOID is set in t_infomask)
69  *                      user data fields
70  *
71  * We store five "virtual" fields Xmin, Cmin, Xmax, Cmax, and Xvac in three
72  * physical fields.  Xmin and Xmax are always really stored, but Cmin, Cmax
73  * and Xvac share a field.      This works because we know that Cmin and Cmax
74  * are only interesting for the lifetime of the inserting and deleting
75  * transaction respectively.  If a tuple is inserted and deleted in the same
76  * transaction, we store a "combo" command id that can be mapped to the real
77  * cmin and cmax, but only by use of local state within the originating
78  * backend.  See combocid.c for more details.  Meanwhile, Xvac is only set by
79  * old-style VACUUM FULL, which does not have any command sub-structure and so
80  * does not need either Cmin or Cmax.  (This requires that old-style VACUUM
81  * FULL never try to move a tuple whose Cmin or Cmax is still interesting,
82  * ie, an insert-in-progress or delete-in-progress tuple.)
83  *
84  * A word about t_ctid: whenever a new tuple is stored on disk, its t_ctid
85  * is initialized with its own TID (location).  If the tuple is ever updated,
86  * its t_ctid is changed to point to the replacement version of the tuple.
87  * Thus, a tuple is the latest version of its row iff XMAX is invalid or
88  * t_ctid points to itself (in which case, if XMAX is valid, the tuple is
89  * either locked or deleted).  One can follow the chain of t_ctid links
90  * to find the newest version of the row.  Beware however that VACUUM might
91  * erase the pointed-to (newer) tuple before erasing the pointing (older)
92  * tuple.  Hence, when following a t_ctid link, it is necessary to check
93  * to see if the referenced slot is empty or contains an unrelated tuple.
94  * Check that the referenced tuple has XMIN equal to the referencing tuple's
95  * XMAX to verify that it is actually the descendant version and not an
96  * unrelated tuple stored into a slot recently freed by VACUUM.  If either
97  * check fails, one may assume that there is no live descendant version.
98  *
99  * Following the fixed header fields, the nulls bitmap is stored (beginning
100  * at t_bits).  The bitmap is *not* stored if t_infomask shows that there
101  * are no nulls in the tuple.  If an OID field is present (as indicated by
102  * t_infomask), then it is stored just before the user data, which begins at
103  * the offset shown by t_hoff.  Note that t_hoff must be a multiple of
104  * MAXALIGN.
105  */
106
107 typedef struct HeapTupleFields
108 {
109         TransactionId t_xmin;           /* inserting xact ID */
110         TransactionId t_xmax;           /* deleting or locking xact ID */
111
112         union
113         {
114                 CommandId       t_cid;          /* inserting or deleting command ID, or both */
115                 TransactionId t_xvac;   /* old-style VACUUM FULL xact ID */
116         }                       t_field3;
117 } HeapTupleFields;
118
119 typedef struct DatumTupleFields
120 {
121         int32           datum_len_;             /* varlena header (do not touch directly!) */
122
123         int32           datum_typmod;   /* -1, or identifier of a record type */
124
125         Oid                     datum_typeid;   /* composite type OID, or RECORDOID */
126
127         /*
128          * Note: field ordering is chosen with thought that Oid might someday
129          * widen to 64 bits.
130          */
131 } DatumTupleFields;
132
133 typedef struct HeapTupleHeaderData
134 {
135         union
136         {
137                 HeapTupleFields t_heap;
138                 DatumTupleFields t_datum;
139         }                       t_choice;
140
141         ItemPointerData t_ctid;         /* current TID of this or newer tuple */
142
143         /* Fields below here must match MinimalTupleData! */
144
145         uint16          t_infomask2;    /* number of attributes + various flags */
146
147         uint16          t_infomask;             /* various flag bits, see below */
148
149         uint8           t_hoff;                 /* sizeof header incl. bitmap, padding */
150
151         /* ^ - 23 bytes - ^ */
152
153         bits8           t_bits[1];              /* bitmap of NULLs -- VARIABLE LENGTH */
154
155         /* MORE DATA FOLLOWS AT END OF STRUCT */
156 } HeapTupleHeaderData;
157
158 typedef HeapTupleHeaderData *HeapTupleHeader;
159
160 /*
161  * information stored in t_infomask:
162  */
163 #define HEAP_HASNULL                    0x0001  /* has null attribute(s) */
164 #define HEAP_HASVARWIDTH                0x0002  /* has variable-width attribute(s) */
165 #define HEAP_HASEXTERNAL                0x0004  /* has external stored attribute(s) */
166 #define HEAP_HASOID                             0x0008  /* has an object-id field */
167 /* bit 0x0010 is available */
168 #define HEAP_COMBOCID                   0x0020  /* t_cid is a combo cid */
169 #define HEAP_XMAX_EXCL_LOCK             0x0040  /* xmax is exclusive locker */
170 #define HEAP_XMAX_SHARED_LOCK   0x0080  /* xmax is shared locker */
171 /* if either LOCK bit is set, xmax hasn't deleted the tuple, only locked it */
172 #define HEAP_IS_LOCKED  (HEAP_XMAX_EXCL_LOCK | HEAP_XMAX_SHARED_LOCK)
173 #define HEAP_XMIN_COMMITTED             0x0100  /* t_xmin committed */
174 #define HEAP_XMIN_INVALID               0x0200  /* t_xmin invalid/aborted */
175 #define HEAP_XMAX_COMMITTED             0x0400  /* t_xmax committed */
176 #define HEAP_XMAX_INVALID               0x0800  /* t_xmax invalid/aborted */
177 #define HEAP_XMAX_IS_MULTI              0x1000  /* t_xmax is a MultiXactId */
178 #define HEAP_UPDATED                    0x2000  /* this is UPDATEd version of row */
179 #define HEAP_MOVED_OFF                  0x4000  /* moved to another place by pre-9.0
180                                                                                  * VACUUM FULL; kept for binary
181                                                                                  * upgrade support */
182 #define HEAP_MOVED_IN                   0x8000  /* moved from another place by pre-9.0
183                                                                                  * VACUUM FULL; kept for binary
184                                                                                  * upgrade support */
185 #define HEAP_MOVED (HEAP_MOVED_OFF | HEAP_MOVED_IN)
186
187 #define HEAP_XACT_MASK                  0xFFE0  /* visibility-related bits */
188
189 /*
190  * information stored in t_infomask2:
191  */
192 #define HEAP_NATTS_MASK                 0x07FF  /* 11 bits for number of attributes */
193 /* bits 0x3800 are available */
194 #define HEAP_HOT_UPDATED                0x4000  /* tuple was HOT-updated */
195 #define HEAP_ONLY_TUPLE                 0x8000  /* this is heap-only tuple */
196
197 #define HEAP2_XACT_MASK                 0xC000  /* visibility-related bits */
198
199 /*
200  * HEAP_TUPLE_HAS_MATCH is a temporary flag used during hash joins.  It is
201  * only used in tuples that are in the hash table, and those don't need
202  * any visibility information, so we can overlay it on a visibility flag
203  * instead of using up a dedicated bit.
204  */
205 #define HEAP_TUPLE_HAS_MATCH    HEAP_ONLY_TUPLE /* tuple has a join match */
206
207 /*
208  * HeapTupleHeader accessor macros
209  *
210  * Note: beware of multiple evaluations of "tup" argument.      But the Set
211  * macros evaluate their other argument only once.
212  */
213
214 #define HeapTupleHeaderGetXmin(tup) \
215 ( \
216         (tup)->t_choice.t_heap.t_xmin \
217 )
218
219 #define HeapTupleHeaderSetXmin(tup, xid) \
220 ( \
221         (tup)->t_choice.t_heap.t_xmin = (xid) \
222 )
223
224 #define HeapTupleHeaderGetXmax(tup) \
225 ( \
226         (tup)->t_choice.t_heap.t_xmax \
227 )
228
229 #define HeapTupleHeaderSetXmax(tup, xid) \
230 ( \
231         (tup)->t_choice.t_heap.t_xmax = (xid) \
232 )
233
234 /*
235  * HeapTupleHeaderGetRawCommandId will give you what's in the header whether
236  * it is useful or not.  Most code should use HeapTupleHeaderGetCmin or
237  * HeapTupleHeaderGetCmax instead, but note that those Assert that you can
238  * get a legitimate result, ie you are in the originating transaction!
239  */
240 #define HeapTupleHeaderGetRawCommandId(tup) \
241 ( \
242         (tup)->t_choice.t_heap.t_field3.t_cid \
243 )
244
245 /* SetCmin is reasonably simple since we never need a combo CID */
246 #define HeapTupleHeaderSetCmin(tup, cid) \
247 do { \
248         Assert(!((tup)->t_infomask & HEAP_MOVED)); \
249         (tup)->t_choice.t_heap.t_field3.t_cid = (cid); \
250         (tup)->t_infomask &= ~HEAP_COMBOCID; \
251 } while (0)
252
253 /* SetCmax must be used after HeapTupleHeaderAdjustCmax; see combocid.c */
254 #define HeapTupleHeaderSetCmax(tup, cid, iscombo) \
255 do { \
256         Assert(!((tup)->t_infomask & HEAP_MOVED)); \
257         (tup)->t_choice.t_heap.t_field3.t_cid = (cid); \
258         if (iscombo) \
259                 (tup)->t_infomask |= HEAP_COMBOCID; \
260         else \
261                 (tup)->t_infomask &= ~HEAP_COMBOCID; \
262 } while (0)
263
264 #define HeapTupleHeaderGetXvac(tup) \
265 ( \
266         ((tup)->t_infomask & HEAP_MOVED) ? \
267                 (tup)->t_choice.t_heap.t_field3.t_xvac \
268         : \
269                 InvalidTransactionId \
270 )
271
272 #define HeapTupleHeaderSetXvac(tup, xid) \
273 do { \
274         Assert((tup)->t_infomask & HEAP_MOVED); \
275         (tup)->t_choice.t_heap.t_field3.t_xvac = (xid); \
276 } while (0)
277
278 #define HeapTupleHeaderGetDatumLength(tup) \
279         VARSIZE(tup)
280
281 #define HeapTupleHeaderSetDatumLength(tup, len) \
282         SET_VARSIZE(tup, len)
283
284 #define HeapTupleHeaderGetTypeId(tup) \
285 ( \
286         (tup)->t_choice.t_datum.datum_typeid \
287 )
288
289 #define HeapTupleHeaderSetTypeId(tup, typeid) \
290 ( \
291         (tup)->t_choice.t_datum.datum_typeid = (typeid) \
292 )
293
294 #define HeapTupleHeaderGetTypMod(tup) \
295 ( \
296         (tup)->t_choice.t_datum.datum_typmod \
297 )
298
299 #define HeapTupleHeaderSetTypMod(tup, typmod) \
300 ( \
301         (tup)->t_choice.t_datum.datum_typmod = (typmod) \
302 )
303
304 #define HeapTupleHeaderGetOid(tup) \
305 ( \
306         ((tup)->t_infomask & HEAP_HASOID) ? \
307                 *((Oid *) ((char *)(tup) + (tup)->t_hoff - sizeof(Oid))) \
308         : \
309                 InvalidOid \
310 )
311
312 #define HeapTupleHeaderSetOid(tup, oid) \
313 do { \
314         Assert((tup)->t_infomask & HEAP_HASOID); \
315         *((Oid *) ((char *)(tup) + (tup)->t_hoff - sizeof(Oid))) = (oid); \
316 } while (0)
317
318 /*
319  * Note that we stop considering a tuple HOT-updated as soon as it is known
320  * aborted or the would-be updating transaction is known aborted.  For best
321  * efficiency, check tuple visibility before using this macro, so that the
322  * INVALID bits will be as up to date as possible.
323  */
324 #define HeapTupleHeaderIsHotUpdated(tup) \
325 ( \
326         ((tup)->t_infomask2 & HEAP_HOT_UPDATED) != 0 && \
327         ((tup)->t_infomask & (HEAP_XMIN_INVALID | HEAP_XMAX_INVALID)) == 0 \
328 )
329
330 #define HeapTupleHeaderSetHotUpdated(tup) \
331 ( \
332         (tup)->t_infomask2 |= HEAP_HOT_UPDATED \
333 )
334
335 #define HeapTupleHeaderClearHotUpdated(tup) \
336 ( \
337         (tup)->t_infomask2 &= ~HEAP_HOT_UPDATED \
338 )
339
340 #define HeapTupleHeaderIsHeapOnly(tup) \
341 ( \
342   (tup)->t_infomask2 & HEAP_ONLY_TUPLE \
343 )
344
345 #define HeapTupleHeaderSetHeapOnly(tup) \
346 ( \
347   (tup)->t_infomask2 |= HEAP_ONLY_TUPLE \
348 )
349
350 #define HeapTupleHeaderClearHeapOnly(tup) \
351 ( \
352   (tup)->t_infomask2 &= ~HEAP_ONLY_TUPLE \
353 )
354
355 #define HeapTupleHeaderHasMatch(tup) \
356 ( \
357   (tup)->t_infomask2 & HEAP_TUPLE_HAS_MATCH \
358 )
359
360 #define HeapTupleHeaderSetMatch(tup) \
361 ( \
362   (tup)->t_infomask2 |= HEAP_TUPLE_HAS_MATCH \
363 )
364
365 #define HeapTupleHeaderClearMatch(tup) \
366 ( \
367   (tup)->t_infomask2 &= ~HEAP_TUPLE_HAS_MATCH \
368 )
369
370 #define HeapTupleHeaderGetNatts(tup) \
371         ((tup)->t_infomask2 & HEAP_NATTS_MASK)
372
373 #define HeapTupleHeaderSetNatts(tup, natts) \
374 ( \
375         (tup)->t_infomask2 = ((tup)->t_infomask2 & ~HEAP_NATTS_MASK) | (natts) \
376 )
377
378
379 /*
380  * BITMAPLEN(NATTS) -
381  *              Computes size of null bitmap given number of data columns.
382  */
383 #define BITMAPLEN(NATTS)        (((int)(NATTS) + 7) / 8)
384
385 /*
386  * MaxHeapTupleSize is the maximum allowed size of a heap tuple, including
387  * header and MAXALIGN alignment padding.  Basically it's BLCKSZ minus the
388  * other stuff that has to be on a disk page.  Since heap pages use no
389  * "special space", there's no deduction for that.
390  *
391  * NOTE: we allow for the ItemId that must point to the tuple, ensuring that
392  * an otherwise-empty page can indeed hold a tuple of this size.  Because
393  * ItemIds and tuples have different alignment requirements, don't assume that
394  * you can, say, fit 2 tuples of size MaxHeapTupleSize/2 on the same page.
395  */
396 #define MaxHeapTupleSize  (BLCKSZ - MAXALIGN(SizeOfPageHeaderData + sizeof(ItemIdData)))
397
398 /*
399  * MaxHeapTuplesPerPage is an upper bound on the number of tuples that can
400  * fit on one heap page.  (Note that indexes could have more, because they
401  * use a smaller tuple header.)  We arrive at the divisor because each tuple
402  * must be maxaligned, and it must have an associated item pointer.
403  *
404  * Note: with HOT, there could theoretically be more line pointers (not actual
405  * tuples) than this on a heap page.  However we constrain the number of line
406  * pointers to this anyway, to avoid excessive line-pointer bloat and not
407  * require increases in the size of work arrays.
408  */
409 #define MaxHeapTuplesPerPage    \
410         ((int) ((BLCKSZ - SizeOfPageHeaderData) / \
411                         (MAXALIGN(offsetof(HeapTupleHeaderData, t_bits)) + sizeof(ItemIdData))))
412
413 /*
414  * MaxAttrSize is a somewhat arbitrary upper limit on the declared size of
415  * data fields of char(n) and similar types.  It need not have anything
416  * directly to do with the *actual* upper limit of varlena values, which
417  * is currently 1Gb (see TOAST structures in postgres.h).  I've set it
418  * at 10Mb which seems like a reasonable number --- tgl 8/6/00.
419  */
420 #define MaxAttrSize             (10 * 1024 * 1024)
421
422
423 /*
424  * MinimalTuple is an alternative representation that is used for transient
425  * tuples inside the executor, in places where transaction status information
426  * is not required, the tuple rowtype is known, and shaving off a few bytes
427  * is worthwhile because we need to store many tuples.  The representation
428  * is chosen so that tuple access routines can work with either full or
429  * minimal tuples via a HeapTupleData pointer structure.  The access routines
430  * see no difference, except that they must not access the transaction status
431  * or t_ctid fields because those aren't there.
432  *
433  * For the most part, MinimalTuples should be accessed via TupleTableSlot
434  * routines.  These routines will prevent access to the "system columns"
435  * and thereby prevent accidental use of the nonexistent fields.
436  *
437  * MinimalTupleData contains a length word, some padding, and fields matching
438  * HeapTupleHeaderData beginning with t_infomask2. The padding is chosen so
439  * that offsetof(t_infomask2) is the same modulo MAXIMUM_ALIGNOF in both
440  * structs.   This makes data alignment rules equivalent in both cases.
441  *
442  * When a minimal tuple is accessed via a HeapTupleData pointer, t_data is
443  * set to point MINIMAL_TUPLE_OFFSET bytes before the actual start of the
444  * minimal tuple --- that is, where a full tuple matching the minimal tuple's
445  * data would start.  This trick is what makes the structs seem equivalent.
446  *
447  * Note that t_hoff is computed the same as in a full tuple, hence it includes
448  * the MINIMAL_TUPLE_OFFSET distance.  t_len does not include that, however.
449  *
450  * MINIMAL_TUPLE_DATA_OFFSET is the offset to the first useful (non-pad) data
451  * other than the length word.  tuplesort.c and tuplestore.c use this to avoid
452  * writing the padding to disk.
453  */
454 #define MINIMAL_TUPLE_OFFSET \
455         ((offsetof(HeapTupleHeaderData, t_infomask2) - sizeof(uint32)) / MAXIMUM_ALIGNOF * MAXIMUM_ALIGNOF)
456 #define MINIMAL_TUPLE_PADDING \
457         ((offsetof(HeapTupleHeaderData, t_infomask2) - sizeof(uint32)) % MAXIMUM_ALIGNOF)
458 #define MINIMAL_TUPLE_DATA_OFFSET \
459         offsetof(MinimalTupleData, t_infomask2)
460
461 typedef struct MinimalTupleData
462 {
463         uint32          t_len;                  /* actual length of minimal tuple */
464
465         char            mt_padding[MINIMAL_TUPLE_PADDING];
466
467         /* Fields below here must match HeapTupleHeaderData! */
468
469         uint16          t_infomask2;    /* number of attributes + various flags */
470
471         uint16          t_infomask;             /* various flag bits, see below */
472
473         uint8           t_hoff;                 /* sizeof header incl. bitmap, padding */
474
475         /* ^ - 23 bytes - ^ */
476
477         bits8           t_bits[1];              /* bitmap of NULLs -- VARIABLE LENGTH */
478
479         /* MORE DATA FOLLOWS AT END OF STRUCT */
480 } MinimalTupleData;
481
482 typedef MinimalTupleData *MinimalTuple;
483
484
485 /*
486  * HeapTupleData is an in-memory data structure that points to a tuple.
487  *
488  * There are several ways in which this data structure is used:
489  *
490  * * Pointer to a tuple in a disk buffer: t_data points directly into the
491  *       buffer (which the code had better be holding a pin on, but this is not
492  *       reflected in HeapTupleData itself).
493  *
494  * * Pointer to nothing: t_data is NULL.  This is used as a failure indication
495  *       in some functions.
496  *
497  * * Part of a palloc'd tuple: the HeapTupleData itself and the tuple
498  *       form a single palloc'd chunk.  t_data points to the memory location
499  *       immediately following the HeapTupleData struct (at offset HEAPTUPLESIZE).
500  *       This is the output format of heap_form_tuple and related routines.
501  *
502  * * Separately allocated tuple: t_data points to a palloc'd chunk that
503  *       is not adjacent to the HeapTupleData.  (This case is deprecated since
504  *       it's difficult to tell apart from case #1.  It should be used only in
505  *       limited contexts where the code knows that case #1 will never apply.)
506  *
507  * * Separately allocated minimal tuple: t_data points MINIMAL_TUPLE_OFFSET
508  *       bytes before the start of a MinimalTuple.      As with the previous case,
509  *       this can't be told apart from case #1 by inspection; code setting up
510  *       or destroying this representation has to know what it's doing.
511  *
512  * t_len should always be valid, except in the pointer-to-nothing case.
513  * t_self and t_tableOid should be valid if the HeapTupleData points to
514  * a disk buffer, or if it represents a copy of a tuple on disk.  They
515  * should be explicitly set invalid in manufactured tuples.
516  */
517 typedef struct HeapTupleData
518 {
519         uint32          t_len;                  /* length of *t_data */
520         ItemPointerData t_self;         /* SelfItemPointer */
521         Oid                     t_tableOid;             /* table the tuple came from */
522         HeapTupleHeader t_data;         /* -> tuple header and data */
523 } HeapTupleData;
524
525 typedef HeapTupleData *HeapTuple;
526
527 #define HEAPTUPLESIZE   MAXALIGN(sizeof(HeapTupleData))
528
529 /*
530  * GETSTRUCT - given a HeapTuple pointer, return address of the user data
531  */
532 #define GETSTRUCT(TUP) ((char *) ((TUP)->t_data) + (TUP)->t_data->t_hoff)
533
534 /*
535  * Accessor macros to be used with HeapTuple pointers.
536  */
537 #define HeapTupleIsValid(tuple) PointerIsValid(tuple)
538
539 #define HeapTupleHasNulls(tuple) \
540                 (((tuple)->t_data->t_infomask & HEAP_HASNULL) != 0)
541
542 #define HeapTupleNoNulls(tuple) \
543                 (!((tuple)->t_data->t_infomask & HEAP_HASNULL))
544
545 #define HeapTupleHasVarWidth(tuple) \
546                 (((tuple)->t_data->t_infomask & HEAP_HASVARWIDTH) != 0)
547
548 #define HeapTupleAllFixed(tuple) \
549                 (!((tuple)->t_data->t_infomask & HEAP_HASVARWIDTH))
550
551 #define HeapTupleHasExternal(tuple) \
552                 (((tuple)->t_data->t_infomask & HEAP_HASEXTERNAL) != 0)
553
554 #define HeapTupleIsHotUpdated(tuple) \
555                 HeapTupleHeaderIsHotUpdated((tuple)->t_data)
556
557 #define HeapTupleSetHotUpdated(tuple) \
558                 HeapTupleHeaderSetHotUpdated((tuple)->t_data)
559
560 #define HeapTupleClearHotUpdated(tuple) \
561                 HeapTupleHeaderClearHotUpdated((tuple)->t_data)
562
563 #define HeapTupleIsHeapOnly(tuple) \
564                 HeapTupleHeaderIsHeapOnly((tuple)->t_data)
565
566 #define HeapTupleSetHeapOnly(tuple) \
567                 HeapTupleHeaderSetHeapOnly((tuple)->t_data)
568
569 #define HeapTupleClearHeapOnly(tuple) \
570                 HeapTupleHeaderClearHeapOnly((tuple)->t_data)
571
572 #define HeapTupleGetOid(tuple) \
573                 HeapTupleHeaderGetOid((tuple)->t_data)
574
575 #define HeapTupleSetOid(tuple, oid) \
576                 HeapTupleHeaderSetOid((tuple)->t_data, (oid))
577
578
579 /*
580  * WAL record definitions for heapam.c's WAL operations
581  *
582  * XLOG allows to store some information in high 4 bits of log
583  * record xl_info field.  We use 3 for opcode and one for init bit.
584  */
585 #define XLOG_HEAP_INSERT                0x00
586 #define XLOG_HEAP_DELETE                0x10
587 #define XLOG_HEAP_UPDATE                0x20
588 /* 0x030 is free, was XLOG_HEAP_MOVE */
589 #define XLOG_HEAP_HOT_UPDATE    0x40
590 #define XLOG_HEAP_NEWPAGE               0x50
591 #define XLOG_HEAP_LOCK                  0x60
592 #define XLOG_HEAP_INPLACE               0x70
593
594 #define XLOG_HEAP_OPMASK                0x70
595 /*
596  * When we insert 1st item on new page in INSERT/UPDATE
597  * we can (and we do) restore entire page in redo
598  */
599 #define XLOG_HEAP_INIT_PAGE             0x80
600 /*
601  * We ran out of opcodes, so heapam.c now has a second RmgrId.  These opcodes
602  * are associated with RM_HEAP2_ID, but are not logically different from
603  * the ones above associated with RM_HEAP_ID.  We apply XLOG_HEAP_OPMASK,
604  * although currently XLOG_HEAP_INIT_PAGE is not used for any of these.
605  */
606 #define XLOG_HEAP2_FREEZE               0x00
607 #define XLOG_HEAP2_CLEAN                0x10
608 /* 0x20 is free, was XLOG_HEAP2_CLEAN_MOVE */
609 #define XLOG_HEAP2_CLEANUP_INFO 0x30
610 #define XLOG_HEAP2_VISIBLE              0x40
611
612 /*
613  * All what we need to find changed tuple
614  *
615  * NB: on most machines, sizeof(xl_heaptid) will include some trailing pad
616  * bytes for alignment.  We don't want to store the pad space in the XLOG,
617  * so use SizeOfHeapTid for space calculations.  Similar comments apply for
618  * the other xl_FOO structs.
619  */
620 typedef struct xl_heaptid
621 {
622         RelFileNode node;
623         ItemPointerData tid;            /* changed tuple id */
624 } xl_heaptid;
625
626 #define SizeOfHeapTid           (offsetof(xl_heaptid, tid) + SizeOfIptrData)
627
628 /* This is what we need to know about delete */
629 typedef struct xl_heap_delete
630 {
631         xl_heaptid      target;                 /* deleted tuple id */
632         bool            all_visible_cleared;    /* PD_ALL_VISIBLE was cleared */
633 } xl_heap_delete;
634
635 #define SizeOfHeapDelete        (offsetof(xl_heap_delete, all_visible_cleared) + sizeof(bool))
636
637 /*
638  * We don't store the whole fixed part (HeapTupleHeaderData) of an inserted
639  * or updated tuple in WAL; we can save a few bytes by reconstructing the
640  * fields that are available elsewhere in the WAL record, or perhaps just
641  * plain needn't be reconstructed.  These are the fields we must store.
642  * NOTE: t_hoff could be recomputed, but we may as well store it because
643  * it will come for free due to alignment considerations.
644  */
645 typedef struct xl_heap_header
646 {
647         uint16          t_infomask2;
648         uint16          t_infomask;
649         uint8           t_hoff;
650 } xl_heap_header;
651
652 #define SizeOfHeapHeader        (offsetof(xl_heap_header, t_hoff) + sizeof(uint8))
653
654 /* This is what we need to know about insert */
655 typedef struct xl_heap_insert
656 {
657         xl_heaptid      target;                 /* inserted tuple id */
658         bool            all_visible_cleared;    /* PD_ALL_VISIBLE was cleared */
659         /* xl_heap_header & TUPLE DATA FOLLOWS AT END OF STRUCT */
660 } xl_heap_insert;
661
662 #define SizeOfHeapInsert        (offsetof(xl_heap_insert, all_visible_cleared) + sizeof(bool))
663
664 /* This is what we need to know about update|hot_update */
665 typedef struct xl_heap_update
666 {
667         xl_heaptid      target;                 /* deleted tuple id */
668         ItemPointerData newtid;         /* new inserted tuple id */
669         bool            all_visible_cleared;    /* PD_ALL_VISIBLE was cleared */
670         bool            new_all_visible_cleared;                /* same for the page of newtid */
671         /* NEW TUPLE xl_heap_header AND TUPLE DATA FOLLOWS AT END OF STRUCT */
672 } xl_heap_update;
673
674 #define SizeOfHeapUpdate        (offsetof(xl_heap_update, new_all_visible_cleared) + sizeof(bool))
675
676 /*
677  * This is what we need to know about vacuum page cleanup/redirect
678  *
679  * The array of OffsetNumbers following the fixed part of the record contains:
680  *      * for each redirected item: the item offset, then the offset redirected to
681  *      * for each now-dead item: the item offset
682  *      * for each now-unused item: the item offset
683  * The total number of OffsetNumbers is therefore 2*nredirected+ndead+nunused.
684  * Note that nunused is not explicitly stored, but may be found by reference
685  * to the total record length.
686  */
687 typedef struct xl_heap_clean
688 {
689         RelFileNode node;
690         BlockNumber block;
691         TransactionId latestRemovedXid;
692         uint16          nredirected;
693         uint16          ndead;
694         /* OFFSET NUMBERS FOLLOW */
695 } xl_heap_clean;
696
697 #define SizeOfHeapClean (offsetof(xl_heap_clean, ndead) + sizeof(uint16))
698
699 /*
700  * Cleanup_info is required in some cases during a lazy VACUUM.
701  * Used for reporting the results of HeapTupleHeaderAdvanceLatestRemovedXid()
702  * see vacuumlazy.c for full explanation
703  */
704 typedef struct xl_heap_cleanup_info
705 {
706         RelFileNode node;
707         TransactionId latestRemovedXid;
708 } xl_heap_cleanup_info;
709
710 #define SizeOfHeapCleanupInfo (sizeof(xl_heap_cleanup_info))
711
712 /* This is for replacing a page's contents in toto */
713 /* NB: this is used for indexes as well as heaps */
714 typedef struct xl_heap_newpage
715 {
716         RelFileNode node;
717         ForkNumber      forknum;
718         BlockNumber blkno;                      /* location of new page */
719         /* entire page contents follow at end of record */
720 } xl_heap_newpage;
721
722 #define SizeOfHeapNewpage       (offsetof(xl_heap_newpage, blkno) + sizeof(BlockNumber))
723
724 /* This is what we need to know about lock */
725 typedef struct xl_heap_lock
726 {
727         xl_heaptid      target;                 /* locked tuple id */
728         TransactionId locking_xid;      /* might be a MultiXactId not xid */
729         bool            xid_is_mxact;   /* is it? */
730         bool            shared_lock;    /* shared or exclusive row lock? */
731 } xl_heap_lock;
732
733 #define SizeOfHeapLock  (offsetof(xl_heap_lock, shared_lock) + sizeof(bool))
734
735 /* This is what we need to know about in-place update */
736 typedef struct xl_heap_inplace
737 {
738         xl_heaptid      target;                 /* updated tuple id */
739         /* TUPLE DATA FOLLOWS AT END OF STRUCT */
740 } xl_heap_inplace;
741
742 #define SizeOfHeapInplace       (offsetof(xl_heap_inplace, target) + SizeOfHeapTid)
743
744 /* This is what we need to know about tuple freezing during vacuum */
745 typedef struct xl_heap_freeze
746 {
747         RelFileNode node;
748         BlockNumber block;
749         TransactionId cutoff_xid;
750         /* TUPLE OFFSET NUMBERS FOLLOW AT THE END */
751 } xl_heap_freeze;
752
753 #define SizeOfHeapFreeze (offsetof(xl_heap_freeze, cutoff_xid) + sizeof(TransactionId))
754
755 /* This is what we need to know about setting a visibility map bit */
756 typedef struct xl_heap_visible
757 {
758         RelFileNode node;
759         BlockNumber block;
760 } xl_heap_visible;
761
762 #define SizeOfHeapVisible (offsetof(xl_heap_visible, block) + sizeof(BlockNumber))
763
764 extern void HeapTupleHeaderAdvanceLatestRemovedXid(HeapTupleHeader tuple,
765                                                                            TransactionId *latestRemovedXid);
766
767 /* HeapTupleHeader functions implemented in utils/time/combocid.c */
768 extern CommandId HeapTupleHeaderGetCmin(HeapTupleHeader tup);
769 extern CommandId HeapTupleHeaderGetCmax(HeapTupleHeader tup);
770 extern void HeapTupleHeaderAdjustCmax(HeapTupleHeader tup,
771                                                   CommandId *cmax,
772                                                   bool *iscombo);
773
774 /* ----------------
775  *              fastgetattr
776  *
777  *              Fetch a user attribute's value as a Datum (might be either a
778  *              value, or a pointer into the data area of the tuple).
779  *
780  *              This must not be used when a system attribute might be requested.
781  *              Furthermore, the passed attnum MUST be valid.  Use heap_getattr()
782  *              instead, if in doubt.
783  *
784  *              This gets called many times, so we macro the cacheable and NULL
785  *              lookups, and call nocachegetattr() for the rest.
786  * ----------------
787  */
788
789 #if !defined(DISABLE_COMPLEX_MACRO)
790
791 #define fastgetattr(tup, attnum, tupleDesc, isnull)                                     \
792 (                                                                                                                                       \
793         AssertMacro((attnum) > 0),                                                                              \
794         (*(isnull) = false),                                                                                    \
795         HeapTupleNoNulls(tup) ?                                                                                 \
796         (                                                                                                                               \
797                 (tupleDesc)->attrs[(attnum)-1]->attcacheoff >= 0 ?                      \
798                 (                                                                                                                       \
799                         fetchatt((tupleDesc)->attrs[(attnum)-1],                                \
800                                 (char *) (tup)->t_data + (tup)->t_data->t_hoff +        \
801                                         (tupleDesc)->attrs[(attnum)-1]->attcacheoff)    \
802                 )                                                                                                                       \
803                 :                                                                                                                       \
804                         nocachegetattr((tup), (attnum), (tupleDesc))                    \
805         )                                                                                                                               \
806         :                                                                                                                               \
807         (                                                                                                                               \
808                 att_isnull((attnum)-1, (tup)->t_data->t_bits) ?                         \
809                 (                                                                                                                       \
810                         (*(isnull) = true),                                                                             \
811                         (Datum)NULL                                                                                             \
812                 )                                                                                                                       \
813                 :                                                                                                                       \
814                 (                                                                                                                       \
815                         nocachegetattr((tup), (attnum), (tupleDesc))                    \
816                 )                                                                                                                       \
817         )                                                                                                                               \
818 )
819 #else                                                   /* defined(DISABLE_COMPLEX_MACRO) */
820
821 extern Datum fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc,
822                         bool *isnull);
823 #endif   /* defined(DISABLE_COMPLEX_MACRO) */
824
825
826 /* ----------------
827  *              heap_getattr
828  *
829  *              Extract an attribute of a heap tuple and return it as a Datum.
830  *              This works for either system or user attributes.  The given attnum
831  *              is properly range-checked.
832  *
833  *              If the field in question has a NULL value, we return a zero Datum
834  *              and set *isnull == true.  Otherwise, we set *isnull == false.
835  *
836  *              <tup> is the pointer to the heap tuple.  <attnum> is the attribute
837  *              number of the column (field) caller wants.      <tupleDesc> is a
838  *              pointer to the structure describing the row and all its fields.
839  * ----------------
840  */
841 #define heap_getattr(tup, attnum, tupleDesc, isnull) \
842 ( \
843         AssertMacro((tup) != NULL), \
844         ( \
845                 ((attnum) > 0) ? \
846                 ( \
847                         ((attnum) > (int) HeapTupleHeaderGetNatts((tup)->t_data)) ? \
848                         ( \
849                                 (*(isnull) = true), \
850                                 (Datum)NULL \
851                         ) \
852                         : \
853                                 fastgetattr((tup), (attnum), (tupleDesc), (isnull)) \
854                 ) \
855                 : \
856                         heap_getsysattr((tup), (attnum), (tupleDesc), (isnull)) \
857         ) \
858 )
859
860 /* prototypes for functions in common/heaptuple.c */
861 extern Size heap_compute_data_size(TupleDesc tupleDesc,
862                                            Datum *values, bool *isnull);
863 extern void heap_fill_tuple(TupleDesc tupleDesc,
864                                 Datum *values, bool *isnull,
865                                 char *data, Size data_size,
866                                 uint16 *infomask, bits8 *bit);
867 extern bool heap_attisnull(HeapTuple tup, int attnum);
868 extern Datum nocachegetattr(HeapTuple tup, int attnum,
869                            TupleDesc att);
870 extern Datum heap_getsysattr(HeapTuple tup, int attnum, TupleDesc tupleDesc,
871                                 bool *isnull);
872 extern HeapTuple heap_copytuple(HeapTuple tuple);
873 extern void heap_copytuple_with_tuple(HeapTuple src, HeapTuple dest);
874 extern HeapTuple heap_form_tuple(TupleDesc tupleDescriptor,
875                                 Datum *values, bool *isnull);
876 extern HeapTuple heap_modify_tuple(HeapTuple tuple,
877                                   TupleDesc tupleDesc,
878                                   Datum *replValues,
879                                   bool *replIsnull,
880                                   bool *doReplace);
881 extern void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc,
882                                   Datum *values, bool *isnull);
883
884 /* these three are deprecated versions of the three above: */
885 extern HeapTuple heap_formtuple(TupleDesc tupleDescriptor,
886                            Datum *values, char *nulls);
887 extern HeapTuple heap_modifytuple(HeapTuple tuple,
888                                  TupleDesc tupleDesc,
889                                  Datum *replValues,
890                                  char *replNulls,
891                                  char *replActions);
892 extern void heap_deformtuple(HeapTuple tuple, TupleDesc tupleDesc,
893                                  Datum *values, char *nulls);
894 extern void heap_freetuple(HeapTuple htup);
895 extern MinimalTuple heap_form_minimal_tuple(TupleDesc tupleDescriptor,
896                                                 Datum *values, bool *isnull);
897 extern void heap_free_minimal_tuple(MinimalTuple mtup);
898 extern MinimalTuple heap_copy_minimal_tuple(MinimalTuple mtup);
899 extern HeapTuple heap_tuple_from_minimal_tuple(MinimalTuple mtup);
900 extern MinimalTuple minimal_tuple_from_heap_tuple(HeapTuple htup);
901
902 #endif   /* HTUP_H */