Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 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
# ALTER TABLE ADD+VALIDATE CONSTRAINT with concurrent writes on a CFS table.
#
# Mirrors alter-table-1.spec: VALIDATE CONSTRAINT acquires
# ShareUpdateExclusiveLock and runs a full table scan to verify all rows
# satisfy the constraint.  On a CFS table that scan reads decompressed
# pages through smgr, so it must see the same set of rows a plain-table
# validation would, regardless of concurrent INSERTs.
#
# The permutations are reduced from the original 137 to the three that
# exercise the locking interaction CFS cares about: validation completing
# before the concurrent write commits, the write blocking validation, and
# both interleaving.
 
setup
{
    CREATE TABLE a (i int PRIMARY KEY);
    CREATE TABLE b (a_id int) WITH (algorithm = 'lz4');
    INSERT INTO a VALUES (0), (1), (2), (3);
    INSERT INTO b SELECT generate_series(1, 1000) % 4;
}
 
teardown
{
    DROP TABLE a, b;
}
 
session s1
step s1     { BEGIN; }
step at1    { ALTER TABLE b ADD CONSTRAINT bfk FOREIGN KEY (a_id) REFERENCES a (i) NOT VALID; }
step sc1    { COMMIT; }
step s2     { BEGIN; }
step at2    { ALTER TABLE b VALIDATE CONSTRAINT bfk; }
step sc2    { COMMIT; }
step s1_check { SELECT ok FROM pg_cfs_check('b'::regclass, true); }
 
session s2
setup       { BEGIN; }
step rx1    { SELECT * FROM b WHERE a_id = 1 LIMIT 1; }
step wx     { INSERT INTO b VALUES (0); }
step rx3    { SELECT * FROM b WHERE a_id = 3 LIMIT 3; }
step c2     { COMMIT; }
 
# Validation completes after the concurrent write commits — both rows
# (original 1000 + the new one) must be visible to the validation scan.
permutation s1 at1 sc1 s2 at2 sc2 rx1 wx rx3 c2 s1_check
# The write blocks validation until it commits.
permutation s1 at1 sc1 s2 rx1 wx rx3 c2 at2 sc2 s1_check
# Write and validation interleave with s2 committing mid-way.
permutation s1 at1 sc1 rx1 s2 at2 wx sc2 rx3 c2 s1_check