Re: [BUG] Fix DETACH with FK pointing to a partitioned table fails
Alvaro Herrera <alvherre@alvh.no-ip.org>
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
To: Junwang Zhao <zhjwpku@gmail.com>
Cc: Tender Wang <tndrwang@gmail.com>, Jehan-Guillaume de Rorthais <jgdr@dalibo.com>,
"pgsql-hackers@postgresql.org" <pgsql-hackers@postgresql.org>, Alexander Lakhin <exclusion@gmail.com>
Date: 2024-07-26T08:36:08Z
Lists: pgsql-hackers
Attachments
- patch.1 (text/plain)
On 2024-Jul-26, Junwang Zhao wrote: > There is a bug report[0] Tender comments might be the same > issue as this one, but I tried Alvaro's and mine patch, neither > could solve that problem, I did not tried Tender's earlier patch > thought. I post the test script below in case you are interested. Yeah, I've been looking at this whole debacle this week and after looking at it more closely, I realized that the overall problem requires a much more invasive solution -- namely, that on DETACH, if the referenced table is partitioned, we need to create additional pg_constraint entries from the now-standalone table (was partition) to each of the partitions of the referenced table; and also add action triggers to each of those. Without that, the constraint is incomplete and doesn't work (as reported multiple times already). One thing I have not yet tried is what if the partition being detach is also partitioned. I mean, do we need to handle each sub-partition explicitly in some way? I think the answer is no, but it needs tests. I have written the patch to do this on detach, and AFAICS it works well, though it changes the behavior of some existing tests (IIRC related to self-referencing FKs). Also, the next problem is making sure that ATTACH deals with it correctly. I'm on this bit today. Self-referencing FKs seem to have additional problems :-( The queries I was talking about are these \set tables ''''prim.*''',''forign.*''',''''lone'''' select oid, conparentid, contype, conname, conrelid::regclass, confrelid::regclass, conkey, confkey, conindid::regclass from pg_constraint where contype = 'f' and (conrelid::regclass::text ~ any (array[:tables]) or confrelid::regclass::text ~ any (array[:tables])) order by contype, conrelid, confrelid; select tgconstraint, oid, tgrelid::regclass, tgconstrrelid::regclass, tgname, tgparentid, tgconstrindid::regclass, tgfoid::regproc from pg_trigger where tgconstraint in (select oid from pg_constraint where conrelid::regclass::text ~ any (array[:tables]) or confrelid::regclass::text ~ any (array[:tables])) order by tgconstraint, tgrelid::regclass::text, tgfoid; Written as a single line in psql they let you quickly see all the constraints and their associated triggers, so for instance you can see whether this sequence create table prim (a int primary key) partition by list (a); create table prim1 partition of prim for values in (1); create table prim2 partition of prim for values in (2); create table forign (a int references prim) partition by list (a); create table forign1 partition of forign for values in (1); create table forign2 partition of forign for values in (2); alter table forign detach partition forign1; produces the same set of constraints and triggers as this other sequence create table prim (a int primary key) partition by list (a); create table prim1 partition of prim for values in (1); create table prim2 partition of prim for values in (2); create table forign (a int references prim) partition by list (a); create table forign2 partition of forign for values in (2); create table forign1 (a int references prim); The patch is more or less like the attached, far from ready. -- Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/ Syntax error: function hell() needs an argument. Please choose what hell you want to involve.
Commits
-
Fix some more bugs in foreign keys connecting partitioned tables
- f7d510a38245 16.5 landed
- 936ab6de9595 17.1 landed
- 2d5fe514052a 18.0 landed
- 1b216fceffe3 15.9 landed
-
Restructure foreign key handling code for ATTACH/DETACH
- d20194cead75 13.17 landed
- 5d83bad6b87d 15.9 landed
- 5914a22f6ea5 17.1 landed
- 53af9491a043 18.0 landed
- 46a8c27a7226 14.14 landed
- 2aaf2a28b87e 16.5 landed
-
Create foreign key triggers in partitioned tables too
- f4566345cf40 15.0 cited