-
Notifications
You must be signed in to change notification settings - Fork 39
DBD::Pg Async Query Ownership and Data Preservation Fixes #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
ping ? |
|
Please pull the recent async-related changes and retest. |
|
Also, this PR has merge conflicts. Please rebase/resolve, @oetiker. |
I will have to re-evaluate after the changes ... |
- Fix statement ownership checks in dbd_st_finish and dbd_st_destroy - Add auto-retrieval of results with PG_OLDQUERY_WAIT instead of discarding - Add ownership verification in pg_db_result to prevent result stealing - Handle $dbh->do() async queries (no statement handle) correctly - Add NULL check for async_sth in handle_old_async cancel path - Add named constants STH_ASYNC_AUTORETRIEVED and STH_ASYNC_AUTOERROR Fixes GitHub Issue bucardo#105 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
e40cc86 to
fe5c547
Compare
Add missing 'use lib' statement to load from blib/ instead of system-installed DBD::Pg. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
The async connect tests create new connections but were hardcoding
an empty password. Now uses $ENV{DBI_PASS} when set, falling back
to empty string for environments with trust authentication.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <[email protected]>
|
The PR is rebased and the entire testsuite is passing including the new testfile. |
|
@turnstep : Could you approve the CI workflow to run for this PR? |
Thank you, @oetiker ! Looks promising. |
DBD::Pg Async Query Ownership and Data Preservation Fixes
Caveat! I developed this code and the tests tests using Claude code in order to fix to #105 and mojolicious/mojo#2276
Overview
This document describes the comprehensive fixes for DBD::Pg's asynchronous query handling, addressing GitHub issue #105 and related problems with
PG_OLDQUERY_WAITdata loss.Problems Addressed
1. GitHub Issue #105: Statement Destruction Cancels Unrelated Async Queries
Problem: Destroying any statement handle would cancel the active async query of another statement, even if the destroyed statement never executed an async query.
Root Cause: The
dbd_st_destroy()anddbd_st_finish()functions unconditionally cleared the globalimp_dbh->async_sthpointer and calledhandle_old_async()without checking if the statement being destroyed actually owned the async query.Impact: Applications using multiple statement handles with async queries would experience random query cancellations when unrelated statements were destroyed or went out of scope.
2. PG_OLDQUERY_WAIT Data Loss
Problem: Using
PG_OLDQUERY_WAITto wait for a previous async query would successfully wait but would discard the query results instead of preserving them.Root Cause: The
handle_old_async()function would consume and discard results when waiting, with no mechanism to preserve them for the owning statement.Impact: Data loss - results from async queries would be irretrievably lost when another query used
PG_OLDQUERY_WAIT.3. Missing Ownership Verification
Problem: Any statement handle could call
pg_result()and retrieve another statement's async results, violating ownership semantics.Root Cause: The
pg_db_result()function did not verify that the calling statement was the one that initiated the async query.Impact: Race conditions, incorrect data retrieval, and unpredictable behavior in multi-statement applications.
Technical Background
PostgreSQL's async query limitations:
imp_dbh->async_sthpointing to the statement that owns the current async queryThe Fixes
Fix 1: Ownership Checking in Statement Lifecycle Functions
Location:
dbdimp.c-dbd_st_finish()(lines 3977-3980)Location:
dbdimp.c-dbd_st_destroy()(lines 4122-4127, 4194-4197)Fix 2: Auto-Retrieve Mechanism for PG_OLDQUERY_WAIT
Location:
dbdimp.c-handle_old_async()(line ~5715)Instead of discarding results, the function now:
async_status = 100to indicate auto-retrieved resultsFix 3: Ownership Verification in pg_db_result()
Location:
dbdimp.c-pg_db_result()(lines 5292-5397)Added comprehensive ownership checking:
Fix 4: Auto-Retrieved Result Handling
Location: dbdimp.c - pg_db_result()
Special handling for auto-retrieved results:
New Async Status Values
The implementation uses named constants for async_status values on statement handles:
Test Coverage
The fix includes comprehensive test coverage in
t/08async_regression.twith 7 test scenarios:Behavior Changes
Before the Fix
PG_OLDQUERY_WAITwould discard previous query resultsAfter the Fix
PG_OLDQUERY_WAITauto-retrieves and preserves resultsMigration Notes
The fixes are backward compatible with one exception: