CREATE FOREIGN TABLE
Defines a new foreign table.
Synopsis
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> }
Description
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 …), Greengage DB 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 the USAGE privilege on the foreign server, as well as the USAGE privilege on all column types used in the table.
Parameters
| Parameter | Description |
|---|---|
IF NOT EXISTS |
Do not throw an error if a relation with the same name already exists. Greengage DB issues a notice in this case. Note that there is no guarantee that the existing relation is anything like the one that would have been created |
table_name |
The name (optionally schema-qualified) of the foreign table to create |
column_name |
The name of a column to create in the new foreign table |
data_type |
The data type of the column, including array specifiers |
NOT NULL |
The column is not allowed to contain null values |
NULL |
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 |
DEFAULT <default_expr> |
The |
server_name |
The name of an existing server to use for the foreign table. For details on defining a server, see CREATE SERVER |
OPTIONS ( <option> '<value>' [, … ] ) |
The options for the new foreign table or one of its columns.
While option names must be unique, a table option and a column option may have the same name.
The option names and values are foreign data wrapper-specific.
Greengage DB validates the options and values using the foreign data wrapper’s |
mpp_execute { 'master' | 'any' | 'all segments' } |
A Greengage DB-specific option that identifies the host from which the foreign data wrapper reads or writes data:
Support for the foreign table |
Notes
The GPORCA optimizer does not support foreign tables. A query on a foreign table always falls back to the Postgres planner.
Examples
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;
Compatibility
CREATE FOREIGN TABLE largely conforms to the SQL standard.
However, much as with CREATE TABLE, Greengage DB permits NULL constraints and zero-column foreign tables.
The ability to specify a default value is a Greengage DB extension, as is the mpp_execute option.