This implementation is inefficient because each allocated temporary ID touches the database, but it's enough to allow to re-open DBs.