Skip to content

feat!: Show the internal query and statement excerpt in server errors - #671

Merged
krlmlr merged 5 commits into
mainfrom
claude/sqlstate-followup
Nov 29, 2026
Merged

krlmlr merged 5 commits into
mainfrom
claude/sqlstate-followup

Conversation

@krlmlr

@krlmlr krlmlr commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #666, fixing the findings of its post-merge review.

  • Internal query. result_stop() now also collects PG_DIAG_INTERNAL_QUERY and PG_DIAG_INTERNAL_POSITION, exposed as the condition fields internal_query and internal_position and shown as bullets. An error in dynamic SQL, such as DO $$ BEGIN EXECUTE 'SELECT * FROM nope'; END $$, shows the failing statement again, as libpq's QUERY: line did.
  • Statement excerpt. A position bullet is followed by the line of the statement it points into, with a caret below, like libpq's LINE n: excerpt. The C++ side passes the statement text to signal_pq_error(), and pq_statement_excerpt() builds the excerpt in R: \r\n, \r and \n end lines, the caret counts display columns, and a line wider than 60 columns is cut around the caret with ..., as libpq does. The internal position gets the excerpt of the internal query.
  • Snapshots across server versions. The new scrub_server_version() transform removes server wording that differs between PostgreSQL versions (the not-null message names the relation from PG 13; PL/pgSQL expressions are an SQL expression from PG 14), so the snapshots keep showing the bullets.
  • Queued warnings travel with the error. A warning queued during the failing call, such as a failed PQcancel() while cleaning up the previous query, used to be dropped by the next call. Signaling it before the error would let options(warn = 2) or a warning handler replace the error, so the error carries it instead: a warnings field and one i Warning: bullet each, also for errors detected by libpq. The "Interrupted." error in step_run() appends them to its message. Every connection entry point that can raise such an error now clears the queue on entry, as the result entry points do.
  • Remaining libpq errors classed. Errors that libpq detects while waiting for data (check_interrupts = TRUE) or for notifications, and a failed PQgetCancel(), used to be plain cpp11::stop() errors that dropped queued warnings. They now go through conn_stop() and are RPostgres_errors with libpq's text. "Interrupted." and a failed select() are classed the same way, without libpq's text. Errors from large-object import/export, from a disconnected connection and from argument checks stay ordinary errors and drop queued warnings, as documented.
  • ?RPostgres-conditions documents the new fields, the excerpt, the warnings field, and which errors are classed.
  • The excerpt is skipped for a statement that is not valid in its encoding, so that it cannot replace the error.

Example:

Error in `dbSendQuery()`:
! Failed to fetch row : relation "no_such_table_579" does not exist
i SQLSTATE: 42P01
i Internal query: SELECT * FROM no_such_table_579
i Internal position: 15
  LINE 1: SELECT * FROM no_such_table_579
                        ^
i Context: PL/pgSQL function inline_code_block line 1 at EXECUTE

Tests: new snapshots for dynamic SQL (one and two lines), a multi-line statement with \r\n, and a long line cut around the position; field checks for internal_query and internal_position; unit tests for the excerpt (line breaks, tabs, wide characters, cuts at either end, a statement of a million characters, positions outside the statement, invalid UTF-8) and for warnings carried by signal_pq_error(). The excerpt only splits the characters around the caret, so a statement of 20 million characters formats in about 0.4 s. A failed PQcancel() was checked by hand by stopping the server with an open result: under options(warn = 2) the caller still gets the RPostgres_error with the warning in warnings; there is no automated test, since it needs the server stopped. test-sqlstate.R, test-deferred-warnings.R and the full suite pass locally against PostgreSQL 16.

Accepted limitations: see the Accepted limitations comment.

Open question: whether to announce the new message format of #666 as a breaking change in NEWS.md (question).

https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc


Generated by Claude Code

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Question: Announce the new error message format of #666 as a breaking change?

#666 changed the text of every server error: the message is now the failed step followed by the server's primary message, and the diagnostic fields follow as i bullets (R/signal_pq_error.R#L103-L107 at d34e76ad). libpq's ERROR: prefix and its DETAIL:, HINT:, CONTEXT: and LINE n: lines are gone. Code that matches the old message text, such as grepl("DETAIL:", conditionMessage(e)) or a snapshot of an RPostgres error in a downstream package, breaks. #666 was squash-merged as a plain feat: and NEWS.md says nothing about it.

.github/CONTRIBUTING.md asks contributors not to edit NEWS.md, but the fledge marker was dropped in #628, so NEWS is now maintained by hand. Options:

  • (a) Add a NEWS.md entry that marks the new message format as a breaking change and points to ?RPostgres-conditions and the sqlstate field, and run a revdep check (the revdep.yaml workflow) to see which reverse dependencies match the old text. (recommended)
  • (b) Add the NEWS.md entry only, without a revdep check.
  • (c) Nothing: leave it to the release notes.

Cost: (a) is one bullet in this PR plus a revdep run of a few hours of CI; (b) is the bullet only. Without a decision, this PR changes no NEWS.md and the change reaches CRAN unannounced. Kirill has the say.


Generated by Claude Code

claude and others added 4 commits November 29, 2026 18:15
Server errors now also carry `internal_query` and `internal_position`,
so that an error in dynamic SQL, such as a string run by `EXECUTE` in PL/pgSQL,
shows the failing statement again.
A position is followed by the line of the statement it points into
with a caret below, like libpq's `LINE n:` excerpt.

Warnings queued during the failing call, such as a failed `PQcancel()`,
are signaled before the error instead of being dropped by the next call.

The snapshots no longer depend on server wording
that differs between PostgreSQL versions.

Follow-up to #666.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc
Signaling a queued warning, such as a failed `PQcancel()`, before the server error
let `options(warn = 2)` or a warning handler replace the error.
The error now carries them in a `warnings` field and as `i Warning:` bullets,
and the "Interrupted." error appends them to its message.
Every connection entry point that can raise such an error clears the queue on entry.

The statement excerpt no longer splits the whole statement into characters:
it finds the line with one regular expression and works on the characters around the caret.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc
Errors detected by libpq while waiting for data or notifications,
and a failed `PQgetCancel()`, now go through `conn_stop()`,
so that they are `RPostgres_error`s with libpq's text and carry queued warnings.
"Interrupted." and a failed `select()` are classed the same way, without libpq's text.

The statement excerpt is skipped for a statement that is not valid in its encoding.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc

krlmlr commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

Accepted limitations

  • src/DbConnection.cpp#L353 at 04f3dfaf: a warning queued before an error, such as a failed PQcancel() once the server is gone, travels with the error, but no automated test covers that path, because it needs the server stopped while a result is open. It was checked by hand twice (stopped server, options(warn = 2), with and without check_interrupts = TRUE and immediate = TRUE): the caller gets an RPostgres_error with the PQcancel() text in warnings. A fix would be a test that runs the connection through a TCP proxy it can cut. Relevance: low, rare path. Reviewer's confidence: high (reproduced in round 2).
  • R/signal_pq_error.R#L74 at 04f3dfaf: ordinary errors still drop queued warnings, namely those of large-object import and export (src/DbConnection.cpp#L132), of a disconnected connection (#L187), and of argument checks. Their entry points clear the queue first, so only a warning from the same call can be lost, and none of them queues one today. Documented in ?RPostgres-conditions (recorded). Relevance: low. Reviewer's confidence: high.
  • The "Interrupted." error is now an RPostgres_error carrying queued warnings, but under a real interrupt R's own interrupt condition reaches the caller first (seen through callr), so the classed error is seldom seen. Relevance: low. Reviewer's confidence: medium.

Generated by Claude Code

@krlmlr
krlmlr marked this pull request as ready for review November 29, 2026 18:57
@krlmlr krlmlr changed the title fix: Show the internal query and statement excerpt in server errors feat!: Show the internal query and statement excerpt in server errors Nov 29, 2026
@krlmlr
krlmlr merged commit 7285d13 into main Nov 29, 2026
9 checks passed
@krlmlr
krlmlr deleted the claude/sqlstate-followup branch November 29, 2026 19:09
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