diff --git a/doc/src/sgml/func/func-info.sgml b/doc/src/sgml/func/func-info.sgml index 4b9c661c20b..72b836cd082 100644 --- a/doc/src/sgml/func/func-info.sgml +++ b/doc/src/sgml/func/func-info.sgml @@ -3827,19 +3827,29 @@ acl | {postgres=arwdDxtm/postgres,foo=r/postgres} pg_get_policy_ddl pg_get_policy_ddl - ( table regclass, policy_name name, pretty boolean ) + ( table regclass, policy_name name , pretty boolean ) text - Reconstructs the CREATE POLICY statement from the system catalogs for a specified table and policy name. - When the pretty flag is set to true, the function returns a well-formatted DDL statement. - The result is a comprehensive CREATE POLICY statement. + Reconstructs the CREATE POLICY statement from the + system catalogs for a specified table and policy name. The result is a + comprehensive CREATE POLICY statement. + + Most of the functions that reconstruct (decompile) database objects have an + optional pretty flag, which if + true causes the result to be + pretty-printed. Pretty-printing adds whitespace for + legibility. Passing false for the + pretty parameter yields the same result as omitting + the parameter. + + diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index c05e4786703..e6d21a1d00e 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -552,6 +552,7 @@ static void get_formatted_string(StringInfo buf, bool pretty, int noOfTabChars, const char *fmt,...) pg_attribute_printf(4, 5); +static char *pg_get_policy_ddl_worker(Oid tableID, Name policyName, bool pretty); #define only_marker(rte) ((rte)->inh ? "" : "ONLY ") @@ -13788,12 +13789,41 @@ get_formatted_string(StringInfo buf, bool pretty, int noOfTabChars, const char * * policyName - Name of the policy for which to generate the DDL. * pretty - If true, format the DDL with indentation and line breaks. */ + Datum pg_get_policy_ddl(PG_FUNCTION_ARGS) +{ + Oid tableID = PG_GETARG_OID(0); + Name policyName = PG_GETARG_NAME(1); + char *res; + + res = pg_get_policy_ddl_worker(tableID, policyName, false); + + if (res == NULL) + PG_RETURN_NULL(); + + PG_RETURN_TEXT_P(string_to_text(res)); +} + +Datum +pg_get_policy_ddl_ext(PG_FUNCTION_ARGS) { Oid tableID = PG_GETARG_OID(0); Name policyName = PG_GETARG_NAME(1); bool pretty = PG_GETARG_BOOL(2); + char *res; + + res = pg_get_policy_ddl_worker(tableID, policyName, pretty); + + if (res == NULL) + PG_RETURN_NULL(); + + PG_RETURN_TEXT_P(string_to_text(res)); +} + +static char * +pg_get_policy_ddl_worker(Oid tableID, Name policyName, bool pretty) +{ bool attrIsNull; int prettyFlags; Datum valueDatum; @@ -13807,7 +13837,7 @@ pg_get_policy_ddl(PG_FUNCTION_ARGS) /* Validate that the relation exists */ if (!OidIsValid(tableID) || get_rel_name(tableID) == NULL) - PG_RETURN_NULL(); + return NULL; initStringInfo(&buf); @@ -13935,5 +13965,5 @@ pg_get_policy_ddl(PG_FUNCTION_ARGS) systable_endscan(sscan); table_close(pgPolicyRel, AccessShareLock); - PG_RETURN_TEXT_P(string_to_text(buf.data)); + return buf.data; } diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 536c5a857da..3bfaf34d535 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -4023,7 +4023,10 @@ prosrc => 'pg_get_function_sqlbody' }, { oid => '8811', descr => 'get CREATE statement for policy', proname => 'pg_get_policy_ddl', prorettype => 'text', - proargtypes => 'regclass name bool', prosrc => 'pg_get_policy_ddl' }, + proargtypes => 'regclass name', prosrc => 'pg_get_policy_ddl' }, +{ oid => '8812', descr => 'get CREATE statement for policy with pretty-print option', + proname => 'pg_get_policy_ddl', prorettype => 'text', + proargtypes => 'regclass name bool', prosrc => 'pg_get_policy_ddl_ext' }, { oid => '1686', descr => 'list of SQL keywords', proname => 'pg_get_keywords', procost => '10', prorows => '500',