setval('myfunsequence', 0)

Started by Ian Hardingover 23 years ago4 messagesgeneral
Jump to latest
#1Ian Harding
ianh@tpchd.org

The subject statement does not work for automagically generated sequences (minvalue is 1 by default, it seems.) I need to reset a sequence to where nextval('myfunsequence') is 1, and the only way to do it (I think) is to setval('myfunsequence', 0).

I can just create this particular sequence with minvalue 0 start 1, but I am wondering what is the downside to this being the default? Alternatively, is there a way to make setval accept a value that is $start - $increment as seems to happen on creation?

Visual Aids Follow...

test=# create sequence myfunsequence;
CREATE
test=# select nextval('myfunsequence');
nextval
---------
1
(1 row)

test=# select nextval('myfunsequence');
nextval
---------
2
(1 row)

test=# select setval('myfunsequence',0);
ERROR: myfunsequence.setval: value 0 is out of bounds (1,9223372036854775807)
test=# select setval('myfunsequence',1);
setval
--------
1
(1 row)

test=# select nextval('myfunsequence');
nextval
---------
2
(1 row)

test=# drop sequence myfunsequence;
DROP
test=# create sequence myfunsequence minvalue 0 start 1;
CREATE
test=# select nextval('myfunsequence');
NOTICE: myfunsequence.nextval: sequence was re-created
nextval
---------
1
(1 row)

test=# select nextval('myfunsequence');
nextval
---------
2
(1 row)

test=# select setval('myfunsequence',0);
setval
--------
0
(1 row)

test=# select nextval('myfunsequence');
nextval
---------
1
(1 row)

Thanks!!

Ian Harding
Programmer/Analyst II
Tacoma-Pierce County Health Department
iharding@tpchd.org
(253) 798-3549

#2scott.marlowe
scott.marlowe@ihs.com
In reply to: Ian Harding (#1)
Re: setval('myfunsequence', 0)

On Fri, 6 Dec 2002, Ian Harding wrote:

The subject statement does not work for automagically generated sequences (minvalue is 1 by default, it seems.) I need to reset a sequence to where nextval('myfunsequence') is 1, and the only way to do it (I think) is to setval('myfunsequence', 0).

I can just create this particular sequence with minvalue 0 start 1, but I am wondering what is the downside to this being the default? Alternatively, is there a way to make setval accept a value that is $start - $increment as seems to happen on creation?

Not true. Take a look at:
http://developer.postgresql.org/docs/postgres/functions-sequence.html

And note near the bottom, that you can setval with a third boolean value,
like so:

SELECT setval('foo', 1, false);

and the next nextval() will return 1

#3Ian Harding
ianh@tpchd.org
In reply to: scott.marlowe (#2)
Re: setval('myfunsequence', 0)

Argh! Sorry. I should RTFM!

"scott.marlowe" <scott.marlowe@ihs.com> 12/06/02 07:53AM >>>

On Fri, 6 Dec 2002, Ian Harding wrote:

The subject statement does not work for automagically generated sequences (minvalue is 1 by default, it seems.) I need to reset a sequence to where nextval('myfunsequence') is 1, and the only way to do it (I think) is to setval('myfunsequence', 0).

I can just create this particular sequence with minvalue 0 start 1, but I am wondering what is the downside to this being the default? Alternatively, is there a way to make setval accept a value that is $start - $increment as seems to happen on creation?

Not true. Take a look at:
http://developer.postgresql.org/docs/postgres/functions-sequence.html

And note near the bottom, that you can setval with a third boolean value,
like so:

SELECT setval('foo', 1, false);

and the next nextval() will return 1

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Ian Harding (#1)
Re: setval('myfunsequence', 0)

"Ian Harding" <ianh@tpchd.org> writes:

I can just create this particular sequence with minvalue 0 start 1, but I am wondering what is the downside to this being the default? Alternatively, is there a way to make setval accept a value that is $start - $increment as seems to happen on creation?

There's a separate bit in the sequence state that causes it not to
advance on first call; this allows the initial state to be $start
and not $start - $increment (which might underflow).

In recent PG versions you can manipulate the no-advance flag with
a 3-parameter setval() call.

regards, tom lane