LCOV - code coverage report
Current view: top level - src/include/access - htup_details.h (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 97.3 % 187 182
Test Date: 2026-11-11 13:16:13 Functions: 98.4 % 61 60
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 92.0 % 50 46

             Branch data     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
     262                 :    31449820 : HEAP_XMAX_IS_LOCKED_ONLY(uint16 infomask)
     263                 :             : {
     264         [ +  + ]:    60652169 :     return (infomask & HEAP_XMAX_LOCK_ONLY) ||
     265         [ -  + ]:    29202349 :         (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                 :      186552 : HEAP_LOCKED_UPGRADED(uint16 infomask)
     288                 :             : {
     289                 :             :     return
     290                 :      336841 :         (infomask & HEAP_XMAX_IS_MULTI) != 0 &&
     291   [ +  +  +  + ]:      332470 :         (infomask & HEAP_XMAX_LOCK_ONLY) != 0 &&
     292         [ -  + ]:      145918 :         (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
     299                 :       99972 : HEAP_XMAX_IS_SHR_LOCKED(uint16 infomask)
     300                 :             : {
     301                 :       99972 :     return (infomask & HEAP_LOCK_MASK) == HEAP_XMAX_SHR_LOCK;
     302                 :             : }
     303                 :             : 
     304                 :             : static inline bool
     305                 :      106774 : HEAP_XMAX_IS_EXCL_LOCKED(uint16 infomask)
     306                 :             : {
     307                 :      106774 :     return (infomask & HEAP_LOCK_MASK) == HEAP_XMAX_EXCL_LOCK;
     308                 :             : }
     309                 :             : 
     310                 :             : static inline bool
     311                 :      105922 : HEAP_XMAX_IS_KEYSHR_LOCKED(uint16 infomask)
     312                 :             : {
     313                 :      105922 :     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                 :   206513012 : HeapTupleHeaderGetRawXmin(const HeapTupleHeaderData *tup)
     356                 :             : {
     357                 :   206513012 :     return tup->t_choice.t_heap.t_xmin;
     358                 :             : }
     359                 :             : 
     360                 :             : static inline TransactionId
     361                 :    71555997 : HeapTupleHeaderGetXmin(const HeapTupleHeaderData *tup)
     362                 :             : {
     363                 :    71555997 :     return HeapTupleHeaderXminFrozen(tup) ?
     364         [ +  + ]:    71555997 :         FrozenTransactionId : HeapTupleHeaderGetRawXmin(tup);
     365                 :             : }
     366                 :             : 
     367                 :             : static inline void
     368                 :    18106852 : HeapTupleHeaderSetXmin(HeapTupleHeaderData *tup, TransactionId xid)
     369                 :             : {
     370                 :    18106852 :     tup->t_choice.t_heap.t_xmin = xid;
     371                 :    18106852 : }
     372                 :             : 
     373                 :             : static inline bool
     374                 :   187564204 : HeapTupleHeaderXminCommitted(const HeapTupleHeaderData *tup)
     375                 :             : {
     376                 :   187564204 :     return (tup->t_infomask & HEAP_XMIN_COMMITTED) != 0;
     377                 :             : }
     378                 :             : 
     379                 :             : static inline bool
     380                 :    49546859 : HeapTupleHeaderXminInvalid(const HeapTupleHeaderData *tup) \
     381                 :             : {
     382                 :    49546859 :     return (tup->t_infomask & (HEAP_XMIN_COMMITTED | HEAP_XMIN_INVALID)) ==
     383                 :             :         HEAP_XMIN_INVALID;
     384                 :             : }
     385                 :             : 
     386                 :             : static inline bool
     387                 :   376943435 : HeapTupleHeaderXminFrozen(const HeapTupleHeaderData *tup)
     388                 :             : {
     389                 :   376943435 :     return (tup->t_infomask & HEAP_XMIN_FROZEN) == HEAP_XMIN_FROZEN;
     390                 :             : }
     391                 :             : 
     392                 :             : static inline void
     393                 :      104151 : HeapTupleHeaderSetXminFrozen(HeapTupleHeaderData *tup)
     394                 :             : {
     395                 :             :     Assert(!HeapTupleHeaderXminInvalid(tup));
     396                 :      104151 :     tup->t_infomask |= HEAP_XMIN_FROZEN;
     397                 :      104151 : }
     398                 :             : 
     399                 :             : static inline TransactionId
     400                 :    88994125 : HeapTupleHeaderGetRawXmax(const HeapTupleHeaderData *tup)
     401                 :             : {
     402                 :    88994125 :     return tup->t_choice.t_heap.t_xmax;
     403                 :             : }
     404                 :             : 
     405                 :             : static inline void
     406                 :    25562864 : HeapTupleHeaderSetXmax(HeapTupleHeaderData *tup, TransactionId xid)
     407                 :             : {
     408                 :    25562864 :     tup->t_choice.t_heap.t_xmax = xid;
     409                 :    25562864 : }
     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                 :    12545319 : HeapTupleHeaderGetUpdateXid(const HeapTupleHeaderData *tup)
     421                 :             : {
     422         [ +  + ]:    12545319 :     if (!((tup)->t_infomask & HEAP_XMAX_INVALID) &&
     423         [ +  + ]:    11835433 :         ((tup)->t_infomask & HEAP_XMAX_IS_MULTI) &&
     424         [ +  + ]:       76749 :         !((tup)->t_infomask & HEAP_XMAX_LOCK_ONLY))
     425                 :       76709 :         return HeapTupleGetUpdateXid(tup);
     426                 :             :     else
     427                 :    12468610 :         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                 :    20016677 : HeapTupleHeaderGetRawCommandId(const HeapTupleHeaderData *tup)
     439                 :             : {
     440                 :    20016677 :     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                 :    18055342 : HeapTupleHeaderSetCmin(HeapTupleHeaderData *tup, CommandId cid)
     446                 :             : {
     447                 :             :     Assert(!(tup->t_infomask & HEAP_MOVED));
     448                 :    18055342 :     tup->t_choice.t_heap.t_field3.t_cid = cid;
     449                 :    18055342 :     tup->t_infomask &= ~HEAP_COMBOCID;
     450                 :    18055342 : }
     451                 :             : 
     452                 :             : /* SetCmax must be used after HeapTupleHeaderAdjustCmax; see combocid.c */
     453                 :             : static inline void
     454                 :     7035358 : HeapTupleHeaderSetCmax(HeapTupleHeaderData *tup, CommandId cid, bool iscombo)
     455                 :             : {
     456                 :             :     Assert(!((tup)->t_infomask & HEAP_MOVED));
     457                 :     7035358 :     tup->t_choice.t_heap.t_field3.t_cid = cid;
     458         [ +  + ]:     7035358 :     if (iscombo)
     459                 :      250955 :         tup->t_infomask |= HEAP_COMBOCID;
     460                 :             :     else
     461                 :     6784403 :         tup->t_infomask &= ~HEAP_COMBOCID;
     462                 :     7035358 : }
     463                 :             : 
     464                 :             : static inline TransactionId
     465                 :    17193995 : HeapTupleHeaderGetXvac(const HeapTupleHeaderData *tup)
     466                 :             : {
     467         [ -  + ]:    17193995 :     if (tup->t_infomask & HEAP_MOVED)
     468                 :           0 :         return tup->t_choice.t_heap.t_field3.t_xvac;
     469                 :             :     else
     470                 :    17193995 :         return InvalidTransactionId;
     471                 :             : }
     472                 :             : 
     473                 :             : static inline void
     474                 :           0 : HeapTupleHeaderSetXvac(HeapTupleHeaderData *tup, TransactionId xid)
     475                 :             : {
     476                 :             :     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
     484                 :   229254150 : HeapTupleHeaderIsSpeculative(const HeapTupleHeaderData *tup)
     485                 :             : {
     486                 :   229254150 :     return ItemPointerGetOffsetNumberNoCheck(&tup->t_ctid) == SpecTokenOffsetNumber;
     487                 :             : }
     488                 :             : 
     489                 :             : static inline BlockNumber
     490                 :           2 : HeapTupleHeaderGetSpeculativeToken(const HeapTupleHeaderData *tup)
     491                 :             : {
     492                 :             :     Assert(HeapTupleHeaderIsSpeculative(tup));
     493                 :           2 :     return ItemPointerGetBlockNumber(&tup->t_ctid);
     494                 :             : }
     495                 :             : 
     496                 :             : static inline void
     497                 :        2279 : HeapTupleHeaderSetSpeculativeToken(HeapTupleHeaderData *tup, BlockNumber token)
     498                 :             : {
     499                 :        2279 :     ItemPointerSet(&tup->t_ctid, token, SpecTokenOffsetNumber);
     500                 :        2279 : }
     501                 :             : 
     502                 :             : static inline bool
     503                 :       17176 : HeapTupleHeaderIndicatesMovedPartitions(const HeapTupleHeaderData *tup)
     504                 :             : {
     505                 :       17176 :     return ItemPointerIndicatesMovedPartitions(&tup->t_ctid);
     506                 :             : }
     507                 :             : 
     508                 :             : static inline void
     509                 :         780 : HeapTupleHeaderSetMovedPartitions(HeapTupleHeaderData *tup)
     510                 :             : {
     511                 :         780 :     ItemPointerSetMovedPartitions(&tup->t_ctid);
     512                 :         780 : }
     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
     526                 :   201325065 : HeapTupleHeaderIsDatum(const HeapTupleHeaderData *tup)
     527                 :             : {
     528                 :   201325065 :     return (tup->t_infomask & HEAP_IS_DATUM) == HEAP_IS_DATUM;
     529                 :             : }
     530                 :             : 
     531                 :             : static inline void
     532                 :    19214008 : HeapTupleHeaderSetDatum(HeapTupleHeaderData *tup)
     533                 :             : {
     534                 :    19214008 :     tup->t_infomask |= HEAP_IS_DATUM;
     535                 :    19214008 : }
     536                 :             : 
     537                 :             : static inline void
     538                 :       47013 : HeapTupleHeaderClearDatum(HeapTupleHeaderData *tup)
     539                 :             : {
     540                 :             :     /* otherwise we might clear transactionally relevant bits */
     541                 :             :     Assert(HeapTupleHeaderIsDatum(tup));
     542                 :       47013 :     tup->t_infomask &= ~HEAP_IS_DATUM;
     543                 :       47013 : }
     544                 :             : 
     545                 :             : static inline uint32
     546                 :     1747275 : HeapTupleHeaderGetDatumLength(const HeapTupleHeaderData *tup)
     547                 :             : {
     548                 :     1747275 :     return VARSIZE(tup);
     549                 :             : }
     550                 :             : 
     551                 :             : static inline void
     552                 :    19213964 : HeapTupleHeaderSetDatumLength(HeapTupleHeaderData *tup, uint32 len)
     553                 :             : {
     554                 :    19213964 :     SET_VARSIZE(tup, len);
     555                 :    19213964 : }
     556                 :             : 
     557                 :             : static inline Oid
     558                 :     7414658 : HeapTupleHeaderGetTypeId(const HeapTupleHeaderData *tup)
     559                 :             : {
     560                 :     7414658 :     return tup->t_choice.t_datum.datum_typeid;
     561                 :             : }
     562                 :             : 
     563                 :             : static inline void
     564                 :    19213964 : HeapTupleHeaderSetTypeId(HeapTupleHeaderData *tup, Oid datum_typeid)
     565                 :             : {
     566                 :    19213964 :     tup->t_choice.t_datum.datum_typeid = datum_typeid;
     567                 :    19213964 : }
     568                 :             : 
     569                 :             : static inline int32
     570                 :     2840957 : 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                 :             :      */
     583         [ +  + ]:     2840957 :     if (HeapTupleHeaderGetTypeId(tup) != RECORDOID)
     584                 :     1198170 :         return -1;
     585                 :     1642787 :     return tup->t_choice.t_datum.datum_typmod;
     586                 :             : }
     587                 :             : 
     588                 :             : static inline void
     589                 :    14170058 : HeapTupleHeaderSetTypMod(HeapTupleHeaderData *tup, int32 typmod)
     590                 :             : {
     591                 :    14170058 :     tup->t_choice.t_datum.datum_typmod = typmod;
     592                 :    14170058 : }
     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
     621                 :     1187820 : HeapTupleHeaderReallyIsDatum(const HeapTupleHeaderData *tup)
     622                 :             : {
     623   [ +  +  +  + ]:     1187820 :     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                 :   194770489 : HeapTupleHeaderIsVersionedDatum(const HeapTupleHeaderData *tup)
     634                 :             : {
     635   [ +  +  +  + ]:   199663755 :     return HeapTupleHeaderIsDatum(tup) &&
     636                 :     4893266 :         ItemPointerGetOffsetNumberNoCheck(&tup->t_ctid) == VersionedDatumOffsetNumber;
     637                 :             : }
     638                 :             : 
     639                 :             : static inline void
     640                 :     5043973 : HeapTupleHeaderSetVersion(HeapTupleHeaderData *tup, XLogRecPtr version)
     641                 :             : {
     642                 :     5043973 :     uint32      version_high = (uint32) (version >> 32);
     643                 :     5043973 :     uint32      version_low = (uint32) (version);
     644                 :             : 
     645                 :             :     Assert(HeapTupleHeaderIsDatum(tup));
     646                 :             : 
     647                 :             :     /* set the version */
     648                 :     5043973 :     tup->t_choice.t_datum.datum_typmod = version_low;
     649                 :     5043973 :     ItemPointerSetBlockNumber(&tup->t_ctid, version_high);
     650                 :             : 
     651                 :             :     /* mark as versioned */
     652                 :     5043973 :     ItemPointerSetOffsetNumber(&tup->t_ctid, VersionedDatumOffsetNumber);
     653                 :     5043973 : }
     654                 :             : 
     655                 :             : static inline XLogRecPtr
     656                 :     1002665 : HeapTupleHeaderGetVersion(const HeapTupleHeaderData *tup)
     657                 :             : {
     658                 :             :     XLogRecPtr  version;
     659                 :             :     uint32      version_low;
     660                 :             :     uint32      version_high;
     661                 :             : 
     662                 :             :     Assert(HeapTupleHeaderIsVersionedDatum(tup));
     663                 :             : 
     664                 :     1002665 :     version_low = tup->t_choice.t_datum.datum_typmod;
     665                 :     1002665 :     version_high = ItemPointerGetBlockNumberNoCheck(&tup->t_ctid);
     666                 :             : 
     667                 :     1002665 :     version = ((uint64) version_high) << 32 | version_low;
     668                 :             : 
     669                 :     1002665 :     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
     679                 :    27011869 : HeapTupleHeaderIsHotUpdated(const HeapTupleHeaderData *tup)
     680                 :             : {
     681                 :             :     return
     682                 :    28860771 :         (tup->t_infomask2 & HEAP_HOT_UPDATED) != 0 &&
     683   [ +  +  +  + ]:    28860763 :         (tup->t_infomask & HEAP_XMAX_INVALID) == 0 &&
     684         [ +  + ]:     1848894 :         !HeapTupleHeaderXminInvalid(tup);
     685                 :             : }
     686                 :             : 
     687                 :             : static inline void
     688                 :      214324 : HeapTupleHeaderSetHotUpdated(HeapTupleHeaderData *tup)
     689                 :             : {
     690                 :      214324 :     tup->t_infomask2 |= HEAP_HOT_UPDATED;
     691                 :      214324 : }
     692                 :             : 
     693                 :             : static inline void
     694                 :     6980086 : HeapTupleHeaderClearHotUpdated(HeapTupleHeaderData *tup)
     695                 :             : {
     696                 :     6980086 :     tup->t_infomask2 &= ~HEAP_HOT_UPDATED;
     697                 :     6980086 : }
     698                 :             : 
     699                 :             : static inline bool
     700                 :    85469500 : HeapTupleHeaderIsHeapOnly(const HeapTupleHeaderData *tup) \
     701                 :             : {
     702                 :    85469500 :     return (tup->t_infomask2 & HEAP_ONLY_TUPLE) != 0;
     703                 :             : }
     704                 :             : 
     705                 :             : static inline void
     706                 :      347584 : HeapTupleHeaderSetHeapOnly(HeapTupleHeaderData *tup)
     707                 :             : {
     708                 :      347584 :     tup->t_infomask2 |= HEAP_ONLY_TUPLE;
     709                 :      347584 : }
     710                 :             : 
     711                 :             : static inline void
     712                 :     4458618 : HeapTupleHeaderClearHeapOnly(HeapTupleHeaderData *tup)
     713                 :             : {
     714                 :     4458618 :     tup->t_infomask2 &= ~HEAP_ONLY_TUPLE;
     715                 :     4458618 : }
     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                 :    34331931 : BITMAPLEN(int NATTS)
     740                 :             : {
     741                 :    34331931 :     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                 :     9252633 : HeapTupleHeaderHasMatch(const MinimalTupleData *tup)
     852                 :             : {
     853                 :     9252633 :     return (tup->t_infomask2 & HEAP_TUPLE_HAS_MATCH) != 0;
     854                 :             : }
     855                 :             : 
     856                 :             : static inline void
     857                 :     3986946 : HeapTupleHeaderSetMatch(MinimalTupleData *tup)
     858                 :             : {
     859                 :     3986946 :     tup->t_infomask2 |= HEAP_TUPLE_HAS_MATCH;
     860                 :     3986946 : }
     861                 :             : 
     862                 :             : static inline void
     863                 :     7653766 : HeapTupleHeaderClearMatch(MinimalTupleData *tup)
     864                 :             : {
     865                 :     7653766 :     tup->t_infomask2 &= ~HEAP_TUPLE_HAS_MATCH;
     866                 :     7653766 : }
     867                 :             : 
     868                 :             : 
     869                 :             : /*
     870                 :             :  * GETSTRUCT - given a HeapTuple pointer, return address of the user data
     871                 :             :  */
     872                 :             : static inline void *
     873                 :    97076701 : GETSTRUCT(const HeapTupleData *tuple)
     874                 :             : {
     875                 :    97076701 :     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                 :   481565603 : HeapTupleHasNulls(const HeapTupleData *tuple)
     884                 :             : {
     885                 :   481565603 :     return (tuple->t_data->t_infomask & HEAP_HASNULL) != 0;
     886                 :             : }
     887                 :             : 
     888                 :             : static inline bool
     889                 :   206897499 : HeapTupleNoNulls(const HeapTupleData *tuple)
     890                 :             : {
     891                 :   206897499 :     return !HeapTupleHasNulls(tuple);
     892                 :             : }
     893                 :             : 
     894                 :             : static inline bool
     895                 :   138259040 : HeapTupleHasVarWidth(const HeapTupleData *tuple)
     896                 :             : {
     897                 :   138259040 :     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                 :    24707185 : HeapTupleHasExternal(const HeapTupleData *tuple)
     908                 :             : {
     909                 :    24707185 :     return (tuple->t_data->t_infomask & HEAP_HASEXTERNAL) != 0;
     910                 :             : }
     911                 :             : 
     912                 :             : static inline bool
     913                 :    10208101 : HeapTupleIsHotUpdated(const HeapTupleData *tuple)
     914                 :             : {
     915                 :    10208101 :     return HeapTupleHeaderIsHotUpdated(tuple->t_data);
     916                 :             : }
     917                 :             : 
     918                 :             : static inline void
     919                 :      173792 : HeapTupleSetHotUpdated(const HeapTupleData *tuple)
     920                 :             : {
     921                 :      173792 :     HeapTupleHeaderSetHotUpdated(tuple->t_data);
     922                 :      173792 : }
     923                 :             : 
     924                 :             : static inline void
     925                 :     4442328 : HeapTupleClearHotUpdated(const HeapTupleData *tuple)
     926                 :             : {
     927                 :     4442328 :     HeapTupleHeaderClearHotUpdated(tuple->t_data);
     928                 :     4442328 : }
     929                 :             : 
     930                 :             : static inline bool
     931                 :    45394188 : HeapTupleIsHeapOnly(const HeapTupleData *tuple)
     932                 :             : {
     933                 :    45394188 :     return HeapTupleHeaderIsHeapOnly(tuple->t_data);
     934                 :             : }
     935                 :             : 
     936                 :             : static inline void
     937                 :      347584 : HeapTupleSetHeapOnly(const HeapTupleData *tuple)
     938                 :             : {
     939                 :      347584 :     HeapTupleHeaderSetHeapOnly(tuple->t_data);
     940                 :      347584 : }
     941                 :             : 
     942                 :             : static inline void
     943                 :     4458618 : HeapTupleClearHeapOnly(const HeapTupleData *tuple)
     944                 :             : {
     945                 :     4458618 :     HeapTupleHeaderClearHeapOnly(tuple->t_data);
     946                 :     4458618 : }
     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 void heap_copytuple_with_tuple(HeapTuple src, HeapTuple dest);
     979                 :             : extern Datum heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc);
     980                 :             : extern HeapTuple heap_form_tuple(TupleDesc tupleDescriptor,
     981                 :             :                                  const Datum *values, const bool *isnull);
     982                 :             : extern HeapTuple heap_modify_tuple(HeapTuple tuple,
     983                 :             :                                    TupleDesc tupleDesc,
     984                 :             :                                    const Datum *replValues,
     985                 :             :                                    const bool *replIsnull,
     986                 :             :                                    const bool *doReplace);
     987                 :             : extern HeapTuple heap_modify_tuple_by_cols(HeapTuple tuple,
     988                 :             :                                            TupleDesc tupleDesc,
     989                 :             :                                            int nCols,
     990                 :             :                                            const int *replCols,
     991                 :             :                                            const Datum *replValues,
     992                 :             :                                            const bool *replIsnull);
     993                 :             : extern void heap_datum_check(HeapTupleHeader tup, TupleDesc tupleDesc);
     994                 :             : extern void heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc,
     995                 :             :                               Datum *values, bool *isnull);
     996                 :             : extern void heap_freetuple(HeapTuple htup);
     997                 :             : extern MinimalTuple heap_form_minimal_tuple(TupleDesc tupleDescriptor,
     998                 :             :                                             const Datum *values, const bool *isnull,
     999                 :             :                                             Size extra);
    1000                 :             : extern void heap_free_minimal_tuple(MinimalTuple mtup);
    1001                 :             : extern MinimalTuple heap_copy_minimal_tuple(MinimalTuple mtup, Size extra);
    1002                 :             : extern HeapTuple heap_tuple_from_minimal_tuple(MinimalTuple mtup);
    1003                 :             : extern MinimalTuple minimal_tuple_from_heap_tuple(HeapTuple htup, Size extra);
    1004                 :             : extern size_t varsize_any(void *p);
    1005                 :             : extern HeapTuple heap_expand_tuple(HeapTuple sourceTuple, TupleDesc tupleDesc);
    1006                 :             : extern MinimalTuple minimal_expand_tuple(HeapTuple sourceTuple, TupleDesc tupleDesc);
    1007                 :             : 
    1008                 :             : #ifndef FRONTEND
    1009                 :             : /*
    1010                 :             :  *  fastgetattr
    1011                 :             :  *      Fetch a user attribute's value as a Datum (might be either a
    1012                 :             :  *      value, or a pointer into the data area of the tuple).
    1013                 :             :  *
    1014                 :             :  *      This must not be used when a system attribute might be requested.
    1015                 :             :  *      Furthermore, the passed attnum MUST be valid.  Use heap_getattr()
    1016                 :             :  *      instead, if in doubt.
    1017                 :             :  *
    1018                 :             :  *      This gets called many times, so we macro the cacheable and NULL
    1019                 :             :  *      lookups, and call nocachegetattr() for the rest.
    1020                 :             :  */
    1021                 :             : static inline Datum
    1022                 :   200024623 : fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
    1023                 :             : {
    1024                 :             :     Assert(attnum > 0);
    1025                 :             : 
    1026                 :             :     /*
    1027                 :             :      * NB: Right now this does not call heap_datum_check(). The reasoning is
    1028                 :             :      * that
    1029                 :             :      *
    1030                 :             :      * a) fastgetattr() is used in quite performance sensitive paths like
    1031                 :             :      * catcache, adding code bloat to it would not be great
    1032                 :             :      *
    1033                 :             :      * b) The requirement that the caller already have validated attnum
    1034                 :             :      * (without that we'd run off the end of the attribute array!), basically
    1035                 :             :      * means it can only be used where the caller is sure that the type isn't
    1036                 :             :      * evolving.
    1037                 :             :      *
    1038                 :             :      * c) fastgetattr() is called from heap_getattr(), so we would often check
    1039                 :             :      * twice.
    1040                 :             :      *
    1041                 :             :      * I think that's the right decision. An investigation of extensions found
    1042                 :             :      * only two users ("rum" and "storage_engine") and both of them use
    1043                 :             :      * fastgetattr() on tables that can't be altered by non-superusers.
    1044                 :             :      *
    1045                 :             :      * While it might seem we should just assert that this is not used for
    1046                 :             :      * datum tuples, we can't - fastgetattr() is often called on a modified
    1047                 :             :      * copy of the tuple, and those are marked as datum tuples, so this would
    1048                 :             :      * spuriously trigger all the time :(.
    1049                 :             :      */
    1050                 :             : 
    1051                 :   200024623 :     *isnull = false;
    1052         [ +  + ]:   200024623 :     if (HeapTupleNoNulls(tup))
    1053                 :             :     {
    1054                 :             :         CompactAttribute *att;
    1055                 :             : 
    1056                 :    82963707 :         att = TupleDescCompactAttr(tupleDesc, attnum - 1);
    1057         [ +  + ]:    82963707 :         if (att->attcacheoff >= 0)
    1058                 :    46365319 :             return fetchatt(att, (char *) tup->t_data + tup->t_data->t_hoff +
    1059                 :             :                             att->attcacheoff);
    1060                 :             :         else
    1061                 :    36598388 :             return nocachegetattr(tup, attnum, tupleDesc);
    1062                 :             :     }
    1063                 :             :     else
    1064                 :             :     {
    1065         [ +  + ]:   117060916 :         if (att_isnull(attnum - 1, tup->t_data->t_bits))
    1066                 :             :         {
    1067                 :    15085621 :             *isnull = true;
    1068                 :    15085621 :             return (Datum) 0;
    1069                 :             :         }
    1070                 :             :         else
    1071                 :   101975295 :             return nocachegetattr(tup, attnum, tupleDesc);
    1072                 :             :     }
    1073                 :             : }
    1074                 :             : 
    1075                 :             : /*
    1076                 :             :  *  heap_getattr
    1077                 :             :  *      Extract an attribute of a heap tuple and return it as a Datum.
    1078                 :             :  *      This works for either system or user attributes.  The given attnum
    1079                 :             :  *      is properly range-checked.
    1080                 :             :  *
    1081                 :             :  *      If the field in question has a NULL value, we return a zero Datum
    1082                 :             :  *      and set *isnull == true.  Otherwise, we set *isnull == false.
    1083                 :             :  *
    1084                 :             :  *      <tup> is the pointer to the heap tuple.  <attnum> is the attribute
    1085                 :             :  *      number of the column (field) caller wants.  <tupleDesc> is a
    1086                 :             :  *      pointer to the structure describing the row and all its fields.
    1087                 :             :  *
    1088                 :             :  */
    1089                 :             : static inline Datum
    1090                 :   187402237 : heap_getattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
    1091                 :             : {
    1092                 :             :     /*
    1093                 :             :      * It's a bit sad to have an external function call here, but we shouldn't
    1094                 :             :      * use it in too many speed critical paths anymore. Separately, the
    1095                 :             :      * function looks like it's getting too complicated to be worth inlining.
    1096                 :             :      */
    1097         [ +  + ]:   187402237 :     if (HeapTupleHeaderIsVersionedDatum(tup->t_data))
    1098                 :     1005139 :         heap_datum_check(tup->t_data, tupleDesc);
    1099                 :             : 
    1100         [ +  - ]:   187402220 :     if (attnum > 0)
    1101                 :             :     {
    1102         [ +  + ]:   187402220 :         if (attnum > (int) HeapTupleHeaderGetNatts(tup->t_data))
    1103                 :         249 :             return getmissingattr(tupleDesc, attnum, isnull);
    1104                 :             :         else
    1105                 :   187401971 :             return fastgetattr(tup, attnum, tupleDesc, isnull);
    1106                 :             :     }
    1107                 :             :     else
    1108                 :           0 :         return heap_getsysattr(tup, attnum, tupleDesc, isnull);
    1109                 :             : }
    1110                 :             : #endif                          /* FRONTEND */
    1111                 :             : 
    1112                 :             : #endif                          /* HTUP_DETAILS_H */
        

Generated by: LCOV version 2.0-1