Skip to content

Commit 0c76c24

Browse files
committed
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
1 parent 49da006 commit 0c76c24

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ static char *pg_get_indexdef_worker(Oid indexrelid, int colno,
320320
int prettyFlags, bool missing_ok);
321321
static char *pg_get_statisticsext_worker(Oid statextid, bool missing_ok);
322322
static char *pg_get_partkeydef_worker(Oid relid, int prettyFlags,
323-
bool attrsOnly);
323+
bool attrsOnly, bool missing_ok);
324324
static char *pg_get_constraintdef_worker(Oid constraintId, bool fullCommand,
325325
int prettyFlags, bool missing_ok);
326326
static text *pg_get_expr_worker(text *expr, Oid relid, const char *relname,
@@ -1555,10 +1555,14 @@ Datum
15551555
pg_get_partkeydef(PG_FUNCTION_ARGS)
15561556
{
15571557
Oid relid = PG_GETARG_OID(0);
1558+
char *res;
1559+
1560+
res = pg_get_partkeydef_worker(relid, PRETTYFLAG_INDENT, false, true);
1561+
1562+
if (res == NULL)
1563+
PG_RETURN_NULL();
15581564

1559-
PG_RETURN_TEXT_P(string_to_text(pg_get_partkeydef_worker(relid,
1560-
PRETTYFLAG_INDENT,
1561-
false)));
1565+
PG_RETURN_TEXT_P(string_to_text(res));
15621566
}
15631567

15641568
/* Internal version that just reports the column definitions */
@@ -1568,15 +1572,15 @@ pg_get_partkeydef_columns(Oid relid, bool pretty)
15681572
int prettyFlags;
15691573

15701574
prettyFlags = pretty ? PRETTYFLAG_PAREN | PRETTYFLAG_INDENT : PRETTYFLAG_INDENT;
1571-
return pg_get_partkeydef_worker(relid, prettyFlags, true);
1575+
return pg_get_partkeydef_worker(relid, prettyFlags, true, false);
15721576
}
15731577

15741578
/*
15751579
* Internal workhorse to decompile a partition key definition.
15761580
*/
15771581
static char *
15781582
pg_get_partkeydef_worker(Oid relid, int prettyFlags,
1579-
bool attrsOnly)
1583+
bool attrsOnly, bool missing_ok)
15801584
{
15811585
Form_pg_partitioned_table form;
15821586
HeapTuple tuple;
@@ -1594,7 +1598,11 @@ pg_get_partkeydef_worker(Oid relid, int prettyFlags,
15941598

15951599
tuple = SearchSysCache1(PARTRELID, ObjectIdGetDatum(relid));
15961600
if (!HeapTupleIsValid(tuple))
1601+
{
1602+
if (missing_ok)
1603+
return NULL;
15971604
elog(ERROR, "cache lookup failed for partition key of %u", relid);
1605+
}
15981606

15991607
form = (Form_pg_partitioned_table) GETSTRUCT(tuple);
16001608

src/test/regress/expected/rules.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3212,6 +3212,12 @@ SELECT pg_get_function_arg_default('pg_class'::regclass, 0);
32123212

32133213
(1 row)
32143214

3215+
SELECT pg_get_partkeydef(0);
3216+
pg_get_partkeydef
3217+
-------------------
3218+
3219+
(1 row)
3220+
32153221
-- test rename for a rule defined on a partitioned table
32163222
CREATE TABLE parted_table (a int) PARTITION BY LIST (a);
32173223
CREATE TABLE parted_table_1 PARTITION OF parted_table FOR VALUES IN (1);

src/test/regress/sql/rules.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,7 @@ SELECT pg_get_function_identity_arguments(0);
11631163
SELECT pg_get_function_result(0);
11641164
SELECT pg_get_function_arg_default(0, 0);
11651165
SELECT pg_get_function_arg_default('pg_class'::regclass, 0);
1166+
SELECT pg_get_partkeydef(0);
11661167

11671168
-- test rename for a rule defined on a partitioned table
11681169
CREATE TABLE parted_table (a int) PARTITION BY LIST (a);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy