BUG #4684: lastval in function

From: "andreas" <postgresql(at)elbrief(dot)de>
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #4684: lastval in function
Date: 2009-02-28 11:03:13
Message-ID: 200902281103.n1SB3Dqu084067@wwwmaster.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs


The following bug has been logged online:

Bug reference: 4684
Logged by: andreas
Email address: postgresql(at)elbrief(dot)de
PostgreSQL version: 8.3.6
Operating system: linux
Description: lastval in function
Details:

create table bla ( id serial primary key , name text not null unique ) ;
create table bla2 ( id serial primary key , blaid int references bla , name
text not null unique ) ;
create or replace function blaa( text ) returns int as $$
my $name = shift ;
my $q = spi_exec_query( "select id from bla where name = '$name'" ) ;
if ( not $q->{ rows }->[ 0 ]->{ id } ) {
spi_exec_query( "insert into bla ( name ) values ( '$name' )" ) ;
$q = spi_exec_query( "select lastval() as id" ) ;
}
return $q->{ rows }->[ 0 ]->{ id } ;
$$ language plperl security definer ;
select blaa( 'test' ) ;
insert into bla2 ( blaid , name ) values ( blaa( 'muster' ) , 'muster' ) ;

select lastval() ;
lastval
---------
2

i expected lastval() should be 1, because this is the id from the
insertstatement.

insert into bla2 ( blaid , name ) values ( blaa( 'muster2' ) , blaa(
'muster3' ) ) ;

select lastval() ;
lastval
---------
4

if nextval is used inside a function in a insertstatement, you get always
the value from inside the last function. but i expected, that lastval()
deliver the value from the insertstatement. i think, this should clearify in
the documentation, or better fixed that the nextval from an insertstatement
is called after the functioncalls.

Andreas

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2009-02-28 16:09:55 Re: BUG #4684: lastval in function
Previous Message Tom Lane 2009-02-27 19:43:26 Re: BUG #4680: Server crashed if using wrong (mismatch) conversion functions