LCOV - code coverage report
Current view: top level - src/backend/access/common - heaptuple.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 93.7 % 527 494
Test Date: 2026-11-15 11:16:13 Functions: 97.2 % 36 35
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 74.5 % 326 243

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * heaptuple.c
       4                 :             :  *    This file contains heap tuple accessor and mutator routines, as well
       5                 :             :  *    as various tuple utilities.
       6                 :             :  *
       7                 :             :  * Some notes about varlenas and this code:
       8                 :             :  *
       9                 :             :  * Before Postgres 8.3 varlenas always had a 4-byte length header, and
      10                 :             :  * therefore always needed 4-byte alignment (at least).  This wasted space
      11                 :             :  * for short varlenas, for example CHAR(1) took 5 bytes and could need up to
      12                 :             :  * 3 additional padding bytes for alignment.
      13                 :             :  *
      14                 :             :  * Now, a short varlena (up to 126 data bytes) is reduced to a 1-byte header
      15                 :             :  * and we don't align it.  To hide this from datatype-specific functions that
      16                 :             :  * don't want to deal with it, such a datum is considered "toasted" and will
      17                 :             :  * be expanded back to the normal 4-byte-header format by pg_detoast_datum.
      18                 :             :  * (In performance-critical code paths we can use pg_detoast_datum_packed
      19                 :             :  * and the appropriate access macros to avoid that overhead.)  Note that this
      20                 :             :  * conversion is performed directly in heap_form_tuple, without invoking
      21                 :             :  * heaptoast.c.
      22                 :             :  *
      23                 :             :  * This change will break any code that assumes it needn't detoast values
      24                 :             :  * that have been put into a tuple but never sent to disk.  Hopefully there
      25                 :             :  * are few such places.
      26                 :             :  *
      27                 :             :  * Varlenas still have alignment INT (or DOUBLE) in pg_type/pg_attribute, since
      28                 :             :  * that's the normal requirement for the untoasted format.  But we ignore that
      29                 :             :  * for the 1-byte-header format.  This means that the actual start position
      30                 :             :  * of a varlena datum may vary depending on which format it has.  To determine
      31                 :             :  * what is stored, we have to require that alignment padding bytes be zero.
      32                 :             :  * (Postgres actually has always zeroed them, but now it's required!)  Since
      33                 :             :  * the first byte of a 1-byte-header varlena can never be zero, we can examine
      34                 :             :  * the first byte after the previous datum to tell if it's a pad byte or the
      35                 :             :  * start of a 1-byte-header varlena.
      36                 :             :  *
      37                 :             :  * Note that while formerly we could rely on the first varlena column of a
      38                 :             :  * system catalog to be at the offset suggested by the C struct for the
      39                 :             :  * catalog, this is now risky: it's only safe if the preceding field is
      40                 :             :  * word-aligned, so that there will never be any padding.
      41                 :             :  *
      42                 :             :  * We don't pack varlenas whose attstorage is PLAIN, since the data type
      43                 :             :  * isn't expecting to have to detoast values.  This is used in particular
      44                 :             :  * by oidvector and int2vector, which are used in the system catalogs
      45                 :             :  * and we'd like to still refer to them via C struct offsets.
      46                 :             :  *
      47                 :             :  *
      48                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
      49                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
      50                 :             :  *
      51                 :             :  *
      52                 :             :  * IDENTIFICATION
      53                 :             :  *    src/backend/access/common/heaptuple.c
      54                 :             :  *
      55                 :             :  *-------------------------------------------------------------------------
      56                 :             :  */
      57                 :             : 
      58                 :             : #include "postgres.h"
      59                 :             : 
      60                 :             : #include "access/heaptoast.h"
      61                 :             : #include "access/sysattr.h"
      62                 :             : #include "access/tupdesc_details.h"
      63                 :             : #include "access/xlogdefs.h"
      64                 :             : #include "catalog/pg_type_d.h"
      65                 :             : #include "common/hashfn.h"
      66                 :             : #include "common/int.h"
      67                 :             : #include "utils/builtins.h"
      68                 :             : #include "utils/datum.h"
      69                 :             : #include "utils/expandeddatum.h"
      70                 :             : #include "utils/hsearch.h"
      71                 :             : #include "utils/memutils.h"
      72                 :             : #include "utils/typcache.h"
      73                 :             : 
      74                 :             : 
      75                 :             : /*
      76                 :             :  * Does att's datatype allow packing into the 1-byte-header varlena format?
      77                 :             :  * While functions that use TupleDescAttr() and assign attstorage =
      78                 :             :  * TYPSTORAGE_PLAIN cannot use packed varlena headers, functions that call
      79                 :             :  * TupleDescInitEntry() use typeForm->typstorage (TYPSTORAGE_EXTENDED) and
      80                 :             :  * can use packed varlena headers, e.g.:
      81                 :             :  *     CREATE TABLE test(a VARCHAR(10000) STORAGE PLAIN);
      82                 :             :  *     INSERT INTO test VALUES (repeat('A',10));
      83                 :             :  * This can be verified with pageinspect.
      84                 :             :  */
      85                 :             : #define ATT_IS_PACKABLE(att) \
      86                 :             :     ((att)->attlen == -1 && (att)->attstorage != TYPSTORAGE_PLAIN)
      87                 :             : /* Use this if it's already known varlena */
      88                 :             : #define VARLENA_ATT_IS_PACKABLE(att) \
      89                 :             :     ((att)->attstorage != TYPSTORAGE_PLAIN)
      90                 :             : 
      91                 :             : /* FormData_pg_attribute.attstorage != TYPSTORAGE_PLAIN and an attlen of -1 */
      92                 :             : #define COMPACT_ATTR_IS_PACKABLE(att) \
      93                 :             :     ((att)->attlen == -1 && (att)->attispackable)
      94                 :             : 
      95                 :             : 
      96                 :             : static void heap_datum_check_coercion(HeapTupleHeader tup, TupleDesc targetDesc);
      97                 :             : 
      98                 :             : 
      99                 :             : /* GUC variable */
     100                 :             : int         composite_type_mismatch = COMPOSITE_TYPE_MISMATCH_ERROR;
     101                 :             : 
     102                 :             : 
     103                 :             : /*
     104                 :             :  * Setup for caching pass-by-ref missing attributes in a way that survives
     105                 :             :  * tupleDesc destruction.
     106                 :             :  */
     107                 :             : 
     108                 :             : typedef struct
     109                 :             : {
     110                 :             :     int         len;
     111                 :             :     Datum       value;
     112                 :             : } missing_cache_key;
     113                 :             : 
     114                 :             : static HTAB *missing_cache = NULL;
     115                 :             : 
     116                 :             : static uint32
     117                 :          21 : missing_hash(const void *key, Size keysize)
     118                 :             : {
     119                 :          21 :     const missing_cache_key *entry = key;
     120                 :             : 
     121                 :          21 :     return hash_bytes((const unsigned char *) DatumGetPointer(entry->value), entry->len);
     122                 :             : }
     123                 :             : 
     124                 :             : static int
     125                 :          15 : missing_match(const void *key1, const void *key2, Size keysize)
     126                 :             : {
     127                 :          15 :     const missing_cache_key *entry1 = key1;
     128                 :          15 :     const missing_cache_key *entry2 = key2;
     129                 :             : 
     130         [ -  + ]:          15 :     if (entry1->len != entry2->len)
     131         [ #  # ]:           0 :         return entry1->len > entry2->len ? 1 : -1;
     132                 :             : 
     133                 :          15 :     return memcmp(DatumGetPointer(entry1->value),
     134                 :          15 :                   DatumGetPointer(entry2->value),
     135                 :          15 :                   entry1->len);
     136                 :             : }
     137                 :             : 
     138                 :             : static void
     139                 :           6 : init_missing_cache(void)
     140                 :             : {
     141                 :             :     HASHCTL     hash_ctl;
     142                 :             : 
     143                 :           6 :     hash_ctl.keysize = sizeof(missing_cache_key);
     144                 :           6 :     hash_ctl.entrysize = sizeof(missing_cache_key);
     145                 :           6 :     hash_ctl.hcxt = TopMemoryContext;
     146                 :           6 :     hash_ctl.hash = missing_hash;
     147                 :           6 :     hash_ctl.match = missing_match;
     148                 :           6 :     missing_cache =
     149                 :           6 :         hash_create("Missing Values Cache",
     150                 :             :                     32,
     151                 :             :                     &hash_ctl,
     152                 :             :                     HASH_ELEM | HASH_CONTEXT | HASH_FUNCTION | HASH_COMPARE);
     153                 :           6 : }
     154                 :             : 
     155                 :             : /* ----------------------------------------------------------------
     156                 :             :  *                      misc support routines
     157                 :             :  * ----------------------------------------------------------------
     158                 :             :  */
     159                 :             : 
     160                 :             : /*
     161                 :             :  * Return the missing value of an attribute, or NULL if there isn't one.
     162                 :             :  */
     163                 :             : Datum
     164                 :        2883 : getmissingattr(TupleDesc tupleDesc,
     165                 :             :                int attnum, bool *isnull)
     166                 :             : {
     167                 :             :     CompactAttribute *att;
     168                 :             : 
     169                 :             :     Assert(attnum <= tupleDesc->natts);
     170                 :             :     Assert(attnum > 0);
     171                 :             : 
     172                 :        2883 :     att = TupleDescCompactAttr(tupleDesc, attnum - 1);
     173                 :             : 
     174         [ +  + ]:        2883 :     if (att->atthasmissing)
     175                 :             :     {
     176                 :             :         AttrMissing *attrmiss;
     177                 :             : 
     178                 :             :         Assert(tupleDesc->constr);
     179                 :             :         Assert(tupleDesc->constr->missing);
     180                 :             : 
     181                 :         104 :         attrmiss = tupleDesc->constr->missing + (attnum - 1);
     182                 :             : 
     183         [ +  - ]:         104 :         if (attrmiss->am_present)
     184                 :             :         {
     185                 :             :             missing_cache_key key;
     186                 :             :             missing_cache_key *entry;
     187                 :             :             bool        found;
     188                 :             :             MemoryContext oldctx;
     189                 :             : 
     190                 :         104 :             *isnull = false;
     191                 :             : 
     192                 :             :             /* no  need to cache by-value attributes */
     193         [ +  + ]:         104 :             if (att->attbyval)
     194                 :          83 :                 return attrmiss->am_value;
     195                 :             : 
     196                 :             :             /* set up cache if required */
     197         [ +  + ]:          21 :             if (missing_cache == NULL)
     198                 :           6 :                 init_missing_cache();
     199                 :             : 
     200                 :             :             /* check if there's a cache entry */
     201                 :             :             Assert(att->attlen > 0 || att->attlen == -1);
     202         [ -  + ]:          21 :             if (att->attlen > 0)
     203                 :           0 :                 key.len = att->attlen;
     204                 :             :             else
     205                 :          21 :                 key.len = VARSIZE_ANY(DatumGetPointer(attrmiss->am_value));
     206                 :          21 :             key.value = attrmiss->am_value;
     207                 :             : 
     208                 :          21 :             entry = hash_search(missing_cache, &key, HASH_ENTER, &found);
     209                 :             : 
     210         [ +  + ]:          21 :             if (!found)
     211                 :             :             {
     212                 :             :                 /* cache miss, so we need a non-transient copy of the datum */
     213                 :           6 :                 oldctx = MemoryContextSwitchTo(TopMemoryContext);
     214                 :           6 :                 entry->value =
     215                 :           6 :                     datumCopy(attrmiss->am_value, false, att->attlen);
     216                 :           6 :                 MemoryContextSwitchTo(oldctx);
     217                 :             :             }
     218                 :             : 
     219                 :          21 :             return entry->value;
     220                 :             :         }
     221                 :             :     }
     222                 :             : 
     223                 :        2779 :     *isnull = true;
     224                 :        2779 :     return PointerGetDatum(NULL);
     225                 :             : }
     226                 :             : 
     227                 :             : /*
     228                 :             :  * heap_compute_data_size
     229                 :             :  *      Determine size of the data area of a tuple to be constructed,
     230                 :             :  *      potentially over MaxAllocSize
     231                 :             :  *
     232                 :             :  * If allow_huge is false, then we raise an error if any intermediate
     233                 :             :  * result goes beyond MaxAllocSize; in 32-bit architectures we also
     234                 :             :  * do that when the result of an addition overflows Size (which is 32-bit)
     235                 :             :  */
     236                 :             : Size
     237                 :    80591336 : heap_compute_data_size(TupleDesc tupleDesc,
     238                 :             :                        const Datum *values,
     239                 :             :                        const bool *isnull,
     240                 :             :                        bool allow_huge)
     241                 :             : {
     242                 :    80591336 :     Size        data_length = 0;
     243                 :    80591336 :     int         numberOfAttributes = tupleDesc->natts;
     244                 :             : 
     245         [ +  + ]:   272430170 :     for (int i = 0; i < numberOfAttributes; i++)
     246                 :             :     {
     247                 :             :         Datum       val;
     248                 :             :         CompactAttribute *atti;
     249                 :             :         bool        ovfl;
     250                 :             : 
     251         [ +  + ]:   191838834 :         if (isnull[i])
     252                 :    17018378 :             continue;
     253                 :             : 
     254                 :   174820456 :         val = values[i];
     255                 :   174820456 :         atti = TupleDescCompactAttr(tupleDesc, i);
     256                 :             : 
     257   [ +  +  +  +  :   197937140 :         if (COMPACT_ATTR_IS_PACKABLE(atti) &&
                   +  + ]
     258                 :    23116684 :             VARATT_CAN_MAKE_SHORT(DatumGetPointer(val)))
     259                 :             :         {
     260                 :             :             /*
     261                 :             :              * we're anticipating converting to a short varlena header, so
     262                 :             :              * adjust length and don't count any alignment
     263                 :             :              */
     264                 :    18146414 :             ovfl = pg_add_size_overflow(data_length,
     265                 :    18146414 :                                         VARATT_CONVERTED_SHORT_SIZE(DatumGetPointer(val)),
     266                 :             :                                         &data_length);
     267                 :             :         }
     268   [ +  +  +  + ]:   162820218 :         else if (atti->attlen == -1 &&
     269                 :     6146176 :                  VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(val)))
     270                 :             :         {
     271                 :             :             /*
     272                 :             :              * we want to flatten the expanded value so that the constructed
     273                 :             :              * tuple doesn't depend on it
     274                 :             :              */
     275                 :        1883 :             data_length = att_nominal_alignby(data_length, atti->attalignby);
     276                 :        1883 :             ovfl = pg_add_size_overflow(data_length,
     277                 :             :                                         EOH_get_flat_size(DatumGetEOHP(val)),
     278                 :             :                                         &data_length);
     279                 :             :         }
     280                 :             :         else
     281                 :             :         {
     282   [ +  +  +  + ]:   156672159 :             data_length = att_datum_alignby(data_length, atti->attalignby,
     283                 :             :                                             atti->attlen, val);
     284                 :   156672159 :             data_length = att_addlength_datum_overflow(data_length, atti->attlen,
     285                 :             :                                                        val, &ovfl);
     286                 :             :         }
     287                 :             : 
     288                 :             :         /*
     289                 :             :          * Unless caller allows huge values, prevent going beyond the palloc
     290                 :             :          * limit.  In cases where caller does allow huge values, we can't
     291                 :             :          * trust the AllocSizeIsValid() test alone on 32-bit platforms, which
     292                 :             :          * is why we use overflow-detecting addition above.
     293                 :             :          */
     294   [ +  -  +  +  :   174820456 :         if (ovfl || (!allow_huge && !AllocSizeIsValid(data_length)))
                   -  + ]
     295         [ #  # ]:           0 :             ereport(ERROR,
     296                 :             :                     errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     297                 :             :                     errmsg("tuple is too big"));
     298                 :             :     }
     299                 :             : 
     300                 :    80591336 :     return data_length;
     301                 :             : }
     302                 :             : 
     303                 :             : /*
     304                 :             :  * Per-attribute helper for heap_fill_tuple and other routines building tuples.
     305                 :             :  *
     306                 :             :  * Fill in either a data value or a bit in the null bitmask
     307                 :             :  */
     308                 :             : static inline void
     309                 :   177329908 : fill_val(CompactAttribute *att,
     310                 :             :          uint8 **bit,
     311                 :             :          int *bitmask,
     312                 :             :          char **dataP,
     313                 :             :          uint16 *infomask,
     314                 :             :          Datum datum,
     315                 :             :          bool isnull)
     316                 :             : {
     317                 :             :     Size        data_length;
     318                 :   177329908 :     char       *data = *dataP;
     319                 :             : 
     320                 :             :     /*
     321                 :             :      * If we're building a null bitmap, set the appropriate bit for the
     322                 :             :      * current column value here.
     323                 :             :      */
     324         [ +  + ]:   177329908 :     if (bit != NULL)
     325                 :             :     {
     326         [ +  + ]:    62534947 :         if (*bitmask != HIGHBIT)
     327                 :    52038139 :             *bitmask <<= 1;
     328                 :             :         else
     329                 :             :         {
     330                 :    10496808 :             *bit += 1;
     331                 :    10496808 :             **bit = 0x0;
     332                 :    10496808 :             *bitmask = 1;
     333                 :             :         }
     334                 :             : 
     335         [ +  + ]:    62534947 :         if (isnull)
     336                 :             :         {
     337                 :    16863040 :             *infomask |= HEAP_HASNULL;
     338                 :    16863040 :             return;
     339                 :             :         }
     340                 :             : 
     341                 :    45671907 :         **bit |= *bitmask;
     342                 :             :     }
     343                 :             : 
     344                 :             :     /*
     345                 :             :      * XXX we use the att_nominal_alignby macro on the pointer value itself,
     346                 :             :      * not on an offset.  This is a bit of a hack.
     347                 :             :      */
     348         [ +  + ]:   160466868 :     if (att->attbyval)
     349                 :             :     {
     350                 :             :         /* pass-by-value */
     351                 :   122091681 :         data = (char *) att_nominal_alignby(data, att->attalignby);
     352                 :   122091681 :         store_att_byval(data, datum, att->attlen);
     353                 :   122091681 :         data_length = att->attlen;
     354                 :             :     }
     355         [ +  + ]:    38375187 :     else if (att->attlen == -1)
     356                 :             :     {
     357                 :             :         /* varlena */
     358                 :    23258818 :         Pointer     val = DatumGetPointer(datum);
     359                 :             : 
     360                 :    23258818 :         *infomask |= HEAP_HASVARWIDTH;
     361         [ +  + ]:    23258818 :         if (VARATT_IS_EXTERNAL(val))
     362                 :             :         {
     363         [ +  + ]:       14308 :             if (VARATT_IS_EXTERNAL_EXPANDED(val))
     364                 :             :             {
     365                 :             :                 /*
     366                 :             :                  * we want to flatten the expanded value so that the
     367                 :             :                  * constructed tuple doesn't depend on it
     368                 :             :                  */
     369                 :        1883 :                 ExpandedObjectHeader *eoh = DatumGetEOHP(datum);
     370                 :             : 
     371                 :        1883 :                 data = (char *) att_nominal_alignby(data, att->attalignby);
     372                 :        1883 :                 data_length = EOH_get_flat_size(eoh);
     373                 :        1883 :                 EOH_flatten_into(eoh, data, data_length);
     374                 :             :             }
     375                 :             :             else
     376                 :             :             {
     377                 :       12425 :                 *infomask |= HEAP_HASEXTERNAL;
     378                 :             :                 /* no alignment, since it's short by definition */
     379                 :       12425 :                 data_length = VARSIZE_EXTERNAL(val);
     380                 :       12425 :                 memcpy(data, val, data_length);
     381                 :             :             }
     382                 :             :         }
     383         [ +  + ]:    23244510 :         else if (VARATT_IS_SHORT(val))
     384                 :             :         {
     385                 :             :             /* no alignment for short varlenas */
     386                 :     3877098 :             data_length = VARSIZE_SHORT(val);
     387                 :     3877098 :             memcpy(data, val, data_length);
     388                 :             :         }
     389   [ +  +  +  + ]:    19367412 :         else if (att->attispackable && VARATT_CAN_MAKE_SHORT(val))
     390                 :             :         {
     391                 :             :             /* convert to short varlena -- no alignment */
     392                 :    17531909 :             data_length = VARATT_CONVERTED_SHORT_SIZE(val);
     393                 :    17531909 :             SET_VARSIZE_SHORT(data, data_length);
     394                 :    17531909 :             memcpy(data + 1, VARDATA(val), data_length - 1);
     395                 :             :         }
     396                 :             :         else
     397                 :             :         {
     398                 :             :             /* full 4-byte header varlena */
     399                 :     1835503 :             data = (char *) att_nominal_alignby(data, att->attalignby);
     400                 :     1835503 :             data_length = VARSIZE(val);
     401                 :     1835503 :             memcpy(data, val, data_length);
     402                 :             :         }
     403                 :             :     }
     404         [ +  + ]:    15116369 :     else if (att->attlen == -2)
     405                 :             :     {
     406                 :             :         /* cstring ... never needs alignment */
     407                 :     1579143 :         *infomask |= HEAP_HASVARWIDTH;
     408                 :             :         Assert(att->attalignby == sizeof(char));
     409                 :     1579143 :         data_length = strlen(DatumGetCString(datum)) + 1;
     410                 :     1579143 :         memcpy(data, DatumGetPointer(datum), data_length);
     411                 :             :     }
     412                 :             :     else
     413                 :             :     {
     414                 :             :         /* fixed-length pass-by-reference */
     415                 :    13537226 :         data = (char *) att_nominal_alignby(data, att->attalignby);
     416                 :             :         Assert(att->attlen > 0);
     417                 :    13537226 :         data_length = att->attlen;
     418                 :    13537226 :         memcpy(data, DatumGetPointer(datum), data_length);
     419                 :             :     }
     420                 :             : 
     421                 :   160466868 :     data += data_length;
     422                 :   160466868 :     *dataP = data;
     423                 :             : }
     424                 :             : 
     425                 :             : /*
     426                 :             :  * heap_fill_tuple
     427                 :             :  *      Load data portion of a tuple from values/isnull arrays
     428                 :             :  *
     429                 :             :  * We also fill the null bitmap (if any) and set the infomask bits
     430                 :             :  * that reflect the tuple's data contents.
     431                 :             :  *
     432                 :             :  * NOTE: it is now REQUIRED that the caller have pre-zeroed the data area.
     433                 :             :  */
     434                 :             : void
     435                 :    67481042 : heap_fill_tuple(TupleDesc tupleDesc,
     436                 :             :                 const Datum *values, const bool *isnull,
     437                 :             :                 char *data, Size data_size,
     438                 :             :                 uint16 *infomask, uint8 *bit)
     439                 :             : {
     440                 :             :     uint8      *bitP;
     441                 :             :     int         bitmask;
     442                 :             :     int         i;
     443                 :    67481042 :     int         numberOfAttributes = tupleDesc->natts;
     444                 :             : 
     445                 :             : #ifdef USE_ASSERT_CHECKING
     446                 :             :     char       *start = data;
     447                 :             : #endif
     448                 :             : 
     449         [ +  + ]:    67481042 :     if (bit != NULL)
     450                 :             :     {
     451                 :     4361413 :         bitP = &bit[-1];
     452                 :     4361413 :         bitmask = HIGHBIT;
     453                 :             :     }
     454                 :             :     else
     455                 :             :     {
     456                 :             :         /* just to keep compiler quiet */
     457                 :    63119629 :         bitP = NULL;
     458                 :    63119629 :         bitmask = 0;
     459                 :             :     }
     460                 :             : 
     461                 :    67481042 :     *infomask &= ~(HEAP_HASNULL | HEAP_HASVARWIDTH | HEAP_HASEXTERNAL);
     462                 :             : 
     463         [ +  + ]:   244810950 :     for (i = 0; i < numberOfAttributes; i++)
     464                 :             :     {
     465                 :   177329908 :         CompactAttribute *attr = TupleDescCompactAttr(tupleDesc, i);
     466                 :             : 
     467   [ +  -  +  + ]:   531989724 :         fill_val(attr,
     468         [ +  + ]:   177329908 :                  bitP ? &bitP : NULL,
     469                 :             :                  &bitmask,
     470                 :             :                  &data,
     471                 :             :                  infomask,
     472                 :   177329562 :                  values ? values[i] : PointerGetDatum(NULL),
     473                 :   177329908 :                  isnull ? isnull[i] : true);
     474                 :             :     }
     475                 :             : 
     476                 :             :     Assert((data - start) == data_size);
     477                 :    67481042 : }
     478                 :             : 
     479                 :             : 
     480                 :             : /* ----------------------------------------------------------------
     481                 :             :  *                      heap tuple interface
     482                 :             :  * ----------------------------------------------------------------
     483                 :             :  */
     484                 :             : 
     485                 :             : /* ----------------
     486                 :             :  *      heap_attisnull  - returns true iff tuple attribute is not present
     487                 :             :  * ----------------
     488                 :             :  */
     489                 :             : bool
     490                 :     6991118 : heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
     491                 :             : {
     492                 :             :     /*
     493                 :             :      * We allow a NULL tupledesc for relations not expected to have missing
     494                 :             :      * values, such as catalog relations and indexes.
     495                 :             :      */
     496                 :             :     Assert(!tupleDesc || attnum <= tupleDesc->natts);
     497         [ -  + ]:     6991118 :     if (attnum > (int) HeapTupleHeaderGetNatts(tup->t_data))
     498                 :             :     {
     499   [ #  #  #  # ]:           0 :         if (tupleDesc &&
     500                 :           0 :             TupleDescCompactAttr(tupleDesc, attnum - 1)->atthasmissing)
     501                 :           0 :             return false;
     502                 :             :         else
     503                 :           0 :             return true;
     504                 :             :     }
     505                 :             : 
     506         [ +  - ]:     6991118 :     if (attnum > 0)
     507                 :             :     {
     508         [ +  + ]:     6991118 :         if (HeapTupleNoNulls(tup))
     509                 :        1998 :             return false;
     510                 :     6989120 :         return att_isnull(attnum - 1, tup->t_data->t_bits);
     511                 :             :     }
     512                 :             : 
     513         [ #  # ]:           0 :     switch (attnum)
     514                 :             :     {
     515                 :           0 :         case TableOidAttributeNumber:
     516                 :             :         case SelfItemPointerAttributeNumber:
     517                 :             :         case MinTransactionIdAttributeNumber:
     518                 :             :         case MinCommandIdAttributeNumber:
     519                 :             :         case MaxTransactionIdAttributeNumber:
     520                 :             :         case MaxCommandIdAttributeNumber:
     521                 :             :             /* these are never null */
     522                 :           0 :             break;
     523                 :             : 
     524                 :           0 :         default:
     525         [ #  # ]:           0 :             elog(ERROR, "invalid attnum: %d", attnum);
     526                 :             :     }
     527                 :             : 
     528                 :           0 :     return false;
     529                 :             : }
     530                 :             : 
     531                 :             : /* ----------------
     532                 :             :  *      nocachegetattr
     533                 :             :  *
     534                 :             :  *      This only gets called from fastgetattr(), in cases where we
     535                 :             :  *      can't use the attcacheoff and the value is not null.
     536                 :             :  *
     537                 :             :  *      NOTE: if you need to change this code, see also heap_deform_tuple.
     538                 :             :  *      Also see nocache_index_getattr, which is the same code for index
     539                 :             :  *      tuples.
     540                 :             :  * ----------------
     541                 :             :  */
     542                 :             : Datum
     543                 :   134476421 : nocachegetattr(HeapTuple tup,
     544                 :             :                int attnum,
     545                 :             :                TupleDesc tupleDesc)
     546                 :             : {
     547                 :             :     CompactAttribute *cattr;
     548                 :   134476421 :     HeapTupleHeader td = tup->t_data;
     549                 :             :     char       *tp;             /* ptr to data part of tuple */
     550                 :   134476421 :     uint8      *bp = td->t_bits; /* ptr to null bitmap in tuple */
     551                 :             :     Size        off;            /* current offset within data */
     552                 :             :     int         startAttr;
     553                 :             :     int         firstNullAttr;
     554                 :             :     int         i;
     555                 :   134476421 :     bool        hasnulls = HeapTupleHasNulls(tup);
     556                 :             : 
     557                 :             :     /*
     558                 :             :      * It shouldn't be possible to allocate overlength tuples, so it's not
     559                 :             :      * possible to get in wraparound problems in the arithmetic below.
     560                 :             :      */
     561                 :             :     Assert(AllocSizeIsValid(tup->t_len));
     562                 :             : 
     563                 :             :     /* Did someone forget to call TupleDescFinalize()? */
     564                 :             :     Assert(tupleDesc->firstNonCachedOffsetAttr >= 0);
     565                 :             : 
     566                 :   134476421 :     attnum--;
     567                 :             : 
     568                 :             :     /*
     569                 :             :      * To minimize the number of attributes we need to look at, start walking
     570                 :             :      * the tuple at the attribute with the highest attcacheoff prior to attnum
     571                 :             :      * or the first NULL attribute prior to attnum, whichever comes first.
     572                 :             :      */
     573         [ +  + ]:   134476421 :     if (hasnulls)
     574                 :    97930037 :         firstNullAttr = first_null_attr(bp, attnum);
     575                 :             :     else
     576                 :    36546384 :         firstNullAttr = attnum;
     577                 :             : 
     578   [ +  +  +  + ]:   134476421 :     if (tupleDesc->firstNonCachedOffsetAttr > 0 && firstNullAttr > 0)
     579                 :             :     {
     580                 :             :         /*
     581                 :             :          * Try to start with the highest attribute with an attcacheoff that's
     582                 :             :          * prior to the one we're looking for, or with the attribute prior to
     583                 :             :          * the first NULL attribute, if there is one.
     584                 :             :          */
     585                 :    46163467 :         startAttr = Min(tupleDesc->firstNonCachedOffsetAttr - 1, firstNullAttr - 1);
     586                 :    46163467 :         off = TupleDescCompactAttr(tupleDesc, startAttr)->attcacheoff;
     587                 :             :     }
     588                 :             :     else
     589                 :             :     {
     590                 :             :         /* Otherwise, start at the beginning... */
     591                 :    88312954 :         startAttr = 0;
     592                 :    88312954 :         off = 0;
     593                 :             :     }
     594                 :             : 
     595                 :   134476421 :     tp = (char *) td + td->t_hoff;
     596                 :             : 
     597                 :             :     /*
     598                 :             :      * Calculate 'off' up to the first NULL attr.  We use two cheaper loops
     599                 :             :      * when the tuple has no variable-width columns.  When variable-width
     600                 :             :      * columns exists, we use att_addlength_pointer() to move the offset
     601                 :             :      * beyond the current attribute.
     602                 :             :      */
     603         [ +  + ]:   134476421 :     if (!HeapTupleHasVarWidth(tup))
     604                 :             :     {
     605         [ +  + ]:    97771312 :         for (i = startAttr; i < firstNullAttr; i++)
     606                 :             :         {
     607                 :    24820602 :             cattr = TupleDescCompactAttr(tupleDesc, i);
     608                 :             : 
     609                 :    24820602 :             off = att_nominal_alignby(off, cattr->attalignby);
     610                 :    24820602 :             off += cattr->attlen;
     611                 :             :         }
     612                 :             : 
     613         [ +  + ]:    77017097 :         for (; i < attnum; i++)
     614                 :             :         {
     615         [ +  + ]:     4066387 :             if (att_isnull(i, bp))
     616                 :     3321428 :                 continue;
     617                 :             : 
     618                 :      744959 :             cattr = TupleDescCompactAttr(tupleDesc, i);
     619                 :             : 
     620                 :      744959 :             off = att_nominal_alignby(off, cattr->attalignby);
     621                 :      744959 :             off += cattr->attlen;
     622                 :             :         }
     623                 :             :     }
     624                 :             :     else
     625                 :             :     {
     626         [ +  + ]:   155415768 :         for (i = startAttr; i < firstNullAttr; i++)
     627                 :             :         {
     628                 :             :             int         attlen;
     629                 :             : 
     630                 :    93890057 :             cattr = TupleDescCompactAttr(tupleDesc, i);
     631                 :    93890057 :             attlen = cattr->attlen;
     632   [ +  +  +  + ]:    93890057 :             off = att_pointer_alignby(off,
     633                 :             :                                       cattr->attalignby,
     634                 :             :                                       attlen,
     635                 :             :                                       tp + off);
     636   [ +  +  +  - ]:    93890057 :             off = att_addlength_pointer(off, attlen, tp + off);
     637                 :             :         }
     638                 :             : 
     639         [ +  + ]:    72925291 :         for (; i < attnum; i++)
     640                 :             :         {
     641                 :             :             int         attlen;
     642                 :             : 
     643         [ +  + ]:    11399580 :             if (att_isnull(i, bp))
     644                 :     8513529 :                 continue;
     645                 :             : 
     646                 :     2886051 :             cattr = TupleDescCompactAttr(tupleDesc, i);
     647                 :     2886051 :             attlen = cattr->attlen;
     648   [ +  +  +  + ]:     2886051 :             off = att_pointer_alignby(off, cattr->attalignby, attlen,
     649                 :             :                                       tp + off);
     650   [ +  +  +  - ]:     2886051 :             off = att_addlength_pointer(off, attlen, tp + off);
     651                 :             :         }
     652                 :             :     }
     653                 :             : 
     654                 :   134476421 :     cattr = TupleDescCompactAttr(tupleDesc, attnum);
     655   [ +  +  +  + ]:   134476421 :     off = att_pointer_alignby(off,
     656                 :             :                               cattr->attalignby,
     657                 :             :                               cattr->attlen,
     658                 :             :                               tp + off);
     659                 :             : 
     660                 :   134476421 :     return fetchatt(cattr, tp + off);
     661                 :             : }
     662                 :             : 
     663                 :             : /* ----------------
     664                 :             :  *      heap_getsysattr
     665                 :             :  *
     666                 :             :  *      Fetch the value of a system attribute for a tuple.
     667                 :             :  *
     668                 :             :  * This is a support routine for heap_getattr().  The function has already
     669                 :             :  * determined that the attnum refers to a system attribute.
     670                 :             :  * ----------------
     671                 :             :  */
     672                 :             : Datum
     673                 :       72864 : heap_getsysattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
     674                 :             : {
     675                 :             :     Datum       result;
     676                 :             : 
     677                 :             :     Assert(tup);
     678                 :             : 
     679                 :             :     /* Currently, no sys attribute ever reads as NULL. */
     680                 :       72864 :     *isnull = false;
     681                 :             : 
     682   [ +  +  +  +  :       72864 :     switch (attnum)
                   +  - ]
     683                 :             :     {
     684                 :          17 :         case SelfItemPointerAttributeNumber:
     685                 :             :             /* pass-by-reference datatype */
     686                 :          17 :             result = PointerGetDatum(&(tup->t_self));
     687                 :          17 :             break;
     688                 :       72601 :         case MinTransactionIdAttributeNumber:
     689                 :       72601 :             result = TransactionIdGetDatum(HeapTupleHeaderGetRawXmin(tup->t_data));
     690                 :       72601 :             break;
     691                 :         119 :         case MaxTransactionIdAttributeNumber:
     692                 :         119 :             result = TransactionIdGetDatum(HeapTupleHeaderGetRawXmax(tup->t_data));
     693                 :         119 :             break;
     694                 :         124 :         case MinCommandIdAttributeNumber:
     695                 :             :         case MaxCommandIdAttributeNumber:
     696                 :             : 
     697                 :             :             /*
     698                 :             :              * cmin and cmax are now both aliases for the same field, which
     699                 :             :              * can in fact also be a combo command id.  XXX perhaps we should
     700                 :             :              * return the "real" cmin or cmax if possible, that is if we are
     701                 :             :              * inside the originating transaction?
     702                 :             :              */
     703                 :         124 :             result = CommandIdGetDatum(HeapTupleHeaderGetRawCommandId(tup->t_data));
     704                 :         124 :             break;
     705                 :           3 :         case TableOidAttributeNumber:
     706                 :           3 :             result = ObjectIdGetDatum(tup->t_tableOid);
     707                 :           3 :             break;
     708                 :           0 :         default:
     709         [ #  # ]:           0 :             elog(ERROR, "invalid attnum: %d", attnum);
     710                 :             :             result = 0;         /* keep compiler quiet */
     711                 :             :             break;
     712                 :             :     }
     713                 :       72864 :     return result;
     714                 :             : }
     715                 :             : 
     716                 :             : /* ----------------
     717                 :             :  *      heap_copytuple
     718                 :             :  *
     719                 :             :  *      returns a copy of an entire tuple
     720                 :             :  *
     721                 :             :  * The HeapTuple struct, tuple header, and tuple data are all allocated
     722                 :             :  * as a single palloc() block.
     723                 :             :  * ----------------
     724                 :             :  */
     725                 :             : HeapTuple
     726                 :    11172190 : heap_copytuple(HeapTuple tuple)
     727                 :             : {
     728                 :             :     HeapTuple   newTuple;
     729                 :             : 
     730   [ +  -  -  + ]:    11172190 :     if (!HeapTupleIsValid(tuple) || tuple->t_data == NULL)
     731                 :           0 :         return NULL;
     732                 :             : 
     733                 :    11172190 :     newTuple = (HeapTuple) palloc(HEAPTUPLESIZE + tuple->t_len);
     734                 :    11172190 :     newTuple->t_len = tuple->t_len;
     735                 :    11172190 :     newTuple->t_self = tuple->t_self;
     736                 :    11172190 :     newTuple->t_tableOid = tuple->t_tableOid;
     737                 :    11172190 :     newTuple->t_data = (HeapTupleHeader) ((char *) newTuple + HEAPTUPLESIZE);
     738                 :    11172190 :     memcpy(newTuple->t_data, tuple->t_data, tuple->t_len);
     739                 :    11172190 :     return newTuple;
     740                 :             : }
     741                 :             : 
     742                 :             : /* ----------------
     743                 :             :  *      heap_copy_tuple_as_datum
     744                 :             :  *
     745                 :             :  *      copy a tuple as a composite-type Datum
     746                 :             :  * ----------------
     747                 :             :  */
     748                 :             : Datum
     749                 :       59253 : heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc)
     750                 :             : {
     751                 :             :     HeapTupleHeader td;
     752                 :             : 
     753                 :             :     /*
     754                 :             :      * If the tuple contains any external TOAST pointers, we have to inline
     755                 :             :      * those fields to meet the conventions for composite-type Datums.
     756                 :             :      */
     757         [ +  + ]:       59253 :     if (HeapTupleHasExternal(tuple))
     758                 :           8 :         return toast_flatten_tuple_to_datum(tuple->t_data,
     759                 :             :                                             tuple->t_len,
     760                 :             :                                             tupleDesc);
     761                 :             : 
     762                 :             :     /*
     763                 :             :      * Fast path for easy case: just make a palloc'd copy and insert the
     764                 :             :      * correct composite-Datum header fields (since those may not be set if
     765                 :             :      * the given tuple came from disk, rather than from heap_form_tuple).
     766                 :             :      */
     767                 :       59245 :     td = (HeapTupleHeader) palloc(tuple->t_len);
     768                 :       59245 :     memcpy(td, tuple->t_data, tuple->t_len);
     769                 :             : 
     770                 :             :     /* if wrongly marked, set the type from scratch */
     771         [ +  + ]:       59245 :     if (HeapTupleHeaderReallyIsDatum(td))
     772                 :       18733 :         heap_datum_update_type(td, tupleDesc);
     773                 :             :     else
     774                 :       40512 :         heap_tuple_set_datum(td, tupleDesc, tuple->t_len);
     775                 :             : 
     776                 :       59241 :     return PointerGetDatum(td);
     777                 :             : }
     778                 :             : 
     779                 :             : /*
     780                 :             :  * heap_form_tuple
     781                 :             :  *      construct a tuple from the given values[] and isnull[] arrays,
     782                 :             :  *      which are of the length indicated by tupleDescriptor->natts
     783                 :             :  *
     784                 :             :  * The result is allocated in the current memory context.
     785                 :             :  */
     786                 :             : HeapTuple
     787                 :    19183376 : heap_form_tuple(TupleDesc tupleDescriptor,
     788                 :             :                 const Datum *values,
     789                 :             :                 const bool *isnull)
     790                 :             : {
     791                 :             :     HeapTuple   tuple;          /* return tuple */
     792                 :             :     HeapTupleHeader td;         /* tuple data */
     793                 :             :     Size        len,
     794                 :             :                 data_len;
     795                 :             :     int         hoff;
     796                 :    19183376 :     bool        hasnull = false;
     797                 :    19183376 :     int         numberOfAttributes = tupleDescriptor->natts;
     798                 :             :     int         i;
     799                 :             : 
     800         [ -  + ]:    19183376 :     if (numberOfAttributes > MaxTupleAttributeNumber)
     801         [ #  # ]:           0 :         ereport(ERROR,
     802                 :             :                 (errcode(ERRCODE_TOO_MANY_COLUMNS),
     803                 :             :                  errmsg("number of columns (%d) exceeds limit (%d)",
     804                 :             :                         numberOfAttributes, MaxTupleAttributeNumber)));
     805                 :             : 
     806                 :             :     /*
     807                 :             :      * Check for nulls
     808                 :             :      */
     809         [ +  + ]:    86314155 :     for (i = 0; i < numberOfAttributes; i++)
     810                 :             :     {
     811         [ +  + ]:    70934417 :         if (isnull[i])
     812                 :             :         {
     813                 :     3803638 :             hasnull = true;
     814                 :     3803638 :             break;
     815                 :             :         }
     816                 :             :     }
     817                 :             : 
     818                 :             :     /*
     819                 :             :      * Determine total space needed
     820                 :             :      */
     821                 :    19183376 :     len = offsetof(HeapTupleHeaderData, t_bits);
     822                 :             : 
     823         [ +  + ]:    19183376 :     if (hasnull)
     824                 :     3803638 :         len += BITMAPLEN(numberOfAttributes);
     825                 :             : 
     826                 :    19183376 :     hoff = len = MAXALIGN(len); /* align user data safely */
     827                 :             : 
     828                 :    19183376 :     data_len = heap_compute_data_size(tupleDescriptor, values, isnull, false);
     829                 :             : 
     830                 :    19183376 :     len += data_len;
     831                 :             : 
     832                 :             :     /*
     833                 :             :      * Allocate and zero the space needed.  Note that the tuple body and
     834                 :             :      * HeapTupleData management structure are allocated in one chunk.
     835                 :             :      */
     836                 :    19183376 :     tuple = (HeapTuple) palloc0(HEAPTUPLESIZE + len);
     837                 :    19183376 :     tuple->t_data = td = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE);
     838                 :             : 
     839                 :             :     /*
     840                 :             :      * And fill in the information.  Note we fill the Datum fields even though
     841                 :             :      * this tuple may never become a Datum.  This lets HeapTupleHeaderGetDatum
     842                 :             :      * identify the tuple type if needed.
     843                 :             :      */
     844                 :    19183376 :     tuple->t_len = len;
     845                 :    19183376 :     ItemPointerSetInvalid(&(tuple->t_self));
     846                 :    19183376 :     tuple->t_tableOid = InvalidOid;
     847                 :             : 
     848                 :             :     /*
     849                 :             :      * Mark the tuple as a datum-style tuple. This is safe even if we
     850                 :             :      * eventually use it as a row type tuple, as e.g. inserting it into a
     851                 :             :      * table will reset the relevant state.
     852                 :             :      *
     853                 :             :      * The reason we always set the datum fields is that there are a lot of
     854                 :             :      * callers to heap_form_tuple() and the vast majority should have the
     855                 :             :      * datum information. It's easier to call HeapTupleHeaderClearDatum() in
     856                 :             :      * the few other cases.
     857                 :             :      */
     858                 :    19183376 :     HeapTupleHeaderSetNatts(td, numberOfAttributes);
     859                 :    19183376 :     td->t_hoff = hoff;
     860                 :             : 
     861                 :    19183376 :     heap_tuple_set_datum(td, tupleDescriptor, len);
     862                 :             : 
     863         [ +  + ]:    19183376 :     heap_fill_tuple(tupleDescriptor,
     864                 :             :                     values,
     865                 :             :                     isnull,
     866                 :             :                     (char *) td + hoff,
     867                 :             :                     data_len,
     868                 :             :                     &td->t_infomask,
     869                 :             :                     (hasnull ? td->t_bits : NULL));
     870                 :             : 
     871                 :    19183376 :     return tuple;
     872                 :             : }
     873                 :             : 
     874                 :             : /*
     875                 :             :  * heap_modify_tuple
     876                 :             :  *      form a new tuple from an old tuple and a set of replacement values.
     877                 :             :  *
     878                 :             :  * The replValues, replIsnull, and doReplace arrays must be of the length
     879                 :             :  * indicated by tupleDesc->natts.  The new tuple is constructed using the data
     880                 :             :  * from replValues/replIsnull at columns where doReplace is true, and using
     881                 :             :  * the data from the old tuple at columns where doReplace is false.
     882                 :             :  *
     883                 :             :  * The result is allocated in the current memory context.
     884                 :             :  */
     885                 :             : HeapTuple
     886                 :       64796 : heap_modify_tuple(HeapTuple tuple,
     887                 :             :                   TupleDesc tupleDesc,
     888                 :             :                   const Datum *replValues,
     889                 :             :                   const bool *replIsnull,
     890                 :             :                   const bool *doReplace)
     891                 :             : {
     892                 :       64796 :     int         numberOfAttributes = tupleDesc->natts;
     893                 :             :     int         attoff;
     894                 :             :     Datum      *values;
     895                 :             :     bool       *isnull;
     896                 :             :     HeapTuple   newTuple;
     897                 :             : 
     898                 :             :     /*
     899                 :             :      * allocate and fill values and isnull arrays from either the tuple or the
     900                 :             :      * repl information, as appropriate.
     901                 :             :      *
     902                 :             :      * NOTE: it's debatable whether to use heap_deform_tuple() here or just
     903                 :             :      * heap_getattr() only the non-replaced columns.  The latter could win if
     904                 :             :      * there are many replaced columns and few non-replaced ones. However,
     905                 :             :      * heap_deform_tuple costs only O(N) while the heap_getattr way would cost
     906                 :             :      * O(N^2) if there are many non-replaced columns, so it seems better to
     907                 :             :      * err on the side of linear cost.
     908                 :             :      */
     909                 :       64796 :     values = palloc_array(Datum, numberOfAttributes);
     910                 :       64796 :     isnull = palloc_array(bool, numberOfAttributes);
     911                 :             : 
     912                 :       64796 :     heap_deform_tuple(tuple, tupleDesc, values, isnull);
     913                 :             : 
     914         [ +  + ]:     1955215 :     for (attoff = 0; attoff < numberOfAttributes; attoff++)
     915                 :             :     {
     916         [ +  + ]:     1890419 :         if (doReplace[attoff])
     917                 :             :         {
     918                 :      919600 :             values[attoff] = replValues[attoff];
     919                 :      919600 :             isnull[attoff] = replIsnull[attoff];
     920                 :             :         }
     921                 :             :     }
     922                 :             : 
     923                 :             :     /*
     924                 :             :      * create a new tuple from the values and isnull arrays
     925                 :             :      */
     926                 :       64796 :     newTuple = heap_form_tuple(tupleDesc, values, isnull);
     927                 :             : 
     928                 :       64796 :     pfree(values);
     929                 :       64796 :     pfree(isnull);
     930                 :             : 
     931                 :             :     /*
     932                 :             :      * copy the identification info of the old tuple: t_self, t_tableOid.
     933                 :             :      *
     934                 :             :      * We used to also set t_data->t_ctid here, but that would invalidate the
     935                 :             :      * version included in datum tuples. There are no known callers that need
     936                 :             :      * t_ctid copied.
     937                 :             :      */
     938                 :       64796 :     newTuple->t_self = tuple->t_self;
     939                 :       64796 :     newTuple->t_tableOid = tuple->t_tableOid;
     940                 :             : 
     941                 :       64796 :     return newTuple;
     942                 :             : }
     943                 :             : 
     944                 :             : /*
     945                 :             :  * heap_modify_tuple_by_cols
     946                 :             :  *      form a new tuple from an old tuple and a set of replacement values.
     947                 :             :  *
     948                 :             :  * This is like heap_modify_tuple, except that instead of specifying which
     949                 :             :  * column(s) to replace by a boolean map, an array of target column numbers
     950                 :             :  * is used.  This is often more convenient when a fixed number of columns
     951                 :             :  * are to be replaced.  The replCols, replValues, and replIsnull arrays must
     952                 :             :  * be of length nCols.  Target column numbers are indexed from 1.
     953                 :             :  *
     954                 :             :  * The result is allocated in the current memory context.
     955                 :             :  */
     956                 :             : HeapTuple
     957                 :         714 : heap_modify_tuple_by_cols(HeapTuple tuple,
     958                 :             :                           TupleDesc tupleDesc,
     959                 :             :                           int nCols,
     960                 :             :                           const int *replCols,
     961                 :             :                           const Datum *replValues,
     962                 :             :                           const bool *replIsnull)
     963                 :             : {
     964                 :         714 :     int         numberOfAttributes = tupleDesc->natts;
     965                 :             :     Datum      *values;
     966                 :             :     bool       *isnull;
     967                 :             :     HeapTuple   newTuple;
     968                 :             :     int         i;
     969                 :             : 
     970                 :             :     /*
     971                 :             :      * allocate and fill values and isnull arrays from the tuple, then replace
     972                 :             :      * selected columns from the input arrays.
     973                 :             :      */
     974                 :         714 :     values = palloc_array(Datum, numberOfAttributes);
     975                 :         714 :     isnull = palloc_array(bool, numberOfAttributes);
     976                 :             : 
     977                 :         714 :     heap_deform_tuple(tuple, tupleDesc, values, isnull);
     978                 :             : 
     979         [ +  + ]:        2140 :     for (i = 0; i < nCols; i++)
     980                 :             :     {
     981                 :        1426 :         int         attnum = replCols[i];
     982                 :             : 
     983   [ +  -  -  + ]:        1426 :         if (attnum <= 0 || attnum > numberOfAttributes)
     984         [ #  # ]:           0 :             elog(ERROR, "invalid column number %d", attnum);
     985                 :        1426 :         values[attnum - 1] = replValues[i];
     986                 :        1426 :         isnull[attnum - 1] = replIsnull[i];
     987                 :             :     }
     988                 :             : 
     989                 :             :     /*
     990                 :             :      * create a new tuple from the values and isnull arrays
     991                 :             :      */
     992                 :         714 :     newTuple = heap_form_tuple(tupleDesc, values, isnull);
     993                 :             : 
     994                 :         714 :     pfree(values);
     995                 :         714 :     pfree(isnull);
     996                 :             : 
     997                 :             :     /* see equivalent code in heap_modify_tuple() */
     998                 :         714 :     newTuple->t_self = tuple->t_self;
     999                 :         714 :     newTuple->t_tableOid = tuple->t_tableOid;
    1000                 :             : 
    1001                 :         714 :     return newTuple;
    1002                 :             : }
    1003                 :             : 
    1004                 :             : /*
    1005                 :             :  * Mark the tuple as a datum-style tuple.
    1006                 :             :  *
    1007                 :             :  * This includes setting the datum's length, typeid and version information.
    1008                 :             :  *
    1009                 :             :  * The caller already needs to have done HeapTupleHeaderSetNatts().
    1010                 :             :  *
    1011                 :             :  * Note that the caller needs to be sure that it is safe to use tupleDesc to
    1012                 :             :  * deform tup, since this function may remove information that could otherwise
    1013                 :             :  * have been used to detect unsafe deforming.
    1014                 :             :  *
    1015                 :             :  * See also src/backend/catalog/README.attver.md
    1016                 :             :  */
    1017                 :             : void
    1018                 :    19286835 : heap_tuple_set_datum(HeapTupleHeader tup, TupleDesc tupleDesc, uint32 len)
    1019                 :             : {
    1020                 :    19286835 :     HeapTupleHeaderSetDatumLength(tup, len);
    1021                 :    19286835 :     HeapTupleHeaderSetTypeId(tup, tupleDesc->tdtypeid);
    1022                 :    19286835 :     HeapTupleHeaderSetDatum(tup);
    1023                 :             : 
    1024         [ +  + ]:    19286835 :     if (tupleDesc->tdtypeid == RECORDOID)
    1025                 :             :     {
    1026                 :             :         /*
    1027                 :             :          * RECORDOID tuples are not protected by versioning (and do not need
    1028                 :             :          * to be).
    1029                 :             :          */
    1030                 :    14240252 :         HeapTupleHeaderSetTypMod(tup, tupleDesc->tdtypmod);
    1031                 :             : 
    1032                 :             :         /*
    1033                 :             :          * Invalidating t_ctid makes the tuple an unversioned datum, even if
    1034                 :             :          * it previously was a versioned one.
    1035                 :             :          */
    1036                 :    14240252 :         ItemPointerSetInvalid(&tup->t_ctid);
    1037                 :             :     }
    1038                 :             :     else
    1039                 :             :     {
    1040                 :     5046583 :         int         natts = HeapTupleHeaderGetNatts(tup);
    1041                 :             :         XLogRecPtr  version;
    1042                 :             : 
    1043                 :             :         /*
    1044                 :             :          * Stamp the datum with the version of the last attribute it actually
    1045                 :             :          * contains, not the version of the last attribute in the tuple desc.
    1046                 :             :          * That is important in case the tuple has fewer attributes than the
    1047                 :             :          * descriptor.
    1048                 :             :          *
    1049                 :             :          * A zero-attribute tuple contains no data to decode, so it is always
    1050                 :             :          * safe to deform; give it an explicit version of InvalidXLogRecPtr,
    1051                 :             :          * so it can be deformed if columns are added later (as the version is
    1052                 :             :          * stored in pg_attribute, a rowtype without columns has no version).
    1053                 :             :          *
    1054                 :             :          * It is never valid to have fewer attributes in the descriptor than
    1055                 :             :          * the tuple when forming a tuple, so error out in that case.
    1056                 :             :          * Hopefully unreachable.
    1057                 :             :          */
    1058         [ +  + ]:     5046583 :         if (natts == 0)
    1059                 :          23 :             version = InvalidXLogRecPtr;
    1060         [ +  + ]:     5046560 :         else if (natts == tupleDesc->natts)
    1061                 :     5046523 :             version = TupleDescGetVersion(tupleDesc);
    1062         [ +  - ]:          37 :         else if (natts < tupleDesc->natts)
    1063                 :          37 :             version = TupleDescGetAttrVersion(tupleDesc, natts);
    1064                 :             :         else
    1065         [ #  # ]:           0 :             elog(ERROR, "datum's descriptor has fewer (%d) attributes than tuple (%d)",
    1066                 :             :                  tupleDesc->natts, natts);
    1067                 :             : 
    1068                 :     5046583 :         HeapTupleHeaderSetVersion(tup, version);
    1069                 :             :     }
    1070                 :    19286835 : }
    1071                 :             : 
    1072                 :             : /*
    1073                 :             :  * Update the type of a datum tuple to the type of tupleDesc.
    1074                 :             :  *
    1075                 :             :  * If the tuple already is a versioned datum, it is verified to be compatible
    1076                 :             :  * with tupleDesc first, as relabeling it would otherwise hide a
    1077                 :             :  * mismatch.
    1078                 :             :  *
    1079                 :             :  * Additionally, if the current type and the target type differ, the types are
    1080                 :             :  * verified to be compatible (if possible).
    1081                 :             :  */
    1082                 :             : void
    1083                 :       18745 : heap_datum_update_type(HeapTupleHeader tup, TupleDesc tupleDesc)
    1084                 :             : {
    1085                 :             :     Assert(HeapTupleHeaderReallyIsDatum(tup));
    1086                 :             : 
    1087                 :       18745 :     heap_datum_check_coercion(tup, tupleDesc);
    1088                 :             : 
    1089                 :       18737 :     heap_tuple_set_datum(tup, tupleDesc,
    1090                 :             :                          HeapTupleHeaderGetDatumLength(tup));
    1091                 :       18737 : }
    1092                 :             : 
    1093                 :             : /*
    1094                 :             :  * Verify that the datum tuple "tup" can be deformed with "deformingDesc",
    1095                 :             :  * the descriptor the caller wants to use.
    1096                 :             :  *
    1097                 :             :  * "datumDesc" is the descriptor of the datum's own row type, against which
    1098                 :             :  * the datum's version has already been checked (if it is versioned).  Only
    1099                 :             :  * the attributes present in the tuple are compared, so this does not
    1100                 :             :  * establish that the two descriptors are interchangeable for other tuples.
    1101                 :             :  */
    1102                 :             : static void
    1103                 :        8303 : verify_tupledesc_compatibility(HeapTupleHeader tup,
    1104                 :             :                                TupleDesc datumDesc, TupleDesc deformingDesc)
    1105                 :             : {
    1106                 :        8303 :     int         datum_natts = HeapTupleHeaderGetNatts(tup);
    1107                 :        8303 :     int         error_level =
    1108                 :        8303 :         composite_type_mismatch == COMPOSITE_TYPE_MISMATCH_WARN ?
    1109         [ -  + ]:        8303 :         WARNING : ERROR;
    1110                 :             : 
    1111                 :             :     /*
    1112                 :             :      * Should never get here with a datumDesc that has insufficient
    1113                 :             :      * attributes.
    1114                 :             :      */
    1115         [ -  + ]:        8303 :     if (datumDesc->natts < datum_natts)
    1116                 :             :     {
    1117         [ #  # ]:           0 :         elog(error_level, "datum's descriptor has fewer (%d) attributes than tuple (%d)",
    1118                 :             :              datumDesc->natts, datum_natts);
    1119                 :           0 :         return;
    1120                 :             :     }
    1121                 :             : 
    1122                 :             :     /*
    1123                 :             :      * We sometimes may get called with a deformingDesc that has extra
    1124                 :             :      * attributes for resjunk columns. That's safe iff the tuple itself does
    1125                 :             :      * not have those columns (the expected case). But we can't accept cases
    1126                 :             :      * where the datum has more attributes than deformingDesc.
    1127                 :             :      */
    1128         [ -  + ]:        8303 :     if (unlikely(deformingDesc->natts < datum_natts))
    1129                 :             :     {
    1130         [ #  # ]:           0 :         ereport(error_level,
    1131                 :             :                 errcode(ERRCODE_DATATYPE_MISMATCH),
    1132                 :             :                 errmsg("tuple descriptor has fewer (%d) attributes than tuple (%d)",
    1133                 :             :                        deformingDesc->natts, datum_natts));
    1134                 :           0 :         return;
    1135                 :             :     }
    1136                 :             : 
    1137                 :             :     /*
    1138                 :             :      * The message isn't translatable, due to the interpolated format strings.
    1139                 :             :      * That's OK, as this is just a defensive layer that shouldn't be reached.
    1140                 :             :      *
    1141                 :             :      * With composite_type_mismatch = warn, only the first mismatch is
    1142                 :             :      * reported.
    1143                 :             :      */
    1144                 :             : #define check_attribute_aspect(aspect, format) \
    1145                 :             :     if (unlikely(attr_datum->aspect != attr_deform->aspect)) \
    1146                 :             :     { \
    1147                 :             :         ereport(error_level, \
    1148                 :             :                 errcode(ERRCODE_DATATYPE_MISMATCH), \
    1149                 :             :                 errmsg_internal("datum attribute \"%s\" at position %d does not match deforming descriptor's %s ("format" vs "format")", \
    1150                 :             :                                 NameStr(attr_datum->attname), \
    1151                 :             :                                 i + 1, \
    1152                 :             :                                 CppAsString(aspect), \
    1153                 :             :                                 attr_datum->aspect, attr_deform->aspect) \
    1154                 :             :             ); \
    1155                 :             :         return; \
    1156                 :             :     }
    1157                 :             : 
    1158         [ +  + ]:       17184 :     for (int i = 0; i < datum_natts; i++)
    1159                 :             :     {
    1160                 :        8881 :         Form_pg_attribute attr_datum = TupleDescAttr(datumDesc, i);
    1161                 :        8881 :         Form_pg_attribute attr_deform = TupleDescAttr(deformingDesc, i);
    1162                 :             : 
    1163                 :             :         /* don't care about the attribute's name being the same */
    1164                 :             : 
    1165         [ +  + ]:        8881 :         if (unlikely(attr_datum->attisdropped != attr_deform->attisdropped))
    1166                 :             :         {
    1167                 :             :             /*
    1168                 :             :              * Targetlists can represent dropped columns as NULL::int4.  A
    1169                 :             :              * NULL attribute consumes neither bytes nor alignment, so its
    1170                 :             :              * type and storage metadata need not match for deforming to be
    1171                 :             :              * safe.
    1172                 :             :              *
    1173                 :             :              * See eval-plan-qual-dropped-column.spec for a test exercising
    1174                 :             :              * this path.
    1175                 :             :              */
    1176   [ +  -  +  - ]:           6 :             bool        isnull = (tup->t_infomask & HEAP_HASNULL) &&
    1177                 :           3 :                 att_isnull(i, tup->t_bits);
    1178                 :             : 
    1179                 :             :             /*
    1180                 :             :              * XXX: At some point in the future we should also check that
    1181                 :             :              * attr_deform's NOT NULLness matches, due to the optimized
    1182                 :             :              * deforming slots can use for NOT NULL columns. But right now we
    1183                 :             :              * don't know if that's guaranteed here (and the constraints
    1184                 :             :              * applying to the tuple might not yet have been validated). We
    1185                 :             :              * also, at the moment, won't get here for slots with that
    1186                 :             :              * optimization. Therefore delay this for later.
    1187                 :             :              */
    1188         [ +  - ]:           3 :             if (isnull)
    1189                 :           3 :                 continue;
    1190                 :             :         }
    1191                 :             : 
    1192                 :             :         /* Other dropped-column mismatches require explicit conversion. */
    1193   [ -  +  -  - ]:        8878 :         check_attribute_aspect(attisdropped, "%u");
    1194                 :             : 
    1195                 :             :         /*
    1196                 :             :          * While, with an atttypid mismatch, the types could be binary
    1197                 :             :          * coercible, we can't know that here, and in any case, it should have
    1198                 :             :          * led to attribute mapping being used.
    1199                 :             :          */
    1200         [ +  + ]:        8878 :         if (!attr_datum->attisdropped)
    1201   [ -  +  -  - ]:        8824 :             check_attribute_aspect(atttypid, "%u");
    1202   [ -  +  -  - ]:        8878 :         check_attribute_aspect(attlen, "%d");
    1203   [ -  +  -  - ]:        8878 :         check_attribute_aspect(attalign, "%c");
    1204                 :             : 
    1205                 :             :         /*
    1206                 :             :          * If the attribute is dropped, byval does not matter. It'd generally
    1207                 :             :          * be correct to check byval regardless, but check_attrmap_match()
    1208                 :             :          * doesn't currently, so that could cause spurious errors.
    1209                 :             :          */
    1210         [ +  + ]:        8878 :         if (!attr_datum->attisdropped)
    1211                 :             :         {
    1212                 :             :             /*
    1213                 :             :              * Theoretically bool could be wider than char, so I'm using %u
    1214                 :             :              * here.
    1215                 :             :              */
    1216   [ -  +  -  - ]:        8824 :             check_attribute_aspect(attbyval, "%u");
    1217                 :             :         }
    1218                 :             : 
    1219                 :             :         /*
    1220                 :             :          * It does not seem necessary to compare atttypmod, attcollation, etc,
    1221                 :             :          * they are not required to match for correct deforming.
    1222                 :             :          */
    1223                 :             :     }
    1224                 :             : #undef check_attribute_aspect
    1225                 :             : }
    1226                 :             : 
    1227                 :             : /*
    1228                 :             :  * Report accessing a tuple with an incompatible tuple descriptor, due to a
    1229                 :             :  * version mismatch.  descVersion is the version the tuple was compared with,
    1230                 :             :  * see datum_attribute_version_ok().  This is out-of-line because it can be
    1231                 :             :  * reached via several paths.
    1232                 :             :  */
    1233                 :             : pg_noinline static void
    1234                 :         379 : heap_datum_version_mismatch_error(Oid tupTypeid, Oid descTypeid,
    1235                 :             :                                   XLogRecPtr tupVersion, XLogRecPtr descVersion)
    1236                 :             : {
    1237                 :         379 :     int         error_level =
    1238                 :         379 :         composite_type_mismatch == COMPOSITE_TYPE_MISMATCH_WARN ?
    1239         [ +  + ]:         379 :         WARNING : ERROR;
    1240                 :             : 
    1241                 :             :     /*
    1242                 :             :      * NB: We can't embed the versions in the primary message without making
    1243                 :             :      * test output unstable. Putting them into DETAIL would require the tests
    1244                 :             :      * to use \set VERBOSITY terse, which would also hide the other DETAIL and
    1245                 :             :      * CONTEXT lines that many of them benefit from. Therefore the versions
    1246                 :             :      * are only reported in the server log. That's not pretty, but ...
    1247                 :             :      */
    1248                 :             : 
    1249                 :             :     /* log details about the mismatch */
    1250         [ +  - ]:         379 :     ereport(LOG_SERVER_ONLY,
    1251                 :             :             errcode(ERRCODE_DATUM_VERSION_MISMATCH),
    1252                 :             :             errmsg("tuple descriptor for type %s (OID %u), version %X/%X is incompatible with tuple type %s (OID %u), version %X/%X",
    1253                 :             :                    format_type_be(descTypeid), descTypeid,
    1254                 :             :                    LSN_FORMAT_ARGS(descVersion),
    1255                 :             :                    format_type_be(tupTypeid), tupTypeid,
    1256                 :             :                    LSN_FORMAT_ARGS(tupVersion)));
    1257                 :             : 
    1258                 :             :     /* raise the actual error / warning */
    1259   [ +  -  +  + ]:         379 :     ereport(error_level,
    1260                 :             :             errcode(ERRCODE_DATUM_VERSION_MISMATCH),
    1261                 :             :             tupVersion < descVersion ?
    1262                 :             :             errmsg("row type of datum tuple was altered after the datum's creation")
    1263                 :             :             :
    1264                 :             :             errmsg("using outdated tuple descriptor when deforming row type"),
    1265                 :             :             errhint("See server log for additional details."));
    1266                 :           4 : }
    1267                 :             : 
    1268                 :             : /*
    1269                 :             :  * Check if the tuple can be safely decoded, despite a mismatch between the
    1270                 :             :  * descriptor's version and the tuple's, due to the tuple having fewer
    1271                 :             :  * attributes and that subset having a matching version.
    1272                 :             :  *
    1273                 :             :  * This can happen legitimately if the tuple has fewer attributes than the
    1274                 :             :  * descriptor, and only attributes not included in the tuple have been
    1275                 :             :  * altered.  To handle that case, we just need to compare the tuple's version
    1276                 :             :  * with the version number of attribute tupNatts in the tuple descriptor.
    1277                 :             :  *
    1278                 :             :  * *cmpVersion is set to the version the tuple was compared with, for the
    1279                 :             :  * caller to report a mismatch.
    1280                 :             :  */
    1281                 :             : static bool
    1282                 :         800 : datum_attribute_version_ok(TupleDesc tupleDesc, int tupNatts,
    1283                 :             :                            XLogRecPtr tupVersion, XLogRecPtr *cmpVersion)
    1284                 :             : {
    1285                 :         800 :     *cmpVersion = TupleDescGetVersion(tupleDesc);
    1286                 :             : 
    1287                 :             :     /* should only be called if the aggregate version does not match */
    1288                 :             :     Assert(*cmpVersion != tupVersion);
    1289                 :             : 
    1290                 :             :     /* always safe to deform no columns */
    1291         [ +  + ]:         800 :     if (tupNatts == 0)
    1292                 :           8 :         return true;
    1293                 :             : 
    1294                 :             :     /*
    1295                 :             :      * Iff the tuple is lacking columns in the descriptor, we can check the
    1296                 :             :      * version of the last column present.
    1297                 :             :      *
    1298                 :             :      * We do not allow cases where the tuple descriptor has fewer columns than
    1299                 :             :      * the tuple, as that indicates using a too old descriptor. It might be
    1300                 :             :      * safe to allow that in some cases, but there don't seem to be any
    1301                 :             :      * convincing cases that'd benefit.
    1302                 :             :      */
    1303         [ +  + ]:         792 :     if (tupleDesc->natts > tupNatts)
    1304                 :             :     {
    1305                 :         446 :         *cmpVersion = TupleDescGetAttrVersion(tupleDesc, tupNatts);
    1306                 :         446 :         return tupVersion == *cmpVersion;
    1307                 :             :     }
    1308                 :             : 
    1309                 :         346 :     return false;
    1310                 :             : }
    1311                 :             : 
    1312                 :             : /*
    1313                 :             :  * Fetch and return a pinned TupleDesc for the type/typmod combination.
    1314                 :             :  *
    1315                 :             :  * This switches to CurTransactionContext during the fetch, in case we get
    1316                 :             :  * called from within something like a tuplesort with a bump context (which
    1317                 :             :  * does not support freeing), which could be a problem during a syscache
    1318                 :             :  * miss. Because syscache misses can leak some memory, it should be a context
    1319                 :             :  * with a somewhat bounded lifetime. Using the current transaction context
    1320                 :             :  * seems like the most obvious choice.
    1321                 :             :  */
    1322                 :             : static TupleDesc
    1323                 :        8314 : heap_datum_fetch_tupledesc(Oid tupTypeid, int32 tupTypmod)
    1324                 :             : {
    1325                 :        8314 :     MemoryContext oldContext = MemoryContextSwitchTo(CurTransactionContext);
    1326                 :        8314 :     TupleDesc   descForTuple = lookup_rowtype_tupdesc(tupTypeid, tupTypmod);
    1327                 :             : 
    1328                 :        8314 :     MemoryContextSwitchTo(oldContext);
    1329                 :             : 
    1330                 :        8314 :     return descForTuple;
    1331                 :             : }
    1332                 :             : 
    1333                 :             : /*
    1334                 :             :  * Fetch the tuple descriptor for the tuple using the tuple's typeid, check
    1335                 :             :  * that the tuple has the same version as that fetched descriptor and finally
    1336                 :             :  * verify that the callerDesc is compatible with that fetched TupleDesc.
    1337                 :             :  *
    1338                 :             :  * Normally this should not be needed, but we have a few codepaths that use
    1339                 :             :  * tuple descriptors that are verified to be compatible, with mismatching
    1340                 :             :  * typeids. In the places this function is used, we can't be sure that the
    1341                 :             :  * mismatch is harmless, so we re-verify compatibility.
    1342                 :             :  *
    1343                 :             :  * NB: This is not cheap, so it's worth making sure that this path is only
    1344                 :             :  * reached for cases with significant per-row costs.
    1345                 :             :  */
    1346                 :             : static void
    1347                 :        8314 : heap_datum_fetch_compare_tupledesc(HeapTupleHeader tup,
    1348                 :             :                                    Oid tupTypeid, int32 tupTypmod,
    1349                 :             :                                    TupleDesc callerDesc)
    1350                 :             : {
    1351                 :        8314 :     int         tupNatts = HeapTupleHeaderGetNatts(tup);
    1352                 :        8314 :     TupleDesc   descForTuple = heap_datum_fetch_tupledesc(tupTypeid, tupTypmod);
    1353                 :        8314 :     XLogRecPtr  descForTupleVersion = TupleDescGetVersion(descForTuple);
    1354                 :             : 
    1355                 :             :     /*
    1356                 :             :      * First check if the tuple descriptor we just looked up has a compatible
    1357                 :             :      * version. It's possible there have been incompatible alterations since
    1358                 :             :      * the tuple was formed.
    1359                 :             :      */
    1360         [ +  + ]:        8314 :     if (HeapTupleHeaderIsVersionedDatum(tup))
    1361                 :             :     {
    1362                 :         292 :         XLogRecPtr  tupVersion = HeapTupleHeaderGetVersion(tup);
    1363                 :             :         XLogRecPtr  cmpVersion;
    1364                 :             : 
    1365         [ +  + ]:         292 :         if (unlikely(descForTupleVersion != tupVersion) &&
    1366         [ +  - ]:          11 :             !datum_attribute_version_ok(descForTuple, tupNatts, tupVersion,
    1367                 :             :                                         &cmpVersion))
    1368                 :          11 :             heap_datum_version_mismatch_error(tupTypeid, descForTuple->tdtypeid,
    1369                 :             :                                               tupVersion, cmpVersion);
    1370                 :             :     }
    1371                 :             : 
    1372                 :             :     /*
    1373                 :             :      * Now check that the caller's descriptor can decode this particular
    1374                 :             :      * tuple, including any physically NULL dropped-column placeholders.
    1375                 :             :      */
    1376                 :        8303 :     verify_tupledesc_compatibility(tup, descForTuple, callerDesc);
    1377                 :             : 
    1378         [ +  - ]:        8303 :     ReleaseTupleDesc(descForTuple);
    1379                 :        8303 : }
    1380                 :             : 
    1381                 :             : /*
    1382                 :             :  * Try to verify that it is safe to coerce tup to have targetDesc's type.
    1383                 :             :  *
    1384                 :             :  * This will often be used when the types don't actually differ, so we try to
    1385                 :             :  * avoid unnecessary checks.
    1386                 :             :  */
    1387                 :             : static void
    1388                 :       18745 : heap_datum_check_coercion(HeapTupleHeader tup, TupleDesc targetDesc)
    1389                 :             : {
    1390                 :       18745 :     Oid         tupTypeid = HeapTupleHeaderGetTypeId(tup);
    1391                 :       18745 :     int32       tupTypmod = HeapTupleHeaderGetTypMod(tup);
    1392                 :             : 
    1393                 :             :     Assert(HeapTupleHeaderIsDatum(tup));
    1394                 :             : 
    1395         [ -  + ]:       18745 :     if (unlikely(composite_type_mismatch == COMPOSITE_TYPE_MISMATCH_DISABLED))
    1396                 :           0 :         return;
    1397                 :             : 
    1398                 :             :     /* can't check if this is wrongly marked as a datum tuple */
    1399         [ -  + ]:       18745 :     if (!HeapTupleHeaderReallyIsDatum(tup))
    1400                 :           0 :         return;
    1401                 :             : 
    1402                 :             :     /*
    1403                 :             :      * Source tuple has an ephemeral row type, so we can't fetch the tuple's
    1404                 :             :      * desc to cross-check against.
    1405                 :             :      */
    1406   [ +  +  -  + ]:       18745 :     if (tupTypeid == RECORDOID && tupTypmod == -1)
    1407                 :           0 :         return;
    1408                 :             : 
    1409         [ +  + ]:       18745 :     if (tupTypeid != targetDesc->tdtypeid ||
    1410         [ +  + ]:       10716 :         (targetDesc->tdtypeid == RECORDOID &&
    1411         [ +  + ]:        3393 :          tupTypmod != targetDesc->tdtypmod))
    1412                 :             :     {
    1413                 :             :         /*
    1414                 :             :          * The tuple's type and the target type differ, cross check the tuple
    1415                 :             :          * descriptors against each other.
    1416                 :             :          */
    1417                 :        8030 :         heap_datum_fetch_compare_tupledesc(tup, tupTypeid, tupTypmod, targetDesc);
    1418                 :             :     }
    1419   [ +  +  +  + ]:       18038 :     else if (HeapTupleHeaderIsVersionedDatum(tup) &&
    1420                 :        7323 :              TupleDescGetVersion(targetDesc) != HeapTupleHeaderGetVersion(tup))
    1421                 :             :     {
    1422                 :             :         /*
    1423                 :             :          * While the tuple's type and the target type are the same, their
    1424                 :             :          * versions differ. Make sure that's OK.
    1425                 :             :          */
    1426                 :           4 :         heap_datum_check(tup, targetDesc);
    1427                 :             :     }
    1428                 :             : }
    1429                 :             : 
    1430                 :             : /*
    1431                 :             :  * Check that a versioned datum tuple can be safely decoded with the
    1432                 :             :  * tupleDesc, raise an error if not.
    1433                 :             :  *
    1434                 :             :  * It is an error to call this for an unversioned datum, callers need to check
    1435                 :             :  * HeapTupleHeaderIsVersionedDatum() first.
    1436                 :             :  *
    1437                 :             :  * This function implements the attribute versioning checks described in
    1438                 :             :  * src/backend/catalog/README.attver.md.  For partially historical reasons, it
    1439                 :             :  * needs to handle some corner cases, described below.
    1440                 :             :  *
    1441                 :             :  * In addition to the versioning checks, this also, if the descriptor and the
    1442                 :             :  * tuple are for different types, tries to check that the datum's and the
    1443                 :             :  * descriptor's types are compatible.  We should work towards making such
    1444                 :             :  * mismatches errors, but until then the additional checks are helpful.
    1445                 :             :  */
    1446                 :             : void
    1447                 :     1110452 : heap_datum_check(HeapTupleHeader tup, TupleDesc tupleDesc)
    1448                 :             : {
    1449                 :     1110452 :     XLogRecPtr  descVersion = TupleDescGetVersion(tupleDesc);
    1450                 :     1110452 :     Oid         tupTypeid = HeapTupleHeaderGetTypeId(tup);
    1451                 :     1110452 :     int32       tupTypmod = HeapTupleHeaderGetTypMod(tup);
    1452                 :             :     XLogRecPtr  tupVersion;
    1453                 :             : 
    1454         [ +  + ]:     1110452 :     if (unlikely(composite_type_mismatch == COMPOSITE_TYPE_MISMATCH_DISABLED))
    1455                 :           4 :         return;
    1456                 :             : 
    1457                 :             :     /*
    1458                 :             :      * This should not be reachable, but given this is new infrastructure and
    1459                 :             :      * out-of-tree code might be doing unexpected things, it seems better to
    1460                 :             :      * verify with a runtime check than just an Assert().
    1461                 :             :      */
    1462         [ -  + ]:     1110448 :     if (unlikely(!HeapTupleHeaderIsVersionedDatum(tup)))
    1463         [ #  # ]:           0 :         elog(ERROR, "heap_datum_check() called on an unversioned datum");
    1464                 :             : 
    1465                 :             :     /* can't check if this is wrongly marked as a datum tuple */
    1466         [ +  + ]:     1110448 :     if (unlikely(!HeapTupleHeaderReallyIsDatum(tup)))
    1467                 :      114855 :         return;
    1468                 :             : 
    1469                 :      995593 :     tupVersion = HeapTupleHeaderGetVersion(tup);
    1470                 :             : 
    1471         [ +  + ]:      995593 :     if (unlikely(tupleDesc->tdtypeid == RECORDOID))
    1472                 :             :     {
    1473                 :             :         /*
    1474                 :             :          * It's possible that we will get called with a RECORDOID tupledesc
    1475                 :             :          * but the datum has a different type, e.g. because some function just
    1476                 :             :          * updated the descriptor's type to RECORDOID.  In that case it's
    1477                 :             :          * really the caller's responsibility to have made sure that the type
    1478                 :             :          * is right. But we better make sure anyway.
    1479                 :             :          *
    1480                 :             :          * While it's not cheap, we can make sure this is safe by first
    1481                 :             :          * fetching the tuple descriptor for the tuple, verifying that its
    1482                 :             :          * version matches, and then checking that the two tuple descriptors
    1483                 :             :          * are compatible.
    1484                 :             :          *
    1485                 :             :          * The known paths reaching here involve per-row triggers, and
    1486                 :             :          * EvalPlanQual rechecks of whole-row references to function scans,
    1487                 :             :          * where the cost of the additional checking is acceptable.
    1488                 :             :          */
    1489                 :          90 :         heap_datum_fetch_compare_tupledesc(tup, tupTypeid, tupTypmod,
    1490                 :             :                                            tupleDesc);
    1491                 :             :     }
    1492         [ -  + ]:      995503 :     else if (unlikely(tupTypeid == InvalidOid))
    1493                 :             :     {
    1494                 :             :         /*
    1495                 :             :          * This could happen if code does a heap_form_tuple() and then
    1496                 :             :          * overwrites typeid etc with invalid fields. However most such code
    1497                 :             :          * is already handled via the HeapTupleHeaderReallyIsDatum() above.
    1498                 :             :          *
    1499                 :             :          * NB: In contrast to some other paths in this function, we can't
    1500                 :             :          * fetch the tuple's "real" tuple descriptor, we lack a valid type
    1501                 :             :          * oid.
    1502                 :             :          */
    1503         [ #  # ]:           0 :         ereport(ERROR,
    1504                 :             :                 errmsg_internal("datum tuple without type information"),
    1505                 :             :                 errhint_internal("An extension may need to be updated."));
    1506                 :             :     }
    1507         [ -  + ]:      995503 :     else if (unlikely(tupTypeid == RECORDOID))
    1508                 :             :     {
    1509                 :             :         /*
    1510                 :             :          * The tupledesc has a version number, but the tuple is a record
    1511                 :             :          * tuple, for which we do not store a version number (since it has to
    1512                 :             :          * be a blessed tupledesc that can never change and there are no
    1513                 :             :          * catalog entries to associate the version with either).
    1514                 :             :          *
    1515                 :             :          * This should not be reached, we don't mark a datum as versioned for
    1516                 :             :          * RECORDOID, but could be reached if some caller relabels a versioned
    1517                 :             :          * datum as RECORDOID without going through heap_tuple_set_datum().
    1518                 :             :          */
    1519         [ #  # ]:           0 :         elog(ERROR, "processing record datum (%u, %d) with rowtype tupdesc (%u, %d, %X/%X)",
    1520                 :             :              tupTypeid, tupTypmod,
    1521                 :             :              tupleDesc->tdtypeid, tupleDesc->tdtypmod,
    1522                 :             :              LSN_FORMAT_ARGS(descVersion));
    1523                 :             :     }
    1524         [ +  + ]:      995503 :     else if (unlikely(tupleDesc->tdtypeid != tupTypeid))
    1525                 :             :     {
    1526                 :             :         /*
    1527                 :             :          * In some partitioned workloads, we currently use the parent
    1528                 :             :          * tupledesc to deform a child tuple, or vice versa. Typically the
    1529                 :             :          * caller will have verified that no conversion is necessary (c.f.
    1530                 :             :          * build_attrmap_by_name_if_req()), but we can't verify that that
    1531                 :             :          * happened here.
    1532                 :             :          *
    1533                 :             :          * XXX: This code pattern seems problematic, but it's probably not
    1534                 :             :          * feasible to fix this in the back branches. Medium term I think the
    1535                 :             :          * tuple conversion paths should update the tuple's typeid even when
    1536                 :             :          * otherwise no conversion is needed.
    1537                 :             :          *
    1538                 :             :          * To make sure this cannot lead to unsafe deforming, we check that
    1539                 :             :          * the tuple descriptor for the tuple (identified by typeid and
    1540                 :             :          * version) and the one used by the caller are compatible.
    1541                 :             :          *
    1542                 :             :          * The known cases leading here are DML with triggers and/or ON
    1543                 :             :          * CONFLICT, where the per-row cost of this check is acceptable.
    1544                 :             :          */
    1545                 :         194 :         heap_datum_fetch_compare_tupledesc(tup, tupTypeid, tupTypmod,
    1546                 :             :                                            tupleDesc);
    1547                 :             :     }
    1548         [ +  + ]:      995309 :     else if (unlikely(tupVersion != descVersion))
    1549                 :             :     {
    1550                 :             :         XLogRecPtr  cmpVersion;
    1551                 :             : 
    1552                 :             :         /*
    1553                 :             :          * The tuple's version does not match the tuple descriptor's.
    1554                 :             :          *
    1555                 :             :          * Check if that's solely because the tuple is shorter (i.e. only new
    1556                 :             :          * columns have been added since the tuple was formed).
    1557                 :             :          *
    1558                 :             :          * Otherwise report the mismatch.
    1559                 :             :          */
    1560         [ +  + ]:         789 :         if (!datum_attribute_version_ok(tupleDesc, HeapTupleHeaderGetNatts(tup),
    1561                 :             :                                         tupVersion, &cmpVersion))
    1562                 :         368 :             heap_datum_version_mismatch_error(tupTypeid, tupleDesc->tdtypeid,
    1563                 :             :                                               tupVersion, cmpVersion);
    1564                 :             :     }
    1565                 :             : }
    1566                 :             : 
    1567                 :             : /*
    1568                 :             :  * Helper for DatumGetHeapTupleHeader / DatumGetHeapTupleHeaderCopy.
    1569                 :             :  *
    1570                 :             :  * This implements the "Upgrading from pre-versioning composite datums" logic
    1571                 :             :  * in src/backend/catalog/README.attver.md.
    1572                 :             :  *
    1573                 :             :  * Due to requiring a copy of the input tuple, this isn't exactly cheap, but
    1574                 :             :  * there should be fewer and fewer unversioned tuples in clusters over time, as
    1575                 :             :  * such datums get re-formed or deleted. We unfortunately can't just fix them
    1576                 :             :  * in-place without causing larger issues.
    1577                 :             :  */
    1578                 :             : static inline HeapTupleHeader
    1579                 :     1734115 : DatumGetHeapTupleHeaderImpl(Datum t, bool copy, bool *was_datum)
    1580                 :             : {
    1581                 :     1734115 :     struct varlena *tpr = (struct varlena *) DatumGetPointer(t);
    1582                 :             :     HeapTupleHeader tp;
    1583                 :             :     Oid         tupTypeid;
    1584                 :             : 
    1585         [ +  + ]:     1734115 :     if (copy)
    1586                 :          23 :         tp = (HeapTupleHeader) pg_detoast_datum_copy(tpr);
    1587                 :             :     else
    1588                 :             :     {
    1589                 :     1734092 :         tp = (HeapTupleHeader) pg_detoast_datum(tpr);
    1590                 :     1734092 :         copy = ((struct varlena *) tp) != tpr;
    1591                 :             :     }
    1592                 :             : 
    1593                 :     1734115 :     tupTypeid = HeapTupleHeaderGetTypeId(tp);
    1594                 :             : 
    1595         [ +  + ]:     1734115 :     if (was_datum)
    1596                 :          15 :         *was_datum = true;
    1597                 :             : 
    1598         [ +  + ]:     1734115 :     if (!HeapTupleHeaderIsDatum(tp))
    1599                 :             :     {
    1600                 :             :         Assert(HeapTupleHeaderGetDatumLength(tp) > 0);
    1601                 :             : 
    1602         [ -  + ]:          44 :         if (was_datum)
    1603                 :           0 :             *was_datum = false;
    1604                 :             : 
    1605                 :             :         /*
    1606                 :             :          * We need to modify the tuple header but it hasn't been copied yet
    1607                 :             :          * (not detoasted, not called by DatumGetHeapTupleHeaderCopy), copy it
    1608                 :             :          * now.
    1609                 :             :          */
    1610         [ +  + ]:          44 :         if (!copy)
    1611                 :             :         {
    1612                 :           8 :             Size        sz = HeapTupleHeaderGetDatumLength(tp);
    1613                 :             :             HeapTupleHeader tp2;
    1614                 :             : 
    1615                 :           8 :             copy = true;
    1616                 :             : 
    1617                 :           8 :             tp2 = (HeapTupleHeader) palloc(sz);
    1618                 :           8 :             memcpy(tp2, tp, sz);
    1619                 :           8 :             tp = tp2;
    1620                 :             :         }
    1621                 :             : 
    1622                 :             :         /*
    1623                 :             :          * Mark as a datum tuple, versioned at version InvalidXLogRecPtr
    1624                 :             :          * unless it's a RECORDOID style datum.
    1625                 :             :          */
    1626                 :          44 :         HeapTupleHeaderSetDatum(tp);
    1627         [ +  - ]:          44 :         if (tupTypeid != RECORDOID)
    1628                 :          44 :             HeapTupleHeaderSetVersion(tp, InvalidXLogRecPtr);
    1629                 :             :     }
    1630                 :             : 
    1631                 :             :     Assert(HeapTupleHeaderReallyIsDatum(tp));
    1632                 :             : 
    1633                 :     1734115 :     return tp;
    1634                 :             : }
    1635                 :             : 
    1636                 :             : HeapTupleHeader
    1637                 :     1734077 : DatumGetHeapTupleHeader(Datum t)
    1638                 :             : {
    1639                 :     1734077 :     return DatumGetHeapTupleHeaderImpl(t, false, NULL);
    1640                 :             : }
    1641                 :             : 
    1642                 :             : /*
    1643                 :             :  * Like DatumGetHeapTupleHeader but returns whether the datum was already
    1644                 :             :  * marked as a datum in *was_datum.
    1645                 :             :  */
    1646                 :             : HeapTupleHeader
    1647                 :          15 : DatumGetHeapTupleHeaderExt(Datum t, bool *was_datum)
    1648                 :             : {
    1649                 :          15 :     return DatumGetHeapTupleHeaderImpl(t, false, was_datum);
    1650                 :             : }
    1651                 :             : 
    1652                 :             : HeapTupleHeader
    1653                 :          23 : DatumGetHeapTupleHeaderCopy(Datum t)
    1654                 :             : {
    1655                 :          23 :     return DatumGetHeapTupleHeaderImpl(t, true, NULL);
    1656                 :             : }
    1657                 :             : 
    1658                 :             : /*
    1659                 :             :  * heap_deform_tuple
    1660                 :             :  *      Given a tuple, extract data into values/isnull arrays; this is
    1661                 :             :  *      the inverse of heap_form_tuple.
    1662                 :             :  *
    1663                 :             :  *      Storage for the values/isnull arrays is provided by the caller;
    1664                 :             :  *      it should be sized according to tupleDesc->natts not
    1665                 :             :  *      HeapTupleHeaderGetNatts(tuple->t_data).
    1666                 :             :  *
    1667                 :             :  *      Note that for pass-by-reference datatypes, the pointer placed
    1668                 :             :  *      in the Datum will point into the given tuple.
    1669                 :             :  *
    1670                 :             :  *      When all or most of a tuple's fields need to be extracted,
    1671                 :             :  *      this routine will be significantly quicker than a loop around
    1672                 :             :  *      heap_getattr; the loop will become O(N^2) as soon as any
    1673                 :             :  *      noncacheable attribute offsets are involved.
    1674                 :             :  */
    1675                 :             : void
    1676                 :     3218506 : heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc,
    1677                 :             :                   Datum *values, bool *isnull)
    1678                 :             : {
    1679                 :     3218506 :     HeapTupleHeader tup = tuple->t_data;
    1680                 :             :     CompactAttribute *cattr;
    1681                 :     3218506 :     bool        hasnulls = HeapTupleHasNulls(tuple);
    1682                 :     3218506 :     int         tdesc_natts = tupleDesc->natts;
    1683                 :             :     int         natts;          /* number of atts to extract */
    1684                 :             :     int         attnum;
    1685                 :             :     char       *tp;             /* ptr to tuple data */
    1686                 :             :     uint32      off;            /* offset in tuple data */
    1687                 :     3218506 :     uint8      *bp = tup->t_bits;    /* ptr to null bitmap in tuple */
    1688                 :             :     int         firstNonCacheOffsetAttr;
    1689                 :             :     int         firstNullAttr;
    1690                 :             : 
    1691         [ +  + ]:     3218506 :     if (HeapTupleHeaderIsVersionedDatum(tup))
    1692                 :       66582 :         heap_datum_check(tup, tupleDesc);
    1693                 :             : 
    1694                 :     3218204 :     natts = HeapTupleHeaderGetNatts(tup);
    1695                 :             : 
    1696                 :             :     /* Did someone forget to call TupleDescFinalize()? */
    1697                 :             :     Assert(tupleDesc->firstNonCachedOffsetAttr >= 0);
    1698                 :             : 
    1699                 :             :     /*
    1700                 :             :      * In inheritance situations, it is possible that the given tuple actually
    1701                 :             :      * has more fields than the caller is expecting.  Don't run off the end of
    1702                 :             :      * the caller's arrays.
    1703                 :             :      */
    1704                 :     3218204 :     natts = Min(natts, tdesc_natts);
    1705                 :     3218204 :     firstNonCacheOffsetAttr = Min(tupleDesc->firstNonCachedOffsetAttr, natts);
    1706                 :             : 
    1707         [ +  + ]:     3218204 :     if (hasnulls)
    1708                 :             :     {
    1709                 :      124069 :         firstNullAttr = first_null_attr(bp, natts);
    1710                 :             : 
    1711                 :             :         /*
    1712                 :             :          * XXX: it'd be nice to use populate_isnull_array() here, but that
    1713                 :             :          * requires that the isnull array's size is rounded up to the next
    1714                 :             :          * multiple of 8.  Doing that would require adjusting many locations
    1715                 :             :          * that allocate the array.
    1716                 :             :          */
    1717                 :      124069 :         firstNonCacheOffsetAttr = Min(firstNonCacheOffsetAttr, firstNullAttr);
    1718                 :             :     }
    1719                 :             :     else
    1720                 :     3094135 :         firstNullAttr = natts;
    1721                 :             : 
    1722                 :     3218204 :     tp = (char *) tup + tup->t_hoff;
    1723                 :     3218204 :     attnum = 0;
    1724                 :             : 
    1725         [ +  + ]:     3218204 :     if (firstNonCacheOffsetAttr > 0)
    1726                 :             :     {
    1727                 :             : #ifdef USE_ASSERT_CHECKING
    1728                 :             :         /* In Assert enabled builds, verify attcacheoff is correct */
    1729                 :             :         int         offcheck = 0;
    1730                 :             : #endif
    1731                 :             :         do
    1732                 :             :         {
    1733                 :     4480038 :             isnull[attnum] = false;
    1734                 :     4480038 :             cattr = TupleDescCompactAttr(tupleDesc, attnum);
    1735                 :     4480038 :             off = cattr->attcacheoff;
    1736                 :             : 
    1737                 :             : #ifdef USE_ASSERT_CHECKING
    1738                 :             :             offcheck = att_nominal_alignby(offcheck, cattr->attalignby);
    1739                 :             :             Assert(offcheck == cattr->attcacheoff);
    1740                 :             :             offcheck += cattr->attlen;
    1741                 :             : #endif
    1742                 :             : 
    1743                 :     8960076 :             values[attnum] = fetch_att_noerr(tp + off,
    1744                 :     4480038 :                                              cattr->attbyval,
    1745                 :     4480038 :                                              cattr->attlen);
    1746         [ +  + ]:     4480038 :         } while (++attnum < firstNonCacheOffsetAttr);
    1747                 :     2622427 :         off += cattr->attlen;
    1748                 :             :     }
    1749                 :             :     else
    1750                 :      595777 :         off = 0;
    1751                 :             : 
    1752         [ +  + ]:     4933939 :     for (; attnum < firstNullAttr; attnum++)
    1753                 :             :     {
    1754                 :     1715735 :         isnull[attnum] = false;
    1755                 :     1715735 :         cattr = TupleDescCompactAttr(tupleDesc, attnum);
    1756                 :     1715735 :         values[attnum] = align_fetch_then_add(tp,
    1757                 :             :                                               &off,
    1758                 :     1715735 :                                               cattr->attbyval,
    1759                 :     1715735 :                                               cattr->attlen,
    1760                 :     1715735 :                                               cattr->attalignby);
    1761                 :             :     }
    1762                 :             : 
    1763         [ +  + ]:     3788364 :     for (; attnum < natts; attnum++)
    1764                 :             :     {
    1765                 :             :         Assert(hasnulls);
    1766                 :             : 
    1767         [ +  + ]:      570160 :         if (att_isnull(attnum, bp))
    1768                 :             :         {
    1769                 :      451270 :             values[attnum] = (Datum) 0;
    1770                 :      451270 :             isnull[attnum] = true;
    1771                 :      451270 :             continue;
    1772                 :             :         }
    1773                 :             : 
    1774                 :      118890 :         isnull[attnum] = false;
    1775                 :      118890 :         cattr = TupleDescCompactAttr(tupleDesc, attnum);
    1776                 :             : 
    1777                 :             :         /* align 'off', fetch the attr's value, and increment off beyond it */
    1778                 :      118890 :         values[attnum] = align_fetch_then_add(tp,
    1779                 :             :                                               &off,
    1780                 :      118890 :                                               cattr->attbyval,
    1781                 :      118890 :                                               cattr->attlen,
    1782                 :      118890 :                                               cattr->attalignby);
    1783                 :             :     }
    1784                 :             : 
    1785                 :             :     /*
    1786                 :             :      * If tuple doesn't have all the atts indicated by tupleDesc, read the
    1787                 :             :      * rest as nulls or missing values as appropriate.
    1788                 :             :      */
    1789         [ +  + ]:     3220838 :     for (; attnum < tdesc_natts; attnum++)
    1790                 :        2634 :         values[attnum] = getmissingattr(tupleDesc, attnum + 1, &isnull[attnum]);
    1791                 :     3218204 : }
    1792                 :             : 
    1793                 :             : /*
    1794                 :             :  * heap_freetuple
    1795                 :             :  */
    1796                 :             : void
    1797                 :    16153274 : heap_freetuple(HeapTuple htup)
    1798                 :             : {
    1799                 :    16153274 :     pfree(htup);
    1800                 :    16153274 : }
    1801                 :             : 
    1802                 :             : 
    1803                 :             : /*
    1804                 :             :  * heap_form_minimal_tuple
    1805                 :             :  *      construct a MinimalTuple from the given values[] and isnull[] arrays,
    1806                 :             :  *      which are of the length indicated by tupleDescriptor->natts
    1807                 :             :  *
    1808                 :             :  * This is exactly like heap_form_tuple() except that the result is a
    1809                 :             :  * "minimal" tuple lacking a HeapTupleData header as well as room for system
    1810                 :             :  * columns.
    1811                 :             :  *
    1812                 :             :  * The result is allocated in the current memory context.
    1813                 :             :  */
    1814                 :             : MinimalTuple
    1815                 :    29560101 : heap_form_minimal_tuple(TupleDesc tupleDescriptor,
    1816                 :             :                         const Datum *values,
    1817                 :             :                         const bool *isnull,
    1818                 :             :                         Size extra)
    1819                 :             : {
    1820                 :             :     MinimalTuple tuple;         /* return tuple */
    1821                 :             :     char       *mem;
    1822                 :             :     Size        len,
    1823                 :             :                 data_len;
    1824                 :             :     int         hoff;
    1825                 :    29560101 :     bool        hasnull = false;
    1826                 :    29560101 :     int         numberOfAttributes = tupleDescriptor->natts;
    1827                 :             :     int         i;
    1828                 :             : 
    1829                 :             :     Assert(extra == MAXALIGN(extra));
    1830                 :             : 
    1831         [ -  + ]:    29560101 :     if (numberOfAttributes > MaxTupleAttributeNumber)
    1832         [ #  # ]:           0 :         ereport(ERROR,
    1833                 :             :                 (errcode(ERRCODE_TOO_MANY_COLUMNS),
    1834                 :             :                  errmsg("number of columns (%d) exceeds limit (%d)",
    1835                 :             :                         numberOfAttributes, MaxTupleAttributeNumber)));
    1836                 :             : 
    1837                 :             :     /*
    1838                 :             :      * Check for nulls
    1839                 :             :      */
    1840         [ +  + ]:    83628483 :     for (i = 0; i < numberOfAttributes; i++)
    1841                 :             :     {
    1842         [ +  + ]:    54543013 :         if (isnull[i])
    1843                 :             :         {
    1844                 :      474631 :             hasnull = true;
    1845                 :      474631 :             break;
    1846                 :             :         }
    1847                 :             :     }
    1848                 :             : 
    1849                 :             :     /*
    1850                 :             :      * Determine total space needed
    1851                 :             :      */
    1852                 :    29560101 :     len = SizeofMinimalTupleHeader;
    1853                 :             : 
    1854         [ +  + ]:    29560101 :     if (hasnull)
    1855                 :      474631 :         len += BITMAPLEN(numberOfAttributes);
    1856                 :             : 
    1857                 :    29560101 :     hoff = len = MAXALIGN(len); /* align user data safely */
    1858                 :             : 
    1859                 :    29560101 :     data_len = heap_compute_data_size(tupleDescriptor, values, isnull, false);
    1860                 :             : 
    1861                 :    29560101 :     len += data_len;
    1862                 :             : 
    1863                 :             :     /*
    1864                 :             :      * Allocate and zero the space needed.
    1865                 :             :      */
    1866                 :    29560101 :     mem = palloc0(len + extra);
    1867                 :    29560101 :     tuple = (MinimalTuple) (mem + extra);
    1868                 :             : 
    1869                 :             :     /*
    1870                 :             :      * And fill in the information.
    1871                 :             :      */
    1872                 :    29560101 :     tuple->t_len = len;
    1873                 :    29560101 :     HeapTupleHeaderSetNatts(tuple, numberOfAttributes);
    1874                 :    29560101 :     tuple->t_hoff = hoff + MINIMAL_TUPLE_OFFSET;
    1875                 :             : 
    1876         [ +  + ]:    29560101 :     heap_fill_tuple(tupleDescriptor,
    1877                 :             :                     values,
    1878                 :             :                     isnull,
    1879                 :             :                     (char *) tuple + hoff,
    1880                 :             :                     data_len,
    1881                 :             :                     &tuple->t_infomask,
    1882                 :             :                     (hasnull ? tuple->t_bits : NULL));
    1883                 :             : 
    1884                 :    29560101 :     return tuple;
    1885                 :             : }
    1886                 :             : 
    1887                 :             : /*
    1888                 :             :  * heap_free_minimal_tuple
    1889                 :             :  */
    1890                 :             : void
    1891                 :    19957412 : heap_free_minimal_tuple(MinimalTuple mtup)
    1892                 :             : {
    1893                 :    19957412 :     pfree(mtup);
    1894                 :    19957412 : }
    1895                 :             : 
    1896                 :             : /*
    1897                 :             :  * heap_copy_minimal_tuple
    1898                 :             :  *      copy a MinimalTuple
    1899                 :             :  *
    1900                 :             :  * The result is allocated in the current memory context.
    1901                 :             :  */
    1902                 :             : MinimalTuple
    1903                 :     3436528 : heap_copy_minimal_tuple(MinimalTuple mtup, Size extra)
    1904                 :             : {
    1905                 :             :     MinimalTuple result;
    1906                 :             :     char       *mem;
    1907                 :             : 
    1908                 :             :     Assert(extra == MAXALIGN(extra));
    1909                 :     3436528 :     mem = palloc(mtup->t_len + extra);
    1910                 :     3436528 :     memset(mem, 0, extra);
    1911                 :     3436528 :     result = (MinimalTuple) (mem + extra);
    1912                 :     3436528 :     memcpy(result, mtup, mtup->t_len);
    1913                 :     3436528 :     return result;
    1914                 :             : }
    1915                 :             : 
    1916                 :             : /*
    1917                 :             :  * heap_tuple_from_minimal_tuple
    1918                 :             :  *      create a HeapTuple by copying from a MinimalTuple;
    1919                 :             :  *      system columns are filled with zeroes
    1920                 :             :  *
    1921                 :             :  * The result is allocated in the current memory context.
    1922                 :             :  * The HeapTuple struct, tuple header, and tuple data are all allocated
    1923                 :             :  * as a single palloc() block.
    1924                 :             :  */
    1925                 :             : HeapTuple
    1926                 :      652262 : heap_tuple_from_minimal_tuple(MinimalTuple mtup)
    1927                 :             : {
    1928                 :             :     HeapTuple   result;
    1929                 :      652262 :     uint32      len = mtup->t_len + MINIMAL_TUPLE_OFFSET;
    1930                 :             : 
    1931                 :      652262 :     result = (HeapTuple) palloc(HEAPTUPLESIZE + len);
    1932                 :      652262 :     result->t_len = len;
    1933                 :      652262 :     ItemPointerSetInvalid(&(result->t_self));
    1934                 :      652262 :     result->t_tableOid = InvalidOid;
    1935                 :      652262 :     result->t_data = (HeapTupleHeader) ((char *) result + HEAPTUPLESIZE);
    1936                 :      652262 :     memcpy((char *) result->t_data + MINIMAL_TUPLE_OFFSET, mtup, mtup->t_len);
    1937                 :      652262 :     memset(result->t_data, 0, offsetof(HeapTupleHeaderData, t_infomask2));
    1938                 :      652262 :     return result;
    1939                 :             : }
    1940                 :             : 
    1941                 :             : /*
    1942                 :             :  * minimal_tuple_from_heap_tuple
    1943                 :             :  *      create a MinimalTuple by copying from a HeapTuple
    1944                 :             :  *
    1945                 :             :  * The result is allocated in the current memory context.
    1946                 :             :  */
    1947                 :             : MinimalTuple
    1948                 :     3085218 : minimal_tuple_from_heap_tuple(HeapTuple htup, Size extra)
    1949                 :             : {
    1950                 :             :     MinimalTuple result;
    1951                 :             :     char       *mem;
    1952                 :             :     uint32      len;
    1953                 :             : 
    1954                 :             :     Assert(extra == MAXALIGN(extra));
    1955                 :             :     Assert(htup->t_len > MINIMAL_TUPLE_OFFSET);
    1956                 :     3085218 :     len = htup->t_len - MINIMAL_TUPLE_OFFSET;
    1957                 :     3085218 :     mem = palloc(len + extra);
    1958                 :     3085218 :     memset(mem, 0, extra);
    1959                 :     3085218 :     result = (MinimalTuple) (mem + extra);
    1960                 :     3085218 :     memcpy(result, (char *) htup->t_data + MINIMAL_TUPLE_OFFSET, len);
    1961                 :             : 
    1962                 :             :     /*
    1963                 :             :      * A minimal tuple won't have the necessary fields to be treated as a
    1964                 :             :      * datum tuple, so we better reset those fields lest they trigger assert
    1965                 :             :      * failures at a later point.
    1966                 :             :      *
    1967                 :             :      * XXX: It'd be good to call heap_datum_check() before losing the ability
    1968                 :             :      * to do so, but we don't have the tupledesc, so we can't trivially do so.
    1969                 :             :      * We could try to fetch the tupledesc from typeid and verify that way?
    1970                 :             :      * But that's not cheap.
    1971                 :             :      */
    1972         [ +  + ]:     3085218 :     if (HeapTupleHeaderIsDatum(htup->t_data))
    1973                 :             :     {
    1974                 :             :         /* like HeapTupleHeaderClearDatum() */
    1975                 :     1188031 :         result->t_infomask &= ~HEAP_IS_DATUM;
    1976                 :             :     }
    1977                 :             : 
    1978                 :     3085218 :     result->t_len = len;
    1979                 :     3085218 :     return result;
    1980                 :             : }
    1981                 :             : 
    1982                 :             : /*
    1983                 :             :  * This mainly exists so JIT can inline the definition, but it's also
    1984                 :             :  * sometimes useful in debugging sessions.
    1985                 :             :  */
    1986                 :             : size_t
    1987                 :           0 : varsize_any(void *p)
    1988                 :             : {
    1989                 :           0 :     return VARSIZE_ANY(p);
    1990                 :             : }
        

Generated by: LCOV version 2.0-1