Search code examples
postgresqlrolesrow-level-security

How to check if the current user inherits the BYPASSRLS attribute from a role?


I need to check whether row-level-security applies to the current_user in a Postgres query. As far as I can tell, I need to check whether the current user is the owner of that table, or has the BYPASSRLS attribute, or is member of a role that has this. That group membership check seems cumbersome though, especially considering the NOINHERIT attribute of some roles… How to achieve this?


Solution

  • I need to check whether row-level-security applies to the current_user

    Indeed there is a much simpler way to check that - in particular for a specific table, which the user (or one of the inherited roles) might be owner of. The solution is to use the row_security_active function:

    row_security_active checks whether row level security is active for the specified table in the context of the current_user and environment. The table can be specified by name or by OID.

    So just use

    SELECT row_security_active('schema.table');