LCOV - differential code coverage report
Current view: top level - src/backend/access/common - detoast.c (source / functions) Coverage Total Hit LBC UBC GBC GNC CBC EUB ECB DUB DCB
Current: 0e5ff9b9b45a657aea12440478dc002e9b01f138 vs 0123ce131fca454009439dfa3b2266d1d40737d7 Lines: 85.0 % 187 159 28 21 138 6 21
Current Date: 2026-03-14 14:10:32 -0400 Functions: 100.0 % 9 9 9 7
Baseline: lcov-20260315-024220-baseline Branches: 60.8 % 176 107 7 62 9 2 96 87 37 15 7
Baseline Date: 2026-03-14 15:27:56 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 100.0 % 21 21 21
(360..) days: 83.1 % 166 138 28 138 6
Function coverage date bins:
(30,360] days: 100.0 % 7 7 7
(360..) days: 100.0 % 2 2 2
Branch coverage date bins:
(30,360] days: 100.0 % 2 2 2
(360..) days: 35.2 % 298 105 7 62 9 96 87 37

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

Generated by: LCOV version 2.4-beta