]> granicus.if.org Git - postgresql/commitdiff
Allow the index name to be omitted in CREATE INDEX, causing the system to
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 23 Dec 2009 17:41:45 +0000 (17:41 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 23 Dec 2009 17:41:45 +0000 (17:41 +0000)
choose an index name the same as it would do for an unnamed index constraint.
(My recent changes to the index naming logic have helped to ensure that this
will be a reasonable choice.)  Per a suggestion from Peter.

A necessary side-effect is to promote CONCURRENTLY to type_func_name_keyword
status, ie, it can't be a table/column/index name anymore unless quoted.
This is not all bad, since we have heard more than once of people typing
CREATE INDEX CONCURRENTLY ON foo (...) and getting a normal index build of
an index named "concurrently", which was not what they wanted.  Now this
syntax will result in a concurrent build of an index with system-chosen
name; which they can rename afterwards if they want something else.

doc/src/sgml/keywords.sgml
doc/src/sgml/ref/create_index.sgml
src/backend/parser/gram.y
src/include/parser/kwlist.h
src/test/regress/expected/create_index.out
src/test/regress/sql/create_index.sql

index 4e8b7d75023d8c34580111b3cbcc29887f9b4301..deb72e79bc81d3bf42d4aa5af4d36ba1d087da95 100644 (file)
@@ -1,4 +1,4 @@
-<!-- $PostgreSQL: pgsql/doc/src/sgml/keywords.sgml,v 2.27 2009/11/05 23:24:22 tgl Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/keywords.sgml,v 2.28 2009/12/23 17:41:43 tgl Exp $ -->
 
 <appendix id="sql-keywords-appendix">
  <title><acronym>SQL</acronym> Key Words</title>
    </row>
    <row>
     <entry><token>CONCURRENTLY</token></entry>
-    <entry>non-reserved</entry>
+    <entry>reserved (can be function or type)</entry>
     <entry></entry>
     <entry></entry>
     <entry></entry>
index 49352accae002cf6789cf198a81cd914b021028b..1966cb9daeb4d85f4b1196082055cb7c30302b21 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/create_index.sgml,v 1.71 2009/03/24 20:17:08 tgl Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/create_index.sgml,v 1.72 2009/12/23 17:41:43 tgl Exp $
 PostgreSQL documentation
 -->
 
@@ -21,7 +21,7 @@ PostgreSQL documentation
 
  <refsynopsisdiv>
 <synopsis>
-CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] <replaceable class="parameter">name</replaceable> ON <replaceable class="parameter">table</replaceable> [ USING <replaceable class="parameter">method</replaceable> ]
+CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ <replaceable class="parameter">name</replaceable> ] ON <replaceable class="parameter">table</replaceable> [ USING <replaceable class="parameter">method</replaceable> ]
     ( { <replaceable class="parameter">column</replaceable> | ( <replaceable class="parameter">expression</replaceable> ) } [ <replaceable class="parameter">opclass</replaceable> ] [ ASC | DESC ] [ NULLS { FIRST | LAST } ] [, ...] )
     [ WITH ( <replaceable class="PARAMETER">storage_parameter</replaceable> = <replaceable class="PARAMETER">value</replaceable> [, ... ] ) ]
     [ TABLESPACE <replaceable class="parameter">tablespace</replaceable> ]
@@ -33,8 +33,8 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] <replaceable class="parameter">name</re
   <title>Description</title>
 
   <para>
-   <command>CREATE INDEX</command> constructs an index named <replaceable
-   class="parameter">name</replaceable> on the specified table.
+   <command>CREATE INDEX</command> constructs an index
+   on the specified column(s) of the specified table.
    Indexes are primarily used to enhance database performance (though
    inappropriate use can result in slower performance).
   </para>
@@ -132,7 +132,9 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] <replaceable class="parameter">name</re
        <para>
         The name of the index to be created.  No schema name can be included
         here; the index is always created in the same schema as its parent
-        table.
+        table.  If the name is omitted, <productname>PostgreSQL</> chooses a
+        suitable name based on the parent table's name and the indexed column
+        name(s).
        </para>
       </listitem>
      </varlistentry>
@@ -514,8 +516,10 @@ CREATE UNIQUE INDEX title_idx ON films (title);
    To create an index on the expression <literal>lower(title)</>,
    allowing efficient case-insensitive searches:
 <programlisting>
-CREATE INDEX lower_title_idx ON films ((lower(title)));
+CREATE INDEX ON films ((lower(title)));
 </programlisting>
+   (In this example we have chosen to omit the index name, so the system
+   will choose a name, typically <literal>films_lower_idx</>.)
   </para>
 
   <para>
@@ -544,7 +548,7 @@ CREATE INDEX gin_idx ON documents_table (locations) WITH (fastupdate = off);
    <literal>films</> and have the index reside in the tablespace
    <literal>indexspace</>:
 <programlisting>
-CREATE INDEX code_idx ON films(code) TABLESPACE indexspace;
+CREATE INDEX code_idx ON films (code) TABLESPACE indexspace;
 </programlisting>
   </para>
 
index 7ff46e05cb1c654bf39a2d8fc27dc789f3cebf12..30663d578ea7b537cf28926f6f2260f9fc2db05c 100644 (file)
@@ -11,7 +11,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.698 2009/12/23 02:35:22 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.699 2009/12/23 17:41:43 tgl Exp $
  *
  * HISTORY
  *       AUTHOR                        DATE                    MAJOR EVENT
@@ -253,7 +253,8 @@ static TypeName *TableFuncTypeName(List *columns);
 
 %type <str>            copy_file_name
                                database_name access_method_clause access_method attr_name
-                               index_name name cursor_name file_name cluster_index_specification
+                               name cursor_name file_name
+                               index_name opt_index_name cluster_index_specification
 
 %type <list>   func_name handler_name qual_Op qual_all_Op subquery_Op
                                opt_class opt_inline_handler opt_validator validator_clause
@@ -325,7 +326,7 @@ static TypeName *TableFuncTypeName(List *columns);
 %type <node>   overlay_placing substr_from substr_for
 
 %type <boolean> opt_instead
-%type <boolean> index_opt_unique opt_verbose opt_full
+%type <boolean> opt_unique opt_concurrently opt_verbose opt_full
 %type <boolean> opt_freeze opt_default opt_recheck
 %type <defelt> opt_binary opt_oids copy_delimiter
 
@@ -4822,36 +4823,17 @@ defacl_privilege_target:
  *
  *             QUERY: CREATE INDEX
  *
- * Note: we can't factor CONCURRENTLY into a separate production without
- * making it a reserved word.
- *
  * Note: we cannot put TABLESPACE clause after WHERE clause unless we are
  * willing to make TABLESPACE a fully reserved word.
  *****************************************************************************/
 
-IndexStmt:     CREATE index_opt_unique INDEX index_name
-                       ON qualified_name access_method_clause '(' index_params ')'
-                       opt_reloptions OptTableSpace where_clause
-                               {
-                                       IndexStmt *n = makeNode(IndexStmt);
-                                       n->unique = $2;
-                                       n->concurrent = false;
-                                       n->idxname = $4;
-                                       n->relation = $6;
-                                       n->accessMethod = $7;
-                                       n->indexParams = $9;
-                                       n->options = $11;
-                                       n->tableSpace = $12;
-                                       n->whereClause = $13;
-                                       $$ = (Node *)n;
-                               }
-                       | CREATE index_opt_unique INDEX CONCURRENTLY index_name
+IndexStmt:     CREATE opt_unique INDEX opt_concurrently opt_index_name
                        ON qualified_name access_method_clause '(' index_params ')'
                        opt_reloptions OptTableSpace where_clause
                                {
                                        IndexStmt *n = makeNode(IndexStmt);
                                        n->unique = $2;
-                                       n->concurrent = true;
+                                       n->concurrent = $4;
                                        n->idxname = $5;
                                        n->relation = $7;
                                        n->accessMethod = $8;
@@ -4863,11 +4845,21 @@ IndexStmt:      CREATE index_opt_unique INDEX index_name
                                }
                ;
 
-index_opt_unique:
+opt_unique:
                        UNIQUE                                                                  { $$ = TRUE; }
                        | /*EMPTY*/                                                             { $$ = FALSE; }
                ;
 
+opt_concurrently:
+                       CONCURRENTLY                                                    { $$ = TRUE; }
+                       | /*EMPTY*/                                                             { $$ = FALSE; }
+               ;
+
+opt_index_name:
+                       index_name                                                              { $$ = $1; }
+                       | /*EMPTY*/                                                             { $$ = NULL; }
+               ;
+
 access_method_clause:
                        USING access_method                                             { $$ = $2; }
                        | /*EMPTY*/                                                             { $$ = DEFAULT_INDEX_TYPE; }
@@ -10696,7 +10688,6 @@ unreserved_keyword:
                        | COMMENTS
                        | COMMIT
                        | COMMITTED
-                       | CONCURRENTLY
                        | CONFIGURATION
                        | CONNECTION
                        | CONSTRAINTS
@@ -10988,6 +10979,7 @@ type_func_name_keyword:
                          AUTHORIZATION
                        | BETWEEN
                        | BINARY
+                       | CONCURRENTLY
                        | CROSS
                        | CURRENT_SCHEMA
                        | FREEZE
index f07057c430644341c7d6cbf2d935cee6b2a5f589..d4c30b1e56159541f34ce34c2967d7ed28a1509d 100644 (file)
@@ -11,7 +11,7 @@
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/include/parser/kwlist.h,v 1.7 2009/12/07 05:22:23 tgl Exp $
+ *       $PostgreSQL: pgsql/src/include/parser/kwlist.h,v 1.8 2009/12/23 17:41:44 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -83,7 +83,7 @@ PG_KEYWORD("comment", COMMENT, UNRESERVED_KEYWORD)
 PG_KEYWORD("comments", COMMENTS, UNRESERVED_KEYWORD)
 PG_KEYWORD("commit", COMMIT, UNRESERVED_KEYWORD)
 PG_KEYWORD("committed", COMMITTED, UNRESERVED_KEYWORD)
-PG_KEYWORD("concurrently", CONCURRENTLY, UNRESERVED_KEYWORD)
+PG_KEYWORD("concurrently", CONCURRENTLY, TYPE_FUNC_NAME_KEYWORD)
 PG_KEYWORD("configuration", CONFIGURATION, UNRESERVED_KEYWORD)
 PG_KEYWORD("connection", CONNECTION, UNRESERVED_KEYWORD)
 PG_KEYWORD("constraint", CONSTRAINT, RESERVED_KEYWORD)
index 079a64098c5c17de5d7e7563e8d0acf359acaa1c..9dd54b39f0989ec3c6a363fac0333fdb927e3655 100644 (file)
@@ -689,7 +689,8 @@ DETAIL:  Key (f2)=(b) is duplicated.
 -- test that expression indexes and partial indexes work concurrently
 CREATE INDEX CONCURRENTLY concur_index4 on concur_heap(f2) WHERE f1='a';
 CREATE INDEX CONCURRENTLY concur_index5 on concur_heap(f2) WHERE f1='x';
-CREATE INDEX CONCURRENTLY concur_index6 on concur_heap((f2||f1));
+-- here we also check that you can default the index name
+CREATE INDEX CONCURRENTLY on concur_heap((f2||f1));
 -- You can't do a concurrent index build in a transaction
 BEGIN;
 CREATE INDEX CONCURRENTLY concur_index7 ON concur_heap(f1);
@@ -711,10 +712,10 @@ Table "public.concur_heap"
 Indexes:
     "concur_index2" UNIQUE, btree (f1)
     "concur_index3" UNIQUE, btree (f2) INVALID
+    "concur_heap_expr_idx" btree ((f2 || f1))
     "concur_index1" btree (f2, f1)
     "concur_index4" btree (f2) WHERE f1 = 'a'::text
     "concur_index5" btree (f2) WHERE f1 = 'x'::text
-    "concur_index6" btree ((f2 || f1))
     "std_index" btree (f2)
 
 DROP TABLE concur_heap;
index 9527ab7a7bff767d38f93d8bafdaa422aa13eba8..b205afa34f7455cda710b8e3616a749a60d95deb 100644 (file)
@@ -300,7 +300,8 @@ CREATE UNIQUE INDEX CONCURRENTLY concur_index3 ON concur_heap(f2);
 -- test that expression indexes and partial indexes work concurrently
 CREATE INDEX CONCURRENTLY concur_index4 on concur_heap(f2) WHERE f1='a';
 CREATE INDEX CONCURRENTLY concur_index5 on concur_heap(f2) WHERE f1='x';
-CREATE INDEX CONCURRENTLY concur_index6 on concur_heap((f2||f1));
+-- here we also check that you can default the index name
+CREATE INDEX CONCURRENTLY on concur_heap((f2||f1));
 
 -- You can't do a concurrent index build in a transaction
 BEGIN;