-- Phase 29 (BUG-20): compact trip IDs and stop BIGSERIAL inflation. -- trips.id has no foreign-key references (deleted_trip_markers keys on -- (device_id, start_time)), so renumbering is purely cosmetic. BEGIN; -- Shift all ids up first so the compact ids (1..N) can't collide with any -- id that still exists during the renumber. UPDATE trips SET id = id + 1000000; -- Renumber compactly in start_time order. WITH renumbered AS ( SELECT id, row_number() OVER (ORDER BY start_time, id) AS new_id FROM trips ) UPDATE trips t SET id = r.new_id FROM renumbered r WHERE t.id = r.id; -- Reset the sequence to the real max id so the next insert gets MAX(id) + 1. SELECT setval( pg_get_serial_sequence('trips', 'id'), (SELECT COALESCE(MAX(id), 0) FROM trips) ); COMMIT;