Skip to content

Commit 988c03a

Browse files
dbgreengonzalemario
authored andcommitted
Add functions to generate DDL for recreating roles.
pg_get_role_ddl(regrole) returns the DDL needed to recreate a role as a single text string. pg_get_role_ddl_statements(regrole) returns the same thing as a set of rows, one per DDL statement. The output includes the CREATE ROLE statement with all role attributes, plus any ALTER ROLE SET configuration parameters (both role-wide and database-specific settings). Passwords cannot be included, since we can only see the hashed values. System roles (names starting with "pg_") are rejected, since users shouldn't be recreating those anyway. Co-authored-by: Mario Gonzalez <gonzalemario@gmail.com> Co-authored-by: Bryan Green <dbryan.green@gmail.com> Reviewed-by: li carol <carol.li2025@outlook.com>
1 parent b4cbc10 commit 988c03a

File tree

6 files changed

+555
-1
lines changed

6 files changed

+555
-1
lines changed

doc/src/sgml/func/func-info.sgml

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2174,7 +2174,61 @@ SELECT currval(pg_get_serial_sequence('sometable', 'id'));
21742174
</tbody>
21752175
</tgroup>
21762176
</table>
2177-
2177+
<table id="functions-object-ddl-table">
2178+
<title>Object DDL Functions</title>
2179+
<tgroup cols="1">
2180+
<thead>
2181+
<row>
2182+
<entry role="func_table_entry"><para role="func_signature">Function</para>
2183+
<para>Description</para>
2184+
</entry>
2185+
</row>
2186+
</thead>
2187+
<tbody>
2188+
<row>
2189+
<entry role="func_table_entry">
2190+
<para role="func_signature">
2191+
<indexterm>
2192+
<primary>pg_get_role_ddl</primary>
2193+
</indexterm>
2194+
<function>pg_get_role_ddl</function> ( <parameter>role</parameter> <type>regrole</type> )
2195+
<returnvalue>text</returnvalue>
2196+
</para>
2197+
<para>
2198+
Returns the DDL commands that would recreate the given role as a single text string.
2199+
The result includes the <command>CREATE ROLE</command> statement and any
2200+
<command>ALTER ROLE</command> statements needed to set role configuration parameters.
2201+
Password information is never included in the output.
2202+
</para>
2203+
<para>
2204+
Returns <literal>NULL</literal> if the role does not exist.
2205+
</para>
2206+
</entry>
2207+
</row>
2208+
<row>
2209+
<entry role="func_table_entry">
2210+
<para role="func_signature">
2211+
<indexterm>
2212+
<primary>pg_get_role_ddl_statements</primary>
2213+
</indexterm>
2214+
<function>pg_get_role_ddl_statements</function> ( <parameter>role</parameter> <type>regrole</type> )
2215+
<returnvalue>setof text</returnvalue>
2216+
</para>
2217+
<para>
2218+
Returns the DDL commands that would recreate the given role as a set of rows,
2219+
with each statement returned as a separate row. The first row contains the
2220+
<command>CREATE ROLE</command> statement, followed by any <command>ALTER ROLE</command>
2221+
statements needed to set role configuration parameters. This format is useful for
2222+
programmatic processing or when you want to filter or analyze individual statements.
2223+
</para>
2224+
<para>
2225+
Returns an empty set if the role does not exist.
2226+
</para>
2227+
</entry>
2228+
</row>
2229+
</tbody>
2230+
</tgroup>
2231+
</table>
21782232
<para>
21792233
Most of the functions that reconstruct (decompile) database objects
21802234
have an optional <parameter>pretty</parameter> flag, which

0 commit comments

Comments
 (0)