Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4747749

Browse files
author
Neil Conway
committed
Implement CREATE TABLE LIKE ... INCLUDING INDEXES. Patch from NikhilS,
based in part on an earlier patch from Trevor Hardcastle, and reviewed by myself.
1 parent 77d27e4 commit 4747749

File tree

15 files changed

+566
-190
lines changed

15 files changed

+566
-190
lines changed

doc/src/sgml/ref/create_table.sgml

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/create_table.sgml,v 1.108 2007/06/03 17:06:03 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/create_table.sgml,v 1.109 2007/07/17 05:02:00 neilc Exp $
33
PostgreSQL documentation
44
-->
55

@@ -23,7 +23,7 @@ PostgreSQL documentation
2323
CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE <replaceable class="PARAMETER">table_name</replaceable> ( [
2424
{ <replaceable class="PARAMETER">column_name</replaceable> <replaceable class="PARAMETER">data_type</replaceable> [ DEFAULT <replaceable>default_expr</> ] [ <replaceable class="PARAMETER">column_constraint</replaceable> [ ... ] ]
2525
| <replaceable>table_constraint</replaceable>
26-
| LIKE <replaceable>parent_table</replaceable> [ { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS } ] ... }
26+
| LIKE <replaceable>parent_table</replaceable> [ { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS | INDEXES } ] ... }
2727
[, ... ]
2828
] )
2929
[ INHERITS ( <replaceable>parent_table</replaceable> [, ... ] ) ]
@@ -237,7 +237,7 @@ and <replaceable class="PARAMETER">table_constraint</replaceable> is:
237237
</varlistentry>
238238

239239
<varlistentry>
240-
<term><literal>LIKE <replaceable>parent_table</replaceable> [ { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS } ]</literal></term>
240+
<term><literal>LIKE <replaceable>parent_table</replaceable> [ { INCLUDING | EXCLUDING } { DEFAULTS | CONSTRAINTS | INDEXES } ]</literal></term>
241241
<listitem>
242242
<para>
243243
The <literal>LIKE</literal> clause specifies a table from which
@@ -265,11 +265,16 @@ and <replaceable class="PARAMETER">table_constraint</replaceable> is:
265265
column constraints and table constraints &mdash; when constraints are
266266
requested, all check constraints are copied.
267267
</para>
268+
<para>
269+
Any indexes on the original table will not be created on the new
270+
table, unless the <literal>INCLUDING INDEXES</literal> clause is
271+
specified.
272+
</para>
268273
<para>
269274
Note also that unlike <literal>INHERITS</literal>, copied columns and
270275
constraints are not merged with similarly named columns and constraints.
271276
If the same name is specified explicitly or in another
272-
<literal>LIKE</literal> clause an error is signalled.
277+
<literal>LIKE</literal> clause, an error is signalled.
273278
</para>
274279
</listitem>
275280
</varlistentry>

src/backend/bootstrap/bootparse.y

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/bootstrap/bootparse.y,v 1.88 2007/03/13 00:33:39 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/bootstrap/bootparse.y,v 1.89 2007/07/17 05:02:00 neilc Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -252,7 +252,7 @@ Boot_DeclareIndexStmt:
252252
LexIDStr($8),
253253
NULL,
254254
$10,
255-
NULL, NIL,
255+
NULL, NIL, NULL,
256256
false, false, false,
257257
false, false, true, false, false);
258258
do_end();
@@ -270,7 +270,7 @@ Boot_DeclareUniqueIndexStmt:
270270
LexIDStr($9),
271271
NULL,
272272
$11,
273-
NULL, NIL,
273+
NULL, NIL, NULL,
274274
true, false, false,
275275
false, false, true, false, false);
276276
do_end();

src/backend/commands/indexcmds.c

+14-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.160 2007/06/23 22:12:50 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.161 2007/07/17 05:02:00 neilc Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -79,6 +79,8 @@ static bool relationHasPrimaryKey(Relation rel);
7979
* to index on.
8080
* 'predicate': the partial-index condition, or NULL if none.
8181
* 'options': reloptions from WITH (in list-of-DefElem form).
82+
* 'src_options': reloptions from the source index, if this is a cloned
83+
* index produced by CREATE TABLE LIKE ... INCLUDING INDEXES
8284
* 'unique': make the index enforce uniqueness.
8385
* 'primary': mark the index as a primary key in the catalogs.
8486
* 'isconstraint': index is for a PRIMARY KEY or UNIQUE constraint,
@@ -100,6 +102,7 @@ DefineIndex(RangeVar *heapRelation,
100102
List *attributeList,
101103
Expr *predicate,
102104
List *options,
105+
char *src_options,
103106
bool unique,
104107
bool primary,
105108
bool isconstraint,
@@ -392,9 +395,17 @@ DefineIndex(RangeVar *heapRelation,
392395
}
393396

394397
/*
395-
* Parse AM-specific options, convert to text array form, validate.
398+
* Parse AM-specific options, convert to text array form,
399+
* validate. The src_options introduced due to using indexes
400+
* via the "CREATE LIKE INCLUDING INDEXES" statement also need to
401+
* be merged here
396402
*/
397-
reloptions = transformRelOptions((Datum) 0, options, false, false);
403+
if (src_options)
404+
reloptions = unflatten_reloptions(src_options);
405+
else
406+
reloptions = (Datum) 0;
407+
408+
reloptions = transformRelOptions(reloptions, options, false, false);
398409

399410
(void) index_reloptions(amoptions, reloptions, true);
400411

src/backend/commands/tablecmds.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.229 2007/07/03 01:30:36 neilc Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.230 2007/07/17 05:02:00 neilc Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -3794,6 +3794,7 @@ ATExecAddIndex(AlteredTableInfo *tab, Relation rel,
37943794
stmt->indexParams, /* parameters */
37953795
(Expr *) stmt->whereClause,
37963796
stmt->options,
3797+
stmt->src_options,
37973798
stmt->unique,
37983799
stmt->primary,
37993800
stmt->isconstraint,

src/backend/nodes/copyfuncs.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Portions Copyright (c) 1994, Regents of the University of California
1616
*
1717
* IDENTIFICATION
18-
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.379 2007/06/11 22:22:40 tgl Exp $
18+
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.380 2007/07/17 05:02:01 neilc Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -2192,6 +2192,7 @@ _copyIndexStmt(IndexStmt *from)
21922192
COPY_STRING_FIELD(tableSpace);
21932193
COPY_NODE_FIELD(indexParams);
21942194
COPY_NODE_FIELD(options);
2195+
COPY_STRING_FIELD(src_options);
21952196
COPY_NODE_FIELD(whereClause);
21962197
COPY_SCALAR_FIELD(unique);
21972198
COPY_SCALAR_FIELD(primary);

src/backend/nodes/equalfuncs.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Portions Copyright (c) 1994, Regents of the University of California
1919
*
2020
* IDENTIFICATION
21-
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.310 2007/06/11 22:22:40 tgl Exp $
21+
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.311 2007/07/17 05:02:01 neilc Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -1044,6 +1044,7 @@ _equalIndexStmt(IndexStmt *a, IndexStmt *b)
10441044
COMPARE_STRING_FIELD(tableSpace);
10451045
COMPARE_NODE_FIELD(indexParams);
10461046
COMPARE_NODE_FIELD(options);
1047+
COMPARE_STRING_FIELD(src_options);
10471048
COMPARE_NODE_FIELD(whereClause);
10481049
COMPARE_SCALAR_FIELD(unique);
10491050
COMPARE_SCALAR_FIELD(primary);

src/backend/nodes/outfuncs.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/nodes/outfuncs.c,v 1.312 2007/07/17 01:21:43 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/nodes/outfuncs.c,v 1.313 2007/07/17 05:02:01 neilc Exp $
1212
*
1313
* NOTES
1414
* Every node type that can appear in stored rules' parsetrees *must*
@@ -1541,6 +1541,7 @@ _outIndexStmt(StringInfo str, IndexStmt *node)
15411541
WRITE_STRING_FIELD(tableSpace);
15421542
WRITE_NODE_FIELD(indexParams);
15431543
WRITE_NODE_FIELD(options);
1544+
WRITE_STRING_FIELD(src_options);
15441545
WRITE_NODE_FIELD(whereClause);
15451546
WRITE_BOOL_FIELD(unique);
15461547
WRITE_BOOL_FIELD(primary);

0 commit comments

Comments
 (0)