1 /*-------------------------------------------------------------------------
4 * POSTGRES heap tuple definitions.
7 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/include/access/htup.h,v 1.80 2005/11/22 18:17:29 momjian Exp $
12 *-------------------------------------------------------------------------
17 #include "storage/bufpage.h"
18 #include "storage/relfilenode.h"
19 #include "access/transam.h"
23 * MaxTupleAttributeNumber limits the number of (user) columns in a tuple.
24 * The key limit on this value is that the size of the fixed overhead for
25 * a tuple, plus the size of the null-values bitmap (at 1 bit per column),
26 * plus MAXALIGN alignment, must fit into t_hoff which is uint8. On most
27 * machines the upper limit without making t_hoff wider would be a little
28 * over 1700. We use round numbers here and for MaxHeapAttributeNumber
29 * so that alterations in HeapTupleHeaderData layout won't change the
30 * supported max number of columns.
32 #define MaxTupleAttributeNumber 1664 /* 8 * 208 */
35 * MaxHeapAttributeNumber limits the number of (user) columns in a table.
36 * This should be somewhat less than MaxTupleAttributeNumber. It must be
37 * at least one less, else we will fail to do UPDATEs on a maximal-width
38 * table (because UPDATE has to form working tuples that include CTID).
39 * In practice we want some additional daylight so that we can gracefully
40 * support operations that add hidden "resjunk" columns, for example
41 * SELECT * FROM wide_table ORDER BY foo, bar, baz.
42 * In any case, depending on column data types you will likely be running
43 * into the disk-block-based limit on overall tuple size if you have more
44 * than a thousand or so columns. TOAST won't help.
47 #define MaxHeapAttributeNumber 1600 /* 8 * 200 */
50 * Heap tuple header. To avoid wasting space, the fields should be
51 * layed out in such a way to avoid structure padding.
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.
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)
71 * We store five "virtual" fields Xmin, Cmin, Xmax, Cmax, and Xvac in four
72 * physical fields. Xmin, Cmin and Xmax are always really stored, but
73 * Cmax and Xvac share a field. This works because we know that there are
74 * only a limited number of states that a tuple can be in, and that Cmax
75 * is only interesting for the lifetime of the deleting transaction.
76 * This assumes that VACUUM FULL never tries to move a tuple whose Cmax
77 * is still interesting (ie, delete-in-progress).
79 * Note that in 7.3 and 7.4 a similar idea was applied to Xmax and Cmin.
80 * However, with the advent of subtransactions, a tuple may need both Xmax
81 * and Cmin simultaneously, so this is no longer possible.
83 * A word about t_ctid: whenever a new tuple is stored on disk, its t_ctid
84 * is initialized with its own TID (location). If the tuple is ever updated,
85 * its t_ctid is changed to point to the replacement version of the tuple.
86 * Thus, a tuple is the latest version of its row iff XMAX is invalid or
87 * t_ctid points to itself (in which case, if XMAX is valid, the tuple is
88 * either locked or deleted). One can follow the chain of t_ctid links
89 * to find the newest version of the row. Beware however that VACUUM might
90 * erase the pointed-to (newer) tuple before erasing the pointing (older)
91 * tuple. Hence, when following a t_ctid link, it is necessary to check
92 * to see if the referenced slot is empty or contains an unrelated tuple.
93 * Check that the referenced tuple has XMIN equal to the referencing tuple's
94 * XMAX to verify that it is actually the descendant version and not an
95 * unrelated tuple stored into a slot recently freed by VACUUM. If either
96 * check fails, one may assume that there is no live descendant version.
98 * Following the fixed header fields, the nulls bitmap is stored (beginning
99 * at t_bits). The bitmap is *not* stored if t_infomask shows that there
100 * are no nulls in the tuple. If an OID field is present (as indicated by
101 * t_infomask), then it is stored just before the user data, which begins at
102 * the offset shown by t_hoff. Note that t_hoff must be a multiple of
107 typedef struct HeapTupleFields
109 TransactionId t_xmin; /* inserting xact ID */
110 CommandId t_cmin; /* inserting command ID */
111 TransactionId t_xmax; /* deleting or locking xact ID */
115 CommandId t_cmax; /* deleting or locking command ID */
116 TransactionId t_xvac; /* VACUUM FULL xact ID */
120 typedef struct DatumTupleFields
122 int32 datum_len; /* required to be a varlena type */
124 int32 datum_typmod; /* -1, or identifier of a record type */
126 Oid datum_typeid; /* composite type OID, or RECORDOID */
129 * Note: field ordering is chosen with thought that Oid might someday
134 typedef struct HeapTupleHeaderData
138 HeapTupleFields t_heap;
139 DatumTupleFields t_datum;
142 ItemPointerData t_ctid; /* current TID of this or newer tuple */
144 int16 t_natts; /* number of attributes */
146 uint16 t_infomask; /* various flag bits, see below */
148 uint8 t_hoff; /* sizeof header incl. bitmap, padding */
150 /* ^ - 27 bytes - ^ */
152 bits8 t_bits[1]; /* bitmap of NULLs -- VARIABLE LENGTH */
154 /* MORE DATA FOLLOWS AT END OF STRUCT */
155 } HeapTupleHeaderData;
157 typedef HeapTupleHeaderData *HeapTupleHeader;
160 * information stored in t_infomask:
162 #define HEAP_HASNULL 0x0001 /* has null attribute(s) */
163 #define HEAP_HASVARWIDTH 0x0002 /* has variable-width attribute(s) */
164 #define HEAP_HASEXTERNAL 0x0004 /* has external stored attribute(s) */
165 #define HEAP_HASCOMPRESSED 0x0008 /* has compressed stored attribute(s) */
166 #define HEAP_HASEXTENDED 0x000C /* the two above combined */
167 #define HEAP_HASOID 0x0010 /* has an object-id field */
168 /* 0x0020 is presently unused */
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 VACUUM
181 #define HEAP_MOVED_IN 0x8000 /* moved from another place by VACUUM
183 #define HEAP_MOVED (HEAP_MOVED_OFF | HEAP_MOVED_IN)
185 #define HEAP_XACT_MASK 0xFFC0 /* visibility-related bits */
189 * HeapTupleHeader accessor macros
191 * Note: beware of multiple evaluations of "tup" argument. But the Set
192 * macros evaluate their other argument only once.
195 #define HeapTupleHeaderGetXmin(tup) \
197 (tup)->t_choice.t_heap.t_xmin \
200 #define HeapTupleHeaderSetXmin(tup, xid) \
202 TransactionIdStore((xid), &(tup)->t_choice.t_heap.t_xmin) \
205 #define HeapTupleHeaderGetXmax(tup) \
207 (tup)->t_choice.t_heap.t_xmax \
210 #define HeapTupleHeaderSetXmax(tup, xid) \
212 TransactionIdStore((xid), &(tup)->t_choice.t_heap.t_xmax) \
215 #define HeapTupleHeaderGetCmin(tup) \
217 (tup)->t_choice.t_heap.t_cmin \
220 #define HeapTupleHeaderSetCmin(tup, cid) \
222 (tup)->t_choice.t_heap.t_cmin = (cid) \
226 * Note: GetCmax will produce wrong answers after SetXvac has been executed
227 * by a transaction other than the inserting one. We could check
228 * HEAP_XMAX_INVALID and return FirstCommandId if it's clear, but since that
229 * bit will be set again if the deleting transaction aborts, there'd be no
230 * real gain in safety from the extra test. So, just rely on the caller not
231 * to trust the value unless it's meaningful.
233 #define HeapTupleHeaderGetCmax(tup) \
235 (tup)->t_choice.t_heap.t_field4.t_cmax \
238 #define HeapTupleHeaderSetCmax(tup, cid) \
240 Assert(!((tup)->t_infomask & HEAP_MOVED)); \
241 (tup)->t_choice.t_heap.t_field4.t_cmax = (cid); \
244 #define HeapTupleHeaderGetXvac(tup) \
246 ((tup)->t_infomask & HEAP_MOVED) ? \
247 (tup)->t_choice.t_heap.t_field4.t_xvac \
249 InvalidTransactionId \
252 #define HeapTupleHeaderSetXvac(tup, xid) \
254 Assert((tup)->t_infomask & HEAP_MOVED); \
255 TransactionIdStore((xid), &(tup)->t_choice.t_heap.t_field4.t_xvac); \
258 #define HeapTupleHeaderGetDatumLength(tup) \
260 (tup)->t_choice.t_datum.datum_len \
263 #define HeapTupleHeaderSetDatumLength(tup, len) \
265 (tup)->t_choice.t_datum.datum_len = (len) \
268 #define HeapTupleHeaderGetTypeId(tup) \
270 (tup)->t_choice.t_datum.datum_typeid \
273 #define HeapTupleHeaderSetTypeId(tup, typeid) \
275 (tup)->t_choice.t_datum.datum_typeid = (typeid) \
278 #define HeapTupleHeaderGetTypMod(tup) \
280 (tup)->t_choice.t_datum.datum_typmod \
283 #define HeapTupleHeaderSetTypMod(tup, typmod) \
285 (tup)->t_choice.t_datum.datum_typmod = (typmod) \
288 #define HeapTupleHeaderGetOid(tup) \
290 ((tup)->t_infomask & HEAP_HASOID) ? \
291 *((Oid *) ((char *)(tup) + (tup)->t_hoff - sizeof(Oid))) \
296 #define HeapTupleHeaderSetOid(tup, oid) \
298 Assert((tup)->t_infomask & HEAP_HASOID); \
299 *((Oid *) ((char *)(tup) + (tup)->t_hoff - sizeof(Oid))) = (oid); \
305 * Computes size of null bitmap given number of data columns.
307 #define BITMAPLEN(NATTS) (((int)(NATTS) + 7) / 8)
310 * MaxTupleSize is the maximum allowed size of a tuple, including header and
311 * MAXALIGN alignment padding. Basically it's BLCKSZ minus the other stuff
312 * that has to be on a disk page. The "other stuff" includes access-method-
313 * dependent "special space", which we assume will be no more than
314 * MaxSpecialSpace bytes (currently, on heap pages it's actually zero).
316 * NOTE: we do not need to count an ItemId for the tuple because
317 * sizeof(PageHeaderData) includes the first ItemId on the page.
319 #define MaxSpecialSpace 32
321 #define MaxTupleSize \
322 (BLCKSZ - MAXALIGN(sizeof(PageHeaderData) + MaxSpecialSpace))
325 * MaxHeapTuplesPerPage is an upper bound on the number of tuples that can
326 * fit on one heap page. (Note that indexes could have more, because they
327 * use a smaller tuple header.) We arrive at the divisor because each tuple
328 * must be maxaligned, and it must have an associated item pointer.
330 #define MaxHeapTuplesPerPage \
331 ((int) ((BLCKSZ - offsetof(PageHeaderData, pd_linp)) / \
332 (MAXALIGN(offsetof(HeapTupleHeaderData, t_bits)) + sizeof(ItemIdData))))
335 * MaxAttrSize is a somewhat arbitrary upper limit on the declared size of
336 * data fields of char(n) and similar types. It need not have anything
337 * directly to do with the *actual* upper limit of varlena values, which
338 * is currently 1Gb (see struct varattrib in postgres.h). I've set it
339 * at 10Mb which seems like a reasonable number --- tgl 8/6/00.
341 #define MaxAttrSize (10 * 1024 * 1024)
345 * Attribute numbers for the system-defined attributes
347 #define SelfItemPointerAttributeNumber (-1)
348 #define ObjectIdAttributeNumber (-2)
349 #define MinTransactionIdAttributeNumber (-3)
350 #define MinCommandIdAttributeNumber (-4)
351 #define MaxTransactionIdAttributeNumber (-5)
352 #define MaxCommandIdAttributeNumber (-6)
353 #define TableOidAttributeNumber (-7)
354 #define FirstLowInvalidHeapAttributeNumber (-8)
358 * HeapTupleData is an in-memory data structure that points to a tuple.
360 * There are several ways in which this data structure is used:
362 * * Pointer to a tuple in a disk buffer: t_data points directly into the
363 * buffer (which the code had better be holding a pin on, but this is not
364 * reflected in HeapTupleData itself).
366 * * Pointer to nothing: t_data is NULL. This is used as a failure indication
369 * * Part of a palloc'd tuple: the HeapTupleData itself and the tuple
370 * form a single palloc'd chunk. t_data points to the memory location
371 * immediately following the HeapTupleData struct (at offset HEAPTUPLESIZE).
372 * This is the output format of heap_form_tuple and related routines.
374 * * Separately allocated tuple: t_data points to a palloc'd chunk that
375 * is not adjacent to the HeapTupleData. (This case is deprecated since
376 * it's difficult to tell apart from case #1. It should be used only in
377 * limited contexts where the code knows that case #1 will never apply.)
379 * t_len should always be valid, except in the pointer-to-nothing case.
380 * t_self and t_tableOid should be valid if the HeapTupleData points to
381 * a disk buffer, or if it represents a copy of a tuple on disk. They
382 * should be explicitly set invalid in manufactured tuples.
384 typedef struct HeapTupleData
386 uint32 t_len; /* length of *t_data */
387 ItemPointerData t_self; /* SelfItemPointer */
388 Oid t_tableOid; /* table the tuple came from */
389 HeapTupleHeader t_data; /* -> tuple header and data */
392 typedef HeapTupleData *HeapTuple;
394 #define HEAPTUPLESIZE MAXALIGN(sizeof(HeapTupleData))
397 * GETSTRUCT - given a HeapTuple pointer, return address of the user data
399 #define GETSTRUCT(TUP) ((char *) ((TUP)->t_data) + (TUP)->t_data->t_hoff)
402 * Accessor macros to be used with HeapTuple pointers.
404 #define HeapTupleIsValid(tuple) PointerIsValid(tuple)
406 #define HeapTupleHasNulls(tuple) \
407 (((tuple)->t_data->t_infomask & HEAP_HASNULL) != 0)
409 #define HeapTupleNoNulls(tuple) \
410 (!((tuple)->t_data->t_infomask & HEAP_HASNULL))
412 #define HeapTupleHasVarWidth(tuple) \
413 (((tuple)->t_data->t_infomask & HEAP_HASVARWIDTH) != 0)
415 #define HeapTupleAllFixed(tuple) \
416 (!((tuple)->t_data->t_infomask & HEAP_HASVARWIDTH))
418 #define HeapTupleHasExternal(tuple) \
419 (((tuple)->t_data->t_infomask & HEAP_HASEXTERNAL) != 0)
421 #define HeapTupleHasCompressed(tuple) \
422 (((tuple)->t_data->t_infomask & HEAP_HASCOMPRESSED) != 0)
424 #define HeapTupleHasExtended(tuple) \
425 (((tuple)->t_data->t_infomask & HEAP_HASEXTENDED) != 0)
427 #define HeapTupleGetOid(tuple) \
428 HeapTupleHeaderGetOid((tuple)->t_data)
430 #define HeapTupleSetOid(tuple, oid) \
431 HeapTupleHeaderSetOid((tuple)->t_data, (oid))
435 * WAL record definitions for heapam.c's WAL operations
437 * XLOG allows to store some information in high 4 bits of log
438 * record xl_info field. We use 3 for opcode and one for init bit.
440 #define XLOG_HEAP_INSERT 0x00
441 #define XLOG_HEAP_DELETE 0x10
442 #define XLOG_HEAP_UPDATE 0x20
443 #define XLOG_HEAP_MOVE 0x30
444 #define XLOG_HEAP_CLEAN 0x40
445 #define XLOG_HEAP_NEWPAGE 0x50
446 #define XLOG_HEAP_LOCK 0x60
447 /* opcode 0x70 still free */
448 #define XLOG_HEAP_OPMASK 0x70
450 * When we insert 1st item on new page in INSERT/UPDATE
451 * we can (and we do) restore entire page in redo
453 #define XLOG_HEAP_INIT_PAGE 0x80
456 * All what we need to find changed tuple
458 * NB: on most machines, sizeof(xl_heaptid) will include some trailing pad
459 * bytes for alignment. We don't want to store the pad space in the XLOG,
460 * so use SizeOfHeapTid for space calculations. Similar comments apply for
461 * the other xl_FOO structs.
463 typedef struct xl_heaptid
466 ItemPointerData tid; /* changed tuple id */
469 #define SizeOfHeapTid (offsetof(xl_heaptid, tid) + SizeOfIptrData)
471 /* This is what we need to know about delete */
472 typedef struct xl_heap_delete
474 xl_heaptid target; /* deleted tuple id */
477 #define SizeOfHeapDelete (offsetof(xl_heap_delete, target) + SizeOfHeapTid)
480 * We don't store the whole fixed part (HeapTupleHeaderData) of an inserted
481 * or updated tuple in WAL; we can save a few bytes by reconstructing the
482 * fields that are available elsewhere in the WAL record, or perhaps just
483 * plain needn't be reconstructed. These are the fields we must store.
484 * NOTE: t_hoff could be recomputed, but we may as well store it because
485 * it will come for free due to alignment considerations.
487 typedef struct xl_heap_header
494 #define SizeOfHeapHeader (offsetof(xl_heap_header, t_hoff) + sizeof(uint8))
496 /* This is what we need to know about insert */
497 typedef struct xl_heap_insert
499 xl_heaptid target; /* inserted tuple id */
500 /* xl_heap_header & TUPLE DATA FOLLOWS AT END OF STRUCT */
503 #define SizeOfHeapInsert (offsetof(xl_heap_insert, target) + SizeOfHeapTid)
505 /* This is what we need to know about update|move */
506 typedef struct xl_heap_update
508 xl_heaptid target; /* deleted tuple id */
509 ItemPointerData newtid; /* new inserted tuple id */
510 /* NEW TUPLE xl_heap_header (PLUS xmax & xmin IF MOVE OP) */
511 /* and TUPLE DATA FOLLOWS AT END OF STRUCT */
514 #define SizeOfHeapUpdate (offsetof(xl_heap_update, newtid) + SizeOfIptrData)
516 /* This is what we need to know about vacuum page cleanup */
517 typedef struct xl_heap_clean
521 /* UNUSED OFFSET NUMBERS FOLLOW AT THE END */
524 #define SizeOfHeapClean (offsetof(xl_heap_clean, block) + sizeof(BlockNumber))
526 /* This is for replacing a page's contents in toto */
527 /* NB: this is used for indexes as well as heaps */
528 typedef struct xl_heap_newpage
531 BlockNumber blkno; /* location of new page */
532 /* entire page contents follow at end of record */
535 #define SizeOfHeapNewpage (offsetof(xl_heap_newpage, blkno) + sizeof(BlockNumber))
537 /* This is what we need to know about lock */
538 typedef struct xl_heap_lock
540 xl_heaptid target; /* locked tuple id */
541 TransactionId locking_xid; /* might be a MultiXactId not xid */
542 bool xid_is_mxact; /* is it? */
543 bool shared_lock; /* shared or exclusive row lock? */
546 #define SizeOfHeapLock (offsetof(xl_heap_lock, shared_lock) + sizeof(bool))