Skip to content

Fix server-cursor queries silently failing with 'keyword not supported: prepare' - #10343

Open
dpage wants to merge 1 commit into
pgadmin-org:masterfrom
dpage:fix/servercursor-prepare-kwarg
Open

Fix server-cursor queries silently failing with 'keyword not supported: prepare'#10343
dpage wants to merge 1 commit into
pgadmin-org:masterfrom
dpage:fix/servercursor-prepare-kwarg

Conversation

@dpage

@dpage dpage commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Problem

Every query run under "Use server cursor?" currently fails silently.

AsyncServerCursor.execute() (psycopg) has never accepted a prepare keyword — a server-side DECLARE CURSOR can't be a prepared statement — and raises TypeError on any unexpected keyword, even one whose value is None.

#10030 widened AsyncDictCursor.execute()/_execute() to forward prepare/binary to whatever cursor it holds, correctly fixing a psycopg_pool.ConnectionPool.check_connection breakage. AsyncDictServerCursor inherits that same execute()/_execute() without overriding it, so every server-cursor query now unconditionally forwards prepare=None into AsyncServerCursor.execute() and fails with:

TypeError: keyword not supported: prepare

That TypeError isn't a psycopg.Error, so execute_async()'s except psycopg.Error doesn't catch it. It propagates into the background QueryThread's generic exception handler, which logs it and builds an internal_server_error response that's discarded (the thread's return value goes nowhere) — so the query silently never runs. The user sees no error; the async cursor is left with no query ever actually executed on it, which is also the precondition behind the crash in #10321 (get_explain_query_length being handed a cursor whose _query is None).

Fix

Give AsyncDictServerCursor its own _execute() that drops prepare before delegating, leaving DictCursor/AsyncDictCursor's forwarding (the actual #10030 fix) untouched for the pool-checkout case it was written for.

Tests

  • Strengthened test_server_cursor.py's existing scenario to assert the poll response's actual status/result, rather than only the HTTP status code and the echoed server_cursor flag (which is just config, not proof the query ran) — this is what let the regression through un-caught.
  • Added a fast, DB-less unit test asserting AsyncDictServerCursor._execute never forwards prepare to the underlying cursor.

Both fail without the fix and pass with it; ran locally against PostgreSQL 18.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed server-cursor query execution with the asynchronous PostgreSQL driver.
    • Queries now execute successfully while preserving binary-result handling and parameters.
    • Improved validation to confirm server-cursor queries return the expected results.
  • Tests

    • Added regression coverage for server-cursor execution and parameter handling.

psycopg's AsyncServerCursor.execute() has never accepted a prepare kwarg
(a server-side DECLARE CURSOR can't be a prepared statement) and raises
TypeError on any unexpected keyword, even one whose value is None.

PR pgadmin-org#10030 widened AsyncDictCursor.execute()/_execute() to forward
prepare/binary to whatever cursor it holds, fixing a real
psycopg_pool.ConnectionPool.check_connection breakage. AsyncDictServerCursor
inherits that same execute()/_execute() without overriding it, so every
server-cursor query now unconditionally forwards prepare=None straight into
AsyncServerCursor.execute() and fails with
"TypeError: keyword not supported: prepare".

That TypeError isn't a psycopg.Error, so execute_async()'s
"except psycopg.Error" doesn't catch it. It propagates into the background
QueryThread's generic exception handler, which logs it and builds an
internal_server_error response that is discarded (the thread's return value
goes nowhere) - so the query silently never runs, invisible to the user,
and the async cursor is left with no query having actually executed on it.

Give AsyncDictServerCursor its own _execute() that drops prepare before
delegating, leaving DictCursor/AsyncDictCursor's forwarding for the
pool-checkout case it was written for untouched.

Strengthen test_server_cursor.py's existing scenario to assert the poll
response's actual status/result instead of only the HTTP status code and
the echoed server_cursor flag, and add a fast, DB-less regression test
asserting AsyncDictServerCursor._execute never forwards prepare.
@coderabbitai

coderabbitai Bot commented Aug 25, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: d3b0381f-b204-4199-8de2-16cf2d6be914

📥 Commits

Reviewing files that changed from the base of the PR and between bc58657 and 4dc3f77.

📒 Files selected for processing (3)
  • web/pgadmin/tools/sqleditor/tests/test_server_cursor.py
  • web/pgadmin/utils/driver/psycopg3/cursor.py
  • web/pgadmin/utils/tests/test_psycopg3_cursor_signature.py

Included review availability: Your plan provides up to 8 included reviews per hour; 5 remain after this review.


Walkthrough

The change updates AsyncDictServerCursor._execute to omit unsupported prepare arguments. It adds regression coverage for argument forwarding and verifies that a server-cursor query returns the expected result.

Changes

Server cursor execution

Layer / File(s) Summary
Update server cursor execution adapter
web/pgadmin/utils/driver/psycopg3/cursor.py
AsyncDictServerCursor._execute converts empty params lists to None and forwards the query, parameters, and binary value without prepare.
Validate server cursor behavior
web/pgadmin/utils/tests/test_psycopg3_cursor_signature.py, web/pgadmin/tools/sqleditor/tests/test_server_cursor.py
Tests verify argument forwarding and confirm that server-cursor execution returns status Success, one row, and the value 1.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 4dc3f

The change narrowly prevents server-cursor queries from failing when an unsupported prepare argument is forwarded, with targeted tests covering the regression; no actionable merge-blocking risk remains.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely identifies the main change: fixing server-cursor queries that fail because psycopg does not support the forwarded "prepare" keyword.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@kundansable kundansable self-assigned this Aug 26, 2026
@kundansable kundansable added this to the 9.18 milestone Aug 27, 2026
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