`curs.copy_from(s, tablename)` and `curs.execute( "select setval(...))` should have some kind of lock, we don't want a different process to change the ids. Or better, still, we could set the serial number at the correct value by *before* importing the data,
by performing some arithmetic.