]> granicus.if.org Git - postgresql/commitdiff
pg_get_partkeydef: return NULL for non-partitions
authorStephen Frost <sfrost@snowman.net>
Wed, 26 Apr 2017 18:59:22 +0000 (14:59 -0400)
committerStephen Frost <sfrost@snowman.net>
Wed, 26 Apr 2017 18:59:22 +0000 (14:59 -0400)
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

src/backend/utils/adt/ruleutils.c
src/test/regress/expected/rules.out
src/test/regress/sql/rules.sql

index 184e5daa05b77a64092864317cca58182d454e29..cbde1fff01cbc8bcc25730d5d619f196d541947e 100644 (file)
@@ -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);
 
index 409692d6957e7fa7ac1ec25cb5e704afa4487d59..0165471a4d7765235bffe98272a72b09bdf1637b 100644 (file)
@@ -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);
index 4fff26621632db41ce59e362a861c86523a2b387..98a8a692d8c990d79f32b82ad56a27aa83fd3b18 100644 (file)
@@ -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);