LCOV - differential code coverage report
Current view: top level - src/backend/access/common - heaptuple.c (source / functions) Coverage Total Hit UBC GNC CBC DUB DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 94.1 % 555 522 33 3 519 110 5
Current Date: 2026-11-28 16:48:44 +0900 Functions: 97.2 % 36 35 1 3 32 4 2
Baseline: lcov-20261128-baseline Branches: 70.7 % 382 270 112 270
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 0.0 % 1 0 1
(7,30] days: 92.8 % 167 155 12 2 153
(30,360] days: 100.0 % 82 82 82
(360..) days: 93.4 % 305 285 20 1 284
Function coverage date bins:
(7,30] days: 100.0 % 13 13 13
(30,360] days: 100.0 % 1 1 1
(360..) days: 95.5 % 22 21 1 3 18
Branch coverage date bins:
(1,7] days: 0.0 % 2 0 2
(7,30] days: 64.9 % 154 100 54 100
(30,360] days: 84.4 % 64 54 10 54
(360..) days: 71.6 % 162 116 46 116

 Age         Owner                    Branch data    TLA  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
 1194 andrew@dunslane.net       117                 :CBC          21 : missing_hash(const void *key, Size keysize)
                                118                 :                : {
  320 peter@eisentraut.org      119                 :             21 :     const missing_cache_key *entry = key;
                                120                 :                : 
  477                           121                 :             21 :     return hash_bytes((const unsigned char *) DatumGetPointer(entry->value), entry->len);
                                122                 :                : }
                                123                 :                : 
                                124                 :                : static int
 1194 andrew@dunslane.net       125                 :             15 : missing_match(const void *key1, const void *key2, Size keysize)
                                126                 :                : {
  320 peter@eisentraut.org      127                 :             15 :     const missing_cache_key *entry1 = key1;
                                128                 :             15 :     const missing_cache_key *entry2 = key2;
                                129                 :                : 
 1194 andrew@dunslane.net       130         [ -  + ]:             15 :     if (entry1->len != entry2->len)
 1194 andrew@dunslane.net       131         [ #  # ]:UBC           0 :         return entry1->len > entry2->len ? 1 : -1;
                                132                 :                : 
 1194 andrew@dunslane.net       133                 :CBC          15 :     return memcmp(DatumGetPointer(entry1->value),
                                134                 :             15 :                   DatumGetPointer(entry2->value),
                                135                 :             15 :                   entry1->len);
                                136                 :                : }
                                137                 :                : 
                                138                 :                : static void
  360 nathan@postgresql.or      139                 :              6 : init_missing_cache(void)
                                140                 :                : {
                                141                 :                :     HASHCTL     hash_ctl;
                                142                 :                : 
 1194 andrew@dunslane.net       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
 3167                           164                 :           2883 : getmissingattr(TupleDesc tupleDesc,
                                165                 :                :                int attnum, bool *isnull)
                                166                 :                : {
                                167                 :                :     CompactAttribute *att;
                                168                 :                : 
                                169         [ -  + ]:           2883 :     Assert(attnum <= tupleDesc->natts);
                                170         [ -  + ]:           2883 :     Assert(attnum > 0);
                                171                 :                : 
  708 drowley@postgresql.o      172                 :           2883 :     att = TupleDescCompactAttr(tupleDesc, attnum - 1);
                                173                 :                : 
 3167 andrew@dunslane.net       174         [ +  + ]:           2883 :     if (att->atthasmissing)
                                175                 :                :     {
                                176                 :                :         AttrMissing *attrmiss;
                                177                 :                : 
                                178         [ -  + ]:            104 :         Assert(tupleDesc->constr);
                                179         [ -  + ]:            104 :         Assert(tupleDesc->constr->missing);
                                180                 :                : 
                                181                 :            104 :         attrmiss = tupleDesc->constr->missing + (attnum - 1);
                                182                 :                : 
 3076 akapila@postgresql.o      183         [ +  - ]:            104 :         if (attrmiss->am_present)
                                184                 :                :         {
                                185                 :                :             missing_cache_key key;
                                186                 :                :             missing_cache_key *entry;
                                187                 :                :             bool        found;
                                188                 :                :             MemoryContext oldctx;
                                189                 :                : 
 3167 andrew@dunslane.net       190                 :            104 :             *isnull = false;
                                191                 :                : 
                                192                 :                :             /* no  need to cache by-value attributes */
 1194                           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   [ +  -  -  + ]:             21 :             Assert(att->attlen > 0 || att->attlen == -1);
                                202         [ -  + ]:             21 :             if (att->attlen > 0)
 1194 andrew@dunslane.net       203                 :UBC           0 :                 key.len = att->attlen;
                                204                 :                :             else
  480 peter@eisentraut.org      205                 :CBC          21 :                 key.len = VARSIZE_ANY(DatumGetPointer(attrmiss->am_value));
 1194 andrew@dunslane.net       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                 :                : 
 3167                           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
 7927 tgl@sss.pgh.pa.us         237                 :GNC    82081391 : heap_compute_data_size(TupleDesc tupleDesc,
                                238                 :                :                        const Datum *values,
                                239                 :                :                        const bool *isnull,
                                240                 :                :                        bool allow_huge)
                                241                 :                : {
 7927 tgl@sss.pgh.pa.us         242                 :CBC    82081391 :     Size        data_length = 0;
                                243                 :       82081391 :     int         numberOfAttributes = tupleDesc->natts;
                                244                 :                : 
   19 alvherre@kurilemu.de      245         [ +  + ]:      289386422 :     for (int i = 0; i < numberOfAttributes; i++)
                                246                 :                :     {
                                247                 :                :         Datum       val;
                                248                 :                :         CompactAttribute *atti;
                                249                 :                :         bool        ovfl;
                                250                 :                : 
 7927 tgl@sss.pgh.pa.us         251         [ +  + ]:      207305031 :         if (isnull[i])
                                252                 :       21869838 :             continue;
                                253                 :                : 
 7176                           254                 :      185435193 :         val = values[i];
  708 drowley@postgresql.o      255                 :      185435193 :         atti = TupleDescCompactAttr(tupleDesc, i);
                                256                 :                : 
                                257   [ +  +  +  +  :      217682455 :         if (COMPACT_ATTR_IS_PACKABLE(atti) &&
                                              +  + ]
 7176 tgl@sss.pgh.pa.us         258                 :       32247262 :             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                 :                :              */
   19 alvherre@kurilemu.de      264                 :       27081567 :             ovfl = pg_add_size_overflow(data_length,
                                265                 :       27081567 :                                         VARATT_CONVERTED_SHORT_SIZE(DatumGetPointer(val)),
                                266                 :                :                                         &data_length);
                                267                 :                :         }
 4216 tgl@sss.pgh.pa.us         268   [ +  +  +  + ]:      164697674 :         else if (atti->attlen == -1 &&
                                269                 :        6344048 :                  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                 :                :              */
  707 drowley@postgresql.o      275                 :           1883 :             data_length = att_nominal_alignby(data_length, atti->attalignby);
   19 alvherre@kurilemu.de      276                 :           1883 :             ovfl = pg_add_size_overflow(data_length,
                                277                 :                :                                         EOH_get_flat_size(DatumGetEOHP(val)),
                                278                 :                :                                         &data_length);
                                279                 :                :         }
                                280                 :                :         else
                                281                 :                :         {
  707 drowley@postgresql.o      282   [ +  +  +  + ]:      158351743 :             data_length = att_datum_alignby(data_length, atti->attalignby,
                                283                 :                :                                             atti->attlen, val);
   19 alvherre@kurilemu.de      284                 :      158351743 :             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   [ +  -  +  +  :      185435193 :         if (ovfl || (!allow_huge && !AllocSizeIsValid(data_length)))
                                              -  + ]
   19 alvherre@kurilemu.de      295         [ #  # ]:UBC           0 :             ereport(ERROR,
                                296                 :                :                     errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
                                297                 :                :                     errmsg("tuple is too big"));
                                298                 :                :     }
                                299                 :                : 
 7927 tgl@sss.pgh.pa.us         300                 :CBC    82081391 :     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
  708 drowley@postgresql.o      309                 :      192677635 : 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;
 3167 andrew@dunslane.net       318                 :      192677635 :     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         [ +  + ]:      192677635 :     if (bit != NULL)
                                325                 :                :     {
                                326         [ +  + ]:       77822274 :         if (*bitmask != HIGHBIT)
                                327                 :       64648429 :             *bitmask <<= 1;
                                328                 :                :         else
                                329                 :                :         {
                                330                 :       13173845 :             *bit += 1;
                                331                 :       13173845 :             **bit = 0x0;
                                332                 :       13173845 :             *bitmask = 1;
                                333                 :                :         }
                                334                 :                : 
                                335         [ +  + ]:       77822274 :         if (isnull)
                                336                 :                :         {
                                337                 :       21705135 :             *infomask |= HEAP_HASNULL;
                                338                 :       21705135 :             return;
                                339                 :                :         }
                                340                 :                : 
                                341                 :       56117139 :         **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         [ +  + ]:      170972500 :     if (att->attbyval)
                                349                 :                :     {
                                350                 :                :         /* pass-by-value */
  707 drowley@postgresql.o      351                 :      123480906 :         data = (char *) att_nominal_alignby(data, att->attalignby);
 3167 andrew@dunslane.net       352                 :      123480906 :         store_att_byval(data, datum, att->attlen);
                                353                 :      123480906 :         data_length = att->attlen;
                                354                 :                :     }
                                355         [ +  + ]:       47491594 :     else if (att->attlen == -1)
                                356                 :                :     {
                                357                 :                :         /* varlena */
                                358                 :       32356666 :         Pointer     val = DatumGetPointer(datum);
                                359                 :                : 
                                360                 :       32356666 :         *infomask |= HEAP_HASVARWIDTH;
                                361         [ +  + ]:       32356666 :         if (VARATT_IS_EXTERNAL(val))
                                362                 :                :         {
                                363         [ +  + ]:          14437 :             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                 :                : 
  707 drowley@postgresql.o      371                 :           1883 :                 data = (char *) att_nominal_alignby(data, att->attalignby);
 3167 andrew@dunslane.net       372                 :           1883 :                 data_length = EOH_get_flat_size(eoh);
                                373                 :           1883 :                 EOH_flatten_into(eoh, data, data_length);
                                374                 :                :             }
                                375                 :                :             else
                                376                 :                :             {
                                377                 :          12554 :                 *infomask |= HEAP_HASEXTERNAL;
                                378                 :                :                 /* no alignment, since it's short by definition */
                                379                 :          12554 :                 data_length = VARSIZE_EXTERNAL(val);
                                380                 :          12554 :                 memcpy(data, val, data_length);
                                381                 :                :             }
                                382                 :                :         }
                                383         [ +  + ]:       32342229 :         else if (VARATT_IS_SHORT(val))
                                384                 :                :         {
                                385                 :                :             /* no alignment for short varlenas */
                                386                 :        3894785 :             data_length = VARSIZE_SHORT(val);
                                387                 :        3894785 :             memcpy(data, val, data_length);
                                388                 :                :         }
  708 drowley@postgresql.o      389   [ +  +  +  + ]:       28447444 :         else if (att->attispackable && VARATT_CAN_MAKE_SHORT(val))
                                390                 :                :         {
                                391                 :                :             /* convert to short varlena -- no alignment */
 3167 andrew@dunslane.net       392                 :       26457055 :             data_length = VARATT_CONVERTED_SHORT_SIZE(val);
                                393                 :       26457055 :             SET_VARSIZE_SHORT(data, data_length);
                                394                 :       26457055 :             memcpy(data + 1, VARDATA(val), data_length - 1);
                                395                 :                :         }
                                396                 :                :         else
                                397                 :                :         {
                                398                 :                :             /* full 4-byte header varlena */
  707 drowley@postgresql.o      399                 :        1990389 :             data = (char *) att_nominal_alignby(data, att->attalignby);
 3167 andrew@dunslane.net       400                 :        1990389 :             data_length = VARSIZE(val);
                                401                 :        1990389 :             memcpy(data, val, data_length);
                                402                 :                :         }
                                403                 :                :     }
                                404         [ +  + ]:       15134928 :     else if (att->attlen == -2)
                                405                 :                :     {
                                406                 :                :         /* cstring ... never needs alignment */
                                407                 :        1581680 :         *infomask |= HEAP_HASVARWIDTH;
  707 drowley@postgresql.o      408         [ -  + ]:        1581680 :         Assert(att->attalignby == sizeof(char));
 3167 andrew@dunslane.net       409                 :        1581680 :         data_length = strlen(DatumGetCString(datum)) + 1;
                                410                 :        1581680 :         memcpy(data, DatumGetPointer(datum), data_length);
                                411                 :                :     }
                                412                 :                :     else
                                413                 :                :     {
                                414                 :                :         /* fixed-length pass-by-reference */
  707 drowley@postgresql.o      415                 :       13553248 :         data = (char *) att_nominal_alignby(data, att->attalignby);
 3167 andrew@dunslane.net       416         [ -  + ]:       13553248 :         Assert(att->attlen > 0);
                                417                 :       13553248 :         data_length = att->attlen;
                                418                 :       13553248 :         memcpy(data, DatumGetPointer(datum), data_length);
                                419                 :                :     }
                                420                 :                : 
                                421                 :      170972500 :     data += data_length;
                                422                 :      170972500 :     *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
 7927 tgl@sss.pgh.pa.us         435                 :       68903491 : 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                 :       68903491 :     int         numberOfAttributes = tupleDesc->natts;
                                444                 :                : 
                                445                 :                : #ifdef USE_ASSERT_CHECKING
 7176                           446                 :       68903491 :     char       *start = data;
                                447                 :                : #endif
                                448                 :                : 
 7927                           449         [ +  + ]:       68903491 :     if (bit != NULL)
                                450                 :                :     {
                                451                 :        5253845 :         bitP = &bit[-1];
 7643 bruce@momjian.us          452                 :        5253845 :         bitmask = HIGHBIT;
                                453                 :                :     }
                                454                 :                :     else
                                455                 :                :     {
                                456                 :                :         /* just to keep compiler quiet */
 7927 tgl@sss.pgh.pa.us         457                 :       63649646 :         bitP = NULL;
                                458                 :       63649646 :         bitmask = 0;
                                459                 :                :     }
                                460                 :                : 
 7176                           461                 :       68903491 :     *infomask &= ~(HEAP_HASNULL | HEAP_HASVARWIDTH | HEAP_HASEXTERNAL);
                                462                 :                : 
 7927                           463         [ +  + ]:      261581126 :     for (i = 0; i < numberOfAttributes; i++)
                                464                 :                :     {
  708 drowley@postgresql.o      465                 :      192677635 :         CompactAttribute *attr = TupleDescCompactAttr(tupleDesc, i);
                                466                 :                : 
 3167 andrew@dunslane.net       467   [ +  -  +  + ]:      578032905 :         fill_val(attr,
                                468         [ +  + ]:      192677635 :                  bitP ? &bitP : NULL,
                                469                 :                :                  &bitmask,
                                470                 :                :                  &data,
                                471                 :                :                  infomask,
                                472                 :      192677274 :                  values ? values[i] : PointerGetDatum(NULL),
                                473                 :      192677635 :                  isnull ? isnull[i] : true);
                                474                 :                :     }
                                475                 :                : 
 7176 tgl@sss.pgh.pa.us         476         [ -  + ]:       68903491 :     Assert((data - start) == data_size);
 7927                           477                 :       68903491 : }
                                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
 3167 andrew@dunslane.net       490                 :        6961215 : 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   [ +  +  -  + ]:        6961215 :     Assert(!tupleDesc || attnum <= tupleDesc->natts);
 7263 bruce@momjian.us          497         [ -  + ]:        6961215 :     if (attnum > (int) HeapTupleHeaderGetNatts(tup->t_data))
                                498                 :                :     {
  708 drowley@postgresql.o      499   [ #  #  #  # ]:UBC           0 :         if (tupleDesc &&
                                500                 :              0 :             TupleDescCompactAttr(tupleDesc, attnum - 1)->atthasmissing)
 3167 andrew@dunslane.net       501                 :              0 :             return false;
                                502                 :                :         else
                                503                 :              0 :             return true;
                                504                 :                :     }
                                505                 :                : 
10674 bruce@momjian.us          506         [ +  - ]:CBC     6961215 :     if (attnum > 0)
                                507                 :                :     {
 8212 tgl@sss.pgh.pa.us         508         [ +  + ]:        6961215 :         if (HeapTupleNoNulls(tup))
 7927                           509                 :           1994 :             return false;
10228 vadim4o@yahoo.com         510                 :        6959221 :         return att_isnull(attnum - 1, tup->t_data->t_bits);
                                511                 :                :     }
                                512                 :                : 
 8212 tgl@sss.pgh.pa.us         513         [ #  # ]:UBC           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                 :                : 
 7927                           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
 1531 pg@bowt.ie                543                 :CBC   140507744 : nocachegetattr(HeapTuple tup,
                                544                 :                :                int attnum,
                                545                 :                :                TupleDesc tupleDesc)
                                546                 :                : {
                                547                 :                :     CompactAttribute *cattr;
                                548                 :      140507744 :     HeapTupleHeader td = tup->t_data;
                                549                 :                :     char       *tp;             /* ptr to data part of tuple */
  243 nathan@postgresql.or      550                 :      140507744 :     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;
  257 drowley@postgresql.o      555                 :      140507744 :     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                 :                :      */
   19 alvherre@kurilemu.de      561         [ -  + ]:      140507744 :     Assert(AllocSizeIsValid(tup->t_len));
                                562                 :                : 
                                563                 :                :     /* Did someone forget to call TupleDescFinalize()? */
  257 drowley@postgresql.o      564         [ -  + ]:      140507744 :     Assert(tupleDesc->firstNonCachedOffsetAttr >= 0);
                                565                 :                : 
 7176 tgl@sss.pgh.pa.us         566                 :      140507744 :     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                 :                :      */
  257 drowley@postgresql.o      573         [ +  + ]:      140507744 :     if (hasnulls)
                                574                 :      101166064 :         firstNullAttr = first_null_attr(bp, attnum);
                                575                 :                :     else
                                576                 :       39341680 :         firstNullAttr = attnum;
                                577                 :                : 
  256                           578   [ +  +  +  + ]:      140507744 :     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                 :       51085180 :         startAttr = Min(tupleDesc->firstNonCachedOffsetAttr - 1, firstNullAttr - 1);
  257                           586                 :       51085180 :         off = TupleDescCompactAttr(tupleDesc, startAttr)->attcacheoff;
                                587                 :                :     }
                                588                 :                :     else
                                589                 :                :     {
                                590                 :                :         /* Otherwise, start at the beginning... */
                                591                 :       89422564 :         startAttr = 0;
                                592                 :       89422564 :         off = 0;
                                593                 :                :     }
                                594                 :                : 
 1531 pg@bowt.ie                595                 :      140507744 :     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                 :                :      */
  257 drowley@postgresql.o      603         [ +  + ]:      140507744 :     if (!HeapTupleHasVarWidth(tup))
                                604                 :                :     {
                                605         [ +  + ]:      105079659 :         for (i = startAttr; i < firstNullAttr; i++)
                                606                 :                :         {
                                607                 :       29071964 :             cattr = TupleDescCompactAttr(tupleDesc, i);
                                608                 :                : 
                                609                 :       29071964 :             off = att_nominal_alignby(off, cattr->attalignby);
                                610                 :       29071964 :             off += cattr->attlen;
                                611                 :                :         }
                                612                 :                : 
                                613         [ +  + ]:       81226994 :         for (; i < attnum; i++)
                                614                 :                :         {
                                615         [ +  + ]:        5219299 :             if (att_isnull(i, bp))
                                616                 :        4274339 :                 continue;
                                617                 :                : 
                                618                 :         944960 :             cattr = TupleDescCompactAttr(tupleDesc, i);
                                619                 :                : 
                                620                 :         944960 :             off = att_nominal_alignby(off, cattr->attalignby);
                                621                 :         944960 :             off += cattr->attlen;
                                622                 :                :         }
                                623                 :                :     }
                                624                 :                :     else
                                625                 :                :     {
                                626         [ +  + ]:      163820980 :         for (i = startAttr; i < firstNullAttr; i++)
                                627                 :                :         {
                                628                 :                :             int         attlen;
                                629                 :                : 
                                630                 :       99320931 :             cattr = TupleDescCompactAttr(tupleDesc, i);
                                631                 :       99320931 :             attlen = cattr->attlen;
                                632   [ +  +  +  + ]:       99320931 :             off = att_pointer_alignby(off,
                                633                 :                :                                       cattr->attalignby,
                                634                 :                :                                       attlen,
                                635                 :                :                                       tp + off);
                                636   [ +  +  +  -  :       99320931 :             off = att_addlength_pointer(off, attlen, tp + off);
                                              -  - ]
                                637                 :                :         }
                                638                 :                : 
                                639         [ +  + ]:       76240662 :         for (; i < attnum; i++)
                                640                 :                :         {
                                641                 :                :             int         attlen;
                                642                 :                : 
                                643         [ +  + ]:       11740613 :             if (att_isnull(i, bp))
                                644                 :        8770764 :                 continue;
                                645                 :                : 
                                646                 :        2969849 :             cattr = TupleDescCompactAttr(tupleDesc, i);
                                647                 :        2969849 :             attlen = cattr->attlen;
                                648   [ +  +  +  + ]:        2969849 :             off = att_pointer_alignby(off, cattr->attalignby, attlen,
                                649                 :                :                                       tp + off);
                                650   [ +  +  +  -  :        2969849 :             off = att_addlength_pointer(off, attlen, tp + off);
                                              -  - ]
                                651                 :                :         }
                                652                 :                :     }
                                653                 :                : 
                                654                 :      140507744 :     cattr = TupleDescCompactAttr(tupleDesc, attnum);
                                655   [ +  +  +  + ]:      140507744 :     off = att_pointer_alignby(off,
                                656                 :                :                               cattr->attalignby,
                                657                 :                :                               cattr->attlen,
                                658                 :                :                               tp + off);
                                659                 :                : 
                                660                 :      140507744 :     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
 8276 tgl@sss.pgh.pa.us         673                 :          72851 : heap_getsysattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
                                674                 :                : {
                                675                 :                :     Datum       result;
                                676                 :                : 
 9467                           677         [ -  + ]:          72851 :     Assert(tup);
                                678                 :                : 
                                679                 :                :     /* Currently, no sys attribute ever reads as NULL. */
 6166 rhaas@postgresql.org      680                 :          72851 :     *isnull = false;
                                681                 :                : 
 9467 tgl@sss.pgh.pa.us         682   [ +  +  +  +  :          72851 :     switch (attnum)
                                              +  - ]
                                683                 :                :     {
                                684                 :             17 :         case SelfItemPointerAttributeNumber:
                                685                 :                :             /* pass-by-reference datatype */
                                686                 :             17 :             result = PointerGetDatum(&(tup->t_self));
                                687                 :             17 :             break;
                                688                 :          72588 :         case MinTransactionIdAttributeNumber:
 4724 rhaas@postgresql.org      689                 :          72588 :             result = TransactionIdGetDatum(HeapTupleHeaderGetRawXmin(tup->t_data));
 9467 tgl@sss.pgh.pa.us         690                 :          72588 :             break;
                                691                 :            119 :         case MaxTransactionIdAttributeNumber:
 5057 alvherre@alvh.no-ip.      692                 :            119 :             result = TransactionIdGetDatum(HeapTupleHeaderGetRawXmax(tup->t_data));
 9467 tgl@sss.pgh.pa.us         693                 :            119 :             break;
 7232                           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));
 9467                           704                 :            124 :             break;
                                705                 :              3 :         case TableOidAttributeNumber:
                                706                 :              3 :             result = ObjectIdGetDatum(tup->t_tableOid);
                                707                 :              3 :             break;
 9467 tgl@sss.pgh.pa.us         708                 :UBC           0 :         default:
 8531                           709         [ #  # ]:              0 :             elog(ERROR, "invalid attnum: %d", attnum);
                                710                 :                :             result = 0;         /* keep compiler quiet */
                                711                 :                :             break;
                                712                 :                :     }
 9467 tgl@sss.pgh.pa.us         713                 :CBC       72851 :     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
11099 scrappy@hub.org           726                 :       10769484 : heap_copytuple(HeapTuple tuple)
                                727                 :                : {
                                728                 :                :     HeapTuple   newTuple;
                                729                 :                : 
10228 vadim4o@yahoo.com         730   [ +  -  -  + ]:       10769484 :     if (!HeapTupleIsValid(tuple) || tuple->t_data == NULL)
10315 bruce@momjian.us          731                 :UBC           0 :         return NULL;
                                732                 :                : 
10228 vadim4o@yahoo.com         733                 :CBC    10769484 :     newTuple = (HeapTuple) palloc(HEAPTUPLESIZE + tuple->t_len);
                                734                 :       10769484 :     newTuple->t_len = tuple->t_len;
                                735                 :       10769484 :     newTuple->t_self = tuple->t_self;
 9510 tgl@sss.pgh.pa.us         736                 :       10769484 :     newTuple->t_tableOid = tuple->t_tableOid;
10228 vadim4o@yahoo.com         737                 :       10769484 :     newTuple->t_data = (HeapTupleHeader) ((char *) newTuple + HEAPTUPLESIZE);
  654 peter@eisentraut.org      738                 :       10769484 :     memcpy(newTuple->t_data, tuple->t_data, tuple->t_len);
10315 bruce@momjian.us          739                 :       10769484 :     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
 4594 tgl@sss.pgh.pa.us         749                 :          59260 : 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         [ +  + ]:          59260 :     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                 :          59252 :     td = (HeapTupleHeader) palloc(tuple->t_len);
  654 peter@eisentraut.org      768                 :          59252 :     memcpy(td, tuple->t_data, tuple->t_len);
                                769                 :                : 
                                770                 :                :     /* if wrongly marked, set the type from scratch */
   19 andres@anarazel.de        771         [ +  + ]:          59252 :     if (HeapTupleHeaderReallyIsDatum(td))
                                772                 :          18739 :         heap_datum_update_type(td, tupleDesc);
                                773                 :                :     else
                                774                 :          40513 :         heap_tuple_set_datum(td, tupleDesc, tuple->t_len);
                                775                 :                : 
 4594 tgl@sss.pgh.pa.us         776                 :          59248 :     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
 7927                           787                 :       20041016 : 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                 :       20041016 :     bool        hasnull = false;
                                797                 :       20041016 :     int         numberOfAttributes = tupleDescriptor->natts;
                                798                 :                :     int         i;
                                799                 :                : 
                                800         [ -  + ]:       20041016 :     if (numberOfAttributes > MaxTupleAttributeNumber)
 7927 tgl@sss.pgh.pa.us         801         [ #  # ]:UBC           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                 :                :      */
 7927 tgl@sss.pgh.pa.us         809         [ +  + ]:CBC    90017521 :     for (i = 0; i < numberOfAttributes; i++)
                                810                 :                :     {
                                811         [ +  + ]:       74663054 :         if (isnull[i])
                                812                 :                :         {
 4594                           813                 :        4686549 :             hasnull = true;
                                814                 :        4686549 :             break;
                                815                 :                :         }
                                816                 :                :     }
                                817                 :                : 
                                818                 :                :     /*
                                819                 :                :      * Determine total space needed
                                820                 :                :      */
 7927                           821                 :       20041016 :     len = offsetof(HeapTupleHeaderData, t_bits);
                                822                 :                : 
                                823         [ +  + ]:       20041016 :     if (hasnull)
                                824                 :        4686549 :         len += BITMAPLEN(numberOfAttributes);
                                825                 :                : 
                                826                 :       20041016 :     hoff = len = MAXALIGN(len); /* align user data safely */
                                827                 :                : 
   19 alvherre@kurilemu.de      828                 :GNC    20041016 :     data_len = heap_compute_data_size(tupleDescriptor, values, isnull, false);
                                829                 :                : 
 7176 tgl@sss.pgh.pa.us         830                 :CBC    20041016 :     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                 :                :      */
 3489 alvherre@alvh.no-ip.      836                 :       20041016 :     tuple = (HeapTuple) palloc0(HEAPTUPLESIZE + len);
 7927 tgl@sss.pgh.pa.us         837                 :       20041016 :     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                 :       20041016 :     tuple->t_len = len;
                                845                 :       20041016 :     ItemPointerSetInvalid(&(tuple->t_self));
                                846                 :       20041016 :     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                 :                :      */
 7263 bruce@momjian.us          858                 :       20041016 :     HeapTupleHeaderSetNatts(td, numberOfAttributes);
 7927 tgl@sss.pgh.pa.us         859                 :       20041016 :     td->t_hoff = hoff;
                                860                 :                : 
   19 andres@anarazel.de        861                 :       20041016 :     heap_tuple_set_datum(td, tupleDescriptor, len);
                                862                 :                : 
 7927 tgl@sss.pgh.pa.us         863         [ +  + ]:       20041016 :     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                 :       20041016 :     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                 :          68110 : heap_modify_tuple(HeapTuple tuple,
                                887                 :                :                   TupleDesc tupleDesc,
                                888                 :                :                   const Datum *replValues,
                                889                 :                :                   const bool *replIsnull,
                                890                 :                :                   const bool *doReplace)
                                891                 :                : {
                                892                 :          68110 :     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                 :                :      */
  353 michael@paquier.xyz       909                 :          68110 :     values = palloc_array(Datum, numberOfAttributes);
                                910                 :          68110 :     isnull = palloc_array(bool, numberOfAttributes);
                                911                 :                : 
 7927 tgl@sss.pgh.pa.us         912                 :          68110 :     heap_deform_tuple(tuple, tupleDesc, values, isnull);
                                913                 :                : 
                                914         [ +  + ]:        2061537 :     for (attoff = 0; attoff < numberOfAttributes; attoff++)
                                915                 :                :     {
                                916         [ +  + ]:        1993427 :         if (doReplace[attoff])
                                917                 :                :         {
                                918                 :        1020991 :             values[attoff] = replValues[attoff];
                                919                 :        1020991 :             isnull[attoff] = replIsnull[attoff];
                                920                 :                :         }
                                921                 :                :     }
                                922                 :                : 
                                923                 :                :     /*
                                924                 :                :      * create a new tuple from the values and isnull arrays
                                925                 :                :      */
                                926                 :          68110 :     newTuple = heap_form_tuple(tupleDesc, values, isnull);
                                927                 :                : 
                                928                 :          68110 :     pfree(values);
                                929                 :          68110 :     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                 :          68110 :     newTuple->t_self = tuple->t_self;
                                939                 :          68110 :     newTuple->t_tableOid = tuple->t_tableOid;
                                940                 :                : 
                                941                 :          68110 :     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
 3672                           957                 :            717 : 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                 :            717 :     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                 :                :      */
  353 michael@paquier.xyz       974                 :            717 :     values = palloc_array(Datum, numberOfAttributes);
                                975                 :            717 :     isnull = palloc_array(bool, numberOfAttributes);
                                976                 :                : 
 3672 tgl@sss.pgh.pa.us         977                 :            717 :     heap_deform_tuple(tuple, tupleDesc, values, isnull);
                                978                 :                : 
                                979         [ +  + ]:           2167 :     for (i = 0; i < nCols; i++)
                                980                 :                :     {
                                981                 :           1450 :         int         attnum = replCols[i];
                                982                 :                : 
                                983   [ +  -  -  + ]:           1450 :         if (attnum <= 0 || attnum > numberOfAttributes)
 3672 tgl@sss.pgh.pa.us         984         [ #  # ]:UBC           0 :             elog(ERROR, "invalid column number %d", attnum);
 3672 tgl@sss.pgh.pa.us         985                 :CBC        1450 :         values[attnum - 1] = replValues[i];
                                986                 :           1450 :         isnull[attnum - 1] = replIsnull[i];
                                987                 :                :     }
                                988                 :                : 
                                989                 :                :     /*
                                990                 :                :      * create a new tuple from the values and isnull arrays
                                991                 :                :      */
                                992                 :            717 :     newTuple = heap_form_tuple(tupleDesc, values, isnull);
                                993                 :                : 
                                994                 :            717 :     pfree(values);
                                995                 :            717 :     pfree(isnull);
                                996                 :                : 
                                997                 :                :     /* see equivalent code in heap_modify_tuple() */
                                998                 :            717 :     newTuple->t_self = tuple->t_self;
                                999                 :            717 :     newTuple->t_tableOid = tuple->t_tableOid;
                               1000                 :                : 
                               1001                 :            717 :     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
   19 andres@anarazel.de       1018                 :       20144544 : heap_tuple_set_datum(HeapTupleHeader tup, TupleDesc tupleDesc, uint32 len)
                               1019                 :                : {
                               1020                 :       20144544 :     HeapTupleHeaderSetDatumLength(tup, len);
                               1021                 :       20144544 :     HeapTupleHeaderSetTypeId(tup, tupleDesc->tdtypeid);
                               1022                 :       20144544 :     HeapTupleHeaderSetDatum(tup);
                               1023                 :                : 
                               1024         [ +  + ]:       20144544 :     if (tupleDesc->tdtypeid == RECORDOID)
                               1025                 :                :     {
                               1026                 :                :         /*
                               1027                 :                :          * RECORDOID tuples are not protected by versioning (and do not need
                               1028                 :                :          * to be).
                               1029                 :                :          */
                               1030                 :       15084219 :         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                 :       15084219 :         ItemPointerSetInvalid(&tup->t_ctid);
                               1037                 :                :     }
                               1038                 :                :     else
                               1039                 :                :     {
                               1040                 :        5060325 :         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         [ +  + ]:        5060325 :         if (natts == 0)
                               1059                 :             23 :             version = InvalidXLogRecPtr;
                               1060         [ +  + ]:        5060302 :         else if (natts == tupleDesc->natts)
                               1061                 :        5060265 :             version = TupleDescGetVersion(tupleDesc);
                               1062         [ +  - ]:             37 :         else if (natts < tupleDesc->natts)
                               1063                 :             37 :             version = TupleDescGetAttrVersion(tupleDesc, natts);
                               1064                 :                :         else
   19 andres@anarazel.de       1065         [ #  # ]:UBC           0 :             elog(ERROR, "datum's descriptor has fewer (%d) attributes than tuple (%d)",
                               1066                 :                :                  tupleDesc->natts, natts);
                               1067                 :                : 
   19 andres@anarazel.de       1068                 :CBC     5060325 :         HeapTupleHeaderSetVersion(tup, version);
                               1069                 :                :     }
                               1070                 :       20144544 : }
                               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                 :          18751 : heap_datum_update_type(HeapTupleHeader tup, TupleDesc tupleDesc)
                               1084                 :                : {
                               1085         [ -  + ]:          18751 :     Assert(HeapTupleHeaderReallyIsDatum(tup));
                               1086                 :                : 
                               1087                 :          18751 :     heap_datum_check_coercion(tup, tupleDesc);
                               1088                 :                : 
                               1089                 :          18743 :     heap_tuple_set_datum(tup, tupleDesc,
                               1090                 :                :                          HeapTupleHeaderGetDatumLength(tup));
                               1091                 :          18743 : }
                               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                 :                :     {
   19 andres@anarazel.de       1117         [ #  # ]:UBC           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                 :                :      */
   19 andres@anarazel.de       1128         [ -  + ]:CBC        8303 :     if (unlikely(deformingDesc->natts < datum_natts))
                               1129                 :                :     {
   19 andres@anarazel.de       1130         [ #  # ]:UBC           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                 :                : 
   19 andres@anarazel.de       1158         [ +  + ]:CBC       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/%08X is incompatible with tuple type %s (OID %u), version %X/%08X",
                               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         [ -  + ]:            800 :     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                 :          18751 : heap_datum_check_coercion(HeapTupleHeader tup, TupleDesc targetDesc)
                               1389                 :                : {
                               1390                 :          18751 :     Oid         tupTypeid = HeapTupleHeaderGetTypeId(tup);
                               1391                 :          18751 :     int32       tupTypmod = HeapTupleHeaderGetTypMod(tup);
                               1392                 :                : 
                               1393         [ -  + ]:          18751 :     Assert(HeapTupleHeaderIsDatum(tup));
                               1394                 :                : 
                               1395         [ -  + ]:          18751 :     if (unlikely(composite_type_mismatch == COMPOSITE_TYPE_MISMATCH_DISABLED))
   19 andres@anarazel.de       1396                 :UBC           0 :         return;
                               1397                 :                : 
                               1398                 :                :     /* can't check if this is wrongly marked as a datum tuple */
   19 andres@anarazel.de       1399         [ -  + ]:CBC       18751 :     if (!HeapTupleHeaderReallyIsDatum(tup))
   19 andres@anarazel.de       1400                 :UBC           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                 :                :      */
   19 andres@anarazel.de       1406   [ +  +  -  + ]:CBC       18751 :     if (tupTypeid == RECORDOID && tupTypmod == -1)
   19 andres@anarazel.de       1407                 :UBC           0 :         return;
                               1408                 :                : 
   19 andres@anarazel.de       1409         [ +  + ]:CBC       18751 :     if (tupTypeid != targetDesc->tdtypeid ||
                               1410         [ +  + ]:          10722 :         (targetDesc->tdtypeid == RECORDOID &&
                               1411         [ +  + ]:           3399 :          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   [ +  +  +  + ]:          18044 :     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                 :        1128140 : heap_datum_check(HeapTupleHeader tup, TupleDesc tupleDesc)
                               1448                 :                : {
                               1449                 :        1128140 :     XLogRecPtr  descVersion = TupleDescGetVersion(tupleDesc);
                               1450                 :        1128140 :     Oid         tupTypeid = HeapTupleHeaderGetTypeId(tup);
                               1451                 :        1128140 :     int32       tupTypmod = HeapTupleHeaderGetTypMod(tup);
                               1452                 :                :     XLogRecPtr  tupVersion;
                               1453                 :                : 
                               1454         [ +  + ]:        1128140 :     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         [ -  + ]:        1128136 :     if (unlikely(!HeapTupleHeaderIsVersionedDatum(tup)))
   19 andres@anarazel.de       1463         [ #  # ]:UBC           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 */
   19 andres@anarazel.de       1466         [ +  + ]:CBC     1128136 :     if (unlikely(!HeapTupleHeaderReallyIsDatum(tup)))
                               1467                 :         121390 :         return;
                               1468                 :                : 
                               1469                 :        1006746 :     tupVersion = HeapTupleHeaderGetVersion(tup);
                               1470                 :                : 
                               1471         [ +  + ]:        1006746 :     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         [ -  + ]:        1006656 :     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                 :                :          */
   19 andres@anarazel.de       1503         [ #  # ]:UBC           0 :         ereport(ERROR,
                               1504                 :                :                 errmsg_internal("datum tuple without type information"),
                               1505                 :                :                 errhint_internal("An extension may need to be updated."));
                               1506                 :                :     }
   19 andres@anarazel.de       1507         [ -  + ]:CBC     1006656 :     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                 :                :          */
    5 alvherre@kurilemu.de     1519         [ #  # ]:UBC           0 :         elog(ERROR, "processing record datum (%u, %d) with rowtype tupdesc (%u, %d, %X/%08X)",
                               1520                 :                :              tupTypeid, tupTypmod,
                               1521                 :                :              tupleDesc->tdtypeid, tupleDesc->tdtypmod,
                               1522                 :                :              LSN_FORMAT_ARGS(descVersion));
                               1523                 :                :     }
   19 andres@anarazel.de       1524         [ +  + ]:CBC     1006656 :     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         [ +  + ]:        1006462 :     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                 :        1738743 : DatumGetHeapTupleHeaderImpl(Datum t, bool copy, bool *was_datum)
                               1580                 :                : {
                               1581                 :        1738743 :     struct varlena *tpr = (struct varlena *) DatumGetPointer(t);
                               1582                 :                :     HeapTupleHeader tp;
                               1583                 :                :     Oid         tupTypeid;
                               1584                 :                : 
                               1585         [ +  + ]:        1738743 :     if (copy)
                               1586                 :             23 :         tp = (HeapTupleHeader) pg_detoast_datum_copy(tpr);
                               1587                 :                :     else
                               1588                 :                :     {
                               1589                 :        1738720 :         tp = (HeapTupleHeader) pg_detoast_datum(tpr);
                               1590                 :        1738720 :         copy = ((struct varlena *) tp) != tpr;
                               1591                 :                :     }
                               1592                 :                : 
                               1593                 :        1738743 :     tupTypeid = HeapTupleHeaderGetTypeId(tp);
                               1594                 :                : 
                               1595         [ +  + ]:        1738743 :     if (was_datum)
                               1596                 :             15 :         *was_datum = true;
                               1597                 :                : 
                               1598         [ +  + ]:        1738743 :     if (!HeapTupleHeaderIsDatum(tp))
                               1599                 :                :     {
                               1600         [ -  + ]:             44 :         Assert(HeapTupleHeaderGetDatumLength(tp) > 0);
                               1601                 :                : 
                               1602         [ -  + ]:             44 :         if (was_datum)
   19 andres@anarazel.de       1603                 :UBC           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                 :                :          */
   19 andres@anarazel.de       1610         [ +  + ]:CBC          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         [ -  + ]:        1738743 :     Assert(HeapTupleHeaderReallyIsDatum(tp));
                               1632                 :                : 
                               1633                 :        1738743 :     return tp;
                               1634                 :                : }
                               1635                 :                : 
                               1636                 :                : HeapTupleHeader
                               1637                 :        1738705 : DatumGetHeapTupleHeader(Datum t)
                               1638                 :                : {
                               1639                 :        1738705 :     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
 7927 tgl@sss.pgh.pa.us        1676                 :        3225083 : heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc,
                               1677                 :                :                   Datum *values, bool *isnull)
                               1678                 :                : {
                               1679                 :        3225083 :     HeapTupleHeader tup = tuple->t_data;
                               1680                 :                :     CompactAttribute *cattr;
                               1681                 :        3225083 :     bool        hasnulls = HeapTupleHasNulls(tuple);
                               1682                 :        3225083 :     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 */
  243 nathan@postgresql.or     1687                 :        3225083 :     uint8      *bp = tup->t_bits;    /* ptr to null bitmap in tuple */
                               1688                 :                :     int         firstNonCacheOffsetAttr;
                               1689                 :                :     int         firstNullAttr;
                               1690                 :                : 
   19 andres@anarazel.de       1691         [ +  + ]:        3225083 :     if (HeapTupleHeaderIsVersionedDatum(tup))
                               1692                 :          67670 :         heap_datum_check(tup, tupleDesc);
                               1693                 :                : 
 7263 bruce@momjian.us         1694                 :        3224781 :     natts = HeapTupleHeaderGetNatts(tup);
                               1695                 :                : 
                               1696                 :                :     /* Did someone forget to call TupleDescFinalize()? */
  257 drowley@postgresql.o     1697         [ -  + ]:        3224781 :     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                 :                :      */
 7927 tgl@sss.pgh.pa.us        1704                 :        3224781 :     natts = Min(natts, tdesc_natts);
  257 drowley@postgresql.o     1705                 :        3224781 :     firstNonCacheOffsetAttr = Min(tupleDesc->firstNonCachedOffsetAttr, natts);
                               1706                 :                : 
                               1707         [ +  + ]:        3224781 :     if (hasnulls)
                               1708                 :                :     {
                               1709                 :         128150 :         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                 :         128150 :         firstNonCacheOffsetAttr = Min(firstNonCacheOffsetAttr, firstNullAttr);
                               1718                 :                :     }
                               1719                 :                :     else
                               1720                 :        3096631 :         firstNullAttr = natts;
                               1721                 :                : 
 7927 tgl@sss.pgh.pa.us        1722                 :        3224781 :     tp = (char *) tup + tup->t_hoff;
  257 drowley@postgresql.o     1723                 :        3224781 :     attnum = 0;
                               1724                 :                : 
                               1725         [ +  + ]:        3224781 :     if (firstNonCacheOffsetAttr > 0)
                               1726                 :                :     {
                               1727                 :                : #ifdef USE_ASSERT_CHECKING
                               1728                 :                :         /* In Assert enabled builds, verify attcacheoff is correct */
                               1729                 :        2627923 :         int         offcheck = 0;
                               1730                 :                : #endif
                               1731                 :                :         do
                               1732                 :                :         {
                               1733                 :        4567113 :             isnull[attnum] = false;
                               1734                 :        4567113 :             cattr = TupleDescCompactAttr(tupleDesc, attnum);
                               1735                 :        4567113 :             off = cattr->attcacheoff;
                               1736                 :                : 
                               1737                 :                : #ifdef USE_ASSERT_CHECKING
                               1738                 :        4567113 :             offcheck = att_nominal_alignby(offcheck, cattr->attalignby);
                               1739         [ -  + ]:        4567113 :             Assert(offcheck == cattr->attcacheoff);
                               1740                 :        4567113 :             offcheck += cattr->attlen;
                               1741                 :                : #endif
                               1742                 :                : 
                               1743                 :        9134226 :             values[attnum] = fetch_att_noerr(tp + off,
                               1744                 :        4567113 :                                              cattr->attbyval,
                               1745                 :        4567113 :                                              cattr->attlen);
                               1746         [ +  + ]:        4567113 :         } while (++attnum < firstNonCacheOffsetAttr);
                               1747                 :        2627923 :         off += cattr->attlen;
                               1748                 :                :     }
                               1749                 :                :     else
                               1750                 :         596858 :         off = 0;
                               1751                 :                : 
                               1752         [ +  + ]:        4947306 :     for (; attnum < firstNullAttr; attnum++)
                               1753                 :                :     {
                               1754                 :        1722525 :         isnull[attnum] = false;
                               1755                 :        1722525 :         cattr = TupleDescCompactAttr(tupleDesc, attnum);
                               1756                 :        1722525 :         values[attnum] = align_fetch_then_add(tp,
                               1757                 :                :                                               &off,
                               1758                 :        1722525 :                                               cattr->attbyval,
                               1759                 :        1722525 :                                               cattr->attlen,
                               1760                 :        1722525 :                                               cattr->attalignby);
                               1761                 :                :     }
                               1762                 :                : 
                               1763         [ +  + ]:        3829932 :     for (; attnum < natts; attnum++)
                               1764                 :                :     {
                               1765         [ -  + ]:         605151 :         Assert(hasnulls);
                               1766                 :                : 
                               1767         [ +  + ]:         605151 :         if (att_isnull(attnum, bp))
                               1768                 :                :         {
 7927 tgl@sss.pgh.pa.us        1769                 :         478009 :             values[attnum] = (Datum) 0;
                               1770                 :         478009 :             isnull[attnum] = true;
                               1771                 :         478009 :             continue;
                               1772                 :                :         }
                               1773                 :                : 
                               1774                 :         127142 :         isnull[attnum] = false;
  257 drowley@postgresql.o     1775                 :         127142 :         cattr = TupleDescCompactAttr(tupleDesc, attnum);
                               1776                 :                : 
                               1777                 :                :         /* align 'off', fetch the attr's value, and increment off beyond it */
                               1778                 :         127142 :         values[attnum] = align_fetch_then_add(tp,
                               1779                 :                :                                               &off,
                               1780                 :         127142 :                                               cattr->attbyval,
                               1781                 :         127142 :                                               cattr->attlen,
                               1782                 :         127142 :                                               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                 :                :      */
 7927 tgl@sss.pgh.pa.us        1789         [ +  + ]:        3227415 :     for (; attnum < tdesc_natts; attnum++)
 3167 andrew@dunslane.net      1790                 :           2634 :         values[attnum] = getmissingattr(tupleDesc, attnum + 1, &isnull[attnum]);
 7927 tgl@sss.pgh.pa.us        1791                 :        3224781 : }
                               1792                 :                : 
                               1793                 :                : /*
                               1794                 :                :  * heap_freetuple
                               1795                 :                :  */
                               1796                 :                : void
 9844 JanWieck@Yahoo.com       1797                 :       15848628 : heap_freetuple(HeapTuple htup)
                               1798                 :                : {
                               1799                 :       15848628 :     pfree(htup);
                               1800                 :       15848628 : }
                               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
 7459 tgl@sss.pgh.pa.us        1815                 :       30148685 : 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                 :       30148685 :     bool        hasnull = false;
                               1826                 :       30148685 :     int         numberOfAttributes = tupleDescriptor->natts;
                               1827                 :                :     int         i;
                               1828                 :                : 
  614 jdavis@postgresql.or     1829         [ -  + ]:       30148685 :     Assert(extra == MAXALIGN(extra));
                               1830                 :                : 
 7459 tgl@sss.pgh.pa.us        1831         [ -  + ]:       30148685 :     if (numberOfAttributes > MaxTupleAttributeNumber)
 7459 tgl@sss.pgh.pa.us        1832         [ #  # ]:UBC           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                 :                :      */
 7459 tgl@sss.pgh.pa.us        1840         [ +  + ]:CBC    84542138 :     for (i = 0; i < numberOfAttributes; i++)
                               1841                 :                :     {
                               1842         [ +  + ]:       54877235 :         if (isnull[i])
                               1843                 :                :         {
 4594                          1844                 :         483782 :             hasnull = true;
                               1845                 :         483782 :             break;
                               1846                 :                :         }
                               1847                 :                :     }
                               1848                 :                : 
                               1849                 :                :     /*
                               1850                 :                :      * Determine total space needed
                               1851                 :                :      */
 4298                          1852                 :       30148685 :     len = SizeofMinimalTupleHeader;
                               1853                 :                : 
 7459                          1854         [ +  + ]:       30148685 :     if (hasnull)
                               1855                 :         483782 :         len += BITMAPLEN(numberOfAttributes);
                               1856                 :                : 
                               1857                 :       30148685 :     hoff = len = MAXALIGN(len); /* align user data safely */
                               1858                 :                : 
   19 alvherre@kurilemu.de     1859                 :GNC    30148685 :     data_len = heap_compute_data_size(tupleDescriptor, values, isnull, false);
                               1860                 :                : 
 7176 tgl@sss.pgh.pa.us        1861                 :CBC    30148685 :     len += data_len;
                               1862                 :                : 
                               1863                 :                :     /*
                               1864                 :                :      * Allocate and zero the space needed.
                               1865                 :                :      */
  614 jdavis@postgresql.or     1866                 :       30148685 :     mem = palloc0(len + extra);
                               1867                 :       30148685 :     tuple = (MinimalTuple) (mem + extra);
                               1868                 :                : 
                               1869                 :                :     /*
                               1870                 :                :      * And fill in the information.
                               1871                 :                :      */
 7459 tgl@sss.pgh.pa.us        1872                 :       30148685 :     tuple->t_len = len;
 7263 bruce@momjian.us         1873                 :       30148685 :     HeapTupleHeaderSetNatts(tuple, numberOfAttributes);
 7459 tgl@sss.pgh.pa.us        1874                 :       30148685 :     tuple->t_hoff = hoff + MINIMAL_TUPLE_OFFSET;
                               1875                 :                : 
                               1876         [ +  + ]:       30148685 :     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                 :       30148685 :     return tuple;
                               1885                 :                : }
                               1886                 :                : 
                               1887                 :                : /*
                               1888                 :                :  * heap_free_minimal_tuple
                               1889                 :                :  */
                               1890                 :                : void
                               1891                 :       24664093 : heap_free_minimal_tuple(MinimalTuple mtup)
                               1892                 :                : {
                               1893                 :       24664093 :     pfree(mtup);
                               1894                 :       24664093 : }
                               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
  614 jdavis@postgresql.or     1903                 :        3470097 : heap_copy_minimal_tuple(MinimalTuple mtup, Size extra)
                               1904                 :                : {
                               1905                 :                :     MinimalTuple result;
                               1906                 :                :     char       *mem;
                               1907                 :                : 
                               1908         [ -  + ]:        3470097 :     Assert(extra == MAXALIGN(extra));
                               1909                 :        3470097 :     mem = palloc(mtup->t_len + extra);
                               1910                 :        3470097 :     memset(mem, 0, extra);
                               1911                 :        3470097 :     result = (MinimalTuple) (mem + extra);
 7459 tgl@sss.pgh.pa.us        1912                 :        3470097 :     memcpy(result, mtup, mtup->t_len);
                               1913                 :        3470097 :     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                 :         652574 : heap_tuple_from_minimal_tuple(MinimalTuple mtup)
                               1927                 :                : {
                               1928                 :                :     HeapTuple   result;
                               1929                 :         652574 :     uint32      len = mtup->t_len + MINIMAL_TUPLE_OFFSET;
                               1930                 :                : 
                               1931                 :         652574 :     result = (HeapTuple) palloc(HEAPTUPLESIZE + len);
                               1932                 :         652574 :     result->t_len = len;
                               1933                 :         652574 :     ItemPointerSetInvalid(&(result->t_self));
                               1934                 :         652574 :     result->t_tableOid = InvalidOid;
                               1935                 :         652574 :     result->t_data = (HeapTupleHeader) ((char *) result + HEAPTUPLESIZE);
                               1936                 :         652574 :     memcpy((char *) result->t_data + MINIMAL_TUPLE_OFFSET, mtup, mtup->t_len);
 7263 bruce@momjian.us         1937                 :         652574 :     memset(result->t_data, 0, offsetof(HeapTupleHeaderData, t_infomask2));
 7459 tgl@sss.pgh.pa.us        1938                 :         652574 :     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
  614 jdavis@postgresql.or     1948                 :        3973617 : minimal_tuple_from_heap_tuple(HeapTuple htup, Size extra)
                               1949                 :                : {
                               1950                 :                :     MinimalTuple result;
                               1951                 :                :     char       *mem;
                               1952                 :                :     uint32      len;
                               1953                 :                : 
                               1954         [ -  + ]:        3973617 :     Assert(extra == MAXALIGN(extra));
 7459 tgl@sss.pgh.pa.us        1955         [ -  + ]:        3973617 :     Assert(htup->t_len > MINIMAL_TUPLE_OFFSET);
                               1956                 :        3973617 :     len = htup->t_len - MINIMAL_TUPLE_OFFSET;
  614 jdavis@postgresql.or     1957                 :        3973617 :     mem = palloc(len + extra);
                               1958                 :        3973617 :     memset(mem, 0, extra);
                               1959                 :        3973617 :     result = (MinimalTuple) (mem + extra);
 7459 tgl@sss.pgh.pa.us        1960                 :        3973617 :     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                 :                :      */
   19 andres@anarazel.de       1972         [ +  + ]:        3973617 :     if (HeapTupleHeaderIsDatum(htup->t_data))
                               1973                 :                :     {
                               1974                 :                :         /* like HeapTupleHeaderClearDatum() */
                               1975                 :        2063685 :         result->t_infomask &= ~HEAP_IS_DATUM;
                               1976                 :                :     }
                               1977                 :                : 
 7459 tgl@sss.pgh.pa.us        1978                 :        3973617 :     result->t_len = len;
                               1979                 :        3973617 :     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
 3169 andres@anarazel.de       1987                 :UBC           0 : varsize_any(void *p)
                               1988                 :                : {
                               1989                 :              0 :     return VARSIZE_ANY(p);
                               1990                 :                : }
        

Generated by: LCOV version 2.0-1