Re: [BUG] parenting a PK constraint to a self-FK one (Was: Self FK oddity when attaching a partition)
Alvaro Herrera <alvherre@alvh.no-ip.org>
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
To: Jehan-Guillaume de Rorthais <jgdr@dalibo.com>
Cc: pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2022-08-23T16:30:06Z
Lists: pgsql-hackers
Attachments
- 0001-test-fix.patch (text/x-diff)
On 2022-Aug-23, Jehan-Guillaume de Rorthais wrote: Hi, [...] > However, it seems get_relation_idx_constraint_oid(), introduced in eb7ed3f3063, > assume there could be only ONE constraint depending to an index. But in fact, > multiple constraints can rely on the same index, eg.: the PK and a self > referencing FK. In consequence, when looking for a constraint depending on an > index for the given relation, either the FK or a PK can appears first depending > on various conditions. It is then possible to trick it make a FK constraint a > parent of a PK... Hmm, wow, that sounds extremely stupid. I think a sufficient fix might be to have get_relation_idx_constraint_oid ignore any constraints that are not unique or primary keys. I tried your scenario with the attached and it seems to work correctly. Can you confirm? (I only ran the pg_regress tests, not anything else for now.) If this is OK, we should make this API quirkiness very explicit in the comments, so the patch needs to be a few lines larger in order to be committable. Also, perhaps the check should be that contype equals either primary or unique, rather than it doesn't equal foreign. -- Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/
Commits
-
Create FKs properly when attaching table as partition
- c301e1c0c09c 15.1 landed
- b0284bfb1db5 16.0 landed
- ab70b3a52fde 12.13 landed
- 41b6e7c9a32e 13.9 landed
- 18865f4df9ca 14.6 landed
-
Fix self-referencing foreign keys with partitioned tables
- 7d520e68ea1e 13.9 landed
- 669803af04e6 12.13 landed
- 614a406b4ff1 16.0 landed
- 6083132abdd4 15.0 landed
- 483d26930b4c 14.6 landed
-
Fix GetForeignKey*Triggers for self-referential FKs
- 8c848cd4b8e9 16.0 landed
- 68b0da67794e 15.0 landed
-
Choose FK name correctly during partition attachment
- e7936f8b3e57 16.0 landed
- ade2409b18e9 15.0 landed
- 80ef25b1adb1 13.9 landed
- 640c20d6266d 14.6 landed
- 562e100aeeaf 12.13 landed
-
Update SQL features
- 7e367924e33e 15.0 cited
-
Restore the previous semantics of get_constraint_index().
- 641f3dffcdf1 15.0 cited