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';





---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to [EMAIL PROTECTED] so that your
message can get through to the mailing list cleanly

Reply via email to