]> granicus.if.org Git - postgresql/blob - src/include/access/htup.h
Tag appropriate files for rc3
[postgresql] / src / include / access / htup.h
1  /*-------------------------------------------------------------------------
2  *
3  * htup.h
4  *        POSTGRES heap tuple definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $PostgreSQL: pgsql/src/include/access/htup.h,v 1.72 2004/12/31 22:03:21 pgsql Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef HTUP_H
15 #define HTUP_H
16
17 #include "storage/bufpage.h"
18 #include "storage/relfilenode.h"
19 #include "access/transam.h"
20
21
22 /*
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.
31  */
32 #define MaxTupleAttributeNumber 1664    /* 8 * 208 */
33
34 /*----------
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.
45  *----------
46  */
47 #define MaxHeapAttributeNumber  1600    /* 8 * 200 */
48
49 /*----------
50  * Heap tuple header.  To avoid wasting space, the fields should be
51  * layed out in such a way 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 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).
78  *
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.
82  *
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
88  * MAXALIGN.
89  *----------
90  */
91
92 typedef struct HeapTupleFields
93 {
94         TransactionId t_xmin;           /* inserting xact ID */
95         CommandId       t_cmin;                 /* inserting command ID */
96         TransactionId t_xmax;           /* deleting xact ID */
97
98         union
99         {
100                 CommandId       t_cmax;         /* deleting command ID */
101                 TransactionId t_xvac;   /* VACUUM FULL xact ID */
102         }                       t_field4;
103 } HeapTupleFields;
104
105 typedef struct DatumTupleFields
106 {
107         int32           datum_len;              /* required to be a varlena type */
108
109         int32           datum_typmod;   /* -1, or identifier of a record type */
110
111         Oid                     datum_typeid;   /* composite type OID, or RECORDOID */
112
113         /*
114          * Note: field ordering is chosen with thought that Oid might someday
115          * widen to 64 bits.
116          */
117 } DatumTupleFields;
118
119 typedef struct HeapTupleHeaderData
120 {
121         union
122         {
123                 HeapTupleFields t_heap;
124                 DatumTupleFields t_datum;
125         }                       t_choice;
126
127         ItemPointerData t_ctid;         /* current TID of this or newer tuple */
128
129         int16           t_natts;                /* number of attributes */
130
131         uint16          t_infomask;             /* various flag bits, see below */
132
133         uint8           t_hoff;                 /* sizeof header incl. bitmap, padding */
134
135         /* ^ - 27 bytes - ^ */
136
137         bits8           t_bits[1];              /* bitmap of NULLs -- VARIABLE LENGTH */
138
139         /* MORE DATA FOLLOWS AT END OF STRUCT */
140 } HeapTupleHeaderData;
141
142 typedef HeapTupleHeaderData *HeapTupleHeader;
143
144 /*
145  * information stored in t_infomask:
146  */
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
150                                                                                  * attribute(s) */
151 #define HEAP_HASCOMPRESSED              0x0008  /* has compressed stored
152                                                                                  * attribute(s) */
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
157                                                                                  * without logging */
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
165                                                                                  * VACUUM FULL */
166 #define HEAP_MOVED_IN                   0x8000  /* moved from another place by
167                                                                                  * VACUUM FULL */
168 #define HEAP_MOVED (HEAP_MOVED_OFF | HEAP_MOVED_IN)
169
170 #define HEAP_XACT_MASK                  0xFFC0  /* visibility-related bits */
171
172
173 /*
174  * HeapTupleHeader accessor macros
175  *
176  * Note: beware of multiple evaluations of "tup" argument.      But the Set
177  * macros evaluate their other argument only once.
178  */
179
180 #define HeapTupleHeaderGetXmin(tup) \
181 ( \
182         (tup)->t_choice.t_heap.t_xmin \
183 )
184
185 #define HeapTupleHeaderSetXmin(tup, xid) \
186 ( \
187         TransactionIdStore((xid), &(tup)->t_choice.t_heap.t_xmin) \
188 )
189
190 #define HeapTupleHeaderGetXmax(tup) \
191 ( \
192         (tup)->t_choice.t_heap.t_xmax \
193 )
194
195 #define HeapTupleHeaderSetXmax(tup, xid) \
196 ( \
197         TransactionIdStore((xid), &(tup)->t_choice.t_heap.t_xmax) \
198 )
199
200 #define HeapTupleHeaderGetCmin(tup) \
201 ( \
202         (tup)->t_choice.t_heap.t_cmin \
203 )
204
205 #define HeapTupleHeaderSetCmin(tup, cid) \
206 ( \
207         (tup)->t_choice.t_heap.t_cmin = (cid) \
208 )
209
210 /*
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.
217  */
218 #define HeapTupleHeaderGetCmax(tup) \
219 ( \
220         (tup)->t_choice.t_heap.t_field4.t_cmax \
221 )
222
223 #define HeapTupleHeaderSetCmax(tup, cid) \
224 do { \
225         Assert(!((tup)->t_infomask & HEAP_MOVED)); \
226         (tup)->t_choice.t_heap.t_field4.t_cmax = (cid); \
227 } while (0)
228
229 #define HeapTupleHeaderGetXvac(tup) \
230 ( \
231         ((tup)->t_infomask & HEAP_MOVED) ? \
232                 (tup)->t_choice.t_heap.t_field4.t_xvac \
233         : \
234                 InvalidTransactionId \
235 )
236
237 #define HeapTupleHeaderSetXvac(tup, xid) \
238 do { \
239         Assert((tup)->t_infomask & HEAP_MOVED); \
240         TransactionIdStore((xid), &(tup)->t_choice.t_heap.t_field4.t_xvac); \
241 } while (0)
242
243 #define HeapTupleHeaderGetDatumLength(tup) \
244 ( \
245         (tup)->t_choice.t_datum.datum_len \
246 )
247
248 #define HeapTupleHeaderSetDatumLength(tup, len) \
249 ( \
250         (tup)->t_choice.t_datum.datum_len = (len) \
251 )
252
253 #define HeapTupleHeaderGetTypeId(tup) \
254 ( \
255         (tup)->t_choice.t_datum.datum_typeid \
256 )
257
258 #define HeapTupleHeaderSetTypeId(tup, typeid) \
259 ( \
260         (tup)->t_choice.t_datum.datum_typeid = (typeid) \
261 )
262
263 #define HeapTupleHeaderGetTypMod(tup) \
264 ( \
265         (tup)->t_choice.t_datum.datum_typmod \
266 )
267
268 #define HeapTupleHeaderSetTypMod(tup, typmod) \
269 ( \
270         (tup)->t_choice.t_datum.datum_typmod = (typmod) \
271 )
272
273 #define HeapTupleHeaderGetOid(tup) \
274 ( \
275         ((tup)->t_infomask & HEAP_HASOID) ? \
276                 *((Oid *) ((char *)(tup) + (tup)->t_hoff - sizeof(Oid))) \
277         : \
278                 InvalidOid \
279 )
280
281 #define HeapTupleHeaderSetOid(tup, oid) \
282 do { \
283         Assert((tup)->t_infomask & HEAP_HASOID); \
284         *((Oid *) ((char *)(tup) + (tup)->t_hoff - sizeof(Oid))) = (oid); \
285 } while (0)
286
287
288 /*
289  * BITMAPLEN(NATTS) -
290  *              Computes size of null bitmap given number of data columns.
291  */
292 #define BITMAPLEN(NATTS)        (((int)(NATTS) + 7) / 8)
293
294 /*
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).
300  *
301  * NOTE: we do not need to count an ItemId for the tuple because
302  * sizeof(PageHeaderData) includes the first ItemId on the page.
303  */
304 #define MaxSpecialSpace  32
305
306 #define MaxTupleSize    \
307         (BLCKSZ - MAXALIGN(sizeof(PageHeaderData) + MaxSpecialSpace))
308
309 /*
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.
315  */
316 #define MaxAttrSize             (10 * 1024 * 1024)
317
318
319 /*
320  * Attribute numbers for the system-defined attributes
321  */
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)
330
331
332 /*
333  * HeapTupleData is an in-memory data structure that points to a tuple.
334  *
335  * This new HeapTuple for version >= 6.5 and this is why it was changed:
336  *
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...
342  *
343  * Change for 7.0:
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.
347  */
348 typedef struct HeapTupleData
349 {
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 */
355 } HeapTupleData;
356
357 typedef HeapTupleData *HeapTuple;
358
359 #define HEAPTUPLESIZE   MAXALIGN(sizeof(HeapTupleData))
360
361 /*
362  * GETSTRUCT - given a HeapTuple pointer, return address of the user data
363  */
364 #define GETSTRUCT(TUP) ((char *) ((TUP)->t_data) + (TUP)->t_data->t_hoff)
365
366 /*
367  * Accessor macros to be used with HeapTuple pointers.
368  */
369 #define HeapTupleIsValid(tuple) PointerIsValid(tuple)
370
371 #define HeapTupleHasNulls(tuple) \
372                 (((tuple)->t_data->t_infomask & HEAP_HASNULL) != 0)
373
374 #define HeapTupleNoNulls(tuple) \
375                 (!((tuple)->t_data->t_infomask & HEAP_HASNULL))
376
377 #define HeapTupleHasVarWidth(tuple) \
378                 (((tuple)->t_data->t_infomask & HEAP_HASVARWIDTH) != 0)
379
380 #define HeapTupleAllFixed(tuple) \
381                 (!((tuple)->t_data->t_infomask & HEAP_HASVARWIDTH))
382
383 #define HeapTupleHasExternal(tuple) \
384                 (((tuple)->t_data->t_infomask & HEAP_HASEXTERNAL) != 0)
385
386 #define HeapTupleHasCompressed(tuple) \
387                 (((tuple)->t_data->t_infomask & HEAP_HASCOMPRESSED) != 0)
388
389 #define HeapTupleHasExtended(tuple) \
390                 (((tuple)->t_data->t_infomask & HEAP_HASEXTENDED) != 0)
391
392 #define HeapTupleGetOid(tuple) \
393                 HeapTupleHeaderGetOid((tuple)->t_data)
394
395 #define HeapTupleSetOid(tuple, oid) \
396                 HeapTupleHeaderSetOid((tuple)->t_data, (oid))
397
398
399 /*
400  * WAL record definitions for heapam.c's WAL operations
401  *
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.
404  */
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
413 /*
414  * When we insert 1st item on new page in INSERT/UPDATE
415  * we can (and we do) restore entire page in redo
416  */
417 #define XLOG_HEAP_INIT_PAGE 0x80
418
419 /*
420  * All what we need to find changed tuple
421  *
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.
426  */
427 typedef struct xl_heaptid
428 {
429         RelFileNode node;
430         ItemPointerData tid;            /* changed tuple id */
431 } xl_heaptid;
432
433 #define SizeOfHeapTid           (offsetof(xl_heaptid, tid) + SizeOfIptrData)
434
435 /* This is what we need to know about delete */
436 typedef struct xl_heap_delete
437 {
438         xl_heaptid      target;                 /* deleted tuple id */
439 } xl_heap_delete;
440
441 #define SizeOfHeapDelete        (offsetof(xl_heap_delete, target) + SizeOfHeapTid)
442
443 /*
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.
450  */
451 typedef struct xl_heap_header
452 {
453         int16           t_natts;
454         uint16          t_infomask;
455         uint8           t_hoff;
456 } xl_heap_header;
457
458 #define SizeOfHeapHeader        (offsetof(xl_heap_header, t_hoff) + sizeof(uint8))
459
460 /* This is what we need to know about insert */
461 typedef struct xl_heap_insert
462 {
463         xl_heaptid      target;                 /* inserted tuple id */
464         /* xl_heap_header & TUPLE DATA FOLLOWS AT END OF STRUCT */
465 } xl_heap_insert;
466
467 #define SizeOfHeapInsert        (offsetof(xl_heap_insert, target) + SizeOfHeapTid)
468
469 /* This is what we need to know about update|move */
470 typedef struct xl_heap_update
471 {
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 */
476 } xl_heap_update;
477
478 #define SizeOfHeapUpdate        (offsetof(xl_heap_update, newtid) + SizeOfIptrData)
479
480 /* This is what we need to know about vacuum page cleanup */
481 typedef struct xl_heap_clean
482 {
483         RelFileNode node;
484         BlockNumber block;
485         /* UNUSED OFFSET NUMBERS FOLLOW AT THE END */
486 } xl_heap_clean;
487
488 #define SizeOfHeapClean (offsetof(xl_heap_clean, block) + sizeof(BlockNumber))
489
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
493 {
494         RelFileNode node;
495         BlockNumber blkno;                      /* location of new page */
496         /* entire page contents follow at end of record */
497 } xl_heap_newpage;
498
499 #define SizeOfHeapNewpage       (offsetof(xl_heap_newpage, blkno) + sizeof(BlockNumber))
500
501 #endif   /* HTUP_H */