Skip to content

Better support for AWS RDS Proxy (avoid Connection Pinning) #5866

Open

Description

@f0rr0

I am using AWS RDS Proxy with Postgres. While investigating the logs for the proxy I saw many warnings with the same message:

https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/rds-proxy.html#rds-proxy-pinning

The client session was pinned to the database connection [dbConnection=3377128289] for the remainder of the session. The proxy can't reuse this connection until the session ends. Reason: SQL changed session settings that the proxy doesn't track. Consider moving session configuration to the proxy's initialization query. Digest: "set search_path = $1; set names $2;".

It looks like Prisma sets these whenever it creates a new connection which causes the Proxy to pin all connections until released rendering the proxy useless. As suggested in the log message above, I would like to set the search_path and NAMES while initialising the Proxy and not each time a new connection is created.

Is there any configuration parameter or any other way through which I can tell Prisma not to set these per connection?

Activity

  1. Jolg42 commented on Feb 26, 2021

    @Jolg42
  2. added
    bug/2-confirmed
    kind/bug
    on Mar 5, 2021
  3. pantharshit00 commented on Mar 5, 2021

    @pantharshit00
  4. pimeys commented on Mar 5, 2021

    @pimeys

    Yep, and found this from our connecting code, with a comment:

    // SET NAMES sets the client text encoding. It needs to be explicitly set for automatic
    // conversion to and from UTF-8 to happen server-side.
    //
    // Relevant docs: https://www.postgresql.org/docs/current/multibyte.html
    let session_variables = format!(
        r##"
        SET search_path = "{schema}";
        SET NAMES 'UTF8';
        "##,
        schema = schema
    );

    I need to ping @tomhoule for that.

    Another thing that raised my attention in the RDS docs:

    Any statement with a text size greater than 16 KB causes the proxy to pin the session.

    Prepared statements cause the proxy to pin the session. This rule applies whether the prepared statement uses SQL text or the binary protocol.

    I do believe we might trigger statements over 16 KB for larger queries, quite easily actually. And we most def use prepared statements everywhere to prevent SQL injections, to not escape the parameters by ourselves (this escaping is not very well documented, Postgres docs just tells you to use statements) and to cache the statements and allow us faster execution of similar queries.

  5. nihemak commented on Mar 20, 2021

    @nihemak

    Could you please tell me the status of this issue? I have encounter the same issue. I'd like to use prisma with AWS RDS Proxy and Postgres. Thanks for awesome ORM!

  6. Jolg42 commented on Mar 30, 2021

    @Jolg42
    Contributor

    @nihemak current status is documented here:

    https://www.prisma.io/docs/guides/deployment/deployment-guides/caveats-when-deploying-to-aws-platforms#aws-rds-proxy

    AWS RDS Proxy

    Prisma is compatible with AWS RDS Proxy. However, there is no benefit in using it for connection pooling with Prisma due to the way RDS Proxy pins connections:

    "Your connections to the proxy can enter a state known as pinning. When a connection is pinned, each later transaction uses the same underlying database connection until the session ends. Other client connections also can't reuse that database connection until the session ends. The session ends when the client connection is dropped." ~ AWS RDS Proxy Docs
    

    Prepared statements (of any size) or query statements greater than 16 KB cause RDS Proxy to pin the session. Because Prisma uses prepared statements for all queries, you won't see any benefit when using RDS Proxy with Prisma.

  7. added this to the 2.21.0 milestone on Mar 31, 2021
  8. 30 remaining items

  9. thomaschaaf commented on Aug 24, 2023

    @thomaschaaf

    mikroORM also doesn't use prepared statements. ;)

  10. raymondKelly commented on Aug 24, 2023

    @raymondKelly

    mikroORM also doesn't use prepared statements. ;)

    Do you have anywhere I can read further into that? Everything I've read on MikroORM is that they do use prepared statements. How do they prevent SQL Injection in their case? I guess I can add logging and see how they do it?

  11. mikerhodes commented on Oct 3, 2023

    @mikerhodes

    I happened to arrive here trying to dig for details on how pgbouncer and AWS's RDS proxy work, and how they differ.

    The discussion around use of prepared statements vs. pgbouncer reminded me of this piece which talks about the difference between "named" and "unnamed" prepared statements. Named are standard PREPARE foo ... statements, but you can still prepare an "unnamed" statement (using a more complex process, the article pulls apart some ruby code to show it) and gain the benefits against SQL injection but avoid the problems with pgbouncer.

    For SQL injection safety, both named and unnamed versions are equivalent: they separate query structure (Parse) from data values (Bind). Adding query bindings when not in a prepared statement simply makes an unnamed statement.

    Chances are you already spotted this, but if not, maybe it'll help.

  12. eugene-kim commented on Dec 15, 2023

    @eugene-kim
  13. mariomnts commented on Dec 18, 2023

    @mariomnts

    This might have been addressed in a recent RDS Proxy release

    https://aws.amazon.com/blogs/database/amazon-rds-proxy-multiplexing-support-for-postgresql-extended-query-protocol/

    According to the announcement:

    Multiplexing support for prepared statements is limited to Extended Query Protocol messages. Multiplexing with Extended Query Protocol is enabled automatically in RDS Proxy.

    Does somebody know if Prisma supports PostgreSQL Extended Query Protocol?

  14. janpio commented on Jan 11, 2024

    @janpio
    Contributor

    A few notes on that announcement:

    • Only applies to PostgreSQL, not MySQL unfortunately
    • Other limitations for PostgreSQL have not changed, SQL queries >16kb and any SET commands (like the set search_path from the original issue description) unfortunately still cause pinning.

    We are looking into it anyway if for some cases with PostgreSQL at least the pinning situation is now usable.

  15. mariomnts commented on Jan 12, 2024

    @mariomnts

    A few notes on that announcement:

    • Only applies to PostgreSQL, not MySQL unfortunately
    • Other limitations for PostgreSQL have not changed, SQL queries >16kb and any SET commands (like the set search_path from the original issue description) unfortunately still cause pinning.

    We are looking into it anyway if for some cases with PostgreSQL at least the pinning situation is now usable.

    Thanks @janpio, please keep us posted 😃

  16. sergio-toro commented on Mar 5, 2024

    @sergio-toro

    This might have been addressed in a recent RDS Proxy release

    https://aws.amazon.com/blogs/database/amazon-rds-proxy-multiplexing-support-for-postgresql-extended-query-protocol/

    It would be great to be able to use RDS Proxy. At our company (https://www.oliva.health/) we will be following this very closely as we are interested, we use Postgres and will be available for testing if you decide to tackle this one 🙌🏽

  17. mariomnts commented on Jun 10, 2024

    @mariomnts

    Hey there! I was wondering if there has been some news regarding Prisma and AWS RDS. It's a real nightmare trying to make those two work at scale in production.

  18. mr-rpl commented on Aug 10, 2024

    @mr-rpl

    Has anyone tried using pgBouncer mode with RDS proxy?

    The docs say that it disables prepared statements -- which is what causes the pinning in RDS proxy to occur.

    I don't know what other side effects may be introduced though. Curious!

  19. ackinc commented on Aug 10, 2024

    @ackinc

    Has anyone tried using pgBouncer mode with RDS proxy?

    The docs say that it disables prepared statements -- which is what causes the pinning in RDS proxy to occur.

    I don't know what other side effects may be introduced though. Curious!

    I have, with no observable side-effects; but I'm no database expert and my app was hardly pushing the database to its limits.

  20. NeishaS commented on Jan 2, 2025

    @NeishaS

    Hello,
    I still have issue of session pinning when using RDS proxy with RDS Aurora Postgres.
    I am using RDS Proxy with Glue Job/Connection (Glue 5).

    I tried updating the URL to include parameters assumeMinServerVersion and ApplicationName but still the sessions are getting pinned with this log message If this prepared statement runs, the client session will be pinned to the database connection [dbConnection=25xxxx]. Reason: SQL changed session settings that the proxy doesn't track. Consider moving session configuration to the proxy's initialization query. Digest: "set session characteristics as transaction isolation level $1 uncommitted".

  21. stwr667 commented on Jul 21, 2025

    @stwr667

    Any statement with a text size greater than 16 KB causes the proxy to pin the session.

    @janpio, can you please clarify this part of the official docs?

    If you consider this SQL block:

    BEGIN;
      INSERT INTO my_test_table (name, description) VALUES ('my VARCHAR column. 1', 'my TEXT column. 1');
      INSERT INTO my_test_table (name, description) VALUES ('my VARCHAR column. 2', 'my TEXT column. 2');
      COMMIT;
    END;

    Does it mean the 16KB size limit will apply to:

    1. The entire SQL block (i.e. the whole transaction in this case), or
    2. An entire statement, e.g. the 1st INSERT statement on line 2, or
    3. The "text" size, e.g. for a column of type TEXT. So e.g. if 'my TEXT column. 1' was greater than the limit?
    4. Something else?
  22. stwr667 commented on Jul 22, 2025

    @stwr667

    For future travellers, AWS support confirmed today that the answer is 2. (from above - An entire statement, e.g. the 1st INSERT statement on line 2).

    See their reply from my support case below:

    Hi Team,

    Thank you for contacting AWS Support Engineering. My name is ****** and I will be assisting you today.

    You reached out to us today as you were looking for some clarification on our RDS Proxy Documentations.

    I understand that Proxy has the following limitation -

    Any statement with a text size greater than 16 KB causes the proxy to pin the session.
    [+] Avoiding pinning an RDS Proxy - https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/rds-proxy-pinning.html

    This limitation applies to a single statement or a single query. It does not apply for the complete transaction.

    The limit is applied to a single statement.

    Adding further, please allow me to share that, the detailed information about the queries can only be seen at the RDS end by enabling statement logging (by setting log_statement=all in the parameter group), so you would be able to view every query running on the RDS.

    Please note that, this setting (log_statement=all) causes logging overhead as additional CPU will be required to log all statements.

    I hope this helps. Please feel free to reach out to me incase of further concerns and I will be happy to assist.

    We value your feedback. Please share your experience by rating this and other correspondences in the AWS Support Center. You can rate a correspondence by selecting the stars in the top right corner of the correspondence.

    Best regards,
    ******.
    Amazon Web Services

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions