Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ipfKr-0005e7-EB for pgsql-general@arkaria.postgresql.org; Thu, 09 Jan 2020 21:27:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ipfKp-00069K-1z for pgsql-general@arkaria.postgresql.org; Thu, 09 Jan 2020 21:27:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ipfKo-00069D-Ok for pgsql-general@lists.postgresql.org; Thu, 09 Jan 2020 21:27:18 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ipfKf-0006V4-GO for pgsql-general@lists.postgresql.org; Thu, 09 Jan 2020 21:27:18 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id 009LR6GN014929; Thu, 9 Jan 2020 16:27:06 -0500 From: Tom Lane To: Matthew Woodcraft cc: pgsql-general@lists.postgresql.org Subject: Re: INSERT locking order In-reply-to: <1578602959.23rey5xcjc.astroid@golux.woodcraft.me.uk> References: <1578602959.23rey5xcjc.astroid@golux.woodcraft.me.uk> Comments: In-reply-to Matthew Woodcraft message dated "Thu, 09 Jan 2020 20:50:29 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <14927.1578605226.1@sss.pgh.pa.us> Date: Thu, 09 Jan 2020 16:27:06 -0500 Message-ID: <14928.1578605226@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Matthew Woodcraft writes: > In an INSERT, are the rows guaranteed to be inserted in the order that > the source query returns its rows, for locking purposes? I dunno that we "guarantee" that, but it's hard to see why the code would behave any differently, at present. > and I run two concurrent copies of > INSERT INTO foo (SELECT n FROM ... ORDER BY n); > is there any guarantee that I'll get a unique constraint violation > rather than a deadlock? Well, the issue there is not about the physical insertion order but the order in which the uniqueness checks happen. I think you'd be all right with a traditional-style PG index, but maybe not with a deferrable unique constraint. regards, tom lane