question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RDS Proxy Session Pinning

See original GitHub issue

The issue

I’m using Npgsql to connect to an AWS RDS Proxy, so as I can utilise proxy as a connection pool between my applications and my RDS instance. However I’ve noticed that the proxy connections are not being re-used due to “session pinning”, and the proxy logs the following warning:

The client session was pinned to the database connection [dbConnection=123456789] for the remainder of the session. The proxy can’t reuse this connection until the session ends. Reason: A parse message was detected.

After reading a few related posts elsewhere (see below for links), I believe this is caused by prepared statements. I’ve enabled Npgsql logging to capture the SQL that’s being generated, which is as follows:

CALL my_schema.my_proc($1, $2, $3)

Does anyone know if this would likely be the reason for the session pinning I’m seeing? If so, are there any ways to work around this with Npgsql?

I’ve done some reading in the docs, and from what I’m understanding, statements are not automatically prepared unless enabled in the connection string (I’ve not done this, so it’s presumably disabled). We can also prepare statements using NpgsqlCommand.Prepare() (again, I’m not doing this). We can also unprepare statements with NpgsqlCommand.Unprepare() or NpgsqlConnection.UnprepareAll(), which only work if we have manually prepared the statement (which I haven’t).

Related links

Steps to reproduce

Execute parameterised SQL statement with Npgsql, targeting an RDS Proxy which targets an RDS instance.

Code:

var connBuilder = new NpgsqlConnectionStringBuilder
{
    Host = "my prds proxy endpoint",
    Port = 5432,
    Database = "my_database",
    SslMode = SslMode.Require,
    Username = "my_username",
    Password = "my_password", 
    Pooling = false // pooling should be handled by the proxy
};

var sql = "CALL my_schema.my_proc(@param_1, @param_2, @param_3)";

using (var conn = new NpgsqlConnection(connBuilder.ConnectionString))
{
    conn.Open();
    using (var cmd = new NpgsqlCommand(sql, conn))
    {
        cmd.Paramaters.Add(new NpgsqlParameter<string>("@param_1", "value 1"));
        cmd.Paramaters.Add(new NpgsqlParameter<string>("@param_2", "value 2"));
        cmd.Paramaters.Add(new NpgsqlParameter<string>("@param_3", "value 3"));
        
        cmd.ExecuteNonQuery();
    }
}

Generated SQL:

CALL my_schema.my_proc($1, $2, $3)

Further technical details

Npgsql version: 5.0.4 PostgreSQL version: RDS Aurora, PostgreSQL 11.9 on x86_64-pc-linux-gnu, compiled by x86_64-pc-linux-gnu-gcc (GCC) 7.4.0, 64-bit

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
lorddevcommented, Aug 10, 2022

I’d like to know whether there’s a solution for this PINNING issue:

image
2reactions
kpenergycommented, May 13, 2021

@roji Thanks for your quick response.

Highlighting the two protocols, and the fact that Npgsql always uses the extended protocol (as of now), was extremely helpful. This confirms why I’m seeing session pinning, as per the AWS docs:

For PostgreSQL, the following interactions cause pinning:

  • Using SET commands
  • Using the extended query protocol such as by using JDBC default settings
  • Creating temporary sequences, tables, or views
  • Declaring cursors
  • Discarding the session state
  • Listening on a notification channel
  • Loading a library module such as auto_explain
  • Manipulating sequences using functions such as nextval and setval
  • Interacting with locks using functions such as pg_advisory_lock and pg_try_advisory_lock
  • Using prepared statements, setting parameters, or resetting a parameter to its default

Looks like I’ll need to use an alternative proxy solution or an alternative provider.

I’ll close this issue off as there’s nothing to be done here - you’ve got an issue for tracking the simple protocol change.

Thanks again.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RDS Proxy and Connection Pinning
Attaching a session to a client until it's finished blocking it from being reused its what is called connection pinning. The list of...
Read more >
How much of an issue is connection pinning with RDS ...
The documentation states that "when a connection is pinned, each later transaction uses the same underlying database connection until the ...
Read more >
Better support for AWS RDS Proxy (avoid Connection ...
What they do when you prepare a statement is that they pin the connection from the pool to the connection from the user....
Read more >
Fix DatabaseConnectionsCurrentlySessionPinned metric ...
The DatabaseConnectionsCurrentlySessionPinned metric in RDS proxy dashboard should be fixed so it actually matches the number of occurrences of connection ...
Read more >
AWS RDS Proxy and session pinning
This issue of session pinning with connection pooling servers (such as RDS Proxy) may be more general, and it may be good to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found