| 1 |
# This test exercises behavior of foreign keys in the face of concurrent |
| 2 |
# detach of partitions in the referenced table. |
| 3 |
# (The cases where the detaching transaction is canceled is interesting |
| 4 |
# because the locking situation is completely different. I didn't verify |
| 5 |
# that keeping both variants adds any extra coverage.) |
| 6 |
# |
| 7 |
# Note: When using "s1cancel", mark the target step (the one to be canceled) |
| 8 |
# as blocking "s1cancel". This ensures consistent reporting regardless of |
| 9 |
# whether "s1cancel" finishes before or after the other step reports failure. |
| 10 |
# Also, ensure the step after "s1cancel" is also an s1 step (use "s1noop" if |
| 11 |
# necessary). This ensures we won't move on to the next step until the cancel |
| 12 |
# is complete. |
| 13 |
|
| 14 |
setup { |
| 15 |
drop table if exists d4_primary, d4_primary1, d4_fk, d4_pid; |
| 16 |
create table d4_primary (a int primary key) partition by list (a); |
| 17 |
create table d4_primary1 partition of d4_primary for values in (1); |
| 18 |
create table d4_primary2 partition of d4_primary for values in (2); |
| 19 |
insert into d4_primary values (1); |
| 20 |
insert into d4_primary values (2); |
| 21 |
create table d4_fk (a int references d4_primary); |
| 22 |
insert into d4_fk values (2); |
| 23 |
create table d4_pid (pid int); |
| 24 |
} |
| 25 |
|
| 26 |
session s1 |
| 27 |
step s1b { begin; } |
| 28 |
step s1brr { begin isolation level repeatable read; } |
| 29 |
step s1s { select * from d4_primary; } |
| 30 |
step s1cancel { select pg_cancel_backend(pid) from d4_pid; } |
| 31 |
step s1noop { } |
| 32 |
step s1insert { insert into d4_fk values (1); } |
| 33 |
step s1c { commit; } |
| 34 |
step s1declare { declare f cursor for select * from d4_primary; } |
| 35 |
step s1declare2 { declare f cursor for select * from d4_fk where a = 2; } |
| 36 |
step s1fetchall { fetch all from f; } |
| 37 |
step s1fetchone { fetch 1 from f; } |
| 38 |
step s1updcur { update d4_fk set a = 1 where current of f; } |
| 39 |
step s1svpt { savepoint f; } |
| 40 |
step s1rollback { rollback to f; } |
| 41 |
|
| 42 |
session s2 |
| 43 |
step s2snitch { insert into d4_pid select pg_backend_pid(); } |
| 44 |
step s2detach { alter table d4_primary detach partition d4_primary1 concurrently; } |
| 45 |
|
| 46 |
session s3 |
| 47 |
step s3brr { begin isolation level repeatable read; } |
| 48 |
step s3insert { insert into d4_fk values (1); } |
| 49 |
step s3commit { commit; } |
| 50 |
step s3vacfreeze { vacuum freeze pg_catalog.pg_inherits; } |
| 51 |
|
| 52 |
# Trying to insert into a partially detached partition is rejected |
| 53 |
permutation s2snitch s1b s1s s2detach s1cancel(s2detach) s1insert s1c |
| 54 |
permutation s2snitch s1b s1s s2detach s1insert s1c |
| 55 |
# ... even under REPEATABLE READ mode. |
| 56 |
permutation s2snitch s1brr s1s s2detach s1cancel(s2detach) s1insert s1c |
| 57 |
permutation s2snitch s1brr s1s s2detach s1insert s1c |
| 58 |
|
| 59 |
# If you read the referenced table using a cursor, you can see a row that the |
| 60 |
# RI query does not see. |
| 61 |
permutation s2snitch s1b s1declare s2detach s1cancel(s2detach) s1fetchall s1insert s1c |
| 62 |
permutation s2snitch s1b s1declare s2detach s1fetchall s1insert s1c |
| 63 |
permutation s2snitch s1b s1declare s2detach s1cancel(s2detach) s1svpt s1insert s1rollback s1fetchall s1c |
| 64 |
permutation s2snitch s1b s1declare s2detach s1svpt s1insert s1rollback s1fetchall s1c |
| 65 |
permutation s2snitch s1b s2detach s1declare s1cancel(s2detach) s1fetchall s1insert s1c |
| 66 |
permutation s2snitch s1b s2detach s1declare s1fetchall s1insert s1c |
| 67 |
permutation s2snitch s1b s2detach s1declare s1cancel(s2detach) s1svpt s1insert s1rollback s1fetchall s1c |
| 68 |
permutation s2snitch s1b s2detach s1declare s1svpt s1insert s1rollback s1fetchall s1c |
| 69 |
|
| 70 |
# Creating the referencing row using a cursor |
| 71 |
permutation s2snitch s1brr s1declare2 s1fetchone s2detach s1cancel(s2detach) s1updcur s1c |
| 72 |
permutation s2snitch s1brr s1declare2 s1fetchone s2detach s1updcur s1c |
| 73 |
permutation s2snitch s1brr s1declare2 s1fetchone s1updcur s2detach s1c |
| 74 |
|
| 75 |
# Try reading the table from an independent session. |
| 76 |
permutation s2snitch s1b s1s s2detach s3insert s1c |
| 77 |
permutation s2snitch s1b s1s s2detach s3brr s3insert s3commit s1cancel(s2detach) s1c |
| 78 |
permutation s2snitch s1b s1s s2detach s3brr s3insert s3commit s1c |
| 79 |
|
| 80 |
# Try one where we VACUUM FREEZE pg_inherits (to verify that xmin change is |
| 81 |
# handled correctly). |
| 82 |
permutation s2snitch s1brr s1s s2detach s1cancel(s2detach) s1noop s3vacfreeze s1s s1insert s1c |
| 83 |
permutation s2snitch s1b s1s s2detach s1cancel(s2detach) s1noop s3vacfreeze s1s s1insert s1c |