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 1gcURO-0006LD-Uk for pgsql-hackers@arkaria.postgresql.org; Thu, 27 Dec 2018 12:07:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gcURM-0005H3-7I for pgsql-hackers@arkaria.postgresql.org; Thu, 27 Dec 2018 12:07:04 +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 1gcURL-0005Bq-Pt for pgsql-hackers@lists.postgresql.org; Thu, 27 Dec 2018 12:07:04 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gcURH-0001aF-L4 for pgsql-hackers@postgresql.org; Thu, 27 Dec 2018 12:07:02 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id E178E21D4F16; Thu, 27 Dec 2018 15:06:56 +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 [10.7.1.8] (unknown [89.39.107.197]) (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 1011921D4EEF; Thu, 27 Dec 2018 15:06:55 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1545912416; bh=JSZww1hTskGPb4H9b50IKBh+YjNzYjiFF/nBXZq2uA0=; h=Subject:To:Cc:References:From:Date:In-Reply-To; b=qk+Ytgbl2jNk78dXZVrUxi+0n3OkwIZHaavMZmzJIxvjGkZZTqaGEqPgdX4TvjN6T dGmtPeuSmoYyDVtX1WOKADTvHCqgHqIa+eCOfJ0vC/5cAQI4UbWNQlA4+Uz4LU4OT3 it2IfROVAl3j8scJZ/YL6uvnaNnD47eV26Dua0AA= Subject: Re: Allow CLUSTER, VACUUM FULL and REINDEX to change tablespace on the fly To: Alvaro Herrera , Robert Haas , Michael Paquier Cc: PostgreSQL Hackers References: <20181226181906.z3sxmsbxmkyimnaj@alvherre.pgsql> From: Alexey Kondratov Message-ID: Date: Thu, 27 Dec 2018 15:06:54 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <20181226181906.z3sxmsbxmkyimnaj@alvherre.pgsql> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, Thank you all for replies. >> ALTER TABLE already has a lot of logic that is oriented towards being >> able to do multiple things at the same time. If we added CLUSTER, >> VACUUM FULL, and REINDEX to that set, then you could, say, change a >> data type, cluster, and change tablespaces all in a single SQL >> command. > That's a great observation. Indeed, I thought that ALTER TABLE executes all actions sequentially one by one, e.g. in the case of ALTER TABLE test_int CLUSTER ON test_int_idx, SET TABLESPACE test_tblspc; it executes CLUSTER and THEN executes SET TABLESPACE. However, if I get it right, ALTER TABLE is rather smart, so in such a case it follows the steps: 1) Only saves new tablespace Oid during prepare phase 1 without actual work; 2) Only executes mark_index_clustered during phase 2, again without actual work done; 3) And finally rewrites relation during phase 3, where CLUSTER and SET TABLESPACE are effectively performed. >> That would be cool, but probably a lot of work. :-( > But is it? ALTER TABLE is already doing one kind of table rewrite > during phase 3, and CLUSTER is just a different kind of table rewrite > (which happens to REINDEX), and VACUUM FULL is just a special case of > CLUSTER. Maybe what we need is an ALTER TABLE variant that executes > CLUSTER's table rewrite during phase 3 instead of its ad-hoc table > rewrite. According to the ALTER TABLE example above, it is already exist for CLUSTER. > As for REINDEX, I think it's valuable to move tablespace together with > the reindexing. You can already do it with the CREATE INDEX > CONCURRENTLY recipe we recommend, of course; but REINDEX CONCURRENTLY is > not going to provide that, and it seems worth doing. Maybe I am missing something, but according to the docs REINDEX CONCURRENTLY does not exist yet, DROP then CREATE CONCURRENTLY is suggested instead. Thus, we have to add REINDEX CONCURRENTLY first, but it is a matter of different patch, I guess. >> Even for plain REINDEX that seems useful. >> -- >> Michael To summarize: 1) Alvaro and Michael agreed, that REINDEX with tablespace move may be useful. This is done in the patch attached to my initial email. Adding REINDEX to ALTER TABLE as new action seems quite questionable for me and not completely semantically correct. ALTER already looks bulky. 2) If I am correct, 'ALTER TABLE ... CLUSTER ON ..., SET TABLESPACE ...' does exactly what I wanted to add to CLUSTER in my patch. So probably no work is necessary here. 3) VACUUM FULL. It seems, that we can add special case 'ALTER TABLE ... VACUUM FULL, SET TABLESPACE ...', which will follow relatively the same path as with CLUSTER ON, but without any specific index. Relation should be rewritten in the new tablespace during phase 3. What do you think? Regards -- Alexey Kondratov Postgres Professional https://www.postgrespro.com Russian Postgres Company