Inserts fail with duplicate key after a data import
Your task
At 10:20 checkout starts failing for some customers. The order service logs show:
ERROR: duplicate key value violates unique constraint "orders_pkey" DETAIL: Key (id)=(918204) already exists.
About 70% of POST /orders calls fail; the ones that succeed get ids like 918,231 and 918,240. Nothing was deployed today, and database CPU and connections look normal. At 10:05 the data team finished moving 4,900 orders from the old marketplace database into the orders table. Their script gave each imported order an explicit id, starting just above our highest existing id (917,950 at the time) and keeping the old system's spacing, so the imported ids run from 918,001 to 925,000 with gaps. The id column is a bigint whose default is nextval('orders_id_seq'). How do you triage and fix this?
It covers
- Sequences
- Primary key
- Data import