Header And Logo

PostgreSQL
| The world's most advanced open source database.

htup_details.h

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------
00002  *
00003  * htup_details.h
00004  *    POSTGRES heap tuple header definitions.
00005  *
00006  *
00007  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
00008  * Portions Copyright (c) 1994, Regents of the University of California
00009  *
00010  * src/include/access/htup_details.h
00011  *
00012  *-------------------------------------------------------------------------
00013  */
00014 #ifndef HTUP_DETAILS_H
00015 #define HTUP_DETAILS_H
00016 
00017 #include "access/htup.h"
00018 #include "access/tupdesc.h"
00019 #include "access/tupmacs.h"
00020 #include "storage/bufpage.h"
00021 
00022 /*
00023  * MaxTupleAttributeNumber limits the number of (user) columns in a tuple.
00024  * The key limit on this value is that the size of the fixed overhead for
00025  * a tuple, plus the size of the null-values bitmap (at 1 bit per column),
00026  * plus MAXALIGN alignment, must fit into t_hoff which is uint8.  On most
00027  * machines the upper limit without making t_hoff wider would be a little
00028  * over 1700.  We use round numbers here and for MaxHeapAttributeNumber
00029  * so that alterations in HeapTupleHeaderData layout won't change the
00030  * supported max number of columns.
00031  */
00032 #define MaxTupleAttributeNumber 1664    /* 8 * 208 */
00033 
00034 /*
00035  * MaxHeapAttributeNumber limits the number of (user) columns in a table.
00036  * This should be somewhat less than MaxTupleAttributeNumber.  It must be
00037  * at least one less, else we will fail to do UPDATEs on a maximal-width
00038  * table (because UPDATE has to form working tuples that include CTID).
00039  * In practice we want some additional daylight so that we can gracefully
00040  * support operations that add hidden "resjunk" columns, for example
00041  * SELECT * FROM wide_table ORDER BY foo, bar, baz.
00042  * In any case, depending on column data types you will likely be running
00043  * into the disk-block-based limit on overall tuple size if you have more
00044  * than a thousand or so columns.  TOAST won't help.
00045  */
00046 #define MaxHeapAttributeNumber  1600    /* 8 * 200 */
00047 
00048 /*
00049  * Heap tuple header.  To avoid wasting space, the fields should be
00050  * laid out in such a way as to avoid structure padding.
00051  *
00052  * Datums of composite types (row types) share the same general structure
00053  * as on-disk tuples, so that the same routines can be used to build and
00054  * examine them.  However the requirements are slightly different: a Datum
00055  * does not need any transaction visibility information, and it does need
00056  * a length word and some embedded type information.  We can achieve this
00057  * by overlaying the xmin/cmin/xmax/cmax/xvac fields of a heap tuple
00058  * with the fields needed in the Datum case.  Typically, all tuples built
00059  * in-memory will be initialized with the Datum fields; but when a tuple is
00060  * about to be inserted in a table, the transaction fields will be filled,
00061  * overwriting the datum fields.
00062  *
00063  * The overall structure of a heap tuple looks like:
00064  *          fixed fields (HeapTupleHeaderData struct)
00065  *          nulls bitmap (if HEAP_HASNULL is set in t_infomask)
00066  *          alignment padding (as needed to make user data MAXALIGN'd)
00067  *          object ID (if HEAP_HASOID is set in t_infomask)
00068  *          user data fields
00069  *
00070  * We store five "virtual" fields Xmin, Cmin, Xmax, Cmax, and Xvac in three
00071  * physical fields.  Xmin and Xmax are always really stored, but Cmin, Cmax
00072  * and Xvac share a field.  This works because we know that Cmin and Cmax
00073  * are only interesting for the lifetime of the inserting and deleting
00074  * transaction respectively.  If a tuple is inserted and deleted in the same
00075  * transaction, we store a "combo" command id that can be mapped to the real
00076  * cmin and cmax, but only by use of local state within the originating
00077  * backend.  See combocid.c for more details.  Meanwhile, Xvac is only set by
00078  * old-style VACUUM FULL, which does not have any command sub-structure and so
00079  * does not need either Cmin or Cmax.  (This requires that old-style VACUUM
00080  * FULL never try to move a tuple whose Cmin or Cmax is still interesting,
00081  * ie, an insert-in-progress or delete-in-progress tuple.)
00082  *
00083  * A word about t_ctid: whenever a new tuple is stored on disk, its t_ctid
00084  * is initialized with its own TID (location).  If the tuple is ever updated,
00085  * its t_ctid is changed to point to the replacement version of the tuple.
00086  * Thus, a tuple is the latest version of its row iff XMAX is invalid or
00087  * t_ctid points to itself (in which case, if XMAX is valid, the tuple is
00088  * either locked or deleted).  One can follow the chain of t_ctid links
00089  * to find the newest version of the row.  Beware however that VACUUM might
00090  * erase the pointed-to (newer) tuple before erasing the pointing (older)
00091  * tuple.  Hence, when following a t_ctid link, it is necessary to check
00092  * to see if the referenced slot is empty or contains an unrelated tuple.
00093  * Check that the referenced tuple has XMIN equal to the referencing tuple's
00094  * XMAX to verify that it is actually the descendant version and not an
00095  * unrelated tuple stored into a slot recently freed by VACUUM.  If either
00096  * check fails, one may assume that there is no live descendant version.
00097  *
00098  * Following the fixed header fields, the nulls bitmap is stored (beginning
00099  * at t_bits).  The bitmap is *not* stored if t_infomask shows that there
00100  * are no nulls in the tuple.  If an OID field is present (as indicated by
00101  * t_infomask), then it is stored just before the user data, which begins at
00102  * the offset shown by t_hoff.  Note that t_hoff must be a multiple of
00103  * MAXALIGN.
00104  */
00105 
00106 typedef struct HeapTupleFields
00107 {
00108     TransactionId t_xmin;       /* inserting xact ID */
00109     TransactionId t_xmax;       /* deleting or locking xact ID */
00110 
00111     union
00112     {
00113         CommandId   t_cid;      /* inserting or deleting command ID, or both */
00114         TransactionId t_xvac;   /* old-style VACUUM FULL xact ID */
00115     }           t_field3;
00116 } HeapTupleFields;
00117 
00118 typedef struct DatumTupleFields
00119 {
00120     int32       datum_len_;     /* varlena header (do not touch directly!) */
00121 
00122     int32       datum_typmod;   /* -1, or identifier of a record type */
00123 
00124     Oid         datum_typeid;   /* composite type OID, or RECORDOID */
00125 
00126     /*
00127      * Note: field ordering is chosen with thought that Oid might someday
00128      * widen to 64 bits.
00129      */
00130 } DatumTupleFields;
00131 
00132 struct HeapTupleHeaderData
00133 {
00134     union
00135     {
00136         HeapTupleFields t_heap;
00137         DatumTupleFields t_datum;
00138     }           t_choice;
00139 
00140     ItemPointerData t_ctid;     /* current TID of this or newer tuple */
00141 
00142     /* Fields below here must match MinimalTupleData! */
00143 
00144     uint16      t_infomask2;    /* number of attributes + various flags */
00145 
00146     uint16      t_infomask;     /* various flag bits, see below */
00147 
00148     uint8       t_hoff;         /* sizeof header incl. bitmap, padding */
00149 
00150     /* ^ - 23 bytes - ^ */
00151 
00152     bits8       t_bits[1];      /* bitmap of NULLs -- VARIABLE LENGTH */
00153 
00154     /* MORE DATA FOLLOWS AT END OF STRUCT */
00155 };
00156 /* typedef appears in tupbasics.h */
00157 
00158 /*
00159  * information stored in t_infomask:
00160  */
00161 #define HEAP_HASNULL            0x0001  /* has null attribute(s) */
00162 #define HEAP_HASVARWIDTH        0x0002  /* has variable-width attribute(s) */
00163 #define HEAP_HASEXTERNAL        0x0004  /* has external stored attribute(s) */
00164 #define HEAP_HASOID             0x0008  /* has an object-id field */
00165 #define HEAP_XMAX_KEYSHR_LOCK   0x0010  /* xmax is a key-shared locker */
00166 #define HEAP_COMBOCID           0x0020  /* t_cid is a combo cid */
00167 #define HEAP_XMAX_EXCL_LOCK     0x0040  /* xmax is exclusive locker */
00168 #define HEAP_XMAX_LOCK_ONLY     0x0080  /* xmax, if valid, is only a locker */
00169 
00170                                         /* xmax is a shared locker */
00171 #define HEAP_XMAX_SHR_LOCK  (HEAP_XMAX_EXCL_LOCK | HEAP_XMAX_KEYSHR_LOCK)
00172 
00173 #define HEAP_LOCK_MASK  (HEAP_XMAX_SHR_LOCK | HEAP_XMAX_EXCL_LOCK | \
00174                          HEAP_XMAX_KEYSHR_LOCK)
00175 #define HEAP_XMIN_COMMITTED     0x0100  /* t_xmin committed */
00176 #define HEAP_XMIN_INVALID       0x0200  /* t_xmin invalid/aborted */
00177 #define HEAP_XMAX_COMMITTED     0x0400  /* t_xmax committed */
00178 #define HEAP_XMAX_INVALID       0x0800  /* t_xmax invalid/aborted */
00179 #define HEAP_XMAX_IS_MULTI      0x1000  /* t_xmax is a MultiXactId */
00180 #define HEAP_UPDATED            0x2000  /* this is UPDATEd version of row */
00181 #define HEAP_MOVED_OFF          0x4000  /* moved to another place by pre-9.0
00182                                          * VACUUM FULL; kept for binary
00183                                          * upgrade support */
00184 #define HEAP_MOVED_IN           0x8000  /* moved from another place by pre-9.0
00185                                          * VACUUM FULL; kept for binary
00186                                          * upgrade support */
00187 #define HEAP_MOVED (HEAP_MOVED_OFF | HEAP_MOVED_IN)
00188 
00189 #define HEAP_XACT_MASK          0xFFF0  /* visibility-related bits */
00190 
00191 /*
00192  * A tuple is only locked (i.e. not updated by its Xmax) if the
00193  * HEAP_XMAX_LOCK_ONLY bit is set; or, for pg_upgrade's sake, if the Xmax is
00194  * not a multi and the EXCL_LOCK bit is set.
00195  *
00196  * See also HeapTupleHeaderIsOnlyLocked, which also checks for a possible
00197  * aborted updater transaction.
00198  *
00199  * Beware of multiple evaluations of the argument.
00200  */
00201 #define HEAP_XMAX_IS_LOCKED_ONLY(infomask) \
00202     (((infomask) & HEAP_XMAX_LOCK_ONLY) || \
00203      (((infomask) & (HEAP_XMAX_IS_MULTI | HEAP_LOCK_MASK)) == HEAP_XMAX_EXCL_LOCK))
00204 
00205 /*
00206  * Use these to test whether a particular lock is applied to a tuple
00207  */
00208 #define HEAP_XMAX_IS_SHR_LOCKED(infomask) \
00209     (((infomask) & HEAP_LOCK_MASK) == HEAP_XMAX_SHR_LOCK)
00210 #define HEAP_XMAX_IS_EXCL_LOCKED(infomask) \
00211     (((infomask) & HEAP_LOCK_MASK) == HEAP_XMAX_EXCL_LOCK)
00212 #define HEAP_XMAX_IS_KEYSHR_LOCKED(infomask) \
00213     (((infomask) & HEAP_LOCK_MASK) == HEAP_XMAX_KEYSHR_LOCK)
00214 
00215 /* turn these all off when Xmax is to change */
00216 #define HEAP_XMAX_BITS (HEAP_XMAX_COMMITTED | HEAP_XMAX_INVALID | \
00217                         HEAP_XMAX_IS_MULTI | HEAP_LOCK_MASK | HEAP_XMAX_LOCK_ONLY)
00218 
00219 /*
00220  * information stored in t_infomask2:
00221  */
00222 #define HEAP_NATTS_MASK         0x07FF  /* 11 bits for number of attributes */
00223 /* bits 0x1800 are available */
00224 #define HEAP_KEYS_UPDATED       0x2000  /* tuple was updated and key cols
00225                                          * modified, or tuple deleted */
00226 #define HEAP_HOT_UPDATED        0x4000  /* tuple was HOT-updated */
00227 #define HEAP_ONLY_TUPLE         0x8000  /* this is heap-only tuple */
00228 
00229 #define HEAP2_XACT_MASK         0xE000  /* visibility-related bits */
00230 
00231 /*
00232  * HEAP_TUPLE_HAS_MATCH is a temporary flag used during hash joins.  It is
00233  * only used in tuples that are in the hash table, and those don't need
00234  * any visibility information, so we can overlay it on a visibility flag
00235  * instead of using up a dedicated bit.
00236  */
00237 #define HEAP_TUPLE_HAS_MATCH    HEAP_ONLY_TUPLE /* tuple has a join match */
00238 
00239 /*
00240  * HeapTupleHeader accessor macros
00241  *
00242  * Note: beware of multiple evaluations of "tup" argument.  But the Set
00243  * macros evaluate their other argument only once.
00244  */
00245 
00246 #define HeapTupleHeaderGetXmin(tup) \
00247 ( \
00248     (tup)->t_choice.t_heap.t_xmin \
00249 )
00250 
00251 #define HeapTupleHeaderSetXmin(tup, xid) \
00252 ( \
00253     (tup)->t_choice.t_heap.t_xmin = (xid) \
00254 )
00255 
00256 /*
00257  * HeapTupleHeaderGetRawXmax gets you the raw Xmax field.  To find out the Xid
00258  * that updated a tuple, you might need to resolve the MultiXactId if certain
00259  * bits are set.  HeapTupleHeaderGetUpdateXid checks those bits and takes care
00260  * to resolve the MultiXactId if necessary.  This might involve multixact I/O,
00261  * so it should only be used if absolutely necessary.
00262  */
00263 #define HeapTupleHeaderGetUpdateXid(tup) \
00264 ( \
00265     (!((tup)->t_infomask & HEAP_XMAX_INVALID) && \
00266      ((tup)->t_infomask & HEAP_XMAX_IS_MULTI) && \
00267      !((tup)->t_infomask & HEAP_XMAX_LOCK_ONLY)) ? \
00268         HeapTupleGetUpdateXid(tup) \
00269     : \
00270         HeapTupleHeaderGetRawXmax(tup) \
00271 )
00272 
00273 #define HeapTupleHeaderGetRawXmax(tup) \
00274 ( \
00275     (tup)->t_choice.t_heap.t_xmax \
00276 )
00277 
00278 #define HeapTupleHeaderSetXmax(tup, xid) \
00279 ( \
00280     (tup)->t_choice.t_heap.t_xmax = (xid) \
00281 )
00282 
00283 /*
00284  * HeapTupleHeaderGetRawCommandId will give you what's in the header whether
00285  * it is useful or not.  Most code should use HeapTupleHeaderGetCmin or
00286  * HeapTupleHeaderGetCmax instead, but note that those Assert that you can
00287  * get a legitimate result, ie you are in the originating transaction!
00288  */
00289 #define HeapTupleHeaderGetRawCommandId(tup) \
00290 ( \
00291     (tup)->t_choice.t_heap.t_field3.t_cid \
00292 )
00293 
00294 /* SetCmin is reasonably simple since we never need a combo CID */
00295 #define HeapTupleHeaderSetCmin(tup, cid) \
00296 do { \
00297     Assert(!((tup)->t_infomask & HEAP_MOVED)); \
00298     (tup)->t_choice.t_heap.t_field3.t_cid = (cid); \
00299     (tup)->t_infomask &= ~HEAP_COMBOCID; \
00300 } while (0)
00301 
00302 /* SetCmax must be used after HeapTupleHeaderAdjustCmax; see combocid.c */
00303 #define HeapTupleHeaderSetCmax(tup, cid, iscombo) \
00304 do { \
00305     Assert(!((tup)->t_infomask & HEAP_MOVED)); \
00306     (tup)->t_choice.t_heap.t_field3.t_cid = (cid); \
00307     if (iscombo) \
00308         (tup)->t_infomask |= HEAP_COMBOCID; \
00309     else \
00310         (tup)->t_infomask &= ~HEAP_COMBOCID; \
00311 } while (0)
00312 
00313 #define HeapTupleHeaderGetXvac(tup) \
00314 ( \
00315     ((tup)->t_infomask & HEAP_MOVED) ? \
00316         (tup)->t_choice.t_heap.t_field3.t_xvac \
00317     : \
00318         InvalidTransactionId \
00319 )
00320 
00321 #define HeapTupleHeaderSetXvac(tup, xid) \
00322 do { \
00323     Assert((tup)->t_infomask & HEAP_MOVED); \
00324     (tup)->t_choice.t_heap.t_field3.t_xvac = (xid); \
00325 } while (0)
00326 
00327 #define HeapTupleHeaderGetDatumLength(tup) \
00328     VARSIZE(tup)
00329 
00330 #define HeapTupleHeaderSetDatumLength(tup, len) \
00331     SET_VARSIZE(tup, len)
00332 
00333 #define HeapTupleHeaderGetTypeId(tup) \
00334 ( \
00335     (tup)->t_choice.t_datum.datum_typeid \
00336 )
00337 
00338 #define HeapTupleHeaderSetTypeId(tup, typeid) \
00339 ( \
00340     (tup)->t_choice.t_datum.datum_typeid = (typeid) \
00341 )
00342 
00343 #define HeapTupleHeaderGetTypMod(tup) \
00344 ( \
00345     (tup)->t_choice.t_datum.datum_typmod \
00346 )
00347 
00348 #define HeapTupleHeaderSetTypMod(tup, typmod) \
00349 ( \
00350     (tup)->t_choice.t_datum.datum_typmod = (typmod) \
00351 )
00352 
00353 #define HeapTupleHeaderGetOid(tup) \
00354 ( \
00355     ((tup)->t_infomask & HEAP_HASOID) ? \
00356         *((Oid *) ((char *)(tup) + (tup)->t_hoff - sizeof(Oid))) \
00357     : \
00358         InvalidOid \
00359 )
00360 
00361 #define HeapTupleHeaderSetOid(tup, oid) \
00362 do { \
00363     Assert((tup)->t_infomask & HEAP_HASOID); \
00364     *((Oid *) ((char *)(tup) + (tup)->t_hoff - sizeof(Oid))) = (oid); \
00365 } while (0)
00366 
00367 /*
00368  * Note that we stop considering a tuple HOT-updated as soon as it is known
00369  * aborted or the would-be updating transaction is known aborted.  For best
00370  * efficiency, check tuple visibility before using this macro, so that the
00371  * INVALID bits will be as up to date as possible.
00372  */
00373 #define HeapTupleHeaderIsHotUpdated(tup) \
00374 ( \
00375     ((tup)->t_infomask2 & HEAP_HOT_UPDATED) != 0 && \
00376     ((tup)->t_infomask & (HEAP_XMIN_INVALID | HEAP_XMAX_INVALID)) == 0 \
00377 )
00378 
00379 #define HeapTupleHeaderSetHotUpdated(tup) \
00380 ( \
00381     (tup)->t_infomask2 |= HEAP_HOT_UPDATED \
00382 )
00383 
00384 #define HeapTupleHeaderClearHotUpdated(tup) \
00385 ( \
00386     (tup)->t_infomask2 &= ~HEAP_HOT_UPDATED \
00387 )
00388 
00389 #define HeapTupleHeaderIsHeapOnly(tup) \
00390 ( \
00391   (tup)->t_infomask2 & HEAP_ONLY_TUPLE \
00392 )
00393 
00394 #define HeapTupleHeaderSetHeapOnly(tup) \
00395 ( \
00396   (tup)->t_infomask2 |= HEAP_ONLY_TUPLE \
00397 )
00398 
00399 #define HeapTupleHeaderClearHeapOnly(tup) \
00400 ( \
00401   (tup)->t_infomask2 &= ~HEAP_ONLY_TUPLE \
00402 )
00403 
00404 #define HeapTupleHeaderHasMatch(tup) \
00405 ( \
00406   (tup)->t_infomask2 & HEAP_TUPLE_HAS_MATCH \
00407 )
00408 
00409 #define HeapTupleHeaderSetMatch(tup) \
00410 ( \
00411   (tup)->t_infomask2 |= HEAP_TUPLE_HAS_MATCH \
00412 )
00413 
00414 #define HeapTupleHeaderClearMatch(tup) \
00415 ( \
00416   (tup)->t_infomask2 &= ~HEAP_TUPLE_HAS_MATCH \
00417 )
00418 
00419 #define HeapTupleHeaderGetNatts(tup) \
00420     ((tup)->t_infomask2 & HEAP_NATTS_MASK)
00421 
00422 #define HeapTupleHeaderSetNatts(tup, natts) \
00423 ( \
00424     (tup)->t_infomask2 = ((tup)->t_infomask2 & ~HEAP_NATTS_MASK) | (natts) \
00425 )
00426 
00427 
00428 /*
00429  * BITMAPLEN(NATTS) -
00430  *      Computes size of null bitmap given number of data columns.
00431  */
00432 #define BITMAPLEN(NATTS)    (((int)(NATTS) + 7) / 8)
00433 
00434 /*
00435  * MaxHeapTupleSize is the maximum allowed size of a heap tuple, including
00436  * header and MAXALIGN alignment padding.  Basically it's BLCKSZ minus the
00437  * other stuff that has to be on a disk page.  Since heap pages use no
00438  * "special space", there's no deduction for that.
00439  *
00440  * NOTE: we allow for the ItemId that must point to the tuple, ensuring that
00441  * an otherwise-empty page can indeed hold a tuple of this size.  Because
00442  * ItemIds and tuples have different alignment requirements, don't assume that
00443  * you can, say, fit 2 tuples of size MaxHeapTupleSize/2 on the same page.
00444  */
00445 #define MaxHeapTupleSize  (BLCKSZ - MAXALIGN(SizeOfPageHeaderData + sizeof(ItemIdData)))
00446 
00447 /*
00448  * MaxHeapTuplesPerPage is an upper bound on the number of tuples that can
00449  * fit on one heap page.  (Note that indexes could have more, because they
00450  * use a smaller tuple header.)  We arrive at the divisor because each tuple
00451  * must be maxaligned, and it must have an associated item pointer.
00452  *
00453  * Note: with HOT, there could theoretically be more line pointers (not actual
00454  * tuples) than this on a heap page.  However we constrain the number of line
00455  * pointers to this anyway, to avoid excessive line-pointer bloat and not
00456  * require increases in the size of work arrays.
00457  */
00458 #define MaxHeapTuplesPerPage    \
00459     ((int) ((BLCKSZ - SizeOfPageHeaderData) / \
00460             (MAXALIGN(offsetof(HeapTupleHeaderData, t_bits)) + sizeof(ItemIdData))))
00461 
00462 /*
00463  * MaxAttrSize is a somewhat arbitrary upper limit on the declared size of
00464  * data fields of char(n) and similar types.  It need not have anything
00465  * directly to do with the *actual* upper limit of varlena values, which
00466  * is currently 1Gb (see TOAST structures in postgres.h).  I've set it
00467  * at 10Mb which seems like a reasonable number --- tgl 8/6/00.
00468  */
00469 #define MaxAttrSize     (10 * 1024 * 1024)
00470 
00471 
00472 /*
00473  * MinimalTuple is an alternative representation that is used for transient
00474  * tuples inside the executor, in places where transaction status information
00475  * is not required, the tuple rowtype is known, and shaving off a few bytes
00476  * is worthwhile because we need to store many tuples.  The representation
00477  * is chosen so that tuple access routines can work with either full or
00478  * minimal tuples via a HeapTupleData pointer structure.  The access routines
00479  * see no difference, except that they must not access the transaction status
00480  * or t_ctid fields because those aren't there.
00481  *
00482  * For the most part, MinimalTuples should be accessed via TupleTableSlot
00483  * routines.  These routines will prevent access to the "system columns"
00484  * and thereby prevent accidental use of the nonexistent fields.
00485  *
00486  * MinimalTupleData contains a length word, some padding, and fields matching
00487  * HeapTupleHeaderData beginning with t_infomask2. The padding is chosen so
00488  * that offsetof(t_infomask2) is the same modulo MAXIMUM_ALIGNOF in both
00489  * structs.   This makes data alignment rules equivalent in both cases.
00490  *
00491  * When a minimal tuple is accessed via a HeapTupleData pointer, t_data is
00492  * set to point MINIMAL_TUPLE_OFFSET bytes before the actual start of the
00493  * minimal tuple --- that is, where a full tuple matching the minimal tuple's
00494  * data would start.  This trick is what makes the structs seem equivalent.
00495  *
00496  * Note that t_hoff is computed the same as in a full tuple, hence it includes
00497  * the MINIMAL_TUPLE_OFFSET distance.  t_len does not include that, however.
00498  *
00499  * MINIMAL_TUPLE_DATA_OFFSET is the offset to the first useful (non-pad) data
00500  * other than the length word.  tuplesort.c and tuplestore.c use this to avoid
00501  * writing the padding to disk.
00502  */
00503 #define MINIMAL_TUPLE_OFFSET \
00504     ((offsetof(HeapTupleHeaderData, t_infomask2) - sizeof(uint32)) / MAXIMUM_ALIGNOF * MAXIMUM_ALIGNOF)
00505 #define MINIMAL_TUPLE_PADDING \
00506     ((offsetof(HeapTupleHeaderData, t_infomask2) - sizeof(uint32)) % MAXIMUM_ALIGNOF)
00507 #define MINIMAL_TUPLE_DATA_OFFSET \
00508     offsetof(MinimalTupleData, t_infomask2)
00509 
00510 struct MinimalTupleData
00511 {
00512     uint32      t_len;          /* actual length of minimal tuple */
00513 
00514     char        mt_padding[MINIMAL_TUPLE_PADDING];
00515 
00516     /* Fields below here must match HeapTupleHeaderData! */
00517 
00518     uint16      t_infomask2;    /* number of attributes + various flags */
00519 
00520     uint16      t_infomask;     /* various flag bits, see below */
00521 
00522     uint8       t_hoff;         /* sizeof header incl. bitmap, padding */
00523 
00524     /* ^ - 23 bytes - ^ */
00525 
00526     bits8       t_bits[1];      /* bitmap of NULLs -- VARIABLE LENGTH */
00527 
00528     /* MORE DATA FOLLOWS AT END OF STRUCT */
00529 };
00530 /* typedef appears in htup.h */
00531 
00532 
00533 /*
00534  * GETSTRUCT - given a HeapTuple pointer, return address of the user data
00535  */
00536 #define GETSTRUCT(TUP) ((char *) ((TUP)->t_data) + (TUP)->t_data->t_hoff)
00537 
00538 /*
00539  * Accessor macros to be used with HeapTuple pointers.
00540  */
00541 
00542 #define HeapTupleHasNulls(tuple) \
00543         (((tuple)->t_data->t_infomask & HEAP_HASNULL) != 0)
00544 
00545 #define HeapTupleNoNulls(tuple) \
00546         (!((tuple)->t_data->t_infomask & HEAP_HASNULL))
00547 
00548 #define HeapTupleHasVarWidth(tuple) \
00549         (((tuple)->t_data->t_infomask & HEAP_HASVARWIDTH) != 0)
00550 
00551 #define HeapTupleAllFixed(tuple) \
00552         (!((tuple)->t_data->t_infomask & HEAP_HASVARWIDTH))
00553 
00554 #define HeapTupleHasExternal(tuple) \
00555         (((tuple)->t_data->t_infomask & HEAP_HASEXTERNAL) != 0)
00556 
00557 #define HeapTupleIsHotUpdated(tuple) \
00558         HeapTupleHeaderIsHotUpdated((tuple)->t_data)
00559 
00560 #define HeapTupleSetHotUpdated(tuple) \
00561         HeapTupleHeaderSetHotUpdated((tuple)->t_data)
00562 
00563 #define HeapTupleClearHotUpdated(tuple) \
00564         HeapTupleHeaderClearHotUpdated((tuple)->t_data)
00565 
00566 #define HeapTupleIsHeapOnly(tuple) \
00567         HeapTupleHeaderIsHeapOnly((tuple)->t_data)
00568 
00569 #define HeapTupleSetHeapOnly(tuple) \
00570         HeapTupleHeaderSetHeapOnly((tuple)->t_data)
00571 
00572 #define HeapTupleClearHeapOnly(tuple) \
00573         HeapTupleHeaderClearHeapOnly((tuple)->t_data)
00574 
00575 #define HeapTupleGetOid(tuple) \
00576         HeapTupleHeaderGetOid((tuple)->t_data)
00577 
00578 #define HeapTupleSetOid(tuple, oid) \
00579         HeapTupleHeaderSetOid((tuple)->t_data, (oid))
00580 
00581 
00582 /* ----------------
00583  *      fastgetattr
00584  *
00585  *      Fetch a user attribute's value as a Datum (might be either a
00586  *      value, or a pointer into the data area of the tuple).
00587  *
00588  *      This must not be used when a system attribute might be requested.
00589  *      Furthermore, the passed attnum MUST be valid.  Use heap_getattr()
00590  *      instead, if in doubt.
00591  *
00592  *      This gets called many times, so we macro the cacheable and NULL
00593  *      lookups, and call nocachegetattr() for the rest.
00594  * ----------------
00595  */
00596 
00597 #if !defined(DISABLE_COMPLEX_MACRO)
00598 
00599 #define fastgetattr(tup, attnum, tupleDesc, isnull)                 \
00600 (                                                                   \
00601     AssertMacro((attnum) > 0),                                      \
00602     (*(isnull) = false),                                            \
00603     HeapTupleNoNulls(tup) ?                                         \
00604     (                                                               \
00605         (tupleDesc)->attrs[(attnum)-1]->attcacheoff >= 0 ?          \
00606         (                                                           \
00607             fetchatt((tupleDesc)->attrs[(attnum)-1],                \
00608                 (char *) (tup)->t_data + (tup)->t_data->t_hoff +    \
00609                     (tupleDesc)->attrs[(attnum)-1]->attcacheoff)    \
00610         )                                                           \
00611         :                                                           \
00612             nocachegetattr((tup), (attnum), (tupleDesc))            \
00613     )                                                               \
00614     :                                                               \
00615     (                                                               \
00616         att_isnull((attnum)-1, (tup)->t_data->t_bits) ?             \
00617         (                                                           \
00618             (*(isnull) = true),                                     \
00619             (Datum)NULL                                             \
00620         )                                                           \
00621         :                                                           \
00622         (                                                           \
00623             nocachegetattr((tup), (attnum), (tupleDesc))            \
00624         )                                                           \
00625     )                                                               \
00626 )
00627 #else                           /* defined(DISABLE_COMPLEX_MACRO) */
00628 
00629 extern Datum fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc,
00630             bool *isnull);
00631 #endif   /* defined(DISABLE_COMPLEX_MACRO) */
00632 
00633 
00634 /* ----------------
00635  *      heap_getattr
00636  *
00637  *      Extract an attribute of a heap tuple and return it as a Datum.
00638  *      This works for either system or user attributes.  The given attnum
00639  *      is properly range-checked.
00640  *
00641  *      If the field in question has a NULL value, we return a zero Datum
00642  *      and set *isnull == true.  Otherwise, we set *isnull == false.
00643  *
00644  *      <tup> is the pointer to the heap tuple.  <attnum> is the attribute
00645  *      number of the column (field) caller wants.  <tupleDesc> is a
00646  *      pointer to the structure describing the row and all its fields.
00647  * ----------------
00648  */
00649 #define heap_getattr(tup, attnum, tupleDesc, isnull) \
00650     ( \
00651         ((attnum) > 0) ? \
00652         ( \
00653             ((attnum) > (int) HeapTupleHeaderGetNatts((tup)->t_data)) ? \
00654             ( \
00655                 (*(isnull) = true), \
00656                 (Datum)NULL \
00657             ) \
00658             : \
00659                 fastgetattr((tup), (attnum), (tupleDesc), (isnull)) \
00660         ) \
00661         : \
00662             heap_getsysattr((tup), (attnum), (tupleDesc), (isnull)) \
00663     )
00664 
00665 
00666 /* prototypes for functions in common/heaptuple.c */
00667 extern Size heap_compute_data_size(TupleDesc tupleDesc,
00668                        Datum *values, bool *isnull);
00669 extern void heap_fill_tuple(TupleDesc tupleDesc,
00670                 Datum *values, bool *isnull,
00671                 char *data, Size data_size,
00672                 uint16 *infomask, bits8 *bit);
00673 extern bool heap_attisnull(HeapTuple tup, int attnum);
00674 extern Datum nocachegetattr(HeapTuple tup, int attnum,
00675                TupleDesc att);
00676 extern Datum heap_getsysattr(HeapTuple tup, int attnum, TupleDesc tupleDesc,
00677                 bool *isnull);
00678 extern HeapTuple heap_copytuple(HeapTuple tuple);
00679 extern void heap_copytuple_with_tuple(HeapTuple src, HeapTuple dest);
00680 extern HeapTuple heap_form_tuple(TupleDesc tupleDescriptor,
00681                 Datum *values, bool *isnull);
00682 extern HeapTuple heap_modify_tuple(HeapTuple tuple,
00683                   TupleDesc tupleDesc,
00684                   Datum *replValues,
00685                   bool *replIsnull,
00686                   bool *doReplace);
00687 extern void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc,
00688                   Datum *values, bool *isnull);
00689 
00690 /* these three are deprecated versions of the three above: */
00691 extern HeapTuple heap_formtuple(TupleDesc tupleDescriptor,
00692                Datum *values, char *nulls);
00693 extern HeapTuple heap_modifytuple(HeapTuple tuple,
00694                  TupleDesc tupleDesc,
00695                  Datum *replValues,
00696                  char *replNulls,
00697                  char *replActions);
00698 extern void heap_deformtuple(HeapTuple tuple, TupleDesc tupleDesc,
00699                  Datum *values, char *nulls);
00700 extern void heap_freetuple(HeapTuple htup);
00701 extern MinimalTuple heap_form_minimal_tuple(TupleDesc tupleDescriptor,
00702                         Datum *values, bool *isnull);
00703 extern void heap_free_minimal_tuple(MinimalTuple mtup);
00704 extern MinimalTuple heap_copy_minimal_tuple(MinimalTuple mtup);
00705 extern HeapTuple heap_tuple_from_minimal_tuple(MinimalTuple mtup);
00706 extern MinimalTuple minimal_tuple_from_heap_tuple(HeapTuple htup);
00707 
00708 #endif   /* HTUP_DETAILS_H */