Skip to content

fix: terminate backends before dropping databases and close pg-cache pools in codegen#847

Merged
pyramation merged 1 commit intomainfrom
devin/1773774783-fix-dropdb-terminate-backends
Mar 17, 2026
Merged

fix: terminate backends before dropping databases and close pg-cache pools in codegen#847
pyramation merged 1 commit intomainfrom
devin/1773774783-fix-dropdb-terminate-backends

Conversation

@pyramation
Copy link
Contributor

Summary

Fixes the systemic dropdb failure when ephemeral databases have lingering connections:

dropdb: error: database removal failed: ERROR: database "codegen_pgpm_..." is being accessed by other users
DETAIL: There are 2 other sessions using the database.

Two-layer fix:

  1. Safety net in DbAdmin.safeDropDb() (pgsql-client): When dropdb fails due to active sessions, terminates backends via pg_terminate_backend and retries — only in the catch path, so the happy path is unchanged and we get log visibility into when this happens.

  2. Proper pool cleanup in codegen PGPM paths (graphql/codegen): deployPgpm() and getPgPool() cache connections in pg-cache that were never released before the ephemeral database was dropped. Now explicitly calls pgCache.delete(dbName) + waitForDisposals() before teardown in both PgpmModuleSchemaSource.fetch() and generateMulti() shared source cleanup.

Review & Testing Checklist for Human

  • Verify pgCache.delete() key matches getPgPool() key: getPgPool() caches by config.database (the database name). The cleanup uses dbConfig.database / shared.ephemeralDb.config.database. Confirm these always resolve to the same key.
  • PostGraphile internal pool: buildSchemaSDL()makeSchema() creates its own connection via makePgService that is not in pg-cache. The safeDropDb terminate-and-retry is the only safety net for these. Consider whether makeSchema should be patched to release its pool, or if the safety net is sufficient.
  • Test with a real codegen PGPM workflow: Run a codegen that uses pgpmModulePath or pgpmWorkspacePath and verify the ephemeral database is cleanly dropped without the "is being accessed" warning.

Notes

  • The safeDropDb terminate-and-retry pattern already existed in pgpm test-packages and pgpm kill commands — this just brings it to the core DbAdmin class so all consumers benefit.
  • The retry is single-attempt. In theory, new connections could be established between pg_terminate_backend and the retry dropdb, but this is extremely unlikely during test/codegen cleanup.

Link to Devin session: https://app.devin.ai/sessions/ca50a2babd834e40a9d5d00b379bd459
Requested by: @pyramation

…pools in codegen

- safeDropDb now catches 'is being accessed by other users' errors,
  terminates backends via pg_terminate_backend, and retries the drop
- PgpmModuleSchemaSource.fetch() now releases pg-cache pool for the
  ephemeral database before calling teardown
- generateMulti() shared PGPM source teardown now releases pg-cache
  pools before dropping ephemeral databases

These changes fix the systemic issue where dropdb fails because
deployPgpm() and getPgPool() cache connections in pg-cache that
are not closed before the database is dropped.
@devin-ai-integration
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@pyramation pyramation merged commit f760e89 into main Mar 17, 2026
44 checks passed
@pyramation pyramation deleted the devin/1773774783-fix-dropdb-terminate-backends branch March 17, 2026 20:29
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