Skip to content

Fully support prepared statements with pgbouncer 1.21.0 #21531

Open

Description

@jineshshah36

Problem

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

Activity

  1. MorenoMdz commented on Oct 18, 2023

    @MorenoMdz

    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.

  2. janpio commented on Oct 18, 2023

    @janpio
    Contributor

    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.

  3. MorenoMdz commented on Oct 24, 2023

    @MorenoMdz

    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.

    I will try to test it this way and report back

  4. MorenoMdz commented on Oct 24, 2023

    @MorenoMdz

    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.

    So I tested with this setup:

    1. PG running locally withing docker + pgbouncer 1.21 running natively locally

      • Without the pgbouncer param, it does throw
      • With the pgbouncer param, it works fine
    2. PG running from cloud, using the direct access url, connected thru the same pgbouncer 1.21 running natively, same results.

  5. janpio commented on Oct 26, 2023

    @janpio
    Contributor

    We actually have reports now that with pgbouncer=true you will get an error: #21635
    That conflicts with your observations @MorenoMdz, correct?

    What error message exactly are you getting in your testing?

  6. MorenoMdz commented on Oct 26, 2023

    @MorenoMdz

    We actually have reports now that with pgbouncer=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.

    [databases]
    db = host=xxx dbname=xxx user=xxx password=xxx
    
    
    [pgbouncer]
    listen_addr = 127.0.0.1
    listen_port = 6432
    auth_type = plain
    auth_file = pgbouncer_users.txt
    logfile = pgbouncer.log
    pidfile = pgbouncer.pid
    ignore_startup_parameters = extra_float_digits
    
  7. janpio commented on Oct 26, 2023

    @janpio
    Contributor

    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).

  8. MorenoMdz commented on Oct 26, 2023

    @MorenoMdz

    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?

  9. janpio commented on Oct 26, 2023

    @janpio
    Contributor

    Unfortunately not. I have not looked deeper into this.

  10. MorenoMdz commented on Oct 26, 2023

    @MorenoMdz

    Unfortunately not. I have not looked deeper into this.

    Ok good news:

    [databases]
    db = host=xxx dbname=xxx user=xxx password=xxx
    
    
    [pgbouncer]
    listen_addr = 127.0.0.1
    listen_port = 6432
    auth_type = plain
    auth_file = pgbouncer_users.txt
    logfile = pgbouncer.log
    pidfile = pgbouncer.pid
    ignore_startup_parameters = extra_float_digits
    ; max_prepared_statements = 1000 

    With this setup:

    • 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
    image

    After
    image

    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

  11. jineshshah36 commented on Oct 27, 2023

    @jineshshah36
    Author

    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.

  12. 28 remaining items

  13. MorenoMdz commented on Nov 19, 2023

    @MorenoMdz

    @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.


  14. janpio commented on Nov 21, 2023

    @janpio
    Contributor

    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.

  15. MorenoMdz commented on Nov 21, 2023

    @MorenoMdz

    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 @janpio this fix doesn't stop the issues when connecting to pgbouncer with max_prepared_statements > 0 and the pgbouncer=true right?

  16. janpio commented on Nov 21, 2023

    @janpio
    Contributor

    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.

  17. MorenoMdz commented on Nov 22, 2023

    @MorenoMdz

    image

    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!

  18. saadbahir commented on Jan 11, 2024

    @saadbahir

    Is it still recommended to use transaction mode like suggested in the Prisma docs ?

    For reference I removed pgbouncer=true alongside max_prepared_statements > 0 and I get a better latency too

    I kept POOL_MODE=session but I am not too sure whether that is the right decision

  19. JelteF commented on Jan 15, 2024

    @JelteF

    The recommended setup is:

    • max_prepared_statements>0
    • pool_mode=transaction
    • remove pgbouncer=true

    edit: (fixed typo that @alessio-zamparelli noticed)

  20. alessio-zamparelli commented on Jan 15, 2024

    @alessio-zamparelli
  21. JelteF commented on Jan 31, 2024

    @JelteF

    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:

    1. https://www.prisma.io/docs/orm/overview/databases/postgresql
    2. https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/databases-connections/pgbouncer
  22. MorenoMdz commented on Jan 31, 2024

    @MorenoMdz

    Thank you very much JelteF!

  23. sdgluck commented on Mar 7, 2024

    @sdgluck

    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 config
    ignore_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.

  24. janpio commented on Mar 11, 2024

    @janpio
    Contributor

    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.

  25. biw commented on Oct 18, 2024

    @biw

    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:

    1. prisma.io/docs/orm/overview/databases/postgresql
    2. prisma.io/docs/orm/prisma-client/setup-and-configuration/databases-connections/pgbouncer

    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?

    PR: prisma/web#6346

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions