Reparse unnamed statements before cursor binding - #1364
Merged
Merged
Conversation
With `statement_cache_size=0`, type introspection can replace the unnamed statement created by `prepare()`. The statement is marked unprepared, but awaiting `PreparedStatement.cursor()` binds it without reparsing, so the server receives arguments for the introspection query instead. Make `bind()` honor the unprepared flag, as `bind_execute()` already does to fix this. Fixes #1335. Closes #1345.
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The fix aligns bind() behavior with the already-correct bind_execute() path, updates the protocol state machine accordingly, and includes a focused regression test for the reported failure mode.
Review effort: Lite
Findings: None
What changed in this PR
This PR fixes a regression where, with statement_cache_size=0, a PreparedStatement marked as unprepared (due to internal type-introspection clobbering the unnamed statement) could still be cursor-bound without reparsing, causing server-side cursor binds to target the wrong unnamed statement.
Changes:
- Make
BaseProtocol.bind()re-Parsethe statement whenPreparedStatementState.preparedis false (matching existingbind_execute()behavior). - Teach the core protocol bind state machine to accept
ParseCompleteduring the bind flow when reparsing occurs. - Add a regression test covering the unnamed-statement + enum-introspection + cursor path with cache disabled.
| File | Description |
|---|---|
| tests/test_cursor.py | Adds a regression test ensuring cursor bind reparses unnamed statements when introspection clobbers them under statement_cache_size=0. |
| asyncpg/protocol/protocol.pyx | Updates bind() to honor the prepared flag by sending a Parse when needed before binding. |
| asyncpg/protocol/coreproto.pyx | Allows ParseComplete to be handled during PROTOCOL_BIND processing when a reparse is emitted ahead of Bind. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
With
statement_cache_size=0, type introspection can replace the unnamedstatement created by
prepare(). The statement is marked unprepared, butawaiting
PreparedStatement.cursor()binds it without reparsing, so theserver receives arguments for the introspection query instead.
Make
bind()honor the unprepared flag, asbind_execute()already doesto fix this.
Fixes #1335.
Closes #1345.