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: 2019-11-21 12:06:29 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-2019, 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       54478 : toast_tuple_init(ToastTupleContext *ttc)
      40             : {
      41       54478 :     TupleDesc   tupleDesc = ttc->ttc_rel->rd_att;
      42       54478 :     int         numAttrs = tupleDesc->natts;
      43             :     int         i;
      44             : 
      45       54478 :     ttc->ttc_flags = 0;
      46             : 
      47      608002 :     for (i = 0; i < numAttrs; i++)
      48             :     {
      49      553524 :         Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
      50             :         struct varlena *old_value;
      51             :         struct varlena *new_value;
      52             : 
      53      553524 :         ttc->ttc_attr[i].tai_colflags = 0;
      54      553524 :         ttc->ttc_attr[i].tai_oldexternal = NULL;
      55             : 
      56      553524 :         if (ttc->ttc_oldvalues != NULL)
      57             :         {
      58             :             /*
      59             :              * For UPDATE get the old and new values of this attribute
      60             :              */
      61       36576 :             old_value =
      62       36576 :                 (struct varlena *) DatumGetPointer(ttc->ttc_oldvalues[i]);
      63       36576 :             new_value =
      64       36576 :                 (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       41586 :             if (att->attlen == -1 && !ttc->ttc_oldisnull[i] &&
      71        5558 :                 VARATT_IS_EXTERNAL_ONDISK(old_value))
      72             :             {
      73        1040 :                 if (ttc->ttc_isnull[i] ||
      74         672 :                     !VARATT_IS_EXTERNAL_ONDISK(new_value) ||
      75         132 :                     memcmp((char *) old_value, (char *) new_value,
      76         132 :                            VARSIZE_EXTERNAL(old_value)) != 0)
      77             :                 {
      78             :                     /*
      79             :                      * The old external stored value isn't needed any more
      80             :                      * after the update
      81             :                      */
      82         484 :                     ttc->ttc_attr[i].tai_colflags |= TOASTCOL_NEEDS_DELETE_OLD;
      83         484 :                     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          64 :                     ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
      93          64 :                     continue;
      94             :                 }
      95             :             }
      96             :         }
      97             :         else
      98             :         {
      99             :             /*
     100             :              * For INSERT simply get the new value
     101             :              */
     102      516948 :             new_value = (struct varlena *) DatumGetPointer(ttc->ttc_values[i]);
     103             :         }
     104             : 
     105             :         /*
     106             :          * Handle NULL attributes
     107             :          */
     108      553460 :         if (ttc->ttc_isnull[i])
     109             :         {
     110       38970 :             ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
     111       38970 :             ttc->ttc_flags |= TOAST_HAS_NULLS;
     112       38970 :             continue;
     113             :         }
     114             : 
     115             :         /*
     116             :          * Now look at varlena attributes
     117             :          */
     118      514490 :         if (att->attlen == -1)
     119             :         {
     120             :             /*
     121             :              * If the table's attribute says PLAIN always, force it so.
     122             :              */
     123      115974 :             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      115974 :             if (VARATT_IS_EXTERNAL(new_value))
     135             :             {
     136         604 :                 ttc->ttc_attr[i].tai_oldexternal = new_value;
     137         604 :                 if (att->attstorage == 'p')
     138           0 :                     new_value = detoast_attr(new_value);
     139             :                 else
     140         604 :                     new_value = detoast_external_attr(new_value);
     141         604 :                 ttc->ttc_values[i] = PointerGetDatum(new_value);
     142         604 :                 ttc->ttc_attr[i].tai_colflags |= TOASTCOL_NEEDS_FREE;
     143         604 :                 ttc->ttc_flags |= (TOAST_NEEDS_CHANGE | TOAST_NEEDS_FREE);
     144             :             }
     145             : 
     146             :             /*
     147             :              * Remember the size of this attribute
     148             :              */
     149      115974 :             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      398516 :             ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
     157             :         }
     158             :     }
     159       54478 : }
     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       68850 : toast_tuple_find_biggest_attribute(ToastTupleContext *ttc,
     179             :                                    bool for_compression, bool check_main)
     180             : {
     181       68850 :     TupleDesc   tupleDesc = ttc->ttc_rel->rd_att;
     182       68850 :     int         numAttrs = tupleDesc->natts;
     183       68850 :     int         biggest_attno = -1;
     184       68850 :     int32       biggest_size = MAXALIGN(TOAST_POINTER_SIZE);
     185       68850 :     int32       skip_colflags = TOASTCOL_IGNORE;
     186             :     int         i;
     187             : 
     188       68850 :     if (for_compression)
     189       65424 :         skip_colflags |= TOASTCOL_INCOMPRESSIBLE;
     190             : 
     191      988208 :     for (i = 0; i < numAttrs; i++)
     192             :     {
     193      919358 :         Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
     194             : 
     195      919358 :         if ((ttc->ttc_attr[i].tai_colflags & skip_colflags) != 0)
     196      753378 :             continue;
     197      165980 :         if (VARATT_IS_EXTERNAL(DatumGetPointer(ttc->ttc_values[i])))
     198           0 :             continue;           /* can't happen, toast_action would be 'p' */
     199      321854 :         if (for_compression &&
     200      155874 :             VARATT_IS_COMPRESSED(DatumGetPointer(ttc->ttc_values[i])))
     201       16564 :             continue;
     202      149416 :         if (check_main && att->attstorage != 'm')
     203           0 :             continue;
     204      149416 :         if (!check_main && att->attstorage != 'x' && att->attstorage != 'e')
     205          16 :             continue;
     206             : 
     207      149400 :         if (ttc->ttc_attr[i].tai_size > biggest_size)
     208             :         {
     209       86644 :             biggest_attno = i;
     210       86644 :             biggest_size = ttc->ttc_attr[i].tai_size;
     211             :         }
     212             :     }
     213             : 
     214       68850 :     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       61470 : toast_tuple_try_compression(ToastTupleContext *ttc, int attribute)
     224             : {
     225       61470 :     Datum      *value = &ttc->ttc_values[attribute];
     226       61470 :     Datum       new_value = toast_compress_datum(*value);
     227       61470 :     ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
     228             : 
     229       61470 :     if (DatumGetPointer(new_value) != NULL)
     230             :     {
     231             :         /* successful compression */
     232       59062 :         if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
     233          22 :             pfree(DatumGetPointer(*value));
     234       59062 :         *value = new_value;
     235       59062 :         attr->tai_colflags |= TOASTCOL_NEEDS_FREE;
     236       59062 :         attr->tai_size = VARSIZE(DatumGetPointer(*value));
     237       59062 :         ttc->ttc_flags |= (TOAST_NEEDS_CHANGE | TOAST_NEEDS_FREE);
     238             :     }
     239             :     else
     240             :     {
     241             :         /* incompressible, ignore on subsequent compression passes */
     242        2408 :         attr->tai_colflags |= TOASTCOL_INCOMPRESSIBLE;
     243             :     }
     244       61470 : }
     245             : 
     246             : /*
     247             :  * Move an attribute to external storage.
     248             :  */
     249             : void
     250       28164 : toast_tuple_externalize(ToastTupleContext *ttc, int attribute, int options)
     251             : {
     252       28164 :     Datum      *value = &ttc->ttc_values[attribute];
     253       28164 :     Datum       old_value = *value;
     254       28164 :     ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
     255             : 
     256       28164 :     attr->tai_colflags |= TOASTCOL_IGNORE;
     257       28164 :     *value = toast_save_datum(ttc->ttc_rel, old_value, attr->tai_oldexternal,
     258             :                               options);
     259       28164 :     if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
     260       27822 :         pfree(DatumGetPointer(old_value));
     261       28164 :     attr->tai_colflags |= TOASTCOL_NEEDS_FREE;
     262       28164 :     ttc->ttc_flags |= (TOAST_NEEDS_CHANGE | TOAST_NEEDS_FREE);
     263       28164 : }
     264             : 
     265             : /*
     266             :  * Perform appropriate cleanup after one tuple has been subjected to TOAST.
     267             :  */
     268             : void
     269       54478 : toast_tuple_cleanup(ToastTupleContext *ttc)
     270             : {
     271       54478 :     TupleDesc   tupleDesc = ttc->ttc_rel->rd_att;
     272       54478 :     int         numAttrs = tupleDesc->natts;
     273             : 
     274             :     /*
     275             :      * Free allocated temp values
     276             :      */
     277       54478 :     if ((ttc->ttc_flags & TOAST_NEEDS_FREE) != 0)
     278             :     {
     279             :         int         i;
     280             : 
     281      603806 :         for (i = 0; i < numAttrs; i++)
     282             :         {
     283      549496 :             ToastAttrInfo *attr = &ttc->ttc_attr[i];
     284             : 
     285      549496 :             if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
     286       59986 :                 pfree(DatumGetPointer(ttc->ttc_values[i]));
     287             :         }
     288             :     }
     289             : 
     290             :     /*
     291             :      * Delete external values from the old tuple
     292             :      */
     293       54478 :     if ((ttc->ttc_flags & TOAST_NEEDS_DELETE_OLD) != 0)
     294             :     {
     295             :         int         i;
     296             : 
     297       11822 :         for (i = 0; i < numAttrs; i++)
     298             :         {
     299       11406 :             ToastAttrInfo *attr = &ttc->ttc_attr[i];
     300             : 
     301       11406 :             if ((attr->tai_colflags & TOASTCOL_NEEDS_DELETE_OLD) != 0)
     302         484 :                 toast_delete_datum(ttc->ttc_rel, ttc->ttc_oldvalues[i], false);
     303             :         }
     304             :     }
     305       54478 : }
     306             : 
     307             : /*
     308             :  * Check for external stored attributes and delete them from the secondary
     309             :  * relation.
     310             :  */
     311             : void
     312         226 : toast_delete_external(Relation rel, Datum *values, bool *isnull,
     313             :                       bool is_speculative)
     314             : {
     315         226 :     TupleDesc   tupleDesc = rel->rd_att;
     316         226 :     int         numAttrs = tupleDesc->natts;
     317             :     int         i;
     318             : 
     319         892 :     for (i = 0; i < numAttrs; i++)
     320             :     {
     321         666 :         if (TupleDescAttr(tupleDesc, i)->attlen == -1)
     322             :         {
     323         278 :             Datum       value = values[i];
     324             : 
     325         278 :             if (isnull[i])
     326          16 :                 continue;
     327         262 :             else if (VARATT_IS_EXTERNAL_ONDISK(PointerGetDatum(value)))
     328         234 :                 toast_delete_datum(rel, value, is_speculative);
     329             :         }
     330             :     }
     331         226 : }

Generated by: LCOV version 1.13