LCOV - differential code coverage report
Current view: top level - src/backend/access/table - toast_helper.c (source / functions) Coverage Total Hit LBC GBC GIC GNC CBC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 97.4 % 116 113 3 2 63 1 47 1 62 1
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 6 6 6 6
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

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

Generated by: LCOV version v1.16-55-g56c0a2a