Setting a pre-existing index as a primary key

Started by Jonah H. Harrisabout 18 years ago25 messageshackers
Jump to latest
#1Jonah H. Harris
jonah.harris@gmail.com

Hey all,

I've run into a couple cases now where it would be helpful to easily
assign an already-existing unique index as a primary key. Unless I
completely missed something, there's no way to do this now without a
bit of catalog hackery.

My implementation idea is as follows:

Proposed Syntax (based on Oracle's syntax)

ALTER TABLE foo ADD CONSTRAINT bar PRIMARY KEY USING INDEX schema.tablename;

Proposed Implementation

1. Verify that the index named is a unique index
2. Check index columns for NOT NULL constraints
3. If indexed columns are not already NOT NULL, apply NOT NULL
4. If NOT NULL succeeds, complete the operation (catalogs,
dependencies, ...), else bail out.

Any comments, ideas, suggestions?

--
Jonah H. Harris, Sr. Software Architect | phone: 732.331.1324
EnterpriseDB Corporation | fax: 732.331.1301
499 Thornall Street, 2nd Floor | jonah.harris@enterprisedb.com
Edison, NJ 08837 | http://www.enterprisedb.com/

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Jonah H. Harris (#1)
Re: Setting a pre-existing index as a primary key

"Jonah H. Harris" <jonah.harris@gmail.com> writes:

I've run into a couple cases now where it would be helpful to easily
assign an already-existing unique index as a primary key.

You need to present a more convincing use-case than this unsupported
assertion. There's hardly any effective difference between a unique
index + NOT NULL constraints and a declared primary key ... so what
did you really need it for?

1. Verify that the index named is a unique index

... and not partial, and not on expressions, and not invalid, and not
using non-default opclasses (which might have a surprising definition of
"equal"), and not already owned by a constraint ... not to mention that
it'd better be an index on the named table, which among other things
removes the need for a schema specification on the index name.

regards, tom lane

#3Jonah H. Harris
jonah.harris@gmail.com
In reply to: Tom Lane (#2)
Re: Setting a pre-existing index as a primary key

On Tue, Apr 8, 2008 at 9:04 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

"Jonah H. Harris" <jonah.harris@gmail.com> writes:

I've run into a couple cases now where it would be helpful to easily
assign an already-existing unique index as a primary key.

You need to present a more convincing use-case than this unsupported
assertion. There's hardly any effective difference between a unique
index + NOT NULL constraints and a declared primary key ... so what
did you really need it for?

Agreed, functionally there's not much of a difference. It's more of a
matter of proper design identifying a primary key.

1. Verify that the index named is a unique index

... and not partial, and not on expressions, and not invalid, and not
using non-default opclasses (which might have a surprising definition of
"equal"), and not already owned by a constraint ... not to mention that
it'd better be an index on the named table, which among other things
removes the need for a schema specification on the index name.

Of course.

--
Jonah H. Harris, Sr. Software Architect | phone: 732.331.1324
EnterpriseDB Corporation | fax: 732.331.1301
499 Thornall Street, 2nd Floor | jonah.harris@enterprisedb.com
Edison, NJ 08837 | http://www.enterprisedb.com/

#4Jaime Casanova
jcasanov@systemguards.com.ec
In reply to: Jonah H. Harris (#3)
Re: Setting a pre-existing index as a primary key

On Tue, Apr 8, 2008 at 10:03 PM, Jonah H. Harris <jonah.harris@gmail.com> wrote:

On Tue, Apr 8, 2008 at 9:04 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

"Jonah H. Harris" <jonah.harris@gmail.com> writes:

I've run into a couple cases now where it would be helpful to easily
assign an already-existing unique index as a primary key.

You need to present a more convincing use-case than this unsupported
assertion. There's hardly any effective difference between a unique
index + NOT NULL constraints and a declared primary key ... so what
did you really need it for?

Agreed, functionally there's not much of a difference. It's more of a
matter of proper design identifying a primary key.

set right constraints it's good for documenting the system itself, i
like the idea...

--
regards,
Jaime Casanova

#5Mario Weilguni
mweilguni@sime.com
In reply to: Tom Lane (#2)
Re: Setting a pre-existing index as a primary key

Tom Lane schrieb:

"Jonah H. Harris" <jonah.harris@gmail.com> writes:

I've run into a couple cases now where it would be helpful to easily
assign an already-existing unique index as a primary key.

You need to present a more convincing use-case than this unsupported
assertion. There's hardly any effective difference between a unique
index + NOT NULL constraints and a declared primary key ... so what
did you really need it for?

In fact it seems to be necessary when connecting with ODBC, I had the
problem a month ago, MsSQL will not work correctly with connected tables
in a postgres database when there is no PK. NOT NULL and unique index
is not enough.

But I think it's overkill to add ALTER commands for this rare corner
case, maybe it's enough to set "indisprimary" on the index?

#6Bruce Momjian
bruce@momjian.us
In reply to: Jonah H. Harris (#1)
Re: Setting a pre-existing index as a primary key

Added to TODO:

o Allow an existing index to be marked as a table's primary key

---------------------------------------------------------------------------

Jonah H. Harris wrote:

Hey all,

I've run into a couple cases now where it would be helpful to easily
assign an already-existing unique index as a primary key. Unless I
completely missed something, there's no way to do this now without a
bit of catalog hackery.

My implementation idea is as follows:

Proposed Syntax (based on Oracle's syntax)

ALTER TABLE foo ADD CONSTRAINT bar PRIMARY KEY USING INDEX schema.tablename;

Proposed Implementation

1. Verify that the index named is a unique index
2. Check index columns for NOT NULL constraints
3. If indexed columns are not already NOT NULL, apply NOT NULL
4. If NOT NULL succeeds, complete the operation (catalogs,
dependencies, ...), else bail out.

Any comments, ideas, suggestions?

--
Jonah H. Harris, Sr. Software Architect | phone: 732.331.1324
EnterpriseDB Corporation | fax: 732.331.1301
499 Thornall Street, 2nd Floor | jonah.harris@enterprisedb.com
Edison, NJ 08837 | http://www.enterprisedb.com/

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ If your life is a hard drive, Christ can be your backup. +

#7Joshua D. Drake
jd@commandprompt.com
In reply to: Bruce Momjian (#6)
Re: Setting a pre-existing index as a primary key

Bruce Momjian wrote:

Added to TODO:

Proposed Implementation

1. Verify that the index named is a unique index
2. Check index columns for NOT NULL constraints
3. If indexed columns are not already NOT NULL, apply NOT NULL
4. If NOT NULL succeeds, complete the operation (catalogs,
dependencies, ...), else bail out.

Any comments, ideas, suggestions?

I would add:

5. Modify index name to use appropriate naming style.

Joshua D. Drake

#8Tom Lane
tgl@sss.pgh.pa.us
In reply to: Joshua D. Drake (#7)
Re: Setting a pre-existing index as a primary key

"Joshua D. Drake" <jd@commandprompt.com> writes:

Bruce Momjian wrote:

Any comments, ideas, suggestions?

I would add:

5. Modify index name to use appropriate naming style.

Why, and exactly what would you define as "appropriate naming style"?
The user has always been free to pick whatever constraint name he
wants.

regards, tom lane

#9Joshua D. Drake
jd@commandprompt.com
In reply to: Tom Lane (#8)
Re: Setting a pre-existing index as a primary key

Tom Lane wrote:

"Joshua D. Drake" <jd@commandprompt.com> writes:

Bruce Momjian wrote:

Any comments, ideas, suggestions?

I would add:

5. Modify index name to use appropriate naming style.

Why, and exactly what would you define as "appropriate naming style"?
The user has always been free to pick whatever constraint name he
wants.

Well it should be optional but it would be nice if we had the option to
have it renamed per the default... meaning the same output if I were to
do this:

create table foo (id serial primary key);

I end up with "foo_pkey" PRIMARY KEY, btree (id)

Which is nice for consistency.

Sincerely,

Joshua D. Drake

Show quoted text

regards, tom lane

#10Tom Lane
tgl@sss.pgh.pa.us
In reply to: Joshua D. Drake (#9)
Re: Setting a pre-existing index as a primary key

"Joshua D. Drake" <jd@commandprompt.com> writes:

Tom Lane wrote:

Why, and exactly what would you define as "appropriate naming style"?
The user has always been free to pick whatever constraint name he
wants.

Well it should be optional but it would be nice if we had the option to
have it renamed per the default... meaning the same output if I were to
do this:

If you want that, you can rename the index (either before or afterwards).
I don't see any reason to clutter the make-constraint-from-index command
with questions of renaming.

regards, tom lane

#11Joshua D. Drake
jd@commandprompt.com
In reply to: Tom Lane (#10)
Re: Setting a pre-existing index as a primary key

Tom Lane wrote:

Well it should be optional but it would be nice if we had the option to
have it renamed per the default... meaning the same output if I were to
do this:

If you want that, you can rename the index (either before or afterwards).
I don't see any reason to clutter the make-constraint-from-index command
with questions of renaming.

As a counter point, I don't see any reason to make the DBA's life
harder. Sure it is just one step but it is a human step, prone to error
and taking more time than it should. Why not just make it easy?
Especially when the easy isn't sacrificing data integrity or quality of
product?

Sincerely,

Joshua D. Drake

Show quoted text

regards, tom lane

#12Andrew Dunstan
andrew@dunslane.net
In reply to: Joshua D. Drake (#11)
Re: Setting a pre-existing index as a primary key

Joshua D. Drake wrote:

Tom Lane wrote:

Well it should be optional but it would be nice if we had the option
to have it renamed per the default... meaning the same output if I
were to do this:

If you want that, you can rename the index (either before or
afterwards).
I don't see any reason to clutter the make-constraint-from-index command
with questions of renaming.

As a counter point, I don't see any reason to make the DBA's life
harder. Sure it is just one step but it is a human step, prone to
error and taking more time than it should. Why not just make it easy?
Especially when the easy isn't sacrificing data integrity or quality
of product?

Because that's not the basis on which we decide to add features. You
need to asses the code complexity, the potential benefit and number of
likely users. In this case, the amount of code required for what would
be nothing more than syntactic sugar for what is in any case a very
simple statement makes me agree with Tom.

cheers

andrew

#13Tom Lane
tgl@sss.pgh.pa.us
In reply to: Joshua D. Drake (#11)
Re: Setting a pre-existing index as a primary key

"Joshua D. Drake" <jd@commandprompt.com> writes:

As a counter point, I don't see any reason to make the DBA's life
harder. Sure it is just one step but it is a human step, prone to error
and taking more time than it should. Why not just make it easy?

I don't see that decorating infrequently-used statements with bizarre
options that duplicate the functionality of other commands is "making it
easy". Apparently your definition of "easy" depends entirely on
keystrokes and not at all on memory/cognitive burden.

IMHO a utility command should do one easily-explained thing. The fewer
options the better.

regards, tom lane

#14Joshua D. Drake
jd@commandprompt.com
In reply to: Tom Lane (#13)
Re: Setting a pre-existing index as a primary key

Tom Lane wrote:

Apparently your definition of "easy" depends entirely on
keystrokes and not at all on memory/cognitive burden.

I was trying to remove one opportunity for human error, which is tied to
memory and cognitive burden. It is very easy to fat finger something. Is
it a critical error? No. Is it obnoxious to have to go back and fix it,
yes. When you are going back to fix, are you going to be grousing about
how PostgreSQL doesn't make this easier, maybe.

IMHO a utility command should do one easily-explained thing. The fewer
options the better.

I would agree with this except that by my definition your argument
fails. You are adding options by not allowing a sane default that
applies consistency to the database. I believe this will cause more
trouble than having the limitation in the first place.

Anyway, I have made my arguments. I believe we are still in the middle
of a commit fest.

Sincerely,

Joshua D. Drake

#15Jonah H. Harris
jonah.harris@gmail.com
In reply to: Joshua D. Drake (#14)
Re: Setting a pre-existing index as a primary key

So, would anyone be averse to something like the following:

ALTER TABLE blah ADD ... PRIMARY KEY (...) USING PREBUILT INDEX index_hame

If the user doesn't specify CONSTRAINT constraint_name, it will
default to current implicit behavior of col_pkey.

-Jonah

On Sat, May 10, 2008 at 1:08 PM, Joshua D. Drake <jd@commandprompt.com> wrote:

Tom Lane wrote:

Apparently your definition of "easy" depends entirely on
keystrokes and not at all on memory/cognitive burden.

I was trying to remove one opportunity for human error, which is tied to
memory and cognitive burden. It is very easy to fat finger something. Is it
a critical error? No. Is it obnoxious to have to go back and fix it, yes.
When you are going back to fix, are you going to be grousing about how
PostgreSQL doesn't make this easier, maybe.

IMHO a utility command should do one easily-explained thing. The fewer
options the better.

I would agree with this except that by my definition your argument fails.
You are adding options by not allowing a sane default that applies
consistency to the database. I believe this will cause more trouble than
having the limitation in the first place.

Anyway, I have made my arguments. I believe we are still in the middle of a
commit fest.

Sincerely,

Joshua D. Drake

--
Jonah H. Harris, Sr. Software Architect | phone: 732.331.1324
EnterpriseDB Corporation | fax: 732.331.1301
499 Thornall Street, 2nd Floor | jonah.harris@enterprisedb.com
Edison, NJ 08837 | http://www.enterprisedb.com/

#16Bruce Momjian
bruce@momjian.us
In reply to: Jonah H. Harris (#15)
Re: Setting a pre-existing index as a primary key

"Jonah H. Harris" <jonah.harris@gmail.com> writes:

So, would anyone be averse to something like the following:

ALTER TABLE blah ADD ... PRIMARY KEY (...) USING PREBUILT INDEX index_hame

If the user doesn't specify CONSTRAINT constraint_name, it will
default to current implicit behavior of col_pkey.

This is all so that the primary key shows up with a nice "PRIMARY KEY" instead
of just the unique index?

The "PREBUILT" seems unnecessary in that syntax.

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Ask me about EnterpriseDB's Slony Replication support!

#17Jonah H. Harris
jonah.harris@gmail.com
In reply to: Bruce Momjian (#16)
Re: Setting a pre-existing index as a primary key

Yes, I just think PREBUILT conveys the meaning of the command more
appropriately. I could care less though.

On Sat, May 10, 2008 at 5:35 PM, Gregory Stark <stark@enterprisedb.com> wrote:

"Jonah H. Harris" <jonah.harris@gmail.com> writes:

So, would anyone be averse to something like the following:

ALTER TABLE blah ADD ... PRIMARY KEY (...) USING PREBUILT INDEX index_hame

If the user doesn't specify CONSTRAINT constraint_name, it will
default to current implicit behavior of col_pkey.

This is all so that the primary key shows up with a nice "PRIMARY KEY" instead
of just the unique index?

The "PREBUILT" seems unnecessary in that syntax.

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Ask me about EnterpriseDB's Slony Replication support!

--
Jonah H. Harris, Sr. Software Architect | phone: 732.331.1324
EnterpriseDB Corporation | fax: 732.331.1301
499 Thornall Street, 2nd Floor | jonah.harris@enterprisedb.com
Edison, NJ 08837 | http://www.enterprisedb.com/

#18Andrew Dunstan
andrew@dunslane.net
In reply to: Jonah H. Harris (#17)
Re: Setting a pre-existing index as a primary key

Jonah H. Harris wrote:

Yes, I just think PREBUILT conveys the meaning of the command more
appropriately. I could care less though.

(Please don't top-answer)

I don't think we should add new keywords unnecessarily.

cheers

andrew

Show quoted text

On Sat, May 10, 2008 at 5:35 PM, Gregory Stark <stark@enterprisedb.com> wrote:

"Jonah H. Harris" <jonah.harris@gmail.com> writes:

So, would anyone be averse to something like the following:

ALTER TABLE blah ADD ... PRIMARY KEY (...) USING PREBUILT INDEX index_hame

If the user doesn't specify CONSTRAINT constraint_name, it will
default to current implicit behavior of col_pkey.

This is all so that the primary key shows up with a nice "PRIMARY KEY" instead
of just the unique index?

The "PREBUILT" seems unnecessary in that syntax.

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Ask me about EnterpriseDB's Slony Replication support!

#19Tom Lane
tgl@sss.pgh.pa.us
In reply to: Jonah H. Harris (#15)
Re: Setting a pre-existing index as a primary key

"Jonah H. Harris" <jonah.harris@gmail.com> writes:

ALTER TABLE blah ADD ... PRIMARY KEY (...) USING PREBUILT INDEX index_hame

If the user doesn't specify CONSTRAINT constraint_name, it will
default to current implicit behavior of col_pkey.

IOW, the default behavior is to rename the index? Doesn't seem to me
to satisfy the principle of least surprise.

I agree with Andrew that creating new keywords just for noise purposes
is not gonna happen.

regards, tom lane

#20Andrew Sullivan
ajs@commandprompt.com
In reply to: Tom Lane (#13)
Re: Setting a pre-existing index as a primary key

On Sat, May 10, 2008 at 11:55:29AM -0400, Tom Lane wrote:

IMHO a utility command should do one easily-explained thing. The fewer
options the better.

Sticking to that principle makes for a better-maintained system. I
agree. If we want to point out, "You might rename your index
afterwards to make it look like other default primary keys," I have no
objection.

A

--
Andrew Sullivan
ajs@commandprompt.com
+1 503 667 4564 x104
http://www.commandprompt.com/

#21Tino Wildenhain
tino@wildenhain.de
In reply to: Joshua D. Drake (#11)
#22David Fetter
david@fetter.org
In reply to: Andrew Sullivan (#20)
#23Bruce Momjian
bruce@momjian.us
In reply to: Joshua D. Drake (#11)
#24Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#23)
#25Andrew Dunstan
andrew@dunslane.net
In reply to: Tom Lane (#24)