Postgres重置主键sequence
实例
将camera_events 的主键 serial 恢复为从1开始
SELECT setval(‘serial’, 1) FROM camera_events;
SELECT pg_get_serial_sequence(‘camera_events’, ‘id’);
结果为
“public.camera_events_id_seq”
SELECT nextval(pg_get_serial_sequence(‘camera_events’, ‘id’));
结果为
1
说明不需要重置
那么
SELECT SETVAL((SELECT pg_get_serial_sequence(‘camera_events’, ‘id’)), 1, false);
就将camera_events_id_seq 恢复为1
据说
ALTER SEQUENCE "camera_events_id_seq" RESTART WITH 1;
也有类似效果