diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index d41c09b68b..6dfa3dc977 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -20209,12 +20209,13 @@ postgres=# SELECT * FROM pg_walfile_name_offset(pg_stop_backup()); pg_partition_tree(oid) setof record - List information about a partition tree for the given partitioned - table, consisting of one row for each partition in a tree. The - information available is the OID of the partition, the OID of its - immediate partitioned table, and its level in the hierarchy, - beginning at 0 for the top-most parent, and - incremented by 1 for each level up. + List information about table in a partition tree for a given + partitioned table, which consists of one row for each partition and + table itself. Information provided includes the OID of the partition, + the OID of its immediate parent, and its level in the hierarchy. + The value of level begins at 0 for the input table + in its role as the root of the partition tree, 1 for + its partitions, 2 for their partitions, and so on. diff --git a/src/backend/utils/adt/partitionfuncs.c b/src/backend/utils/adt/partitionfuncs.c index fc0a904967..41c57cac2d 100644 --- a/src/backend/utils/adt/partitionfuncs.c +++ b/src/backend/utils/adt/partitionfuncs.c @@ -1,7 +1,7 @@ /*------------------------------------------------------------------------- * * partitionfuncs.c - * Functions for accessing partitioning data + * Functions for accessing partitioning related metadata * * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California diff --git a/src/test/regress/sql/partition_info.sql b/src/test/regress/sql/partition_info.sql index 2ad75882b0..d9d96a284c 100644 --- a/src/test/regress/sql/partition_info.sql +++ b/src/test/regress/sql/partition_info.sql @@ -17,11 +17,24 @@ SELECT relid::regclass, parentrelid::regclass, level, pg_relation_size(relid) = 0 AS is_empty FROM pg_partition_tree('ptif_test'::regclass); --- children of the main tree -SELECT relid::regclass, parentrelid::regclass, level - FROM pg_partition_tree('ptif_test0'::regclass); -SELECT relid::regclass, parentrelid::regclass, level - FROM pg_partition_tree('ptif_test01'::regclass); +-- check that it works correctly even if it's passed other tables in the tree + +-- passing an intermediate level partitioned table +SELECT relid::regclass, parentrelid::regclass, level, relkind <> 'p' AS isleaf + FROM pg_partition_tree('ptif_test0'::regclass) p + JOIN pg_class c ON (p.relid = c.oid); + +-- passing a leaf partition +SELECT relid::regclass, parentrelid::regclass, level, relkind <> 'p' AS isleaf + FROM pg_partition_tree('ptif_test01'::regclass) p + JOIN pg_class c ON (p.relid = c.oid); + +-- check that passing a table that's not part of any partition tree works +-- the same as passing a leaf partition +create table ptif_normal_table(a int); +SELECT relid::regclass, parentrelid::regclass, level, relkind <> 'p' AS isleaf + FROM pg_partition_tree('ptif_normal_table'::regclass) p + JOIN pg_class c ON (p.relid = c.oid); -- this results in NULL, as there are no level 1 partitions of a leaf partition SELECT sum(pg_relation_size(relid)) AS total_size