Repository navigation
feat!: Show the internal query and statement excerpt in server errors - #671
Conversation
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Hr1rd6e5NcxC9GSrNJDokc
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
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 Generated by Claude Code |
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
Accepted limitations
Generated by Claude Code |
Follow-up to #666, fixing the findings of its post-merge review.
result_stop()now also collectsPG_DIAG_INTERNAL_QUERYandPG_DIAG_INTERNAL_POSITION, exposed as the condition fieldsinternal_queryandinternal_positionand shown as bullets. An error in dynamic SQL, such asDO $$ BEGIN EXECUTE 'SELECT * FROM nope'; END $$, shows the failing statement again, as libpq'sQUERY:line did.LINE n:excerpt. The C++ side passes the statement text tosignal_pq_error(), andpq_statement_excerpt()builds the excerpt in R:\r\n,\rand\nend 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.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 anSQL expressionfrom PG 14), so the snapshots keep showing the bullets.PQcancel()while cleaning up the previous query, used to be dropped by the next call. Signaling it before the error would letoptions(warn = 2)or a warning handler replace the error, so the error carries it instead: awarningsfield and onei Warning:bullet each, also for errors detected by libpq. The"Interrupted."error instep_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.check_interrupts = TRUE) or for notifications, and a failedPQgetCancel(), used to be plaincpp11::stop()errors that dropped queued warnings. They now go throughconn_stop()and areRPostgres_errors with libpq's text."Interrupted."and a failedselect()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-conditionsdocuments the new fields, the excerpt, thewarningsfield, and which errors are classed.Example:
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 forinternal_queryandinternal_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 bysignal_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 failedPQcancel()was checked by hand by stopping the server with an open result: underoptions(warn = 2)the caller still gets theRPostgres_errorwith the warning inwarnings; there is no automated test, since it needs the server stopped.test-sqlstate.R,test-deferred-warnings.Rand 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