Skip to content

Commit 83ade5d

Browse files
authored
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 #8457 (comment) for EXPLAIN ANALYZE results of the existing query and the updated query.
1 parent 5c257fe commit 83ade5d

File tree

1 file changed

+4
-4
lines changed
  • web/pgadmin/browser/server_groups/servers/roles/templates/roles/sql/default

1 file changed

+4
-4
lines changed

web/pgadmin/browser/server_groups/servers/roles/templates/roles/sql/default/nodes.sql

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
SELECT
2-
r.oid, r.rolname, r.rolcanlogin, r.rolsuper,
3-
pg_catalog.shobj_description(r.oid, 'pg_authid') AS description
4-
FROM
5-
pg_catalog.pg_roles r
2+
r.oid, r.rolname, r.rolcanlogin, r.rolsuper, d.description
3+
FROM pg_catalog.pg_roles r
4+
LEFT JOIN pg_catalog.pg_shdescription d
5+
ON d.objoid = r.oid AND d.classoid = 'pg_catalog.pg_authid'::regclass
66
{% if rid %}
77
WHERE r.oid = {{ rid|qtLiteral(conn) }}::oid
88
{% endif %}

0 commit comments

Comments
 (0)