in reply to Cycle through primary key with DBI

My thought is to do something I usually hate doing (because usually 'sequences' or 'serial fields' do the job but not in this case). Use a control table which just stores the 'next' id number to be used and insert one row with the 'first' id number.

Then when you want to insert into your table, you'll first need to lock the control table (or just the record, I'm not sure if a table lock attempt will block or immediately fail in PostgreSQL, either way you could work it out) select the 'next' id number from the control table, update the control table, unlock the control table, delete from your table, then insert into your table. I'd probably do this all in a transaction so we can rollback if any of it fails.

I'd just do the 'delete' from your table every time, because soon you'll have your thousand records and it won't be worth checking to see if you 'should' delete.