my pl/pgsql functions return weird results

From: Olivier Garcia <ogarcia(at)waidan(dot)com>
To: pgsql-bugs(at)postgresql(dot)org
Subject: my pl/pgsql functions return weird results
Date: 2002-02-22 08:07:28
Message-ID: 1014365248.20427.6.camel@sauron
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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';

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message pgsql-bugs 2002-02-22 08:24:04 Bug #600: how to run the Postgres postmaster with -i flag
Previous Message Justin Clift 2002-02-22 06:04:41 Re: Trying Cygwin version of PostgreSQL again