- Goal: We have an app that is used to store attendance data. It is backed by a table in the database, named attendance. We have a front end that accepts values to be entered into the database table. We were hit with the error Duplicate key value violates unique constraint "attendance_final_pkey" recently.
On debugging and looking at the logs, we realized the primary key of the table, was being incremented randomly, (ie. not just by 1 on every insert. It would have random jumps)
In the screenshot 1 below, we entered the data on Sept 13th, notice the jump from 5 to 842 - I'm not sure why it chose to jump to 842.
In screenshot2 below, on Oct 5th 9:24AM when the attendance was taken, it decided to start incrementing from 838 .. eventually hitting the collision with 842, which started giving us the duplicate pkey error
around 1:14 we reset the sequence val to pick max, it it starts from 1050
This is the set of commands we ran to reset the sequence val:
CREATE SEQUENCE attendance_id_seq;
ALTER TABLE attendance ALTER COLUMN id SET DEFAULT nextval('attendance_id_seq');
SELECT setval('attendance_id_seq', (SELECT MAX(id) FROM attendance));
Now after we reset the sequence it seems to be ok, but we aren't 100% sure if we will hit that error again.
So my question is, what typically causes that error? We don't manually touch the primary keys. We don't manually enter records into the table.
Is there a way to figure out what kind of auto increment Primary key policy we set when the table was created?