diff options
author | Michael Paquier <michael@paquier.xyz> | 2018-10-22 11:04:48 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2018-10-22 11:04:48 +0900 |
commit | 17f206fbc824d2b4b14480199ca9ff7dea417eda (patch) | |
tree | b9094d05581dcd83ae0874731cd9a791ae72c394 | |
parent | 31ff51adc855e3ffe8e3c20e479b8d1a4508feb8 (diff) | |
download | postgresql-17f206fbc824d2b4b14480199ca9ff7dea417eda.tar.gz postgresql-17f206fbc824d2b4b14480199ca9ff7dea417eda.zip |
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
-rw-r--r-- | src/backend/catalog/index.c | 5 | ||||
-rw-r--r-- | src/backend/commands/indexcmds.c | 4 | ||||
-rw-r--r-- | src/test/regress/expected/indexing.out | 56 | ||||
-rw-r--r-- | src/test/regress/sql/indexing.sql | 18 |
4 files changed, 62 insertions, 21 deletions
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index f1ef4c319a0..5885899c9b4 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -994,9 +994,12 @@ index_create(Relation heapRelation, */ CacheInvalidateRelcache(heapRelation); - /* update pg_inherits, if needed */ + /* update pg_inherits and the parent's relhassubclass, if needed */ if (OidIsValid(parentIndexRelid)) + { StoreSingleInheritance(indexRelationId, parentIndexRelid, 1); + 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 3975f62c001..906d7113781 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); + /* set relhassubclass if an index partition has been added to the parent */ + 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 225f4e95274..ca27346f182 100644 --- a/src/test/regress/expected/indexing.out +++ b/src/test/regress/expected/indexing.out @@ -1,25 +1,45 @@ -- 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 any partition. +-- It will be set after the first partition is created. +create index idxpart_idx on idxpart (a); +select relhassubclass from pg_class where relname = 'idxpart_idx'; + relhassubclass +---------------- + f +(1 row) + +drop index idxpart_idx; 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); +-- Even with partitions, relhassubclass should not be set if a partitioned +-- index is created only on the parent. +create index idxpart_idx on only idxpart(a); +select relhassubclass from pg_class where relname = 'idxpart_idx'; + relhassubclass +---------------- + f +(1 row) + +drop index idxpart_idx; 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 +130,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 f145384fbc9..400b7eb7ba3 100644 --- a/src/test/regress/sql/indexing.sql +++ b/src/test/regress/sql/indexing.sql @@ -1,12 +1,26 @@ -- 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 any partition. +-- It will be set after the first partition is created. +create index idxpart_idx on idxpart (a); +select relhassubclass from pg_class where relname = 'idxpart_idx'; +drop index idxpart_idx; + 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); + +-- Even with partitions, relhassubclass should not be set if a partitioned +-- index is created only on the parent. +create index idxpart_idx on only idxpart(a); +select relhassubclass from pg_class where relname = 'idxpart_idx'; +drop index idxpart_idx; + 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 +68,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; |