LCOV - differential code coverage report
Current view: top level - src/include/access - htup_details.h (source / functions) Coverage Total Hit UBC CBC
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 96.9 % 196 190 6 190
Current Date: 2026-11-28 16:48:44 +0900 Functions: 98.4 % 61 60 1 60
Baseline: lcov-20261128-baseline Branches: 79.4 % 68 54 14 54
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 32 32 32
(360..) days: 96.3 % 164 158 6 158
Function coverage date bins:
(7,30] days: 100.0 % 7 7 7
(360..) days: 98.1 % 54 53 1 53
Branch coverage date bins:
(7,30] days: 83.3 % 18 15 3 15
(360..) days: 78.0 % 50 39 11 39

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * htup_details.h
                                  4                 :                :  *    POSTGRES heap tuple header definitions.
                                  5                 :                :  *
                                  6                 :                :  *
                                  7                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                  8                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                  9                 :                :  *
                                 10                 :                :  * src/include/access/htup_details.h
                                 11                 :                :  *
                                 12                 :                :  *-------------------------------------------------------------------------
                                 13                 :                :  */
                                 14                 :                : #ifndef HTUP_DETAILS_H
                                 15                 :                : #define HTUP_DETAILS_H
                                 16                 :                : 
                                 17                 :                : #include "access/htup.h"
                                 18                 :                : #include "access/transam.h"
                                 19                 :                : #include "access/tupdesc.h"
                                 20                 :                : #include "access/tupmacs.h"
                                 21                 :                : #include "storage/bufpage.h"
                                 22                 :                : #include "varatt.h"
                                 23                 :                : 
                                 24                 :                : /*
                                 25                 :                :  * MaxTupleAttributeNumber limits the number of (user) columns in a tuple.
                                 26                 :                :  * The key limit on this value is that the size of the fixed overhead for
                                 27                 :                :  * a tuple, plus the size of the null-values bitmap (at 1 bit per column),
                                 28                 :                :  * plus MAXALIGN alignment, must fit into t_hoff which is uint8.  On most
                                 29                 :                :  * machines the upper limit without making t_hoff wider would be a little
                                 30                 :                :  * over 1700.  We use round numbers here and for MaxHeapAttributeNumber
                                 31                 :                :  * so that alterations in HeapTupleHeaderData layout won't change the
                                 32                 :                :  * supported max number of columns.
                                 33                 :                :  */
                                 34                 :                : #define MaxTupleAttributeNumber 1664    /* 8 * 208 */
                                 35                 :                : 
                                 36                 :                : /*
                                 37                 :                :  * MaxHeapAttributeNumber limits the number of (user) columns in a table.
                                 38                 :                :  * This should be somewhat less than MaxTupleAttributeNumber.  It must be
                                 39                 :                :  * at least one less, else we will fail to do UPDATEs on a maximal-width
                                 40                 :                :  * table (because UPDATE has to form working tuples that include CTID).
                                 41                 :                :  * In practice we want some additional daylight so that we can gracefully
                                 42                 :                :  * support operations that add hidden "resjunk" columns, for example
                                 43                 :                :  * SELECT * FROM wide_table ORDER BY foo, bar, baz.
                                 44                 :                :  * In any case, depending on column data types you will likely be running
                                 45                 :                :  * into the disk-block-based limit on overall tuple size if you have more
                                 46                 :                :  * than a thousand or so columns.  TOAST won't help.
                                 47                 :                :  */
                                 48                 :                : #define MaxHeapAttributeNumber  1600    /* 8 * 200 */
                                 49                 :                : 
                                 50                 :                : /*
                                 51                 :                :  * Heap tuple header.  To avoid wasting space, the fields should be
                                 52                 :                :  * laid out in such a way as to avoid structure padding.
                                 53                 :                :  *
                                 54                 :                :  * Datums of composite types (row types) share the same general structure
                                 55                 :                :  * as on-disk tuples, so that the same routines can be used to build and
                                 56                 :                :  * examine them.  However the requirements are slightly different: a Datum
                                 57                 :                :  * does not need any transaction visibility information, and it does need
                                 58                 :                :  * a length word and some embedded type information.  We can achieve this
                                 59                 :                :  * by overlaying the xmin/cmin/xmax/cmax/xvac fields of a heap tuple
                                 60                 :                :  * with the fields needed in the Datum case.  Typically, all tuples built
                                 61                 :                :  * in-memory will be initialized with the Datum fields; but when a tuple is
                                 62                 :                :  * about to be inserted in a table, the transaction fields will be filled,
                                 63                 :                :  * overwriting the datum fields.
                                 64                 :                :  *
                                 65                 :                :  * The overall structure of a heap tuple looks like:
                                 66                 :                :  *          fixed fields (HeapTupleHeaderData struct)
                                 67                 :                :  *          nulls bitmap (if HEAP_HASNULL is set in t_infomask)
                                 68                 :                :  *          alignment padding (as needed to make user data MAXALIGN'd)
                                 69                 :                :  *          object ID (if HEAP_HASOID_OLD is set in t_infomask, not created
                                 70                 :                :  *          anymore)
                                 71                 :                :  *          user data fields
                                 72                 :                :  *
                                 73                 :                :  * We store five "virtual" fields Xmin, Cmin, Xmax, Cmax, and Xvac in three
                                 74                 :                :  * physical fields.  Xmin and Xmax are always really stored, but Cmin, Cmax
                                 75                 :                :  * and Xvac share a field.  This works because we know that Cmin and Cmax
                                 76                 :                :  * are only interesting for the lifetime of the inserting and deleting
                                 77                 :                :  * transaction respectively.  If a tuple is inserted and deleted in the same
                                 78                 :                :  * transaction, we store a "combo" command id that can be mapped to the real
                                 79                 :                :  * cmin and cmax, but only by use of local state within the originating
                                 80                 :                :  * backend.  See combocid.c for more details.  Meanwhile, Xvac is only set by
                                 81                 :                :  * old-style VACUUM FULL, which does not have any command sub-structure and so
                                 82                 :                :  * does not need either Cmin or Cmax.  (This requires that old-style VACUUM
                                 83                 :                :  * FULL never try to move a tuple whose Cmin or Cmax is still interesting,
                                 84                 :                :  * ie, an insert-in-progress or delete-in-progress tuple.)
                                 85                 :                :  *
                                 86                 :                :  * A word about t_ctid: whenever a new tuple is stored on disk, its t_ctid
                                 87                 :                :  * is initialized with its own TID (location).  If the tuple is ever updated,
                                 88                 :                :  * its t_ctid is changed to point to the replacement version of the tuple.  Or
                                 89                 :                :  * if the tuple is moved from one partition to another, due to an update of
                                 90                 :                :  * the partition key, t_ctid is set to a special value to indicate that
                                 91                 :                :  * (see ItemPointerSetMovedPartitions).  Thus, a tuple is the latest version
                                 92                 :                :  * of its row iff XMAX is invalid or
                                 93                 :                :  * t_ctid points to itself (in which case, if XMAX is valid, the tuple is
                                 94                 :                :  * either locked or deleted).  One can follow the chain of t_ctid links
                                 95                 :                :  * to find the newest version of the row, unless it was moved to a different
                                 96                 :                :  * partition.  Beware however that VACUUM might
                                 97                 :                :  * erase the pointed-to (newer) tuple before erasing the pointing (older)
                                 98                 :                :  * tuple.  Hence, when following a t_ctid link, it is necessary to check
                                 99                 :                :  * to see if the referenced slot is empty or contains an unrelated tuple.
                                100                 :                :  * Check that the referenced tuple has XMIN equal to the referencing tuple's
                                101                 :                :  * XMAX to verify that it is actually the descendant version and not an
                                102                 :                :  * unrelated tuple stored into a slot recently freed by VACUUM.  If either
                                103                 :                :  * check fails, one may assume that there is no live descendant version.
                                104                 :                :  *
                                105                 :                :  * t_ctid is sometimes used to store a speculative insertion token, instead
                                106                 :                :  * of a real TID.  A speculative token is set on a tuple that's being
                                107                 :                :  * inserted, until the inserter is sure that it wants to go ahead with the
                                108                 :                :  * insertion.  Hence a token should only be seen on a tuple with an XMAX
                                109                 :                :  * that's still in-progress, or invalid/aborted.  The token is replaced with
                                110                 :                :  * the tuple's real TID when the insertion is confirmed.  One should never
                                111                 :                :  * see a speculative insertion token while following a chain of t_ctid links,
                                112                 :                :  * because they are not used on updates, only insertions.
                                113                 :                :  *
                                114                 :                :  * Following the fixed header fields, the nulls bitmap is stored (beginning
                                115                 :                :  * at t_bits).  The bitmap is *not* stored if t_infomask shows that there
                                116                 :                :  * are no nulls in the tuple.  If an OID field is present (as indicated by
                                117                 :                :  * t_infomask), then it is stored just before the user data, which begins at
                                118                 :                :  * the offset shown by t_hoff.  Note that t_hoff must be a multiple of
                                119                 :                :  * MAXALIGN.
                                120                 :                :  */
                                121                 :                : 
                                122                 :                : typedef struct HeapTupleFields
                                123                 :                : {
                                124                 :                :     TransactionId t_xmin;       /* inserting xact ID */
                                125                 :                :     TransactionId t_xmax;       /* deleting or locking xact ID */
                                126                 :                : 
                                127                 :                :     union
                                128                 :                :     {
                                129                 :                :         CommandId   t_cid;      /* inserting or deleting command ID, or both */
                                130                 :                :         TransactionId t_xvac;   /* old-style VACUUM FULL xact ID */
                                131                 :                :     }           t_field3;
                                132                 :                : } HeapTupleFields;
                                133                 :                : 
                                134                 :                : typedef struct DatumTupleFields
                                135                 :                : {
                                136                 :                :     int32       datum_len_;     /* varlena header (do not touch directly!) */
                                137                 :                : 
                                138                 :                :     /* ---
                                139                 :                :      * If datum_typeid == RECORDOID, datum_typmod is either the identifier of
                                140                 :                :      * the blessed TupleDesc or -1 if not blessed.
                                141                 :                :      *
                                142                 :                :      * If the tuple is of an identified row type, but has been created before
                                143                 :                :      * versioning was introduced, -1 is stored.
                                144                 :                :      *
                                145                 :                :      * If the tuple is versioned (see HeapTupleHeaderIsVersionedDatum()):
                                146                 :                :      * - datum_typmod contains the lower 32 bits of the version
                                147                 :                :      * - the high 32 bits of the version are stored in t_ctid
                                148                 :                :      *
                                149                 :                :      * For more about versioning see src/backend/catalog/README.attver.md
                                150                 :                :      * ---
                                151                 :                :      */
                                152                 :                :     int32       datum_typmod;
                                153                 :                : 
                                154                 :                :     Oid         datum_typeid;   /* composite type OID, or RECORDOID */
                                155                 :                : 
                                156                 :                :     /*
                                157                 :                :      * datum_typeid cannot be a domain over composite, only plain composite,
                                158                 :                :      * even if the datum is meant as a value of a domain-over-composite type.
                                159                 :                :      * This is in line with the general principle that CoerceToDomain does not
                                160                 :                :      * change the physical representation of the base type value.
                                161                 :                :      *
                                162                 :                :      * Note: field ordering is chosen with thought that Oid might someday
                                163                 :                :      * widen to 64 bits.
                                164                 :                :      */
                                165                 :                : } DatumTupleFields;
                                166                 :                : 
                                167                 :                : struct HeapTupleHeaderData
                                168                 :                : {
                                169                 :                :     union
                                170                 :                :     {
                                171                 :                :         HeapTupleFields t_heap;
                                172                 :                :         DatumTupleFields t_datum;
                                173                 :                :     }           t_choice;
                                174                 :                : 
                                175                 :                :     /*
                                176                 :                :      * Current TID of this or newer tuple.
                                177                 :                :      *
                                178                 :                :      * During speculative insertions, a speculative insertion token is stored
                                179                 :                :      * instead.
                                180                 :                :      *
                                181                 :                :      * If the tuple is a versioned datum tuple, ip_posid is set to
                                182                 :                :      * VersionedDatumOffsetNumber and the high 32 bits of the version number
                                183                 :                :      * are stored in ip_blkid.  The low 32 bits are stored in datum_typmod,
                                184                 :                :      * see also the comment there.
                                185                 :                :      */
                                186                 :                :     ItemPointerData t_ctid;
                                187                 :                : 
                                188                 :                :     /* Fields below here must match MinimalTupleData! */
                                189                 :                : 
                                190                 :                : #define FIELDNO_HEAPTUPLEHEADERDATA_INFOMASK2 2
                                191                 :                :     uint16      t_infomask2;    /* number of attributes + various flags */
                                192                 :                : 
                                193                 :                : #define FIELDNO_HEAPTUPLEHEADERDATA_INFOMASK 3
                                194                 :                :     uint16      t_infomask;     /* various flag bits, see below */
                                195                 :                : 
                                196                 :                : #define FIELDNO_HEAPTUPLEHEADERDATA_HOFF 4
                                197                 :                :     uint8       t_hoff;         /* sizeof header incl. bitmap, padding */
                                198                 :                : 
                                199                 :                :     /* ^ - 23 bytes - ^ */
                                200                 :                : 
                                201                 :                : #define FIELDNO_HEAPTUPLEHEADERDATA_BITS 5
                                202                 :                :     uint8       t_bits[FLEXIBLE_ARRAY_MEMBER];  /* bitmap of NULLs */
                                203                 :                : 
                                204                 :                :     /* MORE DATA FOLLOWS AT END OF STRUCT */
                                205                 :                : };
                                206                 :                : 
                                207                 :                : /* typedef appears in htup.h */
                                208                 :                : 
                                209                 :                : #define SizeofHeapTupleHeader offsetof(HeapTupleHeaderData, t_bits)
                                210                 :                : 
                                211                 :                : /*
                                212                 :                :  * information stored in t_infomask:
                                213                 :                :  */
                                214                 :                : #define HEAP_HASNULL            0x0001  /* has null attribute(s) */
                                215                 :                : #define HEAP_HASVARWIDTH        0x0002  /* has variable-width attribute(s) */
                                216                 :                : #define HEAP_HASEXTERNAL        0x0004  /* has external stored attribute(s) */
                                217                 :                : #define HEAP_HASOID_OLD         0x0008  /* has an object-id field */
                                218                 :                : #define HEAP_XMAX_KEYSHR_LOCK   0x0010  /* xmax is a key-shared locker */
                                219                 :                : #define HEAP_COMBOCID           0x0020  /* t_cid is a combo CID */
                                220                 :                : #define HEAP_XMAX_EXCL_LOCK     0x0040  /* xmax is exclusive locker */
                                221                 :                : #define HEAP_XMAX_LOCK_ONLY     0x0080  /* xmax, if valid, is only a locker */
                                222                 :                : 
                                223                 :                :  /* xmax is a shared locker */
                                224                 :                : #define HEAP_XMAX_SHR_LOCK  (HEAP_XMAX_EXCL_LOCK | HEAP_XMAX_KEYSHR_LOCK)
                                225                 :                : 
                                226                 :                : #define HEAP_LOCK_MASK  (HEAP_XMAX_SHR_LOCK | HEAP_XMAX_EXCL_LOCK | \
                                227                 :                :                          HEAP_XMAX_KEYSHR_LOCK)
                                228                 :                : #define HEAP_XMIN_COMMITTED     0x0100  /* t_xmin committed */
                                229                 :                : #define HEAP_XMIN_INVALID       0x0200  /* t_xmin invalid/aborted */
                                230                 :                : #define HEAP_XMIN_FROZEN        (HEAP_XMIN_COMMITTED|HEAP_XMIN_INVALID)
                                231                 :                : #define HEAP_XMAX_COMMITTED     0x0400  /* t_xmax committed */
                                232                 :                : #define HEAP_XMAX_INVALID       0x0800  /* t_xmax invalid/aborted */
                                233                 :                : #define HEAP_XMAX_IS_MULTI      0x1000  /* t_xmax is a MultiXactId */
                                234                 :                : #define HEAP_UPDATED            0x2000  /* this is UPDATEd version of row */
                                235                 :                : #define HEAP_MOVED_OFF          0x4000  /* moved to another place by pre-9.0
                                236                 :                :                                          * VACUUM FULL; kept for binary
                                237                 :                :                                          * upgrade support */
                                238                 :                : #define HEAP_MOVED_IN           0x8000  /* moved from another place by pre-9.0
                                239                 :                :                                          * VACUUM FULL; kept for binary
                                240                 :                :                                          * upgrade support */
                                241                 :                : #define HEAP_MOVED (HEAP_MOVED_OFF | HEAP_MOVED_IN)
                                242                 :                : 
                                243                 :                : #define HEAP_XACT_MASK          0xFFF0  /* visibility-related bits */
                                244                 :                : 
                                245                 :                : /*
                                246                 :                :  * Marks a tuple as a composite datum, i.e. t_choice.t_datum is valid.  This
                                247                 :                :  * combination of bits never occurs in a heap tuple in a page (xmax can't be
                                248                 :                :  * committed and invalid), and as both bits are part of HEAP_XACT_MASK,
                                249                 :                :  * inserting the tuple into a heap clears it.
                                250                 :                :  */
                                251                 :                : #define HEAP_IS_DATUM           (HEAP_XMAX_COMMITTED | HEAP_XMAX_INVALID)
                                252                 :                : 
                                253                 :                : /*
                                254                 :                :  * A tuple is only locked (i.e. not updated by its Xmax) if the
                                255                 :                :  * HEAP_XMAX_LOCK_ONLY bit is set; or, for pg_upgrade's sake, if the Xmax is
                                256                 :                :  * not a multi and the EXCL_LOCK bit is set.
                                257                 :                :  *
                                258                 :                :  * See also HeapTupleHeaderIsOnlyLocked, which also checks for a possible
                                259                 :                :  * aborted updater transaction.
                                260                 :                :  */
                                261                 :                : static inline bool
  674 peter@eisentraut.org      262                 :CBC    27215976 : HEAP_XMAX_IS_LOCKED_ONLY(uint16 infomask)
                                263                 :                : {
                                264         [ +  + ]:       49857328 :     return (infomask & HEAP_XMAX_LOCK_ONLY) ||
                                265         [ -  + ]:       22641352 :         (infomask & (HEAP_XMAX_IS_MULTI | HEAP_LOCK_MASK)) == HEAP_XMAX_EXCL_LOCK;
                                266                 :                : }
                                267                 :                : 
                                268                 :                : /*
                                269                 :                :  * A tuple that has HEAP_XMAX_IS_MULTI and HEAP_XMAX_LOCK_ONLY but neither of
                                270                 :                :  * HEAP_XMAX_EXCL_LOCK and HEAP_XMAX_KEYSHR_LOCK must come from a tuple that was
                                271                 :                :  * share-locked in 9.2 or earlier and then pg_upgrade'd.
                                272                 :                :  *
                                273                 :                :  * In 9.2 and prior, HEAP_XMAX_IS_MULTI was only set when there were multiple
                                274                 :                :  * FOR SHARE lockers of that tuple.  That set HEAP_XMAX_LOCK_ONLY (with a
                                275                 :                :  * different name back then) but neither of HEAP_XMAX_EXCL_LOCK and
                                276                 :                :  * HEAP_XMAX_KEYSHR_LOCK.  That combination is no longer possible in 9.3 and
                                277                 :                :  * up, so if we see that combination we know for certain that the tuple was
                                278                 :                :  * locked in an earlier release; since all such lockers are gone (they cannot
                                279                 :                :  * survive through pg_upgrade), such tuples can safely be considered not
                                280                 :                :  * locked.
                                281                 :                :  *
                                282                 :                :  * We must not resolve such multixacts locally, because the result would be
                                283                 :                :  * bogus, regardless of where they stand with respect to the current valid
                                284                 :                :  * multixact range.
                                285                 :                :  */
                                286                 :                : static inline bool
                                287                 :         188680 : HEAP_LOCKED_UPGRADED(uint16 infomask)
                                288                 :                : {
                                289                 :                :     return
                                290                 :         341092 :         (infomask & HEAP_XMAX_IS_MULTI) != 0 &&
                                291   [ +  +  +  + ]:         336698 :         (infomask & HEAP_XMAX_LOCK_ONLY) != 0 &&
                                292         [ -  + ]:         148018 :         (infomask & (HEAP_XMAX_EXCL_LOCK | HEAP_XMAX_KEYSHR_LOCK)) == 0;
                                293                 :                : }
                                294                 :                : 
                                295                 :                : /*
                                296                 :                :  * Use these to test whether a particular lock is applied to a tuple
                                297                 :                :  */
                                298                 :                : static inline bool
  402 nathan@postgresql.or      299                 :         100044 : HEAP_XMAX_IS_SHR_LOCKED(uint16 infomask)
                                300                 :                : {
  674 peter@eisentraut.org      301                 :         100044 :     return (infomask & HEAP_LOCK_MASK) == HEAP_XMAX_SHR_LOCK;
                                302                 :                : }
                                303                 :                : 
                                304                 :                : static inline bool
  402 nathan@postgresql.or      305                 :         106848 : HEAP_XMAX_IS_EXCL_LOCKED(uint16 infomask)
                                306                 :                : {
  674 peter@eisentraut.org      307                 :         106848 :     return (infomask & HEAP_LOCK_MASK) == HEAP_XMAX_EXCL_LOCK;
                                308                 :                : }
                                309                 :                : 
                                310                 :                : static inline bool
  402 nathan@postgresql.or      311                 :         705904 : HEAP_XMAX_IS_KEYSHR_LOCKED(uint16 infomask)
                                312                 :                : {
  674 peter@eisentraut.org      313                 :         705904 :     return (infomask & HEAP_LOCK_MASK) == HEAP_XMAX_KEYSHR_LOCK;
                                314                 :                : }
                                315                 :                : 
                                316                 :                : /* turn these all off when Xmax is to change */
                                317                 :                : #define HEAP_XMAX_BITS (HEAP_XMAX_COMMITTED | HEAP_XMAX_INVALID | \
                                318                 :                :                         HEAP_XMAX_IS_MULTI | HEAP_LOCK_MASK | HEAP_XMAX_LOCK_ONLY)
                                319                 :                : 
                                320                 :                : /*
                                321                 :                :  * information stored in t_infomask2:
                                322                 :                :  */
                                323                 :                : #define HEAP_NATTS_MASK         0x07FF  /* 11 bits for number of attributes */
                                324                 :                : /* bits 0x1800 are available */
                                325                 :                : #define HEAP_KEYS_UPDATED       0x2000  /* tuple was updated and key cols
                                326                 :                :                                          * modified, or tuple deleted */
                                327                 :                : #define HEAP_HOT_UPDATED        0x4000  /* tuple was HOT-updated */
                                328                 :                : #define HEAP_ONLY_TUPLE         0x8000  /* this is heap-only tuple */
                                329                 :                : 
                                330                 :                : #define HEAP2_XACT_MASK         0xE000  /* visibility-related bits */
                                331                 :                : 
                                332                 :                : /*
                                333                 :                :  * HEAP_TUPLE_HAS_MATCH is a temporary flag used during hash joins.  It is
                                334                 :                :  * only used in tuples that are in the hash table, and those don't need
                                335                 :                :  * any visibility information, so we can overlay it on a visibility flag
                                336                 :                :  * instead of using up a dedicated bit.
                                337                 :                :  */
                                338                 :                : #define HEAP_TUPLE_HAS_MATCH    HEAP_ONLY_TUPLE /* tuple has a join match */
                                339                 :                : 
                                340                 :                : /*
                                341                 :                :  * HeapTupleHeader accessor functions
                                342                 :                :  */
                                343                 :                : 
                                344                 :                : static bool HeapTupleHeaderXminFrozen(const HeapTupleHeaderData *tup);
                                345                 :                : 
                                346                 :                : /*
                                347                 :                :  * HeapTupleHeaderGetRawXmin returns the "raw" xmin field, which is the xid
                                348                 :                :  * originally used to insert the tuple.  However, the tuple might actually
                                349                 :                :  * be frozen (via HeapTupleHeaderSetXminFrozen) in which case the tuple's xmin
                                350                 :                :  * is visible to every snapshot.  Prior to PostgreSQL 9.4, we actually changed
                                351                 :                :  * the xmin to FrozenTransactionId, and that value may still be encountered
                                352                 :                :  * on disk.
                                353                 :                :  */
                                354                 :                : static inline TransactionId
                                355                 :      223825815 : HeapTupleHeaderGetRawXmin(const HeapTupleHeaderData *tup)
                                356                 :                : {
                                357                 :      223825815 :     return tup->t_choice.t_heap.t_xmin;
                                358                 :                : }
                                359                 :                : 
                                360                 :                : static inline TransactionId
                                361                 :       80419617 : HeapTupleHeaderGetXmin(const HeapTupleHeaderData *tup)
                                362                 :                : {
                                363                 :       80419617 :     return HeapTupleHeaderXminFrozen(tup) ?
                                364         [ +  + ]:       80419617 :         FrozenTransactionId : HeapTupleHeaderGetRawXmin(tup);
                                365                 :                : }
                                366                 :                : 
                                367                 :                : static inline void
                                368                 :       18145627 : HeapTupleHeaderSetXmin(HeapTupleHeaderData *tup, TransactionId xid)
                                369                 :                : {
                                370                 :       18145627 :     tup->t_choice.t_heap.t_xmin = xid;
                                371                 :       18145627 : }
                                372                 :                : 
                                373                 :                : static inline bool
                                374                 :      187037131 : HeapTupleHeaderXminCommitted(const HeapTupleHeaderData *tup)
                                375                 :                : {
                                376                 :      187037131 :     return (tup->t_infomask & HEAP_XMIN_COMMITTED) != 0;
                                377                 :                : }
                                378                 :                : 
                                379                 :                : static inline bool
                                380                 :       49879848 : HeapTupleHeaderXminInvalid(const HeapTupleHeaderData *tup) \
                                381                 :                : {
                                382                 :       49879848 :     return (tup->t_infomask & (HEAP_XMIN_COMMITTED | HEAP_XMIN_INVALID)) ==
                                383                 :                :         HEAP_XMIN_INVALID;
                                384                 :                : }
                                385                 :                : 
                                386                 :                : static inline bool
                                387                 :      382807134 : HeapTupleHeaderXminFrozen(const HeapTupleHeaderData *tup)
                                388                 :                : {
                                389                 :      382807134 :     return (tup->t_infomask & HEAP_XMIN_FROZEN) == HEAP_XMIN_FROZEN;
                                390                 :                : }
                                391                 :                : 
                                392                 :                : static inline void
                                393                 :         104138 : HeapTupleHeaderSetXminFrozen(HeapTupleHeaderData *tup)
                                394                 :                : {
                                395         [ -  + ]:         104138 :     Assert(!HeapTupleHeaderXminInvalid(tup));
                                396                 :         104138 :     tup->t_infomask |= HEAP_XMIN_FROZEN;
                                397                 :         104138 : }
                                398                 :                : 
                                399                 :                : static inline TransactionId
                                400                 :       70493079 : HeapTupleHeaderGetRawXmax(const HeapTupleHeaderData *tup)
                                401                 :                : {
                                402                 :       70493079 :     return tup->t_choice.t_heap.t_xmax;
                                403                 :                : }
                                404                 :                : 
                                405                 :                : static inline void
                                406                 :       25469795 : HeapTupleHeaderSetXmax(HeapTupleHeaderData *tup, TransactionId xid)
                                407                 :                : {
                                408                 :       25469795 :     tup->t_choice.t_heap.t_xmax = xid;
                                409                 :       25469795 : }
                                410                 :                : 
                                411                 :                : #ifndef FRONTEND
                                412                 :                : /*
                                413                 :                :  * HeapTupleHeaderGetRawXmax gets you the raw Xmax field.  To find out the Xid
                                414                 :                :  * that updated a tuple, you might need to resolve the MultiXactId if certain
                                415                 :                :  * bits are set.  HeapTupleHeaderGetUpdateXid checks those bits and takes care
                                416                 :                :  * to resolve the MultiXactId if necessary.  This might involve multixact I/O,
                                417                 :                :  * so it should only be used if absolutely necessary.
                                418                 :                :  */
                                419                 :                : static inline TransactionId
                                420                 :       11540571 : HeapTupleHeaderGetUpdateXid(const HeapTupleHeaderData *tup)
                                421                 :                : {
                                422         [ +  + ]:       11540571 :     if (!((tup)->t_infomask & HEAP_XMAX_INVALID) &&
                                423         [ +  + ]:       10858995 :         ((tup)->t_infomask & HEAP_XMAX_IS_MULTI) &&
                                424         [ +  + ]:          76776 :         !((tup)->t_infomask & HEAP_XMAX_LOCK_ONLY))
                                425                 :          76736 :         return HeapTupleGetUpdateXid(tup);
                                426                 :                :     else
                                427                 :       11463835 :         return HeapTupleHeaderGetRawXmax(tup);
                                428                 :                : }
                                429                 :                : #endif                          /* FRONTEND */
                                430                 :                : 
                                431                 :                : /*
                                432                 :                :  * HeapTupleHeaderGetRawCommandId will give you what's in the header whether
                                433                 :                :  * it is useful or not.  Most code should use HeapTupleHeaderGetCmin or
                                434                 :                :  * HeapTupleHeaderGetCmax instead, but note that those Assert that you can
                                435                 :                :  * get a legitimate result, ie you are in the originating transaction!
                                436                 :                :  */
                                437                 :                : static inline CommandId
                                438                 :       19972675 : HeapTupleHeaderGetRawCommandId(const HeapTupleHeaderData *tup)
                                439                 :                : {
                                440                 :       19972675 :     return tup->t_choice.t_heap.t_field3.t_cid;
                                441                 :                : }
                                442                 :                : 
                                443                 :                : /* SetCmin is reasonably simple since we never need a combo CID */
                                444                 :                : static inline void
                                445                 :       18094012 : HeapTupleHeaderSetCmin(HeapTupleHeaderData *tup, CommandId cid)
                                446                 :                : {
                                447         [ -  + ]:       18094012 :     Assert(!(tup->t_infomask & HEAP_MOVED));
                                448                 :       18094012 :     tup->t_choice.t_heap.t_field3.t_cid = cid;
                                449                 :       18094012 :     tup->t_infomask &= ~HEAP_COMBOCID;
                                450                 :       18094012 : }
                                451                 :                : 
                                452                 :                : /* SetCmax must be used after HeapTupleHeaderAdjustCmax; see combocid.c */
                                453                 :                : static inline void
                                454                 :        6994001 : HeapTupleHeaderSetCmax(HeapTupleHeaderData *tup, CommandId cid, bool iscombo)
                                455                 :                : {
                                456         [ -  + ]:        6994001 :     Assert(!((tup)->t_infomask & HEAP_MOVED));
                                457                 :        6994001 :     tup->t_choice.t_heap.t_field3.t_cid = cid;
                                458         [ +  + ]:        6994001 :     if (iscombo)
                                459                 :         252140 :         tup->t_infomask |= HEAP_COMBOCID;
                                460                 :                :     else
                                461                 :        6741861 :         tup->t_infomask &= ~HEAP_COMBOCID;
                                462                 :        6994001 : }
                                463                 :                : 
                                464                 :                : static inline TransactionId
                                465                 :        9774171 : HeapTupleHeaderGetXvac(const HeapTupleHeaderData *tup)
                                466                 :                : {
                                467         [ -  + ]:        9774171 :     if (tup->t_infomask & HEAP_MOVED)
  674 peter@eisentraut.org      468                 :UBC           0 :         return tup->t_choice.t_heap.t_field3.t_xvac;
                                469                 :                :     else
  674 peter@eisentraut.org      470                 :CBC     9774171 :         return InvalidTransactionId;
                                471                 :                : }
                                472                 :                : 
                                473                 :                : static inline void
  674 peter@eisentraut.org      474                 :UBC           0 : HeapTupleHeaderSetXvac(HeapTupleHeaderData *tup, TransactionId xid)
                                475                 :                : {
                                476         [ #  # ]:              0 :     Assert(tup->t_infomask & HEAP_MOVED);
                                477                 :              0 :     tup->t_choice.t_heap.t_field3.t_xvac = xid;
                                478                 :              0 : }
                                479                 :                : 
                                480                 :                : StaticAssertDecl(MaxOffsetNumber < SpecTokenOffsetNumber,
                                481                 :                :                  "invalid speculative token constant");
                                482                 :                : 
                                483                 :                : static inline bool
  674 peter@eisentraut.org      484                 :CBC   229690037 : HeapTupleHeaderIsSpeculative(const HeapTupleHeaderData *tup)
                                485                 :                : {
                                486                 :      229690037 :     return ItemPointerGetOffsetNumberNoCheck(&tup->t_ctid) == SpecTokenOffsetNumber;
                                487                 :                : }
                                488                 :                : 
                                489                 :                : static inline BlockNumber
                                490                 :              2 : HeapTupleHeaderGetSpeculativeToken(const HeapTupleHeaderData *tup)
                                491                 :                : {
                                492         [ -  + ]:              2 :     Assert(HeapTupleHeaderIsSpeculative(tup));
                                493                 :              2 :     return ItemPointerGetBlockNumber(&tup->t_ctid);
                                494                 :                : }
                                495                 :                : 
                                496                 :                : static inline void
                                497                 :           2278 : HeapTupleHeaderSetSpeculativeToken(HeapTupleHeaderData *tup, BlockNumber token)
                                498                 :                : {
                                499                 :           2278 :     ItemPointerSet(&tup->t_ctid, token, SpecTokenOffsetNumber);
                                500                 :           2278 : }
                                501                 :                : 
                                502                 :                : static inline bool
                                503                 :         162597 : HeapTupleHeaderIndicatesMovedPartitions(const HeapTupleHeaderData *tup)
                                504                 :                : {
                                505                 :         162597 :     return ItemPointerIndicatesMovedPartitions(&tup->t_ctid);
                                506                 :                : }
                                507                 :                : 
                                508                 :                : static inline void
                                509                 :            795 : HeapTupleHeaderSetMovedPartitions(HeapTupleHeaderData *tup)
                                510                 :                : {
                                511                 :            795 :     ItemPointerSetMovedPartitions(&tup->t_ctid);
                                512                 :            795 : }
                                513                 :                : 
                                514                 :                : /*
                                515                 :                :  * Is the tuple a datum tuple, i.e. are the t_choice.t_datum fields valid?
                                516                 :                :  *
                                517                 :                :  * Datum tuples created before HEAP_IS_DATUM was introduced (e.g. pg_upgrade'd
                                518                 :                :  * ones, or ones from an older minor version) are not marked as such on-disk,
                                519                 :                :  * but DatumGetHeapTupleHeader() returns a marked copy whenever such a Datum is
                                520                 :                :  * converted to a HeapTupleHeader. DatumGetHeapTupleHeader() is expected to be
                                521                 :                :  * called at least once in the path towards tuple deformation.
                                522                 :                :  *
                                523                 :                :  * See also HeapTupleHeaderReallyIsDatum().
                                524                 :                :  */
                                525                 :                : static inline bool
   19 andres@anarazel.de        526                 :      235475485 : HeapTupleHeaderIsDatum(const HeapTupleHeaderData *tup)
                                527                 :                : {
                                528                 :      235475485 :     return (tup->t_infomask & HEAP_IS_DATUM) == HEAP_IS_DATUM;
                                529                 :                : }
                                530                 :                : 
                                531                 :                : static inline void
                                532                 :       20144588 : HeapTupleHeaderSetDatum(HeapTupleHeaderData *tup)
                                533                 :                : {
                                534                 :       20144588 :     tup->t_infomask |= HEAP_IS_DATUM;
                                535                 :       20144588 : }
                                536                 :                : 
                                537                 :                : static inline void
                                538                 :          47020 : HeapTupleHeaderClearDatum(HeapTupleHeaderData *tup)
                                539                 :                : {
                                540                 :                :     /* otherwise we might clear transactionally relevant bits */
                                541         [ -  + ]:          47020 :     Assert(HeapTupleHeaderIsDatum(tup));
                                542                 :          47020 :     tup->t_infomask &= ~HEAP_IS_DATUM;
                                543                 :          47020 : }
                                544                 :                : 
                                545                 :                : static inline uint32
  674 peter@eisentraut.org      546                 :        1753349 : HeapTupleHeaderGetDatumLength(const HeapTupleHeaderData *tup)
                                547                 :                : {
                                548                 :        1753349 :     return VARSIZE(tup);
                                549                 :                : }
                                550                 :                : 
                                551                 :                : static inline void
                                552                 :       20144544 : HeapTupleHeaderSetDatumLength(HeapTupleHeaderData *tup, uint32 len)
                                553                 :                : {
                                554                 :       20144544 :     SET_VARSIZE(tup, len);
                                555                 :       20144544 : }
                                556                 :                : 
                                557                 :                : static inline Oid
                                558                 :        8150843 : HeapTupleHeaderGetTypeId(const HeapTupleHeaderData *tup)
                                559                 :                : {
                                560                 :        8150843 :     return tup->t_choice.t_datum.datum_typeid;
                                561                 :                : }
                                562                 :                : 
                                563                 :                : static inline void
                                564                 :       20144544 : HeapTupleHeaderSetTypeId(HeapTupleHeaderData *tup, Oid datum_typeid)
                                565                 :                : {
                                566                 :       20144544 :     tup->t_choice.t_datum.datum_typeid = datum_typeid;
                                567                 :       20144544 : }
                                568                 :                : 
                                569                 :                : static inline int32
                                570                 :        2865177 : HeapTupleHeaderGetTypMod(const HeapTupleHeaderData *tup)
                                571                 :                : {
                                572                 :                :     /*
                                573                 :                :      * If the tuple is versioned (only happens for !RECORDOID), datum_typmod
                                574                 :                :      * carries part of the version. We don't want callers to have to care
                                575                 :                :      * about that, so we return the traditional -1 in that case.
                                576                 :                :      *
                                577                 :                :      * As this is a static inline, extensions compiled with the old definition
                                578                 :                :      * will return the version, but based on an inspection of extensions
                                579                 :                :      * that's ok. Almost all just call lookup_rowtype_tupdesc(), which ignores
                                580                 :                :      * typmod for !RECORDOID cases. The one other case (Citus) is never
                                581                 :                :      * generated by its own planner for !RECORDOID.
                                582                 :                :      */
   19 andres@anarazel.de        583         [ +  + ]:        2865177 :     if (HeapTupleHeaderGetTypeId(tup) != RECORDOID)
                                584                 :        1217509 :         return -1;
  674 peter@eisentraut.org      585                 :        1647668 :     return tup->t_choice.t_datum.datum_typmod;
                                586                 :                : }
                                587                 :                : 
                                588                 :                : static inline void
                                589                 :       15084242 : HeapTupleHeaderSetTypMod(HeapTupleHeaderData *tup, int32 typmod)
                                590                 :                : {
                                591                 :       15084242 :     tup->t_choice.t_datum.datum_typmod = typmod;
                                592                 :       15084242 : }
                                593                 :                : 
                                594                 :                : /*
                                595                 :                :  * Is this really a datum tuple?
                                596                 :                :  *
                                597                 :                :  * postgres_fdw has historically overwritten xmin/xmax/cmin/ctid, several
                                598                 :                :  * out-of-tree extensions have copied that pattern. That clears the length,
                                599                 :                :  * typmod, ... of a datum tuple. Because those extensions were written before
                                600                 :                :  * versioned datums were a thing, they are not calling
                                601                 :                :  * HeapTupleHeaderClearDatum(), resulting in the possibility of getting here
                                602                 :                :  * with partially overwritten version information.
                                603                 :                :  *
                                604                 :                :  * We can detect this by checking if the length of the datum has been zeroed
                                605                 :                :  * out. No real datum tuple can have a zero length, so this does not weaken
                                606                 :                :  * protections.
                                607                 :                :  *
                                608                 :                :  * Other FDWs overwrite just t_ctid, which makes the datum unversioned (a real
                                609                 :                :  * TID's ip_posid is never VersionedDatumOffsetNumber), which will cause
                                610                 :                :  * HeapTupleHeaderIsVersionedDatum() to return false.
                                611                 :                :  *
                                612                 :                :  * It's typically OK to not check a "falsely marked" datum tuple, because
                                613                 :                :  * other protections, e.g. locking of foreign tables against DDL, should
                                614                 :                :  * protect until the tuple is converted to a "real" datum tuple.
                                615                 :                :  *
                                616                 :                :  * NB: This should only be used when relying on the length/typeid/typmod. For
                                617                 :                :  * other cases, e.g. whether HEAP_IS_DATUM is to be cleared,
                                618                 :                :  * HeapTupleHeaderIsDatum() needs to be used.
                                619                 :                :  */
                                620                 :                : static inline bool
   19 andres@anarazel.de        621                 :        2963633 : HeapTupleHeaderReallyIsDatum(const HeapTupleHeaderData *tup)
                                622                 :                : {
                                623   [ +  +  +  + ]:        2963633 :     return HeapTupleHeaderIsDatum(tup) && tup->t_choice.t_datum.datum_len_ != 0;
                                624                 :                : }
                                625                 :                : 
                                626                 :                : /*
                                627                 :                :  * Is the tuple a versioned datum tuple?
                                628                 :                :  *
                                629                 :                :  * RECORDOID datums are never versioned.  Neither are datums whose t_ctid has
                                630                 :                :  * been overwritten, e.g. by FDWs storing a real TID there.
                                631                 :                :  */
                                632                 :                : static inline bool
                                633                 :      203650635 : HeapTupleHeaderIsVersionedDatum(const HeapTupleHeaderData *tup)
                                634                 :                : {
                                635   [ +  +  +  + ]:      210280727 :     return HeapTupleHeaderIsDatum(tup) &&
                                636                 :        6630092 :         ItemPointerGetOffsetNumberNoCheck(&tup->t_ctid) == VersionedDatumOffsetNumber;
                                637                 :                : }
                                638                 :                : 
                                639                 :                : static inline void
                                640                 :        5060369 : HeapTupleHeaderSetVersion(HeapTupleHeaderData *tup, XLogRecPtr version)
                                641                 :                : {
                                642                 :        5060369 :     uint32      version_high = (uint32) (version >> 32);
                                643                 :        5060369 :     uint32      version_low = (uint32) (version);
                                644                 :                : 
                                645         [ -  + ]:        5060369 :     Assert(HeapTupleHeaderIsDatum(tup));
                                646                 :                : 
                                647                 :                :     /* set the version */
                                648                 :        5060369 :     tup->t_choice.t_datum.datum_typmod = version_low;
                                649                 :        5060369 :     ItemPointerSetBlockNumber(&tup->t_ctid, version_high);
                                650                 :                : 
                                651                 :                :     /* mark as versioned */
                                652                 :        5060369 :     ItemPointerSetOffsetNumber(&tup->t_ctid, VersionedDatumOffsetNumber);
                                653                 :        5060369 : }
                                654                 :                : 
                                655                 :                : static inline XLogRecPtr
                                656                 :        1014361 : HeapTupleHeaderGetVersion(const HeapTupleHeaderData *tup)
                                657                 :                : {
                                658                 :                :     XLogRecPtr  version;
                                659                 :                :     uint32      version_low;
                                660                 :                :     uint32      version_high;
                                661                 :                : 
                                662         [ -  + ]:        1014361 :     Assert(HeapTupleHeaderIsVersionedDatum(tup));
                                663                 :                : 
                                664                 :        1014361 :     version_low = tup->t_choice.t_datum.datum_typmod;
                                665                 :        1014361 :     version_high = ItemPointerGetBlockNumberNoCheck(&tup->t_ctid);
                                666                 :                : 
                                667                 :        1014361 :     version = ((uint64) version_high) << 32 | version_low;
                                668                 :                : 
                                669                 :        1014361 :     return version;
                                670                 :                : }
                                671                 :                : 
                                672                 :                : /*
                                673                 :                :  * Note that we stop considering a tuple HOT-updated as soon as it is known
                                674                 :                :  * aborted or the would-be updating transaction is known aborted.  For best
                                675                 :                :  * efficiency, check tuple visibility before using this function, so that the
                                676                 :                :  * INVALID bits will be as up to date as possible.
                                677                 :                :  */
                                678                 :                : static inline bool
  674 peter@eisentraut.org      679                 :       27150180 : HeapTupleHeaderIsHotUpdated(const HeapTupleHeaderData *tup)
                                680                 :                : {
                                681                 :                :     return
                                682                 :       29171695 :         (tup->t_infomask2 & HEAP_HOT_UPDATED) != 0 &&
                                683   [ +  +  +  + ]:       29171687 :         (tup->t_infomask & HEAP_XMAX_INVALID) == 0 &&
                                684         [ +  + ]:        2021507 :         !HeapTupleHeaderXminInvalid(tup);
                                685                 :                : }
                                686                 :                : 
                                687                 :                : static inline void
                                688                 :         217592 : HeapTupleHeaderSetHotUpdated(HeapTupleHeaderData *tup)
                                689                 :                : {
                                690                 :         217592 :     tup->t_infomask2 |= HEAP_HOT_UPDATED;
                                691                 :         217592 : }
                                692                 :                : 
                                693                 :                : static inline void
                                694                 :        6935473 : HeapTupleHeaderClearHotUpdated(HeapTupleHeaderData *tup)
                                695                 :                : {
                                696                 :        6935473 :     tup->t_infomask2 &= ~HEAP_HOT_UPDATED;
                                697                 :        6935473 : }
                                698                 :                : 
                                699                 :                : static inline bool
                                700                 :       83432674 : HeapTupleHeaderIsHeapOnly(const HeapTupleHeaderData *tup) \
                                701                 :                : {
                                702                 :       83432674 :     return (tup->t_infomask2 & HEAP_ONLY_TUPLE) != 0;
                                703                 :                : }
                                704                 :                : 
                                705                 :                : static inline void
                                706                 :         354324 : HeapTupleHeaderSetHeapOnly(HeapTupleHeaderData *tup)
                                707                 :                : {
                                708                 :         354324 :     tup->t_infomask2 |= HEAP_ONLY_TUPLE;
                                709                 :         354324 : }
                                710                 :                : 
                                711                 :                : static inline void
                                712                 :        4463140 : HeapTupleHeaderClearHeapOnly(HeapTupleHeaderData *tup)
                                713                 :                : {
                                714                 :        4463140 :     tup->t_infomask2 &= ~HEAP_ONLY_TUPLE;
                                715                 :        4463140 : }
                                716                 :                : 
                                717                 :                : /*
                                718                 :                :  * These are used with both HeapTuple and MinimalTuple, so they must be
                                719                 :                :  * macros.
                                720                 :                :  */
                                721                 :                : 
                                722                 :                : #define HeapTupleHeaderGetNatts(tup) \
                                723                 :                :     ((tup)->t_infomask2 & HEAP_NATTS_MASK)
                                724                 :                : 
                                725                 :                : #define HeapTupleHeaderSetNatts(tup, natts) \
                                726                 :                : ( \
                                727                 :                :     (tup)->t_infomask2 = ((tup)->t_infomask2 & ~HEAP_NATTS_MASK) | (natts) \
                                728                 :                : )
                                729                 :                : 
                                730                 :                : #define HeapTupleHeaderHasExternal(tup) \
                                731                 :                :         (((tup)->t_infomask & HEAP_HASEXTERNAL) != 0)
                                732                 :                : 
                                733                 :                : 
                                734                 :                : /*
                                735                 :                :  * BITMAPLEN(NATTS) -
                                736                 :                :  *      Computes size of null bitmap given number of data columns.
                                737                 :                :  */
                                738                 :                : static inline int
                                739                 :       36293466 : BITMAPLEN(int NATTS)
                                740                 :                : {
                                741                 :       36293466 :     return (NATTS + 7) / 8;
                                742                 :                : }
                                743                 :                : 
                                744                 :                : /*
                                745                 :                :  * MaxHeapTupleSize is the maximum allowed size of a heap tuple, including
                                746                 :                :  * header and MAXALIGN alignment padding.  Basically it's BLCKSZ minus the
                                747                 :                :  * other stuff that has to be on a disk page.  Since heap pages use no
                                748                 :                :  * "special space", there's no deduction for that.
                                749                 :                :  *
                                750                 :                :  * NOTE: we allow for the ItemId that must point to the tuple, ensuring that
                                751                 :                :  * an otherwise-empty page can indeed hold a tuple of this size.  Because
                                752                 :                :  * ItemIds and tuples have different alignment requirements, don't assume that
                                753                 :                :  * you can, say, fit 2 tuples of size MaxHeapTupleSize/2 on the same page.
                                754                 :                :  */
                                755                 :                : #define MaxHeapTupleSize  (BLCKSZ - MAXALIGN(SizeOfPageHeaderData + sizeof(ItemIdData)))
                                756                 :                : #define MinHeapTupleSize  MAXALIGN(SizeofHeapTupleHeader)
                                757                 :                : 
                                758                 :                : /*
                                759                 :                :  * MaxHeapTuplesPerPage is an upper bound on the number of tuples that can
                                760                 :                :  * fit on one heap page.  (Note that indexes could have more, because they
                                761                 :                :  * use a smaller tuple header.)  We arrive at the divisor because each tuple
                                762                 :                :  * must be maxaligned, and it must have an associated line pointer.
                                763                 :                :  *
                                764                 :                :  * Note: with HOT, there could theoretically be more line pointers (not actual
                                765                 :                :  * tuples) than this on a heap page.  However we constrain the number of line
                                766                 :                :  * pointers to this anyway, to avoid excessive line-pointer bloat and not
                                767                 :                :  * require increases in the size of work arrays.
                                768                 :                :  */
                                769                 :                : #define MaxHeapTuplesPerPage    \
                                770                 :                :     ((int) ((BLCKSZ - SizeOfPageHeaderData) / \
                                771                 :                :             (MAXALIGN(SizeofHeapTupleHeader) + sizeof(ItemIdData))))
                                772                 :                : 
                                773                 :                : /*
                                774                 :                :  * MaxAttrSize is a somewhat arbitrary upper limit on the declared size of
                                775                 :                :  * data fields of char(n) and similar types.  It need not have anything
                                776                 :                :  * directly to do with the *actual* upper limit of varlena values, which
                                777                 :                :  * is currently 1Gb (see TOAST structures in varatt.h).  I've set it
                                778                 :                :  * at 10Mb which seems like a reasonable number --- tgl 8/6/00.
                                779                 :                :  */
                                780                 :                : #define MaxAttrSize     (10 * 1024 * 1024)
                                781                 :                : 
                                782                 :                : 
                                783                 :                : /*
                                784                 :                :  * MinimalTuple is an alternative representation that is used for transient
                                785                 :                :  * tuples inside the executor, in places where transaction status information
                                786                 :                :  * is not required, the tuple rowtype is known, and shaving off a few bytes
                                787                 :                :  * is worthwhile because we need to store many tuples.  The representation
                                788                 :                :  * is chosen so that tuple access routines can work with either full or
                                789                 :                :  * minimal tuples via a HeapTupleData pointer structure.  The access routines
                                790                 :                :  * see no difference, except that they must not access the transaction status
                                791                 :                :  * or t_ctid fields because those aren't there.
                                792                 :                :  *
                                793                 :                :  * For the most part, MinimalTuples should be accessed via TupleTableSlot
                                794                 :                :  * routines.  These routines will prevent access to the "system columns"
                                795                 :                :  * and thereby prevent accidental use of the nonexistent fields.
                                796                 :                :  *
                                797                 :                :  * MinimalTupleData contains a length word, some padding, and fields matching
                                798                 :                :  * HeapTupleHeaderData beginning with t_infomask2. The padding is chosen so
                                799                 :                :  * that offsetof(t_infomask2) is the same modulo MAXIMUM_ALIGNOF in both
                                800                 :                :  * structs.   This makes data alignment rules equivalent in both cases.
                                801                 :                :  *
                                802                 :                :  * When a minimal tuple is accessed via a HeapTupleData pointer, t_data is
                                803                 :                :  * set to point MINIMAL_TUPLE_OFFSET bytes before the actual start of the
                                804                 :                :  * minimal tuple --- that is, where a full tuple matching the minimal tuple's
                                805                 :                :  * data would start.  This trick is what makes the structs seem equivalent.
                                806                 :                :  *
                                807                 :                :  * Note that t_hoff is computed the same as in a full tuple, hence it includes
                                808                 :                :  * the MINIMAL_TUPLE_OFFSET distance.  t_len does not include that, however.
                                809                 :                :  *
                                810                 :                :  * MINIMAL_TUPLE_DATA_OFFSET is the offset to the first useful (non-pad) data
                                811                 :                :  * other than the length word.  tuplesort.c and tuplestore.c use this to avoid
                                812                 :                :  * writing the padding to disk.
                                813                 :                :  */
                                814                 :                : #define MINIMAL_TUPLE_OFFSET \
                                815                 :                :     ((offsetof(HeapTupleHeaderData, t_infomask2) - sizeof(uint32)) / MAXIMUM_ALIGNOF * MAXIMUM_ALIGNOF)
                                816                 :                : #define MINIMAL_TUPLE_PADDING \
                                817                 :                :     ((offsetof(HeapTupleHeaderData, t_infomask2) - sizeof(uint32)) % MAXIMUM_ALIGNOF)
                                818                 :                : #define MINIMAL_TUPLE_DATA_OFFSET \
                                819                 :                :     offsetof(MinimalTupleData, t_infomask2)
                                820                 :                : 
                                821                 :                : struct MinimalTupleData
                                822                 :                : {
                                823                 :                :     uint32      t_len;          /* actual length of minimal tuple */
                                824                 :                : 
                                825                 :                :     char        mt_padding[MINIMAL_TUPLE_PADDING];
                                826                 :                : 
                                827                 :                :     /* Fields below here must match HeapTupleHeaderData! */
                                828                 :                : 
                                829                 :                :     uint16      t_infomask2;    /* number of attributes + various flags */
                                830                 :                : 
                                831                 :                :     uint16      t_infomask;     /* various flag bits, see below */
                                832                 :                : 
                                833                 :                :     uint8       t_hoff;         /* sizeof header incl. bitmap, padding */
                                834                 :                : 
                                835                 :                :     /* ^ - 23 bytes - ^ */
                                836                 :                : 
                                837                 :                :     uint8       t_bits[FLEXIBLE_ARRAY_MEMBER];  /* bitmap of NULLs */
                                838                 :                : 
                                839                 :                :     /* MORE DATA FOLLOWS AT END OF STRUCT */
                                840                 :                : };
                                841                 :                : 
                                842                 :                : /* typedef appears in htup.h */
                                843                 :                : 
                                844                 :                : #define SizeofMinimalTupleHeader offsetof(MinimalTupleData, t_bits)
                                845                 :                : 
                                846                 :                : /*
                                847                 :                :  * MinimalTuple accessor functions
                                848                 :                :  */
                                849                 :                : 
                                850                 :                : static inline bool
                                851                 :        9278599 : HeapTupleHeaderHasMatch(const MinimalTupleData *tup)
                                852                 :                : {
                                853                 :        9278599 :     return (tup->t_infomask2 & HEAP_TUPLE_HAS_MATCH) != 0;
                                854                 :                : }
                                855                 :                : 
                                856                 :                : static inline void
                                857                 :        3991045 : HeapTupleHeaderSetMatch(MinimalTupleData *tup)
                                858                 :                : {
                                859                 :        3991045 :     tup->t_infomask2 |= HEAP_TUPLE_HAS_MATCH;
                                860                 :        3991045 : }
                                861                 :                : 
                                862                 :                : static inline void
                                863                 :        7888427 : HeapTupleHeaderClearMatch(MinimalTupleData *tup)
                                864                 :                : {
                                865                 :        7888427 :     tup->t_infomask2 &= ~HEAP_TUPLE_HAS_MATCH;
                                866                 :        7888427 : }
                                867                 :                : 
                                868                 :                : 
                                869                 :                : /*
                                870                 :                :  * GETSTRUCT - given a HeapTuple pointer, return address of the user data
                                871                 :                :  */
                                872                 :                : static inline void *
                                873                 :       94809581 : GETSTRUCT(const HeapTupleData *tuple)
                                874                 :                : {
                                875                 :       94809581 :     return ((char *) (tuple->t_data) + tuple->t_data->t_hoff);
                                876                 :                : }
                                877                 :                : 
                                878                 :                : /*
                                879                 :                :  * Accessor functions to be used with HeapTuple pointers.
                                880                 :                :  */
                                881                 :                : 
                                882                 :                : static inline bool
                                883                 :      492092304 : HeapTupleHasNulls(const HeapTupleData *tuple)
                                884                 :                : {
                                885                 :      492092304 :     return (tuple->t_data->t_infomask & HEAP_HASNULL) != 0;
                                886                 :                : }
                                887                 :                : 
                                888                 :                : static inline bool
                                889                 :      213275721 : HeapTupleNoNulls(const HeapTupleData *tuple)
                                890                 :                : {
                                891                 :      213275721 :     return !HeapTupleHasNulls(tuple);
                                892                 :                : }
                                893                 :                : 
                                894                 :                : static inline bool
                                895                 :      140507744 : HeapTupleHasVarWidth(const HeapTupleData *tuple)
                                896                 :                : {
                                897                 :      140507744 :     return (tuple->t_data->t_infomask & HEAP_HASVARWIDTH) != 0;
                                898                 :                : }
                                899                 :                : 
                                900                 :                : static inline bool
                                901                 :                : HeapTupleAllFixed(const HeapTupleData *tuple)
                                902                 :                : {
                                903                 :                :     return !HeapTupleHasVarWidth(tuple);
                                904                 :                : }
                                905                 :                : 
                                906                 :                : static inline bool
                                907                 :       24430666 : HeapTupleHasExternal(const HeapTupleData *tuple)
                                908                 :                : {
                                909                 :       24430666 :     return (tuple->t_data->t_infomask & HEAP_HASEXTERNAL) != 0;
                                910                 :                : }
                                911                 :                : 
                                912                 :                : static inline bool
                                913                 :       10404677 : HeapTupleIsHotUpdated(const HeapTupleData *tuple)
                                914                 :                : {
                                915                 :       10404677 :     return HeapTupleHeaderIsHotUpdated(tuple->t_data);
                                916                 :                : }
                                917                 :                : 
                                918                 :                : static inline void
                                919                 :         177162 : HeapTupleSetHotUpdated(const HeapTupleData *tuple)
                                920                 :                : {
                                921                 :         177162 :     HeapTupleHeaderSetHotUpdated(tuple->t_data);
                                922                 :         177162 : }
                                923                 :                : 
                                924                 :                : static inline void
                                925                 :        4447013 : HeapTupleClearHotUpdated(const HeapTupleData *tuple)
                                926                 :                : {
                                927                 :        4447013 :     HeapTupleHeaderClearHotUpdated(tuple->t_data);
                                928                 :        4447013 : }
                                929                 :                : 
                                930                 :                : static inline bool
                                931                 :       46148159 : HeapTupleIsHeapOnly(const HeapTupleData *tuple)
                                932                 :                : {
                                933                 :       46148159 :     return HeapTupleHeaderIsHeapOnly(tuple->t_data);
                                934                 :                : }
                                935                 :                : 
                                936                 :                : static inline void
                                937                 :         354324 : HeapTupleSetHeapOnly(const HeapTupleData *tuple)
                                938                 :                : {
                                939                 :         354324 :     HeapTupleHeaderSetHeapOnly(tuple->t_data);
                                940                 :         354324 : }
                                941                 :                : 
                                942                 :                : static inline void
                                943                 :        4463140 : HeapTupleClearHeapOnly(const HeapTupleData *tuple)
                                944                 :                : {
                                945                 :        4463140 :     HeapTupleHeaderClearHeapOnly(tuple->t_data);
                                946                 :        4463140 : }
                                947                 :                : 
                                948                 :                : /* GUC variable, in common/heaptuple.c */
                                949                 :                : extern PGDLLIMPORT int composite_type_mismatch;
                                950                 :                : 
                                951                 :                : /* possible values for composite_type_mismatch */
                                952                 :                : typedef enum CompositeTypeMismatchOptions
                                953                 :                : {
                                954                 :                :     COMPOSITE_TYPE_MISMATCH_ERROR,
                                955                 :                :     COMPOSITE_TYPE_MISMATCH_WARN,
                                956                 :                :     COMPOSITE_TYPE_MISMATCH_DISABLED,
                                957                 :                : } CompositeTypeMismatchOptions;
                                958                 :                : 
                                959                 :                : /* prototypes for functions in common/heaptuple.c */
                                960                 :                : extern Size heap_compute_data_size(TupleDesc tupleDesc,
                                961                 :                :                                    const Datum *values, const bool *isnull,
                                962                 :                :                                    bool allow_huge);
                                963                 :                : 
                                964                 :                : extern void heap_fill_tuple(TupleDesc tupleDesc,
                                965                 :                :                             const Datum *values, const bool *isnull,
                                966                 :                :                             char *data, Size data_size,
                                967                 :                :                             uint16 *infomask, uint8 *bit);
                                968                 :                : extern void heap_tuple_set_datum(HeapTupleHeader tup, TupleDesc tupleDesc, uint32 len);
                                969                 :                : extern void heap_datum_update_type(HeapTupleHeader tup, TupleDesc tupleDesc);
                                970                 :                : extern bool heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc);
                                971                 :                : extern Datum nocachegetattr(HeapTuple tup, int attnum,
                                972                 :                :                             TupleDesc tupleDesc);
                                973                 :                : extern Datum heap_getsysattr(HeapTuple tup, int attnum, TupleDesc tupleDesc,
                                974                 :                :                              bool *isnull);
                                975                 :                : extern Datum getmissingattr(TupleDesc tupleDesc,
                                976                 :                :                             int attnum, bool *isnull);
                                977                 :                : extern HeapTuple heap_copytuple(HeapTuple tuple);
                                978                 :                : extern Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc);
                                979                 :                : extern HeapTuple heap_form_tuple(TupleDesc tupleDescriptor,
                                980                 :                :                                  const Datum *values, const bool *isnull);
                                981                 :                : extern HeapTuple heap_modify_tuple(HeapTuple tuple,
                                982                 :                :                                    TupleDesc tupleDesc,
                                983                 :                :                                    const Datum *replValues,
                                984                 :                :                                    const bool *replIsnull,
                                985                 :                :                                    const bool *doReplace);
                                986                 :                : extern HeapTuple heap_modify_tuple_by_cols(HeapTuple tuple,
                                987                 :                :                                            TupleDesc tupleDesc,
                                988                 :                :                                            int nCols,
                                989                 :                :                                            const int *replCols,
                                990                 :                :                                            const Datum *replValues,
                                991                 :                :                                            const bool *replIsnull);
                                992                 :                : extern void heap_datum_check(HeapTupleHeader tup, TupleDesc tupleDesc);
                                993                 :                : extern void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc,
                                994                 :                :                               Datum *values, bool *isnull);
                                995                 :                : extern void heap_freetuple(HeapTuple htup);
                                996                 :                : extern MinimalTuple heap_form_minimal_tuple(TupleDesc tupleDescriptor,
                                997                 :                :                                             const Datum *values, const bool *isnull,
                                998                 :                :                                             Size extra);
                                999                 :                : extern void heap_free_minimal_tuple(MinimalTuple mtup);
                               1000                 :                : extern MinimalTuple heap_copy_minimal_tuple(MinimalTuple mtup, Size extra);
                               1001                 :                : extern HeapTuple heap_tuple_from_minimal_tuple(MinimalTuple mtup);
                               1002                 :                : extern MinimalTuple minimal_tuple_from_heap_tuple(HeapTuple htup, Size extra);
                               1003                 :                : extern size_t varsize_any(void *p);
                               1004                 :                : 
                               1005                 :                : #ifndef FRONTEND
                               1006                 :                : /*
                               1007                 :                :  *  fastgetattr
                               1008                 :                :  *      Fetch a user attribute's value as a Datum (might be either a
                               1009                 :                :  *      value, or a pointer into the data area of the tuple).
                               1010                 :                :  *
                               1011                 :                :  *      This must not be used when a system attribute might be requested.
                               1012                 :                :  *      Furthermore, the passed attnum MUST be valid.  Use heap_getattr()
                               1013                 :                :  *      instead, if in doubt.
                               1014                 :                :  *
                               1015                 :                :  *      This gets called many times, so we macro the cacheable and NULL
                               1016                 :                :  *      lookups, and call nocachegetattr() for the rest.
                               1017                 :                :  */
                               1018                 :                : static inline Datum
 1710 alvherre@alvh.no-ip.     1019                 :      206314506 : fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
                               1020                 :                : {
 1697 peter@eisentraut.org     1021         [ -  + ]:      206314506 :     Assert(attnum > 0);
                               1022                 :                : 
                               1023                 :                :     /*
                               1024                 :                :      * NB: Right now this does not call heap_datum_check(). The reasoning is
                               1025                 :                :      * that
                               1026                 :                :      *
                               1027                 :                :      * a) fastgetattr() is used in quite performance sensitive paths like
                               1028                 :                :      * catcache, adding code bloat to it would not be great
                               1029                 :                :      *
                               1030                 :                :      * b) The requirement that the caller already have validated attnum
                               1031                 :                :      * (without that we'd run off the end of the attribute array!), basically
                               1032                 :                :      * means it can only be used where the caller is sure that the type isn't
                               1033                 :                :      * evolving.
                               1034                 :                :      *
                               1035                 :                :      * c) fastgetattr() is called from heap_getattr(), so we would often check
                               1036                 :                :      * twice.
                               1037                 :                :      *
                               1038                 :                :      * I think that's the right decision. An investigation of extensions found
                               1039                 :                :      * only two users ("rum" and "storage_engine") and both of them use
                               1040                 :                :      * fastgetattr() on tables that can't be altered by non-superusers.
                               1041                 :                :      *
                               1042                 :                :      * While it might seem we should just assert that this is not used for
                               1043                 :                :      * datum tuples, we can't - fastgetattr() is often called on a modified
                               1044                 :                :      * copy of the tuple, and those are marked as datum tuples, so this would
                               1045                 :                :      * spuriously trigger all the time :(.
                               1046                 :                :      */
                               1047                 :                : 
 1710 alvherre@alvh.no-ip.     1048                 :      206314506 :     *isnull = false;
                               1049         [ +  + ]:      206314506 :     if (HeapTupleNoNulls(tup))
                               1050                 :                :     {
                               1051                 :                :         CompactAttribute *att;
                               1052                 :                : 
  708 drowley@postgresql.o     1053                 :       89034280 :         att = TupleDescCompactAttr(tupleDesc, attnum - 1);
 1710 alvherre@alvh.no-ip.     1054         [ +  + ]:       89034280 :         if (att->attcacheoff >= 0)
                               1055                 :       49544088 :             return fetchatt(att, (char *) tup->t_data + tup->t_data->t_hoff +
                               1056                 :                :                             att->attcacheoff);
                               1057                 :                :         else
                               1058                 :       39490192 :             return nocachegetattr(tup, attnum, tupleDesc);
                               1059                 :                :     }
                               1060                 :                :     else
                               1061                 :                :     {
                               1062         [ +  + ]:      117280226 :         if (att_isnull(attnum - 1, tup->t_data->t_bits))
                               1063                 :                :         {
                               1064                 :       16028045 :             *isnull = true;
  477 tgl@sss.pgh.pa.us        1065                 :       16028045 :             return (Datum) 0;
                               1066                 :                :         }
                               1067                 :                :         else
 1710 alvherre@alvh.no-ip.     1068                 :      101252181 :             return nocachegetattr(tup, attnum, tupleDesc);
                               1069                 :                :     }
                               1070                 :                : }
                               1071                 :                : 
                               1072                 :                : /*
                               1073                 :                :  *  heap_getattr
                               1074                 :                :  *      Extract an attribute of a heap tuple and return it as a Datum.
                               1075                 :                :  *      This works for either system or user attributes.  The given attnum
                               1076                 :                :  *      is properly range-checked.
                               1077                 :                :  *
                               1078                 :                :  *      If the field in question has a NULL value, we return a zero Datum
                               1079                 :                :  *      and set *isnull == true.  Otherwise, we set *isnull == false.
                               1080                 :                :  *
                               1081                 :                :  *      <tup> is the pointer to the heap tuple.  <attnum> is the attribute
                               1082                 :                :  *      number of the column (field) caller wants.  <tupleDesc> is a
                               1083                 :                :  *      pointer to the structure describing the row and all its fields.
                               1084                 :                :  *
                               1085                 :                :  */
                               1086                 :                : static inline Datum
                               1087                 :      194611543 : heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
                               1088                 :                : {
                               1089                 :                :     /*
                               1090                 :                :      * It's a bit sad to have an external function call here, but we shouldn't
                               1091                 :                :      * use it in too many speed critical paths anymore. Separately, the
                               1092                 :                :      * function looks like it's getting too complicated to be worth inlining.
                               1093                 :                :      */
   19 andres@anarazel.de       1094         [ +  + ]:      194611543 :     if (HeapTupleHeaderIsVersionedDatum(tup->t_data))
                               1095                 :        1019004 :         heap_datum_check(tup->t_data, tupleDesc);
                               1096                 :                : 
 1710 alvherre@alvh.no-ip.     1097         [ +  - ]:      194611526 :     if (attnum > 0)
                               1098                 :                :     {
                               1099         [ +  + ]:      194611526 :         if (attnum > (int) HeapTupleHeaderGetNatts(tup->t_data))
                               1100                 :            249 :             return getmissingattr(tupleDesc, attnum, isnull);
                               1101                 :                :         else
                               1102                 :      194611277 :             return fastgetattr(tup, attnum, tupleDesc, isnull);
                               1103                 :                :     }
                               1104                 :                :     else
 1710 alvherre@alvh.no-ip.     1105                 :UBC           0 :         return heap_getsysattr(tup, attnum, tupleDesc, isnull);
                               1106                 :                : }
                               1107                 :                : #endif                          /* FRONTEND */
                               1108                 :                : 
                               1109                 :                : #endif                          /* HTUP_DETAILS_H */
        

Generated by: LCOV version 2.0-1