1

After restoring database I run next query

SELECT nextval('table_id_seq') 

and I must get max id + 1 something like (select max(id) + 1 from table), but instead I get just max id next time I call it result is correct. This issue happens only to two tables the rest works okay. I use PostgreSQL 10.

Any ideas what it can be.

Roman
  • 11
  • 4
  • Why do you think `nextval()` should return `max() + 1`? –  Mar 13 '18 at 18:27
  • Because this is what described in documentation https://www.postgresql.org/docs/10/static/functions-sequence.html – Roman Mar 13 '18 at 18:53
  • 1
    This might actually be a little restore bug in postgres and you need to reset the serial/sequence. https://wiki.postgresql.org/wiki/Fixing_Sequences – Twelfth Mar 13 '18 at 19:57

1 Answers1

2

Check "last value" of sequence using below query:

select * from sequence_name

If the last value does not match with table max value then use alter sequence and restart the last value as table max value.

Anoop Sharma
  • 381
  • 3
  • 3