/*-------------------------------------------------------------------------
|
* cfs_compact.h
|
* Compressed File Store (CFS) — compaction primitives.
|
*
|
* The .cfp file is append-only: every page update leaves the previous
|
* compressed record unreachable (dead) until compaction reclaims the space.
|
* This header declares the low-level compaction primitives that rebuild the
|
* .cfp and .cfsmap sidecars containing only the live records.
|
*
|
* Compaction is not exposed through SQL directly; it is driven by the
|
* admin function or by VACUUM when the dead-space threshold is
|
* reached. The caller is expected to hold a relation lock that keeps
|
* DDL and competing maintenance out: ShareUpdateExclusiveLock in the
|
* online mode (cfs_compact_online, QDB-1514 — readers and writers keep
|
* running, the catch-up phase carries their appends over) or
|
* AccessExclusiveLock in the offline mode (all access blocked for the
|
* whole duration).
|
*
|
* Copyright (c) 2026, Kirillov Vyacheslav, QDB Development Group
|
*
|
* IDENTIFICATION
|
* src/include/storage/cfs/cfs_compact.h
|
*
|
*-------------------------------------------------------------------------
|
*/
|
#ifndef CFS_COMPACT_H
|
#define CFS_COMPACT_H
|
|
#include "common/relpath.h"
|
#include "storage/cfs/cfs_storage.h"
|
#include "storage/relfilelocator.h"
|
|
typedef struct CfsCompactOptions
|
{
|
bool fsync; /* fsync new files before the rename */
|
bool reset_journal; /* truncate .cfsjrn after a successful swap */
|
} CfsCompactOptions;
|
|
#define CFS_COMPACT_OPTIONS_DEFAULT \
|
{ \
|
.fsync = true, \
|
.reset_journal = true, \
|
}
|
|
typedef struct CfsCompactResult
|
{
|
uint64 live_blocks; /* live records copied */
|
uint64 dead_blocks; /* dead records skipped */
|
uint64 old_cfp_size; /* .cfp size before compaction */
|
uint64 new_cfp_size; /* .cfp size after compaction */
|
uint64 reclaimed_bytes; /* old_cfp_size - new_cfp_size */
|
} CfsCompactResult;
|
|
/*
|
* cfs_compact_copy_live_records — copy every live record from the source
|
* .cfp chain into a fresh chain of temporary segment files rooted at
|
* 'dst_cfp_path' (segment 0 is "<dst>.tmp", segment N is "<dst>.N.tmp"),
|
* building a fresh map in 'dst_map'. Physical offsets in 'dst_map' are
|
* global offsets across the new segment chain (see CFS_SEGMENT_SIZE).
|
* The temporary files are closed before returning (and fsync'd first when
|
* 'do_fsync' is true); *out_eof receives the global EOF offset of the new
|
* chain. Returns the number of records copied.
|
*/
|
extern int64 cfs_compact_copy_live_records(const CfsRelation *cfs,
|
const char *dst_cfp_path,
|
CfsMapEntry *dst_map,
|
bool do_fsync,
|
uint64 *out_eof);
|
|
/*
|
* cfs_compact_relation — rebuild the .cfp and .cfsmap sidecars of a relation
|
* fork so that they contain only live records, then reset the journal.
|
* In the online mode (cfs_compact_online) the copy phase runs without the
|
* append lock and a short catch-up phase carries concurrently appended
|
* records over; the caller must hold a relation lock that excludes DDL and
|
* competing maintenance. In the offline mode the caller must additionally
|
* guarantee that no other backend is writing to the relation.
|
* Returns CFS_STORAGE_OK on success and fills *result.
|
*
|
* 'progress_relid' — when valid, progress is reported through the standard
|
* pgstat progress API (PROGRESS_COMMAND_CFS_COMPACT) so that
|
* pg_stat_progress_cfs_compact shows the running compaction. Pass
|
* InvalidOid to disable progress reporting (used by tests / utilities that
|
* operate on bare locators without an OID).
|
*/
|
extern int cfs_compact_relation(RelFileLocatorBackend rlocator,
|
ForkNumber forknum,
|
const CfsCompactOptions *opts,
|
CfsCompactResult *result,
|
Oid progress_relid);
|
|
#endif /* CFS_COMPACT_H */
|