Repository navigation
Postgres: create organization schemas before their pools connect - #89
Merged
Merged
Conversation
A pooled connection that opened before another pool committed an organization's schema could keep a search path without it, so migrations tried to create pg_catalog.triplex_schema_migrations and failed. Create the schema on a separate unbound connection first, so every schema-bound connection starts after the schema exists. Closing DatabaseManager now also closes each cached database's pools, which previously stayed open until the process exited. Adds an ordering test that fails on the old setup and a concurrent setup stress test that checks for leaked connections. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
|
Documentation preview removed (PR closed). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the intermittent
postgres-integrationfailure seen on #88 ("permission denied to create pg_catalog.triplex_schema_migrations") before the 0.3.0 release (ask 0007). It also fixes a connection leak that turned up while stress-testing it.The race
Each organization database gets its own schema, reached through two pools (adapter and query). Each pool binds
search_path=<schema>,pg_catalogat connection startup. The pool used to open first and then create the schema.I reproduced the CI error locally at about 1 failure in 60 rounds of 16 concurrent organization creations under CPU load, then captured one with Postgres statement logging:
CREATE SCHEMA.CREATE SCHEMA IF NOT EXISTS, which saw the schema and skipped. Its very next unqualifiedCREATE TABLEstill resolved topg_catalog.Postgres 17's
recomputeNamespacePathmarks the search path valid after computing it (namespace.c). A schema-change invalidation that arrives during that computation can therefore be lost, and the stale "schema missing" path sticks.Fix: create the schema on a separate, short-lived connection that is not bound to it, under the existing advisory lock. Only then open the schema-bound pool. Every schema-bound connection now starts after the schema is committed, so it can never compute a path that lacks it.
The leak
DatabaseManagerLive's finalizer stopped its cleanup fiber but never closed cached databases' scopes. Every organization's pools stayed open until the process exited, and the stress loop ran Postgres out of connections. The finalizer now callscloseAllConnections().Tests
packages/postgres/test/integration/organization-setup-stress.test.ts:PG_SETUP_STRESS_ROUNDSandPG_SETUP_STRESS_ORGSraise the load.The race itself is too rare to catch on demand. The old code also passed one 240-round run, so the stress loop is not the proof of the fix; the ordering test is. The fixed code passed 240 rounds × 16 organizations (3,840 creations) under CPU load, with no failures and no leaked connections.
Local:
pnpm checkpasses;pnpm test:postgres:integration79/79.🤖 Generated with Claude Code