mirror of
https://github.com/postgres/postgres.git
synced 2025-04-22 23:02:54 +03:00
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
This commit is contained in:
parent
49da00677d
commit
0c76c2463e
@ -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;
|
||||
|
||||
PG_RETURN_TEXT_P(string_to_text(pg_get_partkeydef_worker(relid,
|
||||
PRETTYFLAG_INDENT,
|
||||
false)));
|
||||
res = pg_get_partkeydef_worker(relid, PRETTYFLAG_INDENT, false, true);
|
||||
|
||||
if (res == NULL)
|
||||
PG_RETURN_NULL();
|
||||
|
||||
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);
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user