LCOV - code coverage report
Current view: top level - src/backend/access/common - indextuple.c (source / functions) Hit Total Coverage
Test: PostgreSQL Lines: 113 127 89.0 %
Date: 2017-09-29 13:40:31 Functions: 4 4 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-2017, 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/heapam.h"
      20             : #include "access/itup.h"
      21             : #include "access/tuptoaster.h"
      22             : 
      23             : 
      24             : /* ----------------------------------------------------------------
      25             :  *                index_ tuple interface routines
      26             :  * ----------------------------------------------------------------
      27             :  */
      28             : 
      29             : /* ----------------
      30             :  *      index_form_tuple
      31             :  *
      32             :  *      This shouldn't leak any memory; otherwise, callers such as
      33             :  *      tuplesort_putindextuplevalues() will be very unhappy.
      34             :  * ----------------
      35             :  */
      36             : IndexTuple
      37     1047425 : index_form_tuple(TupleDesc tupleDescriptor,
      38             :                  Datum *values,
      39             :                  bool *isnull)
      40             : {
      41             :     char       *tp;             /* tuple pointer */
      42             :     IndexTuple  tuple;          /* return tuple */
      43             :     Size        size,
      44             :                 data_size,
      45             :                 hoff;
      46             :     int         i;
      47     1047425 :     unsigned short infomask = 0;
      48     1047425 :     bool        hasnull = false;
      49     1047425 :     uint16      tupmask = 0;
      50     1047425 :     int         numberOfAttributes = tupleDescriptor->natts;
      51             : 
      52             : #ifdef TOAST_INDEX_HACK
      53             :     Datum       untoasted_values[INDEX_MAX_KEYS];
      54             :     bool        untoasted_free[INDEX_MAX_KEYS];
      55             : #endif
      56             : 
      57     1047425 :     if (numberOfAttributes > INDEX_MAX_KEYS)
      58           0 :         ereport(ERROR,
      59             :                 (errcode(ERRCODE_TOO_MANY_COLUMNS),
      60             :                  errmsg("number of index columns (%d) exceeds limit (%d)",
      61             :                         numberOfAttributes, INDEX_MAX_KEYS)));
      62             : 
      63             : #ifdef TOAST_INDEX_HACK
      64     2486266 :     for (i = 0; i < numberOfAttributes; i++)
      65             :     {
      66     1438841 :         Form_pg_attribute att = TupleDescAttr(tupleDescriptor, i);
      67             : 
      68     1438841 :         untoasted_values[i] = values[i];
      69     1438841 :         untoasted_free[i] = false;
      70             : 
      71             :         /* Do nothing if value is NULL or not of varlena type */
      72     1438841 :         if (isnull[i] || att->attlen != -1)
      73     1338462 :             continue;
      74             : 
      75             :         /*
      76             :          * If value is stored EXTERNAL, must fetch it so we are not depending
      77             :          * on outside storage.  This should be improved someday.
      78             :          */
      79      100379 :         if (VARATT_IS_EXTERNAL(DatumGetPointer(values[i])))
      80             :         {
      81           0 :             untoasted_values[i] =
      82           0 :                 PointerGetDatum(heap_tuple_fetch_attr((struct varlena *)
      83             :                                                       DatumGetPointer(values[i])));
      84           0 :             untoasted_free[i] = true;
      85             :         }
      86             : 
      87             :         /*
      88             :          * If value is above size target, and is of a compressible datatype,
      89             :          * try to compress it in-line.
      90             :          */
      91      113558 :         if (!VARATT_IS_EXTENDED(DatumGetPointer(untoasted_values[i])) &&
      92       13279 :             VARSIZE(DatumGetPointer(untoasted_values[i])) > TOAST_INDEX_TARGET &&
      93         100 :             (att->attstorage == 'x' || att->attstorage == 'm'))
      94             :         {
      95         100 :             Datum       cvalue = toast_compress_datum(untoasted_values[i]);
      96             : 
      97         100 :             if (DatumGetPointer(cvalue) != NULL)
      98             :             {
      99             :                 /* successful compression */
     100           0 :                 if (untoasted_free[i])
     101           0 :                     pfree(DatumGetPointer(untoasted_values[i]));
     102           0 :                 untoasted_values[i] = cvalue;
     103           0 :                 untoasted_free[i] = true;
     104             :             }
     105             :         }
     106             :     }
     107             : #endif
     108             : 
     109     2484086 :     for (i = 0; i < numberOfAttributes; i++)
     110             :     {
     111     1438836 :         if (isnull[i])
     112             :         {
     113        2175 :             hasnull = true;
     114        2175 :             break;
     115             :         }
     116             :     }
     117             : 
     118     1047425 :     if (hasnull)
     119        2175 :         infomask |= INDEX_NULL_MASK;
     120             : 
     121     1047425 :     hoff = IndexInfoFindDataOffset(infomask);
     122             : #ifdef TOAST_INDEX_HACK
     123     1047425 :     data_size = heap_compute_data_size(tupleDescriptor,
     124             :                                        untoasted_values, isnull);
     125             : #else
     126             :     data_size = heap_compute_data_size(tupleDescriptor,
     127             :                                        values, isnull);
     128             : #endif
     129     1047425 :     size = hoff + data_size;
     130     1047425 :     size = MAXALIGN(size);      /* be conservative */
     131             : 
     132     1047425 :     tp = (char *) palloc0(size);
     133     1047425 :     tuple = (IndexTuple) tp;
     134             : 
     135     1047425 :     heap_fill_tuple(tupleDescriptor,
     136             : #ifdef TOAST_INDEX_HACK
     137             :                     untoasted_values,
     138             : #else
     139             :                     values,
     140             : #endif
     141             :                     isnull,
     142             :                     (char *) tp + hoff,
     143             :                     data_size,
     144             :                     &tupmask,
     145             :                     (hasnull ? (bits8 *) tp + sizeof(IndexTupleData) : NULL));
     146             : 
     147             : #ifdef TOAST_INDEX_HACK
     148     2486266 :     for (i = 0; i < numberOfAttributes; i++)
     149             :     {
     150     1438841 :         if (untoasted_free[i])
     151           0 :             pfree(DatumGetPointer(untoasted_values[i]));
     152             :     }
     153             : #endif
     154             : 
     155             :     /*
     156             :      * We do this because heap_fill_tuple wants to initialize a "tupmask"
     157             :      * which is used for HeapTuples, but we want an indextuple infomask. The
     158             :      * only relevant info is the "has variable attributes" field. We have
     159             :      * already set the hasnull bit above.
     160             :      */
     161     1047425 :     if (tupmask & HEAP_HASVARWIDTH)
     162      141208 :         infomask |= INDEX_VAR_MASK;
     163             : 
     164             :     /* Also assert we got rid of external attributes */
     165             : #ifdef TOAST_INDEX_HACK
     166     1047425 :     Assert((tupmask & HEAP_HASEXTERNAL) == 0);
     167             : #endif
     168             : 
     169             :     /*
     170             :      * Here we make sure that the size will fit in the field reserved for it
     171             :      * in t_info.
     172             :      */
     173     1047425 :     if ((size & INDEX_SIZE_MASK) != size)
     174           0 :         ereport(ERROR,
     175             :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     176             :                  errmsg("index row requires %zu bytes, maximum size is %zu",
     177             :                         size, (Size) INDEX_SIZE_MASK)));
     178             : 
     179     1047425 :     infomask |= size;
     180             : 
     181             :     /*
     182             :      * initialize metadata
     183             :      */
     184     1047425 :     tuple->t_info = infomask;
     185     1047425 :     return tuple;
     186             : }
     187             : 
     188             : /* ----------------
     189             :  *      nocache_index_getattr
     190             :  *
     191             :  *      This gets called from index_getattr() macro, and only in cases
     192             :  *      where we can't use cacheoffset and the value is not null.
     193             :  *
     194             :  *      This caches attribute offsets in the attribute descriptor.
     195             :  *
     196             :  *      An alternative way to speed things up would be to cache offsets
     197             :  *      with the tuple, but that seems more difficult unless you take
     198             :  *      the storage hit of actually putting those offsets into the
     199             :  *      tuple you send to disk.  Yuck.
     200             :  *
     201             :  *      This scheme will be slightly slower than that, but should
     202             :  *      perform well for queries which hit large #'s of tuples.  After
     203             :  *      you cache the offsets once, examining all the other tuples using
     204             :  *      the same attribute descriptor will go much quicker. -cim 5/4/91
     205             :  * ----------------
     206             :  */
     207             : Datum
     208      206099 : nocache_index_getattr(IndexTuple tup,
     209             :                       int attnum,
     210             :                       TupleDesc tupleDesc)
     211             : {
     212             :     char       *tp;             /* ptr to data part of tuple */
     213      206099 :     bits8      *bp = NULL;      /* ptr to null bitmap in tuple */
     214      206099 :     bool        slow = false;   /* do we have to walk attrs? */
     215             :     int         data_off;       /* tuple data offset */
     216             :     int         off;            /* current offset within data */
     217             : 
     218             :     /* ----------------
     219             :      *   Three cases:
     220             :      *
     221             :      *   1: No nulls and no variable-width attributes.
     222             :      *   2: Has a null or a var-width AFTER att.
     223             :      *   3: Has nulls or var-widths BEFORE att.
     224             :      * ----------------
     225             :      */
     226             : 
     227      206099 :     data_off = IndexInfoFindDataOffset(tup->t_info);
     228             : 
     229      206099 :     attnum--;
     230             : 
     231      206099 :     if (IndexTupleHasNulls(tup))
     232             :     {
     233             :         /*
     234             :          * there's a null somewhere in the tuple
     235             :          *
     236             :          * check to see if desired att is null
     237             :          */
     238             : 
     239             :         /* XXX "knows" t_bits are just after fixed tuple header! */
     240        2239 :         bp = (bits8 *) ((char *) tup + sizeof(IndexTupleData));
     241             : 
     242             :         /*
     243             :          * Now check to see if any preceding bits are null...
     244             :          */
     245             :         {
     246        2239 :             int         byte = attnum >> 3;
     247        2239 :             int         finalbit = attnum & 0x07;
     248             : 
     249             :             /* check for nulls "before" final bit of last byte */
     250        2239 :             if ((~bp[byte]) & ((1 << finalbit) - 1))
     251           1 :                 slow = true;
     252             :             else
     253             :             {
     254             :                 /* check for nulls in any "earlier" bytes */
     255             :                 int         i;
     256             : 
     257        2238 :                 for (i = 0; i < byte; i++)
     258             :                 {
     259           0 :                     if (bp[i] != 0xFF)
     260             :                     {
     261           0 :                         slow = true;
     262           0 :                         break;
     263             :                     }
     264             :                 }
     265             :             }
     266             :         }
     267             :     }
     268             : 
     269      206099 :     tp = (char *) tup + data_off;
     270             : 
     271      206099 :     if (!slow)
     272             :     {
     273             :         Form_pg_attribute att;
     274             : 
     275             :         /*
     276             :          * If we get here, there are no nulls up to and including the target
     277             :          * attribute.  If we have a cached offset, we can use it.
     278             :          */
     279      206098 :         att = TupleDescAttr(tupleDesc, attnum);
     280      206098 :         if (att->attcacheoff >= 0)
     281        2238 :             return fetchatt(att, tp + att->attcacheoff);
     282             : 
     283             :         /*
     284             :          * Otherwise, check for non-fixed-length attrs up to and including
     285             :          * target.  If there aren't any, it's safe to cheaply initialize the
     286             :          * cached offsets for these attrs.
     287             :          */
     288      203860 :         if (IndexTupleHasVarwidths(tup))
     289             :         {
     290             :             int         j;
     291             : 
     292      297692 :             for (j = 0; j <= attnum; j++)
     293             :             {
     294      297682 :                 if (TupleDescAttr(tupleDesc, j)->attlen <= 0)
     295             :                 {
     296      201590 :                     slow = true;
     297      201590 :                     break;
     298             :                 }
     299             :             }
     300             :         }
     301             :     }
     302             : 
     303      203861 :     if (!slow)
     304             :     {
     305        2270 :         int         natts = tupleDesc->natts;
     306        2270 :         int         j = 1;
     307             : 
     308             :         /*
     309             :          * If we get here, we have a tuple with no nulls or var-widths up to
     310             :          * and including the target attribute, so we can use the cached offset
     311             :          * ... only we don't have it yet, or we'd not have got here.  Since
     312             :          * it's cheap to compute offsets for fixed-width columns, we take the
     313             :          * opportunity to initialize the cached offsets for *all* the leading
     314             :          * fixed-width columns, in hope of avoiding future visits to this
     315             :          * routine.
     316             :          */
     317        2270 :         TupleDescAttr(tupleDesc, 0)->attcacheoff = 0;
     318             : 
     319             :         /* we might have set some offsets in the slow path previously */
     320        4540 :         while (j < natts && TupleDescAttr(tupleDesc, j)->attcacheoff > 0)
     321           0 :             j++;
     322             : 
     323        4540 :         off = TupleDescAttr(tupleDesc, j - 1)->attcacheoff +
     324        2270 :             TupleDescAttr(tupleDesc, j - 1)->attlen;
     325             : 
     326        5964 :         for (; j < natts; j++)
     327             :         {
     328        3697 :             Form_pg_attribute att = TupleDescAttr(tupleDesc, j);
     329             : 
     330        3697 :             if (att->attlen <= 0)
     331           3 :                 break;
     332             : 
     333        3694 :             off = att_align_nominal(off, att->attalign);
     334             : 
     335        3694 :             att->attcacheoff = off;
     336             : 
     337        3694 :             off += att->attlen;
     338             :         }
     339             : 
     340        2270 :         Assert(j > attnum);
     341             : 
     342        2270 :         off = TupleDescAttr(tupleDesc, attnum)->attcacheoff;
     343             :     }
     344             :     else
     345             :     {
     346      201591 :         bool        usecache = true;
     347             :         int         i;
     348             : 
     349             :         /*
     350             :          * Now we know that we have to walk the tuple CAREFULLY.  But we still
     351             :          * might be able to cache some offsets for next time.
     352             :          *
     353             :          * Note - This loop is a little tricky.  For each non-null attribute,
     354             :          * we have to first account for alignment padding before the attr,
     355             :          * then advance over the attr based on its length.  Nulls have no
     356             :          * storage and no alignment padding either.  We can use/set
     357             :          * attcacheoff until we reach either a null or a var-width attribute.
     358             :          */
     359      201591 :         off = 0;
     360      506886 :         for (i = 0;; i++)       /* loop exit is at "break" */
     361             :         {
     362      506886 :             Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
     363             : 
     364      506886 :             if (IndexTupleHasNulls(tup) && att_isnull(i, bp))
     365             :             {
     366           1 :                 usecache = false;
     367           1 :                 continue;       /* this cannot be the target att */
     368             :             }
     369             : 
     370             :             /* If we know the next offset, we can skip the rest */
     371      506885 :             if (usecache && att->attcacheoff >= 0)
     372      296082 :                 off = att->attcacheoff;
     373      210803 :             else if (att->attlen == -1)
     374             :             {
     375             :                 /*
     376             :                  * We can only cache the offset for a varlena attribute if the
     377             :                  * offset is already suitably aligned, so that there would be
     378             :                  * no pad bytes in any case: then the offset will be valid for
     379             :                  * either an aligned or unaligned value.
     380             :                  */
     381       65072 :                 if (usecache &&
     382        1279 :                     off == att_align_nominal(off, att->attalign))
     383          20 :                     att->attcacheoff = off;
     384             :                 else
     385             :                 {
     386       63773 :                     off = att_align_pointer(off, att->attalign, -1,
     387             :                                             tp + off);
     388       63773 :                     usecache = false;
     389             :                 }
     390             :             }
     391             :             else
     392             :             {
     393             :                 /* not varlena, so safe to use att_align_nominal */
     394      147010 :                 off = att_align_nominal(off, att->attalign);
     395             : 
     396      147010 :                 if (usecache)
     397         311 :                     att->attcacheoff = off;
     398             :             }
     399             : 
     400      506885 :             if (i == attnum)
     401      201591 :                 break;
     402             : 
     403      305294 :             off = att_addlength_pointer(off, att->attlen, tp + off);
     404             : 
     405      305294 :             if (usecache && att->attlen <= 0)
     406      200008 :                 usecache = false;
     407      305295 :         }
     408             :     }
     409             : 
     410      203861 :     return fetchatt(TupleDescAttr(tupleDesc, attnum), tp + off);
     411             : }
     412             : 
     413             : /*
     414             :  * Convert an index tuple into Datum/isnull arrays.
     415             :  *
     416             :  * The caller must allocate sufficient storage for the output arrays.
     417             :  * (INDEX_MAX_KEYS entries should be enough.)
     418             :  */
     419             : void
     420          38 : index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor,
     421             :                    Datum *values, bool *isnull)
     422             : {
     423             :     int         i;
     424             : 
     425             :     /* Assert to protect callers who allocate fixed-size arrays */
     426          38 :     Assert(tupleDescriptor->natts <= INDEX_MAX_KEYS);
     427             : 
     428          81 :     for (i = 0; i < tupleDescriptor->natts; i++)
     429             :     {
     430          43 :         values[i] = index_getattr(tup, i + 1, tupleDescriptor, &isnull[i]);
     431             :     }
     432          38 : }
     433             : 
     434             : /*
     435             :  * Create a palloc'd copy of an index tuple.
     436             :  */
     437             : IndexTuple
     438       19456 : CopyIndexTuple(IndexTuple source)
     439             : {
     440             :     IndexTuple  result;
     441             :     Size        size;
     442             : 
     443       19456 :     size = IndexTupleSize(source);
     444       19456 :     result = (IndexTuple) palloc(size);
     445       19456 :     memcpy(result, source, size);
     446       19456 :     return result;
     447             : }

Generated by: LCOV version 1.11