DEV Community

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

Posted on Edited on

Postgres said DROP OWNED worked. DROP ROLE disagreed.

Update: Sunnatulla showed in the comments that pinning wasn't the cause. The grantor is, for DROP OWNED as well as REVOKE. I've corrected the explanation below.

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 succeeded, and as far as we knew it removes every privilege the role holds. Postgres was now telling us a privilege remained.

Why it was hard

Two 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 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, and a revoke only removes the grants made by the role doing the revoking. A superuser revokes as the object's owner, so our REVOKE targeted the owner's grants. DROP OWNED works the same way: it revokes the role's privileges as the current user, so it only clears grants that user could have made. The grant we needed to remove had been made by a different role, so both missed it.

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.

One shortcut that looks like it should work doesn't: REVOKE ... GRANTED BY grantor. For object privileges, the grantor named there has to be the current user, so it doesn't save you the role switch.

Checking it's really gone

After the loop, ask pg_shdepend what still references the role. It's a shared catalog, so one query sees every database:

SELECT dbid, classid::regclass, objid, deptype
FROM pg_shdepend
WHERE refobjid = 'r'::regrole;
Enter fullscreen mode Exit fullscreen mode

No rows means DROP ROLE will go through. Any rows tell you which database and catalog still hold something. Grants in other databases and ALTER DEFAULT PRIVILEGES entries are the usual leftovers, and a loop in one database misses them.

What I took away

REVOKE is scoped to the grantor, and that includes DROP OWNED. "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.

DROP OWNED only clears what you could have granted. If an ordinary role with a grant option handed out privileges, DROP OWNED run as a superuser won't remove them. Check aclexplode for grantors that aren't you.

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 (4)

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
 
niyigbodo profile image
Niyi Gbodimowo • • Edited

Hi @sunnatulla_dev Thanks for rerunning it on 17.12, that's a cleaner repro than mine, and you're right. I've updated the post: pinning wasn't the cause, the grantor was. In your test and mine, DROP OWNED BY r cleared the grant a superuser had made but left the one made by another ordinary role, which is why DROP ROLE still failed. That fits how Postgres revokes: you only remove grants made by the role you're acting as, and that applies to DROP OWNED as much as to a plain REVOKE.

The REVOKE ... GRANTED BY dead end is a good catch too. For object privileges the grantor named there has to be the current user, so it doesn't save you the role switch. And DROP OWNED BY r, grantor is only safe when the grantor is being removed as well, since it also drops everything the grantor owns. Easy to miss.

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?

Collapse
 
niyigbodo profile image
Niyi Gbodimowo •

Hi @lycore Yes, that's the shape I ended up with: walk the ACLs with aclexplode, group the entries by grantor, and inside one transaction SET LOCAL ROLE ; REVOKE ... for each group. SET LOCAL means a failure can't leave the session as the wrong role.

I don't rely on the DROP ROLE error as the check, because it only tells you that something is left, not what. After the loop I query pg_shdepend for the role's oid. It's a shared catalog, so from one database you can see entries for every database (the dbid column). That's the part a per-database aclexplode loop misses: grants on objects in other databases, and default privileges set with ALTER DEFAULT PRIVILEGES, which live in pg_default_acl rather than on the objects. If pg_shdepend comes back empty, DROP ROLE goes through. If it doesn't, the rows say exactly which database and catalog to look in.