Skip to content

Commit adba4b3

Browse files
MisterRaindrophackorum
authored andcommitted
psql: filter system functions before visibility checks in \df
1 parent b597835 commit adba4b3

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

src/bin/psql/describe.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,28 @@ describeFunctions(const char *functypes, const char *func_pattern,
546546
appendPQExpBufferStr(&buf, " )\n");
547547
}
548548

549+
/*
550+
* Use p.pronamespace here, rather than n.nspname, so that the
551+
* system schemas can be filtered during the pg_proc scan, before calling
552+
* pg_function_is_visible(). The latter can populate catalog caches for
553+
* every function it examines. The ARRAY subquery becomes an InitPlan and
554+
* quietly ignores either schema if it does not exist.
555+
*/
556+
if (!showSystem && !func_pattern)
557+
{
558+
if (have_where)
559+
appendPQExpBufferStr(&buf, " AND ");
560+
else
561+
{
562+
appendPQExpBufferStr(&buf, "WHERE ");
563+
have_where = true;
564+
}
565+
appendPQExpBufferStr(&buf,
566+
"p.pronamespace <> ALL (ARRAY(\n"
567+
" SELECT oid FROM pg_catalog.pg_namespace\n"
568+
" WHERE nspname IN ('pg_catalog', 'information_schema')))\n");
569+
}
570+
549571
if (!validateSQLNamePattern(&buf, func_pattern, have_where, false,
550572
"n.nspname", "p.proname", NULL,
551573
"pg_catalog.pg_function_is_visible(p.oid)",
@@ -586,10 +608,6 @@ describeFunctions(const char *functypes, const char *func_pattern,
586608
}
587609
}
588610

589-
if (!showSystem && !func_pattern)
590-
appendPQExpBufferStr(&buf, " AND n.nspname <> 'pg_catalog'\n"
591-
" AND n.nspname <> 'information_schema'\n");
592-
593611
appendPQExpBufferStr(&buf, "ORDER BY 1, 2, 4;");
594612

595613
res = PSQLexec(buf.data);

src/bin/psql/t/001_basic.pl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ sub psql_fails_like
7575
psql_like($node, '\copyright', qr/Copyright/, '\copyright');
7676
psql_like($node, '\help', qr/ALTER/, '\help without arguments');
7777
psql_like($node, '\help SELECT', qr/SELECT/, '\help with argument');
78+
psql_like(
79+
$node,
80+
'\set ECHO_HIDDEN on
81+
\df',
82+
qr{
83+
WHERE \s+ p\.pronamespace \s+ <> \s+ ALL \s+ \(ARRAY\(
84+
.*? SELECT \s+ oid \s+ FROM \s+ pg_catalog\.pg_namespace
85+
.*? WHERE \s+ nspname \s+ IN \s+
86+
\('pg_catalog', \s+ 'information_schema'\)\)\)
87+
.*? AND \s+ pg_catalog\.pg_function_is_visible\(p\.oid\)
88+
}xs,
89+
'\df filters system functions before testing visibility');
7890

7991
# Test clean handling of unsupported replication command responses
8092
psql_fails_like(

0 commit comments

Comments
 (0)