RDS Proxy Session Pinning
See original GitHub issueThe 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:
- Created 2 years ago
- Comments:6 (1 by maintainers)
I’d like to know whether there’s a solution for this PINNING issue:
@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:
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.