DEV Community

Cover image for Postgres said DROP OWNED worked. DROP ROLE disagreed.
Niyi Gbodimowo
Niyi Gbodimowo

Posted on

Postgres said DROP OWNED worked. DROP ROLE disagreed.

I'm the founder of Kilimanjaro, and this week's note is from the unglamorous end of the job: tearing down.

When a customer project is removed, we remove the database role that belonged to it. It is the last step of a cleanup that is supposed to leave nothing behind. One day that last step started failing, and it kept failing in a way that made no sense for about an afternoon.

The symptom

The cleanup did what every Postgres guide says to do:

DROP OWNED BY r;
DROP ROLE r;
Enter fullscreen mode Exit fullscreen mode

DROP OWNED reported success. DROP ROLE then failed, every time:

ERROR:  role "r" cannot be dropped because some objects depend on it
DETAIL:  privileges for function pg_advisory_lock(bigint)
Enter fullscreen mode Exit fullscreen mode

That message is the whole mystery. DROP OWNED had just said it removed everything the role owned and every privilege it held. Postgres was now telling us a privilege remained.

Why it was hard

Three things stacked up.

First, DROP OWNED really did succeed at what it does. The role owned nothing and the usual grants were gone, so the places we looked first were clean.

Second, the leftover was a grant on a built-in. pg_advisory_lock lives in pg_catalog, and objects there are pinned: DROP OWNED skips them on purpose, so that you can never accidentally take away part of Postgres itself.

Third, the obvious fix did nothing. We connected as a superuser and ran a plain REVOKE:

REVOKE ALL ON FUNCTION pg_advisory_lock(bigint) FROM r;
Enter fullscreen mode Exit fullscreen mode

It ran without complaint, and DROP ROLE still failed. I assumed a superuser could remove any grant. It cannot, in the sense that matters here.

The cause

A grant in Postgres is recorded together with who made it. When you run REVOKE, you only remove the grants you made. A superuser acts as the function's owner by default, so the revoke targeted the owner's grants, and the grant we needed to remove had been made by a different role.

In our case an earlier piece of setup code had granted EXECUTE on the advisory-lock functions to the project's role, and it had done so while connected as a role with a grant option, not as a superuser. Every grant was stamped with that grantor. Nobody else's REVOKE could remove it.

You can see this directly. The proacl column on pg_proc holds the grants, and aclexplode turns it into rows:

SELECT p.oid::regprocedure AS fn,
       a.grantor::regrole  AS grantor,
       a.grantee::regrole  AS grantee,
       a.privilege_type
FROM pg_proc p, aclexplode(p.proacl) a
WHERE a.grantee = 'r'::regrole;
Enter fullscreen mode Exit fullscreen mode
            fn            | grantor | grantee | privilege_type
--------------------------+---------+---------+----------------
 pg_advisory_lock(bigint) | grantor | r       | EXECUTE
Enter fullscreen mode Exit fullscreen mode

There was the answer in one row: a grantor column that was not the role we were running as.

The fix

Revoke as the grantor, inside a transaction so the role switch cannot leak:

BEGIN;
SET LOCAL ROLE grantor;
REVOKE ALL ON FUNCTION pg_advisory_lock(bigint) FROM r;
COMMIT;

DROP ROLE r;
Enter fullscreen mode Exit fullscreen mode

DROP ROLE succeeded. In the real cleanup there were several functions, so the code loops over the aclexplode rows, and for each one it sets the grantor role locally, revokes on that exact regprocedure, and moves on.

What I took away

REVOKE is scoped to the grantor. "I'm a superuser, so I can undo anything" is not true of grants. If you want a grant gone, you have to be the role that made it, or be able to become that role.

Pinned system objects escape DROP OWNED. Anything you granted on a built-in function is yours to clean up by hand.

Teardown code has to mirror setup code. If your setup grants on built-ins, your teardown has to revoke as the same grantor. The easiest way to guarantee that is to write them together, and to test teardown on a role that was set up by the real setup path, not by a hand-made fixture.

There is a second lesson underneath, and it is the one I keep returning to. Least-privilege grants on system functions are easy to add and awkward to take back. That is a reason to be careful about adding them, and a reason to remove them deliberately when the role goes.

If you have hit a teardown problem that looks like this one, tell me in the comments. The odd ones make the best notes.

See you next Thursday.

Niyi

Top comments (2)

Collapse
 
sunnatulla_dev profile image
Sunnatulla •

I reran this on 17.12 and I don't think pinning is the reason. If the superuser grants EXECUTE on pg_advisory_lock to r, DROP OWNED BY r removes it fine and DROP ROLE works. Only the grant made by the other role survives, so it's the grantor thing alone. Also REVOKE ... GRANTED BY grantor doesn't save you the role switch, it errors with "grantor must be current user". DROP OWNED BY r, grantor clears it in one go, but only if grantor is going away too.

Collapse
 
lycore profile image
Lycore Development •

The point that REVOKE only removes grants made by the current grantor is the part I'd have gotten wrong too. Looping over aclexplode and using SET LOCAL ROLE per grantor is a neat way to keep the teardown generic. Do you also check pg_shdepend for leftovers after the loop, or is the DROP ROLE error itself your safety net?