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 1ifW98-0008H8-FD for pgsql-bugs@arkaria.postgresql.org; Thu, 12 Dec 2019 21:37:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ifW97-0003CZ-7s for pgsql-bugs@arkaria.postgresql.org; Thu, 12 Dec 2019 21:37:17 +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 1ifW96-0003CS-9Z for pgsql-bugs@lists.postgresql.org; Thu, 12 Dec 2019 21:37:17 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ifW92-0001fL-UD for pgsql-bugs@lists.postgresql.org; Thu, 12 Dec 2019 21:37:15 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 4B2B32274C; Thu, 12 Dec 2019 16:37:11 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Thu, 12 Dec 2019 16:37:11 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm3; bh=nH62xlRoF1K3laRyeoYJrJY/BX2 tVAH845E1JwSkTrs=; b=fqA9/DXqIMtBy4VEOwxG1s2jZ64Nk3Rkh2ZgzX14wf1 BW8eZTg7l2yvEpPwDCDjGXFfIZT0sSd9Whkcm1t+4hV1dysOB8ig0VCn8RCp1Nak t1pjDen6gjVpSG8IITHhHfOZg7FvzDOkR22yH9dGkTvJNrJKCh+shB7AdV1ZZtGA F1sEOdj+yIl8ppuc6SGbbWE+Ulwxw0Db6zknyPfdwFtGRm+3y03djA31bN1r4w1Q HK1I/NwwSAaqgjF0PMeT/tv4DLxTGJ2L7oeprM8xfsm6og/gem0PxmWk4HgqvDGT cfuL33e25m6J6eB5i+gm/i/G7R/yCsRIEdoO/S5stMw== 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=fm1; bh=nH62xl RoF1K3laRyeoYJrJY/BX2tVAH845E1JwSkTrs=; b=ptI4mwZS93ZiUFvaFw4Rdr qXhfiHfRy4svLQzr2sEzUuTxFMfyWzOBhe8eViGXvm164VBXGoaUepxRdpgxBWuR ieuz1YKDyjAx2ZCqQXXoCHbzeahrPmhVnkhIl4vkhi89tz2PzKq0zUvZC//dAZPB LbtbPgBffh+gec7qOBOnbiLxe5k29eJwRw2htvwEKsxE66kPKN5gWbryfv+9BUr0 KJYB6IZ6SReDB/QB5AC/cleW7tbnjbmnOW2W7FENtdycui7eIcKe9dZ7HCKQFROM 2eDkK7Hvt4ve0/lBdRaLMyo7HUcQHiEJbquPRmtIG7r5FUvBHJ1JRNbqz2/vgJnA == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrudeljedgudehudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvuffkfhggtggujgesthdtredttddtvdenucfhrhhomheptehnughr vghsucfhrhgvuhhnugcuoegrnhgurhgvshesrghnrghrrgiivghlrdguvgeqnecukfhppe duieejrddvvddtrddvjedrudejjeenucfrrghrrghmpehmrghilhhfrhhomheprghnughr vghssegrnhgrrhgriigvlhdruggvnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from intern.anarazel.de (unknown [167.220.27.177]) by mail.messagingengine.com (Postfix) with ESMTPA id BFB7730602EE; Thu, 12 Dec 2019 16:37:10 -0500 (EST) Date: Thu, 12 Dec 2019 13:37:09 -0800 From: Andres Freund To: Michael Paquier Cc: Tom Lane , Manuel Rigger , PostgreSQL mailing lists Subject: Re: REINDEX CONCURRENTLY unexpectedly fails Message-ID: <20191212213709.neopqccvdo724eha@alap3.anarazel.de> References: <20191113163851.e2enkdpvd3nrobst@alap3.anarazel.de> <19198.1573663534@sss.pgh.pa.us> <20191114080855.GG1910@paquier.xyz> <3808.1573754033@sss.pgh.pa.us> <20191115024512.GE1849@paquier.xyz> <20191115025412.p3beiflrd6bx6wgf@alap3.anarazel.de> <20191115032141.GH1849@paquier.xyz> <20191115080706.GI1849@paquier.xyz> <20191120013649.2xfr2dnznyjgluch@alap3.anarazel.de> <20191120035408.GA4243@paquier.xyz> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20191120035408.GA4243@paquier.xyz> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, On 2019-11-20 12:54:08 +0900, Michael Paquier wrote: > On Tue, Nov 19, 2019 at 05:36:49PM -0800, Andres Freund wrote: > >> Please note that I have not changed index_drop() for the concurrent > >> mode. Per my checks this does not actually cause any direct bugs as > >> this code path just takes care of dropping the dependencies with the > >> index. There could be an argument for changing that on HEAD though, > >> but I am not sure that this is worth the complication either. > > > > I'm extremely doubtful this works correctly. E.g., what prevents the > > tids for index_concurrently_set_dead() from being recycled and pointing > > to a different row after one of the internal transactions? > > ON COMMIT DELETE ROWS does a physical truncation of the relation > files. And as DROP INDEX CONCURRENTLY cannot be run in a transaction > block, you would never face a case where you have no past TIDs which > could be referred to when setting the index as invalid. It's probably not reachable, but it strikes me as really fragile and dangerous. If e.g. somehow ON COMMIT DROP tables could exist when DROP CONCURRENTLY were run, the index_concurrently_set_dead() could very well target a row that's since been deleted in an earlier transaction. > Now I don't actually object to enforce the non-concurrent path in > index_drop() for *all* temporary relations. Anyway, that makes sense > in itself on performance grounds, similarly to the create path, so did > that by enforcing the flag in index_drop() (doDeletion would be > tempting but I took the problem at its root). And added some tests > for the drop path and an extra assertion. Cool. I still think we'd be well served to add a few CheckTableNotInUse() type checks... > >> +-- Temporary tables with concurrent builds > >> +CREATE TEMP TABLE concur_temp (f1 int, f2 text); > >> +INSERT INTO concur_temp VALUES (1, 'foo'), (2, 'bar'); > >> +CREATE INDEX CONCURRENTLY concur_temp_ind ON concur_temp(f1); > >> +DROP TABLE concur_temp; > >> +-- On-commit actions > >> +CREATE TEMP TABLE concur_temp (f1 int, f2 text) > >> + ON COMMIT DELETE ROWS; > >> +INSERT INTO concur_temp VALUES (1, 'foo'), (2, 'bar'); > >> +CREATE INDEX CONCURRENTLY concur_temp_ind ON concur_temp(f1); > >> +DROP TABLE concur_temp; > >> -- > > > > I'd also add a test for ON COMMIT DROP. > > Considered that, but ON COMMIT DROP does not make sense because it > requires a transaction context, which is why I did not add one. And > it seems to me that there is not much value to just check after CIC or > REINDEX's restriction to not run in a transaction block? I added > tests for these two, but I am of the opinion that they don't bring > much. I think because CIC now falls back to non-concurrent mode, it's worthwhile to exercise this path. It seems far from unlikely that the code gets moved around enough that suddenly CIC is allowed in transactions when targetting temp tables. > >> diff --git a/doc/src/sgml/ref/reindex.sgml b/doc/src/sgml/ref/reindex.sgml > >> index 10881ab03a..e5d2b1a06e 100644 > >> --- a/doc/src/sgml/ref/reindex.sgml > >> +++ b/doc/src/sgml/ref/reindex.sgml > >> @@ -162,6 +162,9 @@ REINDEX [ ( VERBOSE ) ] { INDEX | TABLE | SCHEMA | DATABASE | SYSTEM } [ CONCURR > >> — see >> endterm="sql-reindex-concurrently-title"/>. > >> > >> + > >> + This option is ignored for temporary relations. > >> + > >> > >> > >> > > > > I think either this needs to be more verbose, explaining that there's no > > harm from the option being ignored, or it should be ignored as an > > implementation detail. > > I think that documenting it is good for the end-user as well. Why? > Just attempted something in the updated version for all the sections > of the docs involved. -- Michael > diff --git a/src/include/catalog/index.h b/src/include/catalog/index.h > index 1113d25b2d..04d3d4826f 100644 > --- a/src/include/catalog/index.h > +++ b/src/include/catalog/index.h > @@ -113,6 +113,8 @@ extern bool CompareIndexInfo(IndexInfo *info1, IndexInfo *info2, > > extern void BuildSpeculativeIndexInfo(Relation index, IndexInfo *ii); > > +extern bool RelationSupportsConcurrently(char relpersistence); > + > extern void FormIndexDatum(IndexInfo *indexInfo, > TupleTableSlot *slot, > EState *estate, > diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c > index 67f637de11..0012ebf69c 100644 > --- a/src/backend/catalog/index.c > +++ b/src/backend/catalog/index.c > @@ -2017,6 +2017,13 @@ index_drop(Oid indexId, bool concurrent, bool concurrent_lock_mode) > LOCKTAG heaplocktag; > LOCKMODE lockmode; > > + /* > + * Enforce non-concurrent drop if the relation does not support this > + * option. > + */ > + if (!RelationSupportsConcurrently(get_rel_persistence(indexId))) > + concurrent = false; > + Echoing Alvaro, I'm less than convinced by this name. > + /* > + * Enforce non-concurrent build if the relation does not support this > + * option. > + */ > + if (!RelationSupportsConcurrently(rel->rd_rel->relpersistence)) > + stmt->concurrent = false; > + Copying this to some, but not all, the places where RelationSupportsConcurrently() is called doesn't seem helpful... > --- a/doc/src/sgml/ref/create_index.sgml > +++ b/doc/src/sgml/ref/create_index.sgml > @@ -129,6 +129,11 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ [ IF NOT EXISTS ] — see endterm="sql-createindex-concurrently-title"/>. > > + > + This option is ignored for temporary relations as such relations > + are assigned to the session using them, so they are not subject to > + problems with concurrent locking issues. > + > > This strikes me as confusing to users. They won't understand "concurrent locking issues" (nor am I sure that I really know what precisely that means). And "temporary relations as such relations are assigned" is also confusing. If we want to add docs, I'd say at most something like "For temporary tables index creation is always non-concurrent, as no other session can access them, and non-concurrent index creation is cheaper.". Greetings, Andres Freund