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 1hjX4K-0005xZ-Nc for pgsql-bugs@arkaria.postgresql.org; Fri, 05 Jul 2019 22:52:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hjX4J-0007Kw-34 for pgsql-bugs@arkaria.postgresql.org; Fri, 05 Jul 2019 22:52:39 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hjX4I-0007DE-Oj for pgsql-bugs@lists.postgresql.org; Fri, 05 Jul 2019 22:52:38 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hjX4F-00025P-02 for pgsql-bugs@lists.postgresql.org; Fri, 05 Jul 2019 22:52:36 +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 x65Mq457005873; Fri, 5 Jul 2019 18:52:04 -0400 From: Tom Lane To: Manuel Rigger cc: pgsql-bugs@lists.postgresql.org Subject: Re: ALTER TABLE SET WITH OIDS fails after failed CONCURRENTLY index creation In-reply-to: References: Comments: In-reply-to Manuel Rigger message dated "Tue, 02 Jul 2019 12:06:53 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <5871.1562367124.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Fri, 05 Jul 2019 18:52:04 -0400 Message-ID: <5872.1562367124@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Manuel Rigger writes: > Consider the following example: > CREATE TABLE t0(c0 INT); > INSERT INTO t0(c0) VALUES(0), (0); > CREATE UNIQUE INDEX CONCURRENTLY i0 ON t0(c0); > ALTER TABLE t0 SET WITH OIDS; -- expected: no error, actual: ERROR: > could not create unique index "i0" DETAIL: Key (c0)=(0) is > duplicated. > The concurrent creation of the UNIQUE INDEX fails, which is expected. > However, I would expect that the index is then ignored and that the > ALTER TABLE is unaffected. I'm afraid your expectations are too high. This isn't a bug, although the documentation for CREATE INDEX CONCURRENTLY perhaps needs to call out the hazard more explicitly. The docs already say Another caveat when building a unique index concurrently is that the uniqueness constraint is already being enforced against other transactions when the second table scan begins. This means that constraint violations could be reported in other queries prior to the index becoming available for use, or even in cases where the index build eventually fails. Also, if a failure does occur in the second scan, the “invalid” index continues to enforce its uniqueness constraint afterwards. Basically, once an invalid index is in place, it's going to cause you problems until you drop it or make it valid. This is the price of not taking any exclusive locks in CREATE INDEX CONCURRENTLY: we don't really have the option to clean up nicely after failure, because that'd require an exclusive table lock :-( regards, tom lane