Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d913928

Browse files
committed
psql: Add support for \dpS and \zS.
This allows an optional "S" modifier to be added to \dp and \z, to have them include system objects in the list. Note that this also changes the behaviour of a bare \dp or \z without the "S" modifier to include temp objects in the list, and exclude information_schema objects, making them consistent with other psql meta-commands. Nathan Bossart, reviewed by Maxim Orlov. Discussion: https://postgr.es/m/20221206193606.GB3078082@nathanxps13
1 parent 2b6df05 commit d913928

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

doc/src/sgml/ref/psql-ref.sgml

+8-4
Original file line numberDiff line numberDiff line change
@@ -1825,14 +1825,16 @@ INSERT INTO tbl1 VALUES ($1, $2) \bind 'first value' 'second value' \g
18251825

18261826

18271827
<varlistentry>
1828-
<term><literal>\dp [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
1828+
<term><literal>\dp[S] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
18291829
<listitem>
18301830
<para>
18311831
Lists tables, views and sequences with their
18321832
associated access privileges.
18331833
If <replaceable class="parameter">pattern</replaceable> is
18341834
specified, only tables, views and sequences whose names match the
1835-
pattern are listed.
1835+
pattern are listed. By default only user-created objects are shown;
1836+
supply a pattern or the <literal>S</literal> modifier to include
1837+
system objects.
18361838
</para>
18371839

18381840
<para>
@@ -3575,14 +3577,16 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
35753577

35763578

35773579
<varlistentry>
3578-
<term><literal>\z [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
3580+
<term><literal>\z[S] [ <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link> ]</literal></term>
35793581
<listitem>
35803582
<para>
35813583
Lists tables, views and sequences with their
35823584
associated access privileges.
35833585
If a <replaceable class="parameter">pattern</replaceable> is
35843586
specified, only tables, views and sequences whose names match the
3585-
pattern are listed.
3587+
pattern are listed. By default only user-created objects are shown;
3588+
supply a pattern or the <literal>S</literal> modifier to include
3589+
system objects.
35863590
</para>
35873591

35883592
<para>

src/bin/psql/command.c

+15-8
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ static backslashResult exec_command_write(PsqlScanState scan_state, bool active_
140140
static backslashResult exec_command_watch(PsqlScanState scan_state, bool active_branch,
141141
PQExpBuffer query_buf, PQExpBuffer previous_buf);
142142
static backslashResult exec_command_x(PsqlScanState scan_state, bool active_branch);
143-
static backslashResult exec_command_z(PsqlScanState scan_state, bool active_branch);
143+
static backslashResult exec_command_z(PsqlScanState scan_state, bool active_branch,
144+
const char *cmd);
144145
static backslashResult exec_command_shell_escape(PsqlScanState scan_state, bool active_branch);
145146
static backslashResult exec_command_slash_command_help(PsqlScanState scan_state, bool active_branch);
146147
static char *read_connect_arg(PsqlScanState scan_state);
@@ -413,8 +414,8 @@ exec_command(const char *cmd,
413414
query_buf, previous_buf);
414415
else if (strcmp(cmd, "x") == 0)
415416
status = exec_command_x(scan_state, active_branch);
416-
else if (strcmp(cmd, "z") == 0)
417-
status = exec_command_z(scan_state, active_branch);
417+
else if (strcmp(cmd, "z") == 0 || strcmp(cmd, "zS") == 0)
418+
status = exec_command_z(scan_state, active_branch, cmd);
418419
else if (strcmp(cmd, "!") == 0)
419420
status = exec_command_shell_escape(scan_state, active_branch);
420421
else if (strcmp(cmd, "?") == 0)
@@ -875,7 +876,7 @@ exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd)
875876
success = listCollations(pattern, show_verbose, show_system);
876877
break;
877878
case 'p':
878-
success = permissionsList(pattern);
879+
success = permissionsList(pattern, show_system);
879880
break;
880881
case 'P':
881882
{
@@ -2822,16 +2823,22 @@ exec_command_x(PsqlScanState scan_state, bool active_branch)
28222823
* \z -- list table privileges (equivalent to \dp)
28232824
*/
28242825
static backslashResult
2825-
exec_command_z(PsqlScanState scan_state, bool active_branch)
2826+
exec_command_z(PsqlScanState scan_state, bool active_branch, const char *cmd)
28262827
{
28272828
bool success = true;
28282829

28292830
if (active_branch)
28302831
{
2831-
char *pattern = psql_scan_slash_option(scan_state,
2832-
OT_NORMAL, NULL, true);
2832+
char *pattern;
2833+
bool show_system;
2834+
2835+
pattern = psql_scan_slash_option(scan_state,
2836+
OT_NORMAL, NULL, true);
2837+
2838+
show_system = strchr(cmd, 'S') ? true : false;
2839+
2840+
success = permissionsList(pattern, show_system);
28332841

2834-
success = permissionsList(pattern);
28352842
free(pattern);
28362843
}
28372844
else

src/bin/psql/describe.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ listAllDbs(const char *pattern, bool verbose)
10021002
* \z (now also \dp -- perhaps more mnemonic)
10031003
*/
10041004
bool
1005-
permissionsList(const char *pattern)
1005+
permissionsList(const char *pattern, bool showSystem)
10061006
{
10071007
PQExpBufferData buf;
10081008
PGresult *res;
@@ -1121,15 +1121,13 @@ permissionsList(const char *pattern)
11211121
CppAsString2(RELKIND_FOREIGN_TABLE) ","
11221122
CppAsString2(RELKIND_PARTITIONED_TABLE) ")\n");
11231123

1124-
/*
1125-
* Unless a schema pattern is specified, we suppress system and temp
1126-
* tables, since they normally aren't very interesting from a permissions
1127-
* point of view. You can see 'em by explicit request though, eg with \z
1128-
* pg_catalog.*
1129-
*/
1124+
if (!showSystem && !pattern)
1125+
appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
1126+
" AND n.nspname <> 'information_schema'\n");
1127+
11301128
if (!validateSQLNamePattern(&buf, pattern, true, false,
11311129
"n.nspname", "c.relname", NULL,
1132-
"n.nspname !~ '^pg_' AND pg_catalog.pg_table_is_visible(c.oid)",
1130+
"pg_catalog.pg_table_is_visible(c.oid)",
11331131
NULL, 3))
11341132
goto error_return;
11351133

src/bin/psql/describe.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extern bool describeRoles(const char *pattern, bool verbose, bool showSystem);
3838
extern bool listDbRoleSettings(const char *pattern, const char *pattern2);
3939

4040
/* \z (or \dp) */
41-
extern bool permissionsList(const char *pattern);
41+
extern bool permissionsList(const char *pattern, bool showSystem);
4242

4343
/* \ddp */
4444
extern bool listDefaultACLs(const char *pattern);

0 commit comments

Comments
 (0)