Re: resetting sequence to cur max value

From: Berend Tober <btober(at)seaworthysys(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Michael Fuhr <mike(at)fuhr(dot)org>, developer(at)wexwarez(dot)com, pgsql-general(at)postgresql(dot)org
Subject: Re: resetting sequence to cur max value
Date: 2006-12-13 13:27:48
Message-ID: 457FFFD4.9080806@seaworthysys.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Tom Lane wrote:
> Michael Fuhr <mike(at)fuhr(dot)org> writes:
>
>> On Tue, Dec 12, 2006 at 12:19:56PM -0500, Tom Lane wrote:
>>
>>> Usually you do something like
>>> select setval('seq_name', (select max(idcol) from table) + 1);
>>> after loading data into the table.
>>>
>
>
>> Is "+ 1" necessary with the two-parameter form of setval()?
>>
>
> Given the docs you quoted, no --- I was just too lazy to look up whether
> it set is_called or not. With the +1 you don't have to think ;-)
>
>
Even less thinking:

CREATE OR REPLACE FUNCTION public.set_sequence(name, int4)
RETURNS int4 AS
'
DECLARE
l_sequence_name ALIAS FOR $1;
l_last_value ALIAS FOR $2;
BEGIN
IF l_last_value = 0 THEN
PERFORM setval(l_sequence_name,1, False);
ELSE
PERFORM setval(l_sequence_name,l_last_value);
END IF;
RETURN 1;
END;'
LANGUAGE 'plpgsql' VOLATILE;

Regards,
Berend Tober

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tomi N/A 2006-12-13 13:42:32 Re: grant select on all tables of schema or database
Previous Message Martijn van Oosterhout 2006-12-13 12:52:08 Re: Statement timeout not working on broken connections with active queries