Re: addFkRecurseReferencing use unassigned fkconstraint->fk_with_period value

Поиск
Список
Период
Сортировка
Искать

Re: addFkRecurseReferencing use unassigned fkconstraint->fk_with_period value

От:
Andrew Krylosov <krylosov.andrew@gmail.com>
Дата:
Hi,

Haibo Yan wrote:
> The fix is to set conwithperiod from the correct source:

I tested v1 on 3c5d9d914f with assertions enabled on macos.
without_overlaps and the full regress and isolation suites pass.
With only the tablecmds.c changes reverted, the new tests fail at all
four expected rejection cases: VALIDATE, ENFORCED, and both ATTACH
paths.

The changes look right to me.  In addFkRecurseReferencing(), with_period
is correct for both the parser path and the partially reconstructed
Constraint from CloneFkReferencing().  The other two assignments take it
from the catalog row being validated.  I found no other producer of a
foreign-key NewConstraint.

I tried multiranges with gaps in coverage, partitioned tables on both
sides of the FK, recursive validation and enforcement, attaching a
partitioned table with a different column order, and reusing a NOT VALID
FK during ADD FOREIGN KEY.  The affected operations accept uncovered
rows on the base revision and reject them with v1; the corresponding
covered rows pass with v1.

One point for the commit message: constraints that went through one of
these paths on 18.x may already be marked valid with uncovered rows.
The fix does not recheck them, and VALIDATE CONSTRAINT is a no-op for
a valid constraint.  Such rows can be found with a query modeled on the
RI check; alternatively, with the fix, the constraint can be dropped and
re-added as NOT VALID in one transaction and then validated separately,
which keeps it enforced for new rows if validation fails.

For a separate cleanup, RI_Initial_Check() could return false when
riinfo->hasperiod is true.  It already fetches that information, so
this could avoid making callers maintain another copy of the flag.
I think the minimal fix in v1 is suitable for backpatching as it stands.

Two small test nits: "with pre-existing rows" would read better than
"with rows already", and the NOT VALID/NOT ENFORCED block would fit
better after the pg_get_constraintdef checks, keeping those checks
next to the constraint they inspect.

I think this is ready for a committer.

Best regards,
Andrew Krylosov


Re: addFkRecurseReferencing use unassigned fkconstraint->fk_with_period value

От:
Haibo Yan <tristan.yim@gmail.com>
Дата:
Вложения:
On Mon, Sep 28, 2026 at 9:17 AM Andrew Krylosov
 wrote:
>
> Hi,
>
> Haibo Yan wrote:
> > The fix is to set conwithperiod from the correct source:
>
> I tested v1 on 3c5d9d914f with assertions enabled on macos.
> without_overlaps and the full regress and isolation suites pass.
> With only the tablecmds.c changes reverted, the new tests fail at all
> four expected rejection cases: VALIDATE, ENFORCED, and both ATTACH
> paths.
>
> The changes look right to me.  In addFkRecurseReferencing(), with_period
> is correct for both the parser path and the partially reconstructed
> Constraint from CloneFkReferencing().  The other two assignments take it
> from the catalog row being validated.  I found no other producer of a
> foreign-key NewConstraint.
>
> I tried multiranges with gaps in coverage, partitioned tables on both
> sides of the FK, recursive validation and enforcement, attaching a
> partitioned table with a different column order, and reusing a NOT VALID
> FK during ADD FOREIGN KEY.  The affected operations accept uncovered
> rows on the base revision and reject them with v1; the corresponding
> covered rows pass with v1.
>
> One point for the commit message: constraints that went through one of
> these paths on 18.x may already be marked valid with uncovered rows.
> The fix does not recheck them, and VALIDATE CONSTRAINT is a no-op for
> a valid constraint.  Such rows can be found with a query modeled on the
> RI check; alternatively, with the fix, the constraint can be dropped and
> re-added as NOT VALID in one transaction and then validated separately,
> which keeps it enforced for new rows if validation fails.
>
> For a separate cleanup, RI_Initial_Check() could return false when
> riinfo->hasperiod is true.  It already fetches that information, so
> this could avoid making callers maintain another copy of the flag.
> I think the minimal fix in v1 is suitable for backpatching as it stands.
>
> Two small test nits: "with pre-existing rows" would read better than
> "with rows already", and the NOT VALID/NOT ENFORCED block would fit
> better after the pg_get_constraintdef checks, keeping those checks
> next to the constraint they inspect.
>
> I think this is ready for a committer.
>
> Best regards,
> Andrew Krylosov

Hi Andrew,

Thanks for the review and additional testing.

I addressed both test comments in v2: changed the wording to "with pre-existing
rows" and moved the NOT VALID / NOT ENFORCED tests after the
pg_get_constraintdef checks.

There are no code changes from v1.

Thanks,
Haibo

Re: addFkRecurseReferencing use unassigned fkconstraint->fk_with_period value

От:
Paul A Jungwirth <pj@illuminatedcomputing.com>
Дата:
Вложения:
On Mon, Sep 28, 2026 at 11:18 PM Haibo Yan  wrote:
>
 changed the wording to "with pre-existing
> rows" and moved the NOT VALID / NOT ENFORCED tests after the
> pg_get_constraintdef checks.
>
> There are no code changes from v1.

This looks good to me too. Andres reported the VALIDATE CONSTRAINT
case in [0], but it's nice to have fixes to all three cases in one
place.

If you want, here is a v3 with a less verbose commit message. Also I
added the note suggested by Andrew, which seems worth including in
release notes.

[0] https://www.postgresql.org/message-id/sesqxzfcujkcnrwpir5xvhy7iamhsbjiqt6r2xqmb7hqkulte6%40bbuc42qzanzs

Yours,

-- 
Paul              ~{:-)
pj@illuminatedcomputing.com

Re: addFkRecurseReferencing use unassigned fkconstraint->fk_with_period value

От:
Haibo Yan <tristan.yim@gmail.com>
Дата:
On Thu, Oct 8, 2026 at 10:22 PM Paul A Jungwirth
 wrote:
>
> On Mon, Sep 28, 2026 at 11:18 PM Haibo Yan  wrote:
> >
>  changed the wording to "with pre-existing
> > rows" and moved the NOT VALID / NOT ENFORCED tests after the
> > pg_get_constraintdef checks.
> >
> > There are no code changes from v1.
>
> This looks good to me too. Andres reported the VALIDATE CONSTRAINT
> case in [0], but it's nice to have fixes to all three cases in one
> place.
>
> If you want, here is a v3 with a less verbose commit message. Also I
> added the note suggested by Andrew, which seems worth including in
> release notes.
>
> [0] https://www.postgresql.org/message-id/sesqxzfcujkcnrwpir5xvhy7iamhsbjiqt6r2xqmb7hqkulte6%40bbuc42qzanzs
>
> Yours,
>
> --
> Paul              ~{:-)
> pj@illuminatedcomputing.com

Hi Paul,

Thanks for picking this up and preparing v3.

The changes look good to me. One minor wording nit in the commit
message: "despite covering violating rows" might be clearer as
"despite containing violating rows".

Otherwise, v3 looks good to me.

Thanks,
Haibo


Re: addFkRecurseReferencing use unassigned fkconstraint->fk_with_period value

От:
solai v <solai.cdac@gmail.com>
Дата:
Hi,

I tested the v3 patch for the PERIOD foreign-key validation issue.
Before applying the patch, I reproduced the bug: attaching a partition
containing an invalid existing row succeeded unexpectedly, while
inserting the same invalid row afterward failed with a foreign-key
violation.
After applying the patch and rebuilding PostgreSQL, the same partition
attachment was correctly rejected with a foreign-key violation.
I also ran 'make check' and all regression tests passed. The added
regression tests cover 'VALIDATE CONSTRAINT', 'ALTER CONSTRAINT ...
ENFORCED', and 'ATTACH PARTITION', including cases where existing rows
are invalid or their periods are covered after correction.
Based on my testing, the patch fixes the reproduced
partition-attachment issue, and I did not observe regression-test
failures.

Regards,
solai


FAQ