Skip to content

fix(lakekeeper): drop database when admin is not the owner#650

Merged
benben merged 1 commit into
mainfrom
ben/lakekeeper-drop-db-fix-owner
Jun 1, 2026
Merged

fix(lakekeeper): drop database when admin is not the owner#650
benben merged 1 commit into
mainfrom
ben/lakekeeper-drop-db-fix-owner

Conversation

@benben
Copy link
Copy Markdown
Member

@benben benben commented Jun 1, 2026

Follow-up to #649. The drop wired in there hit a real-world ownership
mismatch in mw-dev:

```
drop database lakekeeper_ben_ext_ice: ERROR: must be owner of
database lakekeeper_ben_ext_ice (SQLSTATE 42501)
drop role lakekeeper_ben_ext_ice: ERROR: role
"lakekeeper_ben_ext_ice" cannot be dropped because some objects
depend on it (SQLSTATE 2BP01)
```

`EnsureRole` hands ownership to the per-tenant role
(`ALTER DATABASE OWNER TO ` + `ALTER SCHEMA public OWNER TO`),
so the `lakekeeper_` DB is owned by the tenant, not by the
admin. On shared RDS (external metadata case), the admin is a
non-superuser tenant of the shared cluster
(`ducklingexample`), so:

  • `DROP DATABASE` fails with 42501 (must be owner).
  • The follow-up `DROP ROLE` fails with 2BP01 because the role
    still owns the database.

Fix

DropDatabase:

  • probe `pg_database`; no-op if absent
  • look up the current owner, `GRANT` it to `CURRENT_USER`, then
    `ALTER DATABASE OWNER TO CURRENT_USER` before `DROP DATABASE`
  • `GRANT` + `ALTER OWNER` are best-effort so the path stays correct
    when the admin is already the owner or is a superuser

DropRole:

  • probe `pg_roles`; no-op if absent
  • `GRANT` the role to `CURRENT_USER` so `REASSIGN OWNED` +
    `DROP OWNED` can run on shared RDS (PG14+ requires role-membership)
  • `REASSIGN OWNED` first (handles owned database objects), then
    `DROP OWNED CASCADE` (cluster-wide grants + default privileges),
    then `DROP ROLE`. The first two are best-effort; only the final
    `DROP ROLE` failure is returned

🤖 Generated with Claude Code

#649 wired DropDatabase+DropRole into DeleteForOrg, but the actual
teardown failed in mw-dev with:

  drop database lakekeeper_ben_ext_ice: ERROR: must be owner of
    database lakekeeper_ben_ext_ice (SQLSTATE 42501)
  drop role lakekeeper_ben_ext_ice: ERROR: role
    "lakekeeper_ben_ext_ice" cannot be dropped because some objects
    depend on it (SQLSTATE 2BP01)

EnsureRole runs ALTER DATABASE OWNER TO <role> + ALTER SCHEMA public
OWNER TO <role>, so the lakekeeper_<orgid> DB is owned by the
per-tenant role, not by the admin. On shared RDS (external metadata
case), the admin is a non-superuser tenant of the shared cluster
(e.g. ducklingexample), so DROP DATABASE fails with 42501 and the
subsequent DROP ROLE fails with 2BP01 because the role still owns
the database.

DropDatabase:
- probe pg_database; no-op if absent
- look up the current owner, GRANT it to CURRENT_USER, then
  ALTER DATABASE OWNER TO CURRENT_USER before DROP DATABASE
- GRANT + ALTER OWNER are best-effort so the path stays correct when
  the admin already is the owner / is a superuser

DropRole:
- probe pg_roles; no-op if absent
- GRANT the role to CURRENT_USER so REASSIGN OWNED + DROP OWNED can
  run on shared RDS (the operations require role-membership in PG14+)
- REASSIGN OWNED first (handles owned database objects), then
  DROP OWNED CASCADE (cluster-wide grants + default privileges),
  then DROP ROLE. The first two are best-effort; only DROP ROLE
  failure is returned.
@benben benben merged commit fb94562 into main Jun 1, 2026
22 checks passed
@benben benben deleted the ben/lakekeeper-drop-db-fix-owner branch June 1, 2026 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant