Commit a9f2e31c authored by Peter Eisentraut's avatar Peter Eisentraut

Support CREATE TABLE (LIKE ...) with foreign tables and views

Composite types are not yet supported, because parserOpenTable()
rejects them.
parent 07123dff
......@@ -368,6 +368,11 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
If the same name is specified explicitly or in another
<literal>LIKE</literal> clause, an error is signalled.
</para>
<para>
The <literal>LIKE</literal> clause can also be used to copy columns from
views or foreign tables. Inapplicable options (e.g., <literal>INCLUDING
INDEXES</literal> from a view) are ignored.
</para>
</listitem>
</varlistentry>
......
......@@ -644,10 +644,12 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
relation = parserOpenTable(cxt->pstate, table_like_clause->relation,
AccessShareLock);
if (relation->rd_rel->relkind != RELKIND_RELATION)
if (relation->rd_rel->relkind != RELKIND_RELATION
&& relation->rd_rel->relkind != RELKIND_VIEW
&& relation->rd_rel->relkind != RELKIND_FOREIGN_TABLE)
ereport(ERROR,
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
errmsg("LIKE source relation \"%s\" is not a table",
errmsg("LIKE source relation \"%s\" is not a table, view, or foreign table",
table_like_clause->relation->relname)));
/*
......
......@@ -220,3 +220,22 @@ ERROR: column "a" has a storage parameter conflict
DETAIL: MAIN versus EXTENDED
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
NOTICE: drop cascades to table inhe
/* LIKE with other relation kinds */
CREATE TABLE ctlt4 (a int, b text);
CREATE SEQUENCE ctlseq1;
CREATE TABLE ctlt10 (LIKE ctlseq1); -- fail
ERROR: LIKE source relation "ctlseq1" is not a table, view, or foreign table
CREATE VIEW ctlv1 AS SELECT * FROM ctlt4;
CREATE TABLE ctlt11 (LIKE ctlv1);
CREATE TABLE ctlt11a (LIKE ctlv1 INCLUDING ALL);
CREATE TYPE ctlty1 AS (a int, b text);
CREATE TABLE ctlt12 (LIKE ctlty1); -- currently fails
ERROR: "ctlty1" is a composite type
LINE 1: CREATE TABLE ctlt12 (LIKE ctlty1);
^
DROP SEQUENCE ctlseq1;
DROP TYPE ctlty1;
DROP VIEW ctlv1;
DROP TABLE IF EXISTS ctlt4, ctlt10, ctlt11, ctlt11a, ctlt12;
NOTICE: table "ctlt10" does not exist, skipping
NOTICE: table "ctlt12" does not exist, skipping
......@@ -97,3 +97,23 @@ CREATE TABLE inh_error1 () INHERITS (ctlt1, ctlt4);
CREATE TABLE inh_error2 (LIKE ctlt4 INCLUDING STORAGE) INHERITS (ctlt1);
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
/* LIKE with other relation kinds */
CREATE TABLE ctlt4 (a int, b text);
CREATE SEQUENCE ctlseq1;
CREATE TABLE ctlt10 (LIKE ctlseq1); -- fail
CREATE VIEW ctlv1 AS SELECT * FROM ctlt4;
CREATE TABLE ctlt11 (LIKE ctlv1);
CREATE TABLE ctlt11a (LIKE ctlv1 INCLUDING ALL);
CREATE TYPE ctlty1 AS (a int, b text);
CREATE TABLE ctlt12 (LIKE ctlty1); -- currently fails
DROP SEQUENCE ctlseq1;
DROP TYPE ctlty1;
DROP VIEW ctlv1;
DROP TABLE IF EXISTS ctlt4, ctlt10, ctlt11, ctlt11a, ctlt12;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment