Re: FK violation in partitioned table after truncating a referenced partition

From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Jehan-Guillaume de Rorthais <jgdr(at)dalibo(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: FK violation in partitioned table after truncating a referenced partition
Date: 2020-02-06 22:30:15
Message-ID: 20200206223015.GA32754@alvherre.pgsql
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On 2020-Feb-06, Alvaro Herrera wrote:

> I agree that patching heap_truncate_find_FKs is a reasonable way to fix.
> I propose a slightly different formulation: instead of the loop that you
> have, we can just use the second loop, and add more parent constraints
> to the list if any constraint we scan in turn has a parent constraint.
> So we don't repeat the whole thing, but only that second loop.

Hmm, this doesn't actually work; I modified your test case and I see
that my code fails to do the right thing.

-- Truncate a single partition cascading to another partitioned table.
CREATE TABLE trunc_a (a INT PRIMARY KEY) PARTITION BY RANGE (a);
CREATE TABLE trunc_a1 PARTITION OF trunc_a FOR VALUES FROM (0) TO (10);
CREATE TABLE trunc_a2 PARTITION OF trunc_a FOR VALUES FROM (10) TO (20)
PARTITION BY RANGE (a);
CREATE TABLE trunc_a21 PARTITION OF trunc_a2 FOR VALUES FROM (10) TO (12);
CREATE TABLE trunc_a22 PARTITION OF trunc_a2 FOR VALUES FROM (12) TO (16);
CREATE TABLE trunc_a2d PARTITION OF trunc_a2 DEFAULT;
CREATE TABLE trunc_a3 PARTITION OF trunc_a FOR VALUES FROM (20) TO (30);
INSERT INTO trunc_a VALUES (10), (15), (20), (25);

CREATE TABLE ref_c (
c INT PRIMARY KEY,
a INT REFERENCES trunc_a(a) ON DELETE CASCADE
) PARTITION BY RANGE (c);
CREATE TABLE ref_c1 PARTITION OF ref_c FOR VALUES FROM (100) TO (200);
CREATE TABLE ref_c2 PARTITION OF ref_c FOR VALUES FROM (200) TO (300);
INSERT INTO ref_c VALUES (100, 10), (150, 15), (200, 20), (250, 25);

TRUNCATE TABLE trunc_a21 CASCADE;
SELECT a as "from table ref_c" FROM ref_c;
SELECT a as "from table trunc_a" FROM trunc_a ORDER BY a;

DROP TABLE trunc_a, ref_c;

--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Alvaro Herrera 2020-02-06 23:01:29 Re: FK violation in partitioned table after truncating a referenced partition
Previous Message Andres Freund 2020-02-06 22:12:48 Re: BUG #16247: Cast error on integer