LCOV - code coverage report
Current view: top level - src/backend/access/brin - brin_tuple.c (source / functions) Hit Total Coverage
Test: PostgreSQL 14devel Lines: 209 218 95.9 %
Date: 2021-05-13 09:07:15 Functions: 10 10 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * brin_tuple.c
       3             :  *      Method implementations for tuples in BRIN indexes.
       4             :  *
       5             :  * Intended usage is that code outside this file only deals with
       6             :  * BrinMemTuples, and convert to and from the on-disk representation through
       7             :  * functions in this file.
       8             :  *
       9             :  * NOTES
      10             :  *
      11             :  * A BRIN tuple is similar to a heap tuple, with a few key differences.  The
      12             :  * first interesting difference is that the tuple header is much simpler, only
      13             :  * containing its total length and a small area for flags.  Also, the stored
      14             :  * data does not match the relation tuple descriptor exactly: for each
      15             :  * attribute in the descriptor, the index tuple carries an arbitrary number
      16             :  * of values, depending on the opclass.
      17             :  *
      18             :  * Also, for each column of the index relation there are two null bits: one
      19             :  * (hasnulls) stores whether any tuple within the page range has that column
      20             :  * set to null; the other one (allnulls) stores whether the column values are
      21             :  * all null.  If allnulls is true, then the tuple data area does not contain
      22             :  * values for that column at all; whereas it does if the hasnulls is set.
      23             :  * Note the size of the null bitmask may not be the same as that of the
      24             :  * datum array.
      25             :  *
      26             :  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
      27             :  * Portions Copyright (c) 1994, Regents of the University of California
      28             :  *
      29             :  * IDENTIFICATION
      30             :  *    src/backend/access/brin/brin_tuple.c
      31             :  */
      32             : #include "postgres.h"
      33             : 
      34             : #include "access/brin_tuple.h"
      35             : #include "access/detoast.h"
      36             : #include "access/heaptoast.h"
      37             : #include "access/htup_details.h"
      38             : #include "access/toast_internals.h"
      39             : #include "access/tupdesc.h"
      40             : #include "access/tupmacs.h"
      41             : #include "utils/datum.h"
      42             : #include "utils/memutils.h"
      43             : 
      44             : 
      45             : /*
      46             :  * This enables de-toasting of index entries.  Needed until VACUUM is
      47             :  * smart enough to rebuild indexes from scratch.
      48             :  */
      49             : #define TOAST_INDEX_HACK
      50             : 
      51             : 
      52             : static inline void brin_deconstruct_tuple(BrinDesc *brdesc,
      53             :                                           char *tp, bits8 *nullbits, bool nulls,
      54             :                                           Datum *values, bool *allnulls, bool *hasnulls);
      55             : 
      56             : 
      57             : /*
      58             :  * Return a tuple descriptor used for on-disk storage of BRIN tuples.
      59             :  */
      60             : static TupleDesc
      61      142764 : brtuple_disk_tupdesc(BrinDesc *brdesc)
      62             : {
      63             :     /* We cache these in the BrinDesc */
      64      142764 :     if (brdesc->bd_disktdesc == NULL)
      65             :     {
      66             :         int         i;
      67             :         int         j;
      68        2602 :         AttrNumber  attno = 1;
      69             :         TupleDesc   tupdesc;
      70             :         MemoryContext oldcxt;
      71             : 
      72             :         /* make sure it's in the bdesc's context */
      73        2602 :         oldcxt = MemoryContextSwitchTo(brdesc->bd_context);
      74             : 
      75        2602 :         tupdesc = CreateTemplateTupleDesc(brdesc->bd_totalstored);
      76             : 
      77       49476 :         for (i = 0; i < brdesc->bd_tupdesc->natts; i++)
      78             :         {
      79      129076 :             for (j = 0; j < brdesc->bd_info[i]->oi_nstored; j++)
      80       82202 :                 TupleDescInitEntry(tupdesc, attno++, NULL,
      81       82202 :                                    brdesc->bd_info[i]->oi_typcache[j]->type_id,
      82             :                                    -1, 0);
      83             :         }
      84             : 
      85        2602 :         MemoryContextSwitchTo(oldcxt);
      86             : 
      87        2602 :         brdesc->bd_disktdesc = tupdesc;
      88             :     }
      89             : 
      90      142764 :     return brdesc->bd_disktdesc;
      91             : }
      92             : 
      93             : /*
      94             :  * Generate a new on-disk tuple to be inserted in a BRIN index.
      95             :  *
      96             :  * See brin_form_placeholder_tuple if you touch this.
      97             :  */
      98             : BrinTuple *
      99        3498 : brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple,
     100             :                 Size *size)
     101             : {
     102             :     Datum      *values;
     103             :     bool       *nulls;
     104        3498 :     bool        anynulls = false;
     105             :     BrinTuple  *rettuple;
     106             :     int         keyno;
     107             :     int         idxattno;
     108        3498 :     uint16      phony_infomask = 0;
     109             :     bits8      *phony_nullbitmap;
     110             :     Size        len,
     111             :                 hoff,
     112             :                 data_len;
     113             :     int         i;
     114             : 
     115             : #ifdef TOAST_INDEX_HACK
     116             :     Datum      *untoasted_values;
     117        3498 :     int         nuntoasted = 0;
     118             : #endif
     119             : 
     120             :     Assert(brdesc->bd_totalstored > 0);
     121             : 
     122        3498 :     values = (Datum *) palloc(sizeof(Datum) * brdesc->bd_totalstored);
     123        3498 :     nulls = (bool *) palloc0(sizeof(bool) * brdesc->bd_totalstored);
     124             :     phony_nullbitmap = (bits8 *)
     125        3498 :         palloc(sizeof(bits8) * BITMAPLEN(brdesc->bd_totalstored));
     126             : 
     127             : #ifdef TOAST_INDEX_HACK
     128        3498 :     untoasted_values = (Datum *) palloc(sizeof(Datum) * brdesc->bd_totalstored);
     129             : #endif
     130             : 
     131             :     /*
     132             :      * Set up the values/nulls arrays for heap_fill_tuple
     133             :      */
     134        3498 :     idxattno = 0;
     135       74144 :     for (keyno = 0; keyno < brdesc->bd_tupdesc->natts; keyno++)
     136             :     {
     137             :         int         datumno;
     138             : 
     139             :         /*
     140             :          * "allnulls" is set when there's no nonnull value in any row in the
     141             :          * column; when this happens, there is no data to store.  Thus set the
     142             :          * nullable bits for all data elements of this column and we're done.
     143             :          */
     144       70646 :         if (tuple->bt_columns[keyno].bv_allnulls)
     145             :         {
     146         438 :             for (datumno = 0;
     147         988 :                  datumno < brdesc->bd_info[keyno]->oi_nstored;
     148         550 :                  datumno++)
     149         550 :                 nulls[idxattno++] = true;
     150         438 :             anynulls = true;
     151         438 :             continue;
     152             :         }
     153             : 
     154             :         /*
     155             :          * The "hasnulls" bit is set when there are some null values in the
     156             :          * data.  We still need to store a real value, but the presence of
     157             :          * this means we need a null bitmap.
     158             :          */
     159       70208 :         if (tuple->bt_columns[keyno].bv_hasnulls)
     160        6260 :             anynulls = true;
     161             : 
     162             :         /* If needed, serialize the values before forming the on-disk tuple. */
     163       70208 :         if (tuple->bt_columns[keyno].bv_serialize)
     164             :         {
     165       11444 :             tuple->bt_columns[keyno].bv_serialize(brdesc,
     166             :                                                   tuple->bt_columns[keyno].bv_mem_value,
     167             :                                                   tuple->bt_columns[keyno].bv_values);
     168             :         }
     169             : 
     170             :         /*
     171             :          * Now obtain the values of each stored datum.  Note that some values
     172             :          * might be toasted, and we cannot rely on the original heap values
     173             :          * sticking around forever, so we must detoast them.  Also try to
     174             :          * compress them.
     175             :          */
     176       70208 :         for (datumno = 0;
     177      184276 :              datumno < brdesc->bd_info[keyno]->oi_nstored;
     178      114068 :              datumno++)
     179             :         {
     180      114068 :             Datum       value = tuple->bt_columns[keyno].bv_values[datumno];
     181             : 
     182             : #ifdef TOAST_INDEX_HACK
     183             : 
     184             :             /* We must look at the stored type, not at the index descriptor. */
     185      114068 :             TypeCacheEntry *atttype = brdesc->bd_info[keyno]->oi_typcache[datumno];
     186             : 
     187             :             /* Do we need to free the value at the end? */
     188      114068 :             bool        free_value = false;
     189             : 
     190             :             /* For non-varlena types we don't need to do anything special */
     191      114068 :             if (atttype->typlen != -1)
     192             :             {
     193       58500 :                 values[idxattno++] = value;
     194       58500 :                 continue;
     195             :             }
     196             : 
     197             :             /*
     198             :              * Do nothing if value is not of varlena type. We don't need to
     199             :              * care about NULL values here, thanks to bv_allnulls above.
     200             :              *
     201             :              * If value is stored EXTERNAL, must fetch it so we are not
     202             :              * depending on outside storage.
     203             :              *
     204             :              * XXX Is this actually true? Could it be that the summary is NULL
     205             :              * even for range with non-NULL data? E.g. degenerate bloom filter
     206             :              * may be thrown away, etc.
     207             :              */
     208       55568 :             if (VARATT_IS_EXTERNAL(DatumGetPointer(value)))
     209             :             {
     210          16 :                 value = PointerGetDatum(detoast_external_attr((struct varlena *)
     211             :                                                               DatumGetPointer(value)));
     212          16 :                 free_value = true;
     213             :             }
     214             : 
     215             :             /*
     216             :              * If value is above size target, and is of a compressible
     217             :              * datatype, try to compress it in-line.
     218             :              */
     219       55568 :             if (!VARATT_IS_EXTENDED(DatumGetPointer(value)) &&
     220       31692 :                 VARSIZE(DatumGetPointer(value)) > TOAST_INDEX_TARGET &&
     221          40 :                 (atttype->typstorage == TYPSTORAGE_EXTENDED ||
     222           0 :                  atttype->typstorage == TYPSTORAGE_MAIN))
     223             :             {
     224             :                 Datum       cvalue;
     225             :                 char        compression;
     226          40 :                 Form_pg_attribute att = TupleDescAttr(brdesc->bd_tupdesc,
     227             :                                                       keyno);
     228             : 
     229             :                 /*
     230             :                  * If the BRIN summary and indexed attribute use the same data
     231             :                  * type and it has a valid compression method, we can use the
     232             :                  * same compression method. Otherwise we have to use the
     233             :                  * default method.
     234             :                  */
     235          40 :                 if (att->atttypid == atttype->type_id &&
     236          32 :                     CompressionMethodIsValid(att->attcompression))
     237          32 :                     compression = att->attcompression;
     238             :                 else
     239           8 :                     compression = GetDefaultToastCompression();
     240             : 
     241          40 :                 cvalue = toast_compress_datum(value, compression);
     242             : 
     243          40 :                 if (DatumGetPointer(cvalue) != NULL)
     244             :                 {
     245             :                     /* successful compression */
     246           8 :                     if (free_value)
     247           0 :                         pfree(DatumGetPointer(value));
     248             : 
     249           8 :                     value = cvalue;
     250           8 :                     free_value = true;
     251             :                 }
     252             :             }
     253             : 
     254             :             /*
     255             :              * If we untoasted / compressed the value, we need to free it
     256             :              * after forming the index tuple.
     257             :              */
     258       55568 :             if (free_value)
     259          24 :                 untoasted_values[nuntoasted++] = value;
     260             : 
     261             : #endif
     262             : 
     263       55568 :             values[idxattno++] = value;
     264             :         }
     265             :     }
     266             : 
     267             :     /* Assert we did not overrun temp arrays */
     268             :     Assert(idxattno <= brdesc->bd_totalstored);
     269             : 
     270             :     /* compute total space needed */
     271        3498 :     len = SizeOfBrinTuple;
     272        3498 :     if (anynulls)
     273             :     {
     274             :         /*
     275             :          * We need a double-length bitmap on an on-disk BRIN index tuple; the
     276             :          * first half stores the "allnulls" bits, the second stores
     277             :          * "hasnulls".
     278             :          */
     279         370 :         len += BITMAPLEN(brdesc->bd_tupdesc->natts * 2);
     280             :     }
     281             : 
     282        3498 :     len = hoff = MAXALIGN(len);
     283             : 
     284        3498 :     data_len = heap_compute_data_size(brtuple_disk_tupdesc(brdesc),
     285             :                                       values, nulls);
     286        3498 :     len += data_len;
     287             : 
     288        3498 :     len = MAXALIGN(len);
     289             : 
     290        3498 :     rettuple = palloc0(len);
     291        3498 :     rettuple->bt_blkno = blkno;
     292        3498 :     rettuple->bt_info = hoff;
     293             : 
     294             :     /* Assert that hoff fits in the space available */
     295             :     Assert((rettuple->bt_info & BRIN_OFFSET_MASK) == hoff);
     296             : 
     297             :     /*
     298             :      * The infomask and null bitmap as computed by heap_fill_tuple are useless
     299             :      * to us.  However, that function will not accept a null infomask; and we
     300             :      * need to pass a valid null bitmap so that it will correctly skip
     301             :      * outputting null attributes in the data area.
     302             :      */
     303        3498 :     heap_fill_tuple(brtuple_disk_tupdesc(brdesc),
     304             :                     values,
     305             :                     nulls,
     306             :                     (char *) rettuple + hoff,
     307             :                     data_len,
     308             :                     &phony_infomask,
     309             :                     phony_nullbitmap);
     310             : 
     311             :     /* done with these */
     312        3498 :     pfree(values);
     313        3498 :     pfree(nulls);
     314        3498 :     pfree(phony_nullbitmap);
     315             : 
     316             : #ifdef TOAST_INDEX_HACK
     317        3522 :     for (i = 0; i < nuntoasted; i++)
     318          24 :         pfree(DatumGetPointer(untoasted_values[i]));
     319             : #endif
     320             : 
     321             :     /*
     322             :      * Now fill in the real null bitmasks.  allnulls first.
     323             :      */
     324        3498 :     if (anynulls)
     325             :     {
     326             :         bits8      *bitP;
     327             :         int         bitmask;
     328             : 
     329         370 :         rettuple->bt_info |= BRIN_NULLS_MASK;
     330             : 
     331             :         /*
     332             :          * Note that we reverse the sense of null bits in this module: we
     333             :          * store a 1 for a null attribute rather than a 0.  So we must reverse
     334             :          * the sense of the att_isnull test in brin_deconstruct_tuple as well.
     335             :          */
     336         370 :         bitP = ((bits8 *) ((char *) rettuple + SizeOfBrinTuple)) - 1;
     337         370 :         bitmask = HIGHBIT;
     338        7992 :         for (keyno = 0; keyno < brdesc->bd_tupdesc->natts; keyno++)
     339             :         {
     340        7622 :             if (bitmask != HIGHBIT)
     341        6498 :                 bitmask <<= 1;
     342             :             else
     343             :             {
     344        1124 :                 bitP += 1;
     345        1124 :                 *bitP = 0x0;
     346        1124 :                 bitmask = 1;
     347             :             }
     348             : 
     349        7622 :             if (!tuple->bt_columns[keyno].bv_allnulls)
     350        7184 :                 continue;
     351             : 
     352         438 :             *bitP |= bitmask;
     353             :         }
     354             :         /* hasnulls bits follow */
     355        7992 :         for (keyno = 0; keyno < brdesc->bd_tupdesc->natts; keyno++)
     356             :         {
     357        7622 :             if (bitmask != HIGHBIT)
     358        6760 :                 bitmask <<= 1;
     359             :             else
     360             :             {
     361         862 :                 bitP += 1;
     362         862 :                 *bitP = 0x0;
     363         862 :                 bitmask = 1;
     364             :             }
     365             : 
     366        7622 :             if (!tuple->bt_columns[keyno].bv_hasnulls)
     367        1110 :                 continue;
     368             : 
     369        6512 :             *bitP |= bitmask;
     370             :         }
     371             :     }
     372             : 
     373        3498 :     if (tuple->bt_placeholder)
     374           0 :         rettuple->bt_info |= BRIN_PLACEHOLDER_MASK;
     375             : 
     376        3498 :     *size = len;
     377        3498 :     return rettuple;
     378             : }
     379             : 
     380             : /*
     381             :  * Generate a new on-disk tuple with no data values, marked as placeholder.
     382             :  *
     383             :  * This is a cut-down version of brin_form_tuple.
     384             :  */
     385             : BrinTuple *
     386          94 : brin_form_placeholder_tuple(BrinDesc *brdesc, BlockNumber blkno, Size *size)
     387             : {
     388             :     Size        len;
     389             :     Size        hoff;
     390             :     BrinTuple  *rettuple;
     391             :     int         keyno;
     392             :     bits8      *bitP;
     393             :     int         bitmask;
     394             : 
     395             :     /* compute total space needed: always add nulls */
     396          94 :     len = SizeOfBrinTuple;
     397          94 :     len += BITMAPLEN(brdesc->bd_tupdesc->natts * 2);
     398          94 :     len = hoff = MAXALIGN(len);
     399             : 
     400          94 :     rettuple = palloc0(len);
     401          94 :     rettuple->bt_blkno = blkno;
     402          94 :     rettuple->bt_info = hoff;
     403          94 :     rettuple->bt_info |= BRIN_NULLS_MASK | BRIN_PLACEHOLDER_MASK;
     404             : 
     405          94 :     bitP = ((bits8 *) ((char *) rettuple + SizeOfBrinTuple)) - 1;
     406          94 :     bitmask = HIGHBIT;
     407             :     /* set allnulls true for all attributes */
     408        1920 :     for (keyno = 0; keyno < brdesc->bd_tupdesc->natts; keyno++)
     409             :     {
     410        1826 :         if (bitmask != HIGHBIT)
     411        1560 :             bitmask <<= 1;
     412             :         else
     413             :         {
     414         266 :             bitP += 1;
     415         266 :             *bitP = 0x0;
     416         266 :             bitmask = 1;
     417             :         }
     418             : 
     419        1826 :         *bitP |= bitmask;
     420             :     }
     421             :     /* no need to set hasnulls */
     422             : 
     423          94 :     *size = len;
     424          94 :     return rettuple;
     425             : }
     426             : 
     427             : /*
     428             :  * Free a tuple created by brin_form_tuple
     429             :  */
     430             : void
     431         188 : brin_free_tuple(BrinTuple *tuple)
     432             : {
     433         188 :     pfree(tuple);
     434         188 : }
     435             : 
     436             : /*
     437             :  * Given a brin tuple of size len, create a copy of it.  If 'dest' is not
     438             :  * NULL, its size is destsz, and can be used as output buffer; if the tuple
     439             :  * to be copied does not fit, it is enlarged by repalloc, and the size is
     440             :  * updated to match.  This avoids palloc/free cycles when many brin tuples
     441             :  * are being processed in loops.
     442             :  */
     443             : BrinTuple *
     444      127128 : brin_copy_tuple(BrinTuple *tuple, Size len, BrinTuple *dest, Size *destsz)
     445             : {
     446      127128 :     if (!destsz || *destsz == 0)
     447      127128 :         dest = palloc(len);
     448           0 :     else if (len > *destsz)
     449             :     {
     450           0 :         dest = repalloc(dest, len);
     451           0 :         *destsz = len;
     452             :     }
     453             : 
     454      127128 :     memcpy(dest, tuple, len);
     455             : 
     456      127128 :     return dest;
     457             : }
     458             : 
     459             : /*
     460             :  * Return whether two BrinTuples are bitwise identical.
     461             :  */
     462             : bool
     463        2070 : brin_tuples_equal(const BrinTuple *a, Size alen, const BrinTuple *b, Size blen)
     464             : {
     465        2070 :     if (alen != blen)
     466           0 :         return false;
     467        2070 :     if (memcmp(a, b, alen) != 0)
     468           0 :         return false;
     469        2070 :     return true;
     470             : }
     471             : 
     472             : /*
     473             :  * Create a new BrinMemTuple from scratch, and initialize it to an empty
     474             :  * state.
     475             :  *
     476             :  * Note: we don't provide any means to free a deformed tuple, so make sure to
     477             :  * use a temporary memory context.
     478             :  */
     479             : BrinMemTuple *
     480       12536 : brin_new_memtuple(BrinDesc *brdesc)
     481             : {
     482             :     BrinMemTuple *dtup;
     483             :     long        basesize;
     484             : 
     485       12536 :     basesize = MAXALIGN(sizeof(BrinMemTuple) +
     486             :                         sizeof(BrinValues) * brdesc->bd_tupdesc->natts);
     487       12536 :     dtup = palloc0(basesize + sizeof(Datum) * brdesc->bd_totalstored);
     488             : 
     489       12536 :     dtup->bt_values = palloc(sizeof(Datum) * brdesc->bd_totalstored);
     490       12536 :     dtup->bt_allnulls = palloc(sizeof(bool) * brdesc->bd_tupdesc->natts);
     491       12536 :     dtup->bt_hasnulls = palloc(sizeof(bool) * brdesc->bd_tupdesc->natts);
     492             : 
     493       12536 :     dtup->bt_context = AllocSetContextCreate(CurrentMemoryContext,
     494             :                                              "brin dtuple",
     495             :                                              ALLOCSET_DEFAULT_SIZES);
     496             : 
     497       12536 :     brin_memtuple_initialize(dtup, brdesc);
     498             : 
     499       12536 :     return dtup;
     500             : }
     501             : 
     502             : /*
     503             :  * Reset a BrinMemTuple to initial state.  We return the same tuple, for
     504             :  * notational convenience.
     505             :  */
     506             : BrinMemTuple *
     507      139256 : brin_memtuple_initialize(BrinMemTuple *dtuple, BrinDesc *brdesc)
     508             : {
     509             :     int         i;
     510             :     char       *currdatum;
     511             : 
     512      139256 :     MemoryContextReset(dtuple->bt_context);
     513             : 
     514      139256 :     currdatum = (char *) dtuple +
     515      139256 :         MAXALIGN(sizeof(BrinMemTuple) +
     516             :                  sizeof(BrinValues) * brdesc->bd_tupdesc->natts);
     517     3782664 :     for (i = 0; i < brdesc->bd_tupdesc->natts; i++)
     518             :     {
     519     3643408 :         dtuple->bt_columns[i].bv_attno = i + 1;
     520     3643408 :         dtuple->bt_columns[i].bv_allnulls = true;
     521     3643408 :         dtuple->bt_columns[i].bv_hasnulls = false;
     522     3643408 :         dtuple->bt_columns[i].bv_values = (Datum *) currdatum;
     523             : 
     524     3643408 :         dtuple->bt_columns[i].bv_mem_value = PointerGetDatum(NULL);
     525     3643408 :         dtuple->bt_columns[i].bv_serialize = NULL;
     526     3643408 :         dtuple->bt_columns[i].bv_context = dtuple->bt_context;
     527             : 
     528     3643408 :         currdatum += sizeof(Datum) * brdesc->bd_info[i]->oi_nstored;
     529             :     }
     530             : 
     531      139256 :     return dtuple;
     532             : }
     533             : 
     534             : /*
     535             :  * Convert a BrinTuple back to a BrinMemTuple.  This is the reverse of
     536             :  * brin_form_tuple.
     537             :  *
     538             :  * As an optimization, the caller can pass a previously allocated 'dMemtuple'.
     539             :  * This avoids having to allocate it here, which can be useful when this
     540             :  * function is called many times in a loop.  It is caller's responsibility
     541             :  * that the given BrinMemTuple matches what we need here.
     542             :  *
     543             :  * Note we don't need the "on disk tupdesc" here; we rely on our own routine to
     544             :  * deconstruct the tuple from the on-disk format.
     545             :  */
     546             : BrinMemTuple *
     547      135768 : brin_deform_tuple(BrinDesc *brdesc, BrinTuple *tuple, BrinMemTuple *dMemtuple)
     548             : {
     549             :     BrinMemTuple *dtup;
     550             :     Datum      *values;
     551             :     bool       *allnulls;
     552             :     bool       *hasnulls;
     553             :     char       *tp;
     554             :     bits8      *nullbits;
     555             :     int         keyno;
     556             :     int         valueno;
     557             :     MemoryContext oldcxt;
     558             : 
     559      135768 :     dtup = dMemtuple ? brin_memtuple_initialize(dMemtuple, brdesc) :
     560       10616 :         brin_new_memtuple(brdesc);
     561             : 
     562      135768 :     if (BrinTupleIsPlaceholder(tuple))
     563           0 :         dtup->bt_placeholder = true;
     564      135768 :     dtup->bt_blkno = tuple->bt_blkno;
     565             : 
     566      135768 :     values = dtup->bt_values;
     567      135768 :     allnulls = dtup->bt_allnulls;
     568      135768 :     hasnulls = dtup->bt_hasnulls;
     569             : 
     570      135768 :     tp = (char *) tuple + BrinTupleDataOffset(tuple);
     571             : 
     572      135768 :     if (BrinTupleHasNulls(tuple))
     573       15328 :         nullbits = (bits8 *) ((char *) tuple + SizeOfBrinTuple);
     574             :     else
     575      120440 :         nullbits = NULL;
     576      135768 :     brin_deconstruct_tuple(brdesc,
     577      135768 :                            tp, nullbits, BrinTupleHasNulls(tuple),
     578             :                            values, allnulls, hasnulls);
     579             : 
     580             :     /*
     581             :      * Iterate to assign each of the values to the corresponding item in the
     582             :      * values array of each column.  The copies occur in the tuple's context.
     583             :      */
     584      135768 :     oldcxt = MemoryContextSwitchTo(dtup->bt_context);
     585     3710620 :     for (valueno = 0, keyno = 0; keyno < brdesc->bd_tupdesc->natts; keyno++)
     586             :     {
     587             :         int         i;
     588             : 
     589     3574852 :         if (allnulls[keyno])
     590             :         {
     591        7332 :             valueno += brdesc->bd_info[keyno]->oi_nstored;
     592        7332 :             continue;
     593             :         }
     594             : 
     595             :         /*
     596             :          * We would like to skip datumCopy'ing the values datum in some cases,
     597             :          * caller permitting ...
     598             :          */
     599    10544912 :         for (i = 0; i < brdesc->bd_info[keyno]->oi_nstored; i++)
     600     6977392 :             dtup->bt_columns[keyno].bv_values[i] =
     601    20932176 :                 datumCopy(values[valueno++],
     602     6977392 :                           brdesc->bd_info[keyno]->oi_typcache[i]->typbyval,
     603     6977392 :                           brdesc->bd_info[keyno]->oi_typcache[i]->typlen);
     604             : 
     605     3567520 :         dtup->bt_columns[keyno].bv_hasnulls = hasnulls[keyno];
     606     3567520 :         dtup->bt_columns[keyno].bv_allnulls = false;
     607             : 
     608     3567520 :         dtup->bt_columns[keyno].bv_mem_value = PointerGetDatum(NULL);
     609     3567520 :         dtup->bt_columns[keyno].bv_serialize = NULL;
     610     3567520 :         dtup->bt_columns[keyno].bv_context = dtup->bt_context;
     611             :     }
     612             : 
     613      135768 :     MemoryContextSwitchTo(oldcxt);
     614             : 
     615      135768 :     return dtup;
     616             : }
     617             : 
     618             : /*
     619             :  * brin_deconstruct_tuple
     620             :  *      Guts of attribute extraction from an on-disk BRIN tuple.
     621             :  *
     622             :  * Its arguments are:
     623             :  *  brdesc      BRIN descriptor for the stored tuple
     624             :  *  tp          pointer to the tuple data area
     625             :  *  nullbits    pointer to the tuple nulls bitmask
     626             :  *  nulls       "has nulls" bit in tuple infomask
     627             :  *  values      output values, array of size brdesc->bd_totalstored
     628             :  *  allnulls    output "allnulls", size brdesc->bd_tupdesc->natts
     629             :  *  hasnulls    output "hasnulls", size brdesc->bd_tupdesc->natts
     630             :  *
     631             :  * Output arrays must have been allocated by caller.
     632             :  */
     633             : static inline void
     634      135768 : brin_deconstruct_tuple(BrinDesc *brdesc,
     635             :                        char *tp, bits8 *nullbits, bool nulls,
     636             :                        Datum *values, bool *allnulls, bool *hasnulls)
     637             : {
     638             :     int         attnum;
     639             :     int         stored;
     640             :     TupleDesc   diskdsc;
     641             :     long        off;
     642             : 
     643             :     /*
     644             :      * First iterate to natts to obtain both null flags for each attribute.
     645             :      * Note that we reverse the sense of the att_isnull test, because we store
     646             :      * 1 for a null value (rather than a 1 for a not null value as is the
     647             :      * att_isnull convention used elsewhere.)  See brin_form_tuple.
     648             :      */
     649     3710620 :     for (attnum = 0; attnum < brdesc->bd_tupdesc->natts; attnum++)
     650             :     {
     651             :         /*
     652             :          * the "all nulls" bit means that all values in the page range for
     653             :          * this column are nulls.  Therefore there are no values in the tuple
     654             :          * data area.
     655             :          */
     656     3574852 :         allnulls[attnum] = nulls && !att_isnull(attnum, nullbits);
     657             : 
     658             :         /*
     659             :          * the "has nulls" bit means that some tuples have nulls, but others
     660             :          * have not-null values.  Therefore we know the tuple contains data
     661             :          * for this column.
     662             :          *
     663             :          * The hasnulls bits follow the allnulls bits in the same bitmask.
     664             :          */
     665     7149704 :         hasnulls[attnum] =
     666     3574852 :             nulls && !att_isnull(brdesc->bd_tupdesc->natts + attnum, nullbits);
     667             :     }
     668             : 
     669             :     /*
     670             :      * Iterate to obtain each attribute's stored values.  Note that since we
     671             :      * may reuse attribute entries for more than one column, we cannot cache
     672             :      * offsets here.
     673             :      */
     674      135768 :     diskdsc = brtuple_disk_tupdesc(brdesc);
     675      135768 :     stored = 0;
     676      135768 :     off = 0;
     677     3710620 :     for (attnum = 0; attnum < brdesc->bd_tupdesc->natts; attnum++)
     678             :     {
     679             :         int         datumno;
     680             : 
     681     3574852 :         if (allnulls[attnum])
     682             :         {
     683        7332 :             stored += brdesc->bd_info[attnum]->oi_nstored;
     684        7332 :             continue;
     685             :         }
     686             : 
     687     3567520 :         for (datumno = 0;
     688    10544912 :              datumno < brdesc->bd_info[attnum]->oi_nstored;
     689     6977392 :              datumno++)
     690             :         {
     691     6977392 :             Form_pg_attribute thisatt = TupleDescAttr(diskdsc, stored);
     692             : 
     693     6977392 :             if (thisatt->attlen == -1)
     694             :             {
     695     2458616 :                 off = att_align_pointer(off, thisatt->attalign, -1,
     696             :                                         tp + off);
     697             :             }
     698             :             else
     699             :             {
     700             :                 /* not varlena, so safe to use att_align_nominal */
     701     4518776 :                 off = att_align_nominal(off, thisatt->attalign);
     702             :             }
     703             : 
     704     6977392 :             values[stored++] = fetchatt(thisatt, tp + off);
     705             : 
     706     6977392 :             off = att_addlength_pointer(off, thisatt->attlen, tp + off);
     707             :         }
     708             :     }
     709      135768 : }

Generated by: LCOV version 1.13