From: Stephen Frost Date: Wed, 26 Apr 2017 18:59:22 +0000 (-0400) Subject: pg_get_partkeydef: return NULL for non-partitions X-Git-Tag: REL_10_BETA1~159 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0c76c2463e8ab4cfd633ad8de259050e3f28b78f;p=postgresql pg_get_partkeydef: return NULL for non-partitions Our general rule for pg_get_X(oid) functions is to simply return NULL when passed an invalid or inappropriate OID. Teach pg_get_partkeydef to do this also, making it easier for users to use this function when querying against tables with both partitions and non-partitions (such as pg_class). As a concrete example, this makes pg_dump's life a little easier. Author: Amit Langote --- diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 184e5daa05..cbde1fff01 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -320,7 +320,7 @@ static char *pg_get_indexdef_worker(Oid indexrelid, int colno, int prettyFlags, bool missing_ok); static char *pg_get_statisticsext_worker(Oid statextid, bool missing_ok); static char *pg_get_partkeydef_worker(Oid relid, int prettyFlags, - bool attrsOnly); + bool attrsOnly, bool missing_ok); static char *pg_get_constraintdef_worker(Oid constraintId, bool fullCommand, int prettyFlags, bool missing_ok); static text *pg_get_expr_worker(text *expr, Oid relid, const char *relname, @@ -1555,10 +1555,14 @@ Datum pg_get_partkeydef(PG_FUNCTION_ARGS) { Oid relid = PG_GETARG_OID(0); + char *res; + + res = pg_get_partkeydef_worker(relid, PRETTYFLAG_INDENT, false, true); + + if (res == NULL) + PG_RETURN_NULL(); - PG_RETURN_TEXT_P(string_to_text(pg_get_partkeydef_worker(relid, - PRETTYFLAG_INDENT, - false))); + PG_RETURN_TEXT_P(string_to_text(res)); } /* Internal version that just reports the column definitions */ @@ -1568,7 +1572,7 @@ pg_get_partkeydef_columns(Oid relid, bool pretty) int prettyFlags; prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : PRETTYFLAG_INDENT; - return pg_get_partkeydef_worker(relid, prettyFlags, true); + return pg_get_partkeydef_worker(relid, prettyFlags, true, false); } /* @@ -1576,7 +1580,7 @@ pg_get_partkeydef_columns(Oid relid, bool pretty) */ static char * pg_get_partkeydef_worker(Oid relid, int prettyFlags, - bool attrsOnly) + bool attrsOnly, bool missing_ok) { Form_pg_partitioned_table form; HeapTuple tuple; @@ -1594,7 +1598,11 @@ pg_get_partkeydef_worker(Oid relid, int prettyFlags, tuple = SearchSysCache1(PARTRELID, ObjectIdGetDatum(relid)); if (!HeapTupleIsValid(tuple)) + { + if (missing_ok) + return NULL; elog(ERROR, "cache lookup failed for partition key of %u", relid); + } form = (Form_pg_partitioned_table) GETSTRUCT(tuple); diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index 409692d695..0165471a4d 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -3212,6 +3212,12 @@ SELECT pg_get_function_arg_default('pg_class'::regclass, 0); (1 row) +SELECT pg_get_partkeydef(0); + pg_get_partkeydef +------------------- + +(1 row) + -- test rename for a rule defined on a partitioned table CREATE TABLE parted_table (a int) PARTITION BY LIST (a); CREATE TABLE parted_table_1 PARTITION OF parted_table FOR VALUES IN (1); diff --git a/src/test/regress/sql/rules.sql b/src/test/regress/sql/rules.sql index 4fff266216..98a8a692d8 100644 --- a/src/test/regress/sql/rules.sql +++ b/src/test/regress/sql/rules.sql @@ -1163,6 +1163,7 @@ SELECT pg_get_function_identity_arguments(0); SELECT pg_get_function_result(0); SELECT pg_get_function_arg_default(0, 0); SELECT pg_get_function_arg_default('pg_class'::regclass, 0); +SELECT pg_get_partkeydef(0); -- test rename for a rule defined on a partitioned table CREATE TABLE parted_table (a int) PARTITION BY LIST (a);