Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
SQLSTATE 0LP01 (invalid_grant_operation) appears on GRANT role TO role when the grant would create a cycle. The message role "A" is a member of role "B" says that A already belongs to B, directly or through other roles, so B cannot also become a member of A.
We reproduced a direct reverse grant and an indirect cycle through three roles on PostgreSQL 17. In both, the existing membership already did what was needed.
01Granting a role back in the reverse direction
app_read was already granted to app_write (writers can read). GRANT app_write TO app_read would close the loop, so PostgreSQL refused it.
Setup
CREATE ROLE app_read;
CREATE ROLE app_write;
GRANT app_read TO app_write;CREATE ROLE
CREATE ROLE
GRANT ROLECREATE TABLE orders (id int PRIMARY KEY, amount int);
INSERT INTO orders VALUES (1, 12000);
GRANT USAGE ON SCHEMA lab_a TO app_read;
GRANT SELECT ON orders TO app_read;
GRANT INSERT ON orders TO app_write;CREATE TABLE
INSERT 0 1
GRANT
GRANT
GRANTGRANT app_write TO app_read;ERROR: 0LP01: role "app_write" is a member of role "app_read"SELECT r.rolname AS role, m.rolname AS member
FROM pg_auth_members a
JOIN pg_roles r ON r.oid = a.roleid
JOIN pg_roles m ON m.oid = a.member
WHERE r.rolname LIKE 'app\_%'; role | member
----------+-----------
app_read | app_write
(1 row)pg_auth_members shows role app_read with member app_write.
GRANT app_write TO lab_b;GRANT ROLEKeep one direction. Granting app_write to the user lab_b was enough: lab_b could INSERT into lab_a.orders (from app_write) and SELECT it (from app_read through app_write).
INSERT INTO lab_a.orders VALUES (2, 8000);
SELECT * FROM lab_a.orders ORDER BY id;INSERT 0 1
id | amount
----+--------
1 | 12000
2 | 8000
(2 rows)02An indirect cycle through several roles
The chain was sales_read → sales_write → sales_admin. GRANT sales_admin TO sales_read failed with role "sales_admin" is a member of role "sales_read": sales_admin is already in sales_read through sales_write, even though no direct grant exists.
Setup
CREATE ROLE sales_read;
CREATE ROLE sales_write;
CREATE ROLE sales_admin;
GRANT sales_read TO sales_write;
GRANT sales_write TO sales_admin;CREATE ROLE
CREATE ROLE
CREATE ROLE
GRANT ROLE
GRANT ROLECREATE TABLE sales (id int PRIMARY KEY, amount int);
INSERT INTO sales VALUES (1, 5000);
GRANT USAGE ON SCHEMA lab_a TO sales_read;
GRANT SELECT ON sales TO sales_read;CREATE TABLE
INSERT 0 1
GRANT
GRANTGRANT sales_admin TO sales_read;ERROR: 0LP01: role "sales_admin" is a member of role "sales_read"SELECT pg_has_role('sales_admin', 'sales_read', 'MEMBER') AS admin_has_read,
pg_has_role('sales_read', 'sales_admin', 'MEMBER') AS read_has_admin; admin_has_read | read_has_admin
----------------+----------------
t | f
(1 row)pg_has_role('sales_admin', 'sales_read', 'MEMBER') is t and the reverse is f.
GRANT sales_admin TO lab_b;GRANT ROLENo reverse grant was needed. After GRANT sales_admin TO lab_b, lab_b could read lab_a.sales, a privilege given only to sales_read.
SELECT * FROM lab_a.sales; id | amount
----+--------
1 | 5000
(1 row)FAQ
What does "role A is a member of role B" mean on GRANT?
A is already inside B, directly or through other roles. Our GRANT tried to put B inside A, which would make a loop, so it failed with 0LP01.
How do I check role membership, including indirect?
pg_has_role('member_role', 'group_role', 'MEMBER') counts indirect membership. Direct grants are in pg_auth_members.
How do I give a user both read and write roles?
Grant the higher role to the user. In our test GRANT app_write TO lab_b gave both INSERT and SELECT, because app_write already contained app_read.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12