Conversation
Connection hooks and the active-connection probe can implicitly begin a transaction when DB-API autocommit is disabled. Commit successful connection initialization before returning fresh or pooled connections so PostgreSQL does not retain otherwise unused backends as idle in transaction. Committing preserves successful SQL-based session configuration performed by connection hooks, whereas rolling back could undo that initialization work. Add regression coverage for fresh connections, pooled checkouts, and configurations with active connection checks disabled.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ConnectionPool.set_connection()runs new-connection hooks and an optionalliveness query, commonly
SELECT 1;, but does not finalize the transactionthose operations can start when DB-API autocommit is disabled.
This is observable with PostgreSQL when a long-lived process initializes DAL at
module-import or worker-startup time, outside a request lifecycle. Because no
request teardown follows,
pg_stat_activitycan show that connection asidle in transactionindefinitely, withSELECT 1;as its last query.The behavior was diagnosed in a persistent OVXS deployment by Auden
RovelleQuartz of Omnia Value Exchange Systems.
Change
After the connection hooks and optional active-connection check complete
successfully,
set_connection()now commits the resulting initializationtransaction when either the hooks ran or the active check was performed.
This gives connection acquisition an explicit transaction boundary for:
Why commit
A rollback could undo SQL-based session configuration performed by
after_connectionhooks. Committing, rather than rolling back, preservessuccessful initialization while ending the implicit transaction.
This occurs before the connection is returned to application code, so no
caller-owned transaction exists at this boundary.
If both connection hooks and active checking are disabled,
set_connection()does not add a commit.
Tests
The regression tests model transaction state and cover:
Validation performed against upstream commit
6fd518e09ac4fcf64408abb2259222251c870356:Reproduction outline
pg_stat_activity.Before this change, the initialized backend can remain
idle in transaction,with the connection-test query as its last statement. After this change, the
backend is idle with no open transaction.