Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wVSgX-001yon-25 for pgsql-bugs@arkaria.postgresql.org; Fri, 05 Jun 2026 11:25:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wVSgV-00BwMt-2X for pgsql-bugs@arkaria.postgresql.org; Fri, 05 Jun 2026 11:25:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wVSgV-00BwMl-1C for pgsql-bugs@lists.postgresql.org; Fri, 05 Jun 2026 11:25:55 +0000 Received: from mail.nimrod.no ([195.139.160.70]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wVSgT-00000001POc-0jv7 for pgsql-bugs@lists.postgresql.org; Fri, 05 Jun 2026 11:25:55 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nimrod.no; s=n20260101; t=1780658752; bh=ji5JNBJMzlt2y9xnDOGPCPTnenXPdi8cnxrdCQjVD+s=; h=Date:From:To:Subject:From; b=ENhuQ+JL5ikExKcxHo1kUKLFKRkyntXN328PJkZQxI+DHhCpkPiiH+pcUgZADw5da WT1xA2P6y5zo501lK32K7IsgkAlCfpXRiwNe2fSJf5IsoLmwiD9J3F3GGqjd5/JYi8 3uFqecZpoFS/yTiUAjaJFPDdDM4ux014c9j0bjPp0tV/yCV7TklvT7UUudhtufG2mX 3tjBFrqjLJ4WgMX8h60zXNBP33uw4iopi6voingvcURKDg/nyEtz0HMbUuHJoPdqXg kd3B7MLlk4H9YAtmD2T9lSWeKzuy5BnugVyypTHrAVVPMZGyKeWtNJuXw/wMnWC0bQ P6sZD0qyqrewA== MIME-Version: 1.0 Date: Fri, 05 Jun 2026 13:25:52 +0200 From: Dag Lem To: pgsql-bugs@lists.postgresql.org Subject: REINDEX (CONCURRENTLY) TABLE handles DEFERRED constraints as IMMEDIATE while processing Message-ID: X-Sender: dag@nimrod.no Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, The following bug was detected on PostgreSQL 16.14, and I was able to reproduce it on PostgreSQL 18.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 16.1.1 20260501 (Red Hat 16.1.1-1), 64-bit We have a database table with some 150 million rows, with several indexes and a couple of constraints on the form CONSTRAINT uq_constraint_name UNIQUE (...) DEFERRABLE INITIALLY DEFERRED. While processing, "REINDEX (CONCURRENTLY) TABLE table_name" temporarily treats the DEFERRED constraints as IMMEDIATE, causing transactions to fail with errors on the form 'ERROR: duplicate key value violates unique constraint "uq_constraint_name"'. To see what could cause this issue, I repeatedly did the SELECT below while REINDEX (CONCURRENTLY) was working, while also doing transactions which depend on constraints being DEFERRED. SELECT i.indexrelid, c.relname, i.indisunique, i.indimmediate, i.indisvalid, i.indisready FROM pg_class c JOIN pg_index i ON i.indexrelid = c.oid WHERE c.relkind = 'i' AND c.relname LIKE '%_ccnew' ORDER BY i.indexrelid; This yields output on the form below. indexrelid | relname | indisunique | indimmediate | indisvalid | indisready ------------+-------------------------------------------+-------------+--------------+------------+------------ 124315 | ix_non_unique_index_1_ccnew | f | t | f | t 124316 | ix_non_unique_index_2_ccnew | f | t | f | t ... 124342 | pk_primary_key_ccnew | t | t | f | t 124343 | uq_unique_deferred_constraint_1_ccnew | t | t | f | f 124344 | uq_unique_deferred_constraint_2_ccnew | t | t | f | f 124345 | pg_toast_71419_index_ccnew | t | t | f | f Note how the new indexes backing the DEFERRED constraints are marked with indimmediate = true(!), and that the progress can be followed by looking at the indisready column. As soon as construction of the first index backing a UNIQUE DEFERRED is completed (indisready = true), transactions depending on DEFERRED constraints will start to fail, and will continue to do so until REINDEX (CONCURRENTLY) has completed. Could this be an oversight wrt. how indexes and (deferred) constraints are related? Note how it is currently not possible to safely add a UNIQUE DEFERRED constraint following the example in https://www.postgresql.org/docs/18/sql-altertable.html CREATE UNIQUE INDEX CONCURRENTLY dist_id_temp_idx ON distributors (dist_id); ALTER TABLE distributors DROP CONSTRAINT distributors_pkey, ADD CONSTRAINT distributors_pkey PRIMARY KEY USING INDEX dist_id_temp_idx; For this to work safely with UNIQUE DEFERRED constraints, I assume it would be necessary to add an option to CREATE INDEX to make an index DEFERRED. Best regards, Dag Lem