Repository navigation
fix(adapter-pg): put the schema on the search path after connecting, keeping the existing path - #31184
Closed
wmadden-electric wants to merge 3 commits into
Closed
fix(adapter-pg): put the schema on the search path after connecting, keeping the existing path#31184wmadden-electric wants to merge 3 commits into
wmadden-electric wants to merge 3 commits into
Conversation
The schema option used to add -c search_path=<schema>,public to the pool's startup options. That replaced a search path set on the role or database, made PgBouncer refuse the connection, hid PGOPTIONS, and was overwritten by an options parameter in the connection URL. The pool config now reaches pg.Pool unchanged. When the adapter creates the pool and a schema is set, a connect listener puts the schema in front of the connection's existing search path, unless it is already on it. A failure is logged and passed to onPoolError. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
…path Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Contributor
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
|
Contributor
size-limit report 📦
|
Co-Authored-By: Claude Fable 5.2 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Contributor
Author
|
Closing in favour of #31190, which reverts #29067. Setting the search path after connecting fixes the startup parameter's defects, but behind a pooler in transaction mode a session setting stays on the server connection and reaches the pooler's other clients, and the adapter cannot detect that. So the adapter should not change the search path by default. |
wmadden-electric
added a commit
to aartoni/prisma
that referenced
this pull request
Nov 24, 2026
…unreleased prisma#29067) (prisma#31190) ```ts const adapter = new PrismaPg({ connectionString }, { schema: 'app' }) await prisma.user.findMany() // reads app.users, before and after this change await prisma.$queryRaw`SELECT * FROM users` // after this change: public.users, as in 7.10.1 ``` **Decision: the Postgres adapter does not change a connection's search path. The `schema` option applies only to the queries Prisma Client builds, such as `findMany`, which name the schema themselves. Raw SQL that needs another schema names it, or the user sets the search path on the role or the connection.** That is how every stable release behaves. prisma#29067 changed it on the Prisma 7 branch (`v7`) to fix prisma#26930, where TypedSQL and `$queryRaw` ignore the adapter's `schema`. It made the adapter open every connection with the startup parameter `options=-c search_path=app,public`, so the raw query above read `app.users`. This pull request reverts prisma#29067. It was never in a stable release, only in `7.11.0-dev` builds, so only users of those builds see a change. The cost is that prisma#26930 is a real gap again. prisma#29067 closed it; I will reopen it with the workaround below when this merges. (prisma#29067 also named prisma#24660, the same report from 2024, which was already closed as not planned and stays so.) ## Why revert Everyone who already sets `schema` would get the startup parameter on upgrading to 7.11, and for some of them it breaks a working app: 1. It replaces the search path instead of adding to it. A path set on the database role or the database is lost. Supabase puts an `extensions` schema there, so unqualified calls to extension functions start to fail. 2. PgBouncer in its default configuration refuses a connection whose startup `options` set a parameter outside its short list of known ones. An app that sets `schema` can no longer connect at all. 3. `pg` reads the `PGOPTIONS` environment variable only when the config has no `options`. Once the adapter adds its own, the user's `PGOPTIONS` is ignored. 4. `pg` lets `?options=` in the connection string overwrite the config's `options`, so for those users the search path is silently not set. Defects 3 and 4 could be patched by merging the option strings. Defect 2 cannot be fixed while the adapter sends a startup parameter, and fixing defect 1 needs the server's current path, which is not known before connecting. ## What users do instead Qualify the table in raw SQL (`app.users`), or set the search path on the database role: ```sql ALTER ROLE app_user SET search_path = app, public; ``` A user can also put `-c search_path=...` in their own connection `options`, but PgBouncer in its default configuration refuses that, so the doc comment does not recommend it. The doc comment on the `schema` option is the only source line here that is not a plain revert: ```ts /** * The name of the schema to use in generated queries. Raw queries are not affected: they resolve * unqualified names through the connection's `search_path`. Qualify the name in raw SQL * (`"mySchema"."User"`), or set the search path on the database role * (`ALTER ROLE ... SET search_path = ...`). The adapter does not set it itself, because behind a * connection pooler in transaction mode a session setting is shared with the pooler's other clients. */ schema?: string ``` ## Tests The unit tests and the integration scenario that prisma#29067 added for the startup parameter are removed with it. One new unit test checks that a `schema` adds no startup `options` to the pool config. The existing tests for generated queries with a `schema` stay. ## Alternatives considered - **Patch the startup parameter.** Rejected for the reason above: the PgBouncer refusal and the lost role path remain. - **Set the path with a query after each connection opens, in front of the existing path** (draft prisma#31184, closed in favour of this). It fixes all four defects. But behind a pooler in transaction mode (PgBouncer, Neon's pooled endpoint, Supabase's pooler) a session setting stays on the server connection, which the pooler then hands to another client. With one schema per tenant: tenant A's client sets `search_path` to `tenant_a`, the pooler gives that server connection to tenant B's client, and B's `SELECT * FROM users` reads `tenant_a.users`. The adapter cannot tell it is behind such a pooler, so it should not change session state by default. - **`SET LOCAL` around each raw query.** It does not leak, but it puts every raw query in a transaction and adds round trips, which changes behaviour for all users of `schema`. - **An explicit opt-in option** for the after-connect query. Possible later; it is a new feature, not part of removing a defect from the release branch. Agent: eeyore-69 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Behavior Changes** * Configured schemas continue to apply to generated queries. Raw queries now use the connection’s existing `search_path`; the adapter no longer sets it based on the configured schema. * The adapter no longer logs a warning when using an externally provided connection pool. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-authored-by: Claude Fable 5.2 <[email protected]>
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.
With this adapter,
$queryRawand TypedSQL resolve an unqualified table name inappfirst. #29067 did that by adding-c search_path=app,publicto the connection's startupoptions. This change does it with one query on each new connection, which putsappin front of the search path the connection already has:"$user", publicapp, "$user", publicextensions, public(set on the role)app, extensions, publicpublic, app(the user put it there)Why the startup parameter had to go
It is on
v7but in no stable release yet. It has four defects:extensionsschema failed.optionscarry a parameter it does not track, so an app that setschemacould not connect.pgreadsPGOPTIONSonly when the config has nooptions. Once the adapter added its own, the user'sPGOPTIONSwas ignored.pglets?options=in the connection string overwrite the config'soptions, so for those users the search path was never set.The pool config is now passed to
pguntouched, which removes 3 and 4. The query after connecting removes 1 and 2.How it works
The adapter registers a
connectlistener on a pool it created itself.pg-poolemitsconnectonce per new physical connection, before it hands the client to the caller, andpgruns a client's queries in order, so the search path is in place before the caller's first query. The schema is a bound parameter and the server quotes it. If the query fails, the error goes toonPoolErrorand the connection is used as it is; generated queries name their schema and are unaffected.A pool the user passes in is still left alone.
What users should know
Behind a pooler in transaction mode (PgBouncer, Neon's pooled connections, Supabase's pooler), a session setting stays on the server connection that ran it and other clients of the pooler later reuse that connection. That is true of any
SET, and it is how Prisma 6 set the search path underpgbouncer=true. An app that keeps one schema per tenant behind such a pooler, with all tenants on one database user, should set the search path on the role and not rely on unqualified names in raw SQL. The option's doc comment says so. PgBouncer 1.26 with Postgres 18 trackssearch_pathand does not have this problem.Not verified
schema, so nothing here proves it. A failure would leave the schema off the search path and report throughonPoolError; queries would still run.Alternative considered
Reverting #29067 and leaving the search path to the user. That removes every pooler question, and brings back #26930 (TypedSQL ignores the schema).
Agent: eeyore-69
🤖 Generated with Claude Code