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.92) (envelope-from ) id 1jACDs-0007WY-F5 for pgsql-hackers@arkaria.postgresql.org; Fri, 06 Mar 2020 12:37:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jACDq-0005RH-R5 for pgsql-hackers@arkaria.postgresql.org; Fri, 06 Mar 2020 12:36:58 +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 1jACDq-0005RA-Bv for pgsql-hackers@lists.postgresql.org; Fri, 06 Mar 2020 12:36:58 +0000 Received: from mail-lj1-x242.google.com ([2a00:1450:4864:20::242]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jACDn-0001cb-R8 for pgsql-hackers@postgresql.org; Fri, 06 Mar 2020 12:36:57 +0000 Received: by mail-lj1-x242.google.com with SMTP id 195so2060222ljf.3 for ; Fri, 06 Mar 2020 04:36:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=TNd92BZRZu9v+15wCf+lKlHi3RjT9uhe5j/eV3yVuZ4=; b=FUgPZxnI+e7YSS7fTwY6oHd2VtfitiWrOh/CMda7SrBhjxEmJiCpHq2iBa8AYhpya6 ieiiZHWzdSfm80seYMsd1klrtthWFPRT0aBYEDvkz3Rd1lM1KQdGovOHd8YLsdg09BeX bXOSblex8+a4oFXy1zhyBDMo7Z75igpec8uzfFwIsfFU4NVv1dAHuQCBooWJqK0rQjri j8rxVe/bEj7IfgU0WOCi5V5sMPax4BXSEHUsAHRFUlYEnwOg3K7okJgXy0KFOTVFPS6f 05z2xOXpnKOh6Ugy/yjhoTzI+TELOQWDx3BuqvrDcpRlZb3JwD+nAJUSyxnBzIS3mVnk i9Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=TNd92BZRZu9v+15wCf+lKlHi3RjT9uhe5j/eV3yVuZ4=; b=ZxNp8BgXZB8HCpEj+GpWbVVLcCagG/RERT7ku08sanPuwZB79wyG/IXqsE2DUC+fe9 j4PYq226VB27M3jXLwAbsLS7CmALVQ60SHA7fY23KPqN8SwMwkk6B9KxCgduwyAWkRu6 NXzSWEyH4j02iCHrgtJ0rtkyYucaQoTR5TqNDM+XSqecrjKOJ9py351xF+oWc8cTFuwA JO3dMp6HjnAXAd6NCF7Vm1jLKZhLlpeYhkp16gZiP20F3qdFKCqo3CoLxD9vCUlHj7V/ 1c3CunP39QLIcuiuWFX+8GkdzPr2zFpnL2aXfU754AMrLOruHqQVkjx8/BewdNLJGMU7 1o3w== X-Gm-Message-State: ANhLgQ1+PL54b0kUuV/Eb2+Qgaw+dj4Kfas2l6majCRT0t+70NtAPVlP VWOR9PsXP+Tvvu3aBVCI2tU= X-Google-Smtp-Source: ADFU+vuvF8cpTXW1INTOSHOG62eCU94Bm9AjbqpAkh2viuc95c9D6lJzVES2nIf2DrjEL5OMGJ9Tyw== X-Received: by 2002:a2e:9f49:: with SMTP id v9mr1904185ljk.62.1583498213994; Fri, 06 Mar 2020 04:36:53 -0800 (PST) Received: from nol (82-64-124-11.subs.proxad.net. [82.64.124.11]) by smtp.gmail.com with ESMTPSA id z6sm13437764ljz.2.2020.03.06.04.36.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Mar 2020 04:36:51 -0800 (PST) Date: Fri, 6 Mar 2020 13:36:48 +0100 From: Julien Rouhaud To: Michael Paquier Cc: Justin Pryzby , Sergei Kornilov , Peter Eisentraut , Michael Paquier , Andreas Karlsson , pgsql-hackers Subject: Re: reindex concurrently and two toast indexes Message-ID: <20200306123648.GA49431@nol> References: <20200222150657.GA54846@nol> <20200227073211.GA403330@paquier.xyz> <20200227080735.l32fqcauy73lon7o@nol> <20200303080642.GA1890@paquier.xyz> <20200303092551.GB1890@paquier.xyz> <20200304051510.GE2593@paquier.xyz> <20200305035354.GQ2593@paquier.xyz> <20200305165707.GA35281@nol> <20200306013844.GE52814@paquier.xyz> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="M9NhX3UHpAaciwkO" Content-Disposition: inline In-Reply-To: <20200306013844.GE52814@paquier.xyz> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --M9NhX3UHpAaciwkO Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Fri, Mar 06, 2020 at 10:38:44AM +0900, Michael Paquier wrote: > On Thu, Mar 05, 2020 at 05:57:07PM +0100, Julien Rouhaud wrote: > > I agree that the approach wasn't quite robust. I'll try to look at adding a > > new command for isolationtester, but that's probably not something we want to > > put in pg13? > > Yes, that's too late. > > > Note that while looking at it, I noticed another bug in RIC: > > > > [...] > > > > # reindex table concurrently t1; > > WARNING: 0A000: cannot reindex invalid index "public.t1_val_idx_ccold" concurrently, skipping > > LOCATION: ReindexRelationConcurrently, indexcmds.c:2821 > > WARNING: XX002: cannot reindex invalid index "pg_toast.pg_toast_16395_index_ccold" concurrently, skipping > > LOCATION: ReindexRelationConcurrently, indexcmds.c:2867 > > REINDEX > > # reindex index concurrently t1_val_idx_ccold; > > REINDEX > > > > That case is also fixed in this patch. > > This choice is intentional. The idea about bypassing invalid indexes > for table-level REINDEX is that this would lead to a bloat in the > number of relations to handling if multiple runs are failing, leading > to more and more invalid indexes to handle each time. Allowing a > single invalid non-toast index to be reindexed with CONCURRENTLY can > be helpful in some cases, like for example a CIC for a unique index > that failed and was invalid, where the relation already defined can be > reused. Ah I see, thanks for the clarification. I guess there's room for improvement in the comments about that, since the ERRCODE_FEATURE_NOT_SUPPORTED usage is quite misleading there. v4 attached, which doesn't prevent a REINDEX INDEX CONCURRENTLY on any invalid non-TOAST index anymore. --M9NhX3UHpAaciwkO Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="0001-Don-t-reindex-invalid-indexes-on-TOAST-tables-v4.patch" From 7bf57256192806e1caafc3dec68061e473d3978a Mon Sep 17 00:00:00 2001 From: Julien Rouhaud Date: Fri, 21 Feb 2020 20:15:04 +0100 Subject: [PATCH] Don't reindex invalid indexes on TOAST tables. Such indexes can only be duplicated leftovers of failed REINDEX CONCURRENTLY commands. As we only allow to drop invalid indexes on TOAST tables, reindexing those would lead to useless duplicated indexes that can't be dropped anymore. Reported-by: Sergei Kornilov, Justin Pryzby Author: Julien Rouhaud Reviewed-by: Michael Paquier Discussion: https://postgr.es/m/36712441546604286%40sas1-890ba5c2334a.qloud-c.yandex.net Discussion: https://postgr.es/m/20200216190835.GA21832@telsasoft.com Backpatch-through: 12 --- src/backend/catalog/index.c | 30 ++++++++++++++++++++++++++++++ src/backend/commands/indexcmds.c | 15 +++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 7223679033..d3d28df97a 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -46,6 +46,7 @@ #include "catalog/pg_depend.h" #include "catalog/pg_description.h" #include "catalog/pg_inherits.h" +#include "catalog/pg_namespace_d.h" #include "catalog/pg_opclass.h" #include "catalog/pg_operator.h" #include "catalog/pg_tablespace.h" @@ -3724,6 +3725,35 @@ reindex_relation(Oid relid, int flags, int options) { Oid indexOid = lfirst_oid(indexId); + /* + * We skip any invalid index on a TOAST table. Those can only be + * a duplicate leftover of a failed REINDEX CONCURRENTLY, and if we + * rebuild it it won't be possible to drop it anymore. + */ + if (rel->rd_rel->relnamespace == PG_TOAST_NAMESPACE) + { + HeapTuple tup; + bool skipit; + + tup = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(indexOid)); + if (!HeapTupleIsValid(tup)) + elog(ERROR, "cache lookup failed for index %u", indexOid); + + skipit = ((Form_pg_index) GETSTRUCT(tup))->indisvalid == false; + + ReleaseSysCache(tup); + + if (skipit) + { + ereport(WARNING, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot reindex invalid TOAST index \"%s.%s\", skipping", + get_namespace_name(get_rel_namespace(indexOid)), + get_rel_name(indexOid)))); + continue; + } + } + reindex_index(indexOid, !(flags & REINDEX_REL_CHECK_CONSTRAINTS), persistence, options); diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 3f3a89fe92..08d74fecd1 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -28,6 +28,7 @@ #include "catalog/pg_am.h" #include "catalog/pg_constraint.h" #include "catalog/pg_inherits.h" +#include "catalog/pg_namespace_d.h" #include "catalog/pg_opclass.h" #include "catalog/pg_opfamily.h" #include "catalog/pg_tablespace.h" @@ -2309,6 +2310,7 @@ ReindexIndex(RangeVar *indexRelation, int options, bool concurrent) Oid indOid; Relation irel; char persistence; + bool invalidtoastindex; /* * Find and lock index, and check permissions on table; use callback to @@ -2334,6 +2336,9 @@ ReindexIndex(RangeVar *indexRelation, int options, bool concurrent) */ irel = index_open(indOid, NoLock); + invalidtoastindex = (irel->rd_rel->relnamespace == PG_TOAST_NAMESPACE && + !irel->rd_index->indisvalid); + if (irel->rd_rel->relkind == RELKIND_PARTITIONED_INDEX) { ReindexPartitionedIndex(irel); @@ -2343,6 +2348,16 @@ ReindexIndex(RangeVar *indexRelation, int options, bool concurrent) persistence = irel->rd_rel->relpersistence; index_close(irel, NoLock); + if (invalidtoastindex) + { + ereport(WARNING, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot reindex invalid TOAST index \"%s.%s\", skipping", + get_namespace_name(get_rel_namespace(indOid)), + get_rel_name(indOid)))); + return; + } + if (concurrent && persistence != RELPERSISTENCE_TEMP) ReindexRelationConcurrently(indOid, options); else -- 2.20.1 --M9NhX3UHpAaciwkO--