diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2009-12-23 02:35:25 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2009-12-23 02:35:25 +0000 |
commit | cfc5008a51f4f1e83e9209071882ace91abe1f3f (patch) | |
tree | 4ed533202faa4ce1c86d5f6908811c4d65d6ed0a /src/backend/parser/parse_utilcmd.c | |
parent | b7d67954456f15762c04e5269b64adc88dcd0860 (diff) | |
download | postgresql-cfc5008a51f4f1e83e9209071882ace91abe1f3f.tar.gz postgresql-cfc5008a51f4f1e83e9209071882ace91abe1f3f.zip |
Adjust naming of indexes and their columns per recent discussion.
Index expression columns are now named after the FigureColname result for
their expressions, rather than always being "pg_expression_N". Digits are
appended to this name if needed to make the column name unique within the
index. (That happens for regular columns too, thus fixing the old problem
that CREATE INDEX fooi ON foo (f1, f1) fails. Before exclusion indexes
there was no real reason to do such a thing, but now maybe there is.)
Default names for indexes and associated constraints now include the column
names of all their columns, not only the first one as in previous practice.
(Of course, this will be truncated as needed to fit in NAMEDATALEN. Also,
pkey indexes retain the historical behavior of not naming specific columns
at all.)
An example of the results:
regression=# create table foo (f1 int, f2 text,
regression(# exclude (f1 with =, lower(f2) with =));
NOTICE: CREATE TABLE / EXCLUDE will create implicit index "foo_f1_lower_exclusion" for table "foo"
CREATE TABLE
regression=# \d foo_f1_lower_exclusion
Index "public.foo_f1_lower_exclusion"
Column | Type | Definition
--------+---------+------------
f1 | integer | f1
lower | text | lower(f2)
btree, for table "public.foo"
Diffstat (limited to 'src/backend/parser/parse_utilcmd.c')
-rw-r--r-- | src/backend/parser/parse_utilcmd.c | 47 |
1 files changed, 24 insertions, 23 deletions
diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index 75c8d863dcd..f09e78dd8e3 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -19,7 +19,7 @@ * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/backend/parser/parse_utilcmd.c,v 2.34 2009/12/22 23:54:17 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/parser/parse_utilcmd.c,v 2.35 2009/12/23 02:35:22 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -48,6 +48,7 @@ #include "parser/parse_clause.h" #include "parser/parse_expr.h" #include "parser/parse_relation.h" +#include "parser/parse_target.h" #include "parser/parse_type.h" #include "parser/parse_utilcmd.h" #include "parser/parser.h" @@ -789,34 +790,24 @@ transformInhRelation(ParseState *pstate, CreateStmtContext *cxt, /* * chooseIndexName * - * Set name for unnamed index. See also the same logic in DefineIndex. + * Compute name for an index. This must match code in indexcmds.c. + * + * XXX this is inherently broken because the indexes aren't created + * immediately, so we fail to resolve conflicts when the same name is + * derived for multiple indexes. However, that's a reasonably uncommon + * situation, so we'll live with it for now. */ static char * chooseIndexName(const RangeVar *relation, IndexStmt *index_stmt) { - Oid namespaceId; + Oid namespaceId; + List *colnames; namespaceId = RangeVarGetCreationNamespace(relation); - if (index_stmt->primary) - { - /* no need for column list with pkey */ - return ChooseRelationName(relation->relname, NULL, - "pkey", namespaceId); - } - else if (index_stmt->excludeOpNames != NIL) - { - IndexElem *iparam = (IndexElem *) linitial(index_stmt->indexParams); - - return ChooseRelationName(relation->relname, iparam->name, - "exclusion", namespaceId); - } - else - { - IndexElem *iparam = (IndexElem *) linitial(index_stmt->indexParams); - - return ChooseRelationName(relation->relname, iparam->name, - "key", namespaceId); - } + colnames = ChooseIndexColumnNames(index_stmt->indexParams); + return ChooseIndexName(relation->relname, namespaceId, + colnames, index_stmt->excludeOpNames, + index_stmt->primary, index_stmt->isconstraint); } /* @@ -828,6 +819,7 @@ generateClonedIndexStmt(CreateStmtContext *cxt, Relation source_idx, AttrNumber *attmap) { Oid source_relid = RelationGetRelid(source_idx); + Form_pg_attribute *attrs = RelationGetDescr(source_idx)->attrs; HeapTuple ht_idxrel; HeapTuple ht_idx; Form_pg_class idxrelrec; @@ -1023,6 +1015,9 @@ generateClonedIndexStmt(CreateStmtContext *cxt, Relation source_idx, keycoltype = exprType(indexkey); } + /* Copy the original index column name */ + iparam->indexcolname = pstrdup(NameStr(attrs[keyno]->attname)); + /* Add the operator class name, if non-default */ iparam->opclass = get_opclass(indclass->values[keyno], keycoltype); @@ -1416,6 +1411,7 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt) iparam = makeNode(IndexElem); iparam->name = pstrdup(key); iparam->expr = NULL; + iparam->indexcolname = NULL; iparam->opclass = NIL; iparam->ordering = SORTBY_DEFAULT; iparam->nulls_ordering = SORTBY_NULLS_DEFAULT; @@ -1544,6 +1540,11 @@ transformIndexStmt(IndexStmt *stmt, const char *queryString) if (ielem->expr) { + /* Extract preliminary index col name before transforming expr */ + if (ielem->indexcolname == NULL) + ielem->indexcolname = FigureIndexColname(ielem->expr); + + /* Now do parse transformation of the expression */ ielem->expr = transformExpr(pstate, ielem->expr); /* |