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-07 15:04:32
Message-ID: 20200207150432.GA21902@alvherre.pgsql
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On 2020-Feb-07, Jehan-Guillaume de Rorthais wrote:

> Well, when reading myself, I found a bug in my algorithm. When looking for
> parent constraints harvested during the first loop, I wasn't looking on
> pg_contraint.oid, but on conparentid again. So instead of gathering parent
> constraints to add the parent relation to the list of oids, I was only adding
> siblings constraints. Here the fix:
>
> ScanKeyInit(&key,
> - Anum_pg_constraint_conparentid,
> + Anum_pg_constraint_oid
> BTEqualStrategyNumber, F_OIDEQ,
> ObjectIdGetDatum(parent));
>
> - fkeyScan = systable_beginscan(fkeyRel, ConstraintParentIndexId,
> + fkeyScan = systable_beginscan(fkeyRel, ConstraintOidIndexId,
> true, NULL, 1,
> &key);

Doh, of course. I should have seen that.

Here's another take at the formulation; IMO the loop is more obvious
this way, with a flag to restart from the top rather than keeping track
of the list length. But essentially this is your algorithm.

I couldn't find any fault in this. It would be nice if the cascaded
truncation was more precise, ie. only truncate the referencing
partitions that overlap the ranges covered by the referenced partition
being truncated. But that seems more difficult to achieve, as well as
less clearly defined; if you really want something like that, I think
you can detach the referenced partition.

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

Attachment Content-Type Size
v3-0001-Fix-TRUNCATE-on-a-partition-to-apply-CASCADE-to-p.patch text/x-diff 8.2 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Jehan-Guillaume de Rorthais 2020-02-07 16:19:33 Re: FK violation in partitioned table after truncating a referenced partition
Previous Message Amit Kapila 2020-02-07 10:59:25 Re: ERROR: subtransaction logged without previous top-level txn record