From fc2ac1fb41c2defb8caf825781af75db158fb7a9 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 17 Dec 2014 17:00:53 -0500 Subject: Allow CHECK constraints to be placed on foreign tables. As with NOT NULL constraints, we consider that such constraints are merely reports of constraints that are being enforced by the remote server (or other underlying storage mechanism). Their only real use is to allow planner optimizations, for example in constraint-exclusion checks. Thus, the code changes here amount to little more than removal of the error that was formerly thrown for applying CHECK to a foreign table. (In passing, do a bit of cleanup of the ALTER FOREIGN TABLE reference page, which had accumulated some weird decisions about ordering etc.) Shigeru Hanada and Etsuro Fujita, reviewed by Kyotaro Horiguchi and Ashutosh Bapat. --- doc/src/sgml/ref/alter_foreign_table.sgml | 113 ++++++++++++++++++++++------- doc/src/sgml/ref/create_foreign_table.sgml | 65 ++++++++++++++++- 2 files changed, 149 insertions(+), 29 deletions(-) (limited to 'doc/src') diff --git a/doc/src/sgml/ref/alter_foreign_table.sgml b/doc/src/sgml/ref/alter_foreign_table.sgml index 9d9c439315f..ff48ab88829 100644 --- a/doc/src/sgml/ref/alter_foreign_table.sgml +++ b/doc/src/sgml/ref/alter_foreign_table.sgml @@ -42,6 +42,8 @@ ALTER FOREIGN TABLE [ IF EXISTS ] namecolumn_name SET ( attribute_option = value [, ... ] ) ALTER [ COLUMN ] column_name RESET ( attribute_option [, ... ] ) ALTER [ COLUMN ] column_name OPTIONS ( [ ADD | SET | DROP ] option ['value'] [, ... ]) + ADD table_constraint + DROP CONSTRAINT [ IF EXISTS ] constraint_name [ RESTRICT | CASCADE ] DISABLE TRIGGER [ trigger_name | ALL | USER ] ENABLE TRIGGER [ trigger_name | ALL | USER ] ENABLE REPLICA TRIGGER trigger_name @@ -87,16 +89,6 @@ ALTER FOREIGN TABLE [ IF EXISTS ] name - - IF EXISTS - - - Do not throw an error if the foreign table does not exist. A notice is - issued in this case. - - - - SET DATA TYPE @@ -153,41 +145,54 @@ ALTER FOREIGN TABLE [ IF EXISTS ] name - DISABLE/ENABLE [ REPLICA | ALWAYS ] TRIGGER + ADD table_constraint - These forms configure the firing of trigger(s) belonging to the foreign - table. See the similar form of for more - details. + This form adds a new constraint to a foreign table, using the same + syntax as . + Currently only CHECK constraints are supported. + + + + Unlike the case when adding a constraint to a regular table, nothing is + done to verify the constraint is correct; rather, this action simply + declares that some new condition holds for all rows in the foreign + table. (See the discussion in .) + Note that constraints on foreign tables cannot be marked + NOT VALID since such constraints are simply declarative. - OWNER + DROP CONSTRAINT [ IF EXISTS ] - This form changes the owner of the foreign table to the - specified user. + This form drops the specified constraint on a foreign table. + If IF EXISTS is specified and the constraint + does not exist, no error is thrown. + In this case a notice is issued instead. - RENAME + DISABLE/ENABLE [ REPLICA | ALWAYS ] TRIGGER - The RENAME forms change the name of a foreign table - or the name of an individual column in a foreign table. + These forms configure the firing of trigger(s) belonging to the foreign + table. See the similar form of for more + details. - SET SCHEMA + OWNER - This form moves the foreign table into another schema. + This form changes the owner of the foreign table to the + specified user. @@ -207,6 +212,25 @@ ALTER FOREIGN TABLE [ IF EXISTS ] name + + RENAME + + + The RENAME forms change the name of a foreign table + or the name of an individual column in a foreign table. + + + + + + SET SCHEMA + + + This form moves the foreign table into another schema. + + + + @@ -218,6 +242,12 @@ ALTER FOREIGN TABLE [ IF EXISTS ] name + + If the command is written as ALTER FOREIGN TABLE IF EXISTS ... + and the foreign table does not exist, no error is thrown. A notice is + issued in this case. + + You must own the table to use ALTER FOREIGN TABLE. To change the schema of a foreign table, you must also have @@ -284,12 +314,30 @@ ALTER FOREIGN TABLE [ IF EXISTS ] name + + table_constraint + + + New table constraint for the foreign table. + + + + + + constraint_name + + + Name of an existing constraint to drop. + + + + CASCADE Automatically drop objects that depend on the dropped column - (for example, views referencing the column). + or constraint (for example, views referencing the column). @@ -298,7 +346,7 @@ ALTER FOREIGN TABLE [ IF EXISTS ] nameRESTRICT - Refuse to drop the column if there are any dependent + Refuse to drop the column or constraint if there are any dependent objects. This is the default behavior. @@ -365,10 +413,10 @@ ALTER FOREIGN TABLE [ IF EXISTS ] name Consistency with the foreign server is not checked when a column is added or removed with ADD COLUMN or - DROP COLUMN, a NOT NULL constraint is - added, or a column type is changed with SET DATA TYPE. It is - the user's responsibility to ensure that the table definition matches the - remote side. + DROP COLUMN, a NOT NULL + or CHECK constraint is added, or a column type is changed + with SET DATA TYPE. It is the user's responsibility to ensure + that the table definition matches the remote side. @@ -413,4 +461,13 @@ ALTER FOREIGN TABLE myschema.distributors OPTIONS (ADD opt1 'value', SET opt2, ' extension of SQL, which disallows zero-column foreign tables. + + + See Also + + + + + + diff --git a/doc/src/sgml/ref/create_foreign_table.sgml b/doc/src/sgml/ref/create_foreign_table.sgml index 46a20eff14f..a24aa6e6d1d 100644 --- a/doc/src/sgml/ref/create_foreign_table.sgml +++ b/doc/src/sgml/ref/create_foreign_table.sgml @@ -19,7 +19,8 @@ CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name ( [ - column_name data_type [ OPTIONS ( option 'value' [, ... ] ) ] [ COLLATE collation ] [ column_constraint [ ... ] ] + { column_name data_type [ OPTIONS ( option 'value' [, ... ] ) ] [ COLLATE collation ] [ column_constraint [ ... ] ] + | table_constraint } [, ... ] ] ) SERVER server_name @@ -30,7 +31,13 @@ CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name [ CONSTRAINT constraint_name ] { NOT NULL | NULL | + CHECK ( expression ) | DEFAULT default_expr } + +and table_constraint is: + +[ CONSTRAINT constraint_name ] +CHECK ( expression ) @@ -137,6 +144,28 @@ CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name + + CHECK ( expression ) + + + The CHECK clause specifies an expression producing a + Boolean result which each row in the foreign table is expected + to satisfy; that is, the expression should produce TRUE or UNKNOWN, + never FALSE, for all rows in the foreign table. + A check constraint specified as a column constraint should + reference that column's value only, while an expression + appearing in a table constraint can reference multiple columns. + + + + Currently, CHECK expressions cannot contain + subqueries nor refer to variables other than columns of the + current row. The system column tableoid + may be referenced, but not any other system column. + + + + DEFAULT default_expr @@ -187,6 +216,40 @@ CREATE FOREIGN TABLE [ IF NOT EXISTS ] table_name + + Notes + + + Constraints on foreign tables (such as CHECK + or NOT NULL clauses) are not enforced by the + core PostgreSQL system, and most foreign data wrappers + do not attempt to enforce them either; that is, the constraint is + simply assumed to hold true. There would be little point in such + enforcement since it would only apply to rows inserted or updated via + the foreign table, and not to rows modified by other means, such as + directly on the remote server. Instead, a constraint attached to a + foreign table should represent a constraint that is being enforced by + the remote server. + + + + Some special-purpose foreign data wrappers might be the only access + mechanism for the data they access, and in that case it might be + appropriate for the foreign data wrapper itself to perform constraint + enforcement. But you should not assume that a wrapper does that + unless its documentation says so. + + + + Although PostgreSQL does not attempt to enforce + constraints on foreign tables, it does assume that they are correct + for purposes of query optimization. If there are rows visible in the + foreign table that do not satisfy a declared constraint, queries on + the table might produce incorrect answers. It is the user's + responsibility to ensure that the constraint definition matches + reality. + + Examples -- cgit v1.2.3