LCOV - code coverage report
Current view: top level - src/backend/access/common - heaptuple.c (source / functions) Hit Total Coverage
Test: PostgreSQL 12devel Lines: 415 455 91.2 %
Date: 2018-12-15 21:21:55 Functions: 21 24 87.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             : static Datum getmissingattr(TupleDesc tupleDesc, int attnum, bool *isnull);
      75             : 
      76             : 
      77             : /* ----------------------------------------------------------------
      78             :  *                      misc support routines
      79             :  * ----------------------------------------------------------------
      80             :  */
      81             : 
      82             : /*
      83             :  * Return the missing value of an attribute, or NULL if there isn't one.
      84             :  */
      85             : static Datum
      86        3864 : getmissingattr(TupleDesc tupleDesc,
      87             :                int attnum, bool *isnull)
      88             : {
      89             :     Form_pg_attribute att;
      90             : 
      91             :     Assert(attnum <= tupleDesc->natts);
      92             :     Assert(attnum > 0);
      93             : 
      94        3864 :     att = TupleDescAttr(tupleDesc, attnum - 1);
      95             : 
      96        3864 :     if (att->atthasmissing)
      97             :     {
      98             :         AttrMissing *attrmiss;
      99             : 
     100             :         Assert(tupleDesc->constr);
     101             :         Assert(tupleDesc->constr->missing);
     102             : 
     103        2088 :         attrmiss = tupleDesc->constr->missing + (attnum - 1);
     104             : 
     105        2088 :         if (attrmiss->am_present)
     106             :         {
     107        2088 :             *isnull = false;
     108        2088 :             return attrmiss->am_value;
     109             :         }
     110             :     }
     111             : 
     112        1776 :     *isnull = true;
     113        1776 :     return PointerGetDatum(NULL);
     114             : }
     115             : 
     116             : /*
     117             :  * heap_compute_data_size
     118             :  *      Determine size of the data area of a tuple to be constructed
     119             :  */
     120             : Size
     121    78785968 : heap_compute_data_size(TupleDesc tupleDesc,
     122             :                        Datum *values,
     123             :                        bool *isnull)
     124             : {
     125    78785968 :     Size        data_length = 0;
     126             :     int         i;
     127    78785968 :     int         numberOfAttributes = tupleDesc->natts;
     128             : 
     129   321368992 :     for (i = 0; i < numberOfAttributes; i++)
     130             :     {
     131             :         Datum       val;
     132             :         Form_pg_attribute atti;
     133             : 
     134   242583024 :         if (isnull[i])
     135    20147120 :             continue;
     136             : 
     137   222435904 :         val = values[i];
     138   222435904 :         atti = TupleDescAttr(tupleDesc, i);
     139             : 
     140   238516258 :         if (ATT_IS_PACKABLE(atti) &&
     141    27753938 :             VARATT_CAN_MAKE_SHORT(DatumGetPointer(val)))
     142             :         {
     143             :             /*
     144             :              * we're anticipating converting to a short varlena header, so
     145             :              * adjust length and don't count any alignment
     146             :              */
     147    11322748 :             data_length += VARATT_CONVERTED_SHORT_SIZE(DatumGetPointer(val));
     148             :         }
     149   218588580 :         else if (atti->attlen == -1 &&
     150     7594928 :                  VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(val)))
     151             :         {
     152             :             /*
     153             :              * we want to flatten the expanded value so that the constructed
     154             :              * tuple doesn't depend on it
     155             :              */
     156        1548 :             data_length = att_align_nominal(data_length, atti->attalign);
     157        1548 :             data_length += EOH_get_flat_size(DatumGetEOHP(val));
     158             :         }
     159             :         else
     160             :         {
     161   211111608 :             data_length = att_align_datum(data_length, atti->attalign,
     162             :                                           atti->attlen, val);
     163   211111608 :             data_length = att_addlength_datum(data_length, atti->attlen,
     164             :                                               val);
     165             :         }
     166             :     }
     167             : 
     168    78785968 :     return data_length;
     169             : }
     170             : 
     171             : /*
     172             :  * Per-attribute helper for heap_fill_tuple and other routines building tuples.
     173             :  *
     174             :  * Fill in either a data value or a bit in the null bitmask
     175             :  */
     176             : static inline void
     177   239899548 : fill_val(Form_pg_attribute att,
     178             :          bits8 **bit,
     179             :          int *bitmask,
     180             :          char **dataP,
     181             :          uint16 *infomask,
     182             :          Datum datum,
     183             :          bool isnull)
     184             : {
     185             :     Size        data_length;
     186   239899548 :     char       *data = *dataP;
     187             : 
     188             :     /*
     189             :      * If we're building a null bitmap, set the appropriate bit for the
     190             :      * current column value here.
     191             :      */
     192   239899548 :     if (bit != NULL)
     193             :     {
     194    87570432 :         if (*bitmask != HIGHBIT)
     195    73270744 :             *bitmask <<= 1;
     196             :         else
     197             :         {
     198    14299688 :             *bit += 1;
     199    14299688 :             **bit = 0x0;
     200    14299688 :             *bitmask = 1;
     201             :         }
     202             : 
     203    87570432 :         if (isnull)
     204             :         {
     205    19928930 :             *infomask |= HEAP_HASNULL;
     206    19928930 :             return;
     207             :         }
     208             : 
     209    67641502 :         **bit |= *bitmask;
     210             :     }
     211             : 
     212             :     /*
     213             :      * XXX we use the att_align macros on the pointer value itself, not on an
     214             :      * offset.  This is a bit of a hack.
     215             :      */
     216   219970618 :     if (att->attbyval)
     217             :     {
     218             :         /* pass-by-value */
     219   187833528 :         data = (char *) att_align_nominal(data, att->attalign);
     220   187833528 :         store_att_byval(data, datum, att->attlen);
     221   187833528 :         data_length = att->attlen;
     222             :     }
     223    32137090 :     else if (att->attlen == -1)
     224             :     {
     225             :         /* varlena */
     226    18255546 :         Pointer     val = DatumGetPointer(datum);
     227             : 
     228    18255546 :         *infomask |= HEAP_HASVARWIDTH;
     229    18255546 :         if (VARATT_IS_EXTERNAL(val))
     230             :         {
     231       25850 :             if (VARATT_IS_EXTERNAL_EXPANDED(val))
     232        1548 :             {
     233             :                 /*
     234             :                  * we want to flatten the expanded value so that the
     235             :                  * constructed tuple doesn't depend on it
     236             :                  */
     237        1548 :                 ExpandedObjectHeader *eoh = DatumGetEOHP(datum);
     238             : 
     239        1548 :                 data = (char *) att_align_nominal(data,
     240             :                                                   att->attalign);
     241        1548 :                 data_length = EOH_get_flat_size(eoh);
     242        1548 :                 EOH_flatten_into(eoh, data, data_length);
     243             :             }
     244             :             else
     245             :             {
     246       24302 :                 *infomask |= HEAP_HASEXTERNAL;
     247             :                 /* no alignment, since it's short by definition */
     248       24302 :                 data_length = VARSIZE_EXTERNAL(val);
     249       24302 :                 memcpy(data, val, data_length);
     250             :             }
     251             :         }
     252    18229696 :         else if (VARATT_IS_SHORT(val))
     253             :         {
     254             :             /* no alignment for short varlenas */
     255     3891346 :             data_length = VARSIZE_SHORT(val);
     256     3891346 :             memcpy(data, val, data_length);
     257             :         }
     258    25958992 :         else if (VARLENA_ATT_IS_PACKABLE(att) &&
     259    23206406 :                  VARATT_CAN_MAKE_SHORT(val))
     260             :         {
     261             :             /* convert to short varlena -- no alignment */
     262    11322750 :             data_length = VARATT_CONVERTED_SHORT_SIZE(val);
     263    11322750 :             SET_VARSIZE_SHORT(data, data_length);
     264    11322750 :             memcpy(data + 1, VARDATA(val), data_length - 1);
     265             :         }
     266             :         else
     267             :         {
     268             :             /* full 4-byte header varlena */
     269     3015600 :             data = (char *) att_align_nominal(data,
     270             :                                               att->attalign);
     271     3015600 :             data_length = VARSIZE(val);
     272     3015600 :             memcpy(data, val, data_length);
     273             :         }
     274             :     }
     275    13881544 :     else if (att->attlen == -2)
     276             :     {
     277             :         /* cstring ... never needs alignment */
     278     3056428 :         *infomask |= HEAP_HASVARWIDTH;
     279             :         Assert(att->attalign == 'c');
     280     3056428 :         data_length = strlen(DatumGetCString(datum)) + 1;
     281     3056428 :         memcpy(data, DatumGetPointer(datum), data_length);
     282             :     }
     283             :     else
     284             :     {
     285             :         /* fixed-length pass-by-reference */
     286    10825116 :         data = (char *) att_align_nominal(data, att->attalign);
     287             :         Assert(att->attlen > 0);
     288    10825116 :         data_length = att->attlen;
     289    10825116 :         memcpy(data, DatumGetPointer(datum), data_length);
     290             :     }
     291             : 
     292   219970618 :     data += data_length;
     293   219970618 :     *dataP = data;
     294             : }
     295             : 
     296             : /*
     297             :  * heap_fill_tuple
     298             :  *      Load data portion of a tuple from values/isnull arrays
     299             :  *
     300             :  * We also fill the null bitmap (if any) and set the infomask bits
     301             :  * that reflect the tuple's data contents.
     302             :  *
     303             :  * NOTE: it is now REQUIRED that the caller have pre-zeroed the data area.
     304             :  */
     305             : void
     306    78536734 : heap_fill_tuple(TupleDesc tupleDesc,
     307             :                 Datum *values, bool *isnull,
     308             :                 char *data, Size data_size,
     309             :                 uint16 *infomask, bits8 *bit)
     310             : {
     311             :     bits8      *bitP;
     312             :     int         bitmask;
     313             :     int         i;
     314    78536734 :     int         numberOfAttributes = tupleDesc->natts;
     315             : 
     316             : #ifdef USE_ASSERT_CHECKING
     317             :     char       *start = data;
     318             : #endif
     319             : 
     320    78536734 :     if (bit != NULL)
     321             :     {
     322     6681848 :         bitP = &bit[-1];
     323     6681848 :         bitmask = HIGHBIT;
     324             :     }
     325             :     else
     326             :     {
     327             :         /* just to keep compiler quiet */
     328    71854886 :         bitP = NULL;
     329    71854886 :         bitmask = 0;
     330             :     }
     331             : 
     332    78536734 :     *infomask &= ~(HEAP_HASNULL | HEAP_HASVARWIDTH | HEAP_HASEXTERNAL);
     333             : 
     334   318436210 :     for (i = 0; i < numberOfAttributes; i++)
     335             :     {
     336   239899476 :         Form_pg_attribute attr = TupleDescAttr(tupleDesc, i);
     337             : 
     338   719698270 :         fill_val(attr,
     339   239899476 :                  bitP ? &bitP : NULL,
     340             :                  &bitmask,
     341             :                  &data,
     342             :                  infomask,
     343   239899318 :                  values ? values[i] : PointerGetDatum(NULL),
     344   239899476 :                  isnull ? isnull[i] : true);
     345             :     }
     346             : 
     347             :     Assert((data - start) == data_size);
     348    78536734 : }
     349             : 
     350             : 
     351             : /* ----------------------------------------------------------------
     352             :  *                      heap tuple interface
     353             :  * ----------------------------------------------------------------
     354             :  */
     355             : 
     356             : /* ----------------
     357             :  *      heap_attisnull  - returns true iff tuple attribute is not present
     358             :  * ----------------
     359             :  */
     360             : bool
     361     9226030 : heap_attisnull(HeapTuple tup, int attnum, TupleDesc tupleDesc)
     362             : {
     363             :     /*
     364             :      * We allow a NULL tupledesc for relations not expected to have missing
     365             :      * values, such as catalog relations and indexes.
     366             :      */
     367             :     Assert(!tupleDesc || attnum <= tupleDesc->natts);
     368     9226030 :     if (attnum > (int) HeapTupleHeaderGetNatts(tup->t_data))
     369             :     {
     370           4 :         if (tupleDesc && TupleDescAttr(tupleDesc, attnum - 1)->atthasmissing)
     371           0 :             return false;
     372             :         else
     373           4 :             return true;
     374             :     }
     375             : 
     376     9226026 :     if (attnum > 0)
     377             :     {
     378     9226026 :         if (HeapTupleNoNulls(tup))
     379     1924818 :             return false;
     380     7301208 :         return att_isnull(attnum - 1, tup->t_data->t_bits);
     381             :     }
     382             : 
     383           0 :     switch (attnum)
     384             :     {
     385             :         case TableOidAttributeNumber:
     386             :         case SelfItemPointerAttributeNumber:
     387             :         case MinTransactionIdAttributeNumber:
     388             :         case MinCommandIdAttributeNumber:
     389             :         case MaxTransactionIdAttributeNumber:
     390             :         case MaxCommandIdAttributeNumber:
     391             :             /* these are never null */
     392           0 :             break;
     393             : 
     394             :         default:
     395           0 :             elog(ERROR, "invalid attnum: %d", attnum);
     396             :     }
     397             : 
     398           0 :     return false;
     399             : }
     400             : 
     401             : /* ----------------
     402             :  *      nocachegetattr
     403             :  *
     404             :  *      This only gets called from fastgetattr() macro, in cases where
     405             :  *      we can't use a cacheoffset and the value is not null.
     406             :  *
     407             :  *      This caches attribute offsets in the attribute descriptor.
     408             :  *
     409             :  *      An alternative way to speed things up would be to cache offsets
     410             :  *      with the tuple, but that seems more difficult unless you take
     411             :  *      the storage hit of actually putting those offsets into the
     412             :  *      tuple you send to disk.  Yuck.
     413             :  *
     414             :  *      This scheme will be slightly slower than that, but should
     415             :  *      perform well for queries which hit large #'s of tuples.  After
     416             :  *      you cache the offsets once, examining all the other tuples using
     417             :  *      the same attribute descriptor will go much quicker. -cim 5/4/91
     418             :  *
     419             :  *      NOTE: if you need to change this code, see also heap_deform_tuple.
     420             :  *      Also see nocache_index_getattr, which is the same code for index
     421             :  *      tuples.
     422             :  * ----------------
     423             :  */
     424             : Datum
     425   209022386 : nocachegetattr(HeapTuple tuple,
     426             :                int attnum,
     427             :                TupleDesc tupleDesc)
     428             : {
     429   209022386 :     HeapTupleHeader tup = tuple->t_data;
     430             :     char       *tp;             /* ptr to data part of tuple */
     431   209022386 :     bits8      *bp = tup->t_bits;    /* ptr to null bitmap in tuple */
     432   209022386 :     bool        slow = false;   /* do we have to walk attrs? */
     433             :     int         off;            /* current offset within data */
     434             : 
     435             :     /* ----------------
     436             :      *   Three cases:
     437             :      *
     438             :      *   1: No nulls and no variable-width attributes.
     439             :      *   2: Has a null or a var-width AFTER att.
     440             :      *   3: Has nulls or var-widths BEFORE att.
     441             :      * ----------------
     442             :      */
     443             : 
     444   209022386 :     attnum--;
     445             : 
     446   209022386 :     if (!HeapTupleNoNulls(tuple))
     447             :     {
     448             :         /*
     449             :          * there's a null somewhere in the tuple
     450             :          *
     451             :          * check to see if any preceding bits are null...
     452             :          */
     453   198186142 :         int         byte = attnum >> 3;
     454   198186142 :         int         finalbit = attnum & 0x07;
     455             : 
     456             :         /* check for nulls "before" final bit of last byte */
     457   198186142 :         if ((~bp[byte]) & ((1 << finalbit) - 1))
     458     9919494 :             slow = true;
     459             :         else
     460             :         {
     461             :             /* check for nulls in any "earlier" bytes */
     462             :             int         i;
     463             : 
     464   246887570 :             for (i = 0; i < byte; i++)
     465             :             {
     466    59027200 :                 if (bp[i] != 0xFF)
     467             :                 {
     468      406278 :                     slow = true;
     469      406278 :                     break;
     470             :                 }
     471             :             }
     472             :         }
     473             :     }
     474             : 
     475   209022386 :     tp = (char *) tup + tup->t_hoff;
     476             : 
     477   209022386 :     if (!slow)
     478             :     {
     479             :         Form_pg_attribute att;
     480             : 
     481             :         /*
     482             :          * If we get here, there are no nulls up to and including the target
     483             :          * attribute.  If we have a cached offset, we can use it.
     484             :          */
     485   198696614 :         att = TupleDescAttr(tupleDesc, attnum);
     486   198696614 :         if (att->attcacheoff >= 0)
     487   185128198 :             return fetchatt(att, tp + att->attcacheoff);
     488             : 
     489             :         /*
     490             :          * Otherwise, check for non-fixed-length attrs up to and including
     491             :          * target.  If there aren't any, it's safe to cheaply initialize the
     492             :          * cached offsets for these attrs.
     493             :          */
     494    13568416 :         if (HeapTupleHasVarWidth(tuple))
     495             :         {
     496             :             int         j;
     497             : 
     498    41747752 :             for (j = 0; j <= attnum; j++)
     499             :             {
     500    41721386 :                 if (TupleDescAttr(tupleDesc, j)->attlen <= 0)
     501             :                 {
     502    13474358 :                     slow = true;
     503    13474358 :                     break;
     504             :                 }
     505             :             }
     506             :         }
     507             :     }
     508             : 
     509    23894188 :     if (!slow)
     510             :     {
     511       94058 :         int         natts = tupleDesc->natts;
     512       94058 :         int         j = 1;
     513             : 
     514             :         /*
     515             :          * If we get here, we have a tuple with no nulls or var-widths up to
     516             :          * and including the target attribute, so we can use the cached offset
     517             :          * ... only we don't have it yet, or we'd not have got here.  Since
     518             :          * it's cheap to compute offsets for fixed-width columns, we take the
     519             :          * opportunity to initialize the cached offsets for *all* the leading
     520             :          * fixed-width columns, in hope of avoiding future visits to this
     521             :          * routine.
     522             :          */
     523       94058 :         TupleDescAttr(tupleDesc, 0)->attcacheoff = 0;
     524             : 
     525             :         /* we might have set some offsets in the slow path previously */
     526      190850 :         while (j < natts && TupleDescAttr(tupleDesc, j)->attcacheoff > 0)
     527        2734 :             j++;
     528             : 
     529      188116 :         off = TupleDescAttr(tupleDesc, j - 1)->attcacheoff +
     530       94058 :             TupleDescAttr(tupleDesc, j - 1)->attlen;
     531             : 
     532      801672 :         for (; j < natts; j++)
     533             :         {
     534      749816 :             Form_pg_attribute att = TupleDescAttr(tupleDesc, j);
     535             : 
     536      749816 :             if (att->attlen <= 0)
     537       42202 :                 break;
     538             : 
     539      707614 :             off = att_align_nominal(off, att->attalign);
     540             : 
     541      707614 :             att->attcacheoff = off;
     542             : 
     543      707614 :             off += att->attlen;
     544             :         }
     545             : 
     546             :         Assert(j > attnum);
     547             : 
     548       94058 :         off = TupleDescAttr(tupleDesc, attnum)->attcacheoff;
     549             :     }
     550             :     else
     551             :     {
     552    23800130 :         bool        usecache = true;
     553             :         int         i;
     554             : 
     555             :         /*
     556             :          * Now we know that we have to walk the tuple CAREFULLY.  But we still
     557             :          * might be able to cache some offsets for next time.
     558             :          *
     559             :          * Note - This loop is a little tricky.  For each non-null attribute,
     560             :          * we have to first account for alignment padding before the attr,
     561             :          * then advance over the attr based on its length.  Nulls have no
     562             :          * storage and no alignment padding either.  We can use/set
     563             :          * attcacheoff until we reach either a null or a var-width attribute.
     564             :          */
     565    23800130 :         off = 0;
     566   157536520 :         for (i = 0;; i++)       /* loop exit is at "break" */
     567   133736390 :         {
     568   157536520 :             Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
     569             : 
     570   157536520 :             if (HeapTupleHasNulls(tuple) && att_isnull(i, bp))
     571             :             {
     572    21357308 :                 usecache = false;
     573    21357308 :                 continue;       /* this cannot be the target att */
     574             :             }
     575             : 
     576             :             /* If we know the next offset, we can skip the rest */
     577   136179212 :             if (usecache && att->attcacheoff >= 0)
     578    87764756 :                 off = att->attcacheoff;
     579    48414456 :             else if (att->attlen == -1)
     580             :             {
     581             :                 /*
     582             :                  * We can only cache the offset for a varlena attribute if the
     583             :                  * offset is already suitably aligned, so that there would be
     584             :                  * no pad bytes in any case: then the offset will be valid for
     585             :                  * either an aligned or unaligned value.
     586             :                  */
     587    15071480 :                 if (usecache &&
     588     1384958 :                     off == att_align_nominal(off, att->attalign))
     589       27424 :                     att->attcacheoff = off;
     590             :                 else
     591             :                 {
     592    13659098 :                     off = att_align_pointer(off, att->attalign, -1,
     593             :                                             tp + off);
     594    13659098 :                     usecache = false;
     595             :                 }
     596             :             }
     597             :             else
     598             :             {
     599             :                 /* not varlena, so safe to use att_align_nominal */
     600    34727934 :                 off = att_align_nominal(off, att->attalign);
     601             : 
     602    34727934 :                 if (usecache)
     603      279098 :                     att->attcacheoff = off;
     604             :             }
     605             : 
     606   136179212 :             if (i == attnum)
     607    23800130 :                 break;
     608             : 
     609   112379082 :             off = att_addlength_pointer(off, att->attlen, tp + off);
     610             : 
     611   112379082 :             if (usecache && att->attlen <= 0)
     612    21711328 :                 usecache = false;
     613             :         }
     614             :     }
     615             : 
     616    23894188 :     return fetchatt(TupleDescAttr(tupleDesc, attnum), tp + off);
     617             : }
     618             : 
     619             : /* ----------------
     620             :  *      heap_getsysattr
     621             :  *
     622             :  *      Fetch the value of a system attribute for a tuple.
     623             :  *
     624             :  * This is a support routine for the heap_getattr macro.  The macro
     625             :  * has already determined that the attnum refers to a system attribute.
     626             :  * ----------------
     627             :  */
     628             : Datum
     629     3861248 : heap_getsysattr(HeapTuple tup, int attnum, TupleDesc tupleDesc, bool *isnull)
     630             : {
     631             :     Datum       result;
     632             : 
     633             :     Assert(tup);
     634             : 
     635             :     /* Currently, no sys attribute ever reads as NULL. */
     636     3861248 :     *isnull = false;
     637             : 
     638     3861248 :     switch (attnum)
     639             :     {
     640             :         case SelfItemPointerAttributeNumber:
     641             :             /* pass-by-reference datatype */
     642     1318952 :             result = PointerGetDatum(&(tup->t_self));
     643     1318952 :             break;
     644             :         case MinTransactionIdAttributeNumber:
     645          42 :             result = TransactionIdGetDatum(HeapTupleHeaderGetRawXmin(tup->t_data));
     646          42 :             break;
     647             :         case MaxTransactionIdAttributeNumber:
     648           0 :             result = TransactionIdGetDatum(HeapTupleHeaderGetRawXmax(tup->t_data));
     649           0 :             break;
     650             :         case MinCommandIdAttributeNumber:
     651             :         case MaxCommandIdAttributeNumber:
     652             : 
     653             :             /*
     654             :              * cmin and cmax are now both aliases for the same field, which
     655             :              * can in fact also be a combo command id.  XXX perhaps we should
     656             :              * return the "real" cmin or cmax if possible, that is if we are
     657             :              * inside the originating transaction?
     658             :              */
     659         124 :             result = CommandIdGetDatum(HeapTupleHeaderGetRawCommandId(tup->t_data));
     660         124 :             break;
     661             :         case TableOidAttributeNumber:
     662     2542130 :             result = ObjectIdGetDatum(tup->t_tableOid);
     663     2542130 :             break;
     664             :         default:
     665           0 :             elog(ERROR, "invalid attnum: %d", attnum);
     666             :             result = 0;         /* keep compiler quiet */
     667             :             break;
     668             :     }
     669     3861248 :     return result;
     670             : }
     671             : 
     672             : /* ----------------
     673             :  *      heap_copytuple
     674             :  *
     675             :  *      returns a copy of an entire tuple
     676             :  *
     677             :  * The HeapTuple struct, tuple header, and tuple data are all allocated
     678             :  * as a single palloc() block.
     679             :  * ----------------
     680             :  */
     681             : HeapTuple
     682    16513004 : heap_copytuple(HeapTuple tuple)
     683             : {
     684             :     HeapTuple   newTuple;
     685             : 
     686    16513004 :     if (!HeapTupleIsValid(tuple) || tuple->t_data == NULL)
     687           0 :         return NULL;
     688             : 
     689    16513004 :     newTuple = (HeapTuple) palloc(HEAPTUPLESIZE + tuple->t_len);
     690    16513004 :     newTuple->t_len = tuple->t_len;
     691    16513004 :     newTuple->t_self = tuple->t_self;
     692    16513004 :     newTuple->t_tableOid = tuple->t_tableOid;
     693    16513004 :     newTuple->t_data = (HeapTupleHeader) ((char *) newTuple + HEAPTUPLESIZE);
     694    16513004 :     memcpy((char *) newTuple->t_data, (char *) tuple->t_data, tuple->t_len);
     695    16513004 :     return newTuple;
     696             : }
     697             : 
     698             : /* ----------------
     699             :  *      heap_copytuple_with_tuple
     700             :  *
     701             :  *      copy a tuple into a caller-supplied HeapTuple management struct
     702             :  *
     703             :  * Note that after calling this function, the "dest" HeapTuple will not be
     704             :  * allocated as a single palloc() block (unlike with heap_copytuple()).
     705             :  * ----------------
     706             :  */
     707             : void
     708           0 : heap_copytuple_with_tuple(HeapTuple src, HeapTuple dest)
     709             : {
     710           0 :     if (!HeapTupleIsValid(src) || src->t_data == NULL)
     711             :     {
     712           0 :         dest->t_data = NULL;
     713           0 :         return;
     714             :     }
     715             : 
     716           0 :     dest->t_len = src->t_len;
     717           0 :     dest->t_self = src->t_self;
     718           0 :     dest->t_tableOid = src->t_tableOid;
     719           0 :     dest->t_data = (HeapTupleHeader) palloc(src->t_len);
     720           0 :     memcpy((char *) dest->t_data, (char *) src->t_data, src->t_len);
     721             : }
     722             : 
     723             : /*
     724             :  * Expand a tuple which has less attributes than required. For each attribute
     725             :  * not present in the sourceTuple, if there is a missing value that will be
     726             :  * used. Otherwise the attribute will be set to NULL.
     727             :  *
     728             :  * The source tuple must have less attributes than the required number.
     729             :  *
     730             :  * Only one of targetHeapTuple and targetMinimalTuple may be supplied. The
     731             :  * other argument must be NULL.
     732             :  */
     733             : static void
     734          40 : expand_tuple(HeapTuple *targetHeapTuple,
     735             :              MinimalTuple *targetMinimalTuple,
     736             :              HeapTuple sourceTuple,
     737             :              TupleDesc tupleDesc)
     738             : {
     739          40 :     AttrMissing *attrmiss = NULL;
     740             :     int         attnum;
     741          40 :     int         firstmissingnum = 0;
     742          40 :     bool        hasNulls = HeapTupleHasNulls(sourceTuple);
     743             :     HeapTupleHeader targetTHeader;
     744          40 :     HeapTupleHeader sourceTHeader = sourceTuple->t_data;
     745          40 :     int         sourceNatts = HeapTupleHeaderGetNatts(sourceTHeader);
     746          40 :     int         natts = tupleDesc->natts;
     747             :     int         sourceNullLen;
     748             :     int         targetNullLen;
     749          40 :     Size        sourceDataLen = sourceTuple->t_len - sourceTHeader->t_hoff;
     750             :     Size        targetDataLen;
     751             :     Size        len;
     752             :     int         hoff;
     753          40 :     bits8      *nullBits = NULL;
     754          40 :     int         bitMask = 0;
     755             :     char       *targetData;
     756             :     uint16     *infoMask;
     757             : 
     758             :     Assert((targetHeapTuple && !targetMinimalTuple)
     759             :            || (!targetHeapTuple && targetMinimalTuple));
     760             : 
     761             :     Assert(sourceNatts < natts);
     762             : 
     763          40 :     sourceNullLen = (hasNulls ? BITMAPLEN(sourceNatts) : 0);
     764             : 
     765          40 :     targetDataLen = sourceDataLen;
     766             : 
     767         104 :     if (tupleDesc->constr &&
     768          40 :         tupleDesc->constr->missing)
     769             :     {
     770             :         /*
     771             :          * If there are missing values we want to put them into the tuple.
     772             :          * Before that we have to compute the extra length for the values
     773             :          * array and the variable length data.
     774             :          */
     775          24 :         attrmiss = tupleDesc->constr->missing;
     776             : 
     777             :         /*
     778             :          * Find the first item in attrmiss for which we don't have a value in
     779             :          * the source. We can ignore all the missing entries before that.
     780             :          */
     781          56 :         for (firstmissingnum = sourceNatts;
     782             :              firstmissingnum < natts;
     783           8 :              firstmissingnum++)
     784             :         {
     785          32 :             if (attrmiss[firstmissingnum].am_present)
     786          24 :                 break;
     787             :             else
     788           8 :                 hasNulls = true;
     789             :         }
     790             : 
     791             :         /*
     792             :          * Now walk the missing attributes. If there is a missing value
     793             :          * make space for it. Otherwise, it's going to be NULL.
     794             :          */
     795          88 :         for (attnum = firstmissingnum;
     796             :              attnum < natts;
     797          40 :              attnum++)
     798             :         {
     799          40 :             if (attrmiss[attnum].am_present)
     800             :             {
     801          32 :                 Form_pg_attribute att = TupleDescAttr(tupleDesc, attnum);
     802             : 
     803          32 :                 targetDataLen = att_align_datum(targetDataLen,
     804             :                                                 att->attalign,
     805             :                                                 att->attlen,
     806             :                                                 attrmiss[attnum].am_value);
     807             : 
     808          32 :                 targetDataLen = att_addlength_pointer(targetDataLen,
     809             :                                                       att->attlen,
     810             :                                                       attrmiss[attnum].am_value);
     811             :             }
     812             :             else
     813             :             {
     814             :                 /* no missing value, so it must be null */
     815           8 :                 hasNulls = true;
     816             :             }
     817             :         }
     818             :     }                           /* end if have missing values */
     819             :     else
     820             :     {
     821             :         /*
     822             :          * If there are no missing values at all then NULLS must be allowed,
     823             :          * since some of the attributes are known to be absent.
     824             :          */
     825          16 :         hasNulls = true;
     826             :     }
     827             : 
     828          40 :     len = 0;
     829             : 
     830          40 :     if (hasNulls)
     831             :     {
     832          36 :         targetNullLen = BITMAPLEN(natts);
     833          36 :         len += targetNullLen;
     834             :     }
     835             :     else
     836           4 :         targetNullLen = 0;
     837             : 
     838             :     /*
     839             :      * Allocate and zero the space needed.  Note that the tuple body and
     840             :      * HeapTupleData management structure are allocated in one chunk.
     841             :      */
     842          40 :     if (targetHeapTuple)
     843             :     {
     844          40 :         len += offsetof(HeapTupleHeaderData, t_bits);
     845          40 :         hoff = len = MAXALIGN(len); /* align user data safely */
     846          40 :         len += targetDataLen;
     847             : 
     848          40 :         *targetHeapTuple = (HeapTuple) palloc0(HEAPTUPLESIZE + len);
     849          40 :         (*targetHeapTuple)->t_data
     850          40 :             = targetTHeader
     851          40 :             = (HeapTupleHeader) ((char *) *targetHeapTuple + HEAPTUPLESIZE);
     852          40 :         (*targetHeapTuple)->t_len = len;
     853          40 :         (*targetHeapTuple)->t_tableOid = sourceTuple->t_tableOid;
     854          40 :         (*targetHeapTuple)->t_self = sourceTuple->t_self;
     855             : 
     856          40 :         targetTHeader->t_infomask = sourceTHeader->t_infomask;
     857          40 :         targetTHeader->t_hoff = hoff;
     858          40 :         HeapTupleHeaderSetNatts(targetTHeader, natts);
     859          40 :         HeapTupleHeaderSetDatumLength(targetTHeader, len);
     860          40 :         HeapTupleHeaderSetTypeId(targetTHeader, tupleDesc->tdtypeid);
     861          40 :         HeapTupleHeaderSetTypMod(targetTHeader, tupleDesc->tdtypmod);
     862             :         /* We also make sure that t_ctid is invalid unless explicitly set */
     863          40 :         ItemPointerSetInvalid(&(targetTHeader->t_ctid));
     864          40 :         if (targetNullLen > 0)
     865          36 :             nullBits = (bits8 *) ((char *) (*targetHeapTuple)->t_data
     866             :                                   + offsetof(HeapTupleHeaderData, t_bits));
     867          40 :         targetData = (char *) (*targetHeapTuple)->t_data + hoff;
     868          40 :         infoMask = &(targetTHeader->t_infomask);
     869             :     }
     870             :     else
     871             :     {
     872           0 :         len += SizeofMinimalTupleHeader;
     873           0 :         hoff = len = MAXALIGN(len); /* align user data safely */
     874           0 :         len += targetDataLen;
     875             : 
     876           0 :         *targetMinimalTuple = (MinimalTuple) palloc0(len);
     877           0 :         (*targetMinimalTuple)->t_len = len;
     878           0 :         (*targetMinimalTuple)->t_hoff = hoff + MINIMAL_TUPLE_OFFSET;
     879           0 :         (*targetMinimalTuple)->t_infomask = sourceTHeader->t_infomask;
     880             :         /* Same macro works for MinimalTuples */
     881           0 :         HeapTupleHeaderSetNatts(*targetMinimalTuple, natts);
     882           0 :         if (targetNullLen > 0)
     883           0 :             nullBits = (bits8 *) ((char *) *targetMinimalTuple
     884           0 :                                   + offsetof(MinimalTupleData, t_bits));
     885           0 :         targetData = (char *) *targetMinimalTuple + hoff;
     886           0 :         infoMask = &((*targetMinimalTuple)->t_infomask);
     887             :     }
     888             : 
     889          40 :     if (targetNullLen > 0)
     890             :     {
     891          36 :         if (sourceNullLen > 0)
     892             :         {
     893             :             /* if bitmap pre-existed copy in - all is set */
     894          16 :             memcpy(nullBits,
     895             :                    ((char *) sourceTHeader)
     896             :                    + offsetof(HeapTupleHeaderData, t_bits),
     897             :                    sourceNullLen);
     898          16 :             nullBits += sourceNullLen - 1;
     899             :         }
     900             :         else
     901             :         {
     902          20 :             sourceNullLen = BITMAPLEN(sourceNatts);
     903             :             /* Set NOT NULL for all existing attributes */
     904          20 :             memset(nullBits, 0xff, sourceNullLen);
     905             : 
     906          20 :             nullBits += sourceNullLen - 1;
     907             : 
     908          20 :             if (sourceNatts & 0x07)
     909             :             {
     910             :                 /* build the mask (inverted!) */
     911          20 :                 bitMask = 0xff << (sourceNatts & 0x07);
     912             :                 /* Voila */
     913          20 :                 *nullBits = ~bitMask;
     914             :             }
     915             :         }
     916             : 
     917          36 :         bitMask = (1 << ((sourceNatts - 1) & 0x07));
     918             :     }                           /* End if have null bitmap */
     919             : 
     920          40 :     memcpy(targetData,
     921          40 :            ((char *) sourceTuple->t_data) + sourceTHeader->t_hoff,
     922             :            sourceDataLen);
     923             : 
     924          40 :     targetData += sourceDataLen;
     925             : 
     926             :     /* Now fill in the missing values */
     927         112 :     for (attnum = sourceNatts; attnum < natts; attnum++)
     928             :     {
     929             : 
     930          72 :         Form_pg_attribute attr = TupleDescAttr(tupleDesc, attnum);
     931             : 
     932          72 :         if (attrmiss && attrmiss[attnum].am_present)
     933             :         {
     934          64 :             fill_val(attr,
     935          32 :                      nullBits ? &nullBits : NULL,
     936             :                      &bitMask,
     937             :                      &targetData,
     938             :                      infoMask,
     939          32 :                      attrmiss[attnum].am_value,
     940             :                      false);
     941             :         }
     942             :         else
     943             :         {
     944          40 :             fill_val(attr,
     945             :                      &nullBits,
     946             :                      &bitMask,
     947             :                      &targetData,
     948             :                      infoMask,
     949             :                      (Datum) 0,
     950             :                      true);
     951             :         }
     952             :     }                           /* end loop over missing attributes */
     953          40 : }
     954             : 
     955             : /*
     956             :  * Fill in the missing values for a minimal HeapTuple
     957             :  */
     958             : MinimalTuple
     959           0 : minimal_expand_tuple(HeapTuple sourceTuple, TupleDesc tupleDesc)
     960             : {
     961             :     MinimalTuple minimalTuple;
     962             : 
     963           0 :     expand_tuple(NULL, &minimalTuple, sourceTuple, tupleDesc);
     964           0 :     return minimalTuple;
     965             : }
     966             : 
     967             : /*
     968             :  * Fill in the missing values for an ordinary HeapTuple
     969             :  */
     970             : HeapTuple
     971          40 : heap_expand_tuple(HeapTuple sourceTuple, TupleDesc tupleDesc)
     972             : {
     973             :     HeapTuple   heapTuple;
     974             : 
     975          40 :     expand_tuple(&heapTuple, NULL, sourceTuple, tupleDesc);
     976          40 :     return heapTuple;
     977             : }
     978             : 
     979             : /* ----------------
     980             :  *      heap_copy_tuple_as_datum
     981             :  *
     982             :  *      copy a tuple as a composite-type Datum
     983             :  * ----------------
     984             :  */
     985             : Datum
     986       49642 : heap_copy_tuple_as_datum(HeapTuple tuple, TupleDesc tupleDesc)
     987             : {
     988             :     HeapTupleHeader td;
     989             : 
     990             :     /*
     991             :      * If the tuple contains any external TOAST pointers, we have to inline
     992             :      * those fields to meet the conventions for composite-type Datums.
     993             :      */
     994       49642 :     if (HeapTupleHasExternal(tuple))
     995           0 :         return toast_flatten_tuple_to_datum(tuple->t_data,
     996             :                                             tuple->t_len,
     997             :                                             tupleDesc);
     998             : 
     999             :     /*
    1000             :      * Fast path for easy case: just make a palloc'd copy and insert the
    1001             :      * correct composite-Datum header fields (since those may not be set if
    1002             :      * the given tuple came from disk, rather than from heap_form_tuple).
    1003             :      */
    1004       49642 :     td = (HeapTupleHeader) palloc(tuple->t_len);
    1005       49642 :     memcpy((char *) td, (char *) tuple->t_data, tuple->t_len);
    1006             : 
    1007       49642 :     HeapTupleHeaderSetDatumLength(td, tuple->t_len);
    1008       49642 :     HeapTupleHeaderSetTypeId(td, tupleDesc->tdtypeid);
    1009       49642 :     HeapTupleHeaderSetTypMod(td, tupleDesc->tdtypmod);
    1010             : 
    1011       49642 :     return PointerGetDatum(td);
    1012             : }
    1013             : 
    1014             : /*
    1015             :  * heap_form_tuple
    1016             :  *      construct a tuple from the given values[] and isnull[] arrays,
    1017             :  *      which are of the length indicated by tupleDescriptor->natts
    1018             :  *
    1019             :  * The result is allocated in the current memory context.
    1020             :  */
    1021             : HeapTuple
    1022    24962182 : heap_form_tuple(TupleDesc tupleDescriptor,
    1023             :                 Datum *values,
    1024             :                 bool *isnull)
    1025             : {
    1026             :     HeapTuple   tuple;          /* return tuple */
    1027             :     HeapTupleHeader td;         /* tuple data */
    1028             :     Size        len,
    1029             :                 data_len;
    1030             :     int         hoff;
    1031    24962182 :     bool        hasnull = false;
    1032    24962182 :     int         numberOfAttributes = tupleDescriptor->natts;
    1033             :     int         i;
    1034             : 
    1035    24962182 :     if (numberOfAttributes > MaxTupleAttributeNumber)
    1036           0 :         ereport(ERROR,
    1037             :                 (errcode(ERRCODE_TOO_MANY_COLUMNS),
    1038             :                  errmsg("number of columns (%d) exceeds limit (%d)",
    1039             :                         numberOfAttributes, MaxTupleAttributeNumber)));
    1040             : 
    1041             :     /*
    1042             :      * Check for nulls
    1043             :      */
    1044   147121466 :     for (i = 0; i < numberOfAttributes; i++)
    1045             :     {
    1046   128521128 :         if (isnull[i])
    1047             :         {
    1048     6361844 :             hasnull = true;
    1049     6361844 :             break;
    1050             :         }
    1051             :     }
    1052             : 
    1053             :     /*
    1054             :      * Determine total space needed
    1055             :      */
    1056    24962182 :     len = offsetof(HeapTupleHeaderData, t_bits);
    1057             : 
    1058    24962182 :     if (hasnull)
    1059     6361844 :         len += BITMAPLEN(numberOfAttributes);
    1060             : 
    1061    24962182 :     hoff = len = MAXALIGN(len); /* align user data safely */
    1062             : 
    1063    24962182 :     data_len = heap_compute_data_size(tupleDescriptor, values, isnull);
    1064             : 
    1065    24962182 :     len += data_len;
    1066             : 
    1067             :     /*
    1068             :      * Allocate and zero the space needed.  Note that the tuple body and
    1069             :      * HeapTupleData management structure are allocated in one chunk.
    1070             :      */
    1071    24962182 :     tuple = (HeapTuple) palloc0(HEAPTUPLESIZE + len);
    1072    24962182 :     tuple->t_data = td = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE);
    1073             : 
    1074             :     /*
    1075             :      * And fill in the information.  Note we fill the Datum fields even though
    1076             :      * this tuple may never become a Datum.  This lets HeapTupleHeaderGetDatum
    1077             :      * identify the tuple type if needed.
    1078             :      */
    1079    24962182 :     tuple->t_len = len;
    1080    24962182 :     ItemPointerSetInvalid(&(tuple->t_self));
    1081    24962182 :     tuple->t_tableOid = InvalidOid;
    1082             : 
    1083    24962182 :     HeapTupleHeaderSetDatumLength(td, len);
    1084    24962182 :     HeapTupleHeaderSetTypeId(td, tupleDescriptor->tdtypeid);
    1085    24962182 :     HeapTupleHeaderSetTypMod(td, tupleDescriptor->tdtypmod);
    1086             :     /* We also make sure that t_ctid is invalid unless explicitly set */
    1087    24962182 :     ItemPointerSetInvalid(&(td->t_ctid));
    1088             : 
    1089    24962182 :     HeapTupleHeaderSetNatts(td, numberOfAttributes);
    1090    24962182 :     td->t_hoff = hoff;
    1091             : 
    1092    24962182 :     heap_fill_tuple(tupleDescriptor,
    1093             :                     values,
    1094             :                     isnull,
    1095             :                     (char *) td + hoff,
    1096             :                     data_len,
    1097             :                     &td->t_infomask,
    1098             :                     (hasnull ? td->t_bits : NULL));
    1099             : 
    1100    24962182 :     return tuple;
    1101             : }
    1102             : 
    1103             : /*
    1104             :  * heap_modify_tuple
    1105             :  *      form a new tuple from an old tuple and a set of replacement values.
    1106             :  *
    1107             :  * The replValues, replIsnull, and doReplace arrays must be of the length
    1108             :  * indicated by tupleDesc->natts.  The new tuple is constructed using the data
    1109             :  * from replValues/replIsnull at columns where doReplace is true, and using
    1110             :  * the data from the old tuple at columns where doReplace is false.
    1111             :  *
    1112             :  * The result is allocated in the current memory context.
    1113             :  */
    1114             : HeapTuple
    1115      121224 : heap_modify_tuple(HeapTuple tuple,
    1116             :                   TupleDesc tupleDesc,
    1117             :                   Datum *replValues,
    1118             :                   bool *replIsnull,
    1119             :                   bool *doReplace)
    1120             : {
    1121      121224 :     int         numberOfAttributes = tupleDesc->natts;
    1122             :     int         attoff;
    1123             :     Datum      *values;
    1124             :     bool       *isnull;
    1125             :     HeapTuple   newTuple;
    1126             : 
    1127             :     /*
    1128             :      * allocate and fill values and isnull arrays from either the tuple or the
    1129             :      * repl information, as appropriate.
    1130             :      *
    1131             :      * NOTE: it's debatable whether to use heap_deform_tuple() here or just
    1132             :      * heap_getattr() only the non-replaced columns.  The latter could win if
    1133             :      * there are many replaced columns and few non-replaced ones. However,
    1134             :      * heap_deform_tuple costs only O(N) while the heap_getattr way would cost
    1135             :      * O(N^2) if there are many non-replaced columns, so it seems better to
    1136             :      * err on the side of linear cost.
    1137             :      */
    1138      121224 :     values = (Datum *) palloc(numberOfAttributes * sizeof(Datum));
    1139      121224 :     isnull = (bool *) palloc(numberOfAttributes * sizeof(bool));
    1140             : 
    1141      121224 :     heap_deform_tuple(tuple, tupleDesc, values, isnull);
    1142             : 
    1143     3389868 :     for (attoff = 0; attoff < numberOfAttributes; attoff++)
    1144             :     {
    1145     3268644 :         if (doReplace[attoff])
    1146             :         {
    1147     1919502 :             values[attoff] = replValues[attoff];
    1148     1919502 :             isnull[attoff] = replIsnull[attoff];
    1149             :         }
    1150             :     }
    1151             : 
    1152             :     /*
    1153             :      * create a new tuple from the values and isnull arrays
    1154             :      */
    1155      121224 :     newTuple = heap_form_tuple(tupleDesc, values, isnull);
    1156             : 
    1157      121224 :     pfree(values);
    1158      121224 :     pfree(isnull);
    1159             : 
    1160             :     /*
    1161             :      * copy the identification info of the old tuple: t_ctid, t_self
    1162             :      */
    1163      121224 :     newTuple->t_data->t_ctid = tuple->t_data->t_ctid;
    1164      121224 :     newTuple->t_self = tuple->t_self;
    1165      121224 :     newTuple->t_tableOid = tuple->t_tableOid;
    1166             : 
    1167      121224 :     return newTuple;
    1168             : }
    1169             : 
    1170             : /*
    1171             :  * heap_modify_tuple_by_cols
    1172             :  *      form a new tuple from an old tuple and a set of replacement values.
    1173             :  *
    1174             :  * This is like heap_modify_tuple, except that instead of specifying which
    1175             :  * column(s) to replace by a boolean map, an array of target column numbers
    1176             :  * is used.  This is often more convenient when a fixed number of columns
    1177             :  * are to be replaced.  The replCols, replValues, and replIsnull arrays must
    1178             :  * be of length nCols.  Target column numbers are indexed from 1.
    1179             :  *
    1180             :  * The result is allocated in the current memory context.
    1181             :  */
    1182             : HeapTuple
    1183          24 : heap_modify_tuple_by_cols(HeapTuple tuple,
    1184             :                           TupleDesc tupleDesc,
    1185             :                           int nCols,
    1186             :                           int *replCols,
    1187             :                           Datum *replValues,
    1188             :                           bool *replIsnull)
    1189             : {
    1190          24 :     int         numberOfAttributes = tupleDesc->natts;
    1191             :     Datum      *values;
    1192             :     bool       *isnull;
    1193             :     HeapTuple   newTuple;
    1194             :     int         i;
    1195             : 
    1196             :     /*
    1197             :      * allocate and fill values and isnull arrays from the tuple, then replace
    1198             :      * selected columns from the input arrays.
    1199             :      */
    1200          24 :     values = (Datum *) palloc(numberOfAttributes * sizeof(Datum));
    1201          24 :     isnull = (bool *) palloc(numberOfAttributes * sizeof(bool));
    1202             : 
    1203          24 :     heap_deform_tuple(tuple, tupleDesc, values, isnull);
    1204             : 
    1205          48 :     for (i = 0; i < nCols; i++)
    1206             :     {
    1207          24 :         int         attnum = replCols[i];
    1208             : 
    1209          24 :         if (attnum <= 0 || attnum > numberOfAttributes)
    1210           0 :             elog(ERROR, "invalid column number %d", attnum);
    1211          24 :         values[attnum - 1] = replValues[i];
    1212          24 :         isnull[attnum - 1] = replIsnull[i];
    1213             :     }
    1214             : 
    1215             :     /*
    1216             :      * create a new tuple from the values and isnull arrays
    1217             :      */
    1218          24 :     newTuple = heap_form_tuple(tupleDesc, values, isnull);
    1219             : 
    1220          24 :     pfree(values);
    1221          24 :     pfree(isnull);
    1222             : 
    1223             :     /*
    1224             :      * copy the identification info of the old tuple: t_ctid, t_self
    1225             :      */
    1226          24 :     newTuple->t_data->t_ctid = tuple->t_data->t_ctid;
    1227          24 :     newTuple->t_self = tuple->t_self;
    1228          24 :     newTuple->t_tableOid = tuple->t_tableOid;
    1229             : 
    1230          24 :     return newTuple;
    1231             : }
    1232             : 
    1233             : /*
    1234             :  * heap_deform_tuple
    1235             :  *      Given a tuple, extract data into values/isnull arrays; this is
    1236             :  *      the inverse of heap_form_tuple.
    1237             :  *
    1238             :  *      Storage for the values/isnull arrays is provided by the caller;
    1239             :  *      it should be sized according to tupleDesc->natts not
    1240             :  *      HeapTupleHeaderGetNatts(tuple->t_data).
    1241             :  *
    1242             :  *      Note that for pass-by-reference datatypes, the pointer placed
    1243             :  *      in the Datum will point into the given tuple.
    1244             :  *
    1245             :  *      When all or most of a tuple's fields need to be extracted,
    1246             :  *      this routine will be significantly quicker than a loop around
    1247             :  *      heap_getattr; the loop will become O(N^2) as soon as any
    1248             :  *      noncacheable attribute offsets are involved.
    1249             :  */
    1250             : void
    1251     3913094 : heap_deform_tuple(HeapTuple tuple, TupleDesc tupleDesc,
    1252             :                   Datum *values, bool *isnull)
    1253             : {
    1254     3913094 :     HeapTupleHeader tup = tuple->t_data;
    1255     3913094 :     bool        hasnulls = HeapTupleHasNulls(tuple);
    1256     3913094 :     int         tdesc_natts = tupleDesc->natts;
    1257             :     int         natts;          /* number of atts to extract */
    1258             :     int         attnum;
    1259             :     char       *tp;             /* ptr to tuple data */
    1260             :     uint32      off;            /* offset in tuple data */
    1261     3913094 :     bits8      *bp = tup->t_bits;    /* ptr to null bitmap in tuple */
    1262     3913094 :     bool        slow = false;   /* can we use/set attcacheoff? */
    1263             : 
    1264     3913094 :     natts = HeapTupleHeaderGetNatts(tup);
    1265             : 
    1266             :     /*
    1267             :      * In inheritance situations, it is possible that the given tuple actually
    1268             :      * has more fields than the caller is expecting.  Don't run off the end of
    1269             :      * the caller's arrays.
    1270             :      */
    1271     3913094 :     natts = Min(natts, tdesc_natts);
    1272             : 
    1273     3913094 :     tp = (char *) tup + tup->t_hoff;
    1274             : 
    1275     3913094 :     off = 0;
    1276             : 
    1277    18058350 :     for (attnum = 0; attnum < natts; attnum++)
    1278             :     {
    1279    14145256 :         Form_pg_attribute thisatt = TupleDescAttr(tupleDesc, attnum);
    1280             : 
    1281    14145256 :         if (hasnulls && att_isnull(attnum, bp))
    1282             :         {
    1283     1232238 :             values[attnum] = (Datum) 0;
    1284     1232238 :             isnull[attnum] = true;
    1285     1232238 :             slow = true;        /* can't use attcacheoff anymore */
    1286     1232238 :             continue;
    1287             :         }
    1288             : 
    1289    12913018 :         isnull[attnum] = false;
    1290             : 
    1291    12913018 :         if (!slow && thisatt->attcacheoff >= 0)
    1292    11817650 :             off = thisatt->attcacheoff;
    1293     1095368 :         else if (thisatt->attlen == -1)
    1294             :         {
    1295             :             /*
    1296             :              * We can only cache the offset for a varlena attribute if the
    1297             :              * offset is already suitably aligned, so that there would be no
    1298             :              * pad bytes in any case: then the offset will be valid for either
    1299             :              * an aligned or unaligned value.
    1300             :              */
    1301      604952 :             if (!slow &&
    1302       92846 :                 off == att_align_nominal(off, thisatt->attalign))
    1303        9532 :                 thisatt->attcacheoff = off;
    1304             :             else
    1305             :             {
    1306      502574 :                 off = att_align_pointer(off, thisatt->attalign, -1,
    1307             :                                         tp + off);
    1308      502574 :                 slow = true;
    1309             :             }
    1310             :         }
    1311             :         else
    1312             :         {
    1313             :             /* not varlena, so safe to use att_align_nominal */
    1314      583262 :             off = att_align_nominal(off, thisatt->attalign);
    1315             : 
    1316      583262 :             if (!slow)
    1317       50578 :                 thisatt->attcacheoff = off;
    1318             :         }
    1319             : 
    1320    12913018 :         values[attnum] = fetchatt(thisatt, tp + off);
    1321             : 
    1322    12913018 :         off = att_addlength_pointer(off, thisatt->attlen, tp + off);
    1323             : 
    1324    12913018 :         if (thisatt->attlen <= 0)
    1325     2073470 :             slow = true;        /* can't use attcacheoff anymore */
    1326             :     }
    1327             : 
    1328             :     /*
    1329             :      * If tuple doesn't have all the atts indicated by tupleDesc, read the
    1330             :      * rest as nulls or missing values as appropriate.
    1331             :      */
    1332     3916958 :     for (; attnum < tdesc_natts; attnum++)
    1333        3864 :         values[attnum] = getmissingattr(tupleDesc, attnum + 1, &isnull[attnum]);
    1334     3913094 : }
    1335             : 
    1336             : /*
    1337             :  * heap_freetuple
    1338             :  */
    1339             : void
    1340    23276512 : heap_freetuple(HeapTuple htup)
    1341             : {
    1342    23276512 :     pfree(htup);
    1343    23276512 : }
    1344             : 
    1345             : 
    1346             : /*
    1347             :  * heap_form_minimal_tuple
    1348             :  *      construct a MinimalTuple from the given values[] and isnull[] arrays,
    1349             :  *      which are of the length indicated by tupleDescriptor->natts
    1350             :  *
    1351             :  * This is exactly like heap_form_tuple() except that the result is a
    1352             :  * "minimal" tuple lacking a HeapTupleData header as well as room for system
    1353             :  * columns.
    1354             :  *
    1355             :  * The result is allocated in the current memory context.
    1356             :  */
    1357             : MinimalTuple
    1358    27515526 : heap_form_minimal_tuple(TupleDesc tupleDescriptor,
    1359             :                         Datum *values,
    1360             :                         bool *isnull)
    1361             : {
    1362             :     MinimalTuple tuple;         /* return tuple */
    1363             :     Size        len,
    1364             :                 data_len;
    1365             :     int         hoff;
    1366    27515526 :     bool        hasnull = false;
    1367    27515526 :     int         numberOfAttributes = tupleDescriptor->natts;
    1368             :     int         i;
    1369             : 
    1370    27515526 :     if (numberOfAttributes > MaxTupleAttributeNumber)
    1371           0 :         ereport(ERROR,
    1372             :                 (errcode(ERRCODE_TOO_MANY_COLUMNS),
    1373             :                  errmsg("number of columns (%d) exceeds limit (%d)",
    1374             :                         numberOfAttributes, MaxTupleAttributeNumber)));
    1375             : 
    1376             :     /*
    1377             :      * Check for nulls
    1378             :      */
    1379    70952738 :     for (i = 0; i < numberOfAttributes; i++)
    1380             :     {
    1381    43737012 :         if (isnull[i])
    1382             :         {
    1383      299800 :             hasnull = true;
    1384      299800 :             break;
    1385             :         }
    1386             :     }
    1387             : 
    1388             :     /*
    1389             :      * Determine total space needed
    1390             :      */
    1391    27515526 :     len = SizeofMinimalTupleHeader;
    1392             : 
    1393    27515526 :     if (hasnull)
    1394      299800 :         len += BITMAPLEN(numberOfAttributes);
    1395             : 
    1396    27515526 :     hoff = len = MAXALIGN(len); /* align user data safely */
    1397             : 
    1398    27515526 :     data_len = heap_compute_data_size(tupleDescriptor, values, isnull);
    1399             : 
    1400    27515526 :     len += data_len;
    1401             : 
    1402             :     /*
    1403             :      * Allocate and zero the space needed.
    1404             :      */
    1405    27515526 :     tuple = (MinimalTuple) palloc0(len);
    1406             : 
    1407             :     /*
    1408             :      * And fill in the information.
    1409             :      */
    1410    27515526 :     tuple->t_len = len;
    1411    27515526 :     HeapTupleHeaderSetNatts(tuple, numberOfAttributes);
    1412    27515526 :     tuple->t_hoff = hoff + MINIMAL_TUPLE_OFFSET;
    1413             : 
    1414    27515526 :     heap_fill_tuple(tupleDescriptor,
    1415             :                     values,
    1416             :                     isnull,
    1417             :                     (char *) tuple + hoff,
    1418             :                     data_len,
    1419             :                     &tuple->t_infomask,
    1420             :                     (hasnull ? tuple->t_bits : NULL));
    1421             : 
    1422    27515526 :     return tuple;
    1423             : }
    1424             : 
    1425             : /*
    1426             :  * heap_free_minimal_tuple
    1427             :  */
    1428             : void
    1429    24879162 : heap_free_minimal_tuple(MinimalTuple mtup)
    1430             : {
    1431    24879162 :     pfree(mtup);
    1432    24879162 : }
    1433             : 
    1434             : /*
    1435             :  * heap_copy_minimal_tuple
    1436             :  *      copy a MinimalTuple
    1437             :  *
    1438             :  * The result is allocated in the current memory context.
    1439             :  */
    1440             : MinimalTuple
    1441     2979190 : heap_copy_minimal_tuple(MinimalTuple mtup)
    1442             : {
    1443             :     MinimalTuple result;
    1444             : 
    1445     2979190 :     result = (MinimalTuple) palloc(mtup->t_len);
    1446     2979190 :     memcpy(result, mtup, mtup->t_len);
    1447     2979190 :     return result;
    1448             : }
    1449             : 
    1450             : /*
    1451             :  * heap_tuple_from_minimal_tuple
    1452             :  *      create a HeapTuple by copying from a MinimalTuple;
    1453             :  *      system columns are filled with zeroes
    1454             :  *
    1455             :  * The result is allocated in the current memory context.
    1456             :  * The HeapTuple struct, tuple header, and tuple data are all allocated
    1457             :  * as a single palloc() block.
    1458             :  */
    1459             : HeapTuple
    1460      208526 : heap_tuple_from_minimal_tuple(MinimalTuple mtup)
    1461             : {
    1462             :     HeapTuple   result;
    1463      208526 :     uint32      len = mtup->t_len + MINIMAL_TUPLE_OFFSET;
    1464             : 
    1465      208526 :     result = (HeapTuple) palloc(HEAPTUPLESIZE + len);
    1466      208526 :     result->t_len = len;
    1467      208526 :     ItemPointerSetInvalid(&(result->t_self));
    1468      208526 :     result->t_tableOid = InvalidOid;
    1469      208526 :     result->t_data = (HeapTupleHeader) ((char *) result + HEAPTUPLESIZE);
    1470      208526 :     memcpy((char *) result->t_data + MINIMAL_TUPLE_OFFSET, mtup, mtup->t_len);
    1471      208526 :     memset(result->t_data, 0, offsetof(HeapTupleHeaderData, t_infomask2));
    1472      208526 :     return result;
    1473             : }
    1474             : 
    1475             : /*
    1476             :  * minimal_tuple_from_heap_tuple
    1477             :  *      create a MinimalTuple by copying from a HeapTuple
    1478             :  *
    1479             :  * The result is allocated in the current memory context.
    1480             :  */
    1481             : MinimalTuple
    1482     1510740 : minimal_tuple_from_heap_tuple(HeapTuple htup)
    1483             : {
    1484             :     MinimalTuple result;
    1485             :     uint32      len;
    1486             : 
    1487             :     Assert(htup->t_len > MINIMAL_TUPLE_OFFSET);
    1488     1510740 :     len = htup->t_len - MINIMAL_TUPLE_OFFSET;
    1489     1510740 :     result = (MinimalTuple) palloc(len);
    1490     1510740 :     memcpy(result, (char *) htup->t_data + MINIMAL_TUPLE_OFFSET, len);
    1491     1510740 :     result->t_len = len;
    1492     1510740 :     return result;
    1493             : }
    1494             : 
    1495             : /*
    1496             :  * This mainly exists so JIT can inline the definition, but it's also
    1497             :  * sometimes useful in debugging sessions.
    1498             :  */
    1499             : size_t
    1500           0 : varsize_any(void *p)
    1501             : {
    1502           0 :     return VARSIZE_ANY(p);
    1503             : }

Generated by: LCOV version 1.13