Defines a new foreign table.
CREATE FOREIGN TABLE [ IF NOT EXISTS ] <table_name> ( [
<column_name> <data_type> [ OPTIONS ( <option> '<value>' [, ... ] ) ] [ COLLATE <collation> ] [ <column_constraint> [ ... ] ]
[, ... ]
] )
SERVER <server_name>
[ OPTIONS ( [ mpp_execute { 'master' | 'any' | 'all segments' } [, ] ] <option> '<value>' [, ... ] ) ]
where column_constraint is:
[ CONSTRAINT <constraint_name> ]
{ NOT NULL |
NULL |
DEFAULT <default_expr> }
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, or 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.
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.
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.
The 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.
Greenplum Database uses the default expression in any insert operation that does not specify a value for the column. If there is no default for a column, then the default is null.
A Greenplum Database-specific option that identifies the host from which the foreign-data wrapper reads or writes data:
master
(the default)—Read or write data from the master host.any
—Read data from either the master 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. 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.
The VMware Greenplum Query Optimizer, GPORCA, does not support foreign tables. A query on a foreign table always falls back to the Postgres Planner.
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
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 a default value is a Greenplum Database extension, as is the mpp_execute
option.
ALTER FOREIGN TABLE, DROP FOREIGN TABLE, CREATE SERVER
Parent topic: SQL Commands