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 1kDuVk-0004TF-KA for pgsql-hackers@arkaria.postgresql.org; Thu, 03 Sep 2020 19:03:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kDuVj-0004VJ-I0 for pgsql-hackers@arkaria.postgresql.org; Thu, 03 Sep 2020 19:03:03 +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 1kDuVj-0004VC-8p for pgsql-hackers@lists.postgresql.org; Thu, 03 Sep 2020 19:03:03 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kDuVg-0001oW-G6 for pgsql-hackers@lists.postgresql.org; Thu, 03 Sep 2020 19:03:02 +0000 Received: from [192.168.0.112] (unknown [194.187.204.62]) (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 DCE7321C2F13; Thu, 3 Sep 2020 22:02:58 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1599159778; bh=/lG+/tnacqnpDh9xz5xwpcEPEf6jCnix1MYq7TVvPXA=; h=Subject:To:Cc:References:From:Date:In-Reply-To; b=I0oVGnB+ArkKQbibgZvMRHr8pftEnV8zSmfwBCMSzm3Vl+1w5HUU0rg8Q9prEoe3w jlbGGoR1I/Xr6Jz9N6UDp69ZerzUO/VuLOtwTdVaO53dU/CB9T7fs4CS4sY66ZZDs4 E7VXUpu1yloO//CRJXjdqI9dkltqf1dg5SHlCQig= Subject: =?UTF-8?B?UmU6IOWbnuWkje+8mmhvdyB0byBjcmVhdGUgaW5kZXggY29uY3VycmVu?= =?UTF-8?Q?tly_on_partitioned_table?= To: Michael Paquier , Justin Pryzby Cc: =?UTF-8?B?5p2O5p2wKOaFjui/vSk=?= , pgsql-hackers , =?UTF-8?B?5pu+5paH5peMKOS5ieS7jik=?= , Alvaro Herrera References: <20200618024114.GF118592@paquier.xyz> <20200616010221.GA2353@paquier.xyz> <20200615123742.GL52676@paquier.xyz> <20200808063744.GG20473@telsasoft.com> <20200809050009.GA17986@paquier.xyz> <20200809234423.GH20473@telsasoft.com> <20200812045438.GA11663@paquier.xyz> <20200812052820.GW20473@telsasoft.com> <20200812133708.GB11663@paquier.xyz> <20200814002945.GB2057@paquier.xyz> <20200902013944.GF1489@paquier.xyz> From: Anastasia Lubennikova Message-ID: <4cefde7e-cc68-2e0c-077b-d6fe3a220344@postgrespro.ru> Date: Thu, 3 Sep 2020 22:02:58 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20200902013944.GF1489@paquier.xyz> Content-Type: multipart/alternative; boundary="------------7DBA1B340A77DDC0BB814372" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------7DBA1B340A77DDC0BB814372 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit On 02.09.2020 04:39, Michael Paquier wrote: > > The problem with dropped relations in REINDEX has been addressed by > 1d65416, so I have gone through this patch again and simplified the > use of session locks, these being taken only when doing a REINDEX > CONCURRENTLY for a given partition. This part is in a rather > committable shape IMO, so I would like to get it done first, before > looking more at the other cases with CIC and CLUSTER. I am still > planning to go through it once again. > -- > Michael Thank you for advancing this work. I was reviewing the previous version, but the review became outdated before I sent it. Overall design is fine, but I see a bunch of things that need to be fixed before commit. First of all, this patch fails at cfbot: indexcmds.c:2848:7: error: variable ‘parentoid’ set but not used [-Werror=unused-but-set-variable] Oid parentoid;^ It seems to be just a typo. With this minor fix the patch compiles and passes tests. diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 75008eebde..f5b3c53a83 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -2864,7 +2864,7 @@ reindex_partitions(Oid relid, int options, bool concurrent, /* Save partition OID */ old_context = MemoryContextSwitchTo(reindex_context); - partitions = lappend_oid(partitions, partoid); + partitions = lappend_oid(partitions, parentoid); MemoryContextSwitchTo(old_context); } If this guessed fix is correct, I see the problem in the patch logic. In reindex_partitions() we collect parent relations to pass them to reindex_multiple_internal(). It implicitly changes the logic from REINDEX INDEX to REINDEX RELATION, which is not the same, if table has more than one index. For example, if I add one more index to a partitioned table from a create_index.sql test: create index idx on concur_reindex_part_0_2 using btree (c2); and call REINDEX INDEX CONCURRENTLY concur_reindex_part_index; idx will be reindexed as well. I doubt that it is the desired behavior. A few more random issues, that I noticed at first glance: 1) in create_index.sql Are these two lines intentional checks that must fail? If so, I propose to add a comment. REINDEX TABLE concur_reindex_part_index; REINDEX TABLE CONCURRENTLY concur_reindex_part_index; A few lines around also look like they were copy-pasted and need a second look. 2)  This part of ReindexRelationConcurrently() is misleading.         case RELKIND_PARTITIONED_TABLE:         case RELKIND_PARTITIONED_INDEX:         default:             /* Return error if type of relation is not supported */             ereport(ERROR,                     (errcode(ERRCODE_WRONG_OBJECT_TYPE),                      errmsg("cannot reindex this type of relation concurrently"))); Maybe assertion is enough. It seems, that we should never reach this code because both ReindexTable and ReindexIndex handle those relkinds separately.  Which leads me to the next question. 3) Is there a reason, why reindex_partitions() is not inside ReindexRelationConcurrently() ? I think that logically it belongs there. 4) I haven't tested multi-level partitions yet. In any case, it would be good to document this behavior explicitly. I need a bit more time to review this patch more thoroughly. Please, wait for it, before committing. -- Anastasia Lubennikova Postgres Professional: http://www.postgrespro.com The Russian Postgres Company --------------7DBA1B340A77DDC0BB814372 Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: 8bit
On 02.09.2020 04:39, Michael Paquier wrote:

The problem with dropped relations in REINDEX has been addressed by
1d65416, so I have gone through this patch again and simplified the
use of session locks, these being taken only when doing a REINDEX
CONCURRENTLY for a given partition.  This part is in a rather
committable shape IMO, so I would like to get it done first, before
looking more at the other cases with CIC and CLUSTER.  I am still
planning to go through it once again.
--
Michael

Thank you for advancing this work.

I was reviewing the previous version, but the review became outdated before I sent it. Overall design is fine, but I see a bunch of things that need to be fixed before commit.

First of all, this patch fails at cfbot:

indexcmds.c:2848:7: error: variable ‘parentoid’ set but not used [-Werror=unused-but-set-variable]
Oid parentoid; ^


It seems to be just a typo. With this minor fix the patch compiles and passes tests.

diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 75008eebde..f5b3c53a83 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -2864,7 +2864,7 @@ reindex_partitions(Oid relid, int options, bool concurrent,
 
                /* Save partition OID */
                old_context = MemoryContextSwitchTo(reindex_context);
-               partitions = lappend_oid(partitions, partoid);
+               partitions = lappend_oid(partitions, parentoid);
                MemoryContextSwitchTo(old_context);
        }

If this guessed fix is correct, I see the problem in the patch logic. In reindex_partitions() we collect parent relations to pass them to reindex_multiple_internal(). It implicitly changes the logic from REINDEX INDEX to REINDEX RELATION, which is not the same, if table has more than one index. For example, if I add one more index to a partitioned table from a create_index.sql test:
create index idx on concur_reindex_part_0_2 using btree (c2);

and call

REINDEX INDEX CONCURRENTLY concur_reindex_part_index;
idx will be reindexed as well. I doubt that it is the desired behavior.


A few more random issues, that I noticed at first glance:

1) in create_index.sql

Are these two lines intentional checks that must fail? If so, I propose to add a comment.

REINDEX TABLE concur_reindex_part_index;
REINDEX TABLE CONCURRENTLY concur_reindex_part_index;

A few lines around also look like they were copy-pasted and need a second look.

2)  This part of ReindexRelationConcurrently() is misleading.

        case RELKIND_PARTITIONED_TABLE:
        case RELKIND_PARTITIONED_INDEX:
        default:
            /* Return error if type of relation is not supported */
            ereport(ERROR,
                    (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                     errmsg("cannot reindex this type of relation concurrently")));


Maybe assertion is enough. It seems, that we should never reach this code because both ReindexTable and ReindexIndex handle those relkinds separately.  Which leads me to the next question.

3) Is there a reason, why reindex_partitions() is not inside ReindexRelationConcurrently() ? I think that logically it belongs there.

4) I haven't tested multi-level partitions yet. In any case, it would be good to document this behavior explicitly.


I need a bit more time to review this patch more thoroughly. Please, wait for it, before committing.

-- 
Anastasia Lubennikova
Postgres Professional: http://www.postgrespro.com
The Russian Postgres Company
--------------7DBA1B340A77DDC0BB814372--