hello,

I try to allocate a chunk of ids from a sequence with the following proc. However, if I don't use the 'lock lock_table', the proc may not work when it runs at the same time by different psql sessions. Is there a better way without using the 'lock lock_table' ?

Thanks,
Gary

create or replace function proc_allocate_seq(int)
    returns int as $$

declare
    nNumberOfFiles          alias for $1;

    aFileId int;
    aNewFileId int;

begin

    lock lock_table;

    aFileId = nextval('aa_seq');

; sleep(3);    if you have the proc

    aNewFileId = setval('aa_seq', aFileId + nNumberOfFiles - 1);

    return aFileId;

end;
$$ language plpgsql;

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
      choose an index scan if your joining column's datatypes do not
      match

Reply via email to