LCOV - differential code coverage report
Current view: top level - src/backend/access/common - toast_compression.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 87.0 % 92 80 12 3 77 3
Current Date: 2026-11-28 16:48:44 +0900 Functions: 100.0 % 9 9 1 8
Baseline: lcov-20261128-baseline Branches: 47.5 % 59 28 31 2 26
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 57.1 % 7 4 3 4
(30,360] days: 100.0 % 22 22 3 19
(360..) days: 85.7 % 63 54 9 54
Function coverage date bins:
(30,360] days: 100.0 % 7 7 1 6
(360..) days: 100.0 % 2 2 2
Branch coverage date bins:
(7,30] days: 28.6 % 14 4 10 4
(30,360] days: 100.0 % 2 2 2
(360..) days: 51.2 % 43 22 21 22

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * toast_compression.c
                                  4                 :                :  *    Functions for toast compression.
                                  5                 :                :  *
                                  6                 :                :  * Copyright (c) 2021-2026, PostgreSQL Global Development Group
                                  7                 :                :  *
                                  8                 :                :  *
                                  9                 :                :  * IDENTIFICATION
                                 10                 :                :  *    src/backend/access/common/toast_compression.c
                                 11                 :                :  *
                                 12                 :                :  *-------------------------------------------------------------------------
                                 13                 :                :  */
                                 14                 :                : #include "postgres.h"
                                 15                 :                : 
                                 16                 :                : #ifdef USE_LZ4
                                 17                 :                : #include <lz4.h>
                                 18                 :                : #endif
                                 19                 :                : 
                                 20                 :                : #include "access/detoast.h"
                                 21                 :                : #include "access/toast_compression.h"
                                 22                 :                : #include "common/pg_lzcompress.h"
                                 23                 :                : #include "utils/memutils.h"
                                 24                 :                : #include "varatt.h"
                                 25                 :                : 
                                 26                 :                : /* GUC */
                                 27                 :                : int         default_toast_compression = DEFAULT_TOAST_COMPRESSION;
                                 28                 :                : 
                                 29                 :                : #define NO_COMPRESSION_SUPPORT(method) \
                                 30                 :                :     ereport(ERROR, \
                                 31                 :                :             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), \
                                 32                 :                :              errmsg("compression method %s not supported", method), \
                                 33                 :                :              errdetail("This functionality requires the server to be built with %s support.", method)))
                                 34                 :                : 
                                 35                 :                : /*
                                 36                 :                :  * Compress a varlena using PGLZ.
                                 37                 :                :  *
                                 38                 :                :  * Returns the compressed varlena, or NULL if compression fails.
                                 39                 :                :  */
                                 40                 :                : varlena *
  290 michael@paquier.xyz        41                 :CBC         236 : pglz_compress_datum(const varlena *value)
                                 42                 :                : {
                                 43                 :                :     int32       valsize,
                                 44                 :                :                 len;
                                 45                 :            236 :     varlena    *tmp = NULL;
                                 46                 :                : 
 1553 peter@eisentraut.org       47                 :            236 :     valsize = VARSIZE_ANY_EXHDR(value);
                                 48                 :                : 
                                 49                 :                :     /*
                                 50                 :                :      * No point in wasting a palloc cycle if value size is outside the allowed
                                 51                 :                :      * range for compression.
                                 52                 :                :      */
 2080 rhaas@postgresql.org       53         [ +  - ]:            236 :     if (valsize < PGLZ_strategy_default->min_input_size ||
                                 54         [ -  + ]:            236 :         valsize > PGLZ_strategy_default->max_input_size)
 2080 rhaas@postgresql.org       55                 :UBC           0 :         return NULL;
                                 56                 :                : 
                                 57                 :                :     /*
                                 58                 :                :      * Figure out the maximum possible size of the pglz output, add the bytes
                                 59                 :                :      * that will be needed for varlena overhead, and allocate that amount.
                                 60                 :                :      *
                                 61                 :                :      * VARSIZE_ANY_EXHDR's refusal to return values higher than 1GB are what
                                 62                 :                :      * protect this addition from wraparound; the PGLZ strategy doesn't
                                 63                 :                :      * provide a useful backstop.
                                 64                 :                :      */
  290 michael@paquier.xyz        65                 :CBC         236 :     tmp = (varlena *) palloc(PGLZ_MAX_OUTPUT(valsize) +
                                 66                 :                :                              VARHDRSZ_COMPRESSED);
                                 67                 :                : 
 2080 rhaas@postgresql.org       68                 :            236 :     len = pglz_compress(VARDATA_ANY(value),
                                 69                 :                :                         valsize,
                                 70                 :                :                         (char *) tmp + VARHDRSZ_COMPRESSED,
                                 71                 :                :                         NULL);
                                 72         [ +  + ]:            236 :     if (len < 0)
                                 73                 :                :     {
                                 74                 :            112 :         pfree(tmp);
                                 75                 :            112 :         return NULL;
                                 76                 :                :     }
                                 77                 :                : 
 2077 tgl@sss.pgh.pa.us          78                 :            124 :     SET_VARSIZE_COMPRESSED(tmp, len + VARHDRSZ_COMPRESSED);
                                 79                 :                : 
 2080 rhaas@postgresql.org       80                 :            124 :     return tmp;
                                 81                 :                : }
                                 82                 :                : 
                                 83                 :                : /*
                                 84                 :                :  * Decompress a varlena that was compressed using PGLZ.
                                 85                 :                :  */
                                 86                 :                : varlena *
  290 michael@paquier.xyz        87                 :            383 : pglz_decompress_datum(const varlena *value)
                                 88                 :                : {
                                 89                 :                :     varlena    *result;
                                 90                 :                :     int32       rawsize;
                                 91                 :                : 
                                 92                 :                :     /*
                                 93                 :                :      * Allocate memory for the uncompressed data.  VARLENA_EXTSIZE_MASK
                                 94                 :                :      * protects this against overflow.
                                 95                 :                :      */
                                 96                 :            383 :     result = (varlena *) palloc(VARDATA_COMPRESSED_GET_EXTSIZE(value) + VARHDRSZ);
                                 97                 :                : 
                                 98                 :                :     /* decompress the data */
  306 peter@eisentraut.org       99                 :            383 :     rawsize = pglz_decompress((const char *) value + VARHDRSZ_COMPRESSED,
 2077 tgl@sss.pgh.pa.us         100                 :            383 :                               VARSIZE(value) - VARHDRSZ_COMPRESSED,
                                101                 :                :                               VARDATA(result),
                                102                 :            383 :                               VARDATA_COMPRESSED_GET_EXTSIZE(value), true);
 2080 rhaas@postgresql.org      103         [ -  + ]:            383 :     if (rawsize < 0)
 2080 rhaas@postgresql.org      104         [ #  # ]:UBC           0 :         ereport(ERROR,
                                105                 :                :                 (errcode(ERRCODE_DATA_CORRUPTED),
                                106                 :                :                  errmsg_internal("compressed pglz data is corrupt")));
                                107                 :                : 
 2080 rhaas@postgresql.org      108                 :CBC         383 :     SET_VARSIZE(result, rawsize + VARHDRSZ);
                                109                 :                : 
                                110                 :            383 :     return result;
                                111                 :                : }
                                112                 :                : 
                                113                 :                : /*
                                114                 :                :  * Decompress part of a varlena that was compressed using PGLZ.
                                115                 :                :  */
                                116                 :                : varlena *
  290 michael@paquier.xyz       117                 :             24 : pglz_decompress_datum_slice(const varlena *value,
                                118                 :                :                             int32 slicelength)
                                119                 :                : {
                                120                 :                :     varlena    *result;
                                121                 :                :     int32       rawsize;
                                122                 :                : 
                                123                 :                :     /*
                                124                 :                :      * Allocate memory for the uncompressed data.  Caller must have checked
                                125                 :                :      * the requested size, but verify.
                                126                 :                :      */
   19 alvherre@kurilemu.de      127         [ -  + ]:             24 :     if (!AllocSizeIsValid(slicelength))
   19 alvherre@kurilemu.de      128         [ #  # ]:UBC           0 :         elog(ERROR, "value is too big");
  290 michael@paquier.xyz       129                 :CBC          24 :     result = (varlena *) palloc(slicelength + VARHDRSZ);
                                130                 :                : 
                                131                 :                :     /* decompress the data */
  306 peter@eisentraut.org      132                 :             24 :     rawsize = pglz_decompress((const char *) value + VARHDRSZ_COMPRESSED,
 2077 tgl@sss.pgh.pa.us         133                 :             24 :                               VARSIZE(value) - VARHDRSZ_COMPRESSED,
                                134                 :                :                               VARDATA(result),
                                135                 :                :                               slicelength, false);
 2080 rhaas@postgresql.org      136         [ -  + ]:             24 :     if (rawsize < 0)
 2080 rhaas@postgresql.org      137         [ #  # ]:UBC           0 :         ereport(ERROR,
                                138                 :                :                 (errcode(ERRCODE_DATA_CORRUPTED),
                                139                 :                :                  errmsg_internal("compressed pglz data is corrupt")));
                                140                 :                : 
 2080 rhaas@postgresql.org      141                 :CBC          24 :     SET_VARSIZE(result, rawsize + VARHDRSZ);
                                142                 :                : 
                                143                 :             24 :     return result;
                                144                 :                : }
                                145                 :                : 
                                146                 :                : /*
                                147                 :                :  * Compress a varlena using LZ4.
                                148                 :                :  *
                                149                 :                :  * Returns the compressed varlena, or NULL if compression fails.
                                150                 :                :  */
                                151                 :                : varlena *
  290 michael@paquier.xyz       152                 :          32555 : lz4_compress_datum(const varlena *value)
                                153                 :                : {
                                154                 :                : #ifndef USE_LZ4
                                155                 :                :     NO_COMPRESSION_SUPPORT("lz4");
                                156                 :                :     return NULL;                /* keep compiler quiet */
                                157                 :                : #else
                                158                 :                :     int32       valsize;
                                159                 :                :     int32       len;
                                160                 :                :     int32       max_size;
                                161                 :          32555 :     varlena    *tmp = NULL;
                                162                 :                : 
 2080 rhaas@postgresql.org      163                 :          32555 :     valsize = VARSIZE_ANY_EXHDR(value);
                                164                 :                : 
                                165                 :                :     /*
                                166                 :                :      * Figure out the maximum possible size of the LZ4 output, add the bytes
                                167                 :                :      * that will be needed for varlena overhead, and allocate that amount.
                                168                 :                :      *
                                169                 :                :      * The current (2026) definition of LZ4_compressBound cannot yield a value
                                170                 :                :      * that can wrap-around the VARHDRSZ_COMPRESSED addition in 32-bit
                                171                 :                :      * machines, but we have no way to prevent future LZ4 development (or a
                                172                 :                :      * malicious LZ4 library) from changing that, so verify.
                                173                 :                :      */
                                174                 :          32555 :     max_size = LZ4_compressBound(valsize);
   19 alvherre@kurilemu.de      175         [ -  + ]:          32555 :     if (!AllocSizeIsValid(max_size))
   19 alvherre@kurilemu.de      176         [ #  # ]:UBC           0 :         elog(ERROR, "toast object size is too big");
  290 michael@paquier.xyz       177                 :CBC       32555 :     tmp = (varlena *) palloc(max_size + VARHDRSZ_COMPRESSED);
                                178                 :                : 
 2080 rhaas@postgresql.org      179                 :          32555 :     len = LZ4_compress_default(VARDATA_ANY(value),
                                180                 :                :                                (char *) tmp + VARHDRSZ_COMPRESSED,
                                181                 :                :                                valsize, max_size);
                                182         [ -  + ]:          32555 :     if (len <= 0)
 2080 rhaas@postgresql.org      183         [ #  # ]:UBC           0 :         elog(ERROR, "lz4 compression failed");
                                184                 :                : 
                                185                 :                :     /* data is incompressible so just free the memory and return NULL */
 2080 rhaas@postgresql.org      186         [ +  + ]:CBC       32555 :     if (len > valsize)
                                187                 :                :     {
                                188                 :            538 :         pfree(tmp);
                                189                 :            538 :         return NULL;
                                190                 :                :     }
                                191                 :                : 
 2077 tgl@sss.pgh.pa.us         192                 :          32017 :     SET_VARSIZE_COMPRESSED(tmp, len + VARHDRSZ_COMPRESSED);
                                193                 :                : 
 2080 rhaas@postgresql.org      194                 :          32017 :     return tmp;
                                195                 :                : #endif
                                196                 :                : }
                                197                 :                : 
                                198                 :                : /*
                                199                 :                :  * Decompress a varlena that was compressed using LZ4.
                                200                 :                :  */
                                201                 :                : varlena *
  290 michael@paquier.xyz       202                 :         104525 : lz4_decompress_datum(const varlena *value)
                                203                 :                : {
                                204                 :                : #ifndef USE_LZ4
                                205                 :                :     NO_COMPRESSION_SUPPORT("lz4");
                                206                 :                :     return NULL;                /* keep compiler quiet */
                                207                 :                : #else
                                208                 :                :     int32       rawsize;
                                209                 :                :     varlena    *result;
                                210                 :                : 
                                211                 :                :     /*
                                212                 :                :      * Allocate memory for the uncompressed data.  VARLENA_EXTSIZE_MASK
                                213                 :                :      * protects this against overflow.
                                214                 :                :      */
   19 alvherre@kurilemu.de      215         [ -  + ]:         104525 :     Assert(AllocSizeIsValid(VARDATA_COMPRESSED_GET_EXTSIZE(value)));
  290 michael@paquier.xyz       216                 :         104525 :     result = (varlena *) palloc(VARDATA_COMPRESSED_GET_EXTSIZE(value) + VARHDRSZ);
                                217                 :                : 
                                218                 :                :     /* decompress the data */
  306 peter@eisentraut.org      219                 :         104525 :     rawsize = LZ4_decompress_safe((const char *) value + VARHDRSZ_COMPRESSED,
                                220                 :                :                                   VARDATA(result),
 2077 tgl@sss.pgh.pa.us         221                 :         104525 :                                   VARSIZE(value) - VARHDRSZ_COMPRESSED,
                                222                 :         104525 :                                   VARDATA_COMPRESSED_GET_EXTSIZE(value));
 2080 rhaas@postgresql.org      223         [ -  + ]:         104525 :     if (rawsize < 0)
 2080 rhaas@postgresql.org      224         [ #  # ]:UBC           0 :         ereport(ERROR,
                                225                 :                :                 (errcode(ERRCODE_DATA_CORRUPTED),
                                226                 :                :                  errmsg_internal("compressed lz4 data is corrupt")));
                                227                 :                : 
                                228                 :                : 
 2080 rhaas@postgresql.org      229                 :CBC      104525 :     SET_VARSIZE(result, rawsize + VARHDRSZ);
                                230                 :                : 
                                231                 :         104525 :     return result;
                                232                 :                : #endif
                                233                 :                : }
                                234                 :                : 
                                235                 :                : /*
                                236                 :                :  * Decompress part of a varlena that was compressed using LZ4.
                                237                 :                :  */
                                238                 :                : varlena *
  290 michael@paquier.xyz       239                 :            164 : lz4_decompress_datum_slice(const varlena *value, int32 slicelength)
                                240                 :                : {
                                241                 :                : #ifndef USE_LZ4
                                242                 :                :     NO_COMPRESSION_SUPPORT("lz4");
                                243                 :                :     return NULL;                /* keep compiler quiet */
                                244                 :                : #else
                                245                 :                :     int32       rawsize;
                                246                 :                :     varlena    *result;
                                247                 :                : 
                                248                 :                :     /* slice decompression not supported prior to 1.8.3 */
 2080 rhaas@postgresql.org      249         [ -  + ]:            164 :     if (LZ4_versionNumber() < 10803)
 2080 rhaas@postgresql.org      250                 :UBC           0 :         return lz4_decompress_datum(value);
                                251                 :                : 
                                252                 :                :     /*
                                253                 :                :      * Allocate memory for the uncompressed data.  Caller must have checked
                                254                 :                :      * the requested size, but verify.
                                255                 :                :      */
   19 alvherre@kurilemu.de      256         [ -  + ]:CBC         164 :     if (!AllocSizeIsValid(slicelength))
   19 alvherre@kurilemu.de      257         [ #  # ]:UBC           0 :         elog(ERROR, "toast object size is too big");
  290 michael@paquier.xyz       258                 :CBC         164 :     result = (varlena *) palloc(slicelength + VARHDRSZ);
                                259                 :                : 
                                260                 :                :     /* decompress the data */
  306 peter@eisentraut.org      261                 :            164 :     rawsize = LZ4_decompress_safe_partial((const char *) value + VARHDRSZ_COMPRESSED,
                                262                 :                :                                           VARDATA(result),
 2077 tgl@sss.pgh.pa.us         263                 :            164 :                                           VARSIZE(value) - VARHDRSZ_COMPRESSED,
                                264                 :                :                                           slicelength,
                                265                 :                :                                           slicelength);
 2080 rhaas@postgresql.org      266         [ -  + ]:            164 :     if (rawsize < 0)
 2080 rhaas@postgresql.org      267         [ #  # ]:UBC           0 :         ereport(ERROR,
                                268                 :                :                 (errcode(ERRCODE_DATA_CORRUPTED),
                                269                 :                :                  errmsg_internal("compressed lz4 data is corrupt")));
                                270                 :                : 
 2080 rhaas@postgresql.org      271                 :CBC         164 :     SET_VARSIZE(result, rawsize + VARHDRSZ);
                                272                 :                : 
                                273                 :            164 :     return result;
                                274                 :                : #endif
                                275                 :                : }
                                276                 :                : 
                                277                 :                : /*
                                278                 :                :  * Extract compression ID from a varlena.
                                279                 :                :  *
                                280                 :                :  * Returns TOAST_INVALID_COMPRESSION_ID if the varlena is not compressed.
                                281                 :                :  */
                                282                 :                : ToastCompressionId
  290 michael@paquier.xyz       283                 :            154 : toast_get_compression_id(varlena *attr)
                                284                 :                : {
 2026 tgl@sss.pgh.pa.us         285                 :            154 :     ToastCompressionId cmid = TOAST_INVALID_COMPRESSION_ID;
                                286                 :                : 
                                287                 :                :     /*
                                288                 :                :      * If it is stored externally then fetch the compression method id from
                                289                 :                :      * the external toast pointer.  If compressed inline, fetch it from the
                                290                 :                :      * toast compression header.
                                291                 :                :      */
 2080 rhaas@postgresql.org      292         [ +  + ]:            154 :     if (VARATT_IS_EXTERNAL_ONDISK(attr))
                                293                 :                :     {
                                294                 :                :         toast_external_data toast_ext_data;
                                295                 :                : 
   74 michael@paquier.xyz       296                 :GNC          54 :         toast_external_info_get(attr, &toast_ext_data);
                                297                 :                : 
                                298         [ +  + ]:             54 :         if (VARATT_EXTINFO_IS_COMPRESSED(toast_ext_data.extinfo, toast_ext_data.rawsize))
                                299                 :             23 :             cmid = VARATT_EXTINFO_GET_COMPRESS_METHOD(toast_ext_data.extinfo);
                                300                 :                :     }
 2080 rhaas@postgresql.org      301         [ +  + ]:CBC         100 :     else if (VARATT_IS_COMPRESSED(attr))
 2077 tgl@sss.pgh.pa.us         302                 :             92 :         cmid = VARDATA_COMPRESSED_GET_COMPRESS_METHOD(attr);
                                303                 :                : 
 2080 rhaas@postgresql.org      304                 :            154 :     return cmid;
                                305                 :                : }
                                306                 :                : 
                                307                 :                : /*
                                308                 :                :  * CompressionNameToMethod - Get compression method from compression name
                                309                 :                :  *
                                310                 :                :  * Search in the available built-in methods.  If the compression not found
                                311                 :                :  * in the built-in methods then return InvalidCompressionMethod.
                                312                 :                :  */
                                313                 :                : char
 2075                           314                 :            117 : CompressionNameToMethod(const char *compression)
                                315                 :                : {
                                316         [ +  + ]:            117 :     if (strcmp(compression, "pglz") == 0)
                                317                 :             66 :         return TOAST_PGLZ_COMPRESSION;
                                318         [ +  + ]:             51 :     else if (strcmp(compression, "lz4") == 0)
                                319                 :                :     {
                                320                 :                : #ifndef USE_LZ4
                                321                 :                :         NO_COMPRESSION_SUPPORT("lz4");
                                322                 :                : #endif
                                323                 :             43 :         return TOAST_LZ4_COMPRESSION;
                                324                 :                :     }
                                325                 :                : 
                                326                 :              8 :     return InvalidCompressionMethod;
                                327                 :                : }
                                328                 :                : 
                                329                 :                : /*
                                330                 :                :  * GetCompressionMethodName - Get compression method name
                                331                 :                :  */
                                332                 :                : const char *
                                333                 :             28 : GetCompressionMethodName(char method)
                                334                 :                : {
                                335      [ +  +  - ]:             28 :     switch (method)
                                336                 :                :     {
                                337                 :             16 :         case TOAST_PGLZ_COMPRESSION:
                                338                 :             16 :             return "pglz";
                                339                 :             12 :         case TOAST_LZ4_COMPRESSION:
                                340                 :             12 :             return "lz4";
 2075 rhaas@postgresql.org      341                 :UBC           0 :         default:
                                342         [ #  # ]:              0 :             elog(ERROR, "invalid compression method %c", method);
                                343                 :                :             return NULL;        /* keep compiler quiet */
                                344                 :                :     }
                                345                 :                : }
        

Generated by: LCOV version 2.0-1