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 1l55hG-0003l7-Ry for pgsql-hackers@arkaria.postgresql.org; Thu, 28 Jan 2021 11:42:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l55hF-0005Ty-BZ for pgsql-hackers@arkaria.postgresql.org; Thu, 28 Jan 2021 11:42:45 +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 1l55hF-0005Tr-2b for pgsql-hackers@lists.postgresql.org; Thu, 28 Jan 2021 11:42:45 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l55hC-0001OJ-KB for pgsql-hackers@lists.postgresql.org; Thu, 28 Jan 2021 11:42:44 +0000 Received: from mail.postgrespro.ru (cyclops.postgrespro.ru [93.174.131.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 1430B21C77CE; Thu, 28 Jan 2021 14:42:41 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1611834161; bh=GbLd274MDlkMaNigMGNV36rvNhKbd82GT4vba/1HjcY=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=FbOByp/oq3aQtkAoOST87Da+9Zrx+Jfs7UR3vqdHTH9eq+qYk0SPb0ZE+xJU4dNog 4oQql4n8F/J7bLwXmvV4ruSPT4I82wDf85/ubMAJ6jZEY/q6RmmAqJkK9HMNHX2Wf7 nCkg2RZ+lrynIQRronzCQ0Ylai75JRPn49hXdgd0= MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Thu, 28 Jan 2021 14:42:40 +0300 From: Alexey Kondratov To: Alvaro Herrera Cc: Michael Paquier , 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 In-Reply-To: <20210127213658.GA736@alvherre.pgsql> References: <20210127213658.GA736@alvherre.pgsql> User-Agent: Roundcube Webmail/1.4.0 Message-ID: <55c6c1526b94d2caf54c33262fe00674@postgrespro.ru> X-Sender: a.kondratov@postgrespro.ru List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2021-01-28 00:36, Alvaro Herrera wrote: > On 2021-Jan-28, Alexey Kondratov wrote: > >> I have read more about lock levels and ShareLock should prevent any >> kind of >> physical modification of indexes. We already hold ShareLock doing >> find_all_inheritors(), which is higher than ShareUpdateExclusiveLock, >> so >> using ShareLock seems to be safe here, but I will look on it closer. > > You can look at lock.c where LockConflicts[] is; that would tell you > that ShareLock indeed conflicts with ShareUpdateExclusiveLock ... but > it > does not conflict with itself! So it would be possible to have more > than one process doing this thing at the same time, which surely makes > no sense. > Thanks for the explanation and pointing me to the LockConflicts[]. This is a good reference. > > I didn't look at the patch closely enough to understand why you're > trying to do something like CLUSTER, VACUUM FULL or REINDEX without > holding full AccessExclusiveLock on the relation. But do keep in mind > that once you hold a lock on a relation, trying to grab a weaker lock > afterwards is pretty pointless. > 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. 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. 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? Actually it is not completely clear for me why ShareUpdateExclusiveLock is sufficient for newly added SetRelationTableSpace() as Michael wrote in the comment. Regards -- Alexey Kondratov Postgres Professional https://www.postgrespro.com Russian Postgres Company