creating index names automatically?

Started by Peter Eisentrautover 16 years ago35 messageshackers
Jump to latest
#1Peter Eisentraut
peter_e@gmx.net

Could we create an option to create index names automatically, so you'd
only have to write

CREATE INDEX ON foo (a);

which would pick a name like foo_a_idx. We already do this in a number
of places such as constraint names and sequences without much trouble.
In most cases you don't really need to give an index a smart name since
the purpose is obvious.

Comments?

#2Brendan Jurd
direvus@gmail.com
In reply to: Peter Eisentraut (#1)
Re: creating index names automatically?

2009/12/20 Peter Eisentraut <peter_e@gmx.net>:

Could we create an option to create index names automatically, so you'd
only have to write

CREATE INDEX ON foo (a);

Yes, please.

Cheers,
BJ

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Eisentraut (#1)
Re: creating index names automatically?

Peter Eisentraut <peter_e@gmx.net> writes:

Could we create an option to create index names automatically, so you'd
only have to write

CREATE INDEX ON foo (a);

which would pick a name like foo_a_idx. We already do this in a number
of places such as constraint names and sequences without much trouble.
In most cases you don't really need to give an index a smart name since
the purpose is obvious.

In the cases where that's sensible, you can use constraint syntax, no?

I really doubt that it's that easy to pick a sensible name for an index
on an expression, for example.

regards, tom lane

#4A. Kretschmer
andreas.kretschmer@schollglas.com
In reply to: Peter Eisentraut (#1)
Re: creating index names automatically?

In response to Peter Eisentraut :

Could we create an option to create index names automatically, so you'd
only have to write

CREATE INDEX ON foo (a);

which would pick a name like foo_a_idx. We already do this in a number
of places such as constraint names and sequences without much trouble.
In most cases you don't really need to give an index a smart name since
the purpose is obvious.

Comments?

+1, as an additional option, only if no index-name specified.

Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#3)
Re: creating index names automatically?

I wrote:

In the cases where that's sensible, you can use constraint syntax, no?

I really doubt that it's that easy to pick a sensible name for an index
on an expression, for example.

Although, having said that, I realize we just opened that can of worms
with the exclusion-constraint patch:

regression=# create table foo (f1 text, exclude (lower(f1) with =));
NOTICE: CREATE TABLE / EXCLUDE will create implicit index "foo_exclusion" for table "foo"
CREATE TABLE

The above behavior seems to need improvement already.

regards, tom lane

#6Ron Mayer
rm_pg@cheapcomplexdevices.com
In reply to: Peter Eisentraut (#1)
Re: creating index names automatically?

Peter Eisentraut wrote:

Could we create an option to create index names automatically, so you'd
only have to write

CREATE INDEX ON foo (a);

which would pick a name like foo_a_idx.

Why wouldn't it default to a name more like:

CREATE INDEX "foo(a)" on foo(a);

which would extend pretty nicely to things like:

CREATE INDEX "foo USING GIN(hstore)" ON foo USING GIN(hstore);'

Seems to be both more readable and less chance for arbitrary
collisions if I have column names with underscores. Otherwise
what would the rule distinguishing "create index on foo(a_b)"
from "create index on foo(a,b)", etc.

#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#5)
Re: creating index names automatically?

I wrote:

Although, having said that, I realize we just opened that can of worms
with the exclusion-constraint patch:

regression=# create table foo (f1 text, exclude (lower(f1) with =));
NOTICE: CREATE TABLE / EXCLUDE will create implicit index "foo_exclusion" for table "foo"
CREATE TABLE

The above behavior seems to need improvement already.

And poking further, CREATE TABLE LIKE INCLUDING INDEXES is another place
where we've already bought into automatically generating index names for
arbitrary non-constraint indexes. And it's even dumber --- you get
names involving "_key" for indexes that aren't even unique. So it seems
like we already have a bit of a problem here.

The first thoughts I have about this are:

* Use FigureColname to derive a name for an expression column, except
I'd be inclined to have the fallback case be "expr" not "?column?".

* Append "_index" not "_key" if it's not a constraint-related index.

I'm also a bit tempted to propose that we start using FigureColname
for the actual attribute names of expression indexes, instead of the
not terribly helpful "pg_expression_n" convention. In this case we'd
have to append a number if necessary to make the name unique among the
column names of the index.

Comments?

regards, tom lane

#8Michael Glaesemann
grzm@seespotcode.net
In reply to: Tom Lane (#7)
Re: creating index names automatically?

On Dec 20, 2009, at 13:58 , Tom Lane wrote:

* Append "_index" not "_key" if it's not a constraint-related index.

"_idx" instead of "_index" keeps things a bit shorter (and a couple of
keystrokes further from NAMEDATALEN). There's precedent for
abbreviations with automatic naming in Postgres, e.g., "_fkey".

Michael Glaesemann
grzm seespotcode net

#9Tom Lane
tgl@sss.pgh.pa.us
In reply to: Michael Glaesemann (#8)
Re: creating index names automatically?

Michael Glaesemann <grzm@seespotcode.net> writes:

On Dec 20, 2009, at 13:58 , Tom Lane wrote:

* Append "_index" not "_key" if it's not a constraint-related index.

"_idx" instead of "_index" keeps things a bit shorter (and a couple of
keystrokes further from NAMEDATALEN). There's precedent for
abbreviations with automatic naming in Postgres, e.g., "_fkey".

No objection here.

BTW, I'm having second thoughts about the last part of my proposal:

I'm also a bit tempted to propose that we start using FigureColname
for the actual attribute names of expression indexes, instead of the
not terribly helpful "pg_expression_n" convention.

The trouble with changing the index attnames for expressions is that it
increases the risk of collisions with attnames for regular index
columns. You can hit that case today:

regression=# create table foo (f1 int, f2 text);
CREATE TABLE
regression=# create index fooi on foo(f1, lower(f2));
CREATE INDEX
regression=# \d fooi
Index "public.fooi"
Column | Type | Definition
-----------------+---------+------------
f1 | integer | f1
pg_expression_2 | text | lower(f2)
btree, for table "public.foo"

regression=# alter table foo rename f1 to pg_expression_2;
ERROR: duplicate key value violates unique constraint "pg_attribute_relid_attnam_index"
DETAIL: Key (attrelid, attname)=(64621, pg_expression_2) already exists.

but it's not exactly probable that someone would name a column
pg_expression_N. The risk goes up quite a lot if we might use simple
names like "abs" or "lower" for expression columns.

We could work around this by being willing to rename index columns on
the fly, but that creates a big risk of failing to dump and reload
comments on index columns, because the columns might not get the same
names in a newer PG version. (I seem to remember having objected to the
whole concept of comments on index columns on the grounds that it would
lock us into the current index column naming scheme, and that's exactly
what it's doing here.)

So I think we're stuck with the current column naming rule, but we do
have wiggle room on the name of the index itself.

regards, tom lane

#10Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#3)
Re: creating index names automatically?

Peter Eisentraut <peter_e@gmx.net> writes:

Could we create an option to create index names automatically, so you'd
only have to write

CREATE INDEX ON foo (a);

which would pick a name like foo_a_idx.

Having done all the groundwork to support that nicely, I find that it
doesn't work because of bison limitations :-(. AFAICT, the only way
we could support this syntax would be to make ON a reserved word.
Or at least more reserved than it is now. We used up all the wiggle
room we had by making CONCURRENTLY non-reserved.

Now ON is reserved according to SQL99, but I'm a bit hesitant to
make it so in our grammar for such a marginal feature as this.
Thoughts?

regards, tom lane

#11Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#9)
Re: creating index names automatically?

I wrote:

The trouble with changing the index attnames for expressions is that it
increases the risk of collisions with attnames for regular index
columns. You can hit that case today:

regression=# create table foo (f1 int, f2 text);
CREATE TABLE
regression=# create index fooi on foo(f1, lower(f2));
CREATE INDEX
regression=# \d fooi
Index "public.fooi"
Column | Type | Definition
-----------------+---------+------------
f1 | integer | f1
pg_expression_2 | text | lower(f2)
btree, for table "public.foo"

regression=# alter table foo rename f1 to pg_expression_2;
ERROR: duplicate key value violates unique constraint "pg_attribute_relid_attnam_index"
DETAIL: Key (attrelid, attname)=(64621, pg_expression_2) already exists.

but it's not exactly probable that someone would name a column
pg_expression_N. The risk goes up quite a lot if we might use simple
names like "abs" or "lower" for expression columns.

It strikes me that the easiest way to deal with this is just to get rid
of the code in renameatt() that tries to rename index columns to agree
with the underlying table columns. That code is not nearly bright
enough to deal with collisions, and furthermore it seems rather
inconsistent to try to rename index columns (which are not very
user-visible in the first place) while not renaming the indexes
themselves (which surely are user-visible). There was some marginal
excuse for doing it back when \d didn't show the index column
definition; but now that it does, I don't think the behavior is worth
expending effort on.

regards, tom lane

#12Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Tom Lane (#10)
Re: creating index names automatically?

Tom Lane wrote:

Peter Eisentraut <peter_e@gmx.net> writes:

Could we create an option to create index names automatically, so you'd
only have to write

CREATE INDEX ON foo (a);

which would pick a name like foo_a_idx.

Having done all the groundwork to support that nicely, I find that it
doesn't work because of bison limitations :-(. AFAICT, the only way
we could support this syntax would be to make ON a reserved word.
Or at least more reserved than it is now. We used up all the wiggle
room we had by making CONCURRENTLY non-reserved.

And here's Simon talking about making CONCURRENTLY more reserved so that
people stop creating indexes named "concurrently" ...

http://database-explorer.blogspot.com/2009/09/create-index-concurrently.html

--
Alvaro Herrera http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.

#13Tom Lane
tgl@sss.pgh.pa.us
In reply to: Alvaro Herrera (#12)
Re: creating index names automatically?

Alvaro Herrera <alvherre@commandprompt.com> writes:

Tom Lane wrote:

... AFAICT, the only way
we could support this syntax would be to make ON a reserved word.
Or at least more reserved than it is now. We used up all the wiggle
room we had by making CONCURRENTLY non-reserved.

And here's Simon talking about making CONCURRENTLY more reserved so that
people stop creating indexes named "concurrently" ...
http://database-explorer.blogspot.com/2009/09/create-index-concurrently.html

Hmm. It would actually work if we made CONCURRENTLY reserved instead;
and that would fix Simon's gripe too. That's kind of weird from a
standards-compliance POV, but in terms of the risk of breaking
applications it might be better than reserving ON.

regards, tom lane

#14Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#13)
Re: creating index names automatically?

I wrote:

Hmm. It would actually work if we made CONCURRENTLY reserved instead;
and that would fix Simon's gripe too. That's kind of weird from a
standards-compliance POV, but in terms of the risk of breaking
applications it might be better than reserving ON.

Wait a minute.  I must have been looking at the wrong keyword list
--- ON already is reserved.  The problem is exactly that it can't
tell whether CREATE INDEX CONCURRENTLY ON ... means to default the
index name or to create an index named CONCURRENTLY.  So really the
*only* way to fix this is to make CONCURRENTLY be at least
type_func_name_keyword.

regards, tom lane

#15David E. Wheeler
david@kineticode.com
In reply to: Tom Lane (#14)
Re: creating index names automatically?

On Dec 22, 2009, at 7:31 PM, Tom Lane wrote:

Wait a minute.  I must have been looking at the wrong keyword list
--- ON already is reserved.  The problem is exactly that it can't
tell whether CREATE INDEX CONCURRENTLY ON ... means to default the
index name or to create an index named CONCURRENTLY.  So really the
*only* way to fix this is to make CONCURRENTLY be at least
type_func_name_keyword.

+1 if it prevents indexes from being named "CONCURRENTLY".

Best,

David

#16Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: David E. Wheeler (#15)
Re: creating index names automatically?

David E. Wheeler wrote:

On Dec 22, 2009, at 7:31 PM, Tom Lane wrote:

Wait a minute.  I must have been looking at the wrong keyword list
--- ON already is reserved.  The problem is exactly that it can't
tell whether CREATE INDEX CONCURRENTLY ON ... means to default the
index name or to create an index named CONCURRENTLY.  So really the
*only* way to fix this is to make CONCURRENTLY be at least
type_func_name_keyword.

+1 if it prevents indexes from being named "CONCURRENTLY".

Yeah, if you really want to have an index named like that you can use
double quotes. Seems a sensible compromise.

--
Alvaro Herrera http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.

#17Tom Lane
tgl@sss.pgh.pa.us
In reply to: Alvaro Herrera (#16)
Re: creating index names automatically?

Alvaro Herrera <alvherre@commandprompt.com> writes:

David E. Wheeler wrote:

+1 if it prevents indexes from being named "CONCURRENTLY".

Yeah, if you really want to have an index named like that you can use
double quotes. Seems a sensible compromise.

Well, this will also break tables and columns named "concurrently".
I think the odds of it being a problem are small, but still it is
a reserved word that shouldn't be reserved according to the SQL spec.

regards, tom lane

#18Bruce Momjian
bruce@momjian.us
In reply to: Tom Lane (#17)
Re: creating index names automatically?

On Wed, Dec 23, 2009 at 3:54 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Alvaro Herrera <alvherre@commandprompt.com> writes:

David E. Wheeler wrote:

+1 if it prevents indexes from being named "CONCURRENTLY".

Yeah, if you really want to have an index named like that you can use
double quotes.  Seems a sensible compromise.

Well, this will also break tables and columns named "concurrently".
I think the odds of it being a problem are small, but still it is
a reserved word that shouldn't be reserved according to the SQL spec.

I suppose we could fix this by specifying a precedence and then
explicitly checking if you're trying to make an index named
concurrently and fixing it up later. Not unlike how you suggested we
avoid making WITH a reserved word with the comment that there was more
than one way to skin a cat

--
greg

#19Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#18)
Re: creating index names automatically?

Greg Stark <gsstark@mit.edu> writes:

On Wed, Dec 23, 2009 at 3:54 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Well, this will also break tables and columns named "concurrently".
I think the odds of it being a problem are small, but still it is
a reserved word that shouldn't be reserved according to the SQL spec.

I suppose we could fix this by specifying a precedence and then
explicitly checking if you're trying to make an index named
concurrently and fixing it up later.

No, not really. Past the grammar there is no way to tell concurrently
from "concurrently", ie, if we did it like that then you couldn't even
use double quotes to get around it. Don't overthink this: either we
reserve the word or we don't put in the feature.

regards, tom lane

#20Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#19)
Re: creating index names automatically?

I wrote:

Greg Stark <gsstark@mit.edu> writes:

On Wed, Dec 23, 2009 at 3:54 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

Well, this will also break tables and columns named "concurrently".
I think the odds of it being a problem are small, but still it is
a reserved word that shouldn't be reserved according to the SQL spec.

I suppose we could fix this by specifying a precedence and then
explicitly checking if you're trying to make an index named
concurrently and fixing it up later.

No, not really. Past the grammar there is no way to tell concurrently
from "concurrently", ie, if we did it like that then you couldn't even
use double quotes to get around it. Don't overthink this: either we
reserve the word or we don't put in the feature.

I haven't heard anyone speak against making CONCURRENTLY semi-reserved,
so I'll go ahead and do it that way.

regards, tom lane

#21Bruce Momjian
bruce@momjian.us
In reply to: Tom Lane (#19)
#22Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#21)
#23Bruce Momjian
bruce@momjian.us
In reply to: Tom Lane (#22)
#24Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#23)
#25Guillaume Smet
guillaume.smet@gmail.com
In reply to: Tom Lane (#24)
#26Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#24)
#27Tom Lane
tgl@sss.pgh.pa.us
In reply to: Robert Haas (#26)
#28Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#27)
#29Tom Lane
tgl@sss.pgh.pa.us
In reply to: Robert Haas (#28)
#30Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#29)
#31Tom Lane
tgl@sss.pgh.pa.us
In reply to: Robert Haas (#30)
#32Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#31)
#33Tom Lane
tgl@sss.pgh.pa.us
In reply to: Robert Haas (#32)
#34Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#33)
#35tomas@tuxteam.de
tomas@tuxteam.de
In reply to: Robert Haas (#34)