Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l5fv6-0004Y6-D1 for pgsql-hackers@arkaria.postgresql.org; Sat, 30 Jan 2021 02:23:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l5fv4-0007I3-NF for pgsql-hackers@arkaria.postgresql.org; Sat, 30 Jan 2021 02:23:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l5fv4-0007Hq-F3 for pgsql-hackers@lists.postgresql.org; Sat, 30 Jan 2021 02:23:26 +0000 Received: from wnew2-smtp.messagingengine.com ([64.147.123.27]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l5fuv-0002vp-Pa for pgsql-hackers@lists.postgresql.org; Sat, 30 Jan 2021 02:23:24 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailnew.west.internal (Postfix) with ESMTP id 21D90D58; Fri, 29 Jan 2021 21:23:09 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Fri, 29 Jan 2021 21:23:09 -0500 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=fm3; bh=nn65kf5IZPVZDttBW8gNeIIz4zp IqWfbMHwqTiqH71E=; b=MjJ711ZBlyA5+DZ627ESk3GK7i517OTmnIChpjuJgL6 iWC9taKBa/hXKGuoc/VZ1KCPXBbRaxIb0Ss5Me23LKzG3CnN2wQVhsE2cOhwNx/a Zxlt+TuY9A8jU5wtdtu3CBXRx+hxhhlpgeRpd/hWlI3kfNvDpJwftMqWU1KTKz2p vPd6x+5R7oCiYuQG5oM15R40Koh6cIazs8RfyEsU7lytBz/fdNwBWWCwcciDWti8 TP7EE65jU3dfJDIV3Nk6yzaWezWpfx+kziLq+hOd3zHaqy+IJ5mxejEbpt382YhH u+XzCNPtQIa/LpiPdiW9zBCfzkMO6Tp1ae/SdRw9XCw== 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=nn65kf 5IZPVZDttBW8gNeIIz4zpIqWfbMHwqTiqH71E=; b=Mr6CNhP0OiXnj+SM+CGDSL Ct3sxVzrXhTiDSc4hqxjpqpCY45c5DG499hQezapMWyQGNlV2xxM8ETR5gz0hu4g JwribQe3vhK/djWJHOlQP0Y1WkkoEqn0H3hd3EaTh9btwOdq8PaEkTRnlYgeRcDR JUMHIQG/Urs3hl50sOiz6kDqkR6dhEI4nvygVkMDKQhf1jjvOGkZtFPrwFIJrPq3 y+Vq2mWLUeJy67AOWtN8+6GPAItlDAr2JFZ2Xi2zLJSv3ZvJYAtn59gc/PcyIPey smdte7ZucFXz49abWOEmxKSGzzfskdjO1NrgfmYfwCaFNWTFVoUeHpUatLSnbpkw == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrfeefgdeggecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenfg hrlhcuvffnffculdejtddmnecujfgurhepfffhvffukfhfgggtuggjsehgtderredttddv necuhfhrohhmpefoihgthhgrvghlucfrrghquhhivghruceomhhitghhrggvlhesphgrqh huihgvrhdrgiihiieqnecuggftrfgrthhtvghrnhepvdegudeuhfdtueeltedtveejheeh ieevueeigeelteegleejleeiueeiheegvefhnecukfhppeduuddurddutddvrddukedtrd dukeehnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhep mhhitghhrggvlhesphgrqhhuihgvrhdrgiihii X-ME-Proxy: Received: from paquier.xyz (ee0822lan1.rev.em-net.ne.jp [111.102.180.185]) by mail.messagingengine.com (Postfix) with ESMTPA id 8252724005A; Fri, 29 Jan 2021 21:23:04 -0500 (EST) Date: Sat, 30 Jan 2021 11:23:01 +0900 From: Michael Paquier To: Alexey Kondratov Cc: Alvaro Herrera , Justin Pryzby , Peter Eisentraut , Masahiko Sawada , Steve Singer , pgsql-hackers@lists.postgresql.org, Robert Haas , Alexander Korotkov , Masahiko Sawada , Jose Luis Tallon Subject: Re: Allow CLUSTER, VACUUM FULL and REINDEX to change tablespace on the fly Message-ID: References: <20210127213658.GA736@alvherre.pgsql> <55c6c1526b94d2caf54c33262fe00674@postgrespro.ru> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="3ijwfRZFbUKs6mY+" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --3ijwfRZFbUKs6mY+ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Jan 29, 2021 at 08:56:47PM +0300, Alexey Kondratov wrote: > On 2021-01-28 14:42, Alexey Kondratov wrote: >> No, you are right, we are doing REINDEX with AccessExclusiveLock as it >> was before. This part is a more specific one. It only applies to >> partitioned indexes, which do not hold any data, so we do not reindex >> them directly, only their leafs. However, if we are doing a TABLESPACE >> change, we have to record it in their pg_class entry, so all future >> leaf partitions were created in the proper tablespace. >>=20 >> That way, we open partitioned index relation only for a reference, >> i.e. read-only, but modify pg_class entry under a proper lock >> (RowExclusiveLock). That's why I thought that ShareLock will be >> enough. >>=20 >> IIUC, 'ALTER TABLE ... SET TABLESPACE' uses AccessExclusiveLock even >> for relations with no storage, since AlterTableGetLockLevel() chooses >> it if AT_SetTableSpace is met. This is very similar to our case, so >> probably we should do the same? >>=20 >> Actually it is not completely clear for me why >> ShareUpdateExclusiveLock is sufficient for newly added >> SetRelationTableSpace() as Michael wrote in the comment. Nay, it was not fine. That's something Alvaro has mentioned, leading to 2484329. This also means that the main patch of this thread should refresh the comments at the top of CheckRelationTableSpaceMove() and SetRelationTableSpace() to mention that this is used by REINDEX CONCURRENTLY with a lower lock. > Changed patch to use AccessExclusiveLock in this part for now. This is wh= at > 'ALTER TABLE/INDEX ... SET TABLESPACE' and 'REINDEX' usually do. Anyway, = all > real leaf partitions are processed in the independent transactions later. + if (partkind =3D=3D RELKIND_PARTITIONED_INDEX) + { + Relation iRel =3D index_open(partoid, AccessExclusiveLock); + + if (CheckRelationTableSpaceMove(iRel, params->tablespaceOid)) + SetRelationTableSpace(iRel, + params->tablespaceOid, + InvalidOid); + index_close(iRel, NoLock); Are you sure that this does not represent a risk of deadlocks as EAL is not taken consistently across all the partitions? A second issue here is that this breaks the assumption of REINDEX CONCURRENTLY kicked on partitioned relations that should use ShareUpdateExclusiveLock for all its steps. This would make the first transaction invasive for the user, but we don't want that. This makes me really wonder if we would not be better to restrict this operation for partitioned relation as part of REINDEX as a first step. Another thing, mentioned upthread, is that we could do this part of the switch at the last transaction, or we could silently *not* do the switch for partitioned indexes in the flow of REINDEX, letting users handle that with an extra ALTER TABLE SET TABLESPACE once REINDEX has finished on all the partitions, cascading the command only on the partitioned relation of a tree. It may be interesting to look as well at if we could lower the lock used for partitioned relations with ALTER TABLE SET TABLESPACE from AEL to SUEL, choosing AEL only if at least one partition with storage is involved in the command, CheckRelationTableSpaceMove() discarding anything that has no need to change. -- Michael --3ijwfRZFbUKs6mY+ Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmAUwwUACgkQnvQgOdby QH2q1g/+Pq5H5eb8ycIsMiqjOiYazQG3pZ33DFyoHQJTAOEbv+MJPcJjcA7EHgr0 Tf4oZSR6D4H36BILWZ5Hgv2aa67T+AsP5STE8O/crE2ob7jNfcHrxL+S80dN1zD7 V292eXUsFTRGAoIrPyovW2SbgtanMVHjgRnbweyf/hWORizH2JmxWjUgRygO+jBJ 9EsfBv8bosGymk++UyI56DreSoYQ3XtmBPcTNM3p/bD0yqfZrI+9pgP37+vaPEPY q2bVphtr+40roxVQBPQp05oANL48CiLT2jeH/wUJ3BOC7X4ctxTsiKw6P7j6tONf JiTT3cRaJWUDFsCkGhwEaJKvS8+OPoHWqLsZsNMTJ8AD6Bj+kvoIM9hJbJVnoWZR MjEp4UVDIz7FMYxs2RJR1hxOo5H1jSiWpRAWwYYcGHJ2u5BAgsoLiGdoUfL73QyK +CWFINgACG3k+/n/XT/bxSyrM8s4U2klMBqM1iR3j19Xg13qesENB3qMnMXEuNBL 8awGgpHBkj7KaP/DTDIW7BfJTX7kslYoSXRqULsfxbuXW5Blvd1I/eJE3kQ3Z0sG o25bdcoQaanxBlDp/5czRpb+gUEoEq26d3cGdrxy6O1PNx78+YFzJsTpq+D4wGFe am8XJF9ycMaz7gSzqNuu7hhQiIO3BzOxXG/CViLWts+jnA3ICnE= =2Gee -----END PGP SIGNATURE----- --3ijwfRZFbUKs6mY+--