1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-13 07:41:39 +03:00

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"
This commit is contained in:
Tom Lane
2009-12-23 02:35:25 +00:00
parent b7d6795445
commit cfc5008a51
18 changed files with 277 additions and 104 deletions

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/parser/parse_target.c,v 1.174 2009/10/31 01:41:31 tgl Exp $
* $PostgreSQL: pgsql/src/backend/parser/parse_target.c,v 1.175 2009/12/23 02:35:22 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@ -1410,13 +1410,40 @@ FigureColname(Node *node)
{
char *name = NULL;
FigureColnameInternal(node, &name);
(void) FigureColnameInternal(node, &name);
if (name != NULL)
return name;
/* default result if we can't guess anything */
return "?column?";
}
/*
* FigureIndexColname -
* choose the name for an expression column in an index
*
* This is actually just like FigureColname, except we return NULL if
* we can't pick a good name.
*/
char *
FigureIndexColname(Node *node)
{
char *name = NULL;
(void) FigureColnameInternal(node, &name);
return name;
}
/*
* FigureColnameInternal -
* internal workhorse for FigureColname
*
* Return value indicates strength of confidence in result:
* 0 - no information
* 1 - second-best name choice
* 2 - good name choice
* The return value is actually only used internally.
* If the result isn't zero, *name is set to the chosen name.
*/
static int
FigureColnameInternal(Node *node, char **name)
{