I just found an odd thing about nextval (PostgreSQL 9.0): When nextval is called together with a function returning a
sequence,such as generate_series or unnest, it skips one value between consecutive calls:
create sequence test_sequence;
-- This works as expected
select nextval(' test_sequence'); -- 1
select nextval(' test_sequence'); -- 2
-- This is rather surprising
select nextval(' test_sequence'), generate_series(1, 1); -- 3, 1
select nextval(' test_sequence'), generate_series(1, 1); -- 5, 1
drop sequence test_sequence;
Is there any explanation for why nextval skips a value in the second case?
By the way, if the second query is rewritten as follows, nextval again generates consecutive values:
select nextval(' test_sequence'), ind
from (select generate_series(1, 1) ind) A;
Is this a bug or a feature?
Dmitry Epstein | Developer
Allied Testing
www.alliedtesting.com
We Deliver Quality.