Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
--
-- src/test/performance/cfs/datashape_churn.sql
--
-- Phase 13 / A13.2 -- data-shape benchmark: update-heavy single-page churn.
--
-- Repeatedly rewrites the same small set of rows so a handful of heap pages
-- accumulate many dead records in the .cfp.  Demonstrates dead_bytes/dead_pages
-- growth (the signal the background compactor consumes) and the effect of a
-- manual pg_cfs_compact().
--
--   psql -v rows=200 -v iterations=200 -f datashape_churn.sql
 
DROP TABLE IF EXISTS ds_churn_cfs;
CREATE TABLE ds_churn_cfs (id int primary key, payload text) WITH (algorithm = :'algo');
 
-- A small table that fits in very few pages.
INSERT INTO ds_churn_cfs SELECT g, repeat('v', 100) FROM generate_series(1, :rows) g;
CHECKPOINT;
 
-- Hammer the same rows.  Each generated UPDATE runs as its own statement
-- (autocommit), so every iteration leaves a separate dead CFS record in the
-- .cfp -- exactly the churn the background compactor reclaims.  \gexec is used
-- because psql does not substitute :variables inside a dollar-quoted body.
SELECT format(
        'UPDATE ds_churn_cfs SET payload = repeat(''u'', 100) || %s WHERE id = (%s %% %s) + 1',
        g, g, :rows)
FROM generate_series(1, :iterations) AS g
\gexec
CHECKPOINT;
 
-- Snapshot before compaction.
SELECT 'before' AS phase,
       dead_pages,
       pg_size_pretty(dead_bytes) AS dead_bytes,
       pg_size_pretty(physical_size) AS physical_size
FROM pg_stat_cfs WHERE relname = 'ds_churn_cfs';
 
SELECT pg_cfs_compact('ds_churn_cfs');
 
SELECT 'after' AS phase,
       dead_pages,
       pg_size_pretty(dead_bytes) AS dead_bytes,
       pg_size_pretty(physical_size) AS physical_size
FROM pg_stat_cfs WHERE relname = 'ds_churn_cfs';