16

I want to add a sequence to a column that might already have data, so I'm trying to start it beyond whatever's already there. Assuming there already is data, I would like to have done it this way:

CREATE SEQUENCE my_sequence MINVALUE 1000000 START
    (SELECT MAX(id_column) FROM my_table) OWNED BY my_table.id_column;

but it keeps dying at ( claiming syntax error. It's like the start value has to be cold hard numbers--nothing symbolic.

Of course, an even better solution would be if the sequence could be intelligent enough to avoid duplicate values, since id_column has a unique constraint on it--that's why I'm doing this. But from what I can tell, that's not possible.

I also tried skipping the START and then doing:

ALTER SEQUENCE my_sequence RESTART WITH (SELECT max(id_column)+1 FROM my_table);

but, again, it doesn't seem like to symbolic start values.

I'm running PostgreSQL 9.4 but some of our customers are using stuff as primitive as 8.3.

3 Answers 3

32

You can't specify a dynamic value for the start value.

But you can set the value once the sequence is created:

CREATE SEQUENCE my_sequence MINVALUE 1000000 OWNED BY my_table.id_column;
select setval('my_sequence',  (SELECT MAX(id_column) FROM my_table));
3
  • Amazing... I was about to edit this to add MAX(id_column)+1 so that the sequence would start beyond the max, but when I tested it, it really did start from 1 after the largest id_column! So I guess START 1000000 really means "start at 1000001".
    – Opux
    Commented May 6, 2016 at 20:39
  • I can't confirm what Opux said; I had to do MAX + 1.
    – Noumenon
    Commented Nov 29, 2019 at 14:45
  • 3
    @Noumenon that likely had to do with the sequence's is_called column. If false, the next call to nextval() returns the current value of the sequence. If is_called is true, the sequence will be incremented before returning the new value. When calling setval, you can specify a third param to set is_called, but it defaults to true. In other words, setval('my_sequence', (SELECT MAX(id_column) FROM my_table)); should always cause future defaults to "start from 1 after the largest id_column". This seems true since v7.2. postgresql.org/docs/7.2/functions-sequence.html
    – soupdog
    Commented Aug 12, 2021 at 4:09
9

You can restore you sequence by request:

select setval('my_sequence', (SELECT MAX(id_column) FROM my_table));

Applicable for Postgres 9.2.

0

Just because I was struggling with a slight variation in use case to the accepted answers here and experiencing an error telling me that setval did not exist, thought I'd share in case others had the same.

I needed to set the value of my sequence to the max value in an id column, but I also wanted to combine that with a default starting value if there were no rows.

To do this I used coalesce combined with max like this:

select setval('sequence', cast((select coalesce(max(id),1) from table) as bigint));

The catch here was using cast with the select, without that, you get an error something like:

ERROR:  function setval(unknown, double precision) does not exist
LINE 1: select setval('sequence', (select coalesce(MAX(i...

HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.