LCOV - differential code coverage report
Current view: top level - src/fe_utils - astreamer_zstd.c (source / functions) Coverage Total Hit UBC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 92.1 % 114 105 9 105
Current Date: 2025-09-06 07:49:51 +0900 Functions: 100.0 % 8 8 8
Baseline: lcov-20250906-005545-baseline Branches: 70.0 % 40 28 12 28
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(360..) days: 92.1 % 114 105 9 105
Function coverage date bins:
(360..) days: 100.0 % 8 8 8
Branch coverage date bins:
(360..) days: 70.0 % 40 28 12 28

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * astreamer_zstd.c
                                  4                 :                :  *
                                  5                 :                :  * Archive streamers that deal with data compressed using zstd.
                                  6                 :                :  * astreamer_zstd_compressor applies lz4 compression to the input stream,
                                  7                 :                :  * and astreamer_zstd_decompressor does the reverse.
                                  8                 :                :  *
                                  9                 :                :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
                                 10                 :                :  *
                                 11                 :                :  * IDENTIFICATION
                                 12                 :                :  *        src/fe_utils/astreamer_zstd.c
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : 
                                 16                 :                : #include "postgres_fe.h"
                                 17                 :                : 
                                 18                 :                : #include <unistd.h>
                                 19                 :                : 
                                 20                 :                : #ifdef USE_ZSTD
                                 21                 :                : #include <zstd.h>
                                 22                 :                : #endif
                                 23                 :                : 
                                 24                 :                : #include "common/logging.h"
                                 25                 :                : #include "fe_utils/astreamer.h"
                                 26                 :                : 
                                 27                 :                : #ifdef USE_ZSTD
                                 28                 :                : 
                                 29                 :                : typedef struct astreamer_zstd_frame
                                 30                 :                : {
                                 31                 :                :     astreamer   base;
                                 32                 :                : 
                                 33                 :                :     ZSTD_CCtx  *cctx;
                                 34                 :                :     ZSTD_DCtx  *dctx;
                                 35                 :                :     ZSTD_outBuffer zstd_outBuf;
                                 36                 :                : } astreamer_zstd_frame;
                                 37                 :                : 
                                 38                 :                : static void astreamer_zstd_compressor_content(astreamer *streamer,
                                 39                 :                :                                               astreamer_member *member,
                                 40                 :                :                                               const char *data, int len,
                                 41                 :                :                                               astreamer_archive_context context);
                                 42                 :                : static void astreamer_zstd_compressor_finalize(astreamer *streamer);
                                 43                 :                : static void astreamer_zstd_compressor_free(astreamer *streamer);
                                 44                 :                : 
                                 45                 :                : static const astreamer_ops astreamer_zstd_compressor_ops = {
                                 46                 :                :     .content = astreamer_zstd_compressor_content,
                                 47                 :                :     .finalize = astreamer_zstd_compressor_finalize,
                                 48                 :                :     .free = astreamer_zstd_compressor_free
                                 49                 :                : };
                                 50                 :                : 
                                 51                 :                : static void astreamer_zstd_decompressor_content(astreamer *streamer,
                                 52                 :                :                                                 astreamer_member *member,
                                 53                 :                :                                                 const char *data, int len,
                                 54                 :                :                                                 astreamer_archive_context context);
                                 55                 :                : static void astreamer_zstd_decompressor_finalize(astreamer *streamer);
                                 56                 :                : static void astreamer_zstd_decompressor_free(astreamer *streamer);
                                 57                 :                : 
                                 58                 :                : static const astreamer_ops astreamer_zstd_decompressor_ops = {
                                 59                 :                :     .content = astreamer_zstd_decompressor_content,
                                 60                 :                :     .finalize = astreamer_zstd_decompressor_finalize,
                                 61                 :                :     .free = astreamer_zstd_decompressor_free
                                 62                 :                : };
                                 63                 :                : #endif
                                 64                 :                : 
                                 65                 :                : /*
                                 66                 :                :  * Create a new base backup streamer that performs zstd compression of tar
                                 67                 :                :  * blocks.
                                 68                 :                :  */
                                 69                 :                : astreamer *
  397 rhaas@postgresql.org       70                 :CBC           3 : astreamer_zstd_compressor_new(astreamer *next, pg_compress_specification *compress)
                                 71                 :                : {
                                 72                 :                : #ifdef USE_ZSTD
                                 73                 :                :     astreamer_zstd_frame *streamer;
                                 74                 :                :     size_t      ret;
                                 75                 :                : 
 1279                            76         [ -  + ]:              3 :     Assert(next != NULL);
                                 77                 :                : 
  397                            78                 :              3 :     streamer = palloc0(sizeof(astreamer_zstd_frame));
                                 79                 :                : 
                                 80                 :              3 :     *((const astreamer_ops **) &streamer->base.bbs_ops) =
                                 81                 :                :         &astreamer_zstd_compressor_ops;
                                 82                 :                : 
 1279                            83                 :              3 :     streamer->base.bbs_next = next;
                                 84                 :              3 :     initStringInfo(&streamer->base.bbs_buffer);
                                 85                 :              3 :     enlargeStringInfo(&streamer->base.bbs_buffer, ZSTD_DStreamOutSize());
                                 86                 :                : 
                                 87                 :              3 :     streamer->cctx = ZSTD_createCCtx();
                                 88         [ -  + ]:              3 :     if (!streamer->cctx)
 1247 tgl@sss.pgh.pa.us          89                 :UBC           0 :         pg_fatal("could not create zstd compression context");
                                 90                 :                : 
                                 91                 :                :     /* Set compression level */
 1088 michael@paquier.xyz        92                 :CBC           3 :     ret = ZSTD_CCtx_setParameter(streamer->cctx, ZSTD_c_compressionLevel,
                                 93                 :                :                                  compress->level);
                                 94         [ -  + ]:              3 :     if (ZSTD_isError(ret))
 1088 michael@paquier.xyz        95                 :UBC           0 :         pg_fatal("could not set zstd compression level to %d: %s",
                                 96                 :                :                  compress->level, ZSTD_getErrorName(ret));
                                 97                 :                : 
                                 98                 :                :     /* Set # of workers, if specified */
 1243 michael@paquier.xyz        99         [ +  + ]:CBC           3 :     if ((compress->options & PG_COMPRESSION_OPTION_WORKERS) != 0)
                                100                 :                :     {
                                101                 :                :         /*
                                102                 :                :          * On older versions of libzstd, this option does not exist, and
                                103                 :                :          * trying to set it will fail. Similarly for newer versions if they
                                104                 :                :          * are compiled without threading support.
                                105                 :                :          */
 1256 rhaas@postgresql.org      106                 :              1 :         ret = ZSTD_CCtx_setParameter(streamer->cctx, ZSTD_c_nbWorkers,
                                107                 :                :                                      compress->workers);
                                108         [ -  + ]:              1 :         if (ZSTD_isError(ret))
 1247 tgl@sss.pgh.pa.us         109                 :UBC           0 :             pg_fatal("could not set compression worker count to %d: %s",
                                110                 :                :                      compress->workers, ZSTD_getErrorName(ret));
                                111                 :                :     }
                                112                 :                : 
  884 tomas.vondra@postgre      113         [ +  + ]:CBC           3 :     if ((compress->options & PG_COMPRESSION_OPTION_LONG_DISTANCE) != 0)
                                114                 :                :     {
                                115                 :              1 :         ret = ZSTD_CCtx_setParameter(streamer->cctx,
                                116                 :                :                                      ZSTD_c_enableLongDistanceMatching,
                                117                 :              1 :                                      compress->long_distance);
                                118         [ -  + ]:              1 :         if (ZSTD_isError(ret))
                                119                 :                :         {
  841 peter@eisentraut.org      120                 :UBC           0 :             pg_log_error("could not enable long-distance mode: %s",
                                121                 :                :                          ZSTD_getErrorName(ret));
  884 tomas.vondra@postgre      122                 :              0 :             exit(1);
                                123                 :                :         }
                                124                 :                :     }
                                125                 :                : 
                                126                 :                :     /* Initialize the ZSTD output buffer. */
 1279 rhaas@postgresql.org      127                 :CBC           3 :     streamer->zstd_outBuf.dst = streamer->base.bbs_buffer.data;
                                128                 :              3 :     streamer->zstd_outBuf.size = streamer->base.bbs_buffer.maxlen;
                                129                 :              3 :     streamer->zstd_outBuf.pos = 0;
                                130                 :                : 
                                131                 :              3 :     return &streamer->base;
                                132                 :                : #else
                                133                 :                :     pg_fatal("this build does not support compression with %s", "ZSTD");
                                134                 :                :     return NULL;                /* keep compiler quiet */
                                135                 :                : #endif
                                136                 :                : }
                                137                 :                : 
                                138                 :                : #ifdef USE_ZSTD
                                139                 :                : /*
                                140                 :                :  * Compress the input data to output buffer.
                                141                 :                :  *
                                142                 :                :  * Find out the compression bound based on input data length for each
                                143                 :                :  * invocation to make sure that output buffer has enough capacity to
                                144                 :                :  * accommodate the compressed data. In case if the output buffer
                                145                 :                :  * capacity falls short of compression bound then forward the content
                                146                 :                :  * of output buffer to next streamer and empty the buffer.
                                147                 :                :  */
                                148                 :                : static void
  397                           149                 :           8223 : astreamer_zstd_compressor_content(astreamer *streamer,
                                150                 :                :                                   astreamer_member *member,
                                151                 :                :                                   const char *data, int len,
                                152                 :                :                                   astreamer_archive_context context)
                                153                 :                : {
                                154                 :           8223 :     astreamer_zstd_frame *mystreamer = (astreamer_zstd_frame *) streamer;
 1279                           155                 :           8223 :     ZSTD_inBuffer inBuf = {data, len, 0};
                                156                 :                : 
                                157         [ +  + ]:          24677 :     while (inBuf.pos < inBuf.size)
                                158                 :                :     {
                                159                 :                :         size_t      yet_to_flush;
                                160                 :           8231 :         size_t      max_needed = ZSTD_compressBound(inBuf.size - inBuf.pos);
                                161                 :                : 
                                162                 :                :         /*
                                163                 :                :          * If the output buffer is not left with enough space, send the
                                164                 :                :          * compressed bytes to the next streamer, and empty the buffer.
                                165                 :                :          */
                                166         [ +  + ]:           8231 :         if (mystreamer->zstd_outBuf.size - mystreamer->zstd_outBuf.pos <
                                167                 :                :             max_needed)
                                168                 :                :         {
  397                           169                 :             27 :             astreamer_content(mystreamer->base.bbs_next, member,
                                170                 :             27 :                               mystreamer->zstd_outBuf.dst,
                                171                 :             27 :                               mystreamer->zstd_outBuf.pos,
                                172                 :                :                               context);
                                173                 :                : 
                                174                 :                :             /* Reset the ZSTD output buffer. */
 1279                           175                 :             27 :             mystreamer->zstd_outBuf.dst = mystreamer->base.bbs_buffer.data;
                                176                 :             27 :             mystreamer->zstd_outBuf.size = mystreamer->base.bbs_buffer.maxlen;
                                177                 :             27 :             mystreamer->zstd_outBuf.pos = 0;
                                178                 :                :         }
                                179                 :                : 
                                180                 :                :         yet_to_flush =
                                181                 :           8231 :             ZSTD_compressStream2(mystreamer->cctx, &mystreamer->zstd_outBuf,
                                182                 :                :                                  &inBuf, ZSTD_e_continue);
                                183                 :                : 
                                184         [ +  - ]:           8231 :         if (ZSTD_isError(yet_to_flush))
 1279 rhaas@postgresql.org      185                 :UBC           0 :             pg_log_error("could not compress data: %s",
                                186                 :                :                          ZSTD_getErrorName(yet_to_flush));
                                187                 :                :     }
 1279 rhaas@postgresql.org      188                 :CBC        8223 : }
                                189                 :                : 
                                190                 :                : /*
                                191                 :                :  * End-of-stream processing.
                                192                 :                :  */
                                193                 :                : static void
  397                           194                 :              3 : astreamer_zstd_compressor_finalize(astreamer *streamer)
                                195                 :                : {
                                196                 :              3 :     astreamer_zstd_frame *mystreamer = (astreamer_zstd_frame *) streamer;
                                197                 :                :     size_t      yet_to_flush;
                                198                 :                : 
                                199                 :                :     do
                                200                 :                :     {
 1279                           201                 :              5 :         ZSTD_inBuffer in = {NULL, 0, 0};
                                202                 :              5 :         size_t      max_needed = ZSTD_compressBound(0);
                                203                 :                : 
                                204                 :                :         /*
                                205                 :                :          * If the output buffer is not left with enough space, send the
                                206                 :                :          * compressed bytes to the next streamer, and empty the buffer.
                                207                 :                :          */
                                208         [ +  + ]:              5 :         if (mystreamer->zstd_outBuf.size - mystreamer->zstd_outBuf.pos <
                                209                 :                :             max_needed)
                                210                 :                :         {
  397                           211                 :              2 :             astreamer_content(mystreamer->base.bbs_next, NULL,
                                212                 :              2 :                               mystreamer->zstd_outBuf.dst,
                                213                 :              2 :                               mystreamer->zstd_outBuf.pos,
                                214                 :                :                               ASTREAMER_UNKNOWN);
                                215                 :                : 
                                216                 :                :             /* Reset the ZSTD output buffer. */
 1279                           217                 :              2 :             mystreamer->zstd_outBuf.dst = mystreamer->base.bbs_buffer.data;
                                218                 :              2 :             mystreamer->zstd_outBuf.size = mystreamer->base.bbs_buffer.maxlen;
                                219                 :              2 :             mystreamer->zstd_outBuf.pos = 0;
                                220                 :                :         }
                                221                 :                : 
                                222                 :              5 :         yet_to_flush = ZSTD_compressStream2(mystreamer->cctx,
                                223                 :                :                                             &mystreamer->zstd_outBuf,
                                224                 :                :                                             &in, ZSTD_e_end);
                                225                 :                : 
                                226         [ -  + ]:              5 :         if (ZSTD_isError(yet_to_flush))
 1279 rhaas@postgresql.org      227                 :UBC           0 :             pg_log_error("could not compress data: %s",
                                228                 :                :                          ZSTD_getErrorName(yet_to_flush));
                                229                 :                : 
 1279 rhaas@postgresql.org      230         [ +  + ]:CBC           5 :     } while (yet_to_flush > 0);
                                231                 :                : 
                                232                 :                :     /* Make sure to pass any remaining bytes to the next streamer. */
                                233         [ +  - ]:              3 :     if (mystreamer->zstd_outBuf.pos > 0)
  397                           234                 :              3 :         astreamer_content(mystreamer->base.bbs_next, NULL,
                                235                 :              3 :                           mystreamer->zstd_outBuf.dst,
                                236                 :              3 :                           mystreamer->zstd_outBuf.pos,
                                237                 :                :                           ASTREAMER_UNKNOWN);
                                238                 :                : 
                                239                 :              3 :     astreamer_finalize(mystreamer->base.bbs_next);
 1279                           240                 :              3 : }
                                241                 :                : 
                                242                 :                : /*
                                243                 :                :  * Free memory.
                                244                 :                :  */
                                245                 :                : static void
  397                           246                 :              3 : astreamer_zstd_compressor_free(astreamer *streamer)
                                247                 :                : {
                                248                 :              3 :     astreamer_zstd_frame *mystreamer = (astreamer_zstd_frame *) streamer;
                                249                 :                : 
                                250                 :              3 :     astreamer_free(streamer->bbs_next);
 1279                           251                 :              3 :     ZSTD_freeCCtx(mystreamer->cctx);
                                252                 :              3 :     pfree(streamer->bbs_buffer.data);
                                253                 :              3 :     pfree(streamer);
                                254                 :              3 : }
                                255                 :                : #endif
                                256                 :                : 
                                257                 :                : /*
                                258                 :                :  * Create a new base backup streamer that performs decompression of zstd
                                259                 :                :  * compressed blocks.
                                260                 :                :  */
                                261                 :                : astreamer *
  397                           262                 :              9 : astreamer_zstd_decompressor_new(astreamer *next)
                                263                 :                : {
                                264                 :                : #ifdef USE_ZSTD
                                265                 :                :     astreamer_zstd_frame *streamer;
                                266                 :                : 
 1279                           267         [ -  + ]:              9 :     Assert(next != NULL);
                                268                 :                : 
  397                           269                 :              9 :     streamer = palloc0(sizeof(astreamer_zstd_frame));
                                270                 :              9 :     *((const astreamer_ops **) &streamer->base.bbs_ops) =
                                271                 :                :         &astreamer_zstd_decompressor_ops;
                                272                 :                : 
 1279                           273                 :              9 :     streamer->base.bbs_next = next;
                                274                 :              9 :     initStringInfo(&streamer->base.bbs_buffer);
                                275                 :              9 :     enlargeStringInfo(&streamer->base.bbs_buffer, ZSTD_DStreamOutSize());
                                276                 :                : 
                                277                 :              9 :     streamer->dctx = ZSTD_createDCtx();
                                278         [ -  + ]:              9 :     if (!streamer->dctx)
 1247 tgl@sss.pgh.pa.us         279                 :UBC           0 :         pg_fatal("could not create zstd decompression context");
                                280                 :                : 
                                281                 :                :     /* Initialize the ZSTD output buffer. */
 1279 rhaas@postgresql.org      282                 :CBC           9 :     streamer->zstd_outBuf.dst = streamer->base.bbs_buffer.data;
                                283                 :              9 :     streamer->zstd_outBuf.size = streamer->base.bbs_buffer.maxlen;
                                284                 :              9 :     streamer->zstd_outBuf.pos = 0;
                                285                 :                : 
                                286                 :              9 :     return &streamer->base;
                                287                 :                : #else
                                288                 :                :     pg_fatal("this build does not support compression with %s", "ZSTD");
                                289                 :                :     return NULL;                /* keep compiler quiet */
                                290                 :                : #endif
                                291                 :                : }
                                292                 :                : 
                                293                 :                : #ifdef USE_ZSTD
                                294                 :                : /*
                                295                 :                :  * Decompress the input data to output buffer until we run out of input
                                296                 :                :  * data. Each time the output buffer is full, pass on the decompressed data
                                297                 :                :  * to the next streamer.
                                298                 :                :  */
                                299                 :                : static void
  397                           300                 :            320 : astreamer_zstd_decompressor_content(astreamer *streamer,
                                301                 :                :                                     astreamer_member *member,
                                302                 :                :                                     const char *data, int len,
                                303                 :                :                                     astreamer_archive_context context)
                                304                 :                : {
                                305                 :            320 :     astreamer_zstd_frame *mystreamer = (astreamer_zstd_frame *) streamer;
 1279                           306                 :            320 :     ZSTD_inBuffer inBuf = {data, len, 0};
                                307                 :                : 
                                308         [ +  + ]:           1891 :     while (inBuf.pos < inBuf.size)
                                309                 :                :     {
                                310                 :                :         size_t      ret;
                                311                 :                : 
                                312                 :                :         /*
                                313                 :                :          * If output buffer is full then forward the content to next streamer
                                314                 :                :          * and update the output buffer.
                                315                 :                :          */
                                316         [ +  + ]:           1251 :         if (mystreamer->zstd_outBuf.pos >= mystreamer->zstd_outBuf.size)
                                317                 :                :         {
  397                           318                 :           1116 :             astreamer_content(mystreamer->base.bbs_next, member,
                                319                 :           1116 :                               mystreamer->zstd_outBuf.dst,
                                320                 :           1116 :                               mystreamer->zstd_outBuf.pos,
                                321                 :                :                               context);
                                322                 :                : 
                                323                 :                :             /* Reset the ZSTD output buffer. */
 1279                           324                 :           1116 :             mystreamer->zstd_outBuf.dst = mystreamer->base.bbs_buffer.data;
                                325                 :           1116 :             mystreamer->zstd_outBuf.size = mystreamer->base.bbs_buffer.maxlen;
                                326                 :           1116 :             mystreamer->zstd_outBuf.pos = 0;
                                327                 :                :         }
                                328                 :                : 
                                329                 :           1251 :         ret = ZSTD_decompressStream(mystreamer->dctx,
                                330                 :                :                                     &mystreamer->zstd_outBuf, &inBuf);
                                331                 :                : 
                                332         [ +  - ]:           1251 :         if (ZSTD_isError(ret))
 1247 tgl@sss.pgh.pa.us         333                 :UBC           0 :             pg_log_error("could not decompress data: %s",
                                334                 :                :                          ZSTD_getErrorName(ret));
                                335                 :                :     }
 1279 rhaas@postgresql.org      336                 :CBC         320 : }
                                337                 :                : 
                                338                 :                : /*
                                339                 :                :  * End-of-stream processing.
                                340                 :                :  */
                                341                 :                : static void
  397                           342                 :              9 : astreamer_zstd_decompressor_finalize(astreamer *streamer)
                                343                 :                : {
                                344                 :              9 :     astreamer_zstd_frame *mystreamer = (astreamer_zstd_frame *) streamer;
                                345                 :                : 
                                346                 :                :     /*
                                347                 :                :      * End of the stream, if there is some pending data in output buffers then
                                348                 :                :      * we must forward it to next streamer.
                                349                 :                :      */
 1279                           350         [ +  - ]:              9 :     if (mystreamer->zstd_outBuf.pos > 0)
  397                           351                 :              9 :         astreamer_content(mystreamer->base.bbs_next, NULL,
                                352                 :              9 :                           mystreamer->base.bbs_buffer.data,
                                353                 :                :                           mystreamer->base.bbs_buffer.maxlen,
                                354                 :                :                           ASTREAMER_UNKNOWN);
                                355                 :                : 
                                356                 :              9 :     astreamer_finalize(mystreamer->base.bbs_next);
 1279                           357                 :              9 : }
                                358                 :                : 
                                359                 :                : /*
                                360                 :                :  * Free memory.
                                361                 :                :  */
                                362                 :                : static void
  397                           363                 :              9 : astreamer_zstd_decompressor_free(astreamer *streamer)
                                364                 :                : {
                                365                 :              9 :     astreamer_zstd_frame *mystreamer = (astreamer_zstd_frame *) streamer;
                                366                 :                : 
                                367                 :              9 :     astreamer_free(streamer->bbs_next);
 1279                           368                 :              9 :     ZSTD_freeDCtx(mystreamer->dctx);
                                369                 :              9 :     pfree(streamer->bbs_buffer.data);
                                370                 :              9 :     pfree(streamer);
                                371                 :              9 : }
                                372                 :                : #endif
        

Generated by: LCOV version 2.4-beta