Skip to content

Commit a9f2e31

Browse files
committed
Support CREATE TABLE (LIKE ...) with foreign tables and views
Composite types are not yet supported, because parserOpenTable() rejects them.
1 parent 07123df commit a9f2e31

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

doc/src/sgml/ref/create_table.sgml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,11 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
368368
If the same name is specified explicitly or in another
369369
<literal>LIKE</literal> clause, an error is signalled.
370370
</para>
371+
<para>
372+
The <literal>LIKE</literal> clause can also be used to copy columns from
373+
views or foreign tables. Inapplicable options (e.g., <literal>INCLUDING
374+
INDEXES</literal> from a view) are ignored.
375+
</para>
371376
</listitem>
372377
</varlistentry>
373378

src/backend/parser/parse_utilcmd.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,10 +644,12 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
644644
relation = parserOpenTable(cxt->pstate, table_like_clause->relation,
645645
AccessShareLock);
646646

647-
if (relation->rd_rel->relkind != RELKIND_RELATION)
647+
if (relation->rd_rel->relkind != RELKIND_RELATION
648+
&& relation->rd_rel->relkind != RELKIND_VIEW
649+
&& relation->rd_rel->relkind != RELKIND_FOREIGN_TABLE)
648650
ereport(ERROR,
649651
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
650-
errmsg("LIKE source relation \"%s\" is not a table",
652+
errmsg("LIKE source relation \"%s\" is not a table, view, or foreign table",
651653
table_like_clause->relation->relname)));
652654

653655
/*

src/test/regress/expected/create_table_like.out

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,22 @@ ERROR: column "a" has a storage parameter conflict
220220
DETAIL: MAIN versus EXTENDED
221221
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
222222
NOTICE: drop cascades to table inhe
223+
/* LIKE with other relation kinds */
224+
CREATE TABLE ctlt4 (a int, b text);
225+
CREATE SEQUENCE ctlseq1;
226+
CREATE TABLE ctlt10 (LIKE ctlseq1); -- fail
227+
ERROR: LIKE source relation "ctlseq1" is not a table, view, or foreign table
228+
CREATE VIEW ctlv1 AS SELECT * FROM ctlt4;
229+
CREATE TABLE ctlt11 (LIKE ctlv1);
230+
CREATE TABLE ctlt11a (LIKE ctlv1 INCLUDING ALL);
231+
CREATE TYPE ctlty1 AS (a int, b text);
232+
CREATE TABLE ctlt12 (LIKE ctlty1); -- currently fails
233+
ERROR: "ctlty1" is a composite type
234+
LINE 1: CREATE TABLE ctlt12 (LIKE ctlty1);
235+
^
236+
DROP SEQUENCE ctlseq1;
237+
DROP TYPE ctlty1;
238+
DROP VIEW ctlv1;
239+
DROP TABLE IF EXISTS ctlt4, ctlt10, ctlt11, ctlt11a, ctlt12;
240+
NOTICE: table "ctlt10" does not exist, skipping
241+
NOTICE: table "ctlt12" does not exist, skipping

src/test/regress/sql/create_table_like.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,23 @@ CREATE TABLE inh_error1 () INHERITS (ctlt1, ctlt4);
9797
CREATE TABLE inh_error2 (LIKE ctlt4 INCLUDING STORAGE) INHERITS (ctlt1);
9898

9999
DROP TABLE ctlt1, ctlt2, ctlt3, ctlt4, ctlt12_storage, ctlt12_comments, ctlt1_inh, ctlt13_inh, ctlt13_like, ctlt_all, ctla, ctlb CASCADE;
100+
101+
102+
/* LIKE with other relation kinds */
103+
104+
CREATE TABLE ctlt4 (a int, b text);
105+
106+
CREATE SEQUENCE ctlseq1;
107+
CREATE TABLE ctlt10 (LIKE ctlseq1); -- fail
108+
109+
CREATE VIEW ctlv1 AS SELECT * FROM ctlt4;
110+
CREATE TABLE ctlt11 (LIKE ctlv1);
111+
CREATE TABLE ctlt11a (LIKE ctlv1 INCLUDING ALL);
112+
113+
CREATE TYPE ctlty1 AS (a int, b text);
114+
CREATE TABLE ctlt12 (LIKE ctlty1); -- currently fails
115+
116+
DROP SEQUENCE ctlseq1;
117+
DROP TYPE ctlty1;
118+
DROP VIEW ctlv1;
119+
DROP TABLE IF EXISTS ctlt4, ctlt10, ctlt11, ctlt11a, ctlt12;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy