Skip to content

Fix trailing SQL comments breaking multiline submit and execution#1559

Open
DiegoDAF wants to merge 1 commit intodbcli:mainfrom
DiegoDAF:fix/trailing-comments-semicolon
Open

Fix trailing SQL comments breaking multiline submit and execution#1559
DiegoDAF wants to merge 1 commit intodbcli:mainfrom
DiegoDAF:fix/trailing-comments-semicolon

Conversation

@DiegoDAF
Copy link

@DiegoDAF DiegoDAF commented Mar 2, 2026

Summary

Fixes two related bugs when a SQL comment follows the semicolon, e.g.:

vacuum freeze verbose tpd.file_delivery; -- 82% towards emergency, 971 MB

Bug 1 — pgbuffer._is_complete(): sql.endswith(";") returned False when a trailing comment followed the semicolon. In multiline mode, pressing Enter never submitted the query — the UI appeared frozen.

Bug 2 — pgexecute.run(): sql.rstrip(";") couldn't strip the semicolon because the string ended with the comment text, not ;. The malformed SQL (with embedded ;) was sent to PostgreSQL via psycopg's extended query protocol, which doesn't support multiple statements.

Fix

Both locations now use sqlparse.format(sql, strip_comments=True) before checking for / removing the trailing semicolon.

Files Changed

  • pgcli/pgbuffer.py_is_complete() strips comments before endswith(";")
  • pgcli/pgexecute.pyrun() strips comments before rstrip(";")
  • changelog.rst — Added bug fix entry to Upcoming
  • tests/test_trailing_comments.py — 12 new tests covering edge cases

Test plan

  • All 12 new tests pass (pytest tests/test_trailing_comments.py -v)
  • Full test suite passes (2788 passed, 128 skipped)
  • Manually verified: vacuum freeze verbose tpd.file_delivery; -- 82% towards emergency executes correctly
  • Verify multiline mode submits correctly with trailing comments

Two bugs when a comment follows the semicolon (e.g. "SELECT 1; -- note"):

1. pgbuffer._is_complete() checked sql.endswith(";") which returned False
   when a comment followed, preventing multiline mode from ever submitting

2. pgexecute.run() used rstrip(";") which couldn't find the semicolon
   past the trailing comment, sending malformed SQL to PostgreSQL

Both fixes strip comments (via sqlparse) before checking for the semicolon.

Made with ❤️ and 🤖 Claude
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.

1 participant