lastval()

Started by Dennis Bjorklundover 21 years ago53 messageshackerspatches
Jump to latest
#1Dennis Bjorklund
db@zigo.dhs.org
hackerspatches

Here is a small patch that implements a function lastval() that
works just like currval() except that it give the current
value of the last sequence used by nextval().

Using this function one can do:

# CREATE TABLE abc (a serial, b int);
CREATE TABLE

# SELECT lastval();
ERROR: nextval have not been used in the current session

# INSERT INTO abc(b) VALUES (42);
INSERT 0 1

# SELECT lastval();
lastval
---------
1

Some comments about the implementetion
--------------------------------------

Each backend keeps a list of all used sequences in the session. This patch
adds a sequence pointer that point out one of the sequences in the list
and which is updated by nextval(). This is a simple pointer assignment so
it's very cheap (almost zero cost).

lastval() works just like currval but use the pointed out sequence
instead of geting a sequence name as an argument.

One can implement this by storing the value instead of the sequence
pointer but I decided it's a good thing that it works just like
currval(), behaving the same with respect to rights, locks and such.

General comments
----------------

I know that some of you might want to name this function the same as the
similar function in mysql (LAST_INSERT_ID), but I prefer to name it
similar to the old sequence functions. It's easy to add a LAST_INSERT_ID()
function that call lastval() if needed. Also, LAST_INSERT_ID() in mysql
will always succeed and it returns 0 if there have not been any row
inserted (at least what I think it will do that based on a quick look in
the mysql doc). The above function does not work like that.

--
/Dennis Bj�rklund

Attachments:

pg-lastval.txttext/plain; name=pg-lastval.txtDownload+134-31
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Dennis Bjorklund (#1)
hackerspatches
Re: lastval()

Dennis Bjorklund <db@zigo.dhs.org> writes:

Here is a small patch that implements a function lastval() that
works just like currval() except that it give the current
value of the last sequence used by nextval().

Why is that a good idea? In a complex application it'd be awfully easy
to break logic that depends on such a thing.

regards, tom lane

#3John Hansen
john@geeknet.com.au
In reply to: Tom Lane (#2)
patches
Re: lastval()

Tom Lane wrote:

Sent: Monday, May 09, 2005 8:37 AM
To: Dennis Bjorklund
Cc: pgsql-patches@postgresql.org
Subject: Re: [PATCHES] lastval()

Dennis Bjorklund <db@zigo.dhs.org> writes:

Here is a small patch that implements a function lastval()

that works

just like currval() except that it give the current value

of the last

sequence used by nextval().

Why is that a good idea? In a complex application it'd be
awfully easy to break logic that depends on such a thing.

And you threw out the idea when I proposed it more than a year ago.
However I'd still like to see it, tho in the same shape as 'the other
database'.

Eg, succeeding when nextval has not been called, returning 0.
Thus, my suggestion for last_insert_id() still stands.

regards, tom lane

---------------------------(end of
broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to
majordomo@postgresql.org

... John

#4Neil Conway
neilc@samurai.com
In reply to: Tom Lane (#2)
hackerspatches
Re: lastval()

Tom Lane wrote:

Why is that a good idea? In a complex application it'd be awfully easy
to break logic that depends on such a thing.

True, but I think it offers a usefully concise syntax for simpler
applications. Perhaps the documentation should be amended to mention the
potential risks? (e.g. additional nextval() calls in between the
nextval() you are interested in and the lastval()).

-Neil

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Neil Conway (#4)
hackerspatches
Re: lastval()

Neil Conway <neilc@samurai.com> writes:

Tom Lane wrote:

Why is that a good idea? In a complex application it'd be awfully easy
to break logic that depends on such a thing.

True, but I think it offers a usefully concise syntax for simpler
applications. Perhaps the documentation should be amended to mention the
potential risks?

Like, say, the sequence being deleted before the lastval call?

If I thought it was a good idea at all, I'd bother to criticize the
patch itself --- it's got some problems.

regards, tom lane

#6Dennis Bjorklund
db@zigo.dhs.org
In reply to: Tom Lane (#2)
hackerspatches
Re: lastval()

On Sun, 8 May 2005, Tom Lane wrote:

Why is that a good idea? In a complex application it'd be awfully easy
to break logic that depends on such a thing.

Of course it can break. currval() can also break in a complex application
with triggers and rules that do things the developer does not expect.

There are however lots of cases where it is safe and useful. Not the least
when you want to port an application that uses similar features.

--
/Dennis Bj�rklund

#7Dennis Bjorklund
db@zigo.dhs.org
In reply to: Tom Lane (#5)
hackerspatches
Re: lastval()

On Sun, 8 May 2005, Tom Lane wrote:

Like, say, the sequence being deleted before the lastval call?

Then you get an error message. Same thing if you have revoked the rights
on the sequence before you call lastval().

In this case you can get a value that belong to a sequence that is
deleted. Is that better? To me it's a sign that something is wrong with
the application and an error is better to get. It's not like it's hard to
store a int64 value instead. It's in fact simpler, but I just don't see
that it solve any problem. If anything it can hide problems.

If you want lastval() to work just don't delete the sequence. It's as
simple as that.

The thing is that I don't care how it's implemented, it's the feature
itself that is more importent to decide if we want it or not. I'm sure the
code can be fixed so everybody is happy it in the end,

--
/Dennis Bj�rklund

#8Heikki Linnakangas
heikki.linnakangas@enterprisedb.com
In reply to: Dennis Bjorklund (#7)
hackerspatches
Re: lastval()

On Mon, 9 May 2005, Dennis Bjorklund wrote:

The thing is that I don't care how it's implemented, it's the feature
itself that is more importent to decide if we want it or not. I'm sure the
code can be fixed so everybody is happy it in the end,

You could implement this on top of the current nextval without backend
changes.

Create a wrapper function on top of nextval that stores the value in a
temp table. Or a session variable if your PL language of choice has
them.

lastval would do a select on the temp table.

- Heikki

#9Andrew Dunstan
andrew@dunslane.net
In reply to: Heikki Linnakangas (#8)
hackerspatches
Re: lastval()

Heikki Linnakangas wrote:

On Mon, 9 May 2005, Dennis Bjorklund wrote:

The thing is that I don't care how it's implemented, it's the feature
itself that is more importent to decide if we want it or not. I'm
sure the
code can be fixed so everybody is happy it in the end,

You could implement this on top of the current nextval without backend
changes.

Create a wrapper function on top of nextval that stores the value in a
temp table. Or a session variable if your PL language of choice has them.

lastval would do a select on the temp table.

And this is making life easier for anybody? I don't think so.

cheers

andrew

#10Neil Conway
neilc@samurai.com
In reply to: Dennis Bjorklund (#1)
hackerspatches
Re: lastval()

Dennis Bjorklund wrote:

Here is a small patch that implements a function lastval() that
works just like currval() except that it give the current
value of the last sequence used by nextval().

What do people think of this idea? (Tom seems opposed, I'm just
wondering if there are other opinions out there.)

I like the concept, but I haven't looked at the code -- I'd be happy to
review the implementation, although I won't waste my time if most people
are opposed to the idea itself.

-Neil

#11Joshua D. Drake
jd@commandprompt.com
In reply to: Neil Conway (#10)
hackerspatches
Re: lastval()

Neil Conway wrote:

Dennis Bjorklund wrote:

Here is a small patch that implements a function lastval() that
works just like currval() except that it give the current
value of the last sequence used by nextval().

What do people think of this idea? (Tom seems opposed, I'm just
wondering if there are other opinions out there.)

I like the concept, but I haven't looked at the code -- I'd be happy to
review the implementation, although I won't waste my time if most people
are opposed to the idea itself.

I can't speak to the code but lastval is something that has been
requested by my customers many times.

Sincerely,

Joshua D. Drake

Show quoted text

-Neil

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

#12Bruce Momjian
bruce@momjian.us
In reply to: Neil Conway (#10)
hackerspatches
Re: lastval()

Neil Conway wrote:

Dennis Bjorklund wrote:

Here is a small patch that implements a function lastval() that
works just like currval() except that it give the current
value of the last sequence used by nextval().

What do people think of this idea? (Tom seems opposed, I'm just
wondering if there are other opinions out there.)

I like the idea of lastval, though I would rather see us just use
currval() with no argument for it, rather than invent a new function
name. It does the same as currval('last sequence called') right?

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#13Christopher Kings-Lynne
chriskl@familyhealth.com.au
In reply to: Neil Conway (#10)
hackerspatches
Re: lastval()

I like the concept, but I haven't looked at the code -- I'd be happy to
review the implementation, although I won't waste my time if most people
are opposed to the idea itself.

It'd make implementing various PHP userland functions a real breeze...

Chris

#14Andrew Dunstan
andrew@dunslane.net
In reply to: Christopher Kings-Lynne (#13)
hackerspatches
Re: lastval()

Abhijit Menon-Sen said:

At 2005-05-11 10:55:37 +1000, neilc@samurai.com wrote:

Here is a small patch that implements a function lastval() [...]

What do people think of this idea? (Tom seems opposed, I'm just
wondering if there are other opinions out there.)

For what it's worth, I think it's a bad idea.

In the MySQL wire protocol (hi Dennis!), the "last insert id" is sent
along with every "OK" message, and the client can just keep the value
in memory. Users call a function to retrieve that value, rather than
issuing a "SELECT nextval()".

You can do both - they have an SQL level function as well as supporting it
at the protocol layer. See
http://dev.mysql.com/doc/mysql/en/information-functions.html

So the server-side lastval() function is not enough for any meaningful
compatibility. The client would also need to be changed to provide the
pgsql_last_insert_id() or a similar function (which could do a "SELECT
lastval()" internally).

In this situation -- where both client changes AND a server round-trip
are required -- what's the point of adding cruft to the server? Might
as well confine changes to the client, and use nextval to implement the
feature.

I don't believ it can be sensibly done by the client alone. Either it needs
something like this or it shouldn't be done at all.

By the way, what would lastval() do if an insert trigger inserts a row
into a table with another serial column?

or more than one? Yes, it's not good in certain circumstances. That doesn't
make it useless in all circumstances.

I'm not jumping out of my seat to have this. But as Joshua points out, it is
frequently requested.

cheers

andrew

#15Abhijit Menon-Sen
ams@2ndQuadrant.com
In reply to: Neil Conway (#10)
hackerspatches
Re: lastval()

At 2005-05-11 10:55:37 +1000, neilc@samurai.com wrote:

Here is a small patch that implements a function lastval() [...]

What do people think of this idea? (Tom seems opposed, I'm just
wondering if there are other opinions out there.)

For what it's worth, I think it's a bad idea.

In the MySQL wire protocol (hi Dennis!), the "last insert id" is sent
along with every "OK" message, and the client can just keep the value
in memory. Users call a function to retrieve that value, rather than
issuing a "SELECT nextval()".

So the server-side lastval() function is not enough for any meaningful
compatibility. The client would also need to be changed to provide the
pgsql_last_insert_id() or a similar function (which could do a "SELECT
lastval()" internally).

In this situation -- where both client changes AND a server round-trip
are required -- what's the point of adding cruft to the server? Might
as well confine changes to the client, and use nextval to implement
the feature.

By the way, what would lastval() do if an insert trigger inserts a row
into a table with another serial column?

-- ams

#16John Hansen
john@geeknet.com.au
In reply to: Abhijit Menon-Sen (#15)
patches
Re: lastval()

Neil Conway wrote:

Sent: Wednesday, May 11, 2005 10:56 AM
To: Dennis Bjorklund
Cc: pgsql-patches@postgresql.org
Subject: Re: [PATCHES] lastval()

Dennis Bjorklund wrote:

Here is a small patch that implements a function lastval()

that works

just like currval() except that it give the current value

of the last

sequence used by nextval().

What do people think of this idea? (Tom seems opposed, I'm
just wondering if there are other opinions out there.)

I'm all for it. Even more so if the 'currval(void) called before
nextval(seq_name)' error message could be supressed by a GUC variable
and return 0 instead.

I like the concept, but I haven't looked at the code -- I'd
be happy to review the implementation, although I won't waste
my time if most people are opposed to the idea itself.

-Neil

---------------------------(end of
broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

.. John

#17Bruce Momjian
bruce@momjian.us
In reply to: John Hansen (#16)
patches
Re: lastval()

John Hansen wrote:

Neil Conway wrote:

Sent: Wednesday, May 11, 2005 10:56 AM
To: Dennis Bjorklund
Cc: pgsql-patches@postgresql.org
Subject: Re: [PATCHES] lastval()

Dennis Bjorklund wrote:

Here is a small patch that implements a function lastval()

that works

just like currval() except that it give the current value

of the last

sequence used by nextval().

What do people think of this idea? (Tom seems opposed, I'm
just wondering if there are other opinions out there.)

I'm all for it. Even more so if the 'currval(void) called before
nextval(seq_name)' error message could be supressed by a GUC variable
and return 0 instead.

Why zero and no error?

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#18Bruce Momjian
bruce@momjian.us
In reply to: Abhijit Menon-Sen (#15)
hackerspatches
Re: lastval()

Abhijit Menon-Sen wrote:

At 2005-05-11 10:55:37 +1000, neilc@samurai.com wrote:

Here is a small patch that implements a function lastval() [...]

What do people think of this idea? (Tom seems opposed, I'm just
wondering if there are other opinions out there.)

For what it's worth, I think it's a bad idea.

In the MySQL wire protocol (hi Dennis!), the "last insert id" is sent
along with every "OK" message, and the client can just keep the value
in memory. Users call a function to retrieve that value, rather than
issuing a "SELECT nextval()".

So the server-side lastval() function is not enough for any meaningful
compatibility. The client would also need to be changed to provide the
pgsql_last_insert_id() or a similar function (which could do a "SELECT
lastval()" internally).

In this situation -- where both client changes AND a server round-trip
are required -- what's the point of adding cruft to the server? Might
as well confine changes to the client, and use nextval to implement
the feature.

By the way, what would lastval() do if an insert trigger inserts a row
into a table with another serial column?

It fails, just like it would fail now if the trigger inserted into the
same table that used the trigger, or a rule.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#19Abhijit Menon-Sen
ams@2ndQuadrant.com
In reply to: Bruce Momjian (#18)
hackerspatches
Re: lastval()

At 2005-05-10 23:30:05 -0400, pgman@candle.pha.pa.us wrote:

By the way, what would lastval() do if an insert trigger inserts
a row into a table with another serial column?

It fails, just like it would fail now if the trigger inserted into
the same table that used the trigger, or a rule.

I don't understand what you mean. "Just like it would fail now"? It
doesn't exist yet, how can it fail? And how would it know when to
fail anyway, rather than return a wrong value?

-- ams

#20John Hansen
john@geeknet.com.au
In reply to: Abhijit Menon-Sen (#19)
patches
Re: lastval()

Bruce Momjian wrote:

Sent: Wednesday, May 11, 2005 1:27 PM
To: John Hansen
Cc: Neil Conway; Dennis Bjorklund; pgsql-patches@postgresql.org
Subject: Re: [PATCHES] lastval()

John Hansen wrote:

Neil Conway wrote:

Sent: Wednesday, May 11, 2005 10:56 AM
To: Dennis Bjorklund
Cc: pgsql-patches@postgresql.org
Subject: Re: [PATCHES] lastval()

Dennis Bjorklund wrote:

Here is a small patch that implements a function lastval()

that works

just like currval() except that it give the current value

of the last

sequence used by nextval().

What do people think of this idea? (Tom seems opposed, I'm just
wondering if there are other opinions out there.)

I'm all for it. Even more so if the 'currval(void) called before
nextval(seq_name)' error message could be supressed by a

GUC variable

and return 0 instead.

Why zero and no error?

That's the exact behaviour of the "other database's" equivalent.
Makes porting easier, and avoids hugely annoying error messages in the
logfiles.

Show quoted text
-- 
Bruce Momjian                        |  http://candle.pha.pa.us
pgman@candle.pha.pa.us               |  (610) 359-1001
+  If your life is a hard drive,     |  13 Roberts Road
+  Christ can be your backup.        |  Newtown Square, 
Pennsylvania 19073
#21Bruce Momjian
bruce@momjian.us
In reply to: Abhijit Menon-Sen (#19)
hackerspatches
#22Bruce Momjian
bruce@momjian.us
In reply to: John Hansen (#20)
patches
#23John Hansen
john@geeknet.com.au
In reply to: Bruce Momjian (#22)
patches
#24Neil Conway
neilc@samurai.com
In reply to: John Hansen (#16)
patches
#25John Hansen
john@geeknet.com.au
In reply to: Neil Conway (#24)
patches
#26Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#18)
hackerspatches
#27Tom Lane
tgl@sss.pgh.pa.us
In reply to: John Hansen (#16)
patches
#28John Hansen
john@geeknet.com.au
In reply to: Tom Lane (#27)
patches
#29Alvaro Herrera
alvherre@surnet.cl
In reply to: John Hansen (#23)
patches
#30John Hansen
john@geeknet.com.au
In reply to: Alvaro Herrera (#29)
patches
#31Alvaro Herrera
alvherre@surnet.cl
In reply to: John Hansen (#30)
patches
#32John Hansen
john@geeknet.com.au
In reply to: Alvaro Herrera (#31)
patches
#33Tom Lane
tgl@sss.pgh.pa.us
In reply to: John Hansen (#32)
patches
#34Neil Conway
neilc@samurai.com
In reply to: Dennis Bjorklund (#1)
hackerspatches
#35Neil Conway
neilc@samurai.com
In reply to: Dennis Bjorklund (#1)
hackerspatches
#36Dennis Bjorklund
db@zigo.dhs.org
In reply to: Neil Conway (#35)
hackerspatches
#37Dennis Bjorklund
db@zigo.dhs.org
In reply to: Neil Conway (#34)
hackerspatches
#38Christopher Kings-Lynne
chriskl@familyhealth.com.au
In reply to: Dennis Bjorklund (#36)
hackerspatches
#39John Hansen
john@geeknet.com.au
In reply to: Christopher Kings-Lynne (#38)
patches
#40Neil Conway
neilc@samurai.com
In reply to: Dennis Bjorklund (#37)
hackerspatches
#41Abhijit Menon-Sen
ams@2ndQuadrant.com
In reply to: Neil Conway (#40)
hackerspatches
#42Neil Conway
neilc@samurai.com
In reply to: Abhijit Menon-Sen (#41)
hackerspatches
#43Tom Lane
tgl@sss.pgh.pa.us
In reply to: Neil Conway (#42)
hackerspatches
#44Neil Conway
neilc@samurai.com
In reply to: Tom Lane (#43)
hackerspatches
#45Tom Lane
tgl@sss.pgh.pa.us
In reply to: Neil Conway (#44)
hackerspatches
#46Neil Conway
neilc@samurai.com
In reply to: Tom Lane (#45)
hackerspatches
#47Tom Lane
tgl@sss.pgh.pa.us
In reply to: Neil Conway (#46)
hackerspatches
#48Dennis Bjorklund
db@zigo.dhs.org
In reply to: Tom Lane (#47)
hackerspatches
#49John Hansen
john@geeknet.com.au
In reply to: Dennis Bjorklund (#48)
patches
#50Christopher Kings-Lynne
chriskl@familyhealth.com.au
In reply to: Neil Conway (#42)
hackerspatches
#51Neil Conway
neilc@samurai.com
In reply to: Neil Conway (#40)
hackerspatches
#52Christopher Kings-Lynne
chriskl@familyhealth.com.au
In reply to: Neil Conway (#51)
hackerspatches
#53Neil Conway
neilc@samurai.com
In reply to: Neil Conway (#51)
hackerspatches