Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d8e5628

Browse files
pjungwirCommitfest Bot
authored and
Commitfest Bot
committed
Add test for temporal referential integrity
This commit adds an isolation test showing that temporal foreign keys do not permit referential integrity violations under concurrency, like fk-snapshot-2. You can show that the test fails by passing false for detectNewRows in ri_restrict.
1 parent 1e65c1b commit d8e5628

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Parsed test spec with 2 sessions
2+
3+
starting permutation: s2ins s1del s2c s1c
4+
step s2ins:
5+
INSERT INTO child VALUES ('[1,2)', '[2020-01-01,2030-01-01)', '[1,2)');
6+
7+
step s1del: DELETE FROM parent WHERE id = '[1,2)'; <waiting ...>
8+
step s2c: COMMIT;
9+
step s1del: <... completed>
10+
ERROR: update or delete on table "parent" violates foreign key constraint "child_parent_id_valid_at_fkey" on table "child"
11+
step s1c: COMMIT;
12+
13+
starting permutation: s1del s2ins s1c s2c
14+
step s1del: DELETE FROM parent WHERE id = '[1,2)';
15+
step s2ins:
16+
INSERT INTO child VALUES ('[1,2)', '[2020-01-01,2030-01-01)', '[1,2)');
17+
<waiting ...>
18+
step s1c: COMMIT;
19+
step s2ins: <... completed>
20+
ERROR: could not serialize access due to concurrent update
21+
step s2c: COMMIT;

src/test/isolation/isolation_schedule

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ test: fk-partitioned-1
3636
test: fk-partitioned-2
3737
test: fk-snapshot
3838
test: fk-snapshot-2
39+
test: fk-snapshot-3
3940
test: subxid-overflow
4041
test: eval-plan-qual
4142
test: eval-plan-qual-trigger
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# RI Trigger test
2+
#
3+
# Test C-based temporal referential integrity enforcement.
4+
# Under REPEATABLE READ we need some snapshot trickery in C,
5+
# or we would permit things that violate referential integrity.
6+
7+
setup
8+
{
9+
CREATE TABLE parent (
10+
id int4range NOT NULL,
11+
valid_at daterange NOT NULL,
12+
PRIMARY KEY (id, valid_at WITHOUT OVERLAPS));
13+
CREATE TABLE child (
14+
id int4range NOT NULL,
15+
valid_at daterange NOT NULL,
16+
parent_id int4range,
17+
FOREIGN KEY (parent_id, PERIOD valid_at) REFERENCES parent);
18+
INSERT INTO parent VALUES ('[1,2)', '[2020-01-01,2030-01-01)');
19+
}
20+
21+
teardown { DROP TABLE parent, child; }
22+
23+
session s1
24+
setup { BEGIN ISOLATION LEVEL REPEATABLE READ; }
25+
step s1del { DELETE FROM parent WHERE id = '[1,2)'; }
26+
step s1c { COMMIT; }
27+
28+
session s2
29+
setup { BEGIN ISOLATION LEVEL REPEATABLE READ; }
30+
step s2ins {
31+
INSERT INTO child VALUES ('[1,2)', '[2020-01-01,2030-01-01)', '[1,2)');
32+
}
33+
step s2c { COMMIT; }
34+
35+
# Violates referential integrity unless we use an up-to-date crosscheck snapshot:
36+
permutation s2ins s1del s2c s1c
37+
38+
# Raises a can't-serialize exception
39+
# when the INSERT trigger does SELECT FOR KEY SHARE:
40+
permutation s1del s2ins s1c s2c

0 commit comments

Comments
 (0)