Allow pg_read_all_stats to see database size in \l+

Started by Christoph Berg4 days ago8 messageshackers
Jump to latest
#1Christoph Berg
myon@debian.org

psql \l+ didn't consider pg_read_all_stats when determining whether
the user was allowed to see the database size.

Split off from the "Available disk space per tablespace" patch since
it's really an independent fix. Thanks Zsolt for pointing that out.

This might be considered for backpatching.

Christoph

Attachments:

v1-0001-Allow-pg_read_all_stats-to-see-database-size-in-l.patchtext/x-diff; charset=us-asciiDownload+2-2
#2Fujii Masao
masao.fujii@gmail.com
In reply to: Christoph Berg (#1)
Re: Allow pg_read_all_stats to see database size in \l+

On Wed, Jul 22, 2026 at 8:26 PM Christoph Berg <myon@debian.org> wrote:

psql \l+ didn't consider pg_read_all_stats when determining whether
the user was allowed to see the database size.

+1 to this fix.

Split off from the "Available disk space per tablespace" patch since
it's really an independent fix. Thanks Zsolt for pointing that out.

This might be considered for backpatching.

The pg_read_all_stats check should be fine in master, since psql there
no longer supports pre-v10 servers, which don't have that role. But,
if this is backpatched, psql still needs to work with pre-v10 servers,
so we'll probably need a server version check (e.g., pset.sversion >= 100000)
before checking for pg_read_all_stats, at least in the older stable branches.

Also, should the following description of the \l+ meta-command in
the psql docs be updated?

(Size information is only available for databases that the current
user can connect to.)

Regards,

--
Fujii Masao

#3Christoph Berg
myon@debian.org
In reply to: Fujii Masao (#2)
Re: Allow pg_read_all_stats to see database size in \l+

Thanks for the review!

Re: Fujii Masao

The pg_read_all_stats check should be fine in master, since psql there
no longer supports pre-v10 servers, which don't have that role. But,
if this is backpatched, psql still needs to work with pre-v10 servers,
so we'll probably need a server version check (e.g., pset.sversion >= 100000)
before checking for pg_read_all_stats, at least in the older stable branches.

The SQL-generating code there is already quite complex, and since no
one complained, perhaps just skip the backpatching if it's complicated.

I tried the patch back to version 9.3 and the query still works even
when the role doesn't exist there. (9.2 and earlier failed due to the
protocol version grease.)

Also, should the following description of the \l+ meta-command in
the psql docs be updated?

(Size information is only available for databases that the current
user can connect to.)

Done in v2.

Christoph

Attachments:

v2-0001-Allow-pg_read_all_stats-to-see-database-size-in-l.patchtext/x-diff; charset=us-asciiDownload+5-4
#4Alvaro Herrera
alvherre@2ndquadrant.com
In reply to: Christoph Berg (#3)
Re: Allow pg_read_all_stats to see database size in \l+

On 2026-Jul-22, Christoph Berg wrote:

Thanks for the review!

Re: Fujii Masao

The pg_read_all_stats check should be fine in master, since psql there
no longer supports pre-v10 servers, which don't have that role. But,
if this is backpatched, psql still needs to work with pre-v10 servers,
so we'll probably need a server version check (e.g., pset.sversion >= 100000)
before checking for pg_read_all_stats, at least in the older stable branches.

The SQL-generating code there is already quite complex, and since no
one complained, perhaps just skip the backpatching if it's complicated.

I think it's simple enough:

@@ -1069,12 +1069,16 @@ listAllDbs(const char *pattern, bool verbose)
 	appendPQExpBufferStr(&buf, "       ");
 	printACLColumn(&buf, "d.datacl");
 	if (verbose && pset.sversion >= 80200)
+	{
 		appendPQExpBuffer(&buf,
 						  ",\n       CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')\n"
+						  "          %s"
 						  "            THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))\n"
 						  "            ELSE 'No Access'\n"
 						  "       END as \"%s\"",
+						  pset.sversion >= 100000 ? "OR pg_catalog.pg_has_role('pg_read_all_stats', 'USAGE')\n" : "",
 						  gettext_noop("Size"));
+	}
 	if (verbose && pset.sversion >= 80000)
 		appendPQExpBuffer(&buf,
 						  ",\n       t.spcname as \"%s\"",

--
Álvaro Herrera Breisgau, Deutschland — https://www.EnterpriseDB.com/

#5Christoph Berg
myon@debian.org
In reply to: Alvaro Herrera (#4)
Re: Allow pg_read_all_stats to see database size in \l+

Re: Álvaro Herrera

+ pset.sversion >= 100000 ? "OR pg_catalog.pg_has_role('pg_read_all_stats', 'USAGE')\n" : "",

Should I create patch files for 14..19?

Christoph

#6Fujii Masao
masao.fujii@gmail.com
In reply to: Christoph Berg (#5)
Re: Allow pg_read_all_stats to see database size in \l+

On Wed, Jul 22, 2026 at 11:52 PM Christoph Berg <myon@debian.org> wrote:

Re: Álvaro Herrera

+ pset.sversion >= 100000 ? "OR pg_catalog.pg_has_role('pg_read_all_stats', 'USAGE')\n" : "",

Should I create patch files for 14..19?

I've prepared patches for the older stable branches and attached them.

While doing so, I also updated the documentation changes.

Regards,

--
Fujii Masao

Attachments:

v3-0001-pg14-psql-Allow-pg_read_all_stats-to-see-database-size.txttext/plain; charset=US-ASCII; name=v3-0001-pg14-psql-Allow-pg_read_all_stats-to-see-database-size.txtDownload+7-3
v3-0001-pg16_pg19-psql-Allow-pg_read_all_stats-to-see-database-size.txttext/plain; charset=US-ASCII; name=v3-0001-pg16_pg19-psql-Allow-pg_read_all_stats-to-see-database-size.txtDownload+7-3
v3-0001-pg15-psql-Allow-pg_read_all_stats-to-see-database-size.txttext/plain; charset=US-ASCII; name=v3-0001-pg15-psql-Allow-pg_read_all_stats-to-see-database-size.txtDownload+7-3
v3-0001-pg20-psql-Allow-pg_read_all_stats-to-see-database-size.patchapplication/octet-stream; name=v3-0001-pg20-psql-Allow-pg_read_all_stats-to-see-database-size.patchDownload+5-4
#7Christoph Berg
myon@debian.org
In reply to: Fujii Masao (#6)
Re: Allow pg_read_all_stats to see database size in \l+

Re: Fujii Masao

I've prepared patches for the older stable branches and attached them.

While doing so, I also updated the documentation changes.

Looks good to me, thanks!

I tested the PG15 patch and it works, including against a 9.6 server.

Christoph

#8Fujii Masao
masao.fujii@gmail.com
In reply to: Christoph Berg (#7)
Re: Allow pg_read_all_stats to see database size in \l+

On Thu, Jul 23, 2026 at 7:44 PM Christoph Berg <myon@debian.org> wrote:

Looks good to me, thanks!

I tested the PG15 patch and it works, including against a 9.6 server.

Thanks for the review and test! I've pushed the patch.

Regards,

--
Fujii Masao