LCOV - code coverage report
Current view: top level - src/backend/access/common - heaptuple.c (source / functions) Hit Total Coverage
Test: PostgreSQL 11beta1 Lines: 561 612 91.7 %
Date: 2018-06-18 00:19:56 Functions: 29 31 93.5 %
Legend: Lines: hit not hit

          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             :  * tuptoaster.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 'i' (or 'd') 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 'p', 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-2018, 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/sysattr.h"
      61             : #include "access/tupdesc_details.h"
      62             : #include "access/tuptoaster.h"
      63             : #include "executor/tuptable.h"
      64             : #include "utils/expandeddatum.h"
      65             : 
      66             : 
      67             : /* Does att's datatype allow packing into the 1-byte-header varlena format? */
      68             : #define ATT_IS_PACKABLE(att) \
      69             :     ((att)->attlen == -1 && (att)->attstorage != 'p')
      70             : /* Use this if it's already known varlena */
      71             : #define VARLENA_ATT_IS_PACKABLE(att) \
      72             :     ((att)->attstorage != 'p')
      73             : 
      74             : 
      75             : /* ----------------------------------------------------------------
      76             :  *                      misc support routines
      77             :  * ----------------------------------------------------------------
      78             :  */
      79             : 
      80             : /*
      81             :  * Return the missing value of an attribute, or NULL if there isn't one.
      82             :  */
      83             : static Datum
      84        3908 : getmissingattr(TupleDesc tupleDesc,
      85             :                int attnum, bool *isnull)
      86             : {
      87             :     Form_pg_attribute att;
      88             : 
      89             :     Assert(attnum <= tupleDesc->natts);
      90             :     Assert(attnum > 0);
      91             : 
      92        3908 :     att = TupleDescAttr(tupleDesc, attnum - 1);
      93             : 
      94        3908 :     if (att->atthasmissing)
      95             :     {
      96             :         AttrMissing *attrmiss;
      97             : 
      98             :         Assert(tupleDesc->constr);
      99             :         Assert(tupleDesc->constr->missing);
     100             : 
     101        2128 :         attrmiss = tupleDesc->constr->missing + (attnum - 1);
     102             : 
     103        2128 :         if (attrmiss->ammissingPresent)
     104             :         {
     105        2128 :             *isnull = false;
     106        2128 :             return attrmiss->ammissing;
     107             :         }
     108             :     }
     109             : 
     110        1780 :     *isnull = true;
     111        1780 :     return PointerGetDatum(NULL);
     112             : }
     113             : 
     114             : /*
     115             :  * Fill in missing values for a TupleTableSlot.
     116             :  *
     117             :  * This is only exposed because it's needed for JIT compiled tuple
     118             :  * deforming. That exception aside, there should be no callers outside of this
     119             :  * file.
     120             :  */
     121             : void
     122         922 : slot_getmissingattrs(TupleTableSlot *slot, int startAttNum, int lastAttNum)
     123             : {
     124         922 :     AttrMissing *attrmiss = NULL;
     125             :     int         missattnum;
     126             : 
     127         922 :     if (slot->tts_tupleDescriptor->constr)
     128         592 :         attrmiss = slot->tts_tupleDescriptor->constr->missing;
     129             : 
     130         922 :     if (!attrmiss)
     131             :     {
     132             :         /* no missing values array at all, so just fill everything in as NULL */
     133         358 :         memset(slot->tts_values + startAttNum, 0,
     134         358 :                (lastAttNum - startAttNum) * sizeof(Datum));
     135         358 :         memset(slot->tts_isnull + startAttNum, 1,
     136         358 :                (lastAttNum - startAttNum) * sizeof(bool));
     137             :     }
     138             :     else
     139             :     {
     140             :         /* if there is a missing values array we must process them one by one */
     141        2480 :         for (missattnum = startAttNum;
     142             :              missattnum < lastAttNum;
     143        1352 :              missattnum++)
     144             :         {
     145        1352 :             slot->tts_values[missattnum] = attrmiss[missattnum].ammissing;
     146        2704 :             slot->tts_isnull[missattnum] =
     147        1352 :                 !attrmiss[missattnum].ammissingPresent;
     148             :         }
     149             :     }
     150         922 : }
     151             : 
     152             : /*
     153             :  * heap_compute_data_size
     154             :  *      Determine size of the data area of a tuple to be constructed
     155             :  */
     156             : Size
     157    76742286 : heap_compute_data_size(TupleDesc tupleDesc,
     158             :                        Datum *values,
     159             :                        bool *isnull)
     160             : {
     161    76742286 :     Size        data_length = 0;
     162             :     int         i;
     163    76742286 :     int         numberOfAttributes = tupleDesc->natts;
     164             : 
     165   312277898 :     for (i = 0; i < numberOfAttributes; i++)
     166             :     {
     167             :         Datum       val;
     168             :         Form_pg_attribute atti;
     169             : 
     170   235535612 :         if (isnull[i])
     171    20268936 :             continue;
     172             : 
     173   215266676 :         val = values[i];
     174   215266676 :         atti = TupleDescAttr(tupleDesc, i);
     175             : 
     176   231101690 :         if (ATT_IS_PACKABLE(atti) &&
     177    27355904 :             VARATT_CAN_MAKE_SHORT(DatumGetPointer(val)))
     178             :         {
     179             :             /*
     180             :              * we're anticipating converting to a short varlena header, so
     181             :              * adjust length and don't count any alignment
     182             :              */
     183    11180260 :             data_length += VARATT_CONVERTED_SHORT_SIZE(DatumGetPointer(val));
     184             :         }
     185   211839978 :         else if (atti->attlen == -1 &&
     186     7870480 :                  VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(val)))
     187             :         {
     188             :             /*
     189             :              * we want to flatten the expanded value so that the constructed
     190             :              * tuple doesn't depend on it
     191             :              */
     192        1548 :             data_length = att_align_nominal(data_length, atti->attalign);
     193        1548 :             data_length += EOH_get_flat_size(DatumGetEOHP(val));
     194             :         }
     195             :         else
     196             :         {
     197   204084868 :             data_length = att_align_datum(data_length, atti->attalign,
     198             :                                           atti->attlen, val);
     199   204084868 :             data_length = att_addlength_datum(data_length, atti->attlen,
     200             :                                               val);
     201             :         }
     202             :     }
     203             : 
     204    76742286 :     return data_length;
     205             : }
     206             : 
     207             : /*
     208             :  * Per-attribute helper for heap_fill_tuple and other routines building tuples.
     209             :  *
     210             :  * Fill in either a data value or a bit in the null bitmask
     211             :  */
     212             : static inline void
     213   233253790 : fill_val(Form_pg_attribute att,
     214             :          bits8 **bit,
     215             :          int *bitmask,
     216             :          char **dataP,
     217             :          uint16 *infomask,
     218             :          Datum datum,
     219             :          bool isnull)
     220             : {
     221             :     Size        data_length;
     222   233253790 :     char       *data = *dataP;
     223             : 
     224             :     /*
     225             :      * If we're building a null bitmap, set the appropriate bit for the
     226             :      * current column value here.
     227             :      */
     228   233253790 :     if (bit != NULL)
     229             :     {
     230    84824348 :         if (*bitmask != HIGHBIT)
     231    70400506 :             *bitmask <<= 1;
     232             :         else
     233             :         {
     234    14423842 :             *bit += 1;
     235    14423842 :             **bit = 0x0;
     236    14423842 :             *bitmask = 1;
     237             :         }
     238             : 
     239    84824348 :         if (isnull)
     240             :         {
     241    20053020 :             *infomask |= HEAP_HASNULL;
     242    20053020 :             return;
     243             :         }
     244             : 
     245    64771328 :         **bit |= *bitmask;
     246             :     }
     247             : 
     248             :     /*
     249             :      * XXX we use the att_align macros on the pointer value itself, not on an
     250             :      * offset.  This is a bit of a hack.
     251             :      */
     252   213200770 :     if (att->attbyval)
     253             :     {
     254             :         /* pass-by-value */
     255   182297812 :         data = (char *) att_align_nominal(data, att->attalign);
     256   182297812 :         store_att_byval(data, datum, att->attlen);
     257   182297812 :         data_length = att->attlen;
     258             :     }
     259    30902958 :     else if (att->attlen == -1)
     260             :     {
     261             :         /* varlena */
     262    18400334 :         Pointer     val = DatumGetPointer(datum);
     263             : 
     264    18400334 :         *infomask |= HEAP_HASVARWIDTH;
     265    18400334 :         if (VARATT_IS_EXTERNAL(val))
     266             :         {
     267       25204 :             if (VARATT_IS_EXTERNAL_EXPANDED(val))
     268        1548 :             {
     269             :                 /*
     270             :                  * we want to flatten the expanded value so that the
     271             :                  * constructed tuple doesn't depend on it
     272             :                  */
     273        1548 :                 ExpandedObjectHeader *eoh = DatumGetEOHP(datum);
     274             : 
     275        1548 :                 data = (char *) att_align_nominal(data,
     276             :                                                   att->attalign);
     277        1548 :                 data_length = EOH_get_flat_size(eoh);
     278        1548 :                 EOH_flatten_into(eoh, data, data_length);
     279             :             }
     280             :             else
     281             :             {
     282       23656 :                 *infomask |= HEAP_HASEXTERNAL;
     283             :                 /* no alignment, since it's short by definition */
     284       23656 :                 data_length = VARSIZE_EXTERNAL(val);
     285       23656 :                 memcpy(data, val, data_length);
     286             :             }
     287             :         }
     288    18375130 :         else if (VARATT_IS_SHORT(val))
     289             :         {
     290             :             /* no alignment for short varlenas */
     291     3806506 :             data_length = VARSIZE_SHORT(val);
     292     3806506 :             memcpy(data, val, data_length);
     293             :         }
     294    26038510 :         else if (VARLENA_ATT_IS_PACKABLE(att) &&
     295    22905190 :                  VARATT_CAN_MAKE_SHORT(val))
     296             :         {
     297             :             /* convert to short varlena -- no alignment */
     298    11180260 :             data_length = VARATT_CONVERTED_SHORT_SIZE(val);
     299    11180260 :             SET_VARSIZE_SHORT(data, data_length);
     300    11180260 :             memcpy(data + 1, VARDATA(val), data_length - 1);
     301             :         }
     302             :         else
     303             :         {
     304             :             /* full 4-byte header varlena */
     305     3388364 :             data = (char *) att_align_nominal(data,
     306             :                                               att->attalign);
     307     3388364 :             data_length = VARSIZE(val);
     308     3388364 :             memcpy(data, val, data_length);
     309             :         }
     310             :     }
     311    12502624 :     else if (att->attlen == -2)
     312             :     {
     313             :         /* cstring ... never needs alignment */
     314     2896374 :         *infomask |= HEAP_HASVARWIDTH;
     315             :         Assert(att->attalign == 'c');
     316     2896374 :         data_length = strlen(DatumGetCString(datum)) + 1;
     317     2896374 :         memcpy(data, DatumGetPointer(datum), data_length);
     318             :     }
     319             :     else
     320             :     {
     321             :         /* fixed-length pass-by-reference */
     322     9606250 :         data = (char *) att_align_nominal(data, att->attalign);
     323             :         Assert(att->attlen > 0);
     324     9606250 :         data_length = att->attlen;
     325     9606250 :         memcpy(data, DatumGetPointer(datum), data_length);
     326             :     }
     327             : 
     328   213200770 :     data += data_length;
     329   213200770 :     *dataP = data;
     330             : }
     331             : 
     332             : /*
     333             :  * heap_fill_tuple
     334             :  *      Load data portion of a tuple from values/isnull arrays
     335             :  *
     336             :  * We also fill the null bitmap (if any) and set the infomask bits
     337             :  * that reflect the tuple's data contents.
     338             :  *
     339             :  * NOTE: it is now REQUIRED that the caller have pre-zeroed the data area.
     340             :  */
     341             : void
     342    76497428 : heap_fill_tuple(TupleDesc tupleDesc,
     343             :                 Datum *values, bool *isnull,
     344             :                 char *data, Size data_size,
     345             :                 uint16 *infomask, bits8 *bit)
     346             : {
     347             :     bits8      *bitP;
     348             :     int         bitmask;
     349             :     int         i;
     350    76497428 :     int         numberOfAttributes = tupleDesc->natts;
     351             : 
     352             : #ifdef USE_ASSERT_CHECKING
     353             :     char       *start = data;
     354             : #endif
     355             : 
     356    76497428 :     if (bit != NULL)
     357             :     {
     358     6953492 :         bitP = &bit[-1];
     359     6953492 :         bitmask = HIGHBIT;
     360             :     }
     361             :     else
     362             :     {
     363             :         /* just to keep compiler quiet */
     364    69543936 :         bitP = NULL;
     365    69543936 :         bitmask = 0;
     366             :     }
     367             : 
     368    76497428 :     *infomask &= ~(HEAP_HASNULL | HEAP_HASVARWIDTH | HEAP_HASEXTERNAL);
     369             : 
     370   309751130 :     for (i = 0; i < numberOfAttributes; i++)
     371             :     {
     372   233253702 :         Form_pg_attribute attr = TupleDescAttr(tupleDesc, i);
     373             : 
     374   699760996 :         fill_val(attr,
     375   233253702 :                  bitP ? &bitP : NULL,
     376             :                  &bitmask,
     377             :                  &data,
     378             :                  infomask,
     379   233253592 :                  values ? values[i] : PointerGetDatum(NULL),
     380   233253702 :                  isnull ? isnull[i] : true);
     381             :     }
     382             : 
     383             :     Assert((data - start) == data_size);
     384    76497428 : }
     385             : 
     386             : 
     387             : /* ----------------------------------------------------------------
     388             :  *                      heap tuple interface
     389             :  * ----------------------------------------------------------------
     390             :  */
     391             : 
     392             : /* ----------------
     393             :  *      heap_attisnull  - returns true iff tuple attribute is not present
     394             :  * ----------------
     395             :  */
     396             : bool
     397    29179500 : heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
     398             : {
     399             :     /*
     400             :      * We allow a NULL tupledesc for relations not expected to have missing
     401             :      * values, such as catalog relations and indexes.
     402             :      */
     403             :     Assert(!tupleDesc || attnum <= tupleDesc->natts);
     404    29179500 :     if (attnum > (int) HeapTupleHeaderGetNatts(tup->t_data))
     405             :     {
     406           4 :         if (tupleDesc && TupleDescAttr(tupleDesc, attnum - 1)->atthasmissing)
     407           0 :             return false;
     408             :         else
     409           4 :             return true;
     410             :     }
     411             : 
     412    29179496 :     if (attnum > 0)
     413             :     {
     414    29179496 :         if (HeapTupleNoNulls(tup))
     415    19123054 :             return false;
     416    10056442 :         return att_isnull(attnum - 1, tup->t_data->t_bits);
     417             :     }
     418             : 
     419           0 :     switch (attnum)
     420             :     {
     421             :         case TableOidAttributeNumber:
     422             :         case SelfItemPointerAttributeNumber:
     423             :         case ObjectIdAttributeNumber:
     424             :         case MinTransactionIdAttributeNumber:
     425             :         case MinCommandIdAttributeNumber:
     426             :         case MaxTransactionIdAttributeNumber:
     427             :         case MaxCommandIdAttributeNumber:
     428             :             /* these are never null */
     429           0 :             break;
     430             : 
     431             :         default:
     432           0 :             elog(ERROR, "invalid attnum: %d", attnum);
     433             :     }
     434             : 
     435           0 :     return false;
     436             : }
     437             : 
     438             : /* ----------------
     439             :  *      nocachegetattr
     440             :  *
     441             :  *      This only gets called from fastgetattr() macro, in cases where
     442             :  *      we can't use a cacheoffset and the value is not null.
     443             :  *
     444             :  *      This caches attribute offsets in the attribute descriptor.
     445             :  *
     446             :  *      An alternative way to speed things up would be to cache offsets
     447             :  *      with the tuple, but that seems more difficult unless you take
     448             :  *      the storage hit of actually putting those offsets into the
     449             :  *      tuple you send to disk.  Yuck.
     450             :  *
     451             :  *      This scheme will be slightly slower than that, but should
     452             :  *      perform well for queries which hit large #'s of tuples.  After
     453             :  *      you cache the offsets once, examining all the other tuples using
     454             :  *      the same attribute descriptor will go much quicker. -cim 5/4/91
     455             :  *
     456             :  *      NOTE: if you need to change this code, see also heap_deform_tuple.
     457             :  *      Also see nocache_index_getattr, which is the same code for index
     458             :  *      tuples.
     459             :  * ----------------
     460             :  */
     461             : Datum
     462   146547444 : nocachegetattr(HeapTuple tuple,
     463             :                int attnum,
     464             :                TupleDesc tupleDesc)
     465             : {
     466   146547444 :     HeapTupleHeader tup = tuple->t_data;
     467             :     char       *tp;             /* ptr to data part of tuple */
     468   146547444 :     bits8      *bp = tup->t_bits;    /* ptr to null bitmap in tuple */
     469   146547444 :     bool        slow = false;   /* do we have to walk attrs? */
     470             :     int         off;            /* current offset within data */
     471             : 
     472             :     /* ----------------
     473             :      *   Three cases:
     474             :      *
     475             :      *   1: No nulls and no variable-width attributes.
     476             :      *   2: Has a null or a var-width AFTER att.
     477             :      *   3: Has nulls or var-widths BEFORE att.
     478             :      * ----------------
     479             :      */
     480             : 
     481   146547444 :     attnum--;
     482             : 
     483   146547444 :     if (!HeapTupleNoNulls(tuple))
     484             :     {
     485             :         /*
     486             :          * there's a null somewhere in the tuple
     487             :          *
     488             :          * check to see if any preceding bits are null...
     489             :          */
     490   135381868 :         int         byte = attnum >> 3;
     491   135381868 :         int         finalbit = attnum & 0x07;
     492             : 
     493             :         /* check for nulls "before" final bit of last byte */
     494   135381868 :         if ((~bp[byte]) & ((1 << finalbit) - 1))
     495     8404222 :             slow = true;
     496             :         else
     497             :         {
     498             :             /* check for nulls in any "earlier" bytes */
     499             :             int         i;
     500             : 
     501   178347764 :             for (i = 0; i < byte; i++)
     502             :             {
     503    53052944 :                 if (bp[i] != 0xFF)
     504             :                 {
     505     1682826 :                     slow = true;
     506     1682826 :                     break;
     507             :                 }
     508             :             }
     509             :         }
     510             :     }
     511             : 
     512   146547444 :     tp = (char *) tup + tup->t_hoff;
     513             : 
     514   146547444 :     if (!slow)
     515             :     {
     516             :         Form_pg_attribute att;
     517             : 
     518             :         /*
     519             :          * If we get here, there are no nulls up to and including the target
     520             :          * attribute.  If we have a cached offset, we can use it.
     521             :          */
     522   136460396 :         att = TupleDescAttr(tupleDesc, attnum);
     523   136460396 :         if (att->attcacheoff >= 0)
     524   122588060 :             return fetchatt(att, tp + att->attcacheoff);
     525             : 
     526             :         /*
     527             :          * Otherwise, check for non-fixed-length attrs up to and including
     528             :          * target.  If there aren't any, it's safe to cheaply initialize the
     529             :          * cached offsets for these attrs.
     530             :          */
     531    13872336 :         if (HeapTupleHasVarWidth(tuple))
     532             :         {
     533             :             int         j;
     534             : 
     535    40944130 :             for (j = 0; j <= attnum; j++)
     536             :             {
     537    40924828 :                 if (TupleDescAttr(tupleDesc, j)->attlen <= 0)
     538             :                 {
     539    13792942 :                     slow = true;
     540    13792942 :                     break;
     541             :                 }
     542             :             }
     543             :         }
     544             :     }
     545             : 
     546    23959384 :     if (!slow)
     547             :     {
     548       79394 :         int         natts = tupleDesc->natts;
     549       79394 :         int         j = 1;
     550             : 
     551             :         /*
     552             :          * If we get here, we have a tuple with no nulls or var-widths up to
     553             :          * and including the target attribute, so we can use the cached offset
     554             :          * ... only we don't have it yet, or we'd not have got here.  Since
     555             :          * it's cheap to compute offsets for fixed-width columns, we take the
     556             :          * opportunity to initialize the cached offsets for *all* the leading
     557             :          * fixed-width columns, in hope of avoiding future visits to this
     558             :          * routine.
     559             :          */
     560       79394 :         TupleDescAttr(tupleDesc, 0)->attcacheoff = 0;
     561             : 
     562             :         /* we might have set some offsets in the slow path previously */
     563      162234 :         while (j < natts && TupleDescAttr(tupleDesc, j)->attcacheoff > 0)
     564        3446 :             j++;
     565             : 
     566      158788 :         off = TupleDescAttr(tupleDesc, j - 1)->attcacheoff +
     567       79394 :             TupleDescAttr(tupleDesc, j - 1)->attlen;
     568             : 
     569      672132 :         for (; j < natts; j++)
     570             :         {
     571      623992 :             Form_pg_attribute att = TupleDescAttr(tupleDesc, j);
     572             : 
     573      623992 :             if (att->attlen <= 0)
     574       31254 :                 break;
     575             : 
     576      592738 :             off = att_align_nominal(off, att->attalign);
     577             : 
     578      592738 :             att->attcacheoff = off;
     579             : 
     580      592738 :             off += att->attlen;
     581             :         }
     582             : 
     583             :         Assert(j > attnum);
     584             : 
     585       79394 :         off = TupleDescAttr(tupleDesc, attnum)->attcacheoff;
     586             :     }
     587             :     else
     588             :     {
     589    23879990 :         bool        usecache = true;
     590             :         int         i;
     591             : 
     592             :         /*
     593             :          * Now we know that we have to walk the tuple CAREFULLY.  But we still
     594             :          * might be able to cache some offsets for next time.
     595             :          *
     596             :          * Note - This loop is a little tricky.  For each non-null attribute,
     597             :          * we have to first account for alignment padding before the attr,
     598             :          * then advance over the attr based on its length.  Nulls have no
     599             :          * storage and no alignment padding either.  We can use/set
     600             :          * attcacheoff until we reach either a null or a var-width attribute.
     601             :          */
     602    23879990 :         off = 0;
     603   148779366 :         for (i = 0;; i++)       /* loop exit is at "break" */
     604   124899376 :         {
     605   148779366 :             Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
     606             : 
     607   148779366 :             if (HeapTupleHasNulls(tuple) && att_isnull(i, bp))
     608             :             {
     609    20794694 :                 usecache = false;
     610    20794694 :                 continue;       /* this cannot be the target att */
     611             :             }
     612             : 
     613             :             /* If we know the next offset, we can skip the rest */
     614   127984672 :             if (usecache && att->attcacheoff >= 0)
     615    79226188 :                 off = att->attcacheoff;
     616    48758484 :             else if (att->attlen == -1)
     617             :             {
     618             :                 /*
     619             :                  * We can only cache the offset for a varlena attribute if the
     620             :                  * offset is already suitably aligned, so that there would be
     621             :                  * no pad bytes in any case: then the offset will be valid for
     622             :                  * either an aligned or unaligned value.
     623             :                  */
     624    14720016 :                 if (usecache &&
     625     1420222 :                     off == att_align_nominal(off, att->attalign))
     626       26688 :                     att->attcacheoff = off;
     627             :                 else
     628             :                 {
     629    13273106 :                     off = att_align_pointer(off, att->attalign, -1,
     630             :                                             tp + off);
     631    13273106 :                     usecache = false;
     632             :                 }
     633             :             }
     634             :             else
     635             :             {
     636             :                 /* not varlena, so safe to use att_align_nominal */
     637    35458690 :                 off = att_align_nominal(off, att->attalign);
     638             : 
     639    35458690 :                 if (usecache)
     640      264404 :                     att->attcacheoff = off;
     641             :             }
     642             : 
     643   127984672 :             if (i == attnum)
     644    23879990 :                 break;
     645             : 
     646   104104682 :             off = att_addlength_pointer(off, att->attlen, tp + off);
     647             : 
     648   104104682 :             if (usecache && att->attlen <= 0)
     649    21919002 :                 usecache = false;
     650             :         }
     651             :     }
     652             : 
     653    23959384 :     return fetchatt(TupleDescAttr(tupleDesc, attnum), tp + off);
     654             : }
     655             : 
     656             : /* ----------------
     657             :  *      heap_getsysattr
     658             :  *
     659             :  *      Fetch the value of a system attribute for a tuple.
     660             :  *
     661             :  * This is a support routine for the heap_getattr macro.  The macro
     662             :  * has already determined that the attnum refers to a system attribute.
     663             :  * ----------------
     664             :  */
     665             : Datum
     666    26815614 : heap_getsysattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
     667             : {
     668             :     Datum       result;
     669             : 
     670             :     Assert(tup);
     671             : 
     672             :     /* Currently, no sys attribute ever reads as NULL. */
     673    26815614 :     *isnull = false;
     674             : 
     675    26815614 :     switch (attnum)
     676             :     {
     677             :         case SelfItemPointerAttributeNumber:
     678             :             /* pass-by-reference datatype */
     679     1316580 :             result = PointerGetDatum(&(tup->t_self));
     680     1316580 :             break;
     681             :         case ObjectIdAttributeNumber:
     682    22931322 :             result = ObjectIdGetDatum(HeapTupleGetOid(tup));
     683    22931322 :             break;
     684             :         case MinTransactionIdAttributeNumber:
     685          42 :             result = TransactionIdGetDatum(HeapTupleHeaderGetRawXmin(tup->t_data));
     686          42 :             break;
     687             :         case MaxTransactionIdAttributeNumber:
     688           0 :             result = TransactionIdGetDatum(HeapTupleHeaderGetRawXmax(tup->t_data));
     689           0 :             break;
     690             :         case MinCommandIdAttributeNumber:
     691             :         case MaxCommandIdAttributeNumber:
     692             : 
     693             :             /*
     694             :              * cmin and cmax are now both aliases for the same field, which
     695             :              * can in fact also be a combo command id.  XXX perhaps we should
     696             :              * return the "real" cmin or cmax if possible, that is if we are
     697             :              * inside the originating transaction?
     698             :              */
     699         124 :             result = CommandIdGetDatum(HeapTupleHeaderGetRawCommandId(tup->t_data));
     700         124 :             break;
     701             :         case TableOidAttributeNumber:
     702     2567546 :             result = ObjectIdGetDatum(tup->t_tableOid);
     703     2567546 :             break;
     704             :         default:
     705           0 :             elog(ERROR, "invalid attnum: %d", attnum);
     706             :             result = 0;         /* keep compiler quiet */
     707             :             break;
     708             :     }
     709    26815614 :     return result;
     710             : }
     711             : 
     712             : /* ----------------
     713             :  *      heap_copytuple
     714             :  *
     715             :  *      returns a copy of an entire tuple
     716             :  *
     717             :  * The HeapTuple struct, tuple header, and tuple data are all allocated
     718             :  * as a single palloc() block.
     719             :  * ----------------
     720             :  */
     721             : HeapTuple
     722    15491202 : heap_copytuple(HeapTuple tuple)
     723             : {
     724             :     HeapTuple   newTuple;
     725             : 
     726    15491202 :     if (!HeapTupleIsValid(tuple) || tuple->t_data == NULL)
     727           0 :         return NULL;
     728             : 
     729    15491202 :     newTuple = (HeapTuple) palloc(HEAPTUPLESIZE + tuple->t_len);
     730    15491202 :     newTuple->t_len = tuple->t_len;
     731    15491202 :     newTuple->t_self = tuple->t_self;
     732    15491202 :     newTuple->t_tableOid = tuple->t_tableOid;
     733    15491202 :     newTuple->t_data = (HeapTupleHeader) ((char *) newTuple + HEAPTUPLESIZE);
     734    15491202 :     memcpy((char *) newTuple->t_data, (char *) tuple->t_data, tuple->t_len);
     735    15491202 :     return newTuple;
     736             : }
     737             : 
     738             : /* ----------------
     739             :  *      heap_copytuple_with_tuple
     740             :  *
     741             :  *      copy a tuple into a caller-supplied HeapTuple management struct
     742             :  *
     743             :  * Note that after calling this function, the "dest" HeapTuple will not be
     744             :  * allocated as a single palloc() block (unlike with heap_copytuple()).
     745             :  * ----------------
     746             :  */
     747             : void
     748           0 : heap_copytuple_with_tuple(HeapTuple src, HeapTuple dest)
     749             : {
     750           0 :     if (!HeapTupleIsValid(src) || src->t_data == NULL)
     751             :     {
     752           0 :         dest->t_data = NULL;
     753           0 :         return;
     754             :     }
     755             : 
     756           0 :     dest->t_len = src->t_len;
     757           0 :     dest->t_self = src->t_self;
     758           0 :     dest->t_tableOid = src->t_tableOid;
     759           0 :     dest->t_data = (HeapTupleHeader) palloc(src->t_len);
     760           0 :     memcpy((char *) dest->t_data, (char *) src->t_data, src->t_len);
     761             : }
     762             : 
     763             : /*
     764             :  * Expand a tuple which has less attributes than required. For each attribute
     765             :  * not present in the sourceTuple, if there is a missing value that will be
     766             :  * used. Otherwise the attribute will be set to NULL.
     767             :  *
     768             :  * The source tuple must have less attributes than the required number.
     769             :  *
     770             :  * Only one of targetHeapTuple and targetMinimalTuple may be supplied. The
     771             :  * other argument must be NULL.
     772             :  */
     773             : static void
     774          84 : expand_tuple(HeapTuple *targetHeapTuple,
     775             :              MinimalTuple *targetMinimalTuple,
     776             :              HeapTuple sourceTuple,
     777             :              TupleDesc tupleDesc)
     778             : {
     779          84 :     AttrMissing *attrmiss = NULL;
     780             :     int         attnum;
     781          84 :     int         firstmissingnum = 0;
     782          84 :     bool        hasNulls = HeapTupleHasNulls(sourceTuple);
     783             :     HeapTupleHeader targetTHeader;
     784          84 :     HeapTupleHeader sourceTHeader = sourceTuple->t_data;
     785          84 :     int         sourceNatts = HeapTupleHeaderGetNatts(sourceTHeader);
     786          84 :     int         natts = tupleDesc->natts;
     787             :     int         sourceNullLen;
     788             :     int         targetNullLen;
     789          84 :     Size        sourceDataLen = sourceTuple->t_len - sourceTHeader->t_hoff;
     790             :     Size        targetDataLen;
     791             :     Size        len;
     792             :     int         hoff;
     793          84 :     bits8      *nullBits = NULL;
     794          84 :     int         bitMask = 0;
     795             :     char       *targetData;
     796             :     uint16     *infoMask;
     797             : 
     798             :     Assert((targetHeapTuple && !targetMinimalTuple)
     799             :            || (!targetHeapTuple && targetMinimalTuple));
     800             : 
     801             :     Assert(sourceNatts < natts);
     802             : 
     803          84 :     sourceNullLen = (hasNulls ? BITMAPLEN(sourceNatts) : 0);
     804             : 
     805          84 :     targetDataLen = sourceDataLen;
     806             : 
     807          88 :     if (tupleDesc->constr &&
     808           4 :         tupleDesc->constr->missing)
     809             :     {
     810             :         /*
     811             :          * If there are missing values we want to put them into the tuple.
     812             :          * Before that we have to compute the extra length for the values
     813             :          * array and the variable length data.
     814             :          */
     815           4 :         attrmiss = tupleDesc->constr->missing;
     816             : 
     817             :         /*
     818             :          * Find the first item in attrmiss for which we don't have a value in
     819             :          * the source. We can ignore all the missing entries before that.
     820             :          */
     821           8 :         for (firstmissingnum = sourceNatts;
     822             :              firstmissingnum < natts;
     823           0 :              firstmissingnum++)
     824             :         {
     825           4 :             if (attrmiss[firstmissingnum].ammissingPresent)
     826           4 :                 break;
     827             :         }
     828             : 
     829             :         /*
     830             :          * If there are no more missing values everything else must be NULL
     831             :          */
     832           8 :         if (firstmissingnum >= natts)
     833             :         {
     834           0 :             hasNulls = true;
     835             :         }
     836             :         else
     837             :         {
     838             : 
     839             :             /*
     840             :              * Now walk the missing attributes. If there is a missing value
     841             :              * make space for it. Otherwise, it's going to be NULL.
     842             :              */
     843          16 :             for (attnum = firstmissingnum;
     844             :                  attnum < natts;
     845           8 :                  attnum++)
     846             :             {
     847           8 :                 if (attrmiss[attnum].ammissingPresent)
     848             :                 {
     849           4 :                     Form_pg_attribute att = TupleDescAttr(tupleDesc, attnum);
     850             : 
     851           4 :                     targetDataLen = att_align_datum(targetDataLen,
     852             :                                                     att->attalign,
     853             :                                                     att->attlen,
     854             :                                                     attrmiss[attnum].ammissing);
     855             : 
     856           4 :                     targetDataLen = att_addlength_pointer(targetDataLen,
     857             :                                                           att->attlen,
     858             :                                                           attrmiss[attnum].ammissing);
     859             :                 }
     860             :                 else
     861             :                 {
     862             :                     /* no missing value, so it must be null */
     863           4 :                     hasNulls = true;
     864             :                 }
     865             :             }
     866             :         }
     867             :     }                           /* end if have missing values */
     868             :     else
     869             :     {
     870             :         /*
     871             :          * If there are no missing values at all then NULLS must be allowed,
     872             :          * since some of the attributes are known to be absent.
     873             :          */
     874          80 :         hasNulls = true;
     875             :     }
     876             : 
     877          84 :     len = 0;
     878             : 
     879          84 :     if (hasNulls)
     880             :     {
     881          84 :         targetNullLen = BITMAPLEN(natts);
     882          84 :         len += targetNullLen;
     883             :     }
     884             :     else
     885           0 :         targetNullLen = 0;
     886             : 
     887          84 :     if (tupleDesc->tdhasoid)
     888           0 :         len += sizeof(Oid);
     889             : 
     890             :     /*
     891             :      * Allocate and zero the space needed.  Note that the tuple body and
     892             :      * HeapTupleData management structure are allocated in one chunk.
     893             :      */
     894          84 :     if (targetHeapTuple)
     895             :     {
     896           4 :         len += offsetof(HeapTupleHeaderData, t_bits);
     897           4 :         hoff = len = MAXALIGN(len); /* align user data safely */
     898           4 :         len += targetDataLen;
     899             : 
     900           4 :         *targetHeapTuple = (HeapTuple) palloc0(HEAPTUPLESIZE + len);
     901           4 :         (*targetHeapTuple)->t_data
     902           4 :             = targetTHeader
     903           4 :             = (HeapTupleHeader) ((char *) *targetHeapTuple + HEAPTUPLESIZE);
     904           4 :         (*targetHeapTuple)->t_len = len;
     905           4 :         (*targetHeapTuple)->t_tableOid = sourceTuple->t_tableOid;
     906           4 :         ItemPointerSetInvalid(&((*targetHeapTuple)->t_self));
     907             : 
     908           4 :         targetTHeader->t_infomask = sourceTHeader->t_infomask;
     909           4 :         targetTHeader->t_hoff = hoff;
     910           4 :         HeapTupleHeaderSetNatts(targetTHeader, natts);
     911           4 :         HeapTupleHeaderSetDatumLength(targetTHeader, len);
     912           4 :         HeapTupleHeaderSetTypeId(targetTHeader, tupleDesc->tdtypeid);
     913           4 :         HeapTupleHeaderSetTypMod(targetTHeader, tupleDesc->tdtypmod);
     914             :         /* We also make sure that t_ctid is invalid unless explicitly set */
     915           4 :         ItemPointerSetInvalid(&(targetTHeader->t_ctid));
     916           4 :         if (targetNullLen > 0)
     917           4 :             nullBits = (bits8 *) ((char *) (*targetHeapTuple)->t_data
     918             :                                   + offsetof(HeapTupleHeaderData, t_bits));
     919           4 :         targetData = (char *) (*targetHeapTuple)->t_data + hoff;
     920           4 :         infoMask = &(targetTHeader->t_infomask);
     921             :     }
     922             :     else
     923             :     {
     924          80 :         len += SizeofMinimalTupleHeader;
     925          80 :         hoff = len = MAXALIGN(len); /* align user data safely */
     926          80 :         len += targetDataLen;
     927             : 
     928          80 :         *targetMinimalTuple = (MinimalTuple) palloc0(len);
     929          80 :         (*targetMinimalTuple)->t_len = len;
     930          80 :         (*targetMinimalTuple)->t_hoff = hoff + MINIMAL_TUPLE_OFFSET;
     931          80 :         (*targetMinimalTuple)->t_infomask = sourceTHeader->t_infomask;
     932             :         /* Same macro works for MinimalTuples */
     933          80 :         HeapTupleHeaderSetNatts(*targetMinimalTuple, natts);
     934          80 :         if (targetNullLen > 0)
     935          80 :             nullBits = (bits8 *) ((char *) *targetMinimalTuple
     936          80 :                                   + offsetof(MinimalTupleData, t_bits));
     937          80 :         targetData = (char *) *targetMinimalTuple + hoff;
     938          80 :         infoMask = &((*targetMinimalTuple)->t_infomask);
     939             :     }
     940             : 
     941          84 :     if (targetNullLen > 0)
     942             :     {
     943          84 :         if (sourceNullLen > 0)
     944             :         {
     945             :             /* if bitmap pre-existed copy in - all is set */
     946           2 :             memcpy(nullBits,
     947             :                    ((char *) sourceTHeader)
     948             :                    + offsetof(HeapTupleHeaderData, t_bits),
     949             :                    sourceNullLen);
     950           2 :             nullBits += sourceNullLen - 1;
     951             :         }
     952             :         else
     953             :         {
     954          82 :             sourceNullLen = BITMAPLEN(sourceNatts);
     955             :             /* Set NOT NULL for all existing attributes */
     956          82 :             memset(nullBits, 0xff, sourceNullLen);
     957             : 
     958          82 :             nullBits += sourceNullLen - 1;
     959             : 
     960          82 :             if (sourceNatts & 0x07)
     961             :             {
     962             :                 /* build the mask (inverted!) */
     963          82 :                 bitMask = 0xff << (sourceNatts & 0x07);
     964             :                 /* Voila */
     965          82 :                 *nullBits = ~bitMask;
     966             :             }
     967             :         }
     968             : 
     969          84 :         bitMask = (1 << ((sourceNatts - 1) & 0x07));
     970             :     }                           /* End if have null bitmap */
     971             : 
     972          84 :     memcpy(targetData,
     973          84 :            ((char *) sourceTuple->t_data) + sourceTHeader->t_hoff,
     974             :            sourceDataLen);
     975             : 
     976          84 :     targetData += sourceDataLen;
     977             : 
     978             :     /* Now fill in the missing values */
     979         172 :     for (attnum = sourceNatts; attnum < natts; attnum++)
     980             :     {
     981             : 
     982          88 :         Form_pg_attribute attr = TupleDescAttr(tupleDesc, attnum);
     983             : 
     984          88 :         if (attrmiss && attrmiss[attnum].ammissingPresent)
     985             :         {
     986           8 :             fill_val(attr,
     987           4 :                      nullBits ? &nullBits : NULL,
     988             :                      &bitMask,
     989             :                      &targetData,
     990             :                      infoMask,
     991           4 :                      attrmiss[attnum].ammissing,
     992             :                      false);
     993             :         }
     994             :         else
     995             :         {
     996          84 :             fill_val(attr,
     997             :                      &nullBits,
     998             :                      &bitMask,
     999             :                      &targetData,
    1000             :                      infoMask,
    1001             :                      (Datum) 0,
    1002             :                      true);
    1003             :         }
    1004             :     }                           /* end loop over missing attributes */
    1005          84 : }
    1006             : 
    1007             : /*
    1008             :  * Fill in the missing values for a minimal HeapTuple
    1009             :  */
    1010             : MinimalTuple
    1011          80 : minimal_expand_tuple(HeapTuple sourceTuple, TupleDesc tupleDesc)
    1012             : {
    1013             :     MinimalTuple minimalTuple;
    1014             : 
    1015          80 :     expand_tuple(NULL, &minimalTuple, sourceTuple, tupleDesc);
    1016          80 :     return minimalTuple;
    1017             : }
    1018             : 
    1019             : /*
    1020             :  * Fill in the missing values for an ordinary HeapTuple
    1021             :  */
    1022             : HeapTuple
    1023           4 : heap_expand_tuple(HeapTuple sourceTuple, TupleDesc tupleDesc)
    1024             : {
    1025             :     HeapTuple   heapTuple;
    1026             : 
    1027           4 :     expand_tuple(&heapTuple, NULL, sourceTuple, tupleDesc);
    1028           4 :     return heapTuple;
    1029             : }
    1030             : 
    1031             : /* ----------------
    1032             :  *      heap_copy_tuple_as_datum
    1033             :  *
    1034             :  *      copy a tuple as a composite-type Datum
    1035             :  * ----------------
    1036             :  */
    1037             : Datum
    1038       46800 : heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc)
    1039             : {
    1040             :     HeapTupleHeader td;
    1041             : 
    1042             :     /*
    1043             :      * If the tuple contains any external TOAST pointers, we have to inline
    1044             :      * those fields to meet the conventions for composite-type Datums.
    1045             :      */
    1046       46800 :     if (HeapTupleHasExternal(tuple))
    1047           0 :         return toast_flatten_tuple_to_datum(tuple->t_data,
    1048             :                                             tuple->t_len,
    1049             :                                             tupleDesc);
    1050             : 
    1051             :     /*
    1052             :      * Fast path for easy case: just make a palloc'd copy and insert the
    1053             :      * correct composite-Datum header fields (since those may not be set if
    1054             :      * the given tuple came from disk, rather than from heap_form_tuple).
    1055             :      */
    1056       46800 :     td = (HeapTupleHeader) palloc(tuple->t_len);
    1057       46800 :     memcpy((char *) td, (char *) tuple->t_data, tuple->t_len);
    1058             : 
    1059       46800 :     HeapTupleHeaderSetDatumLength(td, tuple->t_len);
    1060       46800 :     HeapTupleHeaderSetTypeId(td, tupleDesc->tdtypeid);
    1061       46800 :     HeapTupleHeaderSetTypMod(td, tupleDesc->tdtypmod);
    1062             : 
    1063       46800 :     return PointerGetDatum(td);
    1064             : }
    1065             : 
    1066             : /*
    1067             :  * heap_form_tuple
    1068             :  *      construct a tuple from the given values[] and isnull[] arrays,
    1069             :  *      which are of the length indicated by tupleDescriptor->natts
    1070             :  *
    1071             :  * The result is allocated in the current memory context.
    1072             :  */
    1073             : HeapTuple
    1074    23535604 : heap_form_tuple(TupleDesc tupleDescriptor,
    1075             :                 Datum *values,
    1076             :                 bool *isnull)
    1077             : {
    1078             :     HeapTuple   tuple;          /* return tuple */
    1079             :     HeapTupleHeader td;         /* tuple data */
    1080             :     Size        len,
    1081             :                 data_len;
    1082             :     int         hoff;
    1083    23535604 :     bool        hasnull = false;
    1084    23535604 :     int         numberOfAttributes = tupleDescriptor->natts;
    1085             :     int         i;
    1086             : 
    1087    23535604 :     if (numberOfAttributes > MaxTupleAttributeNumber)
    1088           0 :         ereport(ERROR,
    1089             :                 (errcode(ERRCODE_TOO_MANY_COLUMNS),
    1090             :                  errmsg("number of columns (%d) exceeds limit (%d)",
    1091             :                         numberOfAttributes, MaxTupleAttributeNumber)));
    1092             : 
    1093             :     /*
    1094             :      * Check for nulls
    1095             :      */
    1096   137436402 :     for (i = 0; i < numberOfAttributes; i++)
    1097             :     {
    1098   120133572 :         if (isnull[i])
    1099             :         {
    1100     6232774 :             hasnull = true;
    1101     6232774 :             break;
    1102             :         }
    1103             :     }
    1104             : 
    1105             :     /*
    1106             :      * Determine total space needed
    1107             :      */
    1108    23535604 :     len = offsetof(HeapTupleHeaderData, t_bits);
    1109             : 
    1110    23535604 :     if (hasnull)
    1111     6232774 :         len += BITMAPLEN(numberOfAttributes);
    1112             : 
    1113    23535604 :     if (tupleDescriptor->tdhasoid)
    1114     2423580 :         len += sizeof(Oid);
    1115             : 
    1116    23535604 :     hoff = len = MAXALIGN(len); /* align user data safely */
    1117             : 
    1118    23535604 :     data_len = heap_compute_data_size(tupleDescriptor, values, isnull);
    1119             : 
    1120    23535604 :     len += data_len;
    1121             : 
    1122             :     /*
    1123             :      * Allocate and zero the space needed.  Note that the tuple body and
    1124             :      * HeapTupleData management structure are allocated in one chunk.
    1125             :      */
    1126    23535604 :     tuple = (HeapTuple) palloc0(HEAPTUPLESIZE + len);
    1127    23535604 :     tuple->t_data = td = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE);
    1128             : 
    1129             :     /*
    1130             :      * And fill in the information.  Note we fill the Datum fields even though
    1131             :      * this tuple may never become a Datum.  This lets HeapTupleHeaderGetDatum
    1132             :      * identify the tuple type if needed.
    1133             :      */
    1134    23535604 :     tuple->t_len = len;
    1135    23535604 :     ItemPointerSetInvalid(&(tuple->t_self));
    1136    23535604 :     tuple->t_tableOid = InvalidOid;
    1137             : 
    1138    23535604 :     HeapTupleHeaderSetDatumLength(td, len);
    1139    23535604 :     HeapTupleHeaderSetTypeId(td, tupleDescriptor->tdtypeid);
    1140    23535604 :     HeapTupleHeaderSetTypMod(td, tupleDescriptor->tdtypmod);
    1141             :     /* We also make sure that t_ctid is invalid unless explicitly set */
    1142    23535604 :     ItemPointerSetInvalid(&(td->t_ctid));
    1143             : 
    1144    23535604 :     HeapTupleHeaderSetNatts(td, numberOfAttributes);
    1145    23535604 :     td->t_hoff = hoff;
    1146             : 
    1147    23535604 :     if (tupleDescriptor->tdhasoid)   /* else leave infomask = 0 */
    1148     2423580 :         td->t_infomask = HEAP_HASOID;
    1149             : 
    1150    23535604 :     heap_fill_tuple(tupleDescriptor,
    1151             :                     values,
    1152             :                     isnull,
    1153             :                     (char *) td + hoff,
    1154             :                     data_len,
    1155             :                     &td->t_infomask,
    1156             :                     (hasnull ? td->t_bits : NULL));
    1157             : 
    1158    23535604 :     return tuple;
    1159             : }
    1160             : 
    1161             : /*
    1162             :  * heap_modify_tuple
    1163             :  *      form a new tuple from an old tuple and a set of replacement values.
    1164             :  *
    1165             :  * The replValues, replIsnull, and doReplace arrays must be of the length
    1166             :  * indicated by tupleDesc->natts.  The new tuple is constructed using the data
    1167             :  * from replValues/replIsnull at columns where doReplace is true, and using
    1168             :  * the data from the old tuple at columns where doReplace is false.
    1169             :  *
    1170             :  * The result is allocated in the current memory context.
    1171             :  */
    1172             : HeapTuple
    1173      110390 : heap_modify_tuple(HeapTuple tuple,
    1174             :                   TupleDesc tupleDesc,
    1175             :                   Datum *replValues,
    1176             :                   bool *replIsnull,
    1177             :                   bool *doReplace)
    1178             : {
    1179      110390 :     int         numberOfAttributes = tupleDesc->natts;
    1180             :     int         attoff;
    1181             :     Datum      *values;
    1182             :     bool       *isnull;
    1183             :     HeapTuple   newTuple;
    1184             : 
    1185             :     /*
    1186             :      * allocate and fill values and isnull arrays from either the tuple or the
    1187             :      * repl information, as appropriate.
    1188             :      *
    1189             :      * NOTE: it's debatable whether to use heap_deform_tuple() here or just
    1190             :      * heap_getattr() only the non-replaced columns.  The latter could win if
    1191             :      * there are many replaced columns and few non-replaced ones. However,
    1192             :      * heap_deform_tuple costs only O(N) while the heap_getattr way would cost
    1193             :      * O(N^2) if there are many non-replaced columns, so it seems better to
    1194             :      * err on the side of linear cost.
    1195             :      */
    1196      110390 :     values = (Datum *) palloc(numberOfAttributes * sizeof(Datum));
    1197      110390 :     isnull = (bool *) palloc(numberOfAttributes * sizeof(bool));
    1198             : 
    1199      110390 :     heap_deform_tuple(tuple, tupleDesc, values, isnull);
    1200             : 
    1201     2840566 :     for (attoff = 0; attoff < numberOfAttributes; attoff++)
    1202             :     {
    1203     2730176 :         if (doReplace[attoff])
    1204             :         {
    1205     1515770 :             values[attoff] = replValues[attoff];
    1206     1515770 :             isnull[attoff] = replIsnull[attoff];
    1207             :         }
    1208             :     }
    1209             : 
    1210             :     /*
    1211             :      * create a new tuple from the values and isnull arrays
    1212             :      */
    1213      110390 :     newTuple = heap_form_tuple(tupleDesc, values, isnull);
    1214             : 
    1215      110390 :     pfree(values);
    1216      110390 :     pfree(isnull);
    1217             : 
    1218             :     /*
    1219             :      * copy the identification info of the old tuple: t_ctid, t_self, and OID
    1220             :      * (if any)
    1221             :      */
    1222      110390 :     newTuple->t_data->t_ctid = tuple->t_data->t_ctid;
    1223      110390 :     newTuple->t_self = tuple->t_self;
    1224      110390 :     newTuple->t_tableOid = tuple->t_tableOid;
    1225      110390 :     if (tupleDesc->tdhasoid)
    1226       56210 :         HeapTupleSetOid(newTuple, HeapTupleGetOid(tuple));
    1227             : 
    1228      110390 :     return newTuple;
    1229             : }
    1230             : 
    1231             : /*
    1232             :  * heap_modify_tuple_by_cols
    1233             :  *      form a new tuple from an old tuple and a set of replacement values.
    1234             :  *
    1235             :  * This is like heap_modify_tuple, except that instead of specifying which
    1236             :  * column(s) to replace by a boolean map, an array of target column numbers
    1237             :  * is used.  This is often more convenient when a fixed number of columns
    1238             :  * are to be replaced.  The replCols, replValues, and replIsnull arrays must
    1239             :  * be of length nCols.  Target column numbers are indexed from 1.
    1240             :  *
    1241             :  * The result is allocated in the current memory context.
    1242             :  */
    1243             : HeapTuple
    1244          24 : heap_modify_tuple_by_cols(HeapTuple tuple,
    1245             :                           TupleDesc tupleDesc,
    1246             :                           int nCols,
    1247             :                           int *replCols,
    1248             :                           Datum *replValues,
    1249             :                           bool *replIsnull)
    1250             : {
    1251          24 :     int         numberOfAttributes = tupleDesc->natts;
    1252             :     Datum      *values;
    1253             :     bool       *isnull;
    1254             :     HeapTuple   newTuple;
    1255             :     int         i;
    1256             : 
    1257             :     /*
    1258             :      * allocate and fill values and isnull arrays from the tuple, then replace
    1259             :      * selected columns from the input arrays.
    1260             :      */
    1261          24 :     values = (Datum *) palloc(numberOfAttributes * sizeof(Datum));
    1262          24 :     isnull = (bool *) palloc(numberOfAttributes * sizeof(bool));
    1263             : 
    1264          24 :     heap_deform_tuple(tuple, tupleDesc, values, isnull);
    1265             : 
    1266          48 :     for (i = 0; i < nCols; i++)
    1267             :     {
    1268          24 :         int         attnum = replCols[i];
    1269             : 
    1270          24 :         if (attnum <= 0 || attnum > numberOfAttributes)
    1271           0 :             elog(ERROR, "invalid column number %d", attnum);
    1272          24 :         values[attnum - 1] = replValues[i];
    1273          24 :         isnull[attnum - 1] = replIsnull[i];
    1274             :     }
    1275             : 
    1276             :     /*
    1277             :      * create a new tuple from the values and isnull arrays
    1278             :      */
    1279          24 :     newTuple = heap_form_tuple(tupleDesc, values, isnull);
    1280             : 
    1281          24 :     pfree(values);
    1282          24 :     pfree(isnull);
    1283             : 
    1284             :     /*
    1285             :      * copy the identification info of the old tuple: t_ctid, t_self, and OID
    1286             :      * (if any)
    1287             :      */
    1288          24 :     newTuple->t_data->t_ctid = tuple->t_data->t_ctid;
    1289          24 :     newTuple->t_self = tuple->t_self;
    1290          24 :     newTuple->t_tableOid = tuple->t_tableOid;
    1291          24 :     if (tupleDesc->tdhasoid)
    1292           0 :         HeapTupleSetOid(newTuple, HeapTupleGetOid(tuple));
    1293             : 
    1294          24 :     return newTuple;
    1295             : }
    1296             : 
    1297             : /*
    1298             :  * heap_deform_tuple
    1299             :  *      Given a tuple, extract data into values/isnull arrays; this is
    1300             :  *      the inverse of heap_form_tuple.
    1301             :  *
    1302             :  *      Storage for the values/isnull arrays is provided by the caller;
    1303             :  *      it should be sized according to tupleDesc->natts not
    1304             :  *      HeapTupleHeaderGetNatts(tuple->t_data).
    1305             :  *
    1306             :  *      Note that for pass-by-reference datatypes, the pointer placed
    1307             :  *      in the Datum will point into the given tuple.
    1308             :  *
    1309             :  *      When all or most of a tuple's fields need to be extracted,
    1310             :  *      this routine will be significantly quicker than a loop around
    1311             :  *      heap_getattr; the loop will become O(N^2) as soon as any
    1312             :  *      noncacheable attribute offsets are involved.
    1313             :  */
    1314             : void
    1315     2155036 : heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc,
    1316             :                   Datum *values, bool *isnull)
    1317             : {
    1318     2155036 :     HeapTupleHeader tup = tuple->t_data;
    1319     2155036 :     bool        hasnulls = HeapTupleHasNulls(tuple);
    1320     2155036 :     int         tdesc_natts = tupleDesc->natts;
    1321             :     int         natts;          /* number of atts to extract */
    1322             :     int         attnum;
    1323             :     char       *tp;             /* ptr to tuple data */
    1324             :     uint32      off;            /* offset in tuple data */
    1325     2155036 :     bits8      *bp = tup->t_bits;    /* ptr to null bitmap in tuple */
    1326     2155036 :     bool        slow = false;   /* can we use/set attcacheoff? */
    1327             : 
    1328     2155036 :     natts = HeapTupleHeaderGetNatts(tup);
    1329             : 
    1330             :     /*
    1331             :      * In inheritance situations, it is possible that the given tuple actually
    1332             :      * has more fields than the caller is expecting.  Don't run off the end of
    1333             :      * the caller's arrays.
    1334             :      */
    1335     2155036 :     natts = Min(natts, tdesc_natts);
    1336             : 
    1337     2155036 :     tp = (char *) tup + tup->t_hoff;
    1338             : 
    1339     2155036 :     off = 0;
    1340             : 
    1341    12835094 :     for (attnum = 0; attnum < natts; attnum++)
    1342             :     {
    1343    10680058 :         Form_pg_attribute thisatt = TupleDescAttr(tupleDesc, attnum);
    1344             : 
    1345    10680058 :         if (hasnulls && att_isnull(attnum, bp))
    1346             :         {
    1347     1051700 :             values[attnum] = (Datum) 0;
    1348     1051700 :             isnull[attnum] = true;
    1349     1051700 :             slow = true;        /* can't use attcacheoff anymore */
    1350     1051700 :             continue;
    1351             :         }
    1352             : 
    1353     9628358 :         isnull[attnum] = false;
    1354             : 
    1355     9628358 :         if (!slow && thisatt->attcacheoff >= 0)
    1356     8577528 :             off = thisatt->attcacheoff;
    1357     1050830 :         else if (thisatt->attlen == -1)
    1358             :         {
    1359             :             /*
    1360             :              * We can only cache the offset for a varlena attribute if the
    1361             :              * offset is already suitably aligned, so that there would be no
    1362             :              * pad bytes in any case: then the offset will be valid for either
    1363             :              * an aligned or unaligned value.
    1364             :              */
    1365      571788 :             if (!slow &&
    1366       90808 :                 off == att_align_nominal(off, thisatt->attalign))
    1367        8528 :                 thisatt->attcacheoff = off;
    1368             :             else
    1369             :             {
    1370      472452 :                 off = att_align_pointer(off, thisatt->attalign, -1,
    1371             :                                         tp + off);
    1372      472452 :                 slow = true;
    1373             :             }
    1374             :         }
    1375             :         else
    1376             :         {
    1377             :             /* not varlena, so safe to use att_align_nominal */
    1378      569850 :             off = att_align_nominal(off, thisatt->attalign);
    1379             : 
    1380      569850 :             if (!slow)
    1381       37370 :                 thisatt->attcacheoff = off;
    1382             :         }
    1383             : 
    1384     9628358 :         values[attnum] = fetchatt(thisatt, tp + off);
    1385             : 
    1386     9628358 :         off = att_addlength_pointer(off, thisatt->attlen, tp + off);
    1387             : 
    1388     9628358 :         if (thisatt->attlen <= 0)
    1389     1327516 :             slow = true;        /* can't use attcacheoff anymore */
    1390             :     }
    1391             : 
    1392             :     /*
    1393             :      * If tuple doesn't have all the atts indicated by tupleDesc, read the
    1394             :      * rest as nulls or missing values as appropriate.
    1395             :      */
    1396     2158896 :     for (; attnum < tdesc_natts; attnum++)
    1397        3860 :         values[attnum] = getmissingattr(tupleDesc, attnum + 1, &isnull[attnum]);
    1398     2155036 : }
    1399             : 
    1400             : /*
    1401             :  * slot_deform_tuple
    1402             :  *      Given a TupleTableSlot, extract data from the slot's physical tuple
    1403             :  *      into its Datum/isnull arrays.  Data is extracted up through the
    1404             :  *      natts'th column (caller must ensure this is a legal column number).
    1405             :  *
    1406             :  *      This is essentially an incremental version of heap_deform_tuple:
    1407             :  *      on each call we extract attributes up to the one needed, without
    1408             :  *      re-computing information about previously extracted attributes.
    1409             :  *      slot->tts_nvalid is the number of attributes already extracted.
    1410             :  */
    1411             : static void
    1412   118320616 : slot_deform_tuple(TupleTableSlot *slot, int natts)
    1413             : {
    1414   118320616 :     HeapTuple   tuple = slot->tts_tuple;
    1415   118320616 :     TupleDesc   tupleDesc = slot->tts_tupleDescriptor;
    1416   118320616 :     Datum      *values = slot->tts_values;
    1417   118320616 :     bool       *isnull = slot->tts_isnull;
    1418   118320616 :     HeapTupleHeader tup = tuple->t_data;
    1419   118320616 :     bool        hasnulls = HeapTupleHasNulls(tuple);
    1420             :     int         attnum;
    1421             :     char       *tp;             /* ptr to tuple data */
    1422             :     uint32      off;            /* offset in tuple data */
    1423   118320616 :     bits8      *bp = tup->t_bits;    /* ptr to null bitmap in tuple */
    1424             :     bool        slow;           /* can we use/set attcacheoff? */
    1425             : 
    1426             :     /*
    1427             :      * Check whether the first call for this tuple, and initialize or restore
    1428             :      * loop state.
    1429             :      */
    1430   118320616 :     attnum = slot->tts_nvalid;
    1431   118320616 :     if (attnum == 0)
    1432             :     {
    1433             :         /* Start from the first attribute */
    1434    90238942 :         off = 0;
    1435    90238942 :         slow = false;
    1436             :     }
    1437             :     else
    1438             :     {
    1439             :         /* Restore state from previous execution */
    1440    28081674 :         off = slot->tts_off;
    1441    28081674 :         slow = slot->tts_slow;
    1442             :     }
    1443             : 
    1444   118320616 :     tp = (char *) tup + tup->t_hoff;
    1445             : 
    1446   480381860 :     for (; attnum < natts; attnum++)
    1447             :     {
    1448   362061244 :         Form_pg_attribute thisatt = TupleDescAttr(tupleDesc, attnum);
    1449             : 
    1450   362061244 :         if (hasnulls && att_isnull(attnum, bp))
    1451             :         {
    1452    16323844 :             values[attnum] = (Datum) 0;
    1453    16323844 :             isnull[attnum] = true;
    1454    16323844 :             slow = true;        /* can't use attcacheoff anymore */
    1455    16323844 :             continue;
    1456             :         }
    1457             : 
    1458   345737400 :         isnull[attnum] = false;
    1459             : 
    1460   345737400 :         if (!slow && thisatt->attcacheoff >= 0)
    1461   332326150 :             off = thisatt->attcacheoff;
    1462    13411250 :         else if (thisatt->attlen == -1)
    1463             :         {
    1464             :             /*
    1465             :              * We can only cache the offset for a varlena attribute if the
    1466             :              * offset is already suitably aligned, so that there would be no
    1467             :              * pad bytes in any case: then the offset will be valid for either
    1468             :              * an aligned or unaligned value.
    1469             :              */
    1470     5123814 :             if (!slow &&
    1471      718564 :                 off == att_align_nominal(off, thisatt->attalign))
    1472       34920 :                 thisatt->attcacheoff = off;
    1473             :             else
    1474             :             {
    1475     4370330 :                 off = att_align_pointer(off, thisatt->attalign, -1,
    1476             :                                         tp + off);
    1477     4370330 :                 slow = true;
    1478             :             }
    1479             :         }
    1480             :         else
    1481             :         {
    1482             :             /* not varlena, so safe to use att_align_nominal */
    1483     9006000 :             off = att_align_nominal(off, thisatt->attalign);
    1484             : 
    1485     9006000 :             if (!slow)
    1486      304208 :                 thisatt->attcacheoff = off;
    1487             :         }
    1488             : 
    1489   345737400 :         values[attnum] = fetchatt(thisatt, tp + off);
    1490             : 
    1491   345737400 :         off = att_addlength_pointer(off, thisatt->attlen, tp + off);
    1492             : 
    1493   345737400 :         if (thisatt->attlen <= 0)
    1494    19227064 :             slow = true;        /* can't use attcacheoff anymore */
    1495             :     }
    1496             : 
    1497             :     /*
    1498             :      * Save state for next execution
    1499             :      */
    1500   118320616 :     slot->tts_nvalid = attnum;
    1501   118320616 :     slot->tts_off = off;
    1502   118320616 :     slot->tts_slow = slow;
    1503   118320616 : }
    1504             : 
    1505             : /*
    1506             :  * slot_getattr
    1507             :  *      This function fetches an attribute of the slot's current tuple.
    1508             :  *      It is functionally equivalent to heap_getattr, but fetches of
    1509             :  *      multiple attributes of the same tuple will be optimized better,
    1510             :  *      because we avoid O(N^2) behavior from multiple calls of
    1511             :  *      nocachegetattr(), even when attcacheoff isn't usable.
    1512             :  *
    1513             :  *      A difference from raw heap_getattr is that attnums beyond the
    1514             :  *      slot's tupdesc's last attribute will be considered NULL even
    1515             :  *      when the physical tuple is longer than the tupdesc.
    1516             :  */
    1517             : Datum
    1518    89298408 : slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull)
    1519             : {
    1520    89298408 :     HeapTuple   tuple = slot->tts_tuple;
    1521    89298408 :     TupleDesc   tupleDesc = slot->tts_tupleDescriptor;
    1522             :     HeapTupleHeader tup;
    1523             : 
    1524             :     /*
    1525             :      * system attributes are handled by heap_getsysattr
    1526             :      */
    1527    89298408 :     if (attnum <= 0)
    1528             :     {
    1529     2530264 :         if (tuple == NULL)      /* internal error */
    1530           0 :             elog(ERROR, "cannot extract system attribute from virtual tuple");
    1531     2530264 :         if (tuple == &(slot->tts_minhdr))    /* internal error */
    1532           0 :             elog(ERROR, "cannot extract system attribute from minimal tuple");
    1533     2530264 :         return heap_getsysattr(tuple, attnum, tupleDesc, isnull);
    1534             :     }
    1535             : 
    1536             :     /*
    1537             :      * fast path if desired attribute already cached
    1538             :      */
    1539    86768144 :     if (attnum <= slot->tts_nvalid)
    1540             :     {
    1541    29045392 :         *isnull = slot->tts_isnull[attnum - 1];
    1542    29045392 :         return slot->tts_values[attnum - 1];
    1543             :     }
    1544             : 
    1545             :     /*
    1546             :      * return NULL if attnum is out of range according to the tupdesc
    1547             :      */
    1548    57722752 :     if (attnum > tupleDesc->natts)
    1549             :     {
    1550           0 :         *isnull = true;
    1551           0 :         return (Datum) 0;
    1552             :     }
    1553             : 
    1554             :     /*
    1555             :      * otherwise we had better have a physical tuple (tts_nvalid should equal
    1556             :      * natts in all virtual-tuple cases)
    1557             :      */
    1558    57722752 :     if (tuple == NULL)          /* internal error */
    1559           0 :         elog(ERROR, "cannot extract attribute from empty tuple slot");
    1560             : 
    1561             :     /*
    1562             :      * return NULL or missing value if attnum is out of range according to the
    1563             :      * tuple
    1564             :      *
    1565             :      * (We have to check this separately because of various inheritance and
    1566             :      * table-alteration scenarios: the tuple could be either longer or shorter
    1567             :      * than the tupdesc.)
    1568             :      */
    1569    57722752 :     tup = tuple->t_data;
    1570    57722752 :     if (attnum > HeapTupleHeaderGetNatts(tup))
    1571          48 :         return getmissingattr(slot->tts_tupleDescriptor, attnum, isnull);
    1572             : 
    1573             :     /*
    1574             :      * check if target attribute is null: no point in groveling through tuple
    1575             :      */
    1576    57722704 :     if (HeapTupleHasNulls(tuple) && att_isnull(attnum - 1, tup->t_bits))
    1577             :     {
    1578       19828 :         *isnull = true;
    1579       19828 :         return (Datum) 0;
    1580             :     }
    1581             : 
    1582             :     /*
    1583             :      * If the attribute's column has been dropped, we force a NULL result.
    1584             :      * This case should not happen in normal use, but it could happen if we
    1585             :      * are executing a plan cached before the column was dropped.
    1586             :      */
    1587    57702876 :     if (TupleDescAttr(tupleDesc, attnum - 1)->attisdropped)
    1588             :     {
    1589           0 :         *isnull = true;
    1590           0 :         return (Datum) 0;
    1591             :     }
    1592             : 
    1593             :     /*
    1594             :      * Extract the attribute, along with any preceding attributes.
    1595             :      */
    1596    57702876 :     slot_deform_tuple(slot, attnum);
    1597             : 
    1598             :     /*
    1599             :      * The result is acquired from tts_values array.
    1600             :      */
    1601    57702876 :     *isnull = slot->tts_isnull[attnum - 1];
    1602    57702876 :     return slot->tts_values[attnum - 1];
    1603             : }
    1604             : 
    1605             : /*
    1606             :  * slot_getallattrs
    1607             :  *      This function forces all the entries of the slot's Datum/isnull
    1608             :  *      arrays to be valid.  The caller may then extract data directly
    1609             :  *      from those arrays instead of using slot_getattr.
    1610             :  */
    1611             : void
    1612     2282910 : slot_getallattrs(TupleTableSlot *slot)
    1613             : {
    1614     2282910 :     int         tdesc_natts = slot->tts_tupleDescriptor->natts;
    1615             :     int         attnum;
    1616             :     HeapTuple   tuple;
    1617             : 
    1618             :     /* Quick out if we have 'em all already */
    1619     2282910 :     if (slot->tts_nvalid == tdesc_natts)
    1620      684150 :         return;
    1621             : 
    1622             :     /*
    1623             :      * otherwise we had better have a physical tuple (tts_nvalid should equal
    1624             :      * natts in all virtual-tuple cases)
    1625             :      */
    1626     1598760 :     tuple = slot->tts_tuple;
    1627     1598760 :     if (tuple == NULL)          /* internal error */
    1628           0 :         elog(ERROR, "cannot extract attribute from empty tuple slot");
    1629             : 
    1630             :     /*
    1631             :      * load up any slots available from physical tuple
    1632             :      */
    1633     1598760 :     attnum = HeapTupleHeaderGetNatts(tuple->t_data);
    1634     1598760 :     attnum = Min(attnum, tdesc_natts);
    1635             : 
    1636     1598760 :     slot_deform_tuple(slot, attnum);
    1637             : 
    1638     1598760 :     attnum = slot->tts_nvalid;
    1639             : 
    1640             :     /*
    1641             :      * If tuple doesn't have all the atts indicated by tupleDesc, read the
    1642             :      * rest as NULLS or missing values.
    1643             :      */
    1644     1598760 :     if (attnum < tdesc_natts)
    1645          40 :         slot_getmissingattrs(slot, attnum, tdesc_natts);
    1646             : 
    1647     1598760 :     slot->tts_nvalid = tdesc_natts;
    1648             : }
    1649             : 
    1650             : /*
    1651             :  * slot_getsomeattrs
    1652             :  *      This function forces the entries of the slot's Datum/isnull
    1653             :  *      arrays to be valid at least up through the attnum'th entry.
    1654             :  */
    1655             : void
    1656    87540420 : slot_getsomeattrs(TupleTableSlot *slot, int attnum)
    1657             : {
    1658             :     HeapTuple   tuple;
    1659             :     int         attno;
    1660             : 
    1661             :     /* Quick out if we have 'em all already */
    1662    87540420 :     if (slot->tts_nvalid >= attnum)
    1663    28521440 :         return;
    1664             : 
    1665             :     /* Check for caller error */
    1666    59018980 :     if (attnum <= 0 || attnum > slot->tts_tupleDescriptor->natts)
    1667           0 :         elog(ERROR, "invalid attribute number %d", attnum);
    1668             : 
    1669             :     /*
    1670             :      * otherwise we had better have a physical tuple (tts_nvalid should equal
    1671             :      * natts in all virtual-tuple cases)
    1672             :      */
    1673    59018980 :     tuple = slot->tts_tuple;
    1674    59018980 :     if (tuple == NULL)          /* internal error */
    1675           0 :         elog(ERROR, "cannot extract attribute from empty tuple slot");
    1676             : 
    1677             :     /*
    1678             :      * load up any slots available from physical tuple
    1679             :      */
    1680    59018980 :     attno = HeapTupleHeaderGetNatts(tuple->t_data);
    1681    59018980 :     attno = Min(attno, attnum);
    1682             : 
    1683    59018980 :     slot_deform_tuple(slot, attno);
    1684             : 
    1685    59018980 :     attno = slot->tts_nvalid;
    1686             : 
    1687             :     /*
    1688             :      * If tuple doesn't have all the atts indicated by attnum, read the rest
    1689             :      * as NULLs or missing values
    1690             :      */
    1691    59018980 :     if (attno < attnum)
    1692         882 :         slot_getmissingattrs(slot, attno, attnum);
    1693             : 
    1694    59018980 :     slot->tts_nvalid = attnum;
    1695             : }
    1696             : 
    1697             : /*
    1698             :  * slot_attisnull
    1699             :  *      Detect whether an attribute of the slot is null, without
    1700             :  *      actually fetching it.
    1701             :  */
    1702             : bool
    1703    20999434 : slot_attisnull(TupleTableSlot *slot, int attnum)
    1704             : {
    1705    20999434 :     HeapTuple   tuple = slot->tts_tuple;
    1706    20999434 :     TupleDesc   tupleDesc = slot->tts_tupleDescriptor;
    1707             : 
    1708             :     /*
    1709             :      * system attributes are handled by heap_attisnull
    1710             :      */
    1711    20999434 :     if (attnum <= 0)
    1712             :     {
    1713           0 :         if (tuple == NULL)      /* internal error */
    1714           0 :             elog(ERROR, "cannot extract system attribute from virtual tuple");
    1715           0 :         if (tuple == &(slot->tts_minhdr))    /* internal error */
    1716           0 :             elog(ERROR, "cannot extract system attribute from minimal tuple");
    1717           0 :         return heap_attisnull(tuple, attnum, tupleDesc);
    1718             :     }
    1719             : 
    1720             :     /*
    1721             :      * fast path if desired attribute already cached
    1722             :      */
    1723    20999434 :     if (attnum <= slot->tts_nvalid)
    1724      618676 :         return slot->tts_isnull[attnum - 1];
    1725             : 
    1726             :     /*
    1727             :      * return NULL if attnum is out of range according to the tupdesc
    1728             :      */
    1729    20380758 :     if (attnum > tupleDesc->natts)
    1730           0 :         return true;
    1731             : 
    1732             :     /*
    1733             :      * otherwise we had better have a physical tuple (tts_nvalid should equal
    1734             :      * natts in all virtual-tuple cases)
    1735             :      */
    1736    20380758 :     if (tuple == NULL)          /* internal error */
    1737           0 :         elog(ERROR, "cannot extract attribute from empty tuple slot");
    1738             : 
    1739             :     /* and let the tuple tell it */
    1740    20380758 :     return heap_attisnull(tuple, attnum, tupleDesc);
    1741             : }
    1742             : 
    1743             : /*
    1744             :  * slot_getsysattr
    1745             :  *      This function fetches a system attribute of the slot's current tuple.
    1746             :  *      Unlike slot_getattr, if the slot does not contain system attributes,
    1747             :  *      this will return false (with a NULL attribute value) instead of
    1748             :  *      throwing an error.
    1749             :  */
    1750             : bool
    1751         128 : slot_getsysattr(TupleTableSlot *slot, int attnum,
    1752             :                 Datum *value, bool *isnull)
    1753             : {
    1754         128 :     HeapTuple   tuple = slot->tts_tuple;
    1755             : 
    1756             :     Assert(attnum < 0);          /* else caller error */
    1757         256 :     if (tuple == NULL ||
    1758         128 :         tuple == &(slot->tts_minhdr))
    1759             :     {
    1760             :         /* No physical tuple, or minimal tuple, so fail */
    1761           0 :         *value = (Datum) 0;
    1762           0 :         *isnull = true;
    1763           0 :         return false;
    1764             :     }
    1765         128 :     *value = heap_getsysattr(tuple, attnum, slot->tts_tupleDescriptor, isnull);
    1766         128 :     return true;
    1767             : }
    1768             : 
    1769             : /*
    1770             :  * heap_freetuple
    1771             :  */
    1772             : void
    1773    23080432 : heap_freetuple(HeapTuple htup)
    1774             : {
    1775    23080432 :     pfree(htup);
    1776    23080432 : }
    1777             : 
    1778             : 
    1779             : /*
    1780             :  * heap_form_minimal_tuple
    1781             :  *      construct a MinimalTuple from the given values[] and isnull[] arrays,
    1782             :  *      which are of the length indicated by tupleDescriptor->natts
    1783             :  *
    1784             :  * This is exactly like heap_form_tuple() except that the result is a
    1785             :  * "minimal" tuple lacking a HeapTupleData header as well as room for system
    1786             :  * columns.
    1787             :  *
    1788             :  * The result is allocated in the current memory context.
    1789             :  */
    1790             : MinimalTuple
    1791    27392562 : heap_form_minimal_tuple(TupleDesc tupleDescriptor,
    1792             :                         Datum *values,
    1793             :                         bool *isnull)
    1794             : {
    1795             :     MinimalTuple tuple;         /* return tuple */
    1796             :     Size        len,
    1797             :                 data_len;
    1798             :     int         hoff;
    1799    27392562 :     bool        hasnull = false;
    1800    27392562 :     int         numberOfAttributes = tupleDescriptor->natts;
    1801             :     int         i;
    1802             : 
    1803    27392562 :     if (numberOfAttributes > MaxTupleAttributeNumber)
    1804           0 :         ereport(ERROR,
    1805             :                 (errcode(ERRCODE_TOO_MANY_COLUMNS),
    1806             :                  errmsg("number of columns (%d) exceeds limit (%d)",
    1807             :                         numberOfAttributes, MaxTupleAttributeNumber)));
    1808             : 
    1809             :     /*
    1810             :      * Check for nulls
    1811             :      */
    1812    72696134 :     for (i = 0; i < numberOfAttributes; i++)
    1813             :     {
    1814    46004182 :         if (isnull[i])
    1815             :         {
    1816      700610 :             hasnull = true;
    1817      700610 :             break;
    1818             :         }
    1819             :     }
    1820             : 
    1821             :     /*
    1822             :      * Determine total space needed
    1823             :      */
    1824    27392562 :     len = SizeofMinimalTupleHeader;
    1825             : 
    1826    27392562 :     if (hasnull)
    1827      700610 :         len += BITMAPLEN(numberOfAttributes);
    1828             : 
    1829    27392562 :     if (tupleDescriptor->tdhasoid)
    1830           0 :         len += sizeof(Oid);
    1831             : 
    1832    27392562 :     hoff = len = MAXALIGN(len); /* align user data safely */
    1833             : 
    1834    27392562 :     data_len = heap_compute_data_size(tupleDescriptor, values, isnull);
    1835             : 
    1836    27392562 :     len += data_len;
    1837             : 
    1838             :     /*
    1839             :      * Allocate and zero the space needed.
    1840             :      */
    1841    27392562 :     tuple = (MinimalTuple) palloc0(len);
    1842             : 
    1843             :     /*
    1844             :      * And fill in the information.
    1845             :      */
    1846    27392562 :     tuple->t_len = len;
    1847    27392562 :     HeapTupleHeaderSetNatts(tuple, numberOfAttributes);
    1848    27392562 :     tuple->t_hoff = hoff + MINIMAL_TUPLE_OFFSET;
    1849             : 
    1850    27392562 :     if (tupleDescriptor->tdhasoid)   /* else leave infomask = 0 */
    1851           0 :         tuple->t_infomask = HEAP_HASOID;
    1852             : 
    1853    27392562 :     heap_fill_tuple(tupleDescriptor,
    1854             :                     values,
    1855             :                     isnull,
    1856             :                     (char *) tuple + hoff,
    1857             :                     data_len,
    1858             :                     &tuple->t_infomask,
    1859             :                     (hasnull ? tuple->t_bits : NULL));
    1860             : 
    1861    27392562 :     return tuple;
    1862             : }
    1863             : 
    1864             : /*
    1865             :  * heap_free_minimal_tuple
    1866             :  */
    1867             : void
    1868    24041896 : heap_free_minimal_tuple(MinimalTuple mtup)
    1869             : {
    1870    24041896 :     pfree(mtup);
    1871    24041896 : }
    1872             : 
    1873             : /*
    1874             :  * heap_copy_minimal_tuple
    1875             :  *      copy a MinimalTuple
    1876             :  *
    1877             :  * The result is allocated in the current memory context.
    1878             :  */
    1879             : MinimalTuple
    1880     1094140 : heap_copy_minimal_tuple(MinimalTuple mtup)
    1881             : {
    1882             :     MinimalTuple result;
    1883             : 
    1884     1094140 :     result = (MinimalTuple) palloc(mtup->t_len);
    1885     1094140 :     memcpy(result, mtup, mtup->t_len);
    1886     1094140 :     return result;
    1887             : }
    1888             : 
    1889             : /*
    1890             :  * heap_tuple_from_minimal_tuple
    1891             :  *      create a HeapTuple by copying from a MinimalTuple;
    1892             :  *      system columns are filled with zeroes
    1893             :  *
    1894             :  * The result is allocated in the current memory context.
    1895             :  * The HeapTuple struct, tuple header, and tuple data are all allocated
    1896             :  * as a single palloc() block.
    1897             :  */
    1898             : HeapTuple
    1899      320568 : heap_tuple_from_minimal_tuple(MinimalTuple mtup)
    1900             : {
    1901             :     HeapTuple   result;
    1902      320568 :     uint32      len = mtup->t_len + MINIMAL_TUPLE_OFFSET;
    1903             : 
    1904      320568 :     result = (HeapTuple) palloc(HEAPTUPLESIZE + len);
    1905      320568 :     result->t_len = len;
    1906      320568 :     ItemPointerSetInvalid(&(result->t_self));
    1907      320568 :     result->t_tableOid = InvalidOid;
    1908      320568 :     result->t_data = (HeapTupleHeader) ((char *) result + HEAPTUPLESIZE);
    1909      320568 :     memcpy((char *) result->t_data + MINIMAL_TUPLE_OFFSET, mtup, mtup->t_len);
    1910      320568 :     memset(result->t_data, 0, offsetof(HeapTupleHeaderData, t_infomask2));
    1911      320568 :     return result;
    1912             : }
    1913             : 
    1914             : /*
    1915             :  * minimal_tuple_from_heap_tuple
    1916             :  *      create a MinimalTuple by copying from a HeapTuple
    1917             :  *
    1918             :  * The result is allocated in the current memory context.
    1919             :  */
    1920             : MinimalTuple
    1921     1147180 : minimal_tuple_from_heap_tuple(HeapTuple htup)
    1922             : {
    1923             :     MinimalTuple result;
    1924             :     uint32      len;
    1925             : 
    1926             :     Assert(htup->t_len > MINIMAL_TUPLE_OFFSET);
    1927     1147180 :     len = htup->t_len - MINIMAL_TUPLE_OFFSET;
    1928     1147180 :     result = (MinimalTuple) palloc(len);
    1929     1147180 :     memcpy(result, (char *) htup->t_data + MINIMAL_TUPLE_OFFSET, len);
    1930     1147180 :     result->t_len = len;
    1931     1147180 :     return result;
    1932             : }
    1933             : 
    1934             : /*
    1935             :  * This mainly exists so JIT can inline the definition, but it's also
    1936             :  * sometimes useful in debugging sessions.
    1937             :  */
    1938             : size_t
    1939           0 : varsize_any(void *p)
    1940             : {
    1941           0 :     return VARSIZE_ANY(p);
    1942             : }

Generated by: LCOV version 1.13