dbTalk Databases Forums  

determine sequence name for a serial

comp.databases.postgresql.general comp.databases.postgresql.general


Discuss determine sequence name for a serial in the comp.databases.postgresql.general forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
Robby Russell
 
Posts: n/a

Default Re: determine sequence name for a serial - 10-28-2004 , 06:07 PM






On Thu, 2004-10-28 at 16:51 -0600, Ed L. wrote:
Quote:
On Thursday October 28 2004 11:42, Robby Russell wrote:

Thanks, this seems to work well. My goal is to actually create a php
function that takes a result and returns the insert_id like
mysql_insert_id() does, but without needing to know the sequence names
and such. I would make a psql function, but I don't always have that
option with some clients existing systems.

An alternative is to simply select nextval() from a separately-created
sequence object to get the serial value, then insert with that value. No
need to have a serial column then, but you do need to explicitly create the
sequence object, as opposed to SERIAL.

nextval, currval, either way, I would need to know the specific sequence
name. Was looking for a good way to pass a function a schema and table
and return a sequence. I got exactly what I was looking for and have
been able to build a function that will handle this for me. It's part of
a db layer class that I use with mysql and pgsql, and was using
mysql_insert_id and wanted to model a function that would return an id
like the mysql_insert_id function does. (one of the few pgsql/php
functions that doesn't exist in php natively..but does with mysql)

-Robby

--
/***************************************
* Robby Russell | Owner.Developer.Geek
* PLANET ARGON | www.planetargon.com
* Portland, OR | robby (AT) planetargon (DOT) com
* 503.351.4730 | blog.planetargon.com
* PHP/PostgreSQL Hosting & Development
* --- Now supporting PHP5 ---
****************************************/

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQBBgXu40QaQZBaqXgwRAqN3AKCuxq/ZUnBv43W8fLEyuqoxQzWT/QCeLQ3f
CPtaQVng6szrZuNEcgalxls=
=Y+YN
-----END PGP SIGNATURE-----



Reply With Quote
  #12  
Old   
Jonathan Daugherty
 
Posts: n/a

Default Re: determine sequence name for a serial - 10-28-2004 , 06:25 PM






# But I didn't understand why you care to get rid of the explicit reference to
# the sequence object in your code in the first place. In PostgreSQL, at
# least for the past 5 years if not longer, if you create a SERIAL column for
# (schemaname, tablename, columnname), then your sequence will *always* be
# "schemaname.tablename_columnname_seq". If that naming convention changes,
# there will be a whole lotta breakage world-wide.

When a table is renamed, related sequences' names don't change (as of
7.4.5). The ability to automagically pull the sequence based on the
schema.table.column would be nice if you don't want to worry about
having to update your table name and sequence name references in code.

--
Jonathan Daugherty
Command Prompt, Inc. - http://www.commandprompt.com/
PostgreSQL Replication & Support Services, (503) 667-4564


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)


Reply With Quote
  #13  
Old   
Michael Fuhr
 
Posts: n/a

Default Re: determine sequence name for a serial - 10-28-2004 , 06:31 PM



On Thu, Oct 28, 2004 at 04:51:05PM -0600, Ed L. wrote:
Quote:
But I didn't understand why you care to get rid of the explicit reference to
the sequence object in your code in the first place. In PostgreSQL, at
least for the past 5 years if not longer, if you create a SERIAL column for
(schemaname, tablename, columnname), then your sequence will *always* be
"schemaname.tablename_columnname_seq".
Only for certain values of "always." Tables and columns can be renamed,
so the sequence name might no longer be "tablename_columnname_seq",
but rather "oldtablename_oldcolumnname_seq".

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster



Reply With Quote
  #14  
Old   
Ed L.
 
Posts: n/a

Default Re: determine sequence name for a serial - 10-28-2004 , 06:50 PM



On Thursday October 28 2004 5:31, Michael Fuhr wrote:
Quote:
On Thu, Oct 28, 2004 at 04:51:05PM -0600, Ed L. wrote:
But I didn't understand why you care to get rid of the explicit
reference to the sequence object in your code in the first place. In
PostgreSQL, at least for the past 5 years if not longer, if you create
a SERIAL column for (schemaname, tablename, columnname), then your
sequence will *always* be "schemaname.tablename_columnname_seq".

Only for certain values of "always." Tables and columns can be renamed,
so the sequence name might no longer be "tablename_columnname_seq",
but rather "oldtablename_oldcolumnname_seq".
Your point is well taken, I see the gotcha there, and thus the value of a
function.

Ed


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
  #15  
Old   
Greg Stark
 
Posts: n/a

Default Re: determine sequence name for a serial - 10-28-2004 , 06:55 PM




"Ed L." <pgsql (AT) bluepolka (DOT) net> writes:

Quote:
In PostgreSQL, at least for the past 5 years if not longer, if you create a
SERIAL column for (schemaname, tablename, columnname), then your sequence
will *always* be "schemaname.tablename_columnname_seq". If that naming
convention changes, there will be a whole lotta breakage world-wide.
I hope you're wrong about people expecting that to be true because it isn't.
The resulting sequence name is limited to 63 characters and gets truncated if
it goes over. (63!? was it intended to be 64?) I believe the limit used to be
32 characters too.

In any case it's just plain good design to avoid unnecessary
interrelationships between different parts of the code. Practically speaking
it makes renaming something not involve an error-prone search and replace.
More importantly it makes it easier to verify that a piece of code is correct
without having to hunt down all the related bits to be sure the relationships
are correct. It also makes it possible to reuse or refactor the code.

--
greg


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
  #16  
Old   
Alvaro Herrera
 
Posts: n/a

Default Re: determine sequence name for a serial - 10-28-2004 , 07:40 PM



On Thu, Oct 28, 2004 at 07:55:51PM -0400, Greg Stark wrote:
Quote:
"Ed L." <pgsql (AT) bluepolka (DOT) net> writes:

In PostgreSQL, at least for the past 5 years if not longer, if you create a
SERIAL column for (schemaname, tablename, columnname), then your sequence
will *always* be "schemaname.tablename_columnname_seq". If that naming
convention changes, there will be a whole lotta breakage world-wide.

I hope you're wrong about people expecting that to be true because it isn't.
The resulting sequence name is limited to 63 characters and gets truncated if
it goes over. (63!? was it intended to be 64?) I believe the limit used to be
32 characters too.
The NAMEDATALEN constant is defined to be 64, and that includes the
trailing \0, so identifiers are limited to 63 bytes.

--
Alvaro Herrera (<alvherre[a]dcc.uchile.cl>)
"Before you were born your parents weren't as boring as they are now. They
got that way paying your bills, cleaning up your room and listening to you
tell them how idealistic you are." -- Charles J. Sykes' advice to teenagers


---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.