schema: spec-driven status: superseded superseded-by: 2026-11-12-cfs-direct-port-from-qdbpg superseded-date: 2026-11-12 --- > **SUPERSEDED (2026-11-12).** The clean-room implementation track recorded > here (phases 0-4 partial artifacts on branch `pers/pshkenev/cfs-qdb`) has > been abandoned in favor of a direct code port of the completed CFS > subsystem from the qdbpg fork (branch `origin/pers/vkirillov/cfs`, tip > `bb950a4793ef54449c4f7668390e6a535fd57d4b`). See > `openspec/changes/2026-11-12-cfs-direct-port-from-qdbpg/` for the active > change. This change is kept for history; no further work happens under it. ## Why RuDB (PG19 fork) needs transparent page-level on-disk compression for user relations, functionally akin to Postgres Pro Enterprise's CFS. A detailed implementation plan already exists and is adopted as the single source of truth for architecture, on-disk formats, and phasing: - Confluence page id `467513822`, "План имплементации CFS для ИИ", space `QDatabase`, version 8 (2026-07-10), author vkirillov. https://conf.diasoft.ru/pages/viewpage.action?pageId=467513822 Decision (2026-11-06): follow that plan, not any alternative sketch. This OpenSpec change records the adoption, the review amendments that are binding for implementation, working defaults for the plan's open decisions, and the execution model. It intentionally does not duplicate the plan; on any conflict the Confluence page wins unless an amendment below says otherwise. ## What Changes (per the adopted plan) - Three sidecars per compressed main fork: append-only `.cfp` payload, `.cfsmap` snapshot, `.cfsjrn` journal with idempotent `(blocknum, generation)` records; checkpoint order `.cfp -> .cfsjrn -> .cfsmap`, snapshot via `.tmp + fsync + rename`. - No new smgr kind: `smgriscfsenabled()` branch inside `smgrreadv/writev` before `md*`, delegating through `CfsStorageOps` (`read_page/write_page/truncate/zeroextend/replay_journal/close`) in a new `src/backend/storage/cfs/`. - Opt-in per relation via reloptions `algorithm='lz4'|'zstd'` (+ `level`, `min_gain_bytes`); GUCs are defaults only. MAIN_FORKNUM only; FSM/VM/init uncompressed; TEMP/UNLOGGED rejected; indexes and TOAST included. - Shared buffers always hold uncompressed pages; WAL never carries compressed bytes; FPI stays uncompressed. - Compaction: `CfsBgWorker` with relation-level `AccessExclusiveLock` + timeout, copy live records, atomic durable whole-set swap, no WAL. - MVP read path synchronous: `smgrmaxcombine()=1`, sync fallback in `smgrstartreadv()`; per-record async AIO with a decompression callback is a post-MVP phase. ## Binding amendments (from review 2026-11-02..06) These modify the Confluence plan; the plan owner should fold them in, until then this list is authoritative: 1. **Recovery-time CFS selector ADR, blocking Phase 6.** The reloption-based locator cache cannot be populated during physical REDO (smgr sees only `RelFileLocator`; `XLOG_SMGR_CREATE` replay carries no reloptions). Decide and implement: sidecar presence (`.cfsmap` exists) as the smgr-layer authority with reloptions as the create-time control plane, or carry the CFS flag in create/rewrite WAL records. Unresolved, this silently corrupts redo by writing pages into the zero-length base file. 2. **Compaction vs base backup.** `CfsBgWorker` must skip relations while a base backup is in progress (`BackupInProgress()` / online-backup state), otherwise `pg_basebackup` can capture `.cfp` old + `.cfsmap` new. 3. **pg_rewind task added to Phase 12** (A12.7): either verified support for sidecar sets or an explicit, documented "not supported" with detection. 4. **MVP set fix:** include A3.8 (`CfsStorageOps`) in the MVP; A6.5 already depends on it. 5. **AIO specifics written down:** MVP sync path uses `pgaio_io_complete_readv_immediate()` (the `rd.c:411` pattern). The deferred async phase must: register the decompression callback after the buffer-manager callback (chain is innermost-first, `aio_callback.c:240`) so decompression precedes checksum validation; never touch the map from the completer (completions run in other backends / I/O workers); use preallocated per-process scratch (no `palloc` in the critical section); set `PGAIO_HF_BUFFERED` unconditionally; force sync under `io_method=worker` unless the `smgrfd()` reopen assertion (`smgr.c:1154`) is relaxed for moved records. 6. **I/O flags and memory:** `.cfp` is never opened `PG_O_DIRECT` (unaligned record offsets; `debug_io_direct=data` must not apply to sidecars). If the map cache lives in shared memory it is hugepage-backed and must be budgeted in `shared_memory_size_in_huge_pages`. ## Working defaults for the plan's open decisions | Plan item | Default | |---|---| | A2.1 `sizeof(CfsMapEntry)` | target <= 24 bytes (plan's ceiling of 64 is too fat: 8 GB shmem per 1 TB logical); drop `original_size` (always BLCKSZ), 16-bit generation, 16-24 bit entry CRC | | A4.5 map cache | hot-subset LRU bounded by a GUC; full map only for pinned relations | | A6.2 base main fork | keep zero-length; documented as the "not CFS" marker for tools | | A6.4/A7.3 zero pages | invalid-zero sentinel, no record, no journal entry | | A10.4a `ALTER INDEX SET (algorithm)` | no implicit rebuild; require explicit `REINDEX` | | A12.1 `pg_relation_size` | physical sidecar size for CFS main fork; logical via `pg_cfs_relation_size()` | ## Execution model Heavy AI involvement (the plan was authored for it): phases 0-2 collapse into ~1 sprint; two streams (core 3->6->7->8->9, tooling 11/12/14 lagging by ~2 sprints); kill -9 TAP harness built by sprint 2-3 instead of Phase 9; human review gates (line review) on journal/replay, lock ordering, and WAL-write ordering; differential md-vs-CFS and mutation-fuzz tests as AI-cheap verification. Planning envelope: ~8 two-week sprints to MVP, ~12 to feature-complete (1 senior dev + AI + second reviewer), plus +2-3 sprints contingency held for Phases 7/9-style rare-race debugging. ## Capabilities - `cfs`: page-level compressed storage sidecars for permanent heap/index/TOAST main forks. ## Impact New subsystem under `src/backend/storage/cfs/` (codec, on-disk format, storage ops, shmem map cache); smgr branch in `src/backend/storage/smgr/`; reloptions/GUCs; checkpoint and pending-sync (FileTag) integration; utilities (`pg_basebackup`, `pg_verifybackup`, `pg_checksums`, `pg_upgrade`, `pg_rewind`); SQL monitoring functions/views/wait events. Build prerequisites: `--with-lz4 --with-zstd` (both currently off in the validated configure profile) and packaging deps. No data migration; opt-in per relation; zero-overhead requirement for non-CFS relations.