Move sql.save to before max_id update

This fixes a theoretical bug whereby we might update the max_id before the
data has made it in to the database.