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 DCB
Current: f76c13edadc2b319036e0d238703ed56bb23f934 vs 9e17d25e79d4756be08b4a5521b4b58450217137 Lines: 97.5 % 118 115 3 4 111 1
Current Date: 2026-09-20 14:13:17 +0900 Functions: 100.0 % 6 6 2 4
Baseline: lcov-20260920-baseline Branches: 96.2 % 80 77 3 2 2 73
Baseline Date: 2026-09-20 14:13:13 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 100.0 % 4 4 4
(30,360] days: 100.0 % 4 4 4
(360..) days: 97.3 % 110 107 3 107
Function coverage date bins:
(30,360] days: 100.0 % 1 1 1
(360..) days: 100.0 % 5 5 2 3
Branch coverage date bins:
(1,7] days: 100.0 % 2 2 2
(360..) days: 96.2 % 78 75 3 2 73

 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
 2571 rhaas@postgresql.org       41                 :CBC       25015 : toast_tuple_init(ToastTupleContext *ttc)
                                 42                 :                : {
                                 43                 :          25015 :     TupleDesc   tupleDesc = ttc->ttc_rel->rd_att;
                                 44                 :          25015 :     int         numAttrs = tupleDesc->natts;
                                 45                 :                :     int         i;
                                 46                 :                : 
                                 47                 :          25015 :     ttc->ttc_flags = 0;
                                 48                 :                : 
                                 49         [ +  + ]:         256640 :     for (i = 0; i < numAttrs; i++)
                                 50                 :                :     {
                                 51                 :         231625 :         Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
                                 52                 :                :         varlena    *old_value;
                                 53                 :                :         varlena    *new_value;
                                 54                 :                : 
                                 55                 :         231625 :         ttc->ttc_attr[i].tai_colflags = 0;
                                 56                 :         231625 :         ttc->ttc_attr[i].tai_oldexternal = NULL;
 2011                            57                 :         231625 :         ttc->ttc_attr[i].tai_compression = att->attcompression;
                                 58                 :                : 
 2571                            59         [ +  + ]:         231625 :         if (ttc->ttc_oldvalues != NULL)
                                 60                 :                :         {
                                 61                 :                :             /*
                                 62                 :                :              * For UPDATE get the old and new values of this attribute
                                 63                 :                :              */
                                 64                 :                :             old_value =
  221 michael@paquier.xyz        65                 :          59418 :                 (varlena *) DatumGetPointer(ttc->ttc_oldvalues[i]);
                                 66                 :                :             new_value =
                                 67                 :          59418 :                 (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                 :                :              *
                                 73                 :                :              * Note that TOAST pointers could have different vartags, for oid
                                 74                 :                :              * or oid8, and these can have different sizes.
                                 75                 :                :              */
 2571 rhaas@postgresql.org       76   [ +  +  +  +  :          66470 :             if (att->attlen == -1 && !ttc->ttc_oldisnull[i] &&
                                              +  + ]
                                 77                 :           7052 :                 VARATT_IS_EXTERNAL_ONDISK(old_value))
                                 78                 :                :             {
                                 79         [ +  + ]:            789 :                 if (ttc->ttc_isnull[i] ||
                                 80   [ +  +  +  + ]:            842 :                     !VARATT_IS_EXTERNAL_ONDISK(new_value) ||
    5 michael@paquier.xyz        81                 :GNC          68 :                     VARTAG_EXTERNAL(old_value) != VARTAG_EXTERNAL(new_value) ||
  585 peter@eisentraut.org       82         [ +  + ]:CBC          60 :                     memcmp(old_value, new_value,
                                 83                 :                :                            VARSIZE_EXTERNAL(old_value)) != 0)
                                 84                 :                :                 {
                                 85                 :                :                     /*
                                 86                 :                :                      * The old external stored value isn't needed any more
                                 87                 :                :                      * after the update
                                 88                 :                :                      */
 2571 rhaas@postgresql.org       89                 :            730 :                     ttc->ttc_attr[i].tai_colflags |= TOASTCOL_NEEDS_DELETE_OLD;
                                 90                 :            730 :                     ttc->ttc_flags |= TOAST_NEEDS_DELETE_OLD;
                                 91                 :                :                 }
                                 92                 :                :                 else
                                 93                 :                :                 {
                                 94                 :                :                     /*
                                 95                 :                :                      * This attribute isn't changed by this update so we reuse
                                 96                 :                :                      * the original reference to the old value in the new
                                 97                 :                :                      * tuple.
                                 98                 :                :                      */
                                 99                 :             59 :                     ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
                                100                 :             59 :                     continue;
                                101                 :                :                 }
                                102                 :                :             }
                                103                 :                :         }
                                104                 :                :         else
                                105                 :                :         {
                                106                 :                :             /*
                                107                 :                :              * For INSERT simply get the new value
                                108                 :                :              */
  221 michael@paquier.xyz       109                 :         172207 :             new_value = (varlena *) DatumGetPointer(ttc->ttc_values[i]);
                                110                 :                :         }
                                111                 :                : 
                                112                 :                :         /*
                                113                 :                :          * Handle NULL attributes
                                114                 :                :          */
 2571 rhaas@postgresql.org      115         [ +  + ]:         231566 :         if (ttc->ttc_isnull[i])
                                116                 :                :         {
                                117                 :          25409 :             ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
                                118                 :          25409 :             ttc->ttc_flags |= TOAST_HAS_NULLS;
                                119                 :          25409 :             continue;
                                120                 :                :         }
                                121                 :                : 
                                122                 :                :         /*
                                123                 :                :          * Now look at varlena attributes
                                124                 :                :          */
                                125         [ +  + ]:         206157 :         if (att->attlen == -1)
                                126                 :                :         {
                                127                 :                :             /*
                                128                 :                :              * If the table's attribute says PLAIN always, force it so.
                                129                 :                :              */
 2391 tgl@sss.pgh.pa.us         130         [ +  + ]:          47346 :             if (att->attstorage == TYPSTORAGE_PLAIN)
 2571 rhaas@postgresql.org      131                 :           1420 :                 ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
                                132                 :                : 
                                133                 :                :             /*
                                134                 :                :              * We took care of UPDATE above, so any external value we find
                                135                 :                :              * still in the tuple must be someone else's that we cannot reuse
                                136                 :                :              * (this includes the case of an out-of-line in-memory datum).
                                137                 :                :              * Fetch it back (without decompression, unless we are forcing
                                138                 :                :              * PLAIN storage).  If necessary, we'll push it out as a new
                                139                 :                :              * external value below.
                                140                 :                :              */
                                141         [ +  + ]:          47346 :             if (VARATT_IS_EXTERNAL(new_value))
                                142                 :                :             {
                                143                 :            727 :                 ttc->ttc_attr[i].tai_oldexternal = new_value;
 2391 tgl@sss.pgh.pa.us         144         [ -  + ]:            727 :                 if (att->attstorage == TYPSTORAGE_PLAIN)
 2543 rhaas@postgresql.org      145                 :UBC           0 :                     new_value = detoast_attr(new_value);
                                146                 :                :                 else
 2543 rhaas@postgresql.org      147                 :CBC         727 :                     new_value = detoast_external_attr(new_value);
 2571                           148                 :            727 :                 ttc->ttc_values[i] = PointerGetDatum(new_value);
                                149                 :            727 :                 ttc->ttc_attr[i].tai_colflags |= TOASTCOL_NEEDS_FREE;
                                150                 :            727 :                 ttc->ttc_flags |= (TOAST_NEEDS_CHANGE | TOAST_NEEDS_FREE);
                                151                 :                :             }
                                152                 :                : 
                                153                 :                :             /*
                                154                 :                :              * Remember the size of this attribute
                                155                 :                :              */
                                156                 :          47346 :             ttc->ttc_attr[i].tai_size = VARSIZE_ANY(new_value);
                                157                 :                :         }
                                158                 :                :         else
                                159                 :                :         {
                                160                 :                :             /*
                                161                 :                :              * Not a varlena attribute, plain storage always
                                162                 :                :              */
                                163                 :         158811 :             ttc->ttc_attr[i].tai_colflags |= TOASTCOL_IGNORE;
                                164                 :                :         }
                                165                 :                :     }
                                166                 :          25015 : }
                                167                 :                : 
                                168                 :                : /*
                                169                 :                :  * Find the largest varlena attribute that satisfies certain criteria.
                                170                 :                :  *
                                171                 :                :  * The relevant column must not be marked TOASTCOL_IGNORE, and if the
                                172                 :                :  * for_compression flag is passed as true, it must also not be marked
                                173                 :                :  * TOASTCOL_INCOMPRESSIBLE.
                                174                 :                :  *
                                175                 :                :  * The column must have attstorage EXTERNAL or EXTENDED if check_main is
                                176                 :                :  * false, and must have attstorage MAIN if check_main is true.
                                177                 :                :  *
                                178                 :                :  * The column must be larger than the TOAST pointer that would replace it;
                                179                 :                :  * if not, no benefit is to be expected by compressing it.  Note that this
                                180                 :                :  * choice depends on the TOAST value type, oid or oid8.
                                181                 :                :  *
                                182                 :                :  * The return value is the index of the biggest suitable column, or
                                183                 :                :  * -1 if there is none.
                                184                 :                :  */
                                185                 :                : int
                                186                 :          30470 : toast_tuple_find_biggest_attribute(ToastTupleContext *ttc,
                                187                 :                :                                    bool for_compression, bool check_main)
                                188                 :                : {
                                189                 :          30470 :     TupleDesc   tupleDesc = ttc->ttc_rel->rd_att;
                                190                 :          30470 :     int         numAttrs = tupleDesc->natts;
                                191                 :          30470 :     int         biggest_attno = -1;
                                192                 :                :     int32       biggest_size;
                                193                 :          30470 :     int32       skip_colflags = TOASTCOL_IGNORE;
                                194                 :                :     int         i;
                                195                 :                : 
                                196                 :                :     /*
                                197                 :                :      * Size of the TOAST pointer this relation would use.  A relation without
                                198                 :                :      * a TOAST table cannot have any of its attributes moved out-of-line, but
                                199                 :                :      * it can still have some of them compressed.  Fall back to the oid size
                                200                 :                :      * in that case.
                                201                 :                :      */
    5 michael@paquier.xyz       202         [ +  + ]:GNC       30470 :     if (RelationGetToastChunkIdType(ttc->ttc_rel) == OID8OID)
                                203                 :             94 :         biggest_size = MAXALIGN(TOAST_OID8_POINTER_SIZE);
                                204                 :                :     else
                                205                 :          30376 :         biggest_size = MAXALIGN(TOAST_OID_POINTER_SIZE);
                                206                 :                : 
 2571 rhaas@postgresql.org      207         [ +  + ]:CBC       30470 :     if (for_compression)
                                208                 :          29311 :         skip_colflags |= TOASTCOL_INCOMPRESSIBLE;
                                209                 :                : 
                                210         [ +  + ]:         418958 :     for (i = 0; i < numAttrs; i++)
                                211                 :                :     {
                                212                 :         388488 :         Form_pg_attribute att = TupleDescAttr(tupleDesc, i);
                                213                 :                : 
                                214         [ +  + ]:         388488 :         if ((ttc->ttc_attr[i].tai_colflags & skip_colflags) != 0)
                                215                 :         319705 :             continue;
                                216         [ -  + ]:          68783 :         if (VARATT_IS_EXTERNAL(DatumGetPointer(ttc->ttc_values[i])))
 2391 tgl@sss.pgh.pa.us         217                 :UBC           0 :             continue;           /* can't happen, toast_action would be PLAIN */
 2571 rhaas@postgresql.org      218   [ +  +  +  + ]:CBC      133293 :         if (for_compression &&
                                219                 :          64510 :             VARATT_IS_COMPRESSED(DatumGetPointer(ttc->ttc_values[i])))
                                220                 :           9732 :             continue;
 2391 tgl@sss.pgh.pa.us         221   [ +  +  -  + ]:          59051 :         if (check_main && att->attstorage != TYPSTORAGE_MAIN)
 2571 rhaas@postgresql.org      222                 :UBC           0 :             continue;
 2391 tgl@sss.pgh.pa.us         223   [ +  +  +  + ]:CBC       59051 :         if (!check_main && att->attstorage != TYPSTORAGE_EXTENDED &&
                                224         [ +  + ]:           3287 :             att->attstorage != TYPSTORAGE_EXTERNAL)
 2571 rhaas@postgresql.org      225                 :             91 :             continue;
                                226                 :                : 
                                227         [ +  + ]:          58960 :         if (ttc->ttc_attr[i].tai_size > biggest_size)
                                228                 :                :         {
                                229                 :          39069 :             biggest_attno = i;
                                230                 :          39069 :             biggest_size = ttc->ttc_attr[i].tai_size;
                                231                 :                :         }
                                232                 :                :     }
                                233                 :                : 
                                234                 :          30470 :     return biggest_attno;
                                235                 :                : }
                                236                 :                : 
                                237                 :                : /*
                                238                 :                :  * Try compression for an attribute.
                                239                 :                :  *
                                240                 :                :  * If we find that the attribute is not compressible, mark it so.
                                241                 :                :  */
                                242                 :                : void
                                243                 :          24945 : toast_tuple_try_compression(ToastTupleContext *ttc, int attribute)
                                244                 :                : {
                                245                 :          24945 :     Datum      *value = &ttc->ttc_values[attribute];
                                246                 :                :     Datum       new_value;
                                247                 :          24945 :     ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
                                248                 :                : 
 2011                           249                 :          24945 :     new_value = toast_compress_datum(*value, attr->tai_compression);
                                250                 :                : 
 2571                           251         [ +  + ]:          24945 :     if (DatumGetPointer(new_value) != NULL)
                                252                 :                :     {
                                253                 :                :         /* successful compression */
                                254         [ +  + ]:          23874 :         if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
                                255                 :             66 :             pfree(DatumGetPointer(*value));
                                256                 :          23874 :         *value = new_value;
                                257                 :          23874 :         attr->tai_colflags |= TOASTCOL_NEEDS_FREE;
                                258                 :          23874 :         attr->tai_size = VARSIZE(DatumGetPointer(*value));
                                259                 :          23874 :         ttc->ttc_flags |= (TOAST_NEEDS_CHANGE | TOAST_NEEDS_FREE);
                                260                 :                :     }
                                261                 :                :     else
                                262                 :                :     {
                                263                 :                :         /* incompressible, ignore on subsequent compression passes */
                                264                 :           1071 :         attr->tai_colflags |= TOASTCOL_INCOMPRESSIBLE;
                                265                 :                :     }
                                266                 :          24945 : }
                                267                 :                : 
                                268                 :                : /*
                                269                 :                :  * Move an attribute to external storage.
                                270                 :                :  */
                                271                 :                : void
  172 alvherre@kurilemu.de      272                 :          11367 : toast_tuple_externalize(ToastTupleContext *ttc, int attribute, uint32 options)
                                273                 :                : {
 2571 rhaas@postgresql.org      274                 :          11367 :     Datum      *value = &ttc->ttc_values[attribute];
                                275                 :          11367 :     Datum       old_value = *value;
                                276                 :          11367 :     ToastAttrInfo *attr = &ttc->ttc_attr[attribute];
                                277                 :                : 
                                278                 :          11367 :     attr->tai_colflags |= TOASTCOL_IGNORE;
                                279                 :          11367 :     *value = toast_save_datum(ttc->ttc_rel, old_value, attr->tai_oldexternal,
                                280                 :                :                               options);
                                281         [ +  + ]:          11367 :     if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
                                282                 :           8349 :         pfree(DatumGetPointer(old_value));
                                283                 :          11367 :     attr->tai_colflags |= TOASTCOL_NEEDS_FREE;
                                284                 :          11367 :     ttc->ttc_flags |= (TOAST_NEEDS_CHANGE | TOAST_NEEDS_FREE);
                                285                 :          11367 : }
                                286                 :                : 
                                287                 :                : /*
                                288                 :                :  * Perform appropriate cleanup after one tuple has been subjected to TOAST.
                                289                 :                :  */
                                290                 :                : void
                                291                 :          25015 : toast_tuple_cleanup(ToastTupleContext *ttc)
                                292                 :                : {
                                293                 :          25015 :     TupleDesc   tupleDesc = ttc->ttc_rel->rd_att;
                                294                 :          25015 :     int         numAttrs = tupleDesc->natts;
                                295                 :                : 
                                296                 :                :     /*
                                297                 :                :      * Free allocated temp values
                                298                 :                :      */
                                299         [ +  + ]:          25015 :     if ((ttc->ttc_flags & TOAST_NEEDS_FREE) != 0)
                                300                 :                :     {
                                301                 :                :         int         i;
                                302                 :                : 
                                303         [ +  + ]:         255199 :         for (i = 0; i < numAttrs; i++)
                                304                 :                :         {
                                305                 :         230311 :             ToastAttrInfo *attr = &ttc->ttc_attr[i];
                                306                 :                : 
                                307         [ +  + ]:         230311 :             if ((attr->tai_colflags & TOASTCOL_NEEDS_FREE) != 0)
                                308                 :          27553 :                 pfree(DatumGetPointer(ttc->ttc_values[i]));
                                309                 :                :         }
                                310                 :                :     }
                                311                 :                : 
                                312                 :                :     /*
                                313                 :                :      * Delete external values from the old tuple
                                314                 :                :      */
                                315         [ +  + ]:          25015 :     if ((ttc->ttc_flags & TOAST_NEEDS_DELETE_OLD) != 0)
                                316                 :                :     {
                                317                 :                :         int         i;
                                318                 :                : 
                                319         [ +  + ]:          16524 :         for (i = 0; i < numAttrs; i++)
                                320                 :                :         {
                                321                 :          15906 :             ToastAttrInfo *attr = &ttc->ttc_attr[i];
                                322                 :                : 
                                323         [ +  + ]:          15906 :             if ((attr->tai_colflags & TOASTCOL_NEEDS_DELETE_OLD) != 0)
                                324                 :            730 :                 toast_delete_datum(ttc->ttc_rel, ttc->ttc_oldvalues[i], false);
                                325                 :                :         }
                                326                 :                :     }
                                327                 :          25015 : }
                                328                 :                : 
                                329                 :                : /*
                                330                 :                :  * Check for external stored attributes and delete them from the secondary
                                331                 :                :  * relation.
                                332                 :                :  */
                                333                 :                : void
 1076 peter@eisentraut.org      334                 :            530 : toast_delete_external(Relation rel, const Datum *values, const bool *isnull,
                                335                 :                :                       bool is_speculative)
                                336                 :                : {
 2571 rhaas@postgresql.org      337                 :            530 :     TupleDesc   tupleDesc = rel->rd_att;
                                338                 :            530 :     int         numAttrs = tupleDesc->natts;
                                339                 :                :     int         i;
                                340                 :                : 
                                341         [ +  + ]:           5128 :     for (i = 0; i < numAttrs; i++)
                                342                 :                :     {
  639 drowley@postgresql.o      343         [ +  + ]:           4598 :         if (TupleDescCompactAttr(tupleDesc, i)->attlen == -1)
                                344                 :                :         {
 2571 rhaas@postgresql.org      345                 :           1491 :             Datum       value = values[i];
                                346                 :                : 
                                347         [ +  + ]:           1491 :             if (isnull[i])
                                348                 :            423 :                 continue;
  411 peter@eisentraut.org      349         [ +  + ]:           1068 :             else if (VARATT_IS_EXTERNAL_ONDISK(DatumGetPointer(value)))
 2571 rhaas@postgresql.org      350                 :            542 :                 toast_delete_datum(rel, value, is_speculative);
                                351                 :                :         }
                                352                 :                :     }
                                353                 :            530 : }
        

Generated by: LCOV version 2.0-1