Skip to content

Fix downloading a BYTEA cell when the whole result has been fetched. #10906 - #10907

Merged
dpage merged 1 commit into
pgadmin-org:masterfrom
dpage:fix/issue-10906-bytea-download-position
Nov 30, 2026
Merged

dpage merged 1 commit into
pgadmin-org:masterfrom
dpage:fix/issue-10906-bytea-download-position

Conversation

@dpage

@dpage dpage commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • download_binary_data() restored the cursor with scroll(saved_pos, mode='absolute'), but once every row has been read the saved position is one past the last row, which psycopg rejects with IndexError, so the download returned "Invalid row/column position." for any result that fits in the first page.
  • The position is now restored by scrolling to the last row and reading it when psycopg refuses the direct scroll, so the cursor is left exhausted as it was rather than just ignoring the error. Part-way positions and server-side cursors (which never raise) are restored exactly as before.

Test plan

  • New utils.driver.psycopg3.tests.test_download_binary_data against PostgreSQL 18 checks the downloaded value and the cursor position afterwards with none, some and all rows read; the all-rows case fails without the fix.
  • pycodestyle on the changed files.

Closes #10906

Summary by CodeRabbit

  • Bug Fixes
    • Fixed binary data downloads so they preserve the cursor’s position, including when the cursor is at the end of the results.
  • Tests
    • Added coverage for downloads from cursors with no rows, some rows, or all rows already read.

…gadmin-org#10906

download_binary_data() restores the cursor position after reading the
requested row, but once every row has been read that position is one past
the last row, which psycopg refuses to scroll to, so the download failed
with "Invalid row/column position.". Restore it by reading the last row
instead, leaving the cursor exhausted as it was.

Closes pgadmin-org#10906
@dpage dpage self-assigned this Nov 30, 2026
@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Binary data downloads now restore the cursor position through a helper. Tests cover downloads when zero, one, or three rows have already been read.

Changes

Binary download cursor restoration

Layer / File(s) Summary
Restore cursor position
web/pgadmin/utils/driver/psycopg3/connection.py, web/pgadmin/utils/driver/psycopg3/tests/test_download_binary_data.py
download_binary_data uses a helper to restore the saved position. If the cursor is past the final row, the helper moves to the last row and fetches it. Tests check the cursor position and subsequent fetch results for three starting positions.

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: pravesh-sharma, kundansable

Merge Risk: Minimal · up to dc432

Downloading a BYTEA cell after the whole result has been fetched should now work instead of failing with "Invalid row/column position." The change is small and localized, with no concrete merge risk identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the BYTEA download fix after the whole result has been fetched, which matches the main change: a new _restore_cursor_position helper handles the one-past-last-row case in download_bina…
Linked Issues check Passed [#10906] The issue requires that downloading a BYTEA cell succeeds after the whole result has been fetched. Before the change, download_binary_data() restored the cursor with `cur.scroll(saved_pos, …
Out of Scope Changes check Passed All changes concern the reported defect. The helper in connection.py is used only by download_binary_data(). The new test file exercises that same method and its cursor restore behavior. No unrela…
Full details: Title check

Explanation

The title names the BYTEA download fix after the whole result has been fetched, which matches the main change: a new _restore_cursor_position helper handles the one-past-last-row case in download_binary_data. The issue number is also referenced. The title is specific and related to the changeset.

Full details: Linked Issues check

Explanation

[#10906] The issue requires that downloading a BYTEA cell succeeds after the whole result has been fetched. Before the change, download_binary_data() restored the cursor with cur.scroll(saved_pos, mode='absolute'). When every row is read, saved_pos equals the row count, and psycopg raises IndexError, which becomes "Invalid row/column position.". The change adds Connection._restore_cursor_position(cur, pos) in web/pgadmin/utils/driver/psycopg3/connection.py. It calls scroll(pos, mode='absolute'). On IndexError with pos >= 1, it scrolls to pos - 1 and calls fetchone(). This leaves rownumber at pos, so the cursor is exhausted as before. The finally block in download_binary_data() now calls this helper. Part-way positions do not raise and follow the original path. Negative or zero positions re-raise. The new test DownloadBinaryDataTest in web/pgadmin/utils/driver/psycopg3/tests/test_download_binary_data.py covers zero, some, and all rows read. It checks the downloaded value, that rownumber is unchanged, and that later fetches return the remaining rows. I verified the logic from source. I did not execute the test.

Full details: Out of Scope Changes check

Explanation

All changes concern the reported defect. The helper in connection.py is used only by download_binary_data(). The new test file exercises that same method and its cursor restore behavior. No unrelated files or behavior changes appear in the whole-PR diff against the merge base 43d359a6. The Python test file is the supporting test for the fix and is within scope.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 43d359a and dc43227.

Files selected for processing: 2

  • web/pgadmin/utils/driver/psycopg3/connection.py
  • web/pgadmin/utils/driver/psycopg3/tests/test_download_binary_data.py

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

@dpage
dpage merged commit de67205 into pgadmin-org:master Nov 30, 2026
44 of 47 checks passed
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.

Downloading a BYTEA cell fails with "Invalid row/column position." when the whole result has been fetched

1 participant