Исходный код СУБД 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
-- ----------------------------------------------------------------------
-- Parallel VACUUM through the CFS storage manager.
--
-- Parallel VACUUM launches workers that concurrently run ambulkdelete on
-- individual indexes.  On a CFS table each index is itself a CFS relation
-- with sidecar-compressed pages, so the parallel workers' concurrent access
-- to the CFS map cache and sidecar files is the stress point.
--
-- Bug #17245 regression (regress/sql/vacuum_parallel.sql): parallel VACUUM
-- must not skip indexes below min_parallel_index_scan_size.  This adapts
-- that scenario to a CFS table with a plain mirror as the oracle.
-- ----------------------------------------------------------------------
CREATE EXTENSION pg_buffercache;
 
SET max_parallel_maintenance_workers TO 4;
SET min_parallel_index_scan_size TO '128kB';
 
-- CFS heap + plain mirror, three indexes each (two large, one small).
CREATE TABLE cfsvp_cfs (a int) WITH (algorithm = 'lz4', autovacuum_enabled = off);
CREATE TABLE cfsvp_plain (a int) WITH (autovacuum_enabled = off);
INSERT INTO cfsvp_cfs SELECT i FROM generate_series(1, 10000) i;
INSERT INTO cfsvp_plain SELECT i FROM generate_series(1, 10000) i;
 
CREATE INDEX cfsvp_cfs_idx1 ON cfsvp_cfs (a);
CREATE INDEX cfsvp_cfs_idx2 ON cfsvp_cfs (a);
CREATE INDEX cfsvp_cfs_small ON cfsvp_cfs ((1));
CREATE INDEX cfsvp_plain_idx1 ON cfsvp_plain (a);
CREATE INDEX cfsvp_plain_idx2 ON cfsvp_plain (a);
CREATE INDEX cfsvp_plain_small ON cfsvp_plain ((1));
 
-- All three CFS indexes carry sidecars (they inherited CFS from the heap).
SELECT count(*) AS cfs_index_sidecars
  FROM pg_class c
  WHERE c.relname LIKE 'cfsvp_cfs%' AND c.relkind = 'i'
    AND cfs_relation_has_sidecars(c.oid);
 
-- The small index is below the parallel threshold (deduplication makes it
-- tiny), so the leader handles it while workers handle the two large ones.
SELECT EXISTS (
  SELECT 1 FROM pg_class
  WHERE oid = 'cfsvp_cfs_small'::regclass AND
    pg_relation_size(oid) <
    pg_size_bytes(current_setting('min_parallel_index_scan_size'))
) AS leader_will_handle_small_index;
SELECT count(*) AS trigger_parallel_vacuum_nindexes
  FROM pg_class
  WHERE oid IN ('cfsvp_cfs_idx1'::regclass, 'cfsvp_cfs_idx2'::regclass) AND
    pg_relation_size(oid) >=
    pg_size_bytes(current_setting('min_parallel_index_scan_size'));
 
-- DELETE all rows, then parallel VACUUM with B-Tree page deletions
-- (ambulkdelete) on the CFS indexes -- concurrent worker access to CFS.
DELETE FROM cfsvp_cfs;
DELETE FROM cfsvp_plain;
VACUUM (PARALLEL 4, INDEX_CLEANUP ON) cfsvp_cfs;
VACUUM (PARALLEL 4, INDEX_CLEANUP ON) cfsvp_plain;
 
-- Re-insert the same data.
INSERT INTO cfsvp_cfs SELECT i FROM generate_series(1, 10000) i;
INSERT INTO cfsvp_plain SELECT i FROM generate_series(1, 10000) i;
 
RESET max_parallel_maintenance_workers;
RESET min_parallel_index_scan_size;
 
-- Evict the CFS heap so the count reads back through decompression.
CHECKPOINT;
SELECT buffers_evicted > 0 AS evicted
  FROM pg_buffercache_evict_relation('cfsvp_cfs');
 
SELECT count(*) AS cfs_rows FROM cfsvp_cfs;
SELECT count(*) AS plain_rows FROM cfsvp_plain;
 
-- Index scan through the vacuumed-and-rebuilt CFS indexes after eviction.
SET enable_seqscan TO off;
SELECT count(*) AS cfs_idx_scan FROM cfsvp_cfs WHERE a = 5000;
RESET enable_seqscan;
SELECT count(*) AS plain_idx_scan FROM cfsvp_plain WHERE a = 5000;
 
DROP TABLE cfsvp_cfs, cfsvp_plain;
DROP EXTENSION pg_buffercache;