Skip to content

Conversation

@ardentperf
Copy link
Contributor

When a connection is returned to the database/sql pool with an open
transaction, ResetSession now returns driver.ErrBadConn to discard it.
This prevents transaction state from leaking between operations.

Previously, if application code failed to commit or rollback a
transaction, the connection would be returned to the pool and reused.
Subsequent operations would unknowingly execute inside the leaked
transaction, and their work would be lost when the connection was
eventually discarded.

This matches the behavior of pgxpool, which already checks TxStatus when
releasing connections and destroys those not in idle state.

Signed-off-by: Jeremy Schneider schneider@ardentperf.com

When a connection is returned to the database/sql pool with an open
transaction, ResetSession now returns driver.ErrBadConn to discard it.
This prevents transaction state from leaking between operations.

Previously, if application code failed to commit or rollback a
transaction, the connection would be returned to the pool and reused.
Subsequent operations would unknowingly execute inside the leaked
transaction, and their work would be lost when the connection was
eventually discarded.

This matches the behavior of pgxpool, which already checks TxStatus when
releasing connections and destroys those not in idle state.

Signed-off-by: Jeremy Schneider <schneider@ardentperf.com>
@ardentperf
Copy link
Contributor Author

FYI - a reproduction is here https://github.com/ardentperf/pg-idle-test/tree/main/conn_exhaustion

I also used that test framework to test this PR

Would be good for a second pair of eyes to double-check that I'm adding the line in the right place though. I am pretty new to pgx (and golang in general)

@jackc jackc merged commit c2c9ce2 into jackc:master Jan 24, 2026
14 checks passed
@jackc
Copy link
Owner

jackc commented Jan 24, 2026

LGTM. I also added a test that validates correct behavior: 943af21

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants