Re: BUG #19698: IMPORT FOREIGN SCHEMA treats a NOT VALID NOT NULL constraint as validated

From: Kirill Reshke <reshkekirill(at)gmail(dot)com>
To: imchifan(at)163(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19698: IMPORT FOREIGN SCHEMA treats a NOT VALID NOT NULL constraint as validated
Date: 2026-09-18 20:03:30
Message-ID: CALdSSPjL=bQoEeiaf=4nrG04-nG5rH1mEuUu5Y+-dWNTA4koPQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Fri, 18 Sept 2026 at 13:25, PG Bug reporting form
<noreply(at)postgresql(dot)org> wrote:
>
> The following bug has been logged on the website:

> Description
> -----------
> When IMPORT FOREIGN SCHEMA imports a remote table having a NOT NULL
> constraint declared NOT VALID, postgres_fdw creates trusted local NOT NULL
> metadata. The remote table can still contain NULL values because its
> constraint has not been validated. With constraint_exclusion enabled,
> PostgreSQL relies on the imported metadata and incorrectly excludes a query
> that would find such a row. Queries through the imported foreign table can
> therefore silently omit existing rows.

I think this analysis is correct. Thanks.

One simple fix can be simply importing NOT NULL NOT VALID as a
nullable column, but this probably would make some people unhappy.

Another option is to actually declare the column as NOT NULL NOT
VALID. This patch is required to support NOT VALID constr during
create DDL.
NOT VALID contrs are impossible for regular relation since they are
created empty, but that's not the case for FDW. I have done this in
simple POC

PFA both patches.

--
Best regards,
Kirill Reshke

Attachment Content-Type Size
v1-0001-Treat-NOT-NULL-NOT-VALID-as-nullable-during-IMPOR.patch application/octet-stream 1.7 KB
v1-0001-Fix-NOT-NULL-NOT-VALID-constraints-import-in-FDW.patch application/octet-stream 8.0 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message Kirill Reshke 2026-09-18 16:29:58 Re: BUG #19698: IMPORT FOREIGN SCHEMA treats a NOT VALID NOT NULL constraint as validated