/*-------------------------------------------------------------------------
|
* cfs_compress.h
|
* Compressed File Store (CFS) — page-level compress/decompress API.
|
*
|
* This header exposes the primary operations used by the storage
|
* manager integration :
|
*
|
* CfsCompressPage() — compress an 8 kB PostgreSQL page into a
|
* CfsCompressedBlockHeader + payload blob ready to
|
* be appended to a .cfp file.
|
*
|
* CfsDecompressPage() — read a CfsCompressedBlockHeader + payload and
|
* reconstruct the original 8 kB page, verifying
|
* all CRC-32C checksums along the way.
|
*
|
* Both functions are designed to work with caller-supplied scratch buffers
|
* so they are suitable for use on the I/O hot path without palloc().
|
*
|
* Copyright (c) 2026, Kirillov Vyacheslav, QDB Development Group
|
*
|
* IDENTIFICATION
|
* src/include/storage/cfs/cfs_compress.h
|
*
|
*-------------------------------------------------------------------------
|
*/
|
#ifndef CFS_COMPRESS_H
|
#define CFS_COMPRESS_H
|
|
#include "storage/cfs/cfs.h"
|
#include "storage/cfs/cfs_codec.h"
|
|
/* ----------------------------------------------------------------
|
* CfsCompressOptions — parameters passed to CfsCompressPage().
|
* ---------------------------------------------------------------- */
|
typedef struct CfsCompressOptions
|
{
|
CfsAlgorithm algorithm; /* compression algorithm to use */
|
int level; /* algorithm-specific level (1 = fastest) */
|
bool explicit_algorithm; /* ERROR instead of fallback if
|
* unavailable */
|
bool allow_raw_fallback; /* allow RAW fallback when compression
|
* fails */
|
|
/*
|
* Minimum gain in bytes required to accept the compressed form. If
|
* (original_size - compressed_size) < min_gain_bytes, the page is stored
|
* RAW (CFS_ALGORITHM_RAW, full 8 kB payload) even when the requested
|
* algorithm is LZ4 or Zstd.
|
*/
|
uint32 min_gain_bytes;
|
} CfsCompressOptions;
|
|
/* ----------------------------------------------------------------
|
* Default option initialiser
|
* ---------------------------------------------------------------- */
|
#define CFS_COMPRESS_OPTIONS_INIT \
|
{ \
|
.algorithm = CFS_ALGORITHM_LZ4, \
|
.level = CFS_COMPRESS_LEVEL_DEFAULT, \
|
.explicit_algorithm = false, \
|
.allow_raw_fallback = true, \
|
.min_gain_bytes = CFS_MIN_GAIN_BYTES_DEFAULT, \
|
}
|
|
/* ----------------------------------------------------------------
|
* CfsCompressResult — output descriptor from CfsCompressPage().
|
* ---------------------------------------------------------------- */
|
typedef struct CfsCompressResult
|
{
|
/*
|
* Points into the caller-supplied scratch buffer. For a contiguous
|
* result (!payload_external) the region result_data[0 .. total_size)
|
* contains CfsCompressedBlockHeader followed by the payload and is
|
* ready to be written to the .cfp file in one pwrite() call. For an
|
* external payload (RAW, payload_external) only the
|
* CfsCompressedBlockHeader is stored here; the payload lives at
|
* payload_data and the record must be written with the vector form
|
* (see cfs_pwritev_all).
|
*/
|
void *result_data;
|
|
/*
|
* Total byte count: sizeof(CfsCompressedBlockHeader) + aligned payload.
|
* This is the number of bytes to append to the .cfp file.
|
*/
|
size_t total_size;
|
|
/*
|
* Actual compressed (or raw) payload size, without alignment padding.
|
* Stored in CfsMapEntry.compressed_size.
|
*/
|
uint32 compressed_size;
|
|
/*
|
* Algorithm actually used (may differ from opts.algorithm when the page
|
* was stored RAW due to insufficient gain).
|
*/
|
CfsAlgorithm algorithm_used;
|
|
/*
|
* True when the page was stored without compression (RAW).
|
*/
|
bool is_raw;
|
|
/*
|
* True when the payload is NOT laid out behind the header in the
|
* scratch buffer: payload_data then points at the caller's original
|
* page (the D4 zero-copy RAW path) and padding_bytes of zero padding
|
* must follow the payload on disk to reach total_size. The on-disk
|
* record is byte-identical to the copied form; only the write is
|
* vectorised (header, page, zero padding) through cfs_pwritev_all().
|
*/
|
bool payload_external;
|
void *payload_data; /* payload start when payload_external */
|
size_t padding_bytes; /* zero bytes after the payload */
|
} CfsCompressResult;
|
|
/*
|
* CfsDecompressResult — output descriptor from CfsDecompressPage().
|
*/
|
typedef struct CfsDecompressResult
|
{
|
size_t decompressed_size;
|
CfsAlgorithm algorithm_used;
|
bool is_raw;
|
} CfsDecompressResult;
|
|
/* ----------------------------------------------------------------
|
* Primary API
|
* ---------------------------------------------------------------- */
|
|
/*
|
* CfsCompressPage
|
* Compress a single PostgreSQL page.
|
*
|
* Parameters:
|
* page — pointer to the uncompressed 8 kB page (read-only).
|
* page_size — size of the page; must equal BLCKSZ (= CFS_BLCKSZ).
|
* opts — compression options (algorithm, level, min_gain_bytes, lsn).
|
* scratch — caller-supplied buffer of at least CfsCompressScratchSize()
|
* bytes. On success, result->result_data points into this
|
* buffer.
|
* scratch_sz — size of the scratch buffer in bytes.
|
* result — output descriptor; filled on success.
|
*
|
* Errors are reported via ereport(ERROR) only; there is no status return
|
* value.
|
*
|
* Thread safety:
|
* The function uses the per-backend CfsCodecState cached inside
|
* CfsCodecStateCreate(); callers must not share scratch buffers or
|
* CfsCompressResult across threads/backends.
|
*/
|
extern void CfsCompressPage(const void *page,
|
size_t page_size,
|
const CfsCompressOptions *opts,
|
void *scratch,
|
size_t scratch_sz,
|
CfsCompressResult *result);
|
|
/*
|
* CfsCompressPageNoCopy — the D4 write-path form of CfsCompressPage():
|
* identical decisions and identical on-disk bytes, but a RAW result is not
|
* copied into the scratch buffer. result_data then holds only the
|
* CfsCompressedBlockHeader, payload_external is true, payload_data points
|
* at the caller's original page and padding_bytes zero bytes must follow
|
* the payload on disk (write the record with cfs_pwritev_all()). Only the
|
* storage append path may use this form; every other consumer must stick
|
* to the contiguous CfsCompressPage() contract.
|
*/
|
extern void CfsCompressPageNoCopy(const void *page,
|
size_t page_size,
|
const CfsCompressOptions *opts,
|
void *scratch,
|
size_t scratch_sz,
|
CfsCompressResult *result);
|
|
/*
|
* CfsDecompressPage
|
* Decompress a block read from a .cfp file back into a full 8 kB page.
|
*
|
* Parameters:
|
* compressed_data — pointer to CfsCompressedBlockHeader followed immediately
|
* by the compressed payload (as stored in .cfp).
|
* data_size — byte count of compressed_data (header + payload).
|
* out_page — caller-supplied buffer of at least CFS_BLCKSZ bytes;
|
* receives the reconstructed uncompressed page.
|
* out_page_size — size of out_page; must be >= CFS_BLCKSZ.
|
* result — optional output descriptor; may be NULL. When non-NULL
|
* receives decompressed_size, algorithm_used, and is_raw.
|
*
|
* Returns:
|
* CFS_DECOMPRESS_OK — success; out_page contains the 8 kB page.
|
* CFS_DECOMPRESS_BAD_HDR — header CRC32C mismatch or bad magic.
|
* CFS_DECOMPRESS_BAD_DATA — compressed payload CRC32C mismatch.
|
* CFS_DECOMPRESS_CORRUPT — decompression library reported corruption.
|
* CFS_DECOMPRESS_BAD_ORIG — original-page CRC32C mismatch after decomp.
|
* CFS_DECOMPRESS_ERROR — other internal error.
|
*/
|
typedef enum CfsDecompressStatus
|
{
|
CFS_DECOMPRESS_OK = 0,
|
CFS_DECOMPRESS_BAD_HDR = 1, /* magic / version / header CRC bad */
|
CFS_DECOMPRESS_BAD_DATA = 2, /* payload CRC mismatch */
|
CFS_DECOMPRESS_CORRUPT = 3, /* codec reported corrupt data */
|
CFS_DECOMPRESS_BAD_ORIG = 4, /* original-page CRC mismatch */
|
CFS_DECOMPRESS_ERROR = 5, /* unexpected internal error */
|
} CfsDecompressStatus;
|
|
extern CfsDecompressStatus CfsDecompressPage(const void *compressed_data,
|
size_t data_size,
|
void *out_page,
|
size_t out_page_size,
|
CfsDecompressResult *result);
|
|
/* ----------------------------------------------------------------
|
* Utility helpers
|
* ---------------------------------------------------------------- */
|
|
/*
|
* CfsCompressScratchSize(page_size)
|
* Return the minimum scratch buffer size needed by CfsCompressPage()
|
* for the given page size. In practice page_size == BLCKSZ.
|
*
|
* The scratch buffer must hold:
|
* CfsCompressedBlockHeader + max_compressed_payload (all algorithms)
|
*/
|
extern size_t CfsCompressScratchSize(size_t page_size);
|
extern CfsAlgorithm CfsAlgorithmFromName(const char *name);
|
extern const char *CfsAlgorithmGetName(CfsAlgorithm algorithm);
|
|
/*
|
* CfsDecompressStatusString
|
* Human-readable status string for error reporting.
|
*/
|
extern const char *CfsDecompressStatusString(CfsDecompressStatus status);
|
|
/*
|
* CfsVerifyBlockHeader
|
* Validate magic, version, and header CRC of a CfsCompressedBlockHeader.
|
* Returns true on success, false on any mismatch.
|
* Used by pg_cfs_check and recovery code.
|
*/
|
extern bool CfsVerifyBlockHeader(const CfsCompressedBlockHeader *hdr);
|
|
extern uint32 CfsCrc32cHeader(const CfsCompressedBlockHeader *hdr);
|
extern uint32 CfsCrc32cPayload(const void *payload, size_t payload_size);
|
extern void CfsFillBlockHeader(CfsCompressedBlockHeader *hdr,
|
CfsAlgorithm algorithm,
|
const void *compressed_data,
|
uint32 compressed_size,
|
const void *original_data,
|
uint32 original_size);
|
|
#endif /* CFS_COMPRESS_H */
|