On Sun, Dec 09, 2018 at 08:15:07AM +0900, Michael Paquier wrote: > On Sat, Dec 08, 2018 at 08:46:08AM -0500, Stephen Frost wrote: >> We should really have a more clearly defined policy around this, but my >> recollection is that we often prefer to return NULL rather than throwing >> an error for the convenience of people doing things like querying >> pg_class using similar functions. > > Yes, that's visibly right. At least that's what I can see from the > various pg_get_*def and pg_*_is_visible. Returning NULL would indeed > be more consistent.
Thinking more about your argument, scanning fully pg_class is quite sensible as well because there is no need to apply an extra qual on relkind, so let's change the function as you suggest, by returning NULL on invalid relation type. Any opinions about the attached then which does the switch? -- Michael
diff --git a/src/backend/utils/adt/partitionfuncs.c b/src/backend/utils/adt/partitionfuncs.c index 78dd2b542b..87c5a2f4d5 100644 --- a/src/backend/utils/adt/partitionfuncs.c +++ b/src/backend/utils/adt/partitionfuncs.c @@ -23,6 +23,7 @@ #include "funcapi.h" #include "utils/fmgrprotos.h" #include "utils/lsyscache.h" +#include "utils/syscache.h" /* @@ -42,16 +43,16 @@ pg_partition_tree(PG_FUNCTION_ARGS) FuncCallContext *funcctx; ListCell **next; + if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(rootrelid))) + PG_RETURN_NULL(); + /* Only allow relation types that can appear in partition trees. */ if (relkind != RELKIND_RELATION && relkind != RELKIND_FOREIGN_TABLE && relkind != RELKIND_INDEX && relkind != RELKIND_PARTITIONED_TABLE && relkind != RELKIND_PARTITIONED_INDEX) - ereport(ERROR, - (errcode(ERRCODE_WRONG_OBJECT_TYPE), - errmsg("\"%s\" is not a table, a foreign table, or an index", - get_rel_name(rootrelid)))); + PG_RETURN_NULL(); /* stuff done only on the first call of the function */ if (SRF_IS_FIRSTCALL()) diff --git a/src/test/regress/expected/partition_info.out b/src/test/regress/expected/partition_info.out index 6b116125e6..202d820827 100644 --- a/src/test/regress/expected/partition_info.out +++ b/src/test/regress/expected/partition_info.out @@ -6,6 +6,12 @@ SELECT * FROM pg_partition_tree(NULL); -------+-------------+--------+------- (0 rows) +SELECT * FROM pg_partition_tree(0); + relid | parentrelid | isleaf | level +-------+-------------+--------+------- + | | | +(1 row) + -- Test table partition trees CREATE TABLE ptif_test (a int, b int) PARTITION BY range (a); CREATE TABLE ptif_test0 PARTITION OF ptif_test @@ -107,8 +113,16 @@ DROP TABLE ptif_normal_table; CREATE VIEW ptif_test_view AS SELECT 1; CREATE MATERIALIZED VIEW ptif_test_matview AS SELECT 1; SELECT * FROM pg_partition_tree('ptif_test_view'); -ERROR: "ptif_test_view" is not a table, a foreign table, or an index + relid | parentrelid | isleaf | level +-------+-------------+--------+------- + | | | +(1 row) + SELECT * FROM pg_partition_tree('ptif_test_matview'); -ERROR: "ptif_test_matview" is not a table, a foreign table, or an index + relid | parentrelid | isleaf | level +-------+-------------+--------+------- + | | | +(1 row) + DROP VIEW ptif_test_view; DROP MATERIALIZED VIEW ptif_test_matview; diff --git a/src/test/regress/sql/partition_info.sql b/src/test/regress/sql/partition_info.sql index 5a76f22b05..9b55a7fe5c 100644 --- a/src/test/regress/sql/partition_info.sql +++ b/src/test/regress/sql/partition_info.sql @@ -2,6 +2,7 @@ -- Tests for pg_partition_tree -- SELECT * FROM pg_partition_tree(NULL); +SELECT * FROM pg_partition_tree(0); -- Test table partition trees CREATE TABLE ptif_test (a int, b int) PARTITION BY range (a);
signature.asc
Description: PGP signature