Defines a new foreign table.
CREATE FOREIGN TABLE [ IF NOT EXISTS ] <table_name> ( [
{ <column_name> <data_type> [ OPTIONS ( <option> '<value>' [, ... ] ) ] [ COLLATE <collation> ] [ <column_constraint> [ ... ] ]
| <table_constraint> }
[, ... ]
] )
[ INHERITS ( <parent_table> [, ... ] ) ]
SERVER <server_name>
[ OPTIONS ( [ mpp_execute { 'coordinator' | 'any' | 'all segments' } [, ] ] <option> '<value>' [, ... ] ) ]
CREATE FOREIGN TABLE [ IF NOT EXISTS ] <table_name>
PARTITION OF <parent_table> [ (
{ <column_name> [ WITH OPTIONS ] [ <column_constraint> [ ... ] ]
| <table_constraint> }
[, ... ]
) ]
{ FOR VALUES <partition_bound_spec> | DEFAULT }
SERVER <server_name>
[ OPTIONS ( [ mpp_execute { 'coordinator' | 'any' | 'all segments' } [, ] ] <option> '<value>' [, ... ] ) ]
where <column_constraint> is:
[ CONSTRAINT <constraint_name> ]
{ NOT NULL |
NULL |
DEFAULT <default_expr> |
GENERATED ALWAYS AS ( <generation_expr> ) STORED}
and <table_constraint> is:
[ CONSTRAINT <constraint_name> ]
CHECK ( <expression> ) [ NO INHERIT ]
and <partition_bound_spec> is:
IN ( <partition_bound_expr> [, ...] ) |
FROM ( { <partition_bound_expr> | MINVALUE | MAXVALUE } [, ...] )
TO ( { <partition_bound_expr> | MINVALUE | MAXVALUE } [, ...] ) |
WITH ( MODULUS <numeric_literal>, REMAINDER <numeric_literal> )
CREATE FOREIGN TABLE
creates a new foreign table in the current database. The user who creates the foreign table becomes its owner.
If you schema-qualify the table name (for example, CREATE FOREIGN TABLE myschema.mytable ...
), Greenplum Database creates the table in the specified schema. Otherwise, the foreign table is created in the current schema. The name of the foreign table must be distinct from the name of any other foreign table, table, sequence, index, view, or materialized view in the same schema.
Because CREATE FOREIGN TABLE
automatically creates a data type that represents the composite type corresponding to one row of the foreign table, foreign tables cannot have the same name as any existing data type in the same schema.
If the PARTITION OF
clause is specified, then the table is created as a partition of parent_table with specified bounds.
To create a foreign table, you must have USAGE
privilege on the foreign server, as well as USAGE
privilege on all column types used in the table.
COLLATE
clause assigns a collation to the column (which must be of a collatable data type). If not specified, the column data type's default collation is used.
INHERITS
clause specifies a list of tables from which the new foreign table automatically inherits all columns. Parent tables can be plain tables or foreign tables. See the similar form of
CREATE TABLE for more details.
UNIQUE
indexes on the parent table. (See also
ALTER TABLE ATTACH PARTITION.)
col must be positive
can be used to communicate helpful constraint information to client applications. (Double-quotes are needed to specify constraint names that contain spaces.) If a constraint name is not specified, the system generates a name.
The column is allowed to contain null values. This is the default.
This clause is provided only for compatibility with non-standard SQL databases. Its use is discouraged in new applications.
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.
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.
NO INHERIT
will not propagate to child tables.
DEFAULT
clause assigns a default value for the column whose definition it appears within. The value is any variable-free expression; Greenplum Database does not allow subqueries and cross-references to other columns in the current table. The data type of the default expression must match the data type of the column.
STORED
is required to signify that the column will be computed on write. (The computed value will be presented to the foreign-data wrapper for storage and must be returned on reading.)
A Greenplum Database-specific option that identifies the host from which the foreign-data wrapper reads or writes data:
coordinator
(the default)—Read or write data from the coordinator host.any
—Read data from either the coordinator host or any one segment, depending on which path costs less.all segments
—Read or write data from all segments. To support this option value, the foreign-data wrapper must have a policy that matches the segments to data.NoteGreenplum Database supports parallel writes to foreign tables only when you set
mpp_execute 'all segments'
.
Support for the foreign table mpp_execute
option, and the specific modes, is foreign-data wrapper-specific.
The mpp_execute
option can be specified in multiple commands: CREATE FOREIGN TABLE
, CREATE SERVER
, and CREATE FOREIGN DATA WRAPPER
. The foreign table setting takes precedence over the foreign server setting, followed by the foreign-data wrapper setting.
Constraints on foreign tables (such as CHECK
or NOT NULL
clauses) are not enforced by Greenplum Database, 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 Greenplum Database 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 errors or incorrect answers. It is the user's responsibility to ensure that the constraint definition matches reality.
Within a partitioned table containing foreign-table partitions, an UPDATE
that changes the partition key value can cause a row to be moved from a local partition to a foreign-table partition, provided the foreign-data wrapper supports tuple routing. However it is not currently possible to move a row from a foreign-table partition to another partition. An UPDATE
that would require doing that will fail due to the partitioning constraint, assuming that that is properly enforced by the remote server.
Similar considerations apply to generated columns. Stored generated columns are computed on insert or update on the local Greenplum Database server and handed to the foreign-data wrapper for writing out to the foreign data store, but it is not enforced that a query of the foreign table returns values for stored generated columns that are consistent with the generation expression. Again, this might result in incorrect query results.
Create a foreign table named films
with the server named film_server
:
CREATE FOREIGN TABLE films (
code char(5) NOT NULL,
title varchar(40) NOT NULL,
did integer NOT NULL,
date_prod date,
kind varchar(10),
len interval hour to minute
)
SERVER film_server;
Create foreign table measurement_y2016m07
, which will be accessed through the server server_07
, as a partition of the range partitioned table measurement:
CREATE FOREIGN TABLE measurement_y2016m07
PARTITION OF measurement FOR VALUES FROM ('2016-07-01') TO ('2016-08-01')
SERVER server_07;
CREATE FOREIGN TABLE
largely conforms to the SQL standard; however, much as with CREATE TABLE, Greenplum Database permits NULL
constraints and zero-column foreign tables. The ability to specify column default values is a Greenplum Database extension, as is the mpp_execute
option. Table inheritance, in the form defined by Greenplum Database, is nonstandard.
ALTER FOREIGN TABLE, DROP FOREIGN TABLE, CREATE SERVER
Parent topic: SQL Commands