my pl/pgsql functions return weird results - Mailing list pgsql-bugs

From Olivier Garcia
Subject my pl/pgsql functions return weird results
Date
Msg-id 1014365248.20427.6.camel@sauron
Whole thread Raw
In response to Re: Full bug list  (Justin <aa2@bigpond.net.au>)
Responses Re: my pl/pgsql functions return weird results
List pgsql-bugs
Hi!

My base contains a table objectid

create table objectid(
    next bigint
);

INSERT INTO objectid values(1);

I do really need to use this table like if it was a sequence so I made
some pl/pgsql functions ( functions at the end of the mail ).
But when I run them in three psql at the same time the results are
weird... Here are the results :

psql1: 2 ( ok )
psql2: 1 ( instead of 3 )
psql3: 1 ( instead of 4 )

Scary...

and after that, a "select * from objectid;" returns 4 ( that's ok ).

I'm using pgsql 7.1.3 ( debian package 7.1.3-8 on a 2.4.17 kernel )

-----------------------------
----------- Functions
-----------------------------

drop table objectid;

create table objectid(
    next bigint
);

INSERT INTO objectid values(1);

drop function next_objectid();

create function next_objectid() returns bigint as '
DECLARE
   o RECORD;
   a integer;
BEGIN
   raise notice ''before lock'';
   lock table objectid in access exclusive mode;
   raise notice ''locked, before pause'';
   a := pause(1000000);
   raise notice ''pause finished, updating'';
   update objectid set next=next+1;
   select into o next from objectid;
   raise notice ''returning'';
   return o.next;
END;
' language 'plpgsql';

drop function pause(int);

create function pause(int) returns int as '
DECLARE
   i integer;
   j integer;
BEGIN
   FOR i IN 1 .. $1 LOOP
      j := i+1;
   end loop;
   return j;
END;
' language 'plpgsql';

pgsql-bugs by date:

Previous
From: Justin Clift
Date:
Subject: Re: Trying Cygwin version of PostgreSQL again
Next
From: pgsql-bugs@postgresql.org
Date:
Subject: Bug #600: how to run the Postgres postmaster with -i flag