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

Generated by: LCOV version 1.13