gotchasqlCritical
Why does Postgres generate an already used PK value?
Viewed 0 times
whypostgresusedvaluealreadygeneratedoes
Problem
I'm using Django, and every once in a while I get this error:
IntegrityError: duplicate key value violates unique constraint "myapp_mymodel_pkey"
DETAIL: Key (id)=(1) already exists.
My Postgres database does in fact have a myapp_mymodel object with the primary key of 1.
Why would Postgres attempt to use that primary key again? Or, is this most likely my application (or Django's ORM) causing this?
This issue occurred 3 more times in a row just now. What I've found is that when it does occur it happens one or more times in a row for a given table, then not again. It seems to happen for every table before it completely stops for days, happening for at least a minute or so per table when it does occur, and only happening intermittently (not all tables right away).
The fact that this error is so intermittent (happened only 3 or so times in 2 weeks - no other load on the DB, just me testing out my application) is what makes me so wary of a low-level problem.
IntegrityError: duplicate key value violates unique constraint "myapp_mymodel_pkey"
DETAIL: Key (id)=(1) already exists.
My Postgres database does in fact have a myapp_mymodel object with the primary key of 1.
Why would Postgres attempt to use that primary key again? Or, is this most likely my application (or Django's ORM) causing this?
This issue occurred 3 more times in a row just now. What I've found is that when it does occur it happens one or more times in a row for a given table, then not again. It seems to happen for every table before it completely stops for days, happening for at least a minute or so per table when it does occur, and only happening intermittently (not all tables right away).
The fact that this error is so intermittent (happened only 3 or so times in 2 weeks - no other load on the DB, just me testing out my application) is what makes me so wary of a low-level problem.
Solution
PostgreSQL will not try to insert duplicate values on its own, it is you (your application, ORM included) who does.
It can be either a sequence feeding the values to the PK set to the wrong position and the table already containing the value equal to its
The second one means debugging.
Django (or any other popular framework) doesn't reset sequences on its own - otherwise we would have similar questions every other day.
It can be either a sequence feeding the values to the PK set to the wrong position and the table already containing the value equal to its
nextval() - or simply that your application does the wrong thing. The first one is easy to fix:SELECT setval('your_sequence_name', (SELECT max(id) FROM your_table));The second one means debugging.
Django (or any other popular framework) doesn't reset sequences on its own - otherwise we would have similar questions every other day.
Code Snippets
SELECT setval('your_sequence_name', (SELECT max(id) FROM your_table));Context
StackExchange Database Administrators Q#46125, answer score: 66
Revisions (0)
No revisions yet.