You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Fully support prepared statements with pgbouncer 1.21.0 #21531
Today, the way prepared statements are supported with pgbouncer is suboptimal. Prisma will clear all prepared statements in the connection to avoid mismatches. Now that pgbouncer supports prepared statements as of 1.21.0, It would be great to understand if we need to append ?pgbouncer=true or if we can simply run prisma without being in "pgbouncer" mode.
Suggested solution
Clarity on how to fully take advantage of prepared statements with pgbouncer 1.21.0
Reacted by JP Grace, sabburi, Mark Lopez, Ben H, Camilo Luna, Cristian Simion, okozlov-mindk, Alexander, MorenoMdz, Michał and 10 more
Did anyone try running their test suites against a this new PgBouncer version and max_prepared_statements configured, either with or without pgbouncer=true? That would probably be a good first data point.
Did anyone try running their test suites against a this new PgBouncer version and max_prepared_statements configured, either with or without pgbouncer=true? That would probably be a good first data point.
Did anyone try running their test suites against a this new PgBouncer version and max_prepared_statements configured, either with or without pgbouncer=true? That would probably be a good first data point.
We actually have reports now that withpgbouncer=true you will get an error: #21635 That conflicts with your observations @MorenoMdz, correct?
What error message exactly are you getting in your testing?
Can it be because of the Prisma version? We are using 5.4.2 but I would be surprised if it was only for my case. Here if I connect to my local PG or cloud PG without pgbouncer=true thru my local instance of pgbouncer, it will error.
This is the pgbouncer config I used, I don't know if this would have any affect.
That might be the problem: The new functionality is only enabled when max_prepared_statements is set to a non 0 value. Otherwise 1.21 behaves exactly like older PgBouncer versions (which would match your observations).
That might be the problem: The new functionality is only enabled when max_prepared_statements is set to a non 0 value. Otherwise 1.21 behaves exactly like older PgBouncer versions (which would match your observations).
That must be it them, I assumed incorrectly it was supported by default, going to try again.
@janpio do you have any information about how the max_prepared_statements value needs to be calculated?
Pgbouncer running on my machine, targeting PG in the cloud (Aiven) using the direct access URL
Running Prisma 5.4.2 in Nest, calling the requests from Swagger
No pgbouncer=true param in the URL
Got consistent 400ms in a specific endpoint that before was always above 1000ms
(Keep in mind I am in LATAM right now and the server is in central-us so the extra latency is expected)
Before
After
I can run an artillery session from US to get a better load test if you want.
What was causing it to break first is that I had the pool URL of my cloud DB, changing it to the direct URL made it work. What I am not sure yet is why it was breaking against my local PG.
Note that having max_prepared_statements = 1000 or not didn't made any difference in this small test.
Note 2: pgbouncer docs links for the prepared statements setting are broken rn, this is the link
We also tested 1.21.0 with max_prepared_statements = 1000 and removing pgbouncer=true on prisma 5.4.2 and everything just works! We're seeing improved query latency as well and no issues. I think it would be a good idea to deprecate pgbouncer=true and/or recommend using pgbouncer 1.21.0 with prepared statements enabled. This one change has improved our query latency more than any change prisma has shipped (including all of the latest perf improvements). I wouldn't sleep on this.
Reacted by Carlos Eduardo Ramírez, Wes Nishio and Johann Rohn
Reacted by Jelte Fennema-Nio, Wes Nishio and Richard J
Reacted by Michał, JP Grace, Wes Nishio, Lars Öltjenbruns and André Palião
At a low usage time, deploy everything to use the direct URL (no pgbouncer true, different port) so no pooling with be done. We can set the max connections in the DB server to a higher number temporarily and maybe also increase the server config for a couple hours.
Ask Aiven (our cloud provider) to change the max prepared statements setting to 100, so no clients are going to be connecting to the pool, no issues would happen.
Redeploy our cloud functions to connect to the pool, without pgbouncer=true , check that no errors happened.
Redeploy our Node server to connect back to the pool.
Change the configuration/connections number down to what it is now.
Yes, that would probably a be a reasonable way to avoid any user facing errors or downtime.
If that is not 100% required, you can simplify a lot: Ask Aiven to increase max_prepared_statements to a non 0 value, then instantly redeploy all your Node servers to not use pgbouncer=true any more - that will probably still put PgBoucner into a bad state because before the deployment some DEALLOCATE ALL will be run. But then restart the PgBounce instance so it is in a clean state - and you should be fine.
Yes, that would probably a be a reasonable way to avoid any user facing errors or downtime.
If that is not 100% required, you can simplify a lot: Ask Aiven to increase max_prepared_statements to a non 0 value, then instantly redeploy all your Node servers to not use pgbouncer=true any more - that will probably still put PgBoucner into a bad state because before the deployment some DEALLOCATE ALL will be run. But then restart the PgBounce instance so it is in a clean state - and you should be fine.
The blocker is that we have a whole replication setup happening with cloud functions, we are finalizing a migration from Firestore to PG and there are a few hundred requests happening per second still just for the live replication from FS->PG. If we change the cloud functions and the Node server to use the direct URL there is the risk of issues due to rejected requests because of saturated connections from the DB server.
One extra issue is that deploying our Nest instance can take up to 10-30min depending on our provider (Render) and the cloud functions also need a redeploy to update the URL.
That said, we might go ahead and upgrade it tomorrow night as it will likely to be a low usage time in our systems due to the holidays. I will update here depending on how it goes 👍
Also @janpiothis fix doesn't stop the issues when connecting to pgbouncer with max_prepared_statements > 0 and the pgbouncer=true right?
Yes it does, with that fix the DEALLOCATE ALL is not only executed on the database connection, removing its prepared statements - but also the cache of those on Pgbouncer side is emptied. Running a manual DEALLOCATE ALL (or using pgbouncer=true) should not cause any problems any more when this is released. Really looking forward to that.
FYI: I released PgBouncer 1.22.0 just now, which includes the support for DEALLOCATE ALL. So with that release using pgbouncer=true won't cause breakage anymore. But pgbouncer=true is not needed anymore and since 1.21.0 and it is slower, so it's recommended to remove it from your connection strings.
So I think all that's needed in this issue is actually updating the Prisma docs to reflect that new recommendation. In particular these two pages at least:
Reacted by MorenoMdz, Jan Piotrowski, Christopher Brookes, Pascal Sthamer, Evan Shortiss, Jinesh Shah, Tasos Boulis, Andrew, Srujun Thanmay Gupta, Lars Karbo and 9 more
A note regarding custom schema usage with Prisma and PgBouncer. We use a different schema name than the Prisma default. PgBouncer will complain with the following error when the ?schema=X parameter is provided in the connection string:
FATAL: unsupported startup parameter: search_path
A workaround for this is to:
add the ?schema=X parameter in the connection string as usual
configure PgBouncer to ignore the search_path parameter
set the default search path on the database user role
# in PgBouncer configignore_startup_parameters=search_path,extra_float_digits
-- set the search path for database user
ALTER ROLE rolename SET search_path ='X';
My understanding of this workaround is that we are tricking Prisma into thinking it is setting the search path via the query parameter in the connection string, when actually the search path is being set on the database user role. This allows Prisma to derive the schema used by the client.
It would be nice if Prisma accepted the schema name as part of the Prisma schema file, so that the client can be configured to use a custom schema without deriving this from the connection string.
It would be nice if Prisma accepted the schema name as part of the Prisma schema file, so that the client can be configured to use a custom schema without deriving this from the connection string.
The problem would unfortunately persist - as Prisma only uses the ?schema= to run a SET search_path TO ... statement upon connecting to the database - which it would also need to do when it was provided via the Prisma schema.
So I think telling PgBouncer not to react to the search_path query, but instead then setting the search path via another way (like setting it for the role) seems like the way to go.
FYI: I released PgBouncer 1.22.0 just now, which includes the support for DEALLOCATE ALL. So with that release using pgbouncer=true won't cause breakage anymore. But pgbouncer=true is not needed anymore and since 1.21.0 and it is slower, so it's recommended to remove it from your connection strings.
So I think all that's needed in this issue is actually updating the Prisma docs to reflect that new recommendation. In particular these two pages at least:
Hey @JelteF, I'm currently working on updating the Prisma docs to match this ticket, so I just wanted to double-check. Is it still recommended to do the above with the release of a newer PgBouncer that include DEALLOCATE ALL and replication connections?
Activity
MorenoMdz commentedon Oct 18, 2023
I would assume Prisma needs to be updated to support this, based on the information here.
Would be great to have this prioritized, as currently we are facing some issues because of the extra latency.
janpio commentedon Oct 18, 2023
Did anyone try running their test suites against a this new PgBouncer version and
max_prepared_statementsconfigured, either with or withoutpgbouncer=true? That would probably be a good first data point.MorenoMdz commentedon Oct 24, 2023
I will try to test it this way and report back
MorenoMdz commentedon Oct 24, 2023
So I tested with this setup:
PG running locally withing docker + pgbouncer 1.21 running natively locally
PG running from cloud, using the direct access url, connected thru the same pgbouncer 1.21 running natively, same results.
janpio commentedon Oct 26, 2023
We actually have reports now that with
pgbouncer=trueyou will get an error: #21635That conflicts with your observations @MorenoMdz, correct?
What error message exactly are you getting in your testing?
MorenoMdz commentedon Oct 26, 2023
Can it be because of the Prisma version? We are using
5.4.2but I would be surprised if it was only for my case. Here if I connect to my local PG or cloud PG withoutpgbouncer=truethru my local instance of pgbouncer, it will error.This is the pgbouncer config I used, I don't know if this would have any affect.
janpio commentedon Oct 26, 2023
That might be the problem: The new functionality is only enabled when
max_prepared_statementsis set to a non0value. Otherwise 1.21 behaves exactly like older PgBouncer versions (which would match your observations).MorenoMdz commentedon Oct 26, 2023
That must be it them, I assumed incorrectly it was supported by default, going to try again.
@janpio do you have any information about how the
max_prepared_statementsvalue needs to be calculated?janpio commentedon Oct 26, 2023
Unfortunately not. I have not looked deeper into this.
MorenoMdz commentedon Oct 26, 2023
Ok good news:
With this setup:
pgbouncer=trueparam in the URL(Keep in mind I am in LATAM right now and the server is in central-us so the extra latency is expected)
Before

After

I can run an artillery session from US to get a better load test if you want.
What was causing it to break first is that I had the pool URL of my cloud DB, changing it to the direct URL made it work. What I am not sure yet is why it was breaking against my local PG.
Note that having
max_prepared_statements = 1000or not didn't made any difference in this small test.Note 2: pgbouncer docs links for the prepared statements setting are broken rn, this is the link
jineshshah36 commentedon Oct 27, 2023
We also tested
1.21.0withmax_prepared_statements = 1000and removingpgbouncer=trueon prisma5.4.2and everything just works! We're seeing improved query latency as well and no issues. I think it would be a good idea to deprecatepgbouncer=trueand/or recommend using pgbouncer1.21.0with prepared statements enabled. This one change has improved our query latency more than any change prisma has shipped (including all of the latest perf improvements). I wouldn't sleep on this.28 remaining items
MorenoMdz commentedon Nov 19, 2023
@janpio @JelteF would this plan work for us to migrate?
At a low usage time, deploy everything to use the direct URL (no pgbouncer true, different port) so no pooling with be done. We can set the max connections in the DB server to a higher number temporarily and maybe also increase the server config for a couple hours.
Ask Aiven (our cloud provider) to change the max prepared statements setting to 100, so no clients are going to be connecting to the pool, no issues would happen.
Redeploy our cloud functions to connect to the pool, without
pgbouncer=true, check that no errors happened.Redeploy our Node server to connect back to the pool.
Change the configuration/connections number down to what it is now.
janpio commentedon Nov 21, 2023
Yes, that would probably a be a reasonable way to avoid any user facing errors or downtime.
If that is not 100% required, you can simplify a lot: Ask Aiven to increase
max_prepared_statementsto a non 0 value, then instantly redeploy all your Node servers to not usepgbouncer=trueany more - that will probably still put PgBoucner into a bad state because before the deployment someDEALLOCATE ALLwill be run. But then restart the PgBounce instance so it is in a clean state - and you should be fine.MorenoMdz commentedon Nov 21, 2023
The blocker is that we have a whole replication setup happening with cloud functions, we are finalizing a migration from Firestore to PG and there are a few hundred requests happening per second still just for the live replication from FS->PG. If we change the cloud functions and the Node server to use the direct URL there is the risk of issues due to rejected requests because of saturated connections from the DB server.
One extra issue is that deploying our Nest instance can take up to 10-30min depending on our provider (Render) and the cloud functions also need a redeploy to update the URL.
That said, we might go ahead and upgrade it tomorrow night as it will likely to be a low usage time in our systems due to the holidays. I will update here depending on how it goes 👍
Also @janpio this fix doesn't stop the issues when connecting to
pgbouncerwithmax_prepared_statements > 0and thepgbouncer=trueright?janpio commentedon Nov 21, 2023
Yes it does, with that fix the
DEALLOCATE ALLis not only executed on the database connection, removing its prepared statements - but also the cache of those on Pgbouncer side is emptied. Running a manualDEALLOCATE ALL(or usingpgbouncer=true) should not cause any problems any more when this is released. Really looking forward to that.MorenoMdz commentedon Nov 22, 2023
We have done the upgrade last night and we are seeing solid improvements on all our endpoints that do fetch single records already.
Interesting note, Aiven upgraded their PG configuration to already include the said fix, making it easier on our side to upgrade 👍
Thanks @janpio and @JelteF for the hard work here!
saadbahir commentedon Jan 11, 2024
Is it still recommended to use
transactionmode like suggested in the Prisma docs ?For reference I removed
pgbouncer=truealongsidemax_prepared_statements > 0and I get a better latency tooI kept
POOL_MODE=sessionbut I am not too sure whether that is the right decisionJelteF commentedon Jan 15, 2024
The recommended setup is:
edit: (fixed typo that @alessio-zamparelli noticed)
alessio-zamparelli commentedon Jan 15, 2024
JelteF commentedon Jan 31, 2024
FYI: I released PgBouncer 1.22.0 just now, which includes the support for DEALLOCATE ALL. So with that release using
pgbouncer=truewon't cause breakage anymore. Butpgbouncer=trueis not needed anymore and since 1.21.0 and it is slower, so it's recommended to remove it from your connection strings.So I think all that's needed in this issue is actually updating the Prisma docs to reflect that new recommendation. In particular these two pages at least:
MorenoMdz commentedon Jan 31, 2024
Thank you very much JelteF!
sdgluck commentedon Mar 7, 2024
A note regarding custom schema usage with Prisma and PgBouncer. We use a different schema name than the Prisma default. PgBouncer will complain with the following error when the
?schema=Xparameter is provided in the connection string:A workaround for this is to:
?schema=Xparameter in the connection string as usualsearch_pathparameterMy understanding of this workaround is that we are tricking Prisma into thinking it is setting the search path via the query parameter in the connection string, when actually the search path is being set on the database user role. This allows Prisma to derive the schema used by the client.
It would be nice if Prisma accepted the schema name as part of the Prisma schema file, so that the client can be configured to use a custom schema without deriving this from the connection string.
janpio commentedon Mar 11, 2024
The problem would unfortunately persist - as Prisma only uses the
?schema=to run aSET search_path TO ...statement upon connecting to the database - which it would also need to do when it was provided via the Prisma schema.So I think telling PgBouncer not to react to the
search_pathquery, but instead then setting the search path via another way (like setting it for the role) seems like the way to go.biw commentedon Oct 18, 2024
Hey @JelteF, I'm currently working on updating the Prisma docs to match this ticket, so I just wanted to double-check. Is it still recommended to do the above with the release of a newer PgBouncer that include
DEALLOCATE ALLandreplication connections?PR: prisma/web#6346