If database connections jump after a deploy while query execution times stay flat, check whether RDS Proxy can still reuse its backends. A client normally borrows a backend for a transaction. When the proxy detects session state that must persist, it reserves that backend for the client until the client disconnects. An idle application connection can then occupy a database connection, even after COMMIT.
Measure pinned connections and borrow pressure
In CloudWatch, open Metrics → RDS → Per-Proxy Metrics. Choose the same ProxyName dimension for every series in the table and use a 60-second period. Use Sum for the counts in each minute because AWS publishes values from the underlying proxy instances. Use Average for borrow latency, which AWS reports in microseconds. Plot matching windows before and after the deployment, with comparable client traffic. AWS metric definitions and dimensions.
| Metric | Statistic | What it tells you |
|---|---|---|
DatabaseConnectionsCurrentlySessionPinned | Sum | Backends reserved because a client changed session state. This is a gauge, so a sustained value of 40 means roughly 40 occupied backends per sample, rather than 40 new pin events per minute. |
DatabaseConnections | Sum | Backend connections held by the proxy, including connections being set up. Use it as the denominator for the pinned share. |
DatabaseConnectionsCurrentlyBorrowed | Sum | Backends assigned to clients. Compare it with pinned and in-transaction counts on the same timeline. |
DatabaseConnectionsCurrentlyInTransaction | Sum | Backends in open transactions. A sustained rise here calls for a transaction-lifetime check. |
MaxDatabaseConnectionsAllowed | Sum | Backend capacity the proxy can use for workload traffic. Compare it with borrowed connections as a sign of diminishing headroom. |
DatabaseConnectionsBorrowLatency | Average | Time to obtain a backend. Rising latency alongside pinning and shrinking headroom makes the pinning operationally significant. |
In metric math, use 100 * pinned / backends, with matching dimensions and timestamps. Interpret the result only when both metrics have a sample and backends is positive. Check the raw pinned series before treating gaps as zero. For Aurora writer and reader separation, open the proxy's monitoring dashboard and use its per-role pinned percentage. Its role widgets have their own dimensions and aggregation. AWS proxy dashboard reference.
To pull the pinned, total, borrowed, and in-transaction counts without changing proxy settings, set the name, region, and UTC bounds to your incident window. Repeat the same query for a baseline window.
proxy=my-proxy region=us-east-1 start=2026-12-04T14:00:00Z end=2026-12-04T14:30:00Z for metric in DatabaseConnectionsCurrentlySessionPinned DatabaseConnections \ DatabaseConnectionsCurrentlyBorrowed DatabaseConnectionsCurrentlyInTransaction do printf '\n%s\n' "$metric" aws cloudwatch get-metric-statistics \ --region "$region" --namespace AWS/RDS --metric-name "$metric" \ --dimensions "Name=ProxyName,Value=$proxy" \ --start-time "$start" --end-time "$end" \ --period 60 --statistics Sum \ --query 'sort_by(Datapoints, &Timestamp)[].[Timestamp, Sum]' --output table doneTell pinning apart from other pool pressure
- Pinned tracks borrowed while open transactions stay low. Look for session state introduced by a driver, ORM, or connection initialization hook. A fast query can still leave its backend reserved between requests.
- In-transaction tracks borrowed while pinned stays low. Check for
idle in transactionand long transactions. End the transaction before waiting on remote calls or other application work. - Open backends stay high while pinned and borrowed stay low. Check the idle pool and fragmentation by database, user, or startup
application_name. A different application name per worker can fragment the pool even when pinning is low.
These patterns can overlap. Add the application’s connection acquisition latency and request rate before changing pool limits. AWS examples of pinning, fragmentation, and open transactions.
In CloudWatch Logs Insights, select /aws/rds/proxy/<proxy-name> for the same time window. Start with standard pinning warnings. Inspect the reason and SQL digest, and match their first appearance to a deploy or connection-startup path. This query counts log events, while the CloudWatch gauge measures backends still reserved at each sample.
fields @timestamp, @logStream, @message | filter @message like /was pinned to the database connection/ | sort @timestamp desc | limit 50Use enhanced logging briefly if you need to correlate borrow, release, and pin events for an individual backend. Restrict access and retention first because enhanced logs may contain SQL text. AWS connection-level log analysis.
Isolate the statement that pins the session
Use an isolated staging proxy and connect directly to its endpoint with psql -X so a local startup file cannot issue extra commands. Take a SELECT 1 baseline, then open a fresh client for each case. After running a case, leave the client connected at the prompt for at least two complete CloudWatch sampling intervals and close it before starting the next case. Check the pinning warning and the gauge before, during, and after. The expected differences below come from AWS documentation and should be checked against the application’s actual driver and configuration.
Session SET versus SET LOCAL
-- Client A. Leave connected after this statement. SET statement_timeout = '5s'; -- Client B. Leave connected after COMMIT. BEGIN; SET LOCAL statement_timeout = '5s'; SELECT 1; COMMIT;The first setting persists across transactions. The second is scoped to the explicit transaction, and AWS documents an exemption for SET LOCAL inside it. Check what the driver actually sends. AWS lists set_config('statement_timeout', '5s', true) as a pinning case despite its local PostgreSQL scope. If every backend needs the same default, consider the proxy target group's initialization query, which runs on new backend connections. Keep per-request state inside the transaction and validate boundaries before moving a setting. RDS for PostgreSQL pinning rules and Aurora PostgreSQL pinning rules.
SQL PREPARE versus protocol-level preparation
-- A fresh client running SQL commands. PREPARE pin_probe(integer) AS SELECT $1 + 1; EXECUTE pin_probe(41);These are SQL commands, which AWS lists as pinning triggers. Driver-level prepared statements sent through PostgreSQL’s extended query protocol use messages such as Parse and Bind. RDS Proxy can track those statements and recreate them on a newly borrowed backend. Test the driver’s normal code path in a separate client. Looking at pg_prepared_statements through that proxy session can itself trigger a pin. Check startup and connection-release hooks too, especially DISCARD ALL, before changing preparation settings. AWS extended query protocol support.
Session advisory locks versus transaction advisory locks
-- Client A. Use a key reserved for this staging test. SELECT pg_try_advisory_lock(721021::bigint); -- Keep this client connected during the sample window, then clean up. SELECT pg_advisory_unlock(721021::bigint); -- Client B. Run after client A is disconnected. BEGIN; SELECT pg_try_advisory_xact_lock(721021::bigint); -- Continue only if the result is true. SELECT 1; COMMIT;A session lock can outlive COMMIT. The proxy can pin when it sees the session-level function, and releasing the lock does not end the pinned client session. The transaction-level variant is exempt from pinning and releases at transaction end. Both try forms return false if the key is already held by another session. Preserve session locks for work that must remain protected across commits.
While a lock is held, run this read-only query from a separate database monitoring connection to identify advisory lock holders or waiters. It observes PostgreSQL lock state, so correlate with the proxy logs and pinned gauge to attribute a pin.
SELECT a.pid, a.application_name, a.state, age(clock_timestamp(), a.xact_start) AS xact_age, l.mode, l.granted, l.classid, l.objid, l.objsubid FROM pg_locks AS l JOIN pg_stat_activity AS a USING (pid) WHERE l.locktype = 'advisory' AND a.datname = current_database() AND a.backend_type = 'client backend' ORDER BY l.granted, a.xact_start NULLS LAST, a.pid;PostgreSQL advisory lock semantics.
Validate the change at comparable load
Canary one change at a time and allow the old application pool connections to drain. Compare pinned share, borrowed backends against available capacity, and borrow latency after the churn settles. Hold traffic and concurrency roughly constant. Check application behavior too, including timeouts, transaction outcomes, and any locks or settings the code relied on. A lower pinned count caused by fewer connected clients does not establish better reuse.
Written by Kiran Sabne.
For PostgreSQL 14 on AWS RDS or Aurora, see the PG14 Upgrade Readiness checklist before standard support ends on Feb 28, 2027.