On Mon, Oct 07, 2013 at 11:16:56PM -0700, David Fetter wrote:
> Folks,
> 
> Please find attached a patch implementing and documenting, to some
> extent, $subject.  I did this in aid of being able to import SQL
> standard catalogs and other entities where a known example could
> provide a template for a foreign table.
> 
> Should there be errhint()s, too?  Should we pile up all such errors
> and mention them at the end rather than simply bailing on the first
> one?
> 
> TBD: regression tests.

Now included: regression tests for disallowed LIKE options.

Cheers,
David.
-- 
David Fetter <da...@fetter.org> http://fetter.org/
Phone: +1 415 235 3778  AIM: dfetter666  Yahoo!: dfetter
Skype: davidfetter      XMPP: david.fet...@gmail.com
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate
diff --git a/doc/src/sgml/ref/create_foreign_table.sgml 
b/doc/src/sgml/ref/create_foreign_table.sgml
index 1ef4b5e..4a8e265 100644
--- a/doc/src/sgml/ref/create_foreign_table.sgml
+++ b/doc/src/sgml/ref/create_foreign_table.sgml
@@ -20,6 +20,7 @@
 <synopsis>
 CREATE FOREIGN TABLE [ IF NOT EXISTS ] <replaceable 
class="PARAMETER">table_name</replaceable> ( [
     <replaceable class="PARAMETER">column_name</replaceable> <replaceable 
class="PARAMETER">data_type</replaceable> [ OPTIONS ( <replaceable 
class="PARAMETER">option</replaceable> '<replaceable 
class="PARAMETER">value</replaceable>' [, ... ] ) ] [ COLLATE 
<replaceable>collation</replaceable> ] [ <replaceable 
class="PARAMETER">column_constraint</replaceable> [ ... ] ]
+    | LIKE <replaceable>source_table</replaceable> [ 
<replaceable>like_option</replaceable> ... ] }
     [, ... ]
 ] )
   SERVER <replaceable class="parameter">server_name</replaceable>
@@ -114,6 +115,15 @@ CREATE FOREIGN TABLE [ IF NOT EXISTS ] <replaceable 
class="PARAMETER">table_name
    </varlistentry>
 
    <varlistentry>
+    <term><literal>LIKE 
<replaceable>source_table</replaceable></literal></term>
+    <listitem>
+     <para>
+      The <literal>LIKE</literal> clause specifies a table from which
+      the new foreign table automatically copies all column names and their 
data types.
+     </para>
+   </varlistentry>
+
+   <varlistentry>
     <term><literal>NOT NULL</></term>
     <listitem>
      <para>
diff --git a/src/backend/parser/parse_utilcmd.c 
b/src/backend/parser/parse_utilcmd.c
index 19d19e5f..219c910 100644
--- a/src/backend/parser/parse_utilcmd.c
+++ b/src/backend/parser/parse_utilcmd.c
@@ -649,7 +649,7 @@ transformTableConstraint(CreateStmtContext *cxt, Constraint 
*constraint)
 /*
  * transformTableLikeClause
  *
- * Change the LIKE <srctable> portion of a CREATE TABLE statement into
+ * Change the LIKE <srctable> portion of a CREATE [FOREIGN] TABLE statement 
into
  * column definitions which recreate the user defined column portions of
  * <srctable>.
  */
@@ -668,12 +668,6 @@ transformTableLikeClause(CreateStmtContext *cxt, 
TableLikeClause *table_like_cla
        setup_parser_errposition_callback(&pcbstate, cxt->pstate,
                                                                          
table_like_clause->relation->location);
 
-       /* we could support LIKE in many cases, but worry about it another day 
*/
-       if (cxt->isforeign)
-               ereport(ERROR,
-                               (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-                                errmsg("LIKE is not supported for creating 
foreign tables")));
-
        relation = relation_openrv(table_like_clause->relation, 
AccessShareLock);
 
        if (relation->rd_rel->relkind != RELKIND_RELATION &&
@@ -689,6 +683,25 @@ transformTableLikeClause(CreateStmtContext *cxt, 
TableLikeClause *table_like_cla
        cancel_parser_errposition_callback(&pcbstate);
 
        /*
+        * For foreign tables, disallow some options.
+        */
+       if (cxt->isforeign)
+       {
+               if (table_like_clause->options & CREATE_TABLE_LIKE_CONSTRAINTS)
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                        errmsg("ERROR: foreign tables do not 
support LIKE INCLUDING CONSTRAINTS")));
+               else if (table_like_clause->options & CREATE_TABLE_LIKE_INDEXES)
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                        errmsg("ERROR: foreign tables do not 
support LIKE INCLUDING INDEXES")));
+               else if (table_like_clause->options & CREATE_TABLE_LIKE_STORAGE)
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                        errmsg("ERROR: foreign tables do not 
support LIKE INCLUDING STORAGE")));
+       }
+
+       /*
         * Check for privileges
         */
        if (relation->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
diff --git a/src/test/regress/expected/foreign_data.out 
b/src/test/regress/expected/foreign_data.out
index 60506e0..84f5265 100644
--- a/src/test/regress/expected/foreign_data.out
+++ b/src/test/regress/expected/foreign_data.out
@@ -699,6 +699,21 @@ SELECT * FROM ft1;                                         
     -- ERROR
 ERROR:  foreign-data wrapper "dummy" has no handler
 EXPLAIN SELECT * FROM ft1;                                      -- ERROR
 ERROR:  foreign-data wrapper "dummy" has no handler
+CREATE FOREIGN TABLE ft2 (
+    LIKE pg_catalog.pg_enum
+    INCLUDING CONSTRAINTS
+) SERVER s0;                                                    -- ERROR
+ERROR:  ERROR: foreign tables do not support LIKE INCLUDING CONSTRAINTS
+CREATE FOREIGN TABLE ft2 (
+    LIKE pg_catalog.pg_enum
+    INCLUDING INDEXES
+) SERVER s0;                                                    -- ERROR
+ERROR:  ERROR: foreign tables do not support LIKE INCLUDING INDEXES
+CREATE FOREIGN TABLE ft2 (
+    LIKE pg_catalog.pg_enum
+    INCLUDING STORAGE
+) SERVER s0;                                                    -- ERROR
+ERROR:  ERROR: foreign tables do not support LIKE INCLUDING STORAGE
 -- ALTER FOREIGN TABLE
 COMMENT ON FOREIGN TABLE ft1 IS 'foreign table';
 COMMENT ON FOREIGN TABLE ft1 IS NULL;
diff --git a/src/test/regress/sql/foreign_data.sql 
b/src/test/regress/sql/foreign_data.sql
index f819eb1..f36a10c 100644
--- a/src/test/regress/sql/foreign_data.sql
+++ b/src/test/regress/sql/foreign_data.sql
@@ -280,6 +280,18 @@ COMMENT ON COLUMN ft1.c1 IS 'ft1.c1';
 CREATE INDEX id_ft1_c2 ON ft1 (c2);                             -- ERROR
 SELECT * FROM ft1;                                              -- ERROR
 EXPLAIN SELECT * FROM ft1;                                      -- ERROR
+CREATE FOREIGN TABLE ft2 (
+    LIKE pg_catalog.pg_enum
+    INCLUDING CONSTRAINTS
+) SERVER s0;                                                    -- ERROR
+CREATE FOREIGN TABLE ft2 (
+    LIKE pg_catalog.pg_enum
+    INCLUDING INDEXES
+) SERVER s0;                                                    -- ERROR
+CREATE FOREIGN TABLE ft2 (
+    LIKE pg_catalog.pg_enum
+    INCLUDING STORAGE
+) SERVER s0;                                                    -- ERROR
 
 -- ALTER FOREIGN TABLE
 COMMENT ON FOREIGN TABLE ft1 IS 'foreign table';
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to