LCOV - differential code coverage report
Current view: top level - src/backend/access/common - detoast.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 85.6 % 195 167 28 31 136 23
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 9 9 7 2
Baseline: lcov-20261128-baseline Branches: 62.2 % 156 97 2 57 16 81
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 52 52 31 21
(360..) days: 80.4 % 143 115 28 115
Function coverage date bins:
(30,360] days: 100.0 % 7 7 5 2
(360..) days: 100.0 % 2 2 2
Branch coverage date bins:
(30,360] days: 90.0 % 20 18 2 16 2
(360..) days: 58.1 % 136 79 57 79

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * detoast.c
                                  4                 :                :  *    Retrieve compressed or external variable size attributes.
                                  5                 :                :  *
                                  6                 :                :  * Copyright (c) 2000-2026, PostgreSQL Global Development Group
                                  7                 :                :  *
                                  8                 :                :  * IDENTIFICATION
                                  9                 :                :  *    src/backend/access/common/detoast.c
                                 10                 :                :  *
                                 11                 :                :  *-------------------------------------------------------------------------
                                 12                 :                :  */
                                 13                 :                : 
                                 14                 :                : #include "postgres.h"
                                 15                 :                : 
                                 16                 :                : #include "access/detoast.h"
                                 17                 :                : #include "access/table.h"
                                 18                 :                : #include "access/tableam.h"
                                 19                 :                : #include "access/toast_compression.h"
                                 20                 :                : #include "access/toast_internals.h"
                                 21                 :                : #include "common/int.h"
                                 22                 :                : #include "common/pg_lzcompress.h"
                                 23                 :                : #include "utils/expandeddatum.h"
                                 24                 :                : #include "utils/memutils.h"
                                 25                 :                : #include "utils/rel.h"
                                 26                 :                : 
                                 27                 :                : static varlena *toast_fetch_datum(varlena *attr);
                                 28                 :                : static varlena *toast_fetch_datum_slice(varlena *attr,
                                 29                 :                :                                         int32 sliceoffset,
                                 30                 :                :                                         int32 slicelength);
                                 31                 :                : static varlena *toast_decompress_datum(varlena *attr);
                                 32                 :                : static varlena *toast_decompress_datum_slice(varlena *attr, int32 slicelength);
                                 33                 :                : 
                                 34                 :                : /* ----------
                                 35                 :                :  * detoast_external_attr -
                                 36                 :                :  *
                                 37                 :                :  *  Public entry point to get back a toasted value from
                                 38                 :                :  *  external source (possibly still in compressed format).
                                 39                 :                :  *
                                 40                 :                :  * This will return a datum that contains all the data internally, ie, not
                                 41                 :                :  * relying on external storage or memory, but it can still be compressed or
                                 42                 :                :  * have a short header.  Note some callers assume that if the input is an
                                 43                 :                :  * EXTERNAL datum, the result will be a pfree'able chunk.
                                 44                 :                :  * ----------
                                 45                 :                :  */
                                 46                 :                : varlena *
  290 michael@paquier.xyz        47                 :CBC       11578 : detoast_external_attr(varlena *attr)
                                 48                 :                : {
                                 49                 :                :     varlena    *result;
                                 50                 :                : 
                                 51                 :                :     /*
                                 52                 :                :      * It's critical that the stored varlena mask does not allow for toast
                                 53                 :                :      * objects bigger than what can be allocated with palloc; otherwise,
                                 54                 :                :      * buffer overflows can be caused by adversarial toast objects.
                                 55                 :                :      */
                                 56                 :                :     StaticAssertExpr(VARLENA_EXTSIZE_MASK <= MaxAllocSize,
                                 57                 :                :                      "VARLENA_EXTSIZE_MASK is too big");
                                 58                 :                : 
 2700 rhaas@postgresql.org       59         [ +  + ]:          11578 :     if (VARATT_IS_EXTERNAL_ONDISK(attr))
                                 60                 :                :     {
                                 61                 :                :         /*
                                 62                 :                :          * This is an external stored plain value
                                 63                 :                :          */
                                 64                 :           5256 :         result = toast_fetch_datum(attr);
                                 65                 :                :     }
                                 66         [ +  + ]:           6322 :     else if (VARATT_IS_EXTERNAL_INDIRECT(attr))
                                 67                 :                :     {
                                 68                 :                :         /*
                                 69                 :                :          * This is an indirect pointer --- dereference it
                                 70                 :                :          */
                                 71                 :                :         varatt_indirect redirect;
                                 72                 :                : 
                                 73   [ -  +  -  + ]:            359 :         VARATT_EXTERNAL_GET_POINTER(redirect, attr);
  290 michael@paquier.xyz        74                 :            359 :         attr = (varlena *) redirect.pointer;
                                 75                 :                : 
                                 76                 :                :         /* nested indirect Datums aren't allowed */
 2700 rhaas@postgresql.org       77         [ -  + ]:            359 :         Assert(!VARATT_IS_EXTERNAL_INDIRECT(attr));
                                 78                 :                : 
                                 79                 :                :         /* recurse if value is still external in some other way */
                                 80         [ -  + ]:            359 :         if (VARATT_IS_EXTERNAL(attr))
 2612 rhaas@postgresql.org       81                 :UBC           0 :             return detoast_external_attr(attr);
                                 82                 :                : 
                                 83                 :                :         /*
                                 84                 :                :          * Copy into the caller's memory context, in case caller tries to
                                 85                 :                :          * pfree the result.
                                 86                 :                :          */
  290 michael@paquier.xyz        87                 :CBC         359 :         result = (varlena *) palloc(VARSIZE_ANY(attr));
 2700 rhaas@postgresql.org       88                 :            359 :         memcpy(result, attr, VARSIZE_ANY(attr));
                                 89                 :                :     }
                                 90         [ +  - ]:           5963 :     else if (VARATT_IS_EXTERNAL_EXPANDED(attr))
                                 91                 :                :     {
                                 92                 :                :         /*
                                 93                 :                :          * This is an expanded-object pointer --- get flat format
                                 94                 :                :          */
                                 95                 :                :         ExpandedObjectHeader *eoh;
                                 96                 :                :         Size        resultsize;
                                 97                 :                : 
                                 98                 :           5963 :         eoh = DatumGetEOHP(PointerGetDatum(attr));
                                 99                 :           5963 :         resultsize = EOH_get_flat_size(eoh);
  290 michael@paquier.xyz       100                 :           5963 :         result = (varlena *) palloc(resultsize);
  730 peter@eisentraut.org      101                 :           5963 :         EOH_flatten_into(eoh, result, resultsize);
                                102                 :                :     }
                                103                 :                :     else
                                104                 :                :     {
                                105                 :                :         /*
                                106                 :                :          * This is a plain value inside of the main tuple - why am I called?
                                107                 :                :          */
 2700 rhaas@postgresql.org      108                 :UBC           0 :         result = attr;
                                109                 :                :     }
                                110                 :                : 
 2700 rhaas@postgresql.org      111                 :CBC       11578 :     return result;
                                112                 :                : }
                                113                 :                : 
                                114                 :                : 
                                115                 :                : /* ----------
                                116                 :                :  * detoast_attr -
                                117                 :                :  *
                                118                 :                :  *  Public entry point to get back a toasted value from compression
                                119                 :                :  *  or external storage.  The result is always non-extended varlena form.
                                120                 :                :  *
                                121                 :                :  * Note some callers assume that if the input is an EXTERNAL or COMPRESSED
                                122                 :                :  * datum, the result will be a pfree'able chunk.
                                123                 :                :  * ----------
                                124                 :                :  */
                                125                 :                : varlena *
  290 michael@paquier.xyz       126                 :       34377328 : detoast_attr(varlena *attr)
                                127                 :                : {
 2700 rhaas@postgresql.org      128         [ +  + ]:       34377328 :     if (VARATT_IS_EXTERNAL_ONDISK(attr))
                                129                 :                :     {
                                130                 :                :         /*
                                131                 :                :          * This is an externally stored datum --- fetch it back from there
                                132                 :                :          */
                                133                 :          12715 :         attr = toast_fetch_datum(attr);
                                134                 :                :         /* If it's compressed, decompress it */
                                135         [ +  + ]:          12715 :         if (VARATT_IS_COMPRESSED(attr))
                                136                 :                :         {
  290 michael@paquier.xyz       137                 :          12600 :             varlena    *tmp = attr;
                                138                 :                : 
 2700 rhaas@postgresql.org      139                 :          12600 :             attr = toast_decompress_datum(tmp);
                                140                 :          12600 :             pfree(tmp);
                                141                 :                :         }
                                142                 :                :     }
                                143         [ +  + ]:       34364613 :     else if (VARATT_IS_EXTERNAL_INDIRECT(attr))
                                144                 :                :     {
                                145                 :                :         /*
                                146                 :                :          * This is an indirect pointer --- dereference it
                                147                 :                :          */
                                148                 :                :         varatt_indirect redirect;
                                149                 :                : 
                                150   [ -  +  -  + ]:             69 :         VARATT_EXTERNAL_GET_POINTER(redirect, attr);
  290 michael@paquier.xyz       151                 :             69 :         attr = (varlena *) redirect.pointer;
                                152                 :                : 
                                153                 :                :         /* nested indirect Datums aren't allowed */
 2700 rhaas@postgresql.org      154         [ -  + ]:             69 :         Assert(!VARATT_IS_EXTERNAL_INDIRECT(attr));
                                155                 :                : 
                                156                 :                :         /* recurse in case value is still extended in some other way */
 2612                           157                 :             69 :         attr = detoast_attr(attr);
                                158                 :                : 
                                159                 :                :         /* if it isn't, we'd better copy it */
  290 michael@paquier.xyz       160         [ +  + ]:             69 :         if (attr == (varlena *) redirect.pointer)
                                161                 :                :         {
                                162                 :                :             varlena    *result;
                                163                 :                : 
                                164                 :             17 :             result = (varlena *) palloc(VARSIZE_ANY(attr));
 2700 rhaas@postgresql.org      165                 :             17 :             memcpy(result, attr, VARSIZE_ANY(attr));
                                166                 :             17 :             attr = result;
                                167                 :                :         }
                                168                 :                :     }
                                169         [ +  + ]:       34364544 :     else if (VARATT_IS_EXTERNAL_EXPANDED(attr))
                                170                 :                :     {
                                171                 :                :         /*
                                172                 :                :          * This is an expanded-object pointer --- get flat format
                                173                 :                :          */
 2612                           174                 :           5963 :         attr = detoast_external_attr(attr);
                                175                 :                :         /* flatteners are not allowed to produce compressed/short output */
 2700                           176         [ -  + ]:           5963 :         Assert(!VARATT_IS_EXTENDED(attr));
                                177                 :                :     }
                                178         [ +  + ]:       34358581 :     else if (VARATT_IS_COMPRESSED(attr))
                                179                 :                :     {
                                180                 :                :         /*
                                181                 :                :          * This is a compressed value inside of the main tuple
                                182                 :                :          */
                                183                 :          92196 :         attr = toast_decompress_datum(attr);
                                184                 :                :     }
                                185         [ +  + ]:       34266385 :     else if (VARATT_IS_SHORT(attr))
                                186                 :                :     {
                                187                 :                :         /*
                                188                 :                :          * This is a short-header varlena --- convert to 4-byte header format
                                189                 :                :          */
                                190                 :       34266368 :         Size        data_size = VARSIZE_SHORT(attr) - VARHDRSZ_SHORT;
                                191                 :       34266368 :         Size        new_size = data_size + VARHDRSZ;
                                192                 :                :         varlena    *new_attr;
                                193                 :                : 
  290 michael@paquier.xyz       194                 :       34266368 :         new_attr = (varlena *) palloc(new_size);
 2700 rhaas@postgresql.org      195                 :       34266368 :         SET_VARSIZE(new_attr, new_size);
                                196                 :       34266368 :         memcpy(VARDATA(new_attr), VARDATA_SHORT(attr), data_size);
                                197                 :       34266368 :         attr = new_attr;
                                198                 :                :     }
                                199                 :                : 
                                200                 :       34377328 :     return attr;
                                201                 :                : }
                                202                 :                : 
                                203                 :                : 
                                204                 :                : /* ----------
                                205                 :                :  * detoast_attr_slice -
                                206                 :                :  *
                                207                 :                :  *      Public entry point to get back part of a toasted value
                                208                 :                :  *      from compression or external storage.
                                209                 :                :  *
                                210                 :                :  * sliceoffset is where to start (zero or more)
                                211                 :                :  * If slicelength < 0, return everything beyond sliceoffset
                                212                 :                :  * ----------
                                213                 :                :  */
                                214                 :                : varlena *
  290 michael@paquier.xyz       215                 :           2618 : detoast_attr_slice(varlena *attr,
                                216                 :                :                    int32 sliceoffset, int32 slicelength)
                                217                 :                : {
                                218                 :                :     varlena    *preslice;
                                219                 :                :     varlena    *result;
                                220                 :                :     char       *attrdata;
                                221                 :                :     int32       slicelimit;
                                222                 :                :     int32       attrsize;
                                223                 :                : 
 2154 tgl@sss.pgh.pa.us         224         [ -  + ]:           2618 :     if (sliceoffset < 0)
 2154 tgl@sss.pgh.pa.us         225         [ #  # ]:UBC           0 :         elog(ERROR, "invalid sliceoffset: %d", sliceoffset);
                                226                 :                : 
                                227                 :                :     /*
                                228                 :                :      * Compute slicelimit = offset + length, or -1 if we must fetch all of the
                                229                 :                :      * value.  In case of integer overflow, we must fetch all.
                                230                 :                :      */
 2154 tgl@sss.pgh.pa.us         231         [ +  + ]:CBC        2618 :     if (slicelength < 0)
                                232                 :           2129 :         slicelimit = -1;
                                233         [ -  + ]:            489 :     else if (pg_add_s32_overflow(sliceoffset, slicelength, &slicelimit))
 2154 tgl@sss.pgh.pa.us         234                 :UBC           0 :         slicelength = slicelimit = -1;
                                235                 :                : 
 2700 rhaas@postgresql.org      236         [ +  + ]:CBC        2618 :     if (VARATT_IS_EXTERNAL_ONDISK(attr))
                                237                 :                :     {
                                238                 :                :         toast_external_data toast_ext_data;
                                239                 :                :         int32       extsize;
                                240                 :                :         uint32      compress_method;
                                241                 :                :         bool        is_compressed;
                                242                 :                : 
   74 michael@paquier.xyz       243                 :GNC         283 :         toast_external_info_get(attr, &toast_ext_data);
                                244                 :            283 :         extsize = VARATT_EXTINFO_GET_EXTSIZE(toast_ext_data.extinfo);
                                245                 :            283 :         compress_method = VARATT_EXTINFO_GET_COMPRESS_METHOD(toast_ext_data.extinfo);
                                246                 :            283 :         is_compressed = VARATT_EXTINFO_IS_COMPRESSED(toast_ext_data.extinfo, toast_ext_data.rawsize);
                                247                 :                : 
                                248                 :                :         /* fast path for non-compressed external datums */
                                249         [ +  + ]:            283 :         if (!is_compressed)
 2700 rhaas@postgresql.org      250                 :CBC         175 :             return toast_fetch_datum_slice(attr, sliceoffset, slicelength);
                                251                 :                : 
                                252                 :                :         /*
                                253                 :                :          * For compressed values, we need to fetch enough slices to decompress
                                254                 :                :          * at least the requested part (when a prefix is requested).
                                255                 :                :          * Otherwise, just fetch all slices.
                                256                 :                :          */
 2154 tgl@sss.pgh.pa.us         257         [ +  - ]:            108 :         if (slicelimit >= 0)
                                258                 :                :         {
   74 michael@paquier.xyz       259                 :GNC         108 :             int32       max_size = extsize;
                                260                 :                : 
                                261                 :                :             /*
                                262                 :                :              * Determine maximum amount of compressed data needed for a prefix
                                263                 :                :              * of a given length (after decompression).
                                264                 :                :              *
                                265                 :                :              * At least for now, if it's LZ4 data, we'll have to fetch the
                                266                 :                :              * whole thing, because there doesn't seem to be an API call to
                                267                 :                :              * determine how much compressed data we need to be sure of being
                                268                 :                :              * able to decompress the required slice.
                                269                 :                :              */
                                270         [ +  + ]:            108 :             if (compress_method == TOAST_PGLZ_COMPRESSION_ID)
 2080 rhaas@postgresql.org      271                 :CBC           8 :                 max_size = pglz_maximum_compressed_size(slicelimit, max_size);
                                272                 :                : 
                                273                 :                :             /*
                                274                 :                :              * Fetch enough compressed slices (compressed marker will get set
                                275                 :                :              * automatically).
                                276                 :                :              */
 2615 tomas.vondra@postgre      277                 :            108 :             preslice = toast_fetch_datum_slice(attr, 0, max_size);
                                278                 :                :         }
                                279                 :                :         else
 2615 tomas.vondra@postgre      280                 :UBC           0 :             preslice = toast_fetch_datum(attr);
                                281                 :                :     }
 2700 rhaas@postgresql.org      282         [ -  + ]:CBC        2335 :     else if (VARATT_IS_EXTERNAL_INDIRECT(attr))
                                283                 :                :     {
                                284                 :                :         varatt_indirect redirect;
                                285                 :                : 
 2700 rhaas@postgresql.org      286   [ #  #  #  # ]:UBC           0 :         VARATT_EXTERNAL_GET_POINTER(redirect, attr);
                                287                 :                : 
                                288                 :                :         /* nested indirect Datums aren't allowed */
                                289         [ #  # ]:              0 :         Assert(!VARATT_IS_EXTERNAL_INDIRECT(redirect.pointer));
                                290                 :                : 
 2612                           291                 :              0 :         return detoast_attr_slice(redirect.pointer,
                                292                 :                :                                   sliceoffset, slicelength);
                                293                 :                :     }
 2700 rhaas@postgresql.org      294         [ -  + ]:CBC        2335 :     else if (VARATT_IS_EXTERNAL_EXPANDED(attr))
                                295                 :                :     {
                                296                 :                :         /* pass it off to detoast_external_attr to flatten */
 2612 rhaas@postgresql.org      297                 :UBC           0 :         preslice = detoast_external_attr(attr);
                                298                 :                :     }
                                299                 :                :     else
 2700 rhaas@postgresql.org      300                 :CBC        2335 :         preslice = attr;
                                301                 :                : 
                                302         [ -  + ]:           2443 :     Assert(!VARATT_IS_EXTERNAL(preslice));
                                303                 :                : 
                                304         [ +  + ]:           2443 :     if (VARATT_IS_COMPRESSED(preslice))
                                305                 :                :     {
  290 michael@paquier.xyz       306                 :            300 :         varlena    *tmp = preslice;
                                307                 :                : 
                                308                 :                :         /* Decompress enough to encompass the slice and the offset */
 2154 tgl@sss.pgh.pa.us         309         [ +  + ]:            300 :         if (slicelimit >= 0)
                                310                 :            252 :             preslice = toast_decompress_datum_slice(tmp, slicelimit);
                                311                 :                :         else
 2700 rhaas@postgresql.org      312                 :             48 :             preslice = toast_decompress_datum(tmp);
                                313                 :                : 
                                314         [ +  + ]:            300 :         if (tmp != attr)
                                315                 :            108 :             pfree(tmp);
                                316                 :                :     }
                                317                 :                : 
                                318         [ +  + ]:           2443 :     if (VARATT_IS_SHORT(preslice))
                                319                 :                :     {
                                320                 :           1542 :         attrdata = VARDATA_SHORT(preslice);
                                321                 :           1542 :         attrsize = VARSIZE_SHORT(preslice) - VARHDRSZ_SHORT;
                                322                 :                :     }
                                323                 :                :     else
                                324                 :                :     {
                                325                 :            901 :         attrdata = VARDATA(preslice);
                                326                 :            901 :         attrsize = VARSIZE(preslice) - VARHDRSZ;
                                327                 :                :     }
                                328                 :                : 
                                329                 :                :     /* slicing of datum for compressed cases and plain value */
                                330                 :                : 
                                331         [ +  + ]:           2443 :     if (sliceoffset >= attrsize)
                                332                 :                :     {
                                333                 :             15 :         sliceoffset = 0;
                                334                 :             15 :         slicelength = 0;
                                335                 :                :     }
 2154 tgl@sss.pgh.pa.us         336   [ +  +  +  + ]:           2428 :     else if (slicelength < 0 || slicelimit > attrsize)
 2700 rhaas@postgresql.org      337                 :           2115 :         slicelength = attrsize - sliceoffset;
                                338                 :                : 
  290 michael@paquier.xyz       339                 :           2443 :     result = (varlena *) palloc(slicelength + VARHDRSZ);
 2700 rhaas@postgresql.org      340                 :           2443 :     SET_VARSIZE(result, slicelength + VARHDRSZ);
                                341                 :                : 
                                342                 :           2443 :     memcpy(VARDATA(result), attrdata + sliceoffset, slicelength);
                                343                 :                : 
                                344         [ +  + ]:           2443 :     if (preslice != attr)
                                345                 :            300 :         pfree(preslice);
                                346                 :                : 
                                347                 :           2443 :     return result;
                                348                 :                : }
                                349                 :                : 
                                350                 :                : /* ----------
                                351                 :                :  * toast_fetch_datum -
                                352                 :                :  *
                                353                 :                :  *  Reconstruct an in memory Datum from the chunks saved
                                354                 :                :  *  in the toast relation
                                355                 :                :  * ----------
                                356                 :                :  */
                                357                 :                : static varlena *
  290 michael@paquier.xyz       358                 :          17971 : toast_fetch_datum(varlena *attr)
                                359                 :                : {
                                360                 :                :     Relation    toastrel;
                                361                 :                :     varlena    *result;
                                362                 :                :     toast_external_data toast_ext_data;
                                363                 :                :     int32       attrsize;
                                364                 :                :     Oid         toastrelid;
                                365                 :                :     Oid8        valueid;
                                366                 :                : 
 2700 rhaas@postgresql.org      367         [ -  + ]:          17971 :     if (!VARATT_IS_EXTERNAL_ONDISK(attr))
 2700 rhaas@postgresql.org      368         [ #  # ]:UBC           0 :         elog(ERROR, "toast_fetch_datum shouldn't be called for non-ondisk datums");
                                369                 :                : 
   74 michael@paquier.xyz       370                 :GNC       17971 :     toast_external_info_get(attr, &toast_ext_data);
                                371                 :          17971 :     attrsize = VARATT_EXTINFO_GET_EXTSIZE(toast_ext_data.extinfo);
                                372                 :          17971 :     toastrelid = toast_ext_data.toastrelid;
                                373                 :          17971 :     valueid = toast_ext_data.valueid;
                                374                 :                : 
  290 michael@paquier.xyz       375                 :CBC       17971 :     result = (varlena *) palloc(attrsize + VARHDRSZ);
                                376                 :                : 
   74 michael@paquier.xyz       377         [ +  + ]:GNC       17971 :     if (VARATT_EXTINFO_IS_COMPRESSED(toast_ext_data.extinfo, toast_ext_data.rawsize))
 2563 rhaas@postgresql.org      378                 :CBC       17067 :         SET_VARSIZE_COMPRESSED(result, attrsize + VARHDRSZ);
                                379                 :                :     else
                                380                 :            904 :         SET_VARSIZE(result, attrsize + VARHDRSZ);
                                381                 :                : 
 2537                           382         [ -  + ]:          17971 :     if (attrsize == 0)
 2389 tgl@sss.pgh.pa.us         383                 :UBC           0 :         return result;          /* Probably shouldn't happen, but just in
                                384                 :                :                                  * case. */
                                385                 :                : 
                                386                 :                :     /*
                                387                 :                :      * Open the toast relation and its indexes
                                388                 :                :      */
   74 michael@paquier.xyz       389                 :GNC       17971 :     toastrel = table_open(toastrelid, AccessShareLock);
                                390                 :                : 
                                391                 :                :     /* Fetch all chunks */
                                392                 :          17971 :     table_relation_fetch_toast_slice(toastrel, valueid,
                                393                 :                :                                      attrsize, 0, attrsize, result);
                                394                 :                : 
                                395                 :                :     /* Close toast table */
 2700 rhaas@postgresql.org      396                 :CBC       17971 :     table_close(toastrel, AccessShareLock);
                                397                 :                : 
                                398                 :          17971 :     return result;
                                399                 :                : }
                                400                 :                : 
                                401                 :                : /* ----------
                                402                 :                :  * toast_fetch_datum_slice -
                                403                 :                :  *
                                404                 :                :  *  Reconstruct a segment of a Datum from the chunks saved
                                405                 :                :  *  in the toast relation
                                406                 :                :  *
                                407                 :                :  *  Note that this function supports non-compressed external datums
                                408                 :                :  *  and compressed external datums (in which case the requested slice
                                409                 :                :  *  has to be a prefix, i.e. sliceoffset has to be 0).
                                410                 :                :  * ----------
                                411                 :                :  */
                                412                 :                : static varlena *
  290 michael@paquier.xyz       413                 :            283 : toast_fetch_datum_slice(varlena *attr, int32 sliceoffset,
                                414                 :                :                         int32 slicelength)
                                415                 :                : {
                                416                 :                :     Relation    toastrel;
                                417                 :                :     varlena    *result;
                                418                 :                :     toast_external_data toast_ext_data;
                                419                 :                :     int32       attrsize;
                                420                 :                :     Oid         toastrelid;
                                421                 :                :     Oid8        valueid;
                                422                 :                :     bool        is_compressed;
                                423                 :                : 
 2700 rhaas@postgresql.org      424         [ -  + ]:            283 :     if (!VARATT_IS_EXTERNAL_ONDISK(attr))
 2700 rhaas@postgresql.org      425         [ #  # ]:UBC           0 :         elog(ERROR, "toast_fetch_datum_slice shouldn't be called for non-ondisk datums");
                                426                 :                : 
   74 michael@paquier.xyz       427                 :GNC         283 :     toast_external_info_get(attr, &toast_ext_data);
                                428                 :            283 :     attrsize = VARATT_EXTINFO_GET_EXTSIZE(toast_ext_data.extinfo);
                                429                 :            283 :     toastrelid = toast_ext_data.toastrelid;
                                430                 :            283 :     valueid = toast_ext_data.valueid;
                                431                 :            283 :     is_compressed = VARATT_EXTINFO_IS_COMPRESSED(toast_ext_data.extinfo, toast_ext_data.rawsize);
                                432                 :                : 
                                433                 :                :     /*
                                434                 :                :      * It's nonsense to fetch slices of a compressed datum unless when it's a
                                435                 :                :      * prefix -- this isn't lo_* we can't return a compressed datum which is
                                436                 :                :      * meaningful to toast later.
                                437                 :                :      */
                                438   [ +  +  -  + ]:            283 :     Assert(!is_compressed || 0 == sliceoffset);
                                439                 :                : 
 2700 rhaas@postgresql.org      440         [ -  + ]:CBC         283 :     if (sliceoffset >= attrsize)
                                441                 :                :     {
 2700 rhaas@postgresql.org      442                 :UBC           0 :         sliceoffset = 0;
 2563                           443                 :              0 :         slicelength = 0;
                                444                 :                :     }
                                445                 :                : 
                                446                 :                :     /*
                                447                 :                :      * When fetching a prefix of a compressed external datum, account for the
                                448                 :                :      * space required by va_tcinfo, which is stored at the beginning as an
                                449                 :                :      * int32 value.
                                450                 :                :      */
   74 michael@paquier.xyz       451   [ +  +  +  - ]:GNC         283 :     if (is_compressed && slicelength > 0)
 2563 rhaas@postgresql.org      452                 :CBC         108 :         slicelength = slicelength + sizeof(int32);
                                453                 :                : 
                                454                 :                :     /*
                                455                 :                :      * Adjust length request if needed.  (Note: our sole caller,
                                456                 :                :      * detoast_attr_slice, protects us against sliceoffset + slicelength
                                457                 :                :      * overflowing.)
                                458                 :                :      */
                                459   [ +  +  +  + ]:            283 :     if (((sliceoffset + slicelength) > attrsize) || slicelength < 0)
                                460                 :            212 :         slicelength = attrsize - sliceoffset;
                                461                 :                : 
  290 michael@paquier.xyz       462                 :            283 :     result = (varlena *) palloc(slicelength + VARHDRSZ);
                                463                 :                : 
   74 michael@paquier.xyz       464         [ +  + ]:GNC         283 :     if (is_compressed)
 2563 rhaas@postgresql.org      465                 :CBC         108 :         SET_VARSIZE_COMPRESSED(result, slicelength + VARHDRSZ);
                                466                 :                :     else
                                467                 :            175 :         SET_VARSIZE(result, slicelength + VARHDRSZ);
                                468                 :                : 
                                469         [ -  + ]:            283 :     if (slicelength == 0)
 2700 rhaas@postgresql.org      470                 :UBC           0 :         return result;          /* Can save a lot of work at this point! */
                                471                 :                : 
                                472                 :                :     /* Open the toast relation */
   74 michael@paquier.xyz       473                 :GNC         283 :     toastrel = table_open(toastrelid, AccessShareLock);
                                474                 :                : 
                                475                 :                :     /* Fetch all chunks */
                                476                 :            283 :     table_relation_fetch_toast_slice(toastrel, valueid,
                                477                 :                :                                      attrsize, sliceoffset, slicelength,
                                478                 :                :                                      result);
                                479                 :                : 
                                480                 :                :     /* Close toast table */
 2537 rhaas@postgresql.org      481                 :CBC         283 :     table_close(toastrel, AccessShareLock);
                                482                 :                : 
                                483                 :            283 :     return result;
                                484                 :                : }
                                485                 :                : 
                                486                 :                : /* ----------
                                487                 :                :  * toast_decompress_datum -
                                488                 :                :  *
                                489                 :                :  * Decompress a compressed version of a varlena datum
                                490                 :                :  */
                                491                 :                : static varlena *
  290 michael@paquier.xyz       492                 :         104908 : toast_decompress_datum(varlena *attr)
                                493                 :                : {
                                494                 :                :     ToastCompressionId cmid;
                                495                 :                : 
 2700 rhaas@postgresql.org      496         [ -  + ]:         104908 :     Assert(VARATT_IS_COMPRESSED(attr));
                                497                 :                : 
                                498                 :                :     /*
                                499                 :                :      * Fetch the compression method id stored in the compression header and
                                500                 :                :      * decompress the data using the appropriate decompression routine.
                                501                 :                :      */
   64 michael@paquier.xyz       502                 :GNC      104908 :     cmid = VARDATA_COMPRESSED_GET_COMPRESS_METHOD(attr);
 2080 rhaas@postgresql.org      503      [ +  +  - ]:CBC      104908 :     switch (cmid)
                                504                 :                :     {
                                505                 :            383 :         case TOAST_PGLZ_COMPRESSION_ID:
                                506                 :            383 :             return pglz_decompress_datum(attr);
                                507                 :         104525 :         case TOAST_LZ4_COMPRESSION_ID:
                                508                 :         104525 :             return lz4_decompress_datum(attr);
 2080 rhaas@postgresql.org      509                 :UBC           0 :         default:
                                510         [ #  # ]:              0 :             elog(ERROR, "invalid compression method id %d", cmid);
                                511                 :                :             return NULL;        /* keep compiler quiet */
                                512                 :                :     }
                                513                 :                : }
                                514                 :                : 
                                515                 :                : 
                                516                 :                : /* ----------
                                517                 :                :  * toast_decompress_datum_slice -
                                518                 :                :  *
                                519                 :                :  * Decompress the front of a compressed version of a varlena datum.
                                520                 :                :  * offset handling happens in detoast_attr_slice.
                                521                 :                :  * Here we just decompress a slice from the front.
                                522                 :                :  */
                                523                 :                : static varlena *
  290 michael@paquier.xyz       524                 :CBC         252 : toast_decompress_datum_slice(varlena *attr, int32 slicelength)
                                525                 :                : {
                                526                 :                :     ToastCompressionId cmid;
                                527                 :                : 
 2700 rhaas@postgresql.org      528         [ -  + ]:            252 :     Assert(VARATT_IS_COMPRESSED(attr));
                                529                 :                : 
                                530                 :                :     /*
                                531                 :                :      * Some callers may pass a slicelength that's more than the actual
                                532                 :                :      * decompressed size.  If so, just decompress normally.  This avoids
                                533                 :                :      * possibly allocating a larger-than-necessary result object, and may be
                                534                 :                :      * faster and/or more robust as well.  Notably, some versions of liblz4
                                535                 :                :      * have been seen to give wrong results if passed an output size that is
                                536                 :                :      * more than the data's true decompressed size.
                                537                 :                :      */
   64 michael@paquier.xyz       538         [ +  + ]:GNC         252 :     if ((uint32) slicelength >= VARDATA_COMPRESSED_GET_EXTSIZE(attr))
 2077 tgl@sss.pgh.pa.us         539                 :CBC          64 :         return toast_decompress_datum(attr);
                                540                 :                : 
                                541                 :                :     /*
                                542                 :                :      * Fetch the compression method id stored in the compression header and
                                543                 :                :      * decompress the data slice using the appropriate decompression routine.
                                544                 :                :      */
   64 michael@paquier.xyz       545                 :GNC         188 :     cmid = VARDATA_COMPRESSED_GET_COMPRESS_METHOD(attr);
 2080 rhaas@postgresql.org      546      [ +  +  - ]:CBC         188 :     switch (cmid)
                                547                 :                :     {
                                548                 :             24 :         case TOAST_PGLZ_COMPRESSION_ID:
                                549                 :             24 :             return pglz_decompress_datum_slice(attr, slicelength);
                                550                 :            164 :         case TOAST_LZ4_COMPRESSION_ID:
                                551                 :            164 :             return lz4_decompress_datum_slice(attr, slicelength);
 2080 rhaas@postgresql.org      552                 :UBC           0 :         default:
                                553         [ #  # ]:              0 :             elog(ERROR, "invalid compression method id %d", cmid);
                                554                 :                :             return NULL;        /* keep compiler quiet */
                                555                 :                :     }
                                556                 :                : }
                                557                 :                : 
                                558                 :                : /* ----------
                                559                 :                :  * toast_raw_datum_size -
                                560                 :                :  *
                                561                 :                :  *  Return the raw (detoasted) size of a varlena datum
                                562                 :                :  *  (including the VARHDRSZ header)
                                563                 :                :  * ----------
                                564                 :                :  */
                                565                 :                : Size
 2700 rhaas@postgresql.org      566                 :CBC    19814148 : toast_raw_datum_size(Datum value)
                                567                 :                : {
  290 michael@paquier.xyz       568                 :       19814148 :     varlena    *attr = (varlena *) DatumGetPointer(value);
                                569                 :                :     Size        result;
                                570                 :                : 
 2700 rhaas@postgresql.org      571         [ +  + ]:       19814148 :     if (VARATT_IS_EXTERNAL_ONDISK(attr))
                                572                 :                :     {
                                573                 :                :         /* va_rawsize is the size of the original datum -- including header */
                                574                 :                :         toast_external_data toast_ext_data;
                                575                 :                : 
   74 michael@paquier.xyz       576                 :GNC       12504 :         toast_external_info_get(attr, &toast_ext_data);
                                577                 :          12504 :         result = toast_ext_data.rawsize;
                                578                 :                :     }
 2700 rhaas@postgresql.org      579         [ -  + ]:CBC    19801644 :     else if (VARATT_IS_EXTERNAL_INDIRECT(attr))
                                580                 :                :     {
                                581                 :                :         varatt_indirect toast_pointer;
                                582                 :                : 
 2700 rhaas@postgresql.org      583   [ #  #  #  # ]:UBC           0 :         VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
                                584                 :                : 
                                585                 :                :         /* nested indirect Datums aren't allowed */
                                586         [ #  # ]:              0 :         Assert(!VARATT_IS_EXTERNAL_INDIRECT(toast_pointer.pointer));
                                587                 :                : 
                                588                 :              0 :         return toast_raw_datum_size(PointerGetDatum(toast_pointer.pointer));
                                589                 :                :     }
 2700 rhaas@postgresql.org      590         [ -  + ]:CBC    19801644 :     else if (VARATT_IS_EXTERNAL_EXPANDED(attr))
                                591                 :                :     {
 2700 rhaas@postgresql.org      592                 :UBC           0 :         result = EOH_get_flat_size(DatumGetEOHP(value));
                                593                 :                :     }
 2700 rhaas@postgresql.org      594         [ +  + ]:CBC    19801644 :     else if (VARATT_IS_COMPRESSED(attr))
                                595                 :                :     {
                                596                 :                :         /* here, va_rawsize is just the payload size */
 2077 tgl@sss.pgh.pa.us         597                 :          11086 :         result = VARDATA_COMPRESSED_GET_EXTSIZE(attr) + VARHDRSZ;
                                598                 :                :     }
 2700 rhaas@postgresql.org      599         [ +  + ]:       19790558 :     else if (VARATT_IS_SHORT(attr))
                                600                 :                :     {
                                601                 :                :         /*
                                602                 :                :          * we have to normalize the header length to VARHDRSZ or else the
                                603                 :                :          * callers of this function will be confused.
                                604                 :                :          */
                                605                 :       12173004 :         result = VARSIZE_SHORT(attr) - VARHDRSZ_SHORT + VARHDRSZ;
                                606                 :                :     }
                                607                 :                :     else
                                608                 :                :     {
                                609                 :                :         /* plain untoasted datum */
                                610                 :        7617554 :         result = VARSIZE(attr);
                                611                 :                :     }
                                612                 :       19814148 :     return result;
                                613                 :                : }
                                614                 :                : 
                                615                 :                : /* ----------
                                616                 :                :  * toast_datum_size
                                617                 :                :  *
                                618                 :                :  *  Return the physical storage size (possibly compressed) of a varlena datum
                                619                 :                :  * ----------
                                620                 :                :  */
                                621                 :                : Size
                                622                 :             76 : toast_datum_size(Datum value)
                                623                 :                : {
  290 michael@paquier.xyz       624                 :             76 :     varlena    *attr = (varlena *) DatumGetPointer(value);
                                625                 :                :     Size        result;
                                626                 :                : 
 2700 rhaas@postgresql.org      627         [ +  + ]:             76 :     if (VARATT_IS_EXTERNAL_ONDISK(attr))
                                628                 :                :     {
                                629                 :                :         /*
                                630                 :                :          * Attribute is stored externally - return the extsize whether
                                631                 :                :          * compressed or not.  We do not count the size of the toast pointer
                                632                 :                :          * ... should we?
                                633                 :                :          */
                                634                 :                :         toast_external_data toast_ext_data;
                                635                 :                : 
   74 michael@paquier.xyz       636                 :GNC           1 :         toast_external_info_get(attr, &toast_ext_data);
                                637                 :              1 :         result = VARATT_EXTINFO_GET_EXTSIZE(toast_ext_data.extinfo);
                                638                 :                :     }
 2700 rhaas@postgresql.org      639         [ -  + ]:CBC          75 :     else if (VARATT_IS_EXTERNAL_INDIRECT(attr))
                                640                 :                :     {
                                641                 :                :         varatt_indirect toast_pointer;
                                642                 :                : 
 2700 rhaas@postgresql.org      643   [ #  #  #  # ]:UBC           0 :         VARATT_EXTERNAL_GET_POINTER(toast_pointer, attr);
                                644                 :                : 
                                645                 :                :         /* nested indirect Datums aren't allowed */
                                646         [ #  # ]:              0 :         Assert(!VARATT_IS_EXTERNAL_INDIRECT(attr));
                                647                 :                : 
                                648                 :              0 :         return toast_datum_size(PointerGetDatum(toast_pointer.pointer));
                                649                 :                :     }
 2700 rhaas@postgresql.org      650         [ -  + ]:CBC          75 :     else if (VARATT_IS_EXTERNAL_EXPANDED(attr))
                                651                 :                :     {
 2700 rhaas@postgresql.org      652                 :UBC           0 :         result = EOH_get_flat_size(DatumGetEOHP(value));
                                653                 :                :     }
 2700 rhaas@postgresql.org      654         [ -  + ]:CBC          75 :     else if (VARATT_IS_SHORT(attr))
                                655                 :                :     {
 2700 rhaas@postgresql.org      656                 :UBC           0 :         result = VARSIZE_SHORT(attr);
                                657                 :                :     }
                                658                 :                :     else
                                659                 :                :     {
                                660                 :                :         /*
                                661                 :                :          * Attribute is stored inline either compressed or not, just calculate
                                662                 :                :          * the size of the datum in either case.
                                663                 :                :          */
 2700 rhaas@postgresql.org      664                 :CBC          75 :         result = VARSIZE(attr);
                                665                 :                :     }
                                666                 :             76 :     return result;
                                667                 :                : }
        

Generated by: LCOV version 2.0-1