Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x7TxP-00000000PPk-0S01 for pgsql-bugs@arkaria.postgresql.org; Fri, 18 Sep 2026 08:28:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1x7TxO-00000008px0-1PTP for pgsql-bugs@arkaria.postgresql.org; Fri, 18 Sep 2026 08:28:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x7SlY-00000008IrG-2l9B for pgsql-bugs@lists.postgresql.org; Fri, 18 Sep 2026 07:12:12 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x7SlS-00000001AWo-1Cg0 for pgsql-bugs@lists.postgresql.org; Fri, 18 Sep 2026 07:12:12 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=FC0y8WH+KoS0pJcdE8GPeQ7Iw2PZY6radcaIbAUB/j0=; b=LaZ0PCCbsygPl/28j9fu/s5EWI MVDISVqQfN7wBZJLs9iGotnviJrrEyk/8Os91Dy5EjNY46Sqf3/8SyZpyAG5hjcpAbgftBI2swkES YJ+zUCysNbEKnZBk5Jolkj1/pNCQtcmKEhiRrCoLoZlQ3mtNFvBsaKqDz4QS/nCSwOo64NIqw24G5 uAD6kw+Oknr7b6ZkZ6+SCVz9OGmBVkIc1WhiPg8NW3dnNrqJ5EYRZL+YJj/ZNZzmLKKSc2SWKY2Bv XqhEPWnA5BQX7QBBat/G/4kh5Iyr+w1RIBYg1tEdfjQaS/8Pgf0nnY/ZedbRcn0NsGaIAU0W02klx 5EXJUv+g==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x7SlR-001vIH-1B for pgsql-bugs@lists.postgresql.org; Fri, 18 Sep 2026 07:12:05 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1x7SlQ-000000077Qy-06WQ for pgsql-bugs@lists.postgresql.org; Fri, 18 Sep 2026 07:12:04 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19698: IMPORT FOREIGN SCHEMA treats a NOT VALID NOT NULL constraint as validated To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: imchifan@163.com Reply-To: imchifan@163.com, pgsql-bugs@lists.postgresql.org Date: Fri, 18 Sep 2026 07:11:23 +0000 Message-ID: <19698-28fd44978ce03a4e@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19698 Logged by: Qifan Liu Email address: imchifan@163.com PostgreSQL version: 18.6 Operating system: Linux/amd64 Description: =20 PostgreSQL version: PostgreSQL 18.6 Operating system: Linux/amd64 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. Steps to reproduce ------------------ Run the following input with psql: \set ON_ERROR_STOP on CREATE DATABASE fdw_not_valid_test; \connect fdw_not_valid_test CREATE EXTENSION postgres_fdw; CREATE SCHEMA remote_schema; CREATE SCHEMA local_schema; CREATE TABLE remote_schema.t (id integer); INSERT INTO remote_schema.t VALUES (NULL), (1); ALTER TABLE remote_schema.t ADD CONSTRAINT remote_nn NOT NULL id NOT VALID; CREATE SERVER loopback_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (dbname 'fdw_not_valid_test'); CREATE USER MAPPING FOR CURRENT_USER SERVER loopback_server; IMPORT FOREIGN SCHEMA remote_schema LIMIT TO (t) FROM SERVER loopback_server INTO local_schema; SELECT a.attnotnull AS imported_attnotnull, c.convalidated AS imported_constraint_validated FROM pg_attribute a JOIN pg_constraint c ON c.conrelid =3D a.attrelid AND a.attnum =3D ANY (c.conkey) WHERE a.attrelid =3D 'local_schema.t'::regclass AND a.attname =3D 'id' AND c.contype =3D 'n'; SET constraint_exclusion =3D on; SELECT count(*) AS null_rows_visible_through_import FROM local_schema.t WHERE id IS NULL; ALTER FOREIGN TABLE local_schema.t ALTER COLUMN id DROP NOT NULL; SELECT count(*) AS null_rows_after_correcting_metadata FROM local_schema.t WHERE id IS NULL; Actual result ------------- imported_attnotnull | imported_constraint_validated ---------------------+------------------------------- t | t null_rows_visible_through_import ---------------------------------- 0 null_rows_after_correcting_metadata ------------------------------------- 1 The imported constraint is represented as validated NOT NULL metadata. The query initially reports no NULL rows, but reports the existing NULL row after that metadata is removed. Expected result --------------- The imported foreign table must not advertise the remote NOT VALID constraint as a validated NOT NULL invariant. The query through the foreign table should return a count of 1, matching the result after the incorrect local metadata is removed, because the remote NULL row remains valid and visible.