LCOV - differential code coverage report
Current view: top level - src/backend/access/table - toast_helper.c (source / functions) Coverage Total Hit UBC GIC GNC CBC EUB ECB DUB DCB
Current: bed3ffbf9d952be6c7d739d068cdce44c046dfb7 vs 574581b50ac9c63dd9e4abebb731a3b67e5b50f6 Lines: 97.4 % 115 112 3 5 107 1 5
Current Date: 2026-05-05 10:23:31 +0900 Functions: 100.0 % 6 6 3 3 1
Baseline: lcov-20260505-025707-baseline Branches: 96.1 % 76 73 3 4 2 67 11 13 1 3
Baseline Date: 2026-05-05 10:27:06 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 5 5 5
(360..) days: 97.3 % 110 107 3 107 1
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 5 5 2 3
Branch coverage date bins:
(30,360] days: 100.0 % 2 2 2
(360..) days: 72.4 % 98 71 3 4 67 11 13

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

Generated by: LCOV version 2.5.0-beta