Detect indirect superusers via role membership on PG 16+ #24
Merged
Conversation
On PostgreSQL 16+, use a recursive CTE to walk pg_auth_members and detect roles that can gain superuser access via SET ROLE (set_option) or re-granting (admin_option). Adds an access_type label (direct or indirect) to the per-role metric. PG < 16 retains direct-only detection.
Prevent search_path hijack by qualifying pg_roles, pg_auth_members, casts, and equality operators with pg_catalog.
maxenglander
approved these changes
Feb 21, 2026
Collaborator
maxenglander
left a comment
There was a problem hiding this comment.
One concern, if we have a customer that has 100000+ roles, this could get computationally expensive.
I feel like between this and the concern i had with Hirad's extension metric opening too many connections, it might be worth having a way to run certain queries which (a) arent run every 30 seconds (b) may take a lot longer to run than 30s (c) can be written in a way that takes care not to use too much resources, eg via pagination.
The recursive CTE for indirect superuser detection is expensive at scale. Fall back to the simple direct-only query when the instance has >= 1000 roles.
Author
|
@maxenglander FYSA I added a pretty naive check to avoid thrash. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On PostgreSQL 16+, use a recursive CTE to walk pg_auth_members and
detect roles that can gain superuser access via SET ROLE (set_option)
or re-granting (admin_option). Adds an access_type label (direct or
indirect) to the per-role metric. PG < 16 retains direct-only detection.