From: Tom Lane <tgl@sss.pgh.pa.us>
To: Manuel Rigger <rigger.manuel@gmail.com>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: ALTER TABLE SET WITH OIDS fails after failed CONCURRENTLY index creation
Date: Fri, 05 Jul 2019 18:52:04 -0400
Message-ID: <5872.1562367124@sss.pgh.pa.us> (raw)
In-Reply-To: <CA+u7OA4XYhc-qyCgJqwwgMGZDWAyeH821oa5oMzm_HEifZ4BeA@mail.gmail.com>
References: <CA+u7OA4XYhc-qyCgJqwwgMGZDWAyeH821oa5oMzm_HEifZ4BeA@mail.gmail.com>
Manuel Rigger <rigger.manuel@gmail.com> 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, rigger.manuel@gmail.com
Subject: Re: ALTER TABLE SET WITH OIDS fails after failed CONCURRENTLY index creation
In-Reply-To: <5872.1562367124@sss.pgh.pa.us>
References: <CA+u7OA4XYhc-qyCgJqwwgMGZDWAyeH821oa5oMzm_HEifZ4BeA@mail.gmail.com> <5872.1562367124@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox