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 1ibiCf-0004LK-OH for pgsql-hackers@arkaria.postgresql.org; Mon, 02 Dec 2019 09:41:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ibiCe-0007tf-0G for pgsql-hackers@arkaria.postgresql.org; Mon, 02 Dec 2019 09:41:12 +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 1ibiCd-0007tY-9x for pgsql-hackers@lists.postgresql.org; Mon, 02 Dec 2019 09:41:11 +0000 Received: from cyclops.postgrespro.ru ([93.174.131.138] helo=mail.postgrespro.ru) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ibiCZ-000171-BL for pgsql-hackers@lists.postgresql.org; Mon, 02 Dec 2019 09:41:10 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id D44E421C43F9; Mon, 2 Dec 2019 12:41:04 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.27.223] (gw.postgrespro.ru [93.174.131.141]) (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 0C9FC21C022F; Mon, 2 Dec 2019 12:41:04 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1575279664; bh=JeH4PZPSUtA0xn4FYx3BN3v+vyLXjPl5TkkBO59H688=; h=Subject:To:Cc:References:From:Date:In-Reply-To; b=Mmpl5yiAGNhVN/L7AcNjbZGT0i/zyHidQ4WQO2JQ79AXFxu83ZcueysfOke9JCId7 n00VKkhFCYgRWGn31pTFirwgR/CRxzqpp2hxWTy8p9D/W4dDTGfsRAwY1rd9oCmJU2 KaVfRpRNDAeM6JEuflbctyPT8W86thoqgOFgsqew= Subject: Re: Allow CLUSTER, VACUUM FULL and REINDEX to change tablespace on the fly To: Michael Paquier Cc: Masahiko Sawada , Steve Singer , PostgreSQL Hackers , Alvaro Herrera , Robert Haas , Alexander Korotkov , Masahiko Sawada , Jose Luis Tallon References: <8a8f5f73-00d3-55f8-7583-1375ca8f6a91@postgrespro.ru> <157395200750.29912.1178609357962324139.pgcf@coridan.postgresql.org> <827a9139-e02b-2dbf-5c6c-a6fbcaa1739a@postgrespro.ru> <20191127035416.GG5435@paquier.xyz> <3ae48673-283c-3e99-3dd8-36ebb81614b5@postgrespro.ru> <20191202082134.GI1696@paquier.xyz> From: Alexey Kondratov Message-ID: Date: Mon, 2 Dec 2019 12:41:03 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <20191202082134.GI1696@paquier.xyz> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 02.12.2019 11:21, Michael Paquier wrote: > On Wed, Nov 27, 2019 at 08:47:06PM +0300, Alexey Kondratov wrote: >> The only difference is that point 3) and tablespace part of 5) were missing >> in RelationSetNewRelfilenode, so I added them, and I do 4) after 6) in >> REINDEX. Thus, it seems that in my implementation of tablespace change in >> REINDEX I am more sure that "the relation tablespace is correctly updated >> before reindexing", since I do reindex after CCI (point 6), doesn't it? >> >> So why it is fine for ATExecSetTableSpace to do pretty much the same, but >> not for REINDEX? Or the key point is in doing actual work before CCI, but >> for me it seems a bit against what you have wrote? > Nope, the order is not the same on what you do here, causing a > duplication in the tablespace selection within > RelationSetNewRelfilenode() and when flushing the relation on the new > tablespace for the first time after the CCI happens, please see > below. And we should avoid that. > >> Thus, I cannot get your point correctly here. Can you, please, elaborate a >> little bit more your concerns? > The case of REINDEX CONCURRENTLY is pretty simple, because a new > relation which is a copy of the old relation is created before doing > the reindex, so you simply need to set the tablespace OID correctly > in index_concurrently_create_copy(). And actually, I think that the > computation is incorrect because we need to check after > MyDatabaseTableSpace as well, no? No, the same logic already exists in heap_create:     if (reltablespace == MyDatabaseTableSpace)         reltablespace = InvalidOid; Which is called by index_concurrently_create_copy -> index_create -> heap_create. > The case of REINDEX is more tricky, because you are working on a > relation that already exists, hence I think that what you need to do a > different thing before the actual REINDEX: > 1) Update the existing relation's pg_class tuple to point to the new > tablespace. > 2) Do a CommandCounterIncrement. > So I think that the order of the operations you are doing is incorrect, > and that you have a risk of breaking the existing tablespace assignment > logic done when first flushing a new relfilenode. > > This actually brings an extra thing: when doing a plain REINDEX you > need to make sure that the past relfilenode of the relation gets away > properly. The attached POC patch does that before doing the CCI which > is a bit ugly, but that's enough to show my point, and there is no > need to touch RelationSetNewRelfilenode() this way. Thank you for the detailed answer and PoC patch. I will recheck everything and dig deeper into this problem, and come up with something closer to the next 01.2020 commitfest. Regards -- Alexey Kondratov Postgres Professional https://www.postgrespro.com Russian Postgres Company