From a0967f1a71a65e7802f504002a6dc3dfd1f4a6bb Mon Sep 17 00:00:00 2001 From: amit Date: Thu, 3 Aug 2017 10:31:21 +0900 Subject: [PATCH] Honor NOT VALID specification in CREATE FOREIGN TABLE It should be possible for a user to mark the constraints on foreign tables as NOT VALID even when creating the table, because the remote data they point to may contain constraint-violating rows, which the database has no way to detect and show an error message for. --- doc/src/sgml/ref/create_foreign_table.sgml | 7 +++++++ doc/src/sgml/ref/create_table.sgml | 7 +++++++ src/backend/parser/parse_utilcmd.c | 3 ++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/ref/create_foreign_table.sgml b/doc/src/sgml/ref/create_foreign_table.sgml index 065c982082..72bf37b8b9 100644 --- a/doc/src/sgml/ref/create_foreign_table.sgml +++ b/doc/src/sgml/ref/create_foreign_table.sgml @@ -222,6 +222,13 @@ CHECK ( expression ) [ NO INHERIT ] A constraint marked with NO INHERIT will not propagate to child tables. + + + It is possible to mark the constraint as NOT VALID if it is + specified as a table constraint. If marked as such, the database will + not assume that the constraint holds for all the rows in the table until + it is validated by using the VALIDATE CONSTRAINT command. + See . diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml index e9c2c49533..72de64a03e 100644 --- a/doc/src/sgml/ref/create_table.sgml +++ b/doc/src/sgml/ref/create_table.sgml @@ -652,6 +652,13 @@ FROM ( { numeric_literal | PostgreSQL versions before 9.5 did not honor any particular firing order for CHECK constraints.) + + + Note that even if the constraint is marked as NOT VALID, + it is considered validated as the table that is just created cannot + contain any data. In other words, specifying NOT VALID in + this case has no effect. + diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index 9f37f1b920..e54322b460 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -165,6 +165,7 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString) Oid existing_relid; ParseCallbackState pcbstate; bool like_found = false; + bool is_foreign_table = IsA(stmt, CreateForeignTableStmt); /* * We must not scribble on the passed-in CreateStmt, so copy it. (This is @@ -330,7 +331,7 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString) /* * Postprocess check constraints. */ - transformCheckConstraints(&cxt, true); + transformCheckConstraints(&cxt, !is_foreign_table ? true : false); /* * Output results. -- 2.11.0