BUG #18628: Race condition during attach/detach partition breaks constraints of partition having foreign key - Mailing list pgsql-bugs

From PG Bug reporting form
Subject BUG #18628: Race condition during attach/detach partition breaks constraints of partition having foreign key
Date
Msg-id 18628-4f11be4e0b1c9002@postgresql.org
Whole thread Raw
Responses Re: BUG #18628: Race condition during attach/detach partition breaks constraints of partition having foreign key
List pgsql-bugs
The following bug has been logged on the website:

Bug reference:      18628
Logged by:          Alexander Lakhin
Email address:      exclusion@gmail.com
PostgreSQL version: 17rc1
Operating system:   Ubuntu 22.04
Description:

The following script:
for ((i=1;i<=100;i++)); do
psql  -q -c "DROP TABLE IF EXISTS pt, p1, p2 CASCADE;"

echo "
CREATE TABLE pt (id int PRIMARY KEY, rid int,
  FOREIGN KEY (rid) REFERENCES pt(id))
PARTITION BY LIST (id);

CREATE TABLE p1 PARTITION OF pt FOR VALUES IN (1);
CREATE TABLE p2 (id int PRIMARY KEY, rid int);
" | psql -q

psql -c "ALTER TABLE pt DETACH PARTITION p1;" &
psql -c "ALTER TABLE pt ATTACH PARTITION p1 FOR VALUES IN (1);" &
psql -c "ALTER TABLE pt ATTACH PARTITION p2 FOR VALUES IN (2);"
wait

echo  "
ALTER TABLE pt DETACH PARTITION p1;
" | psql 2>&1 | grep 'could not find' && break;
done

psql -c "
SELECT c.conname, c.oid, t.tgname, tgtype
FROM pg_trigger t, pg_constraint c, pg_class cl
WHERE tgconstraint = c.oid AND conrelid = cl.oid AND
  cl.relname = 'p1';"

ends up with:
ERROR:  could not find ON INSERT check triggers of foreign key constraint
16409

and the p1 constraints at the end are:
   conname   |  oid  |            tgname            | tgtype 
-------------+-------+------------------------------+--------
 p1_rid_fkey | 16409 | RI_ConstraintTrigger_a_16410 |      9
 p1_rid_fkey | 16409 | RI_ConstraintTrigger_a_16411 |     17
 pt_rid_fkey | 16399 | RI_ConstraintTrigger_c_16400 |      5
 pt_rid_fkey | 16399 | RI_ConstraintTrigger_c_16401 |     17
(4 rows)

Whilst when the partition is in normal conditions, it's constraints are:
   conname   |  oid  |            tgname            | tgtype 
-------------+-------+------------------------------+--------
 pt_rid_fkey | 19171 | RI_ConstraintTrigger_a_19182 |      9
 pt_rid_fkey | 19171 | RI_ConstraintTrigger_a_19183 |     17
 pt_rid_fkey | 19171 | RI_ConstraintTrigger_c_19172 |      5
 pt_rid_fkey | 19171 | RI_ConstraintTrigger_c_19173 |     17

Reproduced on REL_15_STABLE (starting from 4566345cf) .. master.


pgsql-bugs by date:

Previous
From: David Rowley
Date:
Subject: Re: BUG #18627: Regression (15 -> 16) - Join removal not performed when join condition spans multiple tables
Next
From: Tom Lane
Date:
Subject: Re: Reference to - BUG #18349: ERROR: invalid DSA memory alloc request size 1811939328, CONTEXT: parallel worker