[ACCEPTED]-Postgresql - Using subqueries with alter sequence expressions-alter

Accepted answer
Score: 92

I don't believe you can do it like that 4 but you should be able to use the setval 3 function direction which is what the alter 2 does.

select setval('sequenceX', (select max(table_id)+1 from table), false)

The false will make it return the next 1 sequence number as exactly what is given.

Score: 8

In addition if you have mixed case object 3 names and you're getting an error like this:

ERROR: relation "public.mytable_id_seq" does not exist

... the 2 following version using regclass should 1 be useful:

select setval('"public"."MyTable_Id_seq"'::regclass, (select MAX("Id") FROM "public"."MyTable"))

More Related questions