Re: NOT ENFORCED constraint feature
amul sul <sulamul@gmail.com>
From: Amul Sul <sulamul@gmail.com>
To: Peter Eisentraut <peter@eisentraut.org>
Cc: jian he <jian.universality@gmail.com>,
Alvaro Herrera <alvherre@alvh.no-ip.org>, pgsql-hackers <pgsql-hackers@postgresql.org>,
Joel Jacobson <joel@compiler.org>
Date: 2025-01-28T10:58:00Z
Lists: pgsql-hackers
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Add support for NOT ENFORCED in foreign key constraints
- eec0040c4bcd 18.0 landed
-
Expand test a bit
- 5d5f415816a6 18.0 landed
-
refactor: Pass relation OID instead of Relation to createForeignKeyCheckTriggers()
- ef7a5af77d44 18.0 landed
-
refactor: Split ATExecAlterConstraintInternal()
- 639238b978fe 18.0 landed
-
refactor: Move some code that updates pg_constraint to a separate function
- a3280e2a494f 18.0 landed
-
Move RemoveInheritedConstraint() call slightly earlier
- dabccf45139a 18.0 landed
-
refactor: Split tryAttachPartitionForeignKey()
- 1d26c2d2c4b8 18.0 landed
-
refactor: re-add ATExecAlterChildConstr()
- 64224a834ce4 18.0 landed
-
Add ATAlterConstraint struct for ALTER .. CONSTRAINT
- 80d7f990496b 18.0 landed
-
refactor: split ATExecAlterConstrRecurse()
- 7a947ed25b54 18.0 landed
-
Add support for NOT ENFORCED in CHECK constraints
- ca87c415e2fc 18.0 landed
On Tue, Jan 28, 2025 at 4:01 PM Peter Eisentraut <peter@eisentraut.org> wrote: > > On 20.01.25 17:53, Amul Sul wrote: > >> Attached is a new set of patches. Please ignore patch 0001 here, which > >> was posted separately [1] -- proposes allowing invalid foreign key > >> constraints on partitioned tables. Patch 0002 refactors > >> tryAttachPartitionForeignKey(), primarily needed by the new patch > >> v9-0006 included in this set. This patch handles merging constraints > >> with different enforceability. Without it, a new constraint would be > >> created on the child. However, this patch introduces additional code > >> that may appear somewhat messy or confusing. I've added plenty of > >> comments to clarify the logic. While I’ve done some testing, it hasn’t > >> been extensive. I plan to do more testing in the next week. > >> > >> Please let me know if we should continue with patch 0006 improvement, > >> or if the feature up to patch 0005, which enforces matching > >> enforceability before merging constraints, is sufficient. > >> > >> 1] https://postgr.es/m/CAAJ_b96Bp=-ZwihPPtuaNX=SrZ0U6ZsXD3+fgARO0JuKa8v2jQ@mail.gmail.com > >> > > > > I made minor updates to the attached version, particularly ensuring > > that the order of relation opening and closing remains the same as > > before in ATExecAlterConstrRecurse(). Additionally, I’ve added a > > refactoring patch to make createForeignKeyActionTriggers() accept a > > relation OID instead of a Relation, making this function consistent > > with others like createForeignKeyCheckTriggers(). > > I think v10-0001 has been committed separately now. I can't > successfully apply the remaining patches though. Could you supply an > updated patch set? > Sure, I plan to work on that tomorrow. > Just from looking at them, the refactoring patches 0002, 0003, 0004 seem ok. > > 0005 also seems ok. > Thank you. > > In 0006, this change in the test output should be improved: > > -- XXX: error message is misleading here > ALTER TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key ENFORCED; > -ERROR: ALTER CONSTRAINT statement constraints cannot be marked ENFORCED > -LINE 1: ...TABLE unique_tbl ALTER CONSTRAINT unique_tbl_i_key ENFORCED; > - ^ > +ERROR: constraint "unique_tbl_i_key" of relation "unique_tbl" is not a > foreign key constraint > > Maybe this should be along the lines of "ALTER CONSTRAINT ... ENFORCED > is not supported for %s constraints" or something like that. > Ok, let me see what can be done here. > > This behavior is not correct: > > +-- Changing it back to ENFORCED will leave the constraint in the NOT > VALID state > +ALTER TABLE FKTABLE ALTER CONSTRAINT fktable_ftest1_fkey ENFORCED; > +-- Which needs to be explicitly validated. > +ALTER TABLE FKTABLE VALIDATE CONSTRAINT fktable_ftest1_fkey; > > Setting the constraint to enforced should enforce it immediately. This > SQL statement is covered by the SQL standard. Also, I think it's a > better user experience if you don't require two steps. > Let me clarify: the constraint will be enforced for new inserts and updates, but it won't be validated against existing data, so those will remain marked as invalid. Regards,