LCOV - code coverage report
Current view: top level - src/backend/access/common - indextuple.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 156 162 96.3 %
Date: 2019-11-15 22:06:47 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * indextuple.c
       4             :  *     This file contains index tuple accessor and mutator routines,
       5             :  *     as well as various tuple utilities.
       6             :  *
       7             :  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
       8             :  * Portions Copyright (c) 1994, Regents of the University of California
       9             :  *
      10             :  *
      11             :  * IDENTIFICATION
      12             :  *    src/backend/access/common/indextuple.c
      13             :  *
      14             :  *-------------------------------------------------------------------------
      15             :  */
      16             : 
      17             : #include "postgres.h"
      18             : 
      19             : #include "access/detoast.h"
      20             : #include "access/heaptoast.h"
      21             : #include "access/htup_details.h"
      22             : #include "access/itup.h"
      23             : #include "access/toast_internals.h"
      24             : 
      25             : /*
      26             :  * This enables de-toasting of index entries.  Needed until VACUUM is
      27             :  * smart enough to rebuild indexes from scratch.
      28             :  */
      29             : #define TOAST_INDEX_HACK
      30             : 
      31             : /* ----------------------------------------------------------------
      32             :  *                index_ tuple interface routines
      33             :  * ----------------------------------------------------------------
      34             :  */
      35             : 
      36             : /* ----------------
      37             :  *      index_form_tuple
      38             :  *
      39             :  *      This shouldn't leak any memory; otherwise, callers such as
      40             :  *      tuplesort_putindextuplevalues() will be very unhappy.
      41             :  *
      42             :  *      This shouldn't perform external table access provided caller
      43             :  *      does not pass values that are stored EXTERNAL.
      44             :  * ----------------
      45             :  */
      46             : IndexTuple
      47    27865406 : index_form_tuple(TupleDesc tupleDescriptor,
      48             :                  Datum *values,
      49             :                  bool *isnull)
      50             : {
      51             :     char       *tp;             /* tuple pointer */
      52             :     IndexTuple  tuple;          /* return tuple */
      53             :     Size        size,
      54             :                 data_size,
      55             :                 hoff;
      56             :     int         i;
      57    27865406 :     unsigned short infomask = 0;
      58    27865406 :     bool        hasnull = false;
      59    27865406 :     uint16      tupmask = 0;
      60    27865406 :     int         numberOfAttributes = tupleDescriptor->natts;
      61             : 
      62             : #ifdef TOAST_INDEX_HACK
      63             :     Datum       untoasted_values[INDEX_MAX_KEYS];
      64             :     bool        untoasted_free[INDEX_MAX_KEYS];
      65             : #endif
      66             : 
      67    27865406 :     if (numberOfAttributes > INDEX_MAX_KEYS)
      68           0 :         ereport(ERROR,
      69             :                 (errcode(ERRCODE_TOO_MANY_COLUMNS),
      70             :                  errmsg("number of index columns (%d) exceeds limit (%d)",
      71             :                         numberOfAttributes, INDEX_MAX_KEYS)));
      72             : 
      73             : #ifdef TOAST_INDEX_HACK
      74    83390696 :     for (i = 0; i < numberOfAttributes; i++)
      75             :     {
      76    55525290 :         Form_pg_attribute att = TupleDescAttr(tupleDescriptor, i);
      77             : 
      78    55525290 :         untoasted_values[i] = values[i];
      79    55525290 :         untoasted_free[i] = false;
      80             : 
      81             :         /* Do nothing if value is NULL or not of varlena type */
      82    55525290 :         if (isnull[i] || att->attlen != -1)
      83    53883962 :             continue;
      84             : 
      85             :         /*
      86             :          * If value is stored EXTERNAL, must fetch it so we are not depending
      87             :          * on outside storage.  This should be improved someday.
      88             :          */
      89     1641328 :         if (VARATT_IS_EXTERNAL(DatumGetPointer(values[i])))
      90             :         {
      91         240 :             untoasted_values[i] =
      92         240 :                 PointerGetDatum(detoast_external_attr((struct varlena *)
      93             :                                                       DatumGetPointer(values[i])));
      94         240 :             untoasted_free[i] = true;
      95             :         }
      96             : 
      97             :         /*
      98             :          * If value is above size target, and is of a compressible datatype,
      99             :          * try to compress it in-line.
     100             :          */
     101     2903040 :         if (!VARATT_IS_EXTENDED(DatumGetPointer(untoasted_values[i])) &&
     102     1263312 :             VARSIZE(DatumGetPointer(untoasted_values[i])) > TOAST_INDEX_TARGET &&
     103        1604 :             (att->attstorage == 'x' || att->attstorage == 'm'))
     104             :         {
     105        1596 :             Datum       cvalue = toast_compress_datum(untoasted_values[i]);
     106             : 
     107        1596 :             if (DatumGetPointer(cvalue) != NULL)
     108             :             {
     109             :                 /* successful compression */
     110         802 :                 if (untoasted_free[i])
     111         240 :                     pfree(DatumGetPointer(untoasted_values[i]));
     112         802 :                 untoasted_values[i] = cvalue;
     113         802 :                 untoasted_free[i] = true;
     114             :             }
     115             :         }
     116             :     }
     117             : #endif
     118             : 
     119    83375014 :     for (i = 0; i < numberOfAttributes; i++)
     120             :     {
     121    55523834 :         if (isnull[i])
     122             :         {
     123       14226 :             hasnull = true;
     124       14226 :             break;
     125             :         }
     126             :     }
     127             : 
     128    27865406 :     if (hasnull)
     129       14226 :         infomask |= INDEX_NULL_MASK;
     130             : 
     131    27865406 :     hoff = IndexInfoFindDataOffset(infomask);
     132             : #ifdef TOAST_INDEX_HACK
     133    27865406 :     data_size = heap_compute_data_size(tupleDescriptor,
     134             :                                        untoasted_values, isnull);
     135             : #else
     136             :     data_size = heap_compute_data_size(tupleDescriptor,
     137             :                                        values, isnull);
     138             : #endif
     139    27865406 :     size = hoff + data_size;
     140    27865406 :     size = MAXALIGN(size);      /* be conservative */
     141             : 
     142    27865406 :     tp = (char *) palloc0(size);
     143    27865406 :     tuple = (IndexTuple) tp;
     144             : 
     145    27865406 :     heap_fill_tuple(tupleDescriptor,
     146             : #ifdef TOAST_INDEX_HACK
     147             :                     untoasted_values,
     148             : #else
     149             :                     values,
     150             : #endif
     151             :                     isnull,
     152             :                     (char *) tp + hoff,
     153             :                     data_size,
     154             :                     &tupmask,
     155             :                     (hasnull ? (bits8 *) tp + sizeof(IndexTupleData) : NULL));
     156             : 
     157             : #ifdef TOAST_INDEX_HACK
     158    83390696 :     for (i = 0; i < numberOfAttributes; i++)
     159             :     {
     160    55525290 :         if (untoasted_free[i])
     161         802 :             pfree(DatumGetPointer(untoasted_values[i]));
     162             :     }
     163             : #endif
     164             : 
     165             :     /*
     166             :      * We do this because heap_fill_tuple wants to initialize a "tupmask"
     167             :      * which is used for HeapTuples, but we want an indextuple infomask. The
     168             :      * only relevant info is the "has variable attributes" field. We have
     169             :      * already set the hasnull bit above.
     170             :      */
     171    27865406 :     if (tupmask & HEAP_HASVARWIDTH)
     172     4151230 :         infomask |= INDEX_VAR_MASK;
     173             : 
     174             :     /* Also assert we got rid of external attributes */
     175             : #ifdef TOAST_INDEX_HACK
     176             :     Assert((tupmask & HEAP_HASEXTERNAL) == 0);
     177             : #endif
     178             : 
     179             :     /*
     180             :      * Here we make sure that the size will fit in the field reserved for it
     181             :      * in t_info.
     182             :      */
     183    27865406 :     if ((size & INDEX_SIZE_MASK) != size)
     184           0 :         ereport(ERROR,
     185             :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     186             :                  errmsg("index row requires %zu bytes, maximum size is %zu",
     187             :                         size, (Size) INDEX_SIZE_MASK)));
     188             : 
     189    27865406 :     infomask |= size;
     190             : 
     191             :     /*
     192             :      * initialize metadata
     193             :      */
     194    27865406 :     tuple->t_info = infomask;
     195    27865406 :     return tuple;
     196             : }
     197             : 
     198             : /* ----------------
     199             :  *      nocache_index_getattr
     200             :  *
     201             :  *      This gets called from index_getattr() macro, and only in cases
     202             :  *      where we can't use cacheoffset and the value is not null.
     203             :  *
     204             :  *      This caches attribute offsets in the attribute descriptor.
     205             :  *
     206             :  *      An alternative way to speed things up would be to cache offsets
     207             :  *      with the tuple, but that seems more difficult unless you take
     208             :  *      the storage hit of actually putting those offsets into the
     209             :  *      tuple you send to disk.  Yuck.
     210             :  *
     211             :  *      This scheme will be slightly slower than that, but should
     212             :  *      perform well for queries which hit large #'s of tuples.  After
     213             :  *      you cache the offsets once, examining all the other tuples using
     214             :  *      the same attribute descriptor will go much quicker. -cim 5/4/91
     215             :  * ----------------
     216             :  */
     217             : Datum
     218     6262926 : nocache_index_getattr(IndexTuple tup,
     219             :                       int attnum,
     220             :                       TupleDesc tupleDesc)
     221             : {
     222             :     char       *tp;             /* ptr to data part of tuple */
     223     6262926 :     bits8      *bp = NULL;      /* ptr to null bitmap in tuple */
     224     6262926 :     bool        slow = false;   /* do we have to walk attrs? */
     225             :     int         data_off;       /* tuple data offset */
     226             :     int         off;            /* current offset within data */
     227             : 
     228             :     /* ----------------
     229             :      *   Three cases:
     230             :      *
     231             :      *   1: No nulls and no variable-width attributes.
     232             :      *   2: Has a null or a var-width AFTER att.
     233             :      *   3: Has nulls or var-widths BEFORE att.
     234             :      * ----------------
     235             :      */
     236             : 
     237     6262926 :     data_off = IndexInfoFindDataOffset(tup->t_info);
     238             : 
     239     6262926 :     attnum--;
     240             : 
     241     6262926 :     if (IndexTupleHasNulls(tup))
     242             :     {
     243             :         /*
     244             :          * there's a null somewhere in the tuple
     245             :          *
     246             :          * check to see if desired att is null
     247             :          */
     248             : 
     249             :         /* XXX "knows" t_bits are just after fixed tuple header! */
     250       22898 :         bp = (bits8 *) ((char *) tup + sizeof(IndexTupleData));
     251             : 
     252             :         /*
     253             :          * Now check to see if any preceding bits are null...
     254             :          */
     255             :         {
     256       22898 :             int         byte = attnum >> 3;
     257       22898 :             int         finalbit = attnum & 0x07;
     258             : 
     259             :             /* check for nulls "before" final bit of last byte */
     260       22898 :             if ((~bp[byte]) & ((1 << finalbit) - 1))
     261           8 :                 slow = true;
     262             :             else
     263             :             {
     264             :                 /* check for nulls in any "earlier" bytes */
     265             :                 int         i;
     266             : 
     267       22890 :                 for (i = 0; i < byte; i++)
     268             :                 {
     269           0 :                     if (bp[i] != 0xFF)
     270             :                     {
     271           0 :                         slow = true;
     272           0 :                         break;
     273             :                     }
     274             :                 }
     275             :             }
     276             :         }
     277             :     }
     278             : 
     279     6262926 :     tp = (char *) tup + data_off;
     280             : 
     281     6262926 :     if (!slow)
     282             :     {
     283             :         Form_pg_attribute att;
     284             : 
     285             :         /*
     286             :          * If we get here, there are no nulls up to and including the target
     287             :          * attribute.  If we have a cached offset, we can use it.
     288             :          */
     289     6262918 :         att = TupleDescAttr(tupleDesc, attnum);
     290     6262918 :         if (att->attcacheoff >= 0)
     291       22890 :             return fetchatt(att, tp + att->attcacheoff);
     292             : 
     293             :         /*
     294             :          * Otherwise, check for non-fixed-length attrs up to and including
     295             :          * target.  If there aren't any, it's safe to cheaply initialize the
     296             :          * cached offsets for these attrs.
     297             :          */
     298     6240028 :         if (IndexTupleHasVarwidths(tup))
     299             :         {
     300             :             int         j;
     301             : 
     302     6581512 :             for (j = 0; j <= attnum; j++)
     303             :             {
     304     6579526 :                 if (TupleDescAttr(tupleDesc, j)->attlen <= 0)
     305             :                 {
     306     6191720 :                     slow = true;
     307     6191720 :                     break;
     308             :                 }
     309             :             }
     310             :         }
     311             :     }
     312             : 
     313     6240036 :     if (!slow)
     314             :     {
     315       48308 :         int         natts = tupleDesc->natts;
     316       48308 :         int         j = 1;
     317             : 
     318             :         /*
     319             :          * If we get here, we have a tuple with no nulls or var-widths up to
     320             :          * and including the target attribute, so we can use the cached offset
     321             :          * ... only we don't have it yet, or we'd not have got here.  Since
     322             :          * it's cheap to compute offsets for fixed-width columns, we take the
     323             :          * opportunity to initialize the cached offsets for *all* the leading
     324             :          * fixed-width columns, in hope of avoiding future visits to this
     325             :          * routine.
     326             :          */
     327       48308 :         TupleDescAttr(tupleDesc, 0)->attcacheoff = 0;
     328             : 
     329             :         /* we might have set some offsets in the slow path previously */
     330       96616 :         while (j < natts && TupleDescAttr(tupleDesc, j)->attcacheoff > 0)
     331           0 :             j++;
     332             : 
     333       96616 :         off = TupleDescAttr(tupleDesc, j - 1)->attcacheoff +
     334       48308 :             TupleDescAttr(tupleDesc, j - 1)->attlen;
     335             : 
     336      112630 :         for (; j < natts; j++)
     337             :         {
     338       66280 :             Form_pg_attribute att = TupleDescAttr(tupleDesc, j);
     339             : 
     340       66280 :             if (att->attlen <= 0)
     341        1958 :                 break;
     342             : 
     343       64322 :             off = att_align_nominal(off, att->attalign);
     344             : 
     345       64322 :             att->attcacheoff = off;
     346             : 
     347       64322 :             off += att->attlen;
     348             :         }
     349             : 
     350             :         Assert(j > attnum);
     351             : 
     352       48308 :         off = TupleDescAttr(tupleDesc, attnum)->attcacheoff;
     353             :     }
     354             :     else
     355             :     {
     356     6191728 :         bool        usecache = true;
     357             :         int         i;
     358             : 
     359             :         /*
     360             :          * Now we know that we have to walk the tuple CAREFULLY.  But we still
     361             :          * might be able to cache some offsets for next time.
     362             :          *
     363             :          * Note - This loop is a little tricky.  For each non-null attribute,
     364             :          * we have to first account for alignment padding before the attr,
     365             :          * then advance over the attr based on its length.  Nulls have no
     366             :          * storage and no alignment padding either.  We can use/set
     367             :          * attcacheoff until we reach either a null or a var-width attribute.
     368             :          */
     369     6191728 :         off = 0;
     370    13279386 :         for (i = 0;; i++)       /* loop exit is at "break" */
     371     7087658 :         {
     372    13279386 :             Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
     373             : 
     374    13279386 :             if (IndexTupleHasNulls(tup) && att_isnull(i, bp))
     375             :             {
     376           8 :                 usecache = false;
     377           8 :                 continue;       /* this cannot be the target att */
     378             :             }
     379             : 
     380             :             /* If we know the next offset, we can skip the rest */
     381    13279378 :             if (usecache && att->attcacheoff >= 0)
     382     6560198 :                 off = att->attcacheoff;
     383     6719180 :             else if (att->attlen == -1)
     384             :             {
     385             :                 /*
     386             :                  * We can only cache the offset for a varlena attribute if the
     387             :                  * offset is already suitably aligned, so that there would be
     388             :                  * no pad bytes in any case: then the offset will be valid for
     389             :                  * either an aligned or unaligned value.
     390             :                  */
     391     1360866 :                 if (usecache &&
     392        5192 :                     off == att_align_nominal(off, att->attalign))
     393         152 :                     att->attcacheoff = off;
     394             :                 else
     395             :                 {
     396     1355522 :                     off = att_align_pointer(off, att->attalign, -1,
     397             :                                             tp + off);
     398     1355522 :                     usecache = false;
     399             :                 }
     400             :             }
     401             :             else
     402             :             {
     403             :                 /* not varlena, so safe to use att_align_nominal */
     404     5363506 :                 off = att_align_nominal(off, att->attalign);
     405             : 
     406     5363506 :                 if (usecache)
     407       11162 :                     att->attcacheoff = off;
     408             :             }
     409             : 
     410    13279378 :             if (i == attnum)
     411     6191728 :                 break;
     412             : 
     413     7087650 :             off = att_addlength_pointer(off, att->attlen, tp + off);
     414             : 
     415     7087650 :             if (usecache && att->attlen <= 0)
     416     6175398 :                 usecache = false;
     417             :         }
     418             :     }
     419             : 
     420     6240036 :     return fetchatt(TupleDescAttr(tupleDesc, attnum), tp + off);
     421             : }
     422             : 
     423             : /*
     424             :  * Convert an index tuple into Datum/isnull arrays.
     425             :  *
     426             :  * The caller must allocate sufficient storage for the output arrays.
     427             :  * (INDEX_MAX_KEYS entries should be enough.)
     428             :  *
     429             :  * This is nearly the same as heap_deform_tuple(), but for IndexTuples.
     430             :  * One difference is that the tuple should never have any missing columns.
     431             :  */
     432             : void
     433     2112990 : index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor,
     434             :                    Datum *values, bool *isnull)
     435             : {
     436     2112990 :     int         hasnulls = IndexTupleHasNulls(tup);
     437     2112990 :     int         natts = tupleDescriptor->natts; /* number of atts to extract */
     438             :     int         attnum;
     439             :     char       *tp;             /* ptr to tuple data */
     440             :     int         off;            /* offset in tuple data */
     441             :     bits8      *bp;             /* ptr to null bitmap in tuple */
     442     2112990 :     bool        slow = false;   /* can we use/set attcacheoff? */
     443             : 
     444             :     /* Assert to protect callers who allocate fixed-size arrays */
     445             :     Assert(natts <= INDEX_MAX_KEYS);
     446             : 
     447             :     /* XXX "knows" t_bits are just after fixed tuple header! */
     448     2112990 :     bp = (bits8 *) ((char *) tup + sizeof(IndexTupleData));
     449             : 
     450     2112990 :     tp = (char *) tup + IndexInfoFindDataOffset(tup->t_info);
     451     2112990 :     off = 0;
     452             : 
     453     4903550 :     for (attnum = 0; attnum < natts; attnum++)
     454             :     {
     455     2790560 :         Form_pg_attribute thisatt = TupleDescAttr(tupleDescriptor, attnum);
     456             : 
     457     2790560 :         if (hasnulls && att_isnull(attnum, bp))
     458             :         {
     459          92 :             values[attnum] = (Datum) 0;
     460          92 :             isnull[attnum] = true;
     461          92 :             slow = true;        /* can't use attcacheoff anymore */
     462          92 :             continue;
     463             :         }
     464             : 
     465     2790468 :         isnull[attnum] = false;
     466             : 
     467     2790468 :         if (!slow && thisatt->attcacheoff >= 0)
     468     2782644 :             off = thisatt->attcacheoff;
     469        7824 :         else if (thisatt->attlen == -1)
     470             :         {
     471             :             /*
     472             :              * We can only cache the offset for a varlena attribute if the
     473             :              * offset is already suitably aligned, so that there would be no
     474             :              * pad bytes in any case: then the offset will be valid for either
     475             :              * an aligned or unaligned value.
     476             :              */
     477        2708 :             if (!slow &&
     478           4 :                 off == att_align_nominal(off, thisatt->attalign))
     479           4 :                 thisatt->attcacheoff = off;
     480             :             else
     481             :             {
     482        2700 :                 off = att_align_pointer(off, thisatt->attalign, -1,
     483             :                                         tp + off);
     484        2700 :                 slow = true;
     485             :             }
     486             :         }
     487             :         else
     488             :         {
     489             :             /* not varlena, so safe to use att_align_nominal */
     490        5120 :             off = att_align_nominal(off, thisatt->attalign);
     491             : 
     492        5120 :             if (!slow)
     493         576 :                 thisatt->attcacheoff = off;
     494             :         }
     495             : 
     496     2790468 :         values[attnum] = fetchatt(thisatt, tp + off);
     497             : 
     498     2790468 :         off = att_addlength_pointer(off, thisatt->attlen, tp + off);
     499             : 
     500     2790468 :         if (thisatt->attlen <= 0)
     501       57816 :             slow = true;        /* can't use attcacheoff anymore */
     502             :     }
     503     2112990 : }
     504             : 
     505             : /*
     506             :  * Create a palloc'd copy of an index tuple.
     507             :  */
     508             : IndexTuple
     509      205580 : CopyIndexTuple(IndexTuple source)
     510             : {
     511             :     IndexTuple  result;
     512             :     Size        size;
     513             : 
     514      205580 :     size = IndexTupleSize(source);
     515      205580 :     result = (IndexTuple) palloc(size);
     516      205580 :     memcpy(result, source, size);
     517      205580 :     return result;
     518             : }
     519             : 
     520             : /*
     521             :  * Create a palloc'd copy of an index tuple, leaving only the first
     522             :  * leavenatts attributes remaining.
     523             :  *
     524             :  * Truncation is guaranteed to result in an index tuple that is no
     525             :  * larger than the original.  It is safe to use the IndexTuple with
     526             :  * the original tuple descriptor, but caller must avoid actually
     527             :  * accessing truncated attributes from returned tuple!  In practice
     528             :  * this means that index_getattr() must be called with special care,
     529             :  * and that the truncated tuple should only ever be accessed by code
     530             :  * under caller's direct control.
     531             :  *
     532             :  * It's safe to call this function with a buffer lock held, since it
     533             :  * never performs external table access.  If it ever became possible
     534             :  * for index tuples to contain EXTERNAL TOAST values, then this would
     535             :  * have to be revisited.
     536             :  */
     537             : IndexTuple
     538       82454 : index_truncate_tuple(TupleDesc sourceDescriptor, IndexTuple source,
     539             :                      int leavenatts)
     540             : {
     541             :     TupleDesc   truncdesc;
     542             :     Datum       values[INDEX_MAX_KEYS];
     543             :     bool        isnull[INDEX_MAX_KEYS];
     544             :     IndexTuple  truncated;
     545             : 
     546             :     Assert(leavenatts <= sourceDescriptor->natts);
     547             : 
     548             :     /* Easy case: no truncation actually required */
     549       82454 :     if (leavenatts == sourceDescriptor->natts)
     550       33828 :         return CopyIndexTuple(source);
     551             : 
     552             :     /* Create temporary descriptor to scribble on */
     553       48626 :     truncdesc = palloc(TupleDescSize(sourceDescriptor));
     554       48626 :     TupleDescCopy(truncdesc, sourceDescriptor);
     555       48626 :     truncdesc->natts = leavenatts;
     556             : 
     557             :     /* Deform, form copy of tuple with fewer attributes */
     558       48626 :     index_deform_tuple(source, truncdesc, values, isnull);
     559       48626 :     truncated = index_form_tuple(truncdesc, values, isnull);
     560       48626 :     truncated->t_tid = source->t_tid;
     561             :     Assert(IndexTupleSize(truncated) <= IndexTupleSize(source));
     562             : 
     563             :     /*
     564             :      * Cannot leak memory here, TupleDescCopy() doesn't allocate any inner
     565             :      * structure, so, plain pfree() should clean all allocated memory
     566             :      */
     567       48626 :     pfree(truncdesc);
     568             : 
     569       48626 :     return truncated;
     570             : }

Generated by: LCOV version 1.13