From 7bb83dfe4b84a808b7ecf1f84561bdf6fd05ab0a Mon Sep 17 00:00:00 2001 From: reshke Date: Fri, 18 Sep 2026 22:25:13 +0300 Subject: [PATCH v1] Treat NOT NULL NOT VALID as nullable during IMPORT SCHEMA --- contrib/postgres_fdw/postgres_fdw.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c index fbe01fdde85..2ab5e2bd1e3 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -6601,8 +6601,27 @@ postgresImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid) appendStringInfoString(&buf, "SELECT relname, " " attname, " - " format_type(atttypid, atttypmod), " - " attnotnull, " + " format_type(atttypid, atttypmod), "); + + /* + * NOT VALID NOT NULL constraints are supported since Postgres 18. + * pg_attribute.attnotnull is set for such constraints, but they are not + * yet validated, so the remote table might still contain NULLs. + */ + if (PQserverVersion(conn) >= 180000) + appendStringInfoString(&buf, + " CASE WHEN EXISTS (" + " SELECT 1 FROM pg_catalog.pg_constraint con " + " WHERE con.conrelid = c.oid " + " AND con.contype = 'n' " + " AND a.attnum = ANY (con.conkey) " + " AND NOT con.convalidated) " + " THEN false ELSE a.attnotnull END, "); + else + appendStringInfoString(&buf, + " attnotnull, "); + + appendStringInfoString(&buf, " pg_get_expr(adbin, adrelid), "); /* Generated columns are supported since Postgres 12 */ -- 2.43.0