OSDN Git Service

Re-run pgindent, fixing a problem where comment lines after a blank
[pg-rex/syncrep.git] / 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.80 2005/11/22 18:17:29 momjian 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  * 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.
97  *
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
103  * MAXALIGN.
104  *----------
105  */
106
107 typedef struct HeapTupleFields
108 {
109         TransactionId t_xmin;           /* inserting xact ID */
110         CommandId       t_cmin;                 /* inserting command ID */
111         TransactionId t_xmax;           /* deleting or locking xact ID */
112
113         union
114         {
115                 CommandId       t_cmax;         /* deleting or locking command ID */
116                 TransactionId t_xvac;   /* VACUUM FULL xact ID */
117         }                       t_field4;
118 } HeapTupleFields;
119
120 typedef struct DatumTupleFields
121 {
122         int32           datum_len;              /* required to be a varlena type */
123
124         int32           datum_typmod;   /* -1, or identifier of a record type */
125
126         Oid                     datum_typeid;   /* composite type OID, or RECORDOID */
127
128         /*
129          * Note: field ordering is chosen with thought that Oid might someday
130          * widen to 64 bits.
131          */
132 } DatumTupleFields;
133
134 typedef struct HeapTupleHeaderData
135 {
136         union
137         {
138                 HeapTupleFields t_heap;
139                 DatumTupleFields t_datum;
140         }                       t_choice;
141
142         ItemPointerData t_ctid;         /* current TID of this or newer tuple */
143
144         int16           t_natts;                /* number of attributes */
145
146         uint16          t_infomask;             /* various flag bits, see below */
147
148         uint8           t_hoff;                 /* sizeof header incl. bitmap, padding */
149
150         /* ^ - 27 bytes - ^ */
151
152         bits8           t_bits[1];              /* bitmap of NULLs -- VARIABLE LENGTH */
153
154         /* MORE DATA FOLLOWS AT END OF STRUCT */
155 } HeapTupleHeaderData;
156
157 typedef HeapTupleHeaderData *HeapTupleHeader;
158
159 /*
160  * information stored in t_infomask:
161  */
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
180                                                                                  * FULL */
181 #define HEAP_MOVED_IN                   0x8000  /* moved from another place by VACUUM
182                                                                                  * FULL */
183 #define HEAP_MOVED (HEAP_MOVED_OFF | HEAP_MOVED_IN)
184
185 #define HEAP_XACT_MASK                  0xFFC0  /* visibility-related bits */
186
187
188 /*
189  * HeapTupleHeader accessor macros
190  *
191  * Note: beware of multiple evaluations of "tup" argument.      But the Set
192  * macros evaluate their other argument only once.
193  */
194
195 #define HeapTupleHeaderGetXmin(tup) \
196 ( \
197         (tup)->t_choice.t_heap.t_xmin \
198 )
199
200 #define HeapTupleHeaderSetXmin(tup, xid) \
201 ( \
202         TransactionIdStore((xid), &(tup)->t_choice.t_heap.t_xmin) \
203 )
204
205 #define HeapTupleHeaderGetXmax(tup) \
206 ( \
207         (tup)->t_choice.t_heap.t_xmax \
208 )
209
210 #define HeapTupleHeaderSetXmax(tup, xid) \
211 ( \
212         TransactionIdStore((xid), &(tup)->t_choice.t_heap.t_xmax) \
213 )
214
215 #define HeapTupleHeaderGetCmin(tup) \
216 ( \
217         (tup)->t_choice.t_heap.t_cmin \
218 )
219
220 #define HeapTupleHeaderSetCmin(tup, cid) \
221 ( \
222         (tup)->t_choice.t_heap.t_cmin = (cid) \
223 )
224
225 /*
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.
232  */
233 #define HeapTupleHeaderGetCmax(tup) \
234 ( \
235         (tup)->t_choice.t_heap.t_field4.t_cmax \
236 )
237
238 #define HeapTupleHeaderSetCmax(tup, cid) \
239 do { \
240         Assert(!((tup)->t_infomask & HEAP_MOVED)); \
241         (tup)->t_choice.t_heap.t_field4.t_cmax = (cid); \
242 } while (0)
243
244 #define HeapTupleHeaderGetXvac(tup) \
245 ( \
246         ((tup)->t_infomask & HEAP_MOVED) ? \
247                 (tup)->t_choice.t_heap.t_field4.t_xvac \
248         : \
249                 InvalidTransactionId \
250 )
251
252 #define HeapTupleHeaderSetXvac(tup, xid) \
253 do { \
254         Assert((tup)->t_infomask & HEAP_MOVED); \
255         TransactionIdStore((xid), &(tup)->t_choice.t_heap.t_field4.t_xvac); \
256 } while (0)
257
258 #define HeapTupleHeaderGetDatumLength(tup) \
259 ( \
260         (tup)->t_choice.t_datum.datum_len \
261 )
262
263 #define HeapTupleHeaderSetDatumLength(tup, len) \
264 ( \
265         (tup)->t_choice.t_datum.datum_len = (len) \
266 )
267
268 #define HeapTupleHeaderGetTypeId(tup) \
269 ( \
270         (tup)->t_choice.t_datum.datum_typeid \
271 )
272
273 #define HeapTupleHeaderSetTypeId(tup, typeid) \
274 ( \
275         (tup)->t_choice.t_datum.datum_typeid = (typeid) \
276 )
277
278 #define HeapTupleHeaderGetTypMod(tup) \
279 ( \
280         (tup)->t_choice.t_datum.datum_typmod \
281 )
282
283 #define HeapTupleHeaderSetTypMod(tup, typmod) \
284 ( \
285         (tup)->t_choice.t_datum.datum_typmod = (typmod) \
286 )
287
288 #define HeapTupleHeaderGetOid(tup) \
289 ( \
290         ((tup)->t_infomask & HEAP_HASOID) ? \
291                 *((Oid *) ((char *)(tup) + (tup)->t_hoff - sizeof(Oid))) \
292         : \
293                 InvalidOid \
294 )
295
296 #define HeapTupleHeaderSetOid(tup, oid) \
297 do { \
298         Assert((tup)->t_infomask & HEAP_HASOID); \
299         *((Oid *) ((char *)(tup) + (tup)->t_hoff - sizeof(Oid))) = (oid); \
300 } while (0)
301
302
303 /*
304  * BITMAPLEN(NATTS) -
305  *              Computes size of null bitmap given number of data columns.
306  */
307 #define BITMAPLEN(NATTS)        (((int)(NATTS) + 7) / 8)
308
309 /*
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).
315  *
316  * NOTE: we do not need to count an ItemId for the tuple because
317  * sizeof(PageHeaderData) includes the first ItemId on the page.
318  */
319 #define MaxSpecialSpace  32
320
321 #define MaxTupleSize    \
322         (BLCKSZ - MAXALIGN(sizeof(PageHeaderData) + MaxSpecialSpace))
323
324 /*
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.
329  */
330 #define MaxHeapTuplesPerPage    \
331         ((int) ((BLCKSZ - offsetof(PageHeaderData, pd_linp)) / \
332                         (MAXALIGN(offsetof(HeapTupleHeaderData, t_bits)) + sizeof(ItemIdData))))
333
334 /*
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.
340  */
341 #define MaxAttrSize             (10 * 1024 * 1024)
342
343
344 /*
345  * Attribute numbers for the system-defined attributes
346  */
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)
355
356
357 /*
358  * HeapTupleData is an in-memory data structure that points to a tuple.
359  *
360  * There are several ways in which this data structure is used:
361  *
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).
365  *
366  * * Pointer to nothing: t_data is NULL.  This is used as a failure indication
367  *       in some functions.
368  *
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.
373  *
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.)
378  *
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.
383  */
384 typedef struct HeapTupleData
385 {
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 */
390 } HeapTupleData;
391
392 typedef HeapTupleData *HeapTuple;
393
394 #define HEAPTUPLESIZE   MAXALIGN(sizeof(HeapTupleData))
395
396 /*
397  * GETSTRUCT - given a HeapTuple pointer, return address of the user data
398  */
399 #define GETSTRUCT(TUP) ((char *) ((TUP)->t_data) + (TUP)->t_data->t_hoff)
400
401 /*
402  * Accessor macros to be used with HeapTuple pointers.
403  */
404 #define HeapTupleIsValid(tuple) PointerIsValid(tuple)
405
406 #define HeapTupleHasNulls(tuple) \
407                 (((tuple)->t_data->t_infomask & HEAP_HASNULL) != 0)
408
409 #define HeapTupleNoNulls(tuple) \
410                 (!((tuple)->t_data->t_infomask & HEAP_HASNULL))
411
412 #define HeapTupleHasVarWidth(tuple) \
413                 (((tuple)->t_data->t_infomask & HEAP_HASVARWIDTH) != 0)
414
415 #define HeapTupleAllFixed(tuple) \
416                 (!((tuple)->t_data->t_infomask & HEAP_HASVARWIDTH))
417
418 #define HeapTupleHasExternal(tuple) \
419                 (((tuple)->t_data->t_infomask & HEAP_HASEXTERNAL) != 0)
420
421 #define HeapTupleHasCompressed(tuple) \
422                 (((tuple)->t_data->t_infomask & HEAP_HASCOMPRESSED) != 0)
423
424 #define HeapTupleHasExtended(tuple) \
425                 (((tuple)->t_data->t_infomask & HEAP_HASEXTENDED) != 0)
426
427 #define HeapTupleGetOid(tuple) \
428                 HeapTupleHeaderGetOid((tuple)->t_data)
429
430 #define HeapTupleSetOid(tuple, oid) \
431                 HeapTupleHeaderSetOid((tuple)->t_data, (oid))
432
433
434 /*
435  * WAL record definitions for heapam.c's WAL operations
436  *
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.
439  */
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
449 /*
450  * When we insert 1st item on new page in INSERT/UPDATE
451  * we can (and we do) restore entire page in redo
452  */
453 #define XLOG_HEAP_INIT_PAGE 0x80
454
455 /*
456  * All what we need to find changed tuple
457  *
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.
462  */
463 typedef struct xl_heaptid
464 {
465         RelFileNode node;
466         ItemPointerData tid;            /* changed tuple id */
467 } xl_heaptid;
468
469 #define SizeOfHeapTid           (offsetof(xl_heaptid, tid) + SizeOfIptrData)
470
471 /* This is what we need to know about delete */
472 typedef struct xl_heap_delete
473 {
474         xl_heaptid      target;                 /* deleted tuple id */
475 } xl_heap_delete;
476
477 #define SizeOfHeapDelete        (offsetof(xl_heap_delete, target) + SizeOfHeapTid)
478
479 /*
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.
486  */
487 typedef struct xl_heap_header
488 {
489         int16           t_natts;
490         uint16          t_infomask;
491         uint8           t_hoff;
492 } xl_heap_header;
493
494 #define SizeOfHeapHeader        (offsetof(xl_heap_header, t_hoff) + sizeof(uint8))
495
496 /* This is what we need to know about insert */
497 typedef struct xl_heap_insert
498 {
499         xl_heaptid      target;                 /* inserted tuple id */
500         /* xl_heap_header & TUPLE DATA FOLLOWS AT END OF STRUCT */
501 } xl_heap_insert;
502
503 #define SizeOfHeapInsert        (offsetof(xl_heap_insert, target) + SizeOfHeapTid)
504
505 /* This is what we need to know about update|move */
506 typedef struct xl_heap_update
507 {
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 */
512 } xl_heap_update;
513
514 #define SizeOfHeapUpdate        (offsetof(xl_heap_update, newtid) + SizeOfIptrData)
515
516 /* This is what we need to know about vacuum page cleanup */
517 typedef struct xl_heap_clean
518 {
519         RelFileNode node;
520         BlockNumber block;
521         /* UNUSED OFFSET NUMBERS FOLLOW AT THE END */
522 } xl_heap_clean;
523
524 #define SizeOfHeapClean (offsetof(xl_heap_clean, block) + sizeof(BlockNumber))
525
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
529 {
530         RelFileNode node;
531         BlockNumber blkno;                      /* location of new page */
532         /* entire page contents follow at end of record */
533 } xl_heap_newpage;
534
535 #define SizeOfHeapNewpage       (offsetof(xl_heap_newpage, blkno) + sizeof(BlockNumber))
536
537 /* This is what we need to know about lock */
538 typedef struct xl_heap_lock
539 {
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? */
544 } xl_heap_lock;
545
546 #define SizeOfHeapLock  (offsetof(xl_heap_lock, shared_lock) + sizeof(bool))
547
548 #endif   /* HTUP_H */