/*-------------------------------------------------------------------------
|
* cfs_storage.h
|
* Compressed File Store (CFS) — autonomous storage engine.
|
*
|
* This module implements the standalone CFS storage layer: it owns the three
|
* sidecar files (.cfp / .cfsmap / .cfsjrn) for one relation fork and provides
|
* page-level read/write, crash-safe map updates through the journal, journal
|
* replay and compaction primitives. It has no dependency on the buffer
|
* manager, smgr or shared memory; integration with those subsystems happens
|
* through the storage interface declared in this file
|
* the bottom of this file.
|
*
|
* Crash-safety model
|
* ------------------
|
* 1. compress page -> form CfsCompressedBlockHeader
|
* 2. append record at aligned EOF of .cfp
|
* 3. append an UPDATE journal record to .cfsjrn
|
* 4. update the in-memory map
|
* 5. the .cfsmap snapshot is rewritten only at checkpoint time
|
*
|
* On open the in-memory map is rebuilt by loading the last .cfsmap snapshot
|
* and replaying the .cfsjrn; the replay is idempotent, so it can be repeated
|
* safely. After a successful checkpoint the journal is truncated.
|
*
|
* Copyright (c) 2026, Kirillov Vyacheslav, QDB Development Group
|
*
|
* IDENTIFICATION
|
* src/include/storage/cfs/cfs_storage.h
|
*
|
*-------------------------------------------------------------------------
|
*/
|
#ifndef CFS_STORAGE_H
|
#define CFS_STORAGE_H
|
|
#include "access/xlogdefs.h"
|
#include "common/relpath.h"
|
#include "storage/block.h"
|
#include "storage/cfs/cfs.h"
|
#include "storage/cfs/cfs_compress.h"
|
#include "storage/cfs/cfs_format.h"
|
#include "storage/cfs/cfs_path.h"
|
#include "storage/relfilelocator.h"
|
|
/*
|
* The operation outcome codes (CfsStorageStatus) moved to the common
|
* base header cfs.h: even the raw I/O layer (cfs_io.h) returns them.
|
*/
|
|
/* ----------------------------------------------------------------
|
* CfsOpenFlags — passed to cfs_open().
|
* ---------------------------------------------------------------- */
|
typedef enum CfsOpenFlags
|
{
|
CFS_OPEN_RDONLY = 1 << 0, /* open sidecars read-only */
|
CFS_OPEN_RDWR = 1 << 1, /* open sidecars read/write */
|
CFS_OPEN_CREATE = 1 << 2, /* create sidecars if they are absent */
|
CFS_OPEN_STRICT = 1 << 3, /* treat corruption as a hard error */
|
|
/*
|
* Repair a torn journal tail (a half-written trailing record left by a
|
* crash) by truncating it. Safe in a single-writer context (the recovery
|
* scan run by CfsRecoveryInit() before any backend can write) and under
|
* the per-relation append lock, which excludes every writer mid-append:
|
* compaction opens with it, and the refresh-based paths (no-pool reads,
|
* nblocks, immedsync, pool segment sync) hold the lock. The normal
|
* lock-free handle open must NOT set this: a concurrent writer mid-append
|
* leaves exactly such a trailing record under the append lock, and
|
* truncating it out from under the writer corrupts the record (see
|
* cfs_map_load()).
|
*/
|
CFS_OPEN_REPAIR_TAIL = 1 << 4, /* truncate torn journal tail */
|
|
/*
|
* Open the handle "lazily": do not load the full in-memory page map from
|
* .cfsmap + .cfsjrn. The block map is then resolved through the shared
|
* segment pool (cfs_shmem.c) on every access; cfs->map stays NULL and the
|
* map_hdr.nblocks counter is maintained directly. Used by the regular
|
* smgr paths, which must not spend per-process memory on a full private
|
* map copy. Compaction and the snapshot writer still open full
|
* (non-lazy) handles; the startup scan folds journals through a lazy
|
* one, so no map is ever loaded at startup.
|
*/
|
CFS_OPEN_LAZY_MAP = 1 << 5, /* no private map; shared segment pool */
|
|
/*
|
* The open runs in the startup recovery scan: corruption discovered by
|
* the map load follows cfs_recovery_strict severity (WARNING by default)
|
* instead of cfs_checksum_failure, because at startup an ERROR already
|
* means "the server does not come up". Since the startup scan folds
|
* journals on a lazy handle without a map load (cfs_replay_one_locator),
|
* no current caller sets this; it stays defined for a recovery-context
|
* full-map open.
|
*/
|
CFS_OPEN_RECOVERY_SCAN = 1 << 6, /* startup-scan corruption policy */
|
|
/*
|
* A caller that may legitimately find the sidecars gone between its
|
* existence check and this open (the checkpoint racing a DROP) asks for
|
* NULL instead of an ERROR when all three sidecar files are missing. Only
|
* the fully-missing case: a half-dropped pair is damage and still fails
|
* loudly.
|
*/
|
CFS_OPEN_MISSING_OK = 1 << 7, /* all-missing sidecars: NULL, not ERROR */
|
} CfsOpenFlags;
|
|
/* ----------------------------------------------------------------
|
* CfsMapLoadContext — who is loading the map; picks the corruption
|
* reporting channel (see cfs_map_load()).
|
* ---------------------------------------------------------------- */
|
typedef enum CfsMapLoadContext
|
{
|
CFS_LOAD_NORMAL, /* backend: cfs_checksum_failure policy */
|
CFS_LOAD_RECOVERY, /* startup scan: cfs_recovery_strict */
|
CFS_LOAD_READ, /* smgr read path: defer only row-level
|
* corruption (rows with no covering journal
|
* record), re-reported by the AIO consumer
|
* aio_cfs_readv_report() (or cfs_smgr_readv()
|
* on the synchronous path). Structural
|
* prologue/journal-header damage still raises
|
* ERROR; cfs_smgr_startreadv() converts it
|
* into the AIO failure code, not a throw. */
|
} CfsMapLoadContext;
|
|
/* ----------------------------------------------------------------
|
* CfsWriteOptions — per-write compression parameters.
|
* ---------------------------------------------------------------- */
|
typedef struct CfsWriteOptions
|
{
|
CfsAlgorithm algorithm; /* algorithm to use for this page */
|
int level; /* compression level (1 = fastest) */
|
bool explicit_algorithm; /* ERROR if algorithm unavailable */
|
bool allow_raw_fallback; /* fall back to RAW if not shrinking */
|
uint32 min_gain_bytes; /* minimum gain to accept compression */
|
bool skip_fsync; /* bulk write: do not register dirty files;
|
* the caller arranges the end-of-operation
|
* sync itself (smgrregistersync /
|
* smgrimmedsync), mirroring md.c */
|
|
/*
|
* True when this write is a genuine relation extension (smgrextend), so
|
* cfs_write_page journals a RESIZE record to make nblocks recovery
|
* deterministic. Plain overwrites via the smgr write path (bgwriter /
|
* checkpoint flushes of already-extended pages) set this to false: their
|
* growth of the logical size was already journal-recorded by the
|
* originating extend, and a stale-handle flush must not emit a RESIZE
|
* that could shrink the replayed size below the real one.
|
*/
|
bool is_extend;
|
} CfsWriteOptions;
|
|
#define CFS_WRITE_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, \
|
.skip_fsync = false, \
|
.is_extend = true, \
|
}
|
|
/* ----------------------------------------------------------------
|
* CfsRelation — in-memory descriptor of an open CFS relation fork.
|
*
|
* All paths are palloc'd in the context that owned cfs_open(); the
|
* compression scratch buffer is allocated once and reused for every write.
|
* ---------------------------------------------------------------- */
|
typedef struct CfsRelation
|
{
|
RelFileLocatorBackend rlocator; /* relation identity (may be synthetic) */
|
ForkNumber forknum; /* always MAIN_FORKNUM */
|
int flags; /* CfsOpenFlags bitmask */
|
|
/* Sidecar file paths (relative to DataDir). */
|
char *cfp_path;
|
char *map_path;
|
char *jrn_path;
|
|
/*
|
* Raw file descriptors (-1 when closed). The .cfp file is segmented (see
|
* CFS_SEGMENT_SIZE in cfs.h): cfp_fd is the descriptor of the segment
|
* that currently receives appends (cfp_append_segno), while cfp_read_fd
|
* is a one-entry cache of the segment used by the last read
|
* (cfp_read_segno). Map and journal are single-segment files.
|
*/
|
int cfp_fd;
|
uint32 cfp_append_segno;
|
int cfp_read_fd;
|
uint32 cfp_read_segno;
|
int map_fd;
|
int jrn_fd;
|
|
/* In-memory page map state (nothing of it is stored in .cfsmap). */
|
CfsMapState map_hdr; /* nblocks, cfp_size, max_generation */
|
CfsMapEntry *map; /* array [0 .. map_capacity) */
|
uint64 map_capacity; /* allocated slots in *map */
|
|
/*
|
* Append cursors (byte offsets of next append). cfp_write_off is a
|
* global offset across the segmented .cfp chain (see CFS_SEGMENT_SIZE).
|
*/
|
off_t cfp_write_off; /* end of .cfp */
|
off_t jrn_write_off; /* end of .cfsjrn (first free record slot) */
|
|
/*
|
* Highest .cfsjrn byte offset already reflected in the in-memory map.
|
* Used by cfs_refresh_map() to replay only the journal delta on a cache
|
* miss, keeping the per-backend map current without re-reading the whole
|
* snapshot on every miss.
|
*/
|
off_t map_synced_jrn_off;
|
|
/*
|
* Journal incarnation observed when the in-memory map was last synced
|
* (see CfsJournalFileHeader.incarnation). cfs_refresh_map() compares the
|
* on-disk incarnation against this value: a mismatch means the journal
|
* was reset by a checkpoint or a compaction swap and the delta replay
|
* cursor is meaningless, so a full reload is required even if the file
|
* grew back past the saved offset (ABA).
|
*/
|
uint16 jrn_incarnation;
|
|
/*
|
* Map generation observed in the shared cache when this handle was (last)
|
* opened. Compaction swaps the sidecar files and bumps the generation; a
|
* writer holding a stale handle detects the mismatch under the
|
* per-relation append lock and reopens before writing (see
|
* cfs_handle_refresh_if_stale()).
|
*/
|
uint64 open_generation;
|
|
/* Compression scratch buffer (sized for one BLCKSZ page). */
|
void *compress_scratch;
|
size_t compress_scratch_sz;
|
|
bool read_only; /* opened CFS_OPEN_RDONLY */
|
bool strict; /* corruption is a hard error */
|
|
/*
|
* The last map load left rows that failed their entry_crc and that no
|
* journal record healed: they load as INVALID, but the on-disk damage
|
* must not be papered over. A writer that persists the in-memory map
|
* must check this flag and refuse - saving the snapshot would replace
|
* the corrupt rows with valid INVALID ones and truncate away the
|
* journal, laundering the corruption off disk. (The startup scan no
|
* longer loads maps: since it folds journals row by row, it never
|
* touches uncovered rows at all.)
|
*/
|
bool unhealed_corruption;
|
} CfsRelation;
|
|
/* ----------------------------------------------------------------
|
* Relation lifecycle
|
* ---------------------------------------------------------------- */
|
|
/*
|
* cfs_open — open the sidecar files for a relation fork.
|
*
|
* With CFS_OPEN_CREATE the sidecars are created if absent. The in-memory
|
* map is rebuilt by loading the .cfsmap snapshot and replaying the journal.
|
* Returns a palloc'd CfsRelation, or NULL when the locator is unsupported
|
* (temporary relations are unsupported).
|
*/
|
extern CfsRelation *cfs_open(RelFileLocatorBackend rlocator,
|
ForkNumber forknum, int flags);
|
|
/*
|
* cfs_open_paths — like cfs_open(), but with explicit sidecar paths. Used
|
* by the test harness and by utilities that operate on arbitrary paths.
|
* 'rlocator' / 'forknum' are stored for diagnostics only.
|
*/
|
extern CfsRelation *cfs_open_paths(RelFileLocatorBackend rlocator,
|
ForkNumber forknum,
|
const char *cfp_path,
|
const char *map_path,
|
const char *jrn_path,
|
int flags);
|
|
/* Close all descriptors and release the CfsRelation. */
|
extern void cfs_close(CfsRelation *cfs);
|
|
/*
|
* cfs_reopen — close and reopen the sidecar files of an existing handle,
|
* reloading the in-memory map. Used when a compaction swapped the files
|
* underneath this handle: the old descriptors point at the replaced
|
* (orphaned) inode and the in-memory map holds stale physical offsets.
|
*/
|
extern int cfs_reopen(CfsRelation *cfs);
|
|
/*
|
* cfs_map_file_swapped — true when the .cfsmap path names a different
|
* inode/device than the handle's descriptor (a compaction or immedsync
|
* replaced the snapshot after it was opened). Declared in cfs_state.h:
|
* it is the map-side half of the ownership-state judge.
|
*/
|
|
/*
|
* cfs_reopen_if_map_swapped — reopen the handle when the .cfsmap path now
|
* points at a different inode than cfs->map_fd. The caller must hold the
|
* per-relation append lock: checkpoint and compaction replace the snapshot
|
* only under its exclusive mode.
|
*/
|
extern bool cfs_reopen_if_map_swapped(CfsRelation *cfs);
|
|
/* Create fresh sidecar files for a relation and return an open handle. */
|
extern CfsRelation *cfs_create(RelFileLocatorBackend rlocator,
|
ForkNumber forknum);
|
|
/* Remove all three sidecar files for a relation fork. */
|
extern void cfs_unlink(RelFileLocatorBackend rlocator, ForkNumber forknum);
|
|
/* Return true when at least one sidecar file exists for the relation. */
|
extern bool cfs_exists(RelFileLocatorBackend rlocator, ForkNumber forknum);
|
|
/* Return the cached path of a sidecar kind (NULL when out of range). */
|
extern const char *cfs_relation_path(const CfsRelation *cfs, CfsFileKind kind);
|
|
/* ----------------------------------------------------------------
|
* .cfp append-only data file
|
* ---------------------------------------------------------------- */
|
|
/*
|
* cfs_append_compressed_record — append an already-compressed record
|
* (produced by cfs_compress_page) to the .cfp file. Fills *out_entry with
|
* the CfsMapEntry describing the new record. Does NOT touch the journal or
|
* the in-memory map (the caller coordinates that). The caller must hold the
|
* per-relation append lock.
|
*/
|
extern int cfs_append_compressed_record(CfsRelation *cfs,
|
const CfsCompressResult *cres,
|
CfsMapEntry *out_entry);
|
|
/*
|
* cfs_compress_page — compress one page into the per-handle scratch buffer
|
* and report the compress as a wait event (WAIT_EVENT_CFS_WRITE). Fills
|
* *cres with the CfsCompressResult. This is the single shared entry point for
|
* page compression used by cfs_write_page_cached (hot path, compress outside
|
* the append lock) and cfs_write_page (ops wrapper). Does NOT touch the
|
* .cfp file, journal or map.
|
*/
|
extern int cfs_compress_page(CfsRelation *cfs, const void *page,
|
size_t page_size,
|
const CfsWriteOptions *opts,
|
CfsCompressResult *cres);
|
|
/*
|
* cfs_read_record — read and decompress the record stored at 'offset' in
|
* .cfp into 'dst' (must be >= CFS_BLCKSZ bytes). 'compressed_size' is the
|
* payload size recorded in the map entry. 'offset' is a global offset
|
* across the segmented .cfp chain.
|
*/
|
extern int cfs_read_record(CfsRelation *cfs, off_t offset,
|
uint32 compressed_size, void *dst,
|
size_t dst_size);
|
|
/*
|
* cfs_read_records_span — bulk variant of cfs_read_record() for a run of
|
* physically contiguous records of one .cfp segment (entries[i+1].offset ==
|
* entries[i].offset + CFS_RECORD_SIZE(entries[i].size)). One pread serves
|
* the whole span; each record is validated and decompressed into
|
* buffers[b] exactly as cfs_read_record() would. On failure returns the
|
* status of the first failure and stores the failing block number
|
* (first_block, or first_block + i for a record-level failure) in
|
* *fail_block when non-NULL.
|
*/
|
extern int cfs_read_records_span(CfsRelation *cfs,
|
const CfsMapEntry *entries,
|
BlockNumber first_block,
|
BlockNumber nentries, void **buffers,
|
BlockNumber *fail_block);
|
|
/*
|
* cfs_read_raw_record — read CFS_RECORD_SIZE bytes of the record stored at
|
* the global .cfp offset 'offset' into 'buf' verbatim (no decompression).
|
* Used by compaction, which copies live records byte-for-byte.
|
*/
|
extern int cfs_read_raw_record(CfsRelation *cfs, off_t offset,
|
uint32 compressed_size, void *buf);
|
|
/*
|
* cfs_pwrite_all / cfs_pread_all / cfs_fd_size — the raw I/O primitives —
|
* moved to cfs_io.h (the lowest, domain-free layer of the subsystem).
|
*/
|
#include "storage/cfs/cfs_io.h"
|
|
/*
|
* Callback for cfs_journal_replay_segment(): one applied journal record.
|
* 'entry' is the new map entry for 'blocknum', or NULL when the record
|
* removes the entry (DELETE, or a RESIZE shrinking past the block). The
|
* callback must not raise errors: a record it cannot apply is skipped by
|
* the caller's policy.
|
*/
|
typedef void (*CfsJournalSegCB) (BlockNumber blocknum,
|
const CfsMapEntry *entry,
|
void *arg);
|
|
/*
|
* cfs_journal_replay_segment — replay journal records from 'start_off'
|
* whose target block lies in [first_block, first_block +
|
* CFS_MAP_SEGMENT_BLOCKS), delivering each through 'cb'. RESIZE records
|
* shrink the segment range like DELETE. Stops at a torn trailing record
|
* (the writer may be mid-append under the append lock the caller holds).
|
* A journal header that does not validate raises an ERROR: every reset
|
* leaves a header behind, so its absence or damage is corruption, never a
|
* legitimate empty journal. Returns the offset the replay stopped at
|
* (>= start_off; the caller stores it as the segment's journal sync
|
* position), or -1 on a read error.
|
*/
|
extern off_t cfs_journal_replay_segment(CfsRelation *cfs, off_t start_off,
|
BlockNumber first_block,
|
CfsJournalSegCB cb, void *arg);
|
|
/* cfs_recover_compaction moved to cfs_swap.h. */
|
|
|
/* fsync one sidecar kind. */
|
extern int cfs_fsync_kind(CfsRelation *cfs, CfsFileKind kind);
|
|
/*
|
* cfs_read_cfp_epoch — read the file-creation epoch from the .cfp lead-in.
|
* Used by incremental base backup to prove append-only continuity with a
|
* prior backup. Returns false (with *epoch == 0) when unreadable.
|
*/
|
extern bool cfs_read_cfp_epoch(const char *cfp_path, uint64 *epoch);
|
|
/* ----------------------------------------------------------------
|
* .cfsmap page map snapshot
|
* ---------------------------------------------------------------- */
|
|
/* Rebuild the in-memory map: load snapshot + replay journal.
|
*
|
* When repair_torn_tail is true, physically truncate a genuine torn trailing
|
* journal record. This is only safe for a caller that holds the per-relation
|
* append lock or runs in a single-writer recovery context; the normal handle
|
* open must pass false (see cfs_open_paths()). A stop in the middle of the
|
* journal (valid records follow the torn one) is corruption: it is never
|
* truncated, the handle is marked unhealed and the journal is preserved.
|
*
|
* 'context' selects how unhealed corruption (a row whose CRC fails and whose
|
* block no journal record covers) is reported: a recovery-context load
|
* follows cfs_recovery_strict (WARNING by default); the smgr read path
|
* defers only that row-level report and returns CFS_STORAGE_CORRUPT for the
|
* smgr/AIO report path to re-report centrally through the backend's
|
* cfs_checksum_failure policy; every other caller reports through that
|
* backend policy right away. Structural prologue/header errors still raise;
|
* cfs_smgr_startreadv() converts them into the AIO failure code. */
|
extern int cfs_map_load(CfsRelation *cfs, bool repair_torn_tail,
|
CfsMapLoadContext context);
|
|
/*
|
* cfs_refresh_map — bring this backend's in-memory page map up to date with
|
* the on-disk .cfsmap snapshot + .cfsjrn by replaying only the journal delta
|
* since the last refresh (or doing a full reload if the journal was
|
* truncated by a checkpoint). Used on a shared-cache miss so a cross-backend
|
* update is never read from a stale local map.
|
*
|
* 'repair' may truncate a torn journal tail and must be false unless the
|
* caller holds the per-relation append lock: only then can no writer be
|
* mid-append (the checkpoint's lock-free phase 1 passes false). The
|
* same flag gates the swapped-sidecar branches: with repair == false a
|
* compaction-swapped handle returns CFS_STORAGE_NOENT (never reopens -
|
* resolving an interrupted swap needs the lock), and the caller must
|
* skip the relation; lock-holding callers reopen as before.
|
*
|
* 'context' routes unhealed corruption the way cfs_map_load() does: the
|
* read path defers only that row-level report (the refresh may run inside
|
* an AIO callback), every other caller reports through the backend policy
|
* immediately. Structural prologue/header errors still raise and are
|
* converted into the AIO failure code by cfs_smgr_startreadv().
|
*/
|
extern int cfs_refresh_map(CfsRelation *cfs, bool repair,
|
CfsMapLoadContext context);
|
|
/*
|
* cfs_map_needs_refresh — cheap unlocked probe: has the journal moved since
|
* this handle last synced its private map? A false positive costs a delta
|
* replay; a false negative would serve stale data, so an unreadable header
|
* is reported as "needs refresh". Only meaningful for a full handle.
|
*/
|
extern bool cfs_map_needs_refresh(CfsRelation *cfs);
|
|
/* Atomically write the in-memory map to .cfsmap (tmp + fsync + rename). */
|
extern int cfs_map_save_snapshot(CfsRelation *cfs);
|
|
/*
|
* Return a pointer to entry 'blocknum', growing the array capacity as needed.
|
* Pure lookup: never changes map_hdr.nblocks (see cfs_write_page /
|
* cfs_zero_extend for explicit, journaled growth).
|
*/
|
extern CfsMapEntry *cfs_map_get_entry(CfsRelation *cfs, BlockNumber blocknum);
|
|
/* Overwrite entry 'blocknum' with *entry. */
|
extern void cfs_map_set_entry(CfsRelation *cfs, BlockNumber blocknum,
|
const CfsMapEntry *entry);
|
|
/* Ensure the map array can hold at least 'capacity' slots. */
|
extern void cfs_map_ensure_capacity(CfsRelation *cfs, uint64 capacity);
|
|
/* Logical number of blocks (from the snapshot rows count / map state). */
|
extern BlockNumber cfs_map_nblocks(const CfsRelation *cfs);
|
|
/* Mark all entries >= nblocks invalid and shrink the logical size. */
|
extern void cfs_map_truncate(CfsRelation *cfs, BlockNumber nblocks);
|
|
/* ----------------------------------------------------------------
|
* .cfsjrn journal
|
* ---------------------------------------------------------------- */
|
|
/* Append an UPDATE record. */
|
extern void cfs_journal_append_update(CfsRelation *cfs, BlockNumber blocknum,
|
const CfsMapEntry *new_entry);
|
|
/* Append a RESIZE record (target nblocks stored in blocknum field). */
|
extern void cfs_journal_append_resize(CfsRelation *cfs, BlockNumber nblocks);
|
|
/*
|
* cfs_journal_replay — replay every valid journal record against the
|
* in-memory map. Idempotent: replaying twice yields the same map.
|
* Returns the number of records applied, or -1 on hard error. *stop_off
|
* is set to the offset where the replay stopped (the first torn/invalid
|
* record or the clean end of records), suitable for baselining
|
* map_synced_jrn_off. *mid_file_stop reports whether that stop was
|
* corruption (valid records follow the torn one) rather than a crash-torn
|
* tail; the caller must preserve the journal in that case.
|
*/
|
extern int64 cfs_journal_replay(CfsRelation *cfs, off_t *stop_off,
|
bool *mid_file_stop,
|
char *corrupt_mask, uint64 corrupt_mask_len,
|
CfsMapLoadContext context);
|
|
/*
|
* cfs_journal_scan_max_generation — unexported (review M2): its only
|
* production caller (cfs_generation_summary) lives in cfs_storage.c.
|
*/
|
|
/*
|
* cfs_generation_summary - the relation's generation watermark: the highest
|
* generation the in-memory map holds, raised by the journal header stamp and
|
* by every UPDATE record still in the journal. One call for every consumer
|
* that needs the full value (the shared counter seed); callers that only
|
* need the map watermark call cfs_map_live_summary() directly.
|
*/
|
extern int cfs_generation_summary(CfsRelation *cfs, uint32 *max_generation);
|
|
/*
|
* Result of one cfs_journal_fold() pass. The fold never truncates the map
|
* or the journal: it reports the last RESIZE it saw and the generation
|
* watermark of the records it applied, and the checkpoint decides when to
|
* apply the resize and reset the journal (phase 2, under the append lock).
|
*/
|
typedef struct CfsJournalFoldResult
|
{
|
off_t stop_off; /* where the fold stopped */
|
uint64 superseded_pages; /* previously valid rows overwritten */
|
uint64 superseded_bytes; /* payload bytes of those rows: the
|
* checkpoint's dead-space delta, in
|
* CFS_RECORD_SIZE units */
|
uint32 max_generation; /* highest applied UPDATE generation */
|
BlockNumber resize_target; /* last RESIZE target */
|
bool have_resize; /* a RESIZE record was seen */
|
bool torn_stop; /* stopped on a torn record */
|
bool torn_is_tail; /* ...and that record is the file tail */
|
} CfsJournalFoldResult;
|
|
/*
|
* cfs_journal_fold - fold journal records in [start_off, limit) into the
|
* .cfsmap file in place: one row per UPDATE/DELETE record at its
|
* positional offset, one map fsync per pass. A record at or beyond the
|
* caller's fold boundary stops the pass and survives as the journal tail.
|
* RESIZE records are reported, never applied here (stage 1 runs without
|
* the append lock; the caller resizes in phase 2). Stops at the first
|
* torn record, classified like the replay. Returns the folded row count,
|
* or -1 (error raised).
|
*
|
* Stages: 1 is the checkpoint's lock-free phase-1 pass and 2 its locked
|
* phase-2 tail pass; 3 (CFS_FOLD_STAGE_RECOVERY) is the startup recovery
|
* fold of cfs_replay_one_locator(). The stage selects the severity and
|
* the test seams: stage 2 escalates a row-write failure to PANIC via
|
* data_sync_elevel (it runs under the append lock), stages 1 and 3 raise
|
* a plain ERROR; stage 3 routes skipped UPDATE records through the
|
* recovery channel (sink.recovery_severity, cfs_recovery_strict), stamps
|
* its torn/mid-file reports with the "recovery" role and carries its own
|
* injection points (cfs-fail-journal-read-recovery,
|
* cfs-recovery-fold-row).
|
*/
|
#define CFS_FOLD_STAGE_RECOVERY 3
|
|
extern int64 cfs_journal_fold(CfsRelation *cfs, off_t start_off, off_t limit,
|
int stage, CfsJournalFoldResult *result);
|
|
/*
|
* cfs_journal_incarnation - the journal's current on-disk incarnation -
|
* was absorbed by the ownership-state judge (cfs_state.h).
|
*/
|
|
/*
|
* cfs_map_live_summary - the full-map scan returning the relation's
|
* generation watermark (the highest generation the in-memory map holds).
|
* Only the paths that legitimately own a full map call it (immedsync,
|
* the pool seed); the checkpoint and the startup recovery fold never do -
|
* they take the watermark from the journal header and their folds, so
|
* their cost stays proportional to the journal, never the table.
|
*/
|
extern uint32 cfs_map_live_summary(CfsRelation *cfs);
|
|
/* ----------------------------------------------------------------
|
* Page-level read/write API
|
* ---------------------------------------------------------------- */
|
|
/*
|
* cfs_read_page — look up blocknum, read & decompress into dst (>= BLCKSZ).
|
* A never-written block inside the logical range returns a zero page.
|
*/
|
extern int cfs_read_page(CfsRelation *cfs, BlockNumber blocknum,
|
char *dst);
|
|
/*
|
* cfs_write_compressed_page — append an already-compressed record and
|
* journal it. When 'out_entry' is not NULL it receives the new map entry
|
* (with the caller-supplied generation and CRC), so lazy handles can
|
* publish it into the shared segment pool; full handles additionally
|
* store it in their private map.
|
*/
|
extern int cfs_write_compressed_page(CfsRelation *cfs,
|
BlockNumber blocknum,
|
const CfsCompressResult *cres,
|
const CfsWriteOptions *opts,
|
uint32 generation,
|
CfsMapEntry *out_entry);
|
|
/*
|
* cfs_write_page — compress, append to .cfp, journal the update and update
|
* the in-memory map atomically.
|
*/
|
/*
|
* Single-threaded API: append a pre-formatted page WITHOUT the append lock.
|
*
|
* Contract: only with a pin, and without parallel smgr writers. The
|
* caller must (a) be the only writer of this relation — no concurrent
|
* smgr writer, compaction or checkpoint may touch it; the smgr path uses
|
* cfs_write_page_cached() under the append lock instead — and (b) hold a
|
* pin on the relation's metadata slot (cfs_map_cache_pin()) for the whole
|
* call whenever the relation may have a slot. The entry maintains the
|
* shared .cfp tail protocol on the caller's behalf (D1): an unpinned slot
|
* could be evicted and recycled mid-call, so without a pin the relation
|
* must not have a slot at all (the protocol then degrades to the
|
* historical per-append cfs_cfp_discover_eof() walk).
|
*/
|
extern int cfs_write_page(CfsRelation *cfs, BlockNumber blocknum,
|
const char *src,
|
const CfsWriteOptions *opts);
|
|
/*
|
* Extend the logical map by 'count' zero blocks starting at 'start'. The
|
* appended RESIZE record dirties the journal; registering it with the
|
* checkpointer sync queue is the caller's job (the smgr wrapper does it
|
* outside the append lock).
|
*/
|
extern int cfs_zero_extend(CfsRelation *cfs, BlockNumber start,
|
BlockNumber count);
|
|
/* Truncate the relation to 'nblocks' logical blocks. */
|
extern int cfs_truncate(CfsRelation *cfs, BlockNumber nblocks);
|
|
/*
|
* The CfsStorageOps dispatch table was removed: it had exactly one
|
* implementation, no test ever swapped it, and the smgr bridge already
|
* calls cfs_open and cfs_fsync_kind directly. Integrations call the
|
* storage functions themselves.
|
*/
|
|
#endif /* CFS_STORAGE_H */
|