Skip to content

Commit 5220562

Browse files
committed
Improve documentation about CREATE TABLE ... LIKE.
The docs failed to explain that LIKE INCLUDING INDEXES would not preserve the names of indexes and associated constraints. Also, it wasn't mentioned that EXCLUDE constraints would be copied by this option. The latter oversight seems enough of a documentation bug to justify back-patching. In passing, do some minor copy-editing in the same area, and add an entry for LIKE under "Compatibility", since it's not exactly a faithful implementation of the standard's feature. Discussion: <20160728151154.AABE64016B@smtp.hushmail.com>
1 parent 1be0387 commit 5220562

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

doc/src/sgml/ref/create_table.sgml

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -328,48 +328,60 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXI
328328
table.
329329
</para>
330330
<para>
331-
Default expressions for the copied column definitions will only be
332-
copied if <literal>INCLUDING DEFAULTS</literal> is specified. The
331+
Default expressions for the copied column definitions will be copied
332+
only if <literal>INCLUDING DEFAULTS</literal> is specified. The
333333
default behavior is to exclude default expressions, resulting in the
334334
copied columns in the new table having null defaults.
335+
Note that copying defaults that call database-modification functions,
336+
such as <function>nextval</>, may create a functional linkage between
337+
the original and new tables.
335338
</para>
336339
<para>
337340
Not-null constraints are always copied to the new table.
338341
<literal>CHECK</literal> constraints will be copied only if
339342
<literal>INCLUDING CONSTRAINTS</literal> is specified.
340-
Indexes, <literal>PRIMARY KEY</>, and <literal>UNIQUE</> constraints
341-
on the original table will be created on the new table only if the
342-
<literal>INCLUDING INDEXES</literal> clause is specified.
343343
No distinction is made between column constraints and table
344344
constraints.
345345
</para>
346-
<para><literal>STORAGE</> settings for the copied column definitions will only
347-
be copied if <literal>INCLUDING STORAGE</literal> is specified. The
346+
<para>
347+
Indexes, <literal>PRIMARY KEY</>, <literal>UNIQUE</>,
348+
and <literal>EXCLUDE</> constraints on the original table will be
349+
created on the new table only if <literal>INCLUDING INDEXES</literal>
350+
is specified. Names for the new indexes and constraints are
351+
chosen according to the default rules, regardless of how the originals
352+
were named. (This behavior avoids possible duplicate-name failures for
353+
the new indexes.)
354+
</para>
355+
<para>
356+
<literal>STORAGE</> settings for the copied column definitions will be
357+
copied only if <literal>INCLUDING STORAGE</literal> is specified. The
348358
default behavior is to exclude <literal>STORAGE</> settings, resulting
349359
in the copied columns in the new table having type-specific default
350360
settings. For more on <literal>STORAGE</> settings, see
351361
<xref linkend="storage-toast">.
352362
</para>
353363
<para>
354364
Comments for the copied columns, constraints, and indexes
355-
will only be copied if <literal>INCLUDING COMMENTS</literal>
365+
will be copied only if <literal>INCLUDING COMMENTS</literal>
356366
is specified. The default behavior is to exclude comments, resulting in
357367
the copied columns and constraints in the new table having no comments.
358368
</para>
359-
<para><literal>INCLUDING ALL</literal> is an abbreviated form of
369+
<para>
370+
<literal>INCLUDING ALL</literal> is an abbreviated form of
360371
<literal>INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES INCLUDING STORAGE INCLUDING COMMENTS</literal>.
361372
</para>
362373
<para>
363-
Note also that unlike <literal>INHERITS</literal>, columns and
374+
Note that unlike <literal>INHERITS</literal>, columns and
364375
constraints copied by <literal>LIKE</> are not merged with similarly
365376
named columns and constraints.
366377
If the same name is specified explicitly or in another
367378
<literal>LIKE</literal> clause, an error is signaled.
368379
</para>
369380
<para>
370-
The <literal>LIKE</literal> clause can also be used to copy columns from
371-
views, foreign tables, or composite types. Inapplicable options (e.g., <literal>INCLUDING
372-
INDEXES</literal> from a view) are ignored.
381+
The <literal>LIKE</literal> clause can also be used to copy column
382+
definitions from views, foreign tables, or composite types.
383+
Inapplicable options (e.g., <literal>INCLUDING INDEXES</literal> from
384+
a view) are ignored.
373385
</para>
374386
</listitem>
375387
</varlistentry>
@@ -1444,6 +1456,17 @@ CREATE TABLE employees OF employee_type (
14441456
</para>
14451457
</refsect2>
14461458

1459+
<refsect2>
1460+
<title><literal>LIKE</> Clause</title>
1461+
1462+
<para>
1463+
While a <literal>LIKE</> clause exists in the SQL standard, many of the
1464+
options that <productname>PostgreSQL</productname> accepts for it are not
1465+
in the standard, and some of the standard's options are not implemented
1466+
by <productname>PostgreSQL</productname>.
1467+
</para>
1468+
</refsect2>
1469+
14471470
<refsect2>
14481471
<title><literal>WITH</> Clause</title>
14491472

src/backend/parser/parse_utilcmd.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,9 @@ generateClonedIndexStmt(CreateStmtContext *cxt, Relation source_idx,
10561056

10571057
/*
10581058
* We don't try to preserve the name of the source index; instead, just
1059-
* let DefineIndex() choose a reasonable name.
1059+
* let DefineIndex() choose a reasonable name. (If we tried to preserve
1060+
* the name, we'd get duplicate-relation-name failures unless the source
1061+
* table was in a different schema.)
10601062
*/
10611063
index->idxname = NULL;
10621064

0 commit comments

Comments
 (0)