LCOV - code coverage report
Current view: top level - src/backend/access/common - indextuple.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 156 163 95.7 %
Date: 2020-05-25 05:06:35 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-2020, 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    29977182 : 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    29977182 :     unsigned short infomask = 0;
      58    29977182 :     bool        hasnull = false;
      59    29977182 :     uint16      tupmask = 0;
      60    29977182 :     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    29977182 :     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    90319352 :     for (i = 0; i < numberOfAttributes; i++)
      75             :     {
      76    60342170 :         Form_pg_attribute att = TupleDescAttr(tupleDescriptor, i);
      77             : 
      78    60342170 :         untoasted_values[i] = values[i];
      79    60342170 :         untoasted_free[i] = false;
      80             : 
      81             :         /* Do nothing if value is NULL or not of varlena type */
      82    60342170 :         if (isnull[i] || att->attlen != -1)
      83    58411632 :             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     1930538 :         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     1930538 :         if (!VARATT_IS_EXTENDED(DatumGetPointer(untoasted_values[i])) &&
     102     1536600 :             VARSIZE(DatumGetPointer(untoasted_values[i])) > TOAST_INDEX_TARGET &&
     103      115624 :             (att->attstorage == TYPSTORAGE_EXTENDED ||
     104      112970 :              att->attstorage == TYPSTORAGE_MAIN))
     105             :         {
     106        2654 :             Datum       cvalue = toast_compress_datum(untoasted_values[i]);
     107             : 
     108        2654 :             if (DatumGetPointer(cvalue) != NULL)
     109             :             {
     110             :                 /* successful compression */
     111         340 :                 if (untoasted_free[i])
     112           0 :                     pfree(DatumGetPointer(untoasted_values[i]));
     113         340 :                 untoasted_values[i] = cvalue;
     114         340 :                 untoasted_free[i] = true;
     115             :             }
     116             :         }
     117             :     }
     118             : #endif
     119             : 
     120    90300380 :     for (i = 0; i < numberOfAttributes; i++)
     121             :     {
     122    60340714 :         if (isnull[i])
     123             :         {
     124       17516 :             hasnull = true;
     125       17516 :             break;
     126             :         }
     127             :     }
     128             : 
     129    29977182 :     if (hasnull)
     130       17516 :         infomask |= INDEX_NULL_MASK;
     131             : 
     132    29977182 :     hoff = IndexInfoFindDataOffset(infomask);
     133             : #ifdef TOAST_INDEX_HACK
     134    29977182 :     data_size = heap_compute_data_size(tupleDescriptor,
     135             :                                        untoasted_values, isnull);
     136             : #else
     137             :     data_size = heap_compute_data_size(tupleDescriptor,
     138             :                                        values, isnull);
     139             : #endif
     140    29977182 :     size = hoff + data_size;
     141    29977182 :     size = MAXALIGN(size);      /* be conservative */
     142             : 
     143    29977182 :     tp = (char *) palloc0(size);
     144    29977182 :     tuple = (IndexTuple) tp;
     145             : 
     146    29977182 :     heap_fill_tuple(tupleDescriptor,
     147             : #ifdef TOAST_INDEX_HACK
     148             :                     untoasted_values,
     149             : #else
     150             :                     values,
     151             : #endif
     152             :                     isnull,
     153             :                     (char *) tp + hoff,
     154             :                     data_size,
     155             :                     &tupmask,
     156             :                     (hasnull ? (bits8 *) tp + sizeof(IndexTupleData) : NULL));
     157             : 
     158             : #ifdef TOAST_INDEX_HACK
     159    90319352 :     for (i = 0; i < numberOfAttributes; i++)
     160             :     {
     161    60342170 :         if (untoasted_free[i])
     162         580 :             pfree(DatumGetPointer(untoasted_values[i]));
     163             :     }
     164             : #endif
     165             : 
     166             :     /*
     167             :      * We do this because heap_fill_tuple wants to initialize a "tupmask"
     168             :      * which is used for HeapTuples, but we want an indextuple infomask. The
     169             :      * only relevant info is the "has variable attributes" field. We have
     170             :      * already set the hasnull bit above.
     171             :      */
     172    29977182 :     if (tupmask & HEAP_HASVARWIDTH)
     173     4647328 :         infomask |= INDEX_VAR_MASK;
     174             : 
     175             :     /* Also assert we got rid of external attributes */
     176             : #ifdef TOAST_INDEX_HACK
     177             :     Assert((tupmask & HEAP_HASEXTERNAL) == 0);
     178             : #endif
     179             : 
     180             :     /*
     181             :      * Here we make sure that the size will fit in the field reserved for it
     182             :      * in t_info.
     183             :      */
     184    29977182 :     if ((size & INDEX_SIZE_MASK) != size)
     185           0 :         ereport(ERROR,
     186             :                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
     187             :                  errmsg("index row requires %zu bytes, maximum size is %zu",
     188             :                         size, (Size) INDEX_SIZE_MASK)));
     189             : 
     190    29977182 :     infomask |= size;
     191             : 
     192             :     /*
     193             :      * initialize metadata
     194             :      */
     195    29977182 :     tuple->t_info = infomask;
     196    29977182 :     return tuple;
     197             : }
     198             : 
     199             : /* ----------------
     200             :  *      nocache_index_getattr
     201             :  *
     202             :  *      This gets called from index_getattr() macro, and only in cases
     203             :  *      where we can't use cacheoffset and the value is not null.
     204             :  *
     205             :  *      This caches attribute offsets in the attribute descriptor.
     206             :  *
     207             :  *      An alternative way to speed things up would be to cache offsets
     208             :  *      with the tuple, but that seems more difficult unless you take
     209             :  *      the storage hit of actually putting those offsets into the
     210             :  *      tuple you send to disk.  Yuck.
     211             :  *
     212             :  *      This scheme will be slightly slower than that, but should
     213             :  *      perform well for queries which hit large #'s of tuples.  After
     214             :  *      you cache the offsets once, examining all the other tuples using
     215             :  *      the same attribute descriptor will go much quicker. -cim 5/4/91
     216             :  * ----------------
     217             :  */
     218             : Datum
     219     6769202 : nocache_index_getattr(IndexTuple tup,
     220             :                       int attnum,
     221             :                       TupleDesc tupleDesc)
     222             : {
     223             :     char       *tp;             /* ptr to data part of tuple */
     224     6769202 :     bits8      *bp = NULL;      /* ptr to null bitmap in tuple */
     225     6769202 :     bool        slow = false;   /* do we have to walk attrs? */
     226             :     int         data_off;       /* tuple data offset */
     227             :     int         off;            /* current offset within data */
     228             : 
     229             :     /* ----------------
     230             :      *   Three cases:
     231             :      *
     232             :      *   1: No nulls and no variable-width attributes.
     233             :      *   2: Has a null or a var-width AFTER att.
     234             :      *   3: Has nulls or var-widths BEFORE att.
     235             :      * ----------------
     236             :      */
     237             : 
     238     6769202 :     data_off = IndexInfoFindDataOffset(tup->t_info);
     239             : 
     240     6769202 :     attnum--;
     241             : 
     242     6769202 :     if (IndexTupleHasNulls(tup))
     243             :     {
     244             :         /*
     245             :          * there's a null somewhere in the tuple
     246             :          *
     247             :          * check to see if desired att is null
     248             :          */
     249             : 
     250             :         /* XXX "knows" t_bits are just after fixed tuple header! */
     251       28782 :         bp = (bits8 *) ((char *) tup + sizeof(IndexTupleData));
     252             : 
     253             :         /*
     254             :          * Now check to see if any preceding bits are null...
     255             :          */
     256             :         {
     257       28782 :             int         byte = attnum >> 3;
     258       28782 :             int         finalbit = attnum & 0x07;
     259             : 
     260             :             /* check for nulls "before" final bit of last byte */
     261       28782 :             if ((~bp[byte]) & ((1 << finalbit) - 1))
     262           8 :                 slow = true;
     263             :             else
     264             :             {
     265             :                 /* check for nulls in any "earlier" bytes */
     266             :                 int         i;
     267             : 
     268       28774 :                 for (i = 0; i < byte; i++)
     269             :                 {
     270           0 :                     if (bp[i] != 0xFF)
     271             :                     {
     272           0 :                         slow = true;
     273           0 :                         break;
     274             :                     }
     275             :                 }
     276             :             }
     277             :         }
     278             :     }
     279             : 
     280     6769202 :     tp = (char *) tup + data_off;
     281             : 
     282     6769202 :     if (!slow)
     283             :     {
     284             :         Form_pg_attribute att;
     285             : 
     286             :         /*
     287             :          * If we get here, there are no nulls up to and including the target
     288             :          * attribute.  If we have a cached offset, we can use it.
     289             :          */
     290     6769194 :         att = TupleDescAttr(tupleDesc, attnum);
     291     6769194 :         if (att->attcacheoff >= 0)
     292       28682 :             return fetchatt(att, tp + att->attcacheoff);
     293             : 
     294             :         /*
     295             :          * Otherwise, check for non-fixed-length attrs up to and including
     296             :          * target.  If there aren't any, it's safe to cheaply initialize the
     297             :          * cached offsets for these attrs.
     298             :          */
     299     6740512 :         if (IndexTupleHasVarwidths(tup))
     300             :         {
     301             :             int         j;
     302             : 
     303     7108860 :             for (j = 0; j <= attnum; j++)
     304             :             {
     305     7106656 :                 if (TupleDescAttr(tupleDesc, j)->attlen <= 0)
     306             :                 {
     307     6687074 :                     slow = true;
     308     6687074 :                     break;
     309             :                 }
     310             :             }
     311             :         }
     312             :     }
     313             : 
     314     6740520 :     if (!slow)
     315             :     {
     316       53438 :         int         natts = tupleDesc->natts;
     317       53438 :         int         j = 1;
     318             : 
     319             :         /*
     320             :          * If we get here, we have a tuple with no nulls or var-widths up to
     321             :          * and including the target attribute, so we can use the cached offset
     322             :          * ... only we don't have it yet, or we'd not have got here.  Since
     323             :          * it's cheap to compute offsets for fixed-width columns, we take the
     324             :          * opportunity to initialize the cached offsets for *all* the leading
     325             :          * fixed-width columns, in hope of avoiding future visits to this
     326             :          * routine.
     327             :          */
     328       53438 :         TupleDescAttr(tupleDesc, 0)->attcacheoff = 0;
     329             : 
     330             :         /* we might have set some offsets in the slow path previously */
     331       53438 :         while (j < natts && TupleDescAttr(tupleDesc, j)->attcacheoff > 0)
     332           0 :             j++;
     333             : 
     334      106876 :         off = TupleDescAttr(tupleDesc, j - 1)->attcacheoff +
     335       53438 :             TupleDescAttr(tupleDesc, j - 1)->attlen;
     336             : 
     337      125068 :         for (; j < natts; j++)
     338             :         {
     339       73806 :             Form_pg_attribute att = TupleDescAttr(tupleDesc, j);
     340             : 
     341       73806 :             if (att->attlen <= 0)
     342        2176 :                 break;
     343             : 
     344       71630 :             off = att_align_nominal(off, att->attalign);
     345             : 
     346       71630 :             att->attcacheoff = off;
     347             : 
     348       71630 :             off += att->attlen;
     349             :         }
     350             : 
     351             :         Assert(j > attnum);
     352             : 
     353       53438 :         off = TupleDescAttr(tupleDesc, attnum)->attcacheoff;
     354             :     }
     355             :     else
     356             :     {
     357     6687082 :         bool        usecache = true;
     358             :         int         i;
     359             : 
     360             :         /*
     361             :          * Now we know that we have to walk the tuple CAREFULLY.  But we still
     362             :          * might be able to cache some offsets for next time.
     363             :          *
     364             :          * Note - This loop is a little tricky.  For each non-null attribute,
     365             :          * we have to first account for alignment padding before the attr,
     366             :          * then advance over the attr based on its length.  Nulls have no
     367             :          * storage and no alignment padding either.  We can use/set
     368             :          * attcacheoff until we reach either a null or a var-width attribute.
     369             :          */
     370     6687082 :         off = 0;
     371     6687082 :         for (i = 0;; i++)       /* loop exit is at "break" */
     372     7660086 :         {
     373    14347168 :             Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
     374             : 
     375    14347168 :             if (IndexTupleHasNulls(tup) && att_isnull(i, bp))
     376             :             {
     377           8 :                 usecache = false;
     378           8 :                 continue;       /* this cannot be the target att */
     379             :             }
     380             : 
     381             :             /* If we know the next offset, we can skip the rest */
     382    14347160 :             if (usecache && att->attcacheoff >= 0)
     383     7086168 :                 off = att->attcacheoff;
     384     7260992 :             else if (att->attlen == -1)
     385             :             {
     386             :                 /*
     387             :                  * We can only cache the offset for a varlena attribute if the
     388             :                  * offset is already suitably aligned, so that there would be
     389             :                  * no pad bytes in any case: then the offset will be valid for
     390             :                  * either an aligned or unaligned value.
     391             :                  */
     392     1523190 :                 if (usecache &&
     393        5194 :                     off == att_align_nominal(off, att->attalign))
     394         154 :                     att->attcacheoff = off;
     395             :                 else
     396             :                 {
     397     1517842 :                     off = att_align_pointer(off, att->attalign, -1,
     398             :                                             tp + off);
     399     1517842 :                     usecache = false;
     400             :                 }
     401             :             }
     402             :             else
     403             :             {
     404             :                 /* not varlena, so safe to use att_align_nominal */
     405     5742996 :                 off = att_align_nominal(off, att->attalign);
     406             : 
     407     5742996 :                 if (usecache)
     408       11992 :                     att->attcacheoff = off;
     409             :             }
     410             : 
     411    14347160 :             if (i == attnum)
     412     6687082 :                 break;
     413             : 
     414     7660078 :             off = att_addlength_pointer(off, att->attlen, tp + off);
     415             : 
     416     7660078 :             if (usecache && att->attlen <= 0)
     417     6669920 :                 usecache = false;
     418             :         }
     419             :     }
     420             : 
     421     6740520 :     return fetchatt(TupleDescAttr(tupleDesc, attnum), tp + off);
     422             : }
     423             : 
     424             : /*
     425             :  * Convert an index tuple into Datum/isnull arrays.
     426             :  *
     427             :  * The caller must allocate sufficient storage for the output arrays.
     428             :  * (INDEX_MAX_KEYS entries should be enough.)
     429             :  *
     430             :  * This is nearly the same as heap_deform_tuple(), but for IndexTuples.
     431             :  * One difference is that the tuple should never have any missing columns.
     432             :  */
     433             : void
     434     2123916 : index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor,
     435             :                    Datum *values, bool *isnull)
     436             : {
     437     2123916 :     int         hasnulls = IndexTupleHasNulls(tup);
     438     2123916 :     int         natts = tupleDescriptor->natts; /* number of atts to extract */
     439             :     int         attnum;
     440             :     char       *tp;             /* ptr to tuple data */
     441             :     int         off;            /* offset in tuple data */
     442             :     bits8      *bp;             /* ptr to null bitmap in tuple */
     443     2123916 :     bool        slow = false;   /* can we use/set attcacheoff? */
     444             : 
     445             :     /* Assert to protect callers who allocate fixed-size arrays */
     446             :     Assert(natts <= INDEX_MAX_KEYS);
     447             : 
     448             :     /* XXX "knows" t_bits are just after fixed tuple header! */
     449     2123916 :     bp = (bits8 *) ((char *) tup + sizeof(IndexTupleData));
     450             : 
     451     2123916 :     tp = (char *) tup + IndexInfoFindDataOffset(tup->t_info);
     452     2123916 :     off = 0;
     453             : 
     454     4886628 :     for (attnum = 0; attnum < natts; attnum++)
     455             :     {
     456     2762712 :         Form_pg_attribute thisatt = TupleDescAttr(tupleDescriptor, attnum);
     457             : 
     458     2762712 :         if (hasnulls && att_isnull(attnum, bp))
     459             :         {
     460          92 :             values[attnum] = (Datum) 0;
     461          92 :             isnull[attnum] = true;
     462          92 :             slow = true;        /* can't use attcacheoff anymore */
     463          92 :             continue;
     464             :         }
     465             : 
     466     2762620 :         isnull[attnum] = false;
     467             : 
     468     2762620 :         if (!slow && thisatt->attcacheoff >= 0)
     469     2755550 :             off = thisatt->attcacheoff;
     470        7070 :         else if (thisatt->attlen == -1)
     471             :         {
     472             :             /*
     473             :              * We can only cache the offset for a varlena attribute if the
     474             :              * offset is already suitably aligned, so that there would be no
     475             :              * pad bytes in any case: then the offset will be valid for either
     476             :              * an aligned or unaligned value.
     477             :              */
     478        1838 :             if (!slow &&
     479           4 :                 off == att_align_nominal(off, thisatt->attalign))
     480           4 :                 thisatt->attcacheoff = off;
     481             :             else
     482             :             {
     483        1830 :                 off = att_align_pointer(off, thisatt->attalign, -1,
     484             :                                         tp + off);
     485        1830 :                 slow = true;
     486             :             }
     487             :         }
     488             :         else
     489             :         {
     490             :             /* not varlena, so safe to use att_align_nominal */
     491        5236 :             off = att_align_nominal(off, thisatt->attalign);
     492             : 
     493        5236 :             if (!slow)
     494         614 :                 thisatt->attcacheoff = off;
     495             :         }
     496             : 
     497     2762620 :         values[attnum] = fetchatt(thisatt, tp + off);
     498             : 
     499     2762620 :         off = att_addlength_pointer(off, thisatt->attlen, tp + off);
     500             : 
     501     2762620 :         if (thisatt->attlen <= 0)
     502       26888 :             slow = true;        /* can't use attcacheoff anymore */
     503             :     }
     504     2123916 : }
     505             : 
     506             : /*
     507             :  * Create a palloc'd copy of an index tuple.
     508             :  */
     509             : IndexTuple
     510     6243370 : CopyIndexTuple(IndexTuple source)
     511             : {
     512             :     IndexTuple  result;
     513             :     Size        size;
     514             : 
     515     6243370 :     size = IndexTupleSize(source);
     516     6243370 :     result = (IndexTuple) palloc(size);
     517     6243370 :     memcpy(result, source, size);
     518     6243370 :     return result;
     519             : }
     520             : 
     521             : /*
     522             :  * Create a palloc'd copy of an index tuple, leaving only the first
     523             :  * leavenatts attributes remaining.
     524             :  *
     525             :  * Truncation is guaranteed to result in an index tuple that is no
     526             :  * larger than the original.  It is safe to use the IndexTuple with
     527             :  * the original tuple descriptor, but caller must avoid actually
     528             :  * accessing truncated attributes from returned tuple!  In practice
     529             :  * this means that index_getattr() must be called with special care,
     530             :  * and that the truncated tuple should only ever be accessed by code
     531             :  * under caller's direct control.
     532             :  *
     533             :  * It's safe to call this function with a buffer lock held, since it
     534             :  * never performs external table access.  If it ever became possible
     535             :  * for index tuples to contain EXTERNAL TOAST values, then this would
     536             :  * have to be revisited.
     537             :  */
     538             : IndexTuple
     539       97946 : index_truncate_tuple(TupleDesc sourceDescriptor, IndexTuple source,
     540             :                      int leavenatts)
     541             : {
     542             :     TupleDesc   truncdesc;
     543             :     Datum       values[INDEX_MAX_KEYS];
     544             :     bool        isnull[INDEX_MAX_KEYS];
     545             :     IndexTuple  truncated;
     546             : 
     547             :     Assert(leavenatts <= sourceDescriptor->natts);
     548             : 
     549             :     /* Easy case: no truncation actually required */
     550       97946 :     if (leavenatts == sourceDescriptor->natts)
     551       43576 :         return CopyIndexTuple(source);
     552             : 
     553             :     /* Create temporary descriptor to scribble on */
     554       54370 :     truncdesc = palloc(TupleDescSize(sourceDescriptor));
     555       54370 :     TupleDescCopy(truncdesc, sourceDescriptor);
     556       54370 :     truncdesc->natts = leavenatts;
     557             : 
     558             :     /* Deform, form copy of tuple with fewer attributes */
     559       54370 :     index_deform_tuple(source, truncdesc, values, isnull);
     560       54370 :     truncated = index_form_tuple(truncdesc, values, isnull);
     561       54370 :     truncated->t_tid = source->t_tid;
     562             :     Assert(IndexTupleSize(truncated) <= IndexTupleSize(source));
     563             : 
     564             :     /*
     565             :      * Cannot leak memory here, TupleDescCopy() doesn't allocate any inner
     566             :      * structure, so, plain pfree() should clean all allocated memory
     567             :      */
     568       54370 :     pfree(truncdesc);
     569             : 
     570       54370 :     return truncated;
     571             : }

Generated by: LCOV version 1.13