Re: ZSTD TOAST compression, and an extensible compression method encoding - Mailing list pgsql-hackers

From wenhui qiu
Subject Re: ZSTD TOAST compression, and an extensible compression method encoding
Date
Msg-id CAGjGUAKdj1DGaS1aQCev3H5dxj5Fc9BNBvciq9NOtQEHswaP7A@mail.gmail.com
Whole thread
In response to Re: ZSTD TOAST compression, and an extensible compression method encoding  (Nikhil Kumar Veldanda <veldanda.nikhilkumar17@gmail.com>)
Responses Re: ZSTD TOAST compression, and an extensible compression method encoding
List pgsql-hackers

Hi Nikhil,


Thanks for the updated patch set! Looking at patch 4/4, I noticed a minor issue in zstd_compress_datum() regarding the check for incompressible data:

  /* data is incompressible so just free the memory and return NULL */
  if (len > (size_t) valsize)
  {
    pfree(tmp);
    return NULL;
  }
Should this condition be >= instead of >?  if (len >= (size_t) valsize)
Reasons:
If len == valsize, the compressed payload alone is already the same size as the uncompressed data. Once VARHDRSZ_COMPRESSED_LONG (9 bytes) is added, the compressed datum is strictly larger than the original. There is no compression benefit here.
If len == valsize, zstd_compress_datum() currently proceeds to call SET_VARSIZE_COMPRESSED() and returns tmp, only for the caller toast_compress_datum() to immediately reject it via if (VARSIZE(tmp) < valsize - 2) and pfree(tmp). Freeing it early and returning NULL saves redundant operations.
This is also consistent with lz4_compress_datum() in the same file:

 /*
  * If the compressed size is greater than or equal to the raw data size,
  * then data is incompressible so just free the memory and return NULL.
  */
 if (len >= valsize)
 {
  pfree(tmp);
  return NULL;
 }

pgsql-hackers by date:

Previous
From: Matthias van de Meent
Date:
Subject: Re: BUG: pg_class.relchecks overflow, making table undroppable
Next
From: Ilia Evdokimov
Date:
Subject: Re: Fold NOT IN / <> ALL expressions containing NULL to FALSE