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 1hInTm-0008B4-DD for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Apr 2019 04:56:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hInTl-0008TG-40 for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Apr 2019 04:56:25 +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 1hInTj-0008T9-BT for pgsql-hackers@lists.postgresql.org; Tue, 23 Apr 2019 04:56:24 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hInTf-0001C8-1K for pgsql-hackers@lists.postgresql.org; Tue, 23 Apr 2019 04:56:22 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id C420322165; Tue, 23 Apr 2019 00:56:16 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Tue, 23 Apr 2019 00:56:16 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm1; bh=2fMXstvrMS3Ed77czuxsbfMGvTb uFuwuunCp40pQX80=; b=rgSsmihbBD6PsD4pAGGER+6u8TupiNGxMiM7cxEzmAw O+AiSy7/597rcP31vak8f5+CESuNuhV3cmQj+AQApwsQ84aSXEhRHz7LNrNdpNsL IR1vHK68I25kvpEwUEqg7oPTyOpll5861mI1Sty1aRYd4DLP3k+Frr7TZhZsBeZs pw1lRkolInUnoFm0rrIqmzGhshnHb9J2UCLF7bVmAFL7VqiNovgbQGqsA+6s2kUU HvuTaCm4YuR8nZk2lknOAwkdR3juzEMVJbn2FnhHOBkeXVrGdP6+toNaV0Cbemwg TzU8eWh8LIbzFTeRkDRnbdEoBjaaCxBePC06ymsj6cQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=2fMXst vrMS3Ed77czuxsbfMGvTbuFuwuunCp40pQX80=; b=geW5yJudMoozTi4Kyz/UHF pbFb1Ce2/+MxR4VvBThNL/QkPJia3HEgcOKKSFBQoozrDijY1SEn5L5XW4ncDpZs lDZ1ZSPJgY7OWqTaZR+nvwsi6mmwFIvCopP1/wltH8xhpc8qc4vnaImD5m0uw8A6 5FgRSNRGx/ncxQy5BOSrjtER8ySdwGi48GzIGH4wa6X8jAbeR3iXmz8u5PkHriKU dqEcRMwAgyOqTm7/q0EZMEF1N1WUvG44r8m6BYQslTlZGYmg1wIXL9FKhnUhLMeC gKJWnfEgT5OczcoGUgtcxFwmopwYa83aGwmSqT56oMmZ0c1FvsRlIwIchiBl7KZg == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduuddrgeejgdejlecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfg hrlhcuvffnffculdefhedmnecujfgurhepfffhvffukfhfgggtuggjsehgtderredttddv necuhfhrohhmpefoihgthhgrvghlucfrrghquhhivghruceomhhitghhrggvlhesphgrqh huihgvrhdrgiihiieqnecukfhppeduudekrddvgedurddvhedurddugedtnecurfgrrhgr mhepmhgrihhlfhhrohhmpehmihgthhgrvghlsehprghquhhivghrrdighiiinecuvehluh hsthgvrhfuihiivgeptd X-ME-Proxy: Received: from paquier.xyz (mp76f1fb8c.ap.nuro.jp [118.241.251.140]) by mail.messagingengine.com (Postfix) with ESMTPA id A11FBE415C; Tue, 23 Apr 2019 00:56:13 -0400 (EDT) Date: Tue, 23 Apr 2019 13:56:09 +0900 From: Michael Paquier To: Postgres hackers Cc: Masao Fujii , Tom Lane Subject: Re: REINDEX INDEX results in a crash for an index of pg_class since 9.6 Message-ID: <20190423045609.GJ2712@paquier.xyz> References: <20190418011430.GA19133@paquier.xyz> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="8m/hfNLtAhX2NvnO" Content-Disposition: inline In-Reply-To: <20190418011430.GA19133@paquier.xyz> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --8m/hfNLtAhX2NvnO Content-Type: multipart/mixed; boundary="NZiXfHLGvOGtDZMn" Content-Disposition: inline --NZiXfHLGvOGtDZMn Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Apr 18, 2019 at 10:14:30AM +0900, Michael Paquier wrote: > Doing a REINDEX TABLE directly on pg_class proves to work correctly, > and CONCURRENTLY is not supported for catalog tables. >=20 > Bisecting my way through it, the first commit causing the breakage is > that: > commit: 01e386a325549b7755739f31308de4be8eea110d > author: Tom Lane > date: Wed, 23 Dec 2015 20:09:01 -0500 > Avoid VACUUM FULL altogether in initdb. This brings down to a first, simple, solution which is to issue a VACUUM FULL on pg_class at the end of make_template0() in initdb.c to avoid any subsequent problems if trying to issue a REINDEX on anything related to pg_class, and it won't fix any existing deployments: --- a/src/bin/initdb/initdb.c +++ b/src/bin/initdb/initdb.c @@ -2042,6 +2042,11 @@ make_template0(FILE *cmdfd) * Finally vacuum to clean up dead rows in pg_database */ "VACUUM pg_database;\n\n", + + /* + * And rebuild pg_class. + */ + "VACUUM FULL pg_class;\n\n", NULL }; Now... > The reason why this does not work is that CatalogIndexInsert() tries > to do an index_insert directly on the index worked on. And the reason > why this works at table level is that we have tweaks in > reindex_relation() to enforce the list of valid indexes in the > relation cache with RelationSetIndexList(). It seems to me that the > logic in reindex_index() is wrong from the start, and that all the > index list handling done in reindex_relation() should just be in > reindex_index() so as REINDEX INDEX gets the right call. I got to wonder if this dance with the relation cache is actually necessary, because we could directly tell CatalogIndexInsert() to not insert a tuple into an index which is bring rebuilt, and the index rebuild would cause an entry to be added to pg_class anyway thanks to RelationSetNewRelfilenode(). This can obviously only happen for pg_class indexes. Any thoughts about both approaches? -- Michael --NZiXfHLGvOGtDZMn Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="reindex-pgclass-v1.patch" Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index e9399bef14..d4284ba08a 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -3502,7 +3502,6 @@ reindex_relation(Oid relid, int flags, int options) Relation rel; Oid toast_relid; List *indexIds; - bool is_pg_class; bool result; int i; =20 @@ -3538,32 +3537,8 @@ reindex_relation(Oid relid, int flags, int options) */ indexIds =3D RelationGetIndexList(rel); =20 - /* - * reindex_index will attempt to update the pg_class rows for the relation - * and index. If we are processing pg_class itself, we want to make sure - * that the updates do not try to insert index entries into indexes we - * have not processed yet. (When we are trying to recover from corrupted - * indexes, that could easily cause a crash.) We can accomplish this - * because CatalogTupleInsert/CatalogTupleUpdate will use the relcache's - * index list to know which indexes to update. We just force the index - * list to be only the stuff we've processed. - * - * It is okay to not insert entries into the indexes we have not processed - * yet because all of this is transaction-safe. If we fail partway - * through, the updated rows are dead and it doesn't matter whether they - * have index entries. Also, a new pg_class index will be created with a - * correct entry for its own pg_class row because we do - * RelationSetNewRelfilenode() before we do index_build(). - */ - is_pg_class =3D (RelationGetRelid(rel) =3D=3D RelationRelationId); - - /* Ensure rd_indexattr is valid; see comments for RelationSetIndexList */ - if (is_pg_class) - (void) RelationGetIndexAttrBitmap(rel, INDEX_ATTR_BITMAP_ALL); - PG_TRY(); { - List *doneIndexes; ListCell *indexId; char persistence; =20 @@ -3591,15 +3566,11 @@ reindex_relation(Oid relid, int flags, int options) persistence =3D rel->rd_rel->relpersistence; =20 /* Reindex all the indexes. */ - doneIndexes =3D NIL; i =3D 1; foreach(indexId, indexIds) { Oid indexOid =3D lfirst_oid(indexId); =20 - if (is_pg_class) - RelationSetIndexList(rel, doneIndexes); - reindex_index(indexOid, !(flags & REINDEX_REL_CHECK_CONSTRAINTS), persistence, options); =20 @@ -3608,9 +3579,6 @@ reindex_relation(Oid relid, int flags, int options) /* Index should no longer be in the pending list */ Assert(!ReindexIsProcessingIndex(indexOid)); =20 - if (is_pg_class) - doneIndexes =3D lappend_oid(doneIndexes, indexOid); - /* Set index rebuild count */ pgstat_progress_update_param(PROGRESS_CLUSTER_INDEX_REBUILD_COUNT, i); @@ -3626,9 +3594,6 @@ reindex_relation(Oid relid, int flags, int options) PG_END_TRY(); ResetReindexPending(); =20 - if (is_pg_class) - RelationSetIndexList(rel, indexIds); - /* * Close rel, but continue to hold the lock. */ diff --git a/src/backend/catalog/indexing.c b/src/backend/catalog/indexing.c index 0c994122d8..d102fcecae 100644 --- a/src/backend/catalog/indexing.c +++ b/src/backend/catalog/indexing.c @@ -114,6 +114,22 @@ CatalogIndexInsert(CatalogIndexState indstate, HeapTup= le heapTuple) if (!indexInfo->ii_ReadyForInserts) continue; =20 + /* + * If trying to insert a tuple for an index which is being rebuilt, + * ignore it. This can happen only for an index related to pg_class. + * + * A REINDEX being transaction-safe, it is fine to fail partway as the + * inserted rows are dead and it doesn't matter whether they have index + * entries. A new pg_class index will be created with a correct entry + * for its own pg_class row because we do RelationSetNewRelfilenode() + * before we do index_build() in reindex_index(). + */ + if (ReindexIsProcessingIndex(RelationGetRelid(relationDescs[i]))) + { + Assert(RelationGetRelid(heapRelation) =3D=3D RelationRelationId); + continue; + } + /* * Expressional and partial indexes on system catalogs are not * supported, nor exclusion constraints, nor deferred uniqueness diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/r= elcache.c index bab59f16e6..e5cb9eb916 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -4544,52 +4544,6 @@ insert_ordered_oid(List *list, Oid datum) return list; } =20 -/* - * RelationSetIndexList -- externally force the index list contents - * - * This is used to temporarily override what we think the set of valid - * indexes is (including the presence or absence of an OID index). - * The forcing will be valid only until transaction commit or abort. - * - * This should only be applied to nailed relations, because in a non-nailed - * relation the hacked index list could be lost at any time due to SI - * messages. In practice it is only used on pg_class (see REINDEX). - * - * It is up to the caller to make sure the given list is correctly ordered. - * - * We deliberately do not change rd_indexattr here: even when operating - * with a temporary partial index list, HOT-update decisions must be made - * correctly with respect to the full index set. It is up to the caller - * to ensure that a correct rd_indexattr set has been cached before first - * calling RelationSetIndexList; else a subsequent inquiry might cause a - * wrong rd_indexattr set to get computed and cached. Likewise, we do not - * touch rd_keyattr, rd_pkattr or rd_idattr. - */ -void -RelationSetIndexList(Relation relation, List *indexIds) -{ - MemoryContext oldcxt; - - Assert(relation->rd_isnailed); - /* Copy the list into the cache context (could fail for lack of mem) */ - oldcxt =3D MemoryContextSwitchTo(CacheMemoryContext); - indexIds =3D list_copy(indexIds); - MemoryContextSwitchTo(oldcxt); - /* Okay to replace old list */ - list_free(relation->rd_indexlist); - relation->rd_indexlist =3D indexIds; - - /* - * For the moment, assume the target rel hasn't got a pk or replica index. - * We'll load them on demand in the API that wraps access to them. - */ - relation->rd_pkindex =3D InvalidOid; - relation->rd_replidindex =3D InvalidOid; - relation->rd_indexvalid =3D 2; /* mark list as forced */ - /* Flag relation as needing eoxact cleanup (to reset the list) */ - EOXactListAdd(relation); -} - /* * RelationGetPrimaryKeyIndex -- get OID of the relation's primary key ind= ex * diff --git a/src/include/utils/relcache.h b/src/include/utils/relcache.h index 809d6aa123..364495a5f0 100644 --- a/src/include/utils/relcache.h +++ b/src/include/utils/relcache.h @@ -66,9 +66,6 @@ extern void RelationGetExclusionInfo(Relation indexRelati= on, Oid **procs, uint16 **strategies); =20 -extern void RelationSetIndexList(Relation relation, - List *indexIds); - extern void RelationInitIndexAccessInfo(Relation relation); =20 /* caller must include pg_publication.h */ --NZiXfHLGvOGtDZMn-- --8m/hfNLtAhX2NvnO Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAly+mukACgkQnvQgOdby QH0h3w/6Ar5UpmLuzd3hdnjOgOCX0slIVq9PZbAwQ+29EjbHDNQyl0LoxxIdQYpa p8+ZBvtGEvqksKHif7Er2s2q3tpXWWUK8ttgjTaroalrwctbaQFoPYk9ll53QqXH w9SrXB1ZSirD98kbnASo5Fyra4M1/HJxPXcw/qKk8aaZZwrFLjNmycDB49i8RIXi zQG9hYyDoGkYDXLlCHDSixs8VUVOFHCzVrdIyCul6pGLfRXekmGfK8dFjKPhE8He YOspO+nubsGqh2Agv99qAzlaHxk4y+oR2SDjfEmZa4Tp70kj/p4QI/ctRRwvCcdF Y6nbDBAaIuDCAnd1y+VksdX5DzgYATWhjjXXd5BfbpmQMPrJwIGpv9c3LvX0Ddwy 3/YqbhDjV6wGN7BbuPQRJ0atBMjsT2ErKiPipy6AGmLBQALzltHdxRLmifrEfVag f3vVloUsnVMyxei6PpyizGxH35gd83ZxJNPQMa5R8iAW6SHmWy+i2dVCUD4VPCv0 cqMdNIJDm0+CHNFHM0iblNkMl45alvCvCulurFK38uezcageembqkQlOrdRArxOg kepGiwMduwbAoHl0d1NozNZ/+RbcHEhGDrR1m4+8rCV+uBa85Cks9hGkqPMUpp1s rEgYi9ahNOtuH+y+CENjZrg8J+1tf7ko60Up3EO2ABG6KDqfZlI= =OziF -----END PGP SIGNATURE----- --8m/hfNLtAhX2NvnO--