From 83ade5dcc26ab506a8935dbee8d4f56edb991272 Mon Sep 17 00:00:00 2001 From: Michal Charemza Date: Mon, 17 Feb 2025 11:50:28 +0000 Subject: [PATCH] Update the query that fetches roles to be faster with many roles. This changes the query that fetches roles from using the pg_catalog.shobj_description function to using an explicit join on pg_catalog.pg_shdescription. In my specific case, in a production system with over 20k roles, this reduces the query time down from 8 minutes(!) down to ~40ms. Just in case it was a locking issue, I did use pg_activity to investigate locks, and nothing was being blocked during the 8 minutes. This fixes a problem that exists only for certain users, and specifically in my case where the user that runs the query is themselves a member (directly and indirectly) of a high number of roles. Running the existing query as a user that has a low number of roles (and specifically, the master user in an AWS RDS database), the existing query is fairly performant. My best explanation as to the performance difference comes from the definition of shobj_description https://github.com/postgres/postgres/blob/2a8a00674e973dec4d7d74d5e16c7d7cdedf2be5/src/backend/catalog/system_functions.sql#L303-L312. Each time it's called it runs a query against pg_shdescription and pg_class, and so in my case runs 20k times, compared to the equivalent of once when doing the explicit join on pg_shdescription. It should also be noted that in my case pg_class is quite big - currently about 500k rows (although it has grown to ~5 million in the past, but that was borderline a mistake). See https://github.com/pgadmin-org/pgadmin4/pull/8457#issue-2853052114 for EXPLAIN ANALYZE results of the existing query and the updated query. --- .../servers/roles/templates/roles/sql/default/nodes.sql | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/web/pgadmin/browser/server_groups/servers/roles/templates/roles/sql/default/nodes.sql b/web/pgadmin/browser/server_groups/servers/roles/templates/roles/sql/default/nodes.sql index 28a03dea8..9ee5aa2c9 100644 --- a/web/pgadmin/browser/server_groups/servers/roles/templates/roles/sql/default/nodes.sql +++ b/web/pgadmin/browser/server_groups/servers/roles/templates/roles/sql/default/nodes.sql @@ -1,8 +1,8 @@ SELECT - r.oid, r.rolname, r.rolcanlogin, r.rolsuper, - pg_catalog.shobj_description(r.oid, 'pg_authid') AS description -FROM - pg_catalog.pg_roles r + r.oid, r.rolname, r.rolcanlogin, r.rolsuper, d.description +FROM pg_catalog.pg_roles r + LEFT JOIN pg_catalog.pg_shdescription d + ON d.objoid = r.oid AND d.classoid = 'pg_catalog.pg_authid'::regclass {% if rid %} WHERE r.oid = {{ rid|qtLiteral(conn) }}::oid {% endif %}