Skip to content

Commit 17f206f

Browse files
committed
Set pg_class.relhassubclass for partitioned indexes
Like for relations, switching this parameter is optimistic by turning it on each time a partitioned index gains a partition. So seeing this parameter set to true means that the partitioned index has or has had partitions. The flag cannot be reset yet for partitioned indexes, which is something not obvious anyway as partitioned relations exist to have partitions. This allows to track more conveniently partition trees for indexes, which will come in use with an upcoming patch helping in listing partition trees with an SQL-callable function. Author: Amit Langote Reviewed-by: Michael Paquier Discussion: https://postgr.es/m/80306490-b5fc-ea34-4427-f29c52156052@lab.ntt.co.jp
1 parent 31ff51a commit 17f206f

File tree

4 files changed

+62
-21
lines changed

4 files changed

+62
-21
lines changed

src/backend/catalog/index.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,9 +994,12 @@ index_create(Relation heapRelation,
994994
*/
995995
CacheInvalidateRelcache(heapRelation);
996996

997-
/* update pg_inherits, if needed */
997+
/* update pg_inherits and the parent's relhassubclass, if needed */
998998
if (OidIsValid(parentIndexRelid))
999+
{
9991000
StoreSingleInheritance(indexRelationId, parentIndexRelid, 1);
1001+
SetRelationHasSubclass(parentIndexRelid, true);
1002+
}
10001003

10011004
/*
10021005
* Register constraint and dependencies for the index.

src/backend/commands/indexcmds.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,6 +2608,10 @@ IndexSetParentIndex(Relation partitionIdx, Oid parentOid)
26082608
systable_endscan(scan);
26092609
relation_close(pg_inherits, RowExclusiveLock);
26102610

2611+
/* set relhassubclass if an index partition has been added to the parent */
2612+
if (OidIsValid(parentOid))
2613+
SetRelationHasSubclass(parentOid, true);
2614+
26112615
if (fix_dependencies)
26122616
{
26132617
ObjectAddress partIdx;

src/test/regress/expected/indexing.out

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
11
-- Creating an index on a partitioned table makes the partitions
22
-- automatically get the index
33
create table idxpart (a int, b int, c text) partition by range (a);
4+
-- relhassubclass of a partitioned index is false before creating any partition.
5+
-- It will be set after the first partition is created.
6+
create index idxpart_idx on idxpart (a);
7+
select relhassubclass from pg_class where relname = 'idxpart_idx';
8+
relhassubclass
9+
----------------
10+
f
11+
(1 row)
12+
13+
drop index idxpart_idx;
414
create table idxpart1 partition of idxpart for values from (0) to (10);
515
create table idxpart2 partition of idxpart for values from (10) to (100)
616
partition by range (b);
717
create table idxpart21 partition of idxpart2 for values from (0) to (100);
18+
-- Even with partitions, relhassubclass should not be set if a partitioned
19+
-- index is created only on the parent.
20+
create index idxpart_idx on only idxpart(a);
21+
select relhassubclass from pg_class where relname = 'idxpart_idx';
22+
relhassubclass
23+
----------------
24+
f
25+
(1 row)
26+
27+
drop index idxpart_idx;
828
create index on idxpart (a);
9-
select relname, relkind, inhparent::regclass
29+
select relname, relkind, relhassubclass, inhparent::regclass
1030
from pg_class left join pg_index ix on (indexrelid = oid)
1131
left join pg_inherits on (ix.indexrelid = inhrelid)
1232
where relname like 'idxpart%' order by relname;
13-
relname | relkind | inhparent
14-
-----------------+---------+----------------
15-
idxpart | p |
16-
idxpart1 | r |
17-
idxpart1_a_idx | i | idxpart_a_idx
18-
idxpart2 | p |
19-
idxpart21 | r |
20-
idxpart21_a_idx | i | idxpart2_a_idx
21-
idxpart2_a_idx | I | idxpart_a_idx
22-
idxpart_a_idx | I |
33+
relname | relkind | relhassubclass | inhparent
34+
-----------------+---------+----------------+----------------
35+
idxpart | p | t |
36+
idxpart1 | r | f |
37+
idxpart1_a_idx | i | f | idxpart_a_idx
38+
idxpart2 | p | t |
39+
idxpart21 | r | f |
40+
idxpart21_a_idx | i | f | idxpart2_a_idx
41+
idxpart2_a_idx | I | t | idxpart_a_idx
42+
idxpart_a_idx | I | t |
2343
(8 rows)
2444

2545
drop table idxpart;
@@ -110,16 +130,16 @@ Partition of: idxpart FOR VALUES FROM (0, 0) TO (10, 10)
110130
Indexes:
111131
"idxpart1_a_b_idx" btree (a, b)
112132

113-
select relname, relkind, inhparent::regclass
133+
select relname, relkind, relhassubclass, inhparent::regclass
114134
from pg_class left join pg_index ix on (indexrelid = oid)
115135
left join pg_inherits on (ix.indexrelid = inhrelid)
116136
where relname like 'idxpart%' order by relname;
117-
relname | relkind | inhparent
118-
------------------+---------+-----------------
119-
idxpart | p |
120-
idxpart1 | r |
121-
idxpart1_a_b_idx | i | idxpart_a_b_idx
122-
idxpart_a_b_idx | I |
137+
relname | relkind | relhassubclass | inhparent
138+
------------------+---------+----------------+-----------------
139+
idxpart | p | t |
140+
idxpart1 | r | f |
141+
idxpart1_a_b_idx | i | f | idxpart_a_b_idx
142+
idxpart_a_b_idx | I | t |
123143
(4 rows)
124144

125145
drop table idxpart;

src/test/regress/sql/indexing.sql

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
-- Creating an index on a partitioned table makes the partitions
22
-- automatically get the index
33
create table idxpart (a int, b int, c text) partition by range (a);
4+
5+
-- relhassubclass of a partitioned index is false before creating any partition.
6+
-- It will be set after the first partition is created.
7+
create index idxpart_idx on idxpart (a);
8+
select relhassubclass from pg_class where relname = 'idxpart_idx';
9+
drop index idxpart_idx;
10+
411
create table idxpart1 partition of idxpart for values from (0) to (10);
512
create table idxpart2 partition of idxpart for values from (10) to (100)
613
partition by range (b);
714
create table idxpart21 partition of idxpart2 for values from (0) to (100);
15+
16+
-- Even with partitions, relhassubclass should not be set if a partitioned
17+
-- index is created only on the parent.
18+
create index idxpart_idx on only idxpart(a);
19+
select relhassubclass from pg_class where relname = 'idxpart_idx';
20+
drop index idxpart_idx;
21+
822
create index on idxpart (a);
9-
select relname, relkind, inhparent::regclass
23+
select relname, relkind, relhassubclass, inhparent::regclass
1024
from pg_class left join pg_index ix on (indexrelid = oid)
1125
left join pg_inherits on (ix.indexrelid = inhrelid)
1226
where relname like 'idxpart%' order by relname;
@@ -54,7 +68,7 @@ create table idxpart1 partition of idxpart for values from (0, 0) to (10, 10);
5468
create index on idxpart1 (a, b);
5569
create index on idxpart (a, b);
5670
\d idxpart1
57-
select relname, relkind, inhparent::regclass
71+
select relname, relkind, relhassubclass, inhparent::regclass
5872
from pg_class left join pg_index ix on (indexrelid = oid)
5973
left join pg_inherits on (ix.indexrelid = inhrelid)
6074
where relname like 'idxpart%' order by relname;

0 commit comments

Comments
 (0)