-- ---------------------------------------------------------------------- -- QDB-1513: batched read path (advisory maxcombine + span pread). -- -- The physical layout of .cfp records follows the order pages are flushed -- to the sidecars, so the parts below pick scenarios whose order is -- deterministic: -- -- Part 1: right after a bulk INSERT the heap pages mostly still live only -- in shared_buffers (zero-extended, no records yet). Whatever mix of -- holes and already-flushed records the timing of the bgwriter leaves, -- the batched reader must agree with the per-block path byte-for-byte on -- every page of the relation. -- -- Part 2: a btree build writes its pages synchronously through -- bulk_write in block order, so the freshly built CFS index (composite -- key, no deduplication) stores logical neighbours back-to-back: -- cfs_map_contiguous_run() must see full runs (capped by the probe's -- 16-block window) and cfs_read_pages_batch() must reproduce every page -- byte-for-byte (poison-filled batch buffers are compared against the -- per-block cfs_read_page_cached() reads through a full private-map -- handle). -- -- Part 3: a corrupted payload byte of a record inside the contiguous run -- must fail the batched range read with CFS_STORAGE_CORRUPT naming -- exactly that block, and a restored byte must read cleanly again. -- -- Part 4: a non-HOT update dirties a couple of index leaf pages, which -- the checkpoint re-appends at the tail of .cfp; the requested ranges now -- contain offset gaps, and the batched reader must fall back per-block -- around them and still reproduce every page exactly. -- -- Part 5: compaction of the heap plus a real seqscan of the evicted -- relation (lazy pool-backed handle, the smgr readv path bufmgr drives -- with io_combine_limit) must return the same data as a plain -- uncompressed table loaded with the same rows. -- ---------------------------------------------------------------------- -- The corrupt test case intentionally triggers WARNINGs from -- CfsDecompressPage(); suppress client warnings so the regression output -- stays stable (same approach as cfs_codec.sql). The CREATE EXTENSION -- notice is hidden the same way: in the full schedule cfs_codec.sql has -- already installed the extension. SET client_min_messages = warning; CREATE EXTENSION IF NOT EXISTS test_cfs; RESET client_min_messages; CREATE EXTENSION pg_buffercache; CREATE TABLE rb_lz4 (id int, payload text) WITH (algorithm = 'lz4'); INSERT INTO rb_lz4 SELECT g, repeat('rb ', 60) FROM generate_series(1, 5000) g; -- Part 1: holes (and whatever the bgwriter already flushed) must degrade -- per-block and still compare equal. SELECT batch_ok AND pages_checked > 20 AS heap_holes_degrade_correctly FROM cfs_test_read_batch('rb_lz4'); -- Part 2: the index build flushes pages synchronously in block order. CREATE INDEX rb_lz4_payload_idx ON rb_lz4 (payload, id); -- Block 0 is the btree metapage, which the sort-build writes last: its -- record lands at the tail, so the run starting at block 0 is short by -- construction while the leaf/internal span is fully contiguous. SELECT run0 = 1 AND run_max = 16 AS leaves_contiguous, pages_checked > 100 AS whole_index_rechecked, batch_ok AS batch_matches_perblock FROM cfs_test_read_batch('rb_lz4_payload_idx'); -- Part 3: corrupt one record of the contiguous run, then restore it. -- (Warnings from CfsDecompressPage() are expected and hidden: the CRC -- values printed depend on the compressed bytes of the record.) SET client_min_messages = error; SELECT cfs_test_read_batch_corrupt('rb_lz4_payload_idx', 3) AS corrupt_mid_run; RESET client_min_messages; -- The failed span read already served (and per-page counted) the records -- before the corrupt one; the partial batch must keep the counter invariant -- intact even on this error path. SELECT read_count = read_batched_pages + read_fallback_pages AS invariant_after_corrupt FROM pg_stat_cfs WHERE relid = 'rb_lz4_payload_idx'::regclass; -- Part 4: move a couple of index leaf pages to the tail of .cfp. UPDATE rb_lz4 SET id = 5000 + id WHERE id = 2500; CHECKPOINT; -- Two index leaf pages (the old and the new position of the updated -- key) are re-appended at the tail, splitting the span; the 16-block -- probe window still finds full contiguous runs between the gaps, and -- the batched reader must degrade per-block exactly around them. SELECT run_max = 16 AS long_runs_survive, pages_checked > 100 AS whole_index_rechecked, batch_ok AS batch_matches_perblock FROM cfs_test_read_batch('rb_lz4_payload_idx'); -- Part 5: compaction plus a real seqscan through the smgr readv path. CREATE TABLE rb_plain (id int, payload text); INSERT INTO rb_plain SELECT id, payload FROM rb_lz4; SELECT pg_cfs_compact('rb_lz4') ~ 'reclaimed \d+' AS compacted; SELECT batch_ok AND pages_checked > 100 AS heap_batched_after_compact FROM cfs_test_read_batch('rb_lz4'); CHECKPOINT; SELECT buffers_evicted > 0 AS evicted FROM pg_buffercache_evict_relation('rb_lz4'); SELECT (SELECT md5(string_agg(payload, '' ORDER BY id)) FROM rb_lz4) = (SELECT md5(string_agg(payload, '' ORDER BY id)) FROM rb_plain) AS seqscan_matches_plain; -- Part 6: batch-read observability (QDB-1513 evidence base). The wait -- event split must expose decompression separately from the raw read, and -- the pg_stat_cfs counters must attribute every served page either to a -- batched span or to the per-block fallback (default shared-map mode). SELECT count(*) = 1 AS decompress_wait_event FROM pg_wait_events WHERE name = 'CfsDecompress'; SET enable_indexscan = off; SET enable_indexonlyscan = off; SET enable_bitmapscan = off; -- The compacted heap is physically contiguous: a cold seqscan is served by -- span reads. DO $$ BEGIN PERFORM pg_cfs_reset_stats('rb_lz4'); END $$; SELECT buffers_evicted > 0 AS evicted FROM pg_buffercache_evict_relation('rb_lz4'); SELECT count(*) > 0 AS seqscan_ran FROM rb_lz4; SELECT read_batch_count > 0 AS spans_served, read_batched_pages > 0 AS pages_batched, read_count = read_batched_pages + read_fallback_pages AS invariant_holds FROM pg_stat_cfs WHERE relid = 'rb_lz4'::regclass; -- Updated pages are re-appended at the tail of .cfp, so their old offsets -- become gaps inside runs: those blocks must degrade per-block (fallback) -- and the invariant must survive. UPDATE rb_lz4 SET payload = payload || 'x' WHERE id % 500 = 0; CHECKPOINT; DO $$ BEGIN PERFORM pg_cfs_reset_stats('rb_lz4'); END $$; SELECT buffers_evicted > 0 AS evicted FROM pg_buffercache_evict_relation('rb_lz4'); SELECT count(*) > 0 AS seqscan_ran FROM rb_lz4; SELECT read_fallback_pages > 0 AS fallback_used, read_count = read_batched_pages + read_fallback_pages AS invariant_holds FROM pg_stat_cfs WHERE relid = 'rb_lz4'::regclass; RESET enable_indexscan; RESET enable_indexonlyscan; RESET enable_bitmapscan; DROP TABLE rb_lz4, rb_plain;