allocate chunk of sequence - Mailing list pgsql-general

From Gary Fu
Subject allocate chunk of sequence
Date
Msg-id f4rlrp$22cc$1@news.hub.org
Whole thread Raw
Responses Re: allocate chunk of sequence  (Scott Marlowe <smarlowe@g2switchworks.com>)
List pgsql-general
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;

pgsql-general by date:

Previous
From: "Andrus"
Date:
Subject: Re: how to speed up query
Next
From: "Andrus"
Date:
Subject: Disable duplicate index creation