Thanks for commenting.

On 2018/10/19 15:17, Michael Paquier wrote:
> On Fri, Oct 19, 2018 at 01:45:03AM -0400, Tom Lane wrote:
>> Amit Langote <langote_amit...@lab.ntt.co.jp> writes:
>>> Should relhassubclass be set/reset for partitioned indexes?
>>
>> Seems like a reasonable idea to me, at least the "set" end of it.
>> We don't ever clear relhassubclass for tables, so maybe that's
>> not necessary for indexes either.

We *do* reset opportunistically during analyze of inheritance parent; the
following code in acquire_inherited_sample_rows() can reset it:

 /*
  * Check that there's at least one descendant, else fail.  This could
  * happen despite analyze_rel's relhassubclass check, if table once had a
  * child but no longer does.  In that case, we can clear the
  * relhassubclass field so as not to make the same mistake again later.
  * (This is safe because we hold ShareUpdateExclusiveLock.)
  */
 if (list_length(tableOIDs) < 2)
 {
     /* CCI because we already updated the pg_class row in this command */
     CommandCounterIncrement();
     SetRelationHasSubclass(RelationGetRelid(onerel), false);
     ereport(elevel,
             (errmsg("skipping analyze of \"%s.%s\" inheritance tree ---
this inheritance tree contains no child tables",
                     get_namespace_name(RelationGetNamespace(onerel)),
                     RelationGetRelationName(onerel))));
     return 0;
 }


Similarly, perhaps we can make REINDEX on a partitioned index reset the
flag if it doesn't find any children.  We won't be able to do that today
though, because:

static void
ReindexPartitionedIndex(Relation parentIdx)
{
    ereport(ERROR,
            (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
             errmsg("REINDEX is not yet implemented for partitioned
indexes")));
}

> No objections to the proposal.  Allowing find_inheritance_children to
> find index trees for partitioned indexes could be actually useful for
> extensions like pg_partman.

Thanks.  Attached a patch to set relhassubclass when an index partition is
added to a partitioned index.

Regards,
Amit
>From f0c01ab41b35a5f21a90b0294d8216da78eb8882 Mon Sep 17 00:00:00 2001
From: amit <amitlangot...@gmail.com>
Date: Fri, 19 Oct 2018 17:05:00 +0900
Subject: [PATCH 1/2] Set relhassubclass on index parents

---
 src/backend/catalog/index.c            |  5 ++++
 src/backend/commands/indexcmds.c       |  4 +++
 src/test/regress/expected/indexing.out | 46 +++++++++++++++++++++-------------
 src/test/regress/sql/indexing.sql      | 11 ++++++--
 4 files changed, 46 insertions(+), 20 deletions(-)

diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index f1ef4c319a..62cc6a5bb9 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -996,8 +996,13 @@ index_create(Relation heapRelation,
 
        /* update pg_inherits, if needed */
        if (OidIsValid(parentIndexRelid))
+       {
                StoreSingleInheritance(indexRelationId, parentIndexRelid, 1);
 
+               /* Also, set the parent's relhassubclass. */
+               SetRelationHasSubclass(parentIndexRelid, true);
+       }
+
        /*
         * Register constraint and dependencies for the index.
         *
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 3975f62c00..c392352871 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -2608,6 +2608,10 @@ IndexSetParentIndex(Relation partitionIdx, Oid parentOid)
        systable_endscan(scan);
        relation_close(pg_inherits, RowExclusiveLock);
 
+       /* If we added an index partition to parent, set its relhassubclass. */
+       if (OidIsValid(parentOid))
+               SetRelationHasSubclass(parentOid, true);
+
        if (fix_dependencies)
        {
                ObjectAddress partIdx;
diff --git a/src/test/regress/expected/indexing.out 
b/src/test/regress/expected/indexing.out
index 225f4e9527..710b32192f 100644
--- a/src/test/regress/expected/indexing.out
+++ b/src/test/regress/expected/indexing.out
@@ -1,25 +1,35 @@
 -- Creating an index on a partitioned table makes the partitions
 -- automatically get the index
 create table idxpart (a int, b int, c text) partition by range (a);
+-- relhassubclass of a partitioned index is false before creating its partition
+-- it will be set below once partitions get created
+create index check_relhassubclass_of_this on idxpart (a);
+select relhassubclass from pg_class where relname = 
'check_relhassubclass_of_this';
+ relhassubclass 
+----------------
+ f
+(1 row)
+
+drop index check_relhassubclass_of_this;
 create table idxpart1 partition of idxpart for values from (0) to (10);
 create table idxpart2 partition of idxpart for values from (10) to (100)
        partition by range (b);
 create table idxpart21 partition of idxpart2 for values from (0) to (100);
 create index on idxpart (a);
-select relname, relkind, inhparent::regclass
+select relname, relkind, relhassubclass, inhparent::regclass
     from pg_class left join pg_index ix on (indexrelid = oid)
        left join pg_inherits on (ix.indexrelid = inhrelid)
        where relname like 'idxpart%' order by relname;
-     relname     | relkind |   inhparent    
------------------+---------+----------------
- idxpart         | p       | 
- idxpart1        | r       | 
- idxpart1_a_idx  | i       | idxpart_a_idx
- idxpart2        | p       | 
- idxpart21       | r       | 
- idxpart21_a_idx | i       | idxpart2_a_idx
- idxpart2_a_idx  | I       | idxpart_a_idx
- idxpart_a_idx   | I       | 
+     relname     | relkind | relhassubclass |   inhparent    
+-----------------+---------+----------------+----------------
+ idxpart         | p       | t              | 
+ idxpart1        | r       | f              | 
+ idxpart1_a_idx  | i       | f              | idxpart_a_idx
+ idxpart2        | p       | t              | 
+ idxpart21       | r       | f              | 
+ idxpart21_a_idx | i       | f              | idxpart2_a_idx
+ idxpart2_a_idx  | I       | t              | idxpart_a_idx
+ idxpart_a_idx   | I       | t              | 
 (8 rows)
 
 drop table idxpart;
@@ -110,16 +120,16 @@ Partition of: idxpart FOR VALUES FROM (0, 0) TO (10, 10)
 Indexes:
     "idxpart1_a_b_idx" btree (a, b)
 
-select relname, relkind, inhparent::regclass
+select relname, relkind, relhassubclass, inhparent::regclass
     from pg_class left join pg_index ix on (indexrelid = oid)
        left join pg_inherits on (ix.indexrelid = inhrelid)
        where relname like 'idxpart%' order by relname;
-     relname      | relkind |    inhparent    
-------------------+---------+-----------------
- idxpart          | p       | 
- idxpart1         | r       | 
- idxpart1_a_b_idx | i       | idxpart_a_b_idx
- idxpart_a_b_idx  | I       | 
+     relname      | relkind | relhassubclass |    inhparent    
+------------------+---------+----------------+-----------------
+ idxpart          | p       | t              | 
+ idxpart1         | r       | f              | 
+ idxpart1_a_b_idx | i       | f              | idxpart_a_b_idx
+ idxpart_a_b_idx  | I       | t              | 
 (4 rows)
 
 drop table idxpart;
diff --git a/src/test/regress/sql/indexing.sql 
b/src/test/regress/sql/indexing.sql
index f145384fbc..b0feb1911d 100644
--- a/src/test/regress/sql/indexing.sql
+++ b/src/test/regress/sql/indexing.sql
@@ -1,12 +1,19 @@
 -- Creating an index on a partitioned table makes the partitions
 -- automatically get the index
 create table idxpart (a int, b int, c text) partition by range (a);
+
+-- relhassubclass of a partitioned index is false before creating its partition
+-- it will be set below once partitions get created
+create index check_relhassubclass_of_this on idxpart (a);
+select relhassubclass from pg_class where relname = 
'check_relhassubclass_of_this';
+drop index check_relhassubclass_of_this;
+
 create table idxpart1 partition of idxpart for values from (0) to (10);
 create table idxpart2 partition of idxpart for values from (10) to (100)
        partition by range (b);
 create table idxpart21 partition of idxpart2 for values from (0) to (100);
 create index on idxpart (a);
-select relname, relkind, inhparent::regclass
+select relname, relkind, relhassubclass, inhparent::regclass
     from pg_class left join pg_index ix on (indexrelid = oid)
        left join pg_inherits on (ix.indexrelid = inhrelid)
        where relname like 'idxpart%' order by relname;
@@ -54,7 +61,7 @@ create table idxpart1 partition of idxpart for values from 
(0, 0) to (10, 10);
 create index on idxpart1 (a, b);
 create index on idxpart (a, b);
 \d idxpart1
-select relname, relkind, inhparent::regclass
+select relname, relkind, relhassubclass, inhparent::regclass
     from pg_class left join pg_index ix on (indexrelid = oid)
        left join pg_inherits on (ix.indexrelid = inhrelid)
        where relname like 'idxpart%' order by relname;
-- 
2.11.0

Reply via email to