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.72 2004/12/31 22:03:21 pgsql 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 * Following the fixed header fields, the nulls bitmap is stored (beginning
84 * at t_bits). The bitmap is *not* stored if t_infomask shows that there
85 * are no nulls in the tuple. If an OID field is present (as indicated by
86 * t_infomask), then it is stored just before the user data, which begins at
87 * the offset shown by t_hoff. Note that t_hoff must be a multiple of
92 typedef struct HeapTupleFields
94 TransactionId t_xmin; /* inserting xact ID */
95 CommandId t_cmin; /* inserting command ID */
96 TransactionId t_xmax; /* deleting xact ID */
100 CommandId t_cmax; /* deleting command ID */
101 TransactionId t_xvac; /* VACUUM FULL xact ID */
105 typedef struct DatumTupleFields
107 int32 datum_len; /* required to be a varlena type */
109 int32 datum_typmod; /* -1, or identifier of a record type */
111 Oid datum_typeid; /* composite type OID, or RECORDOID */
114 * Note: field ordering is chosen with thought that Oid might someday
119 typedef struct HeapTupleHeaderData
123 HeapTupleFields t_heap;
124 DatumTupleFields t_datum;
127 ItemPointerData t_ctid; /* current TID of this or newer tuple */
129 int16 t_natts; /* number of attributes */
131 uint16 t_infomask; /* various flag bits, see below */
133 uint8 t_hoff; /* sizeof header incl. bitmap, padding */
135 /* ^ - 27 bytes - ^ */
137 bits8 t_bits[1]; /* bitmap of NULLs -- VARIABLE LENGTH */
139 /* MORE DATA FOLLOWS AT END OF STRUCT */
140 } HeapTupleHeaderData;
142 typedef HeapTupleHeaderData *HeapTupleHeader;
145 * information stored in t_infomask:
147 #define HEAP_HASNULL 0x0001 /* has null attribute(s) */
148 #define HEAP_HASVARWIDTH 0x0002 /* has variable-width attribute(s) */
149 #define HEAP_HASEXTERNAL 0x0004 /* has external stored
151 #define HEAP_HASCOMPRESSED 0x0008 /* has compressed stored
153 #define HEAP_HASEXTENDED 0x000C /* the two above combined */
154 #define HEAP_HASOID 0x0010 /* has an object-id field */
155 /* 0x0020 and 0x0040 are unused */
156 #define HEAP_XMAX_UNLOGGED 0x0080 /* to lock tuple for update
158 #define HEAP_XMIN_COMMITTED 0x0100 /* t_xmin committed */
159 #define HEAP_XMIN_INVALID 0x0200 /* t_xmin invalid/aborted */
160 #define HEAP_XMAX_COMMITTED 0x0400 /* t_xmax committed */
161 #define HEAP_XMAX_INVALID 0x0800 /* t_xmax invalid/aborted */
162 #define HEAP_MARKED_FOR_UPDATE 0x1000 /* marked for UPDATE */
163 #define HEAP_UPDATED 0x2000 /* this is UPDATEd version of row */
164 #define HEAP_MOVED_OFF 0x4000 /* moved to another place by
166 #define HEAP_MOVED_IN 0x8000 /* moved from another place by
168 #define HEAP_MOVED (HEAP_MOVED_OFF | HEAP_MOVED_IN)
170 #define HEAP_XACT_MASK 0xFFC0 /* visibility-related bits */
174 * HeapTupleHeader accessor macros
176 * Note: beware of multiple evaluations of "tup" argument. But the Set
177 * macros evaluate their other argument only once.
180 #define HeapTupleHeaderGetXmin(tup) \
182 (tup)->t_choice.t_heap.t_xmin \
185 #define HeapTupleHeaderSetXmin(tup, xid) \
187 TransactionIdStore((xid), &(tup)->t_choice.t_heap.t_xmin) \
190 #define HeapTupleHeaderGetXmax(tup) \
192 (tup)->t_choice.t_heap.t_xmax \
195 #define HeapTupleHeaderSetXmax(tup, xid) \
197 TransactionIdStore((xid), &(tup)->t_choice.t_heap.t_xmax) \
200 #define HeapTupleHeaderGetCmin(tup) \
202 (tup)->t_choice.t_heap.t_cmin \
205 #define HeapTupleHeaderSetCmin(tup, cid) \
207 (tup)->t_choice.t_heap.t_cmin = (cid) \
211 * Note: GetCmax will produce wrong answers after SetXvac has been executed
212 * by a transaction other than the inserting one. We could check
213 * HEAP_XMAX_INVALID and return FirstCommandId if it's clear, but since that
214 * bit will be set again if the deleting transaction aborts, there'd be no
215 * real gain in safety from the extra test. So, just rely on the caller not
216 * to trust the value unless it's meaningful.
218 #define HeapTupleHeaderGetCmax(tup) \
220 (tup)->t_choice.t_heap.t_field4.t_cmax \
223 #define HeapTupleHeaderSetCmax(tup, cid) \
225 Assert(!((tup)->t_infomask & HEAP_MOVED)); \
226 (tup)->t_choice.t_heap.t_field4.t_cmax = (cid); \
229 #define HeapTupleHeaderGetXvac(tup) \
231 ((tup)->t_infomask & HEAP_MOVED) ? \
232 (tup)->t_choice.t_heap.t_field4.t_xvac \
234 InvalidTransactionId \
237 #define HeapTupleHeaderSetXvac(tup, xid) \
239 Assert((tup)->t_infomask & HEAP_MOVED); \
240 TransactionIdStore((xid), &(tup)->t_choice.t_heap.t_field4.t_xvac); \
243 #define HeapTupleHeaderGetDatumLength(tup) \
245 (tup)->t_choice.t_datum.datum_len \
248 #define HeapTupleHeaderSetDatumLength(tup, len) \
250 (tup)->t_choice.t_datum.datum_len = (len) \
253 #define HeapTupleHeaderGetTypeId(tup) \
255 (tup)->t_choice.t_datum.datum_typeid \
258 #define HeapTupleHeaderSetTypeId(tup, typeid) \
260 (tup)->t_choice.t_datum.datum_typeid = (typeid) \
263 #define HeapTupleHeaderGetTypMod(tup) \
265 (tup)->t_choice.t_datum.datum_typmod \
268 #define HeapTupleHeaderSetTypMod(tup, typmod) \
270 (tup)->t_choice.t_datum.datum_typmod = (typmod) \
273 #define HeapTupleHeaderGetOid(tup) \
275 ((tup)->t_infomask & HEAP_HASOID) ? \
276 *((Oid *) ((char *)(tup) + (tup)->t_hoff - sizeof(Oid))) \
281 #define HeapTupleHeaderSetOid(tup, oid) \
283 Assert((tup)->t_infomask & HEAP_HASOID); \
284 *((Oid *) ((char *)(tup) + (tup)->t_hoff - sizeof(Oid))) = (oid); \
290 * Computes size of null bitmap given number of data columns.
292 #define BITMAPLEN(NATTS) (((int)(NATTS) + 7) / 8)
295 * MaxTupleSize is the maximum allowed size of a tuple, including header and
296 * MAXALIGN alignment padding. Basically it's BLCKSZ minus the other stuff
297 * that has to be on a disk page. The "other stuff" includes access-method-
298 * dependent "special space", which we assume will be no more than
299 * MaxSpecialSpace bytes (currently, on heap pages it's actually zero).
301 * NOTE: we do not need to count an ItemId for the tuple because
302 * sizeof(PageHeaderData) includes the first ItemId on the page.
304 #define MaxSpecialSpace 32
306 #define MaxTupleSize \
307 (BLCKSZ - MAXALIGN(sizeof(PageHeaderData) + MaxSpecialSpace))
310 * MaxAttrSize is a somewhat arbitrary upper limit on the declared size of
311 * data fields of char(n) and similar types. It need not have anything
312 * directly to do with the *actual* upper limit of varlena values, which
313 * is currently 1Gb (see struct varattrib in postgres.h). I've set it
314 * at 10Mb which seems like a reasonable number --- tgl 8/6/00.
316 #define MaxAttrSize (10 * 1024 * 1024)
320 * Attribute numbers for the system-defined attributes
322 #define SelfItemPointerAttributeNumber (-1)
323 #define ObjectIdAttributeNumber (-2)
324 #define MinTransactionIdAttributeNumber (-3)
325 #define MinCommandIdAttributeNumber (-4)
326 #define MaxTransactionIdAttributeNumber (-5)
327 #define MaxCommandIdAttributeNumber (-6)
328 #define TableOidAttributeNumber (-7)
329 #define FirstLowInvalidHeapAttributeNumber (-8)
333 * HeapTupleData is an in-memory data structure that points to a tuple.
335 * This new HeapTuple for version >= 6.5 and this is why it was changed:
337 * 1. t_len moved off on-disk tuple data - ItemIdData is used to get len;
338 * 2. t_ctid above is not self tuple TID now - it may point to
339 * updated version of tuple (required by MVCC);
340 * 3. someday someone let tuple to cross block boundaries -
341 * he have to add something below...
344 * Up to now t_data could be NULL, the memory location directly following
345 * HeapTupleData, or pointing into a buffer. Now, it could also point to
346 * a separate allocation that was done in the t_datamcxt memory context.
348 typedef struct HeapTupleData
350 uint32 t_len; /* length of *t_data */
351 ItemPointerData t_self; /* SelfItemPointer */
352 Oid t_tableOid; /* table the tuple came from */
353 MemoryContext t_datamcxt; /* memory context of allocation */
354 HeapTupleHeader t_data; /* -> tuple header and data */
357 typedef HeapTupleData *HeapTuple;
359 #define HEAPTUPLESIZE MAXALIGN(sizeof(HeapTupleData))
362 * GETSTRUCT - given a HeapTuple pointer, return address of the user data
364 #define GETSTRUCT(TUP) ((char *) ((TUP)->t_data) + (TUP)->t_data->t_hoff)
367 * Accessor macros to be used with HeapTuple pointers.
369 #define HeapTupleIsValid(tuple) PointerIsValid(tuple)
371 #define HeapTupleHasNulls(tuple) \
372 (((tuple)->t_data->t_infomask & HEAP_HASNULL) != 0)
374 #define HeapTupleNoNulls(tuple) \
375 (!((tuple)->t_data->t_infomask & HEAP_HASNULL))
377 #define HeapTupleHasVarWidth(tuple) \
378 (((tuple)->t_data->t_infomask & HEAP_HASVARWIDTH) != 0)
380 #define HeapTupleAllFixed(tuple) \
381 (!((tuple)->t_data->t_infomask & HEAP_HASVARWIDTH))
383 #define HeapTupleHasExternal(tuple) \
384 (((tuple)->t_data->t_infomask & HEAP_HASEXTERNAL) != 0)
386 #define HeapTupleHasCompressed(tuple) \
387 (((tuple)->t_data->t_infomask & HEAP_HASCOMPRESSED) != 0)
389 #define HeapTupleHasExtended(tuple) \
390 (((tuple)->t_data->t_infomask & HEAP_HASEXTENDED) != 0)
392 #define HeapTupleGetOid(tuple) \
393 HeapTupleHeaderGetOid((tuple)->t_data)
395 #define HeapTupleSetOid(tuple, oid) \
396 HeapTupleHeaderSetOid((tuple)->t_data, (oid))
400 * WAL record definitions for heapam.c's WAL operations
402 * XLOG allows to store some information in high 4 bits of log
403 * record xl_info field. We use 3 for opcode and one for init bit.
405 #define XLOG_HEAP_INSERT 0x00
406 #define XLOG_HEAP_DELETE 0x10
407 #define XLOG_HEAP_UPDATE 0x20
408 #define XLOG_HEAP_MOVE 0x30
409 #define XLOG_HEAP_CLEAN 0x40
410 #define XLOG_HEAP_NEWPAGE 0x50
411 /* opcodes 0x60, 0x70 still free */
412 #define XLOG_HEAP_OPMASK 0x70
414 * When we insert 1st item on new page in INSERT/UPDATE
415 * we can (and we do) restore entire page in redo
417 #define XLOG_HEAP_INIT_PAGE 0x80
420 * All what we need to find changed tuple
422 * NB: on most machines, sizeof(xl_heaptid) will include some trailing pad
423 * bytes for alignment. We don't want to store the pad space in the XLOG,
424 * so use SizeOfHeapTid for space calculations. Similar comments apply for
425 * the other xl_FOO structs.
427 typedef struct xl_heaptid
430 ItemPointerData tid; /* changed tuple id */
433 #define SizeOfHeapTid (offsetof(xl_heaptid, tid) + SizeOfIptrData)
435 /* This is what we need to know about delete */
436 typedef struct xl_heap_delete
438 xl_heaptid target; /* deleted tuple id */
441 #define SizeOfHeapDelete (offsetof(xl_heap_delete, target) + SizeOfHeapTid)
444 * We don't store the whole fixed part (HeapTupleHeaderData) of an inserted
445 * or updated tuple in WAL; we can save a few bytes by reconstructing the
446 * fields that are available elsewhere in the WAL record, or perhaps just
447 * plain needn't be reconstructed. These are the fields we must store.
448 * NOTE: t_hoff could be recomputed, but we may as well store it because
449 * it will come for free due to alignment considerations.
451 typedef struct xl_heap_header
458 #define SizeOfHeapHeader (offsetof(xl_heap_header, t_hoff) + sizeof(uint8))
460 /* This is what we need to know about insert */
461 typedef struct xl_heap_insert
463 xl_heaptid target; /* inserted tuple id */
464 /* xl_heap_header & TUPLE DATA FOLLOWS AT END OF STRUCT */
467 #define SizeOfHeapInsert (offsetof(xl_heap_insert, target) + SizeOfHeapTid)
469 /* This is what we need to know about update|move */
470 typedef struct xl_heap_update
472 xl_heaptid target; /* deleted tuple id */
473 ItemPointerData newtid; /* new inserted tuple id */
474 /* NEW TUPLE xl_heap_header (PLUS xmax & xmin IF MOVE OP) */
475 /* and TUPLE DATA FOLLOWS AT END OF STRUCT */
478 #define SizeOfHeapUpdate (offsetof(xl_heap_update, newtid) + SizeOfIptrData)
480 /* This is what we need to know about vacuum page cleanup */
481 typedef struct xl_heap_clean
485 /* UNUSED OFFSET NUMBERS FOLLOW AT THE END */
488 #define SizeOfHeapClean (offsetof(xl_heap_clean, block) + sizeof(BlockNumber))
490 /* This is for replacing a page's contents in toto */
491 /* NB: this is used for indexes as well as heaps */
492 typedef struct xl_heap_newpage
495 BlockNumber blkno; /* location of new page */
496 /* entire page contents follow at end of record */
499 #define SizeOfHeapNewpage (offsetof(xl_heap_newpage, blkno) + sizeof(BlockNumber))