Upsert with autoincrement 3

This is rebased version of upset-with-autoincrement-2,
which has been reviewed in #418.