LCOV - code coverage report
Current view: top level - src/backend/access/table - toast_helper.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 113 116 97.4 %
Date: 2020-02-27 19:06:28 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * toast_helper.c
       4             :  *    Helper functions for table AMs implementing compressed or
       5             :  *    out-of-line storage of varlena attributes.
       6             :  *
       7             :  * Copyright (c) 2000-2020, PostgreSQL Global Development Group
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/backend/access/common/toast_helper.c
      11             :  *
      12             :  *-------------------------------------------------------------------------
      13             :  */
      14             : 
      15             : #include "postgres.h"
      16             : 
      17             : #include "access/detoast.h"
      18             : #include "access/table.h"
      19             : #include "access/toast_helper.h"
      20             : #include "access/toast_internals.h"
      21             : 
      22             : /*
      23             :  * Prepare to TOAST a tuple.
      24             :  *
      25             :  * tupleDesc, toast_values, and toast_isnull are required parameters; they
      26             :  * provide the necessary details about the tuple to be toasted.
      27             :  *
      28             :  * toast_oldvalues and toast_oldisnull should be NULL for a newly-inserted
      29             :  * tuple; for an update, they should describe the existing tuple.
      30             :  *
      31             :  * All of these arrays should have a length equal to tupleDesc->natts.
      32             :  *
      33             :  * On return, toast_flags and toast_attr will have been initialized.
      34             :  * toast_flags is just a single uint8, but toast_attr is an caller-provided
      35             :  * array with a length equal to tupleDesc->natts.  The caller need not
      36             :  * perform any initialization of the array before calling this function.
      37             :  */
      38             : void
      39       55470 : toast_tuple_init(ToastTupleContext *ttc)
      40             : {
      41       55470 :     TupleDesc   tupleDesc = ttc->ttc_rel->rd_att;
      42       55470 :     int         numAttrs = tupleDesc->natts;
      43             :     int         i;
      44             : 
      45       55470 :     ttc->ttc_flags = 0;
      46             : 
      47      617914 :     for (i = 0; i < numAttrs; i++)
      48             :     {
      49      562444 :         Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
      50             :         struct varlena *old_value;
      51             :         struct varlena *new_value;
      52             : 
      53      562444 :         ttc->ttc_attr[i].tai_colflags = 0;
      54      562444 :         ttc->ttc_attr[i].tai_oldexternal = NULL;
      55             : 
      56      562444 :         if (ttc->ttc_oldvalues != NULL)
      57             :         {
      58             :             /*
      59             :              * For UPDATE get the old and new values of this attribute
      60             :              */
      61       36936 :             old_value =
      62       36936 :                 (struct varlena *) DatumGetPointer(ttc->ttc_oldvalues[i]);
      63       36936 :             new_value =
      64       36936 :                 (struct varlena *) DatumGetPointer(ttc->ttc_values[i]);
      65             : 
      66             :             /*
      67             :              * If the old value is stored on disk, check if it has changed so
      68             :              * we have to delete it later.
      69             :              */
      70       36936 :             if (att->attlen == -1 && !ttc->ttc_oldisnull[i] &&
      71        5078 :                 VARATT_IS_EXTERNAL_ONDISK(old_value))
      72             :             {
      73         548 :                 if (ttc->ttc_isnull[i] ||
      74         496 :                     !VARATT_IS_EXTERNAL_ONDISK(new_value) ||
      75          70 :                     memcmp((char *) old_value, (char *) new_value,
      76          70 :                            VARSIZE_EXTERNAL(old_value)) != 0)
      77             :                 {
      78             :                     /*
      79             :                      * The old external stored value isn't needed any more
      80             :                      * after the update
      81             :                      */
      82         480 :                     ttc->ttc_attr[i].tai_colflags |= TOASTCOL_NEEDS_DELETE_OLD;
      83         480 :                     ttc->ttc_flags |= TOAST_NEEDS_DELETE_OLD;
      84             :                 }
      85             :                 else
      86             :                 {
      87             :                     /*
      88             :                      * This attribute isn't changed by this update so we reuse
      89             :                      * the original reference to the old value in the new
      90             :                      * tuple.
      91             :                      */
      92          68 :                     ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
      93          68 :                     continue;
      94             :                 }
      95             :             }
      96             :         }
      97             :         else
      98             :         {
      99             :             /*
     100             :              * For INSERT simply get the new value
     101             :              */
     102      525508 :             new_value = (struct varlena *) DatumGetPointer(ttc->ttc_values[i]);
     103             :         }
     104             : 
     105             :         /*
     106             :          * Handle NULL attributes
     107             :          */
     108      562376 :         if (ttc->ttc_isnull[i])
     109             :         {
     110       39272 :             ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
     111       39272 :             ttc->ttc_flags |= TOAST_HAS_NULLS;
     112       39272 :             continue;
     113             :         }
     114             : 
     115             :         /*
     116             :          * Now look at varlena attributes
     117             :          */
     118      523104 :         if (att->attlen == -1)
     119             :         {
     120             :             /*
     121             :              * If the table's attribute says PLAIN always, force it so.
     122             :              */
     123      118030 :             if (att->attstorage == 'p')
     124          22 :                 ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
     125             : 
     126             :             /*
     127             :              * We took care of UPDATE above, so any external value we find
     128             :              * still in the tuple must be someone else's that we cannot reuse
     129             :              * (this includes the case of an out-of-line in-memory datum).
     130             :              * Fetch it back (without decompression, unless we are forcing
     131             :              * PLAIN storage).  If necessary, we'll push it out as a new
     132             :              * external value below.
     133             :              */
     134      118030 :             if (VARATT_IS_EXTERNAL(new_value))
     135             :             {
     136         606 :                 ttc->ttc_attr[i].tai_oldexternal = new_value;
     137         606 :                 if (att->attstorage == 'p')
     138           0 :                     new_value = detoast_attr(new_value);
     139             :                 else
     140         606 :                     new_value = detoast_external_attr(new_value);
     141         606 :                 ttc->ttc_values[i] = PointerGetDatum(new_value);
     142         606 :                 ttc->ttc_attr[i].tai_colflags |= TOASTCOL_NEEDS_FREE;
     143         606 :                 ttc->ttc_flags |= (TOAST_NEEDS_CHANGE | TOAST_NEEDS_FREE);
     144             :             }
     145             : 
     146             :             /*
     147             :              * Remember the size of this attribute
     148             :              */
     149      118030 :             ttc->ttc_attr[i].tai_size = VARSIZE_ANY(new_value);
     150             :         }
     151             :         else
     152             :         {
     153             :             /*
     154             :              * Not a varlena attribute, plain storage always
     155             :              */
     156      405074 :             ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
     157             :         }
     158             :     }
     159       55470 : }
     160             : 
     161             : /*
     162             :  * Find the largest varlena attribute that satisfies certain criteria.
     163             :  *
     164             :  * The relevant column must not be marked TOASTCOL_IGNORE, and if the
     165             :  * for_compression flag is passed as true, it must also not be marked
     166             :  * TOASTCOL_INCOMPRESSIBLE.
     167             :  *
     168             :  * The column must have attstorage 'e' or 'x' if check_main is false, and
     169             :  * must have attstorage 'm' if check_main is true.
     170             :  *
     171             :  * The column must have a minimum size of MAXALIGN(TOAST_POINTER_SIZE);
     172             :  * if not, no benefit is to be expected by compressing it.
     173             :  *
     174             :  * The return value is the index of the biggest suitable column, or
     175             :  * -1 if there is none.
     176             :  */
     177             : int
     178       69216 : toast_tuple_find_biggest_attribute(ToastTupleContext *ttc,
     179             :                                    bool for_compression, bool check_main)
     180             : {
     181       69216 :     TupleDesc   tupleDesc = ttc->ttc_rel->rd_att;
     182       69216 :     int         numAttrs = tupleDesc->natts;
     183       69216 :     int         biggest_attno = -1;
     184       69216 :     int32       biggest_size = MAXALIGN(TOAST_POINTER_SIZE);
     185       69216 :     int32       skip_colflags = TOASTCOL_IGNORE;
     186             :     int         i;
     187             : 
     188       69216 :     if (for_compression)
     189       66100 :         skip_colflags |= TOASTCOL_INCOMPRESSIBLE;
     190             : 
     191      992706 :     for (i = 0; i < numAttrs; i++)
     192             :     {
     193      923490 :         Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
     194             : 
     195      923490 :         if ((ttc->ttc_attr[i].tai_colflags & skip_colflags) != 0)
     196      756686 :             continue;
     197      166804 :         if (VARATT_IS_EXTERNAL(DatumGetPointer(ttc->ttc_values[i])))
     198           0 :             continue;           /* can't happen, toast_action would be 'p' */
     199      166804 :         if (for_compression &&
     200      157320 :             VARATT_IS_COMPRESSED(DatumGetPointer(ttc->ttc_values[i])))
     201       16292 :             continue;
     202      150512 :         if (check_main && att->attstorage != 'm')
     203           0 :             continue;
     204      150512 :         if (!check_main && att->attstorage != 'x' && att->attstorage != 'e')
     205          20 :             continue;
     206             : 
     207      150492 :         if (ttc->ttc_attr[i].tai_size > biggest_size)
     208             :         {
     209       87412 :             biggest_attno = i;
     210       87412 :             biggest_size = ttc->ttc_attr[i].tai_size;
     211             :         }
     212             :     }
     213             : 
     214       69216 :     return biggest_attno;
     215             : }
     216             : 
     217             : /*
     218             :  * Try compression for an attribute.
     219             :  *
     220             :  * If we find that the attribute is not compressible, mark it so.
     221             :  */
     222             : void
     223       62456 : toast_tuple_try_compression(ToastTupleContext *ttc, int attribute)
     224             : {
     225       62456 :     Datum      *value = &ttc->ttc_values[attribute];
     226       62456 :     Datum       new_value = toast_compress_datum(*value);
     227       62456 :     ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
     228             : 
     229       62456 :     if (DatumGetPointer(new_value) != NULL)
     230             :     {
     231             :         /* successful compression */
     232       60062 :         if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
     233          22 :             pfree(DatumGetPointer(*value));
     234       60062 :         *value = new_value;
     235       60062 :         attr->tai_colflags |= TOASTCOL_NEEDS_FREE;
     236       60062 :         attr->tai_size = VARSIZE(DatumGetPointer(*value));
     237       60062 :         ttc->ttc_flags |= (TOAST_NEEDS_CHANGE | TOAST_NEEDS_FREE);
     238             :     }
     239             :     else
     240             :     {
     241             :         /* incompressible, ignore on subsequent compression passes */
     242        2394 :         attr->tai_colflags |= TOASTCOL_INCOMPRESSIBLE;
     243             :     }
     244       62456 : }
     245             : 
     246             : /*
     247             :  * Move an attribute to external storage.
     248             :  */
     249             : void
     250       28658 : toast_tuple_externalize(ToastTupleContext *ttc, int attribute, int options)
     251             : {
     252       28658 :     Datum      *value = &ttc->ttc_values[attribute];
     253       28658 :     Datum       old_value = *value;
     254       28658 :     ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
     255             : 
     256       28658 :     attr->tai_colflags |= TOASTCOL_IGNORE;
     257       28658 :     *value = toast_save_datum(ttc->ttc_rel, old_value, attr->tai_oldexternal,
     258             :                               options);
     259       28658 :     if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
     260       28312 :         pfree(DatumGetPointer(old_value));
     261       28658 :     attr->tai_colflags |= TOASTCOL_NEEDS_FREE;
     262       28658 :     ttc->ttc_flags |= (TOAST_NEEDS_CHANGE | TOAST_NEEDS_FREE);
     263       28658 : }
     264             : 
     265             : /*
     266             :  * Perform appropriate cleanup after one tuple has been subjected to TOAST.
     267             :  */
     268             : void
     269       55470 : toast_tuple_cleanup(ToastTupleContext *ttc)
     270             : {
     271       55470 :     TupleDesc   tupleDesc = ttc->ttc_rel->rd_att;
     272       55470 :     int         numAttrs = tupleDesc->natts;
     273             : 
     274             :     /*
     275             :      * Free allocated temp values
     276             :      */
     277       55470 :     if ((ttc->ttc_flags & TOAST_NEEDS_FREE) != 0)
     278             :     {
     279             :         int         i;
     280             : 
     281      613682 :         for (i = 0; i < numAttrs; i++)
     282             :         {
     283      558388 :             ToastAttrInfo *attr = &ttc->ttc_attr[i];
     284             : 
     285      558388 :             if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
     286       60992 :                 pfree(DatumGetPointer(ttc->ttc_values[i]));
     287             :         }
     288             :     }
     289             : 
     290             :     /*
     291             :      * Delete external values from the old tuple
     292             :      */
     293       55470 :     if ((ttc->ttc_flags & TOAST_NEEDS_DELETE_OLD) != 0)
     294             :     {
     295             :         int         i;
     296             : 
     297       11530 :         for (i = 0; i < numAttrs; i++)
     298             :         {
     299       11118 :             ToastAttrInfo *attr = &ttc->ttc_attr[i];
     300             : 
     301       11118 :             if ((attr->tai_colflags & TOASTCOL_NEEDS_DELETE_OLD) != 0)
     302         480 :                 toast_delete_datum(ttc->ttc_rel, ttc->ttc_oldvalues[i], false);
     303             :         }
     304             :     }
     305       55470 : }
     306             : 
     307             : /*
     308             :  * Check for external stored attributes and delete them from the secondary
     309             :  * relation.
     310             :  */
     311             : void
     312         236 : toast_delete_external(Relation rel, Datum *values, bool *isnull,
     313             :                       bool is_speculative)
     314             : {
     315         236 :     TupleDesc   tupleDesc = rel->rd_att;
     316         236 :     int         numAttrs = tupleDesc->natts;
     317             :     int         i;
     318             : 
     319         970 :     for (i = 0; i < numAttrs; i++)
     320             :     {
     321         734 :         if (TupleDescAttr(tupleDesc, i)->attlen == -1)
     322             :         {
     323         298 :             Datum       value = values[i];
     324             : 
     325         298 :             if (isnull[i])
     326          16 :                 continue;
     327         282 :             else if (VARATT_IS_EXTERNAL_ONDISK(PointerGetDatum(value)))
     328         244 :                 toast_delete_datum(rel, value, is_speculative);
     329             :         }
     330             :     }
     331         236 : }

Generated by: LCOV version 1.13