[PATCH] psql: Display SQLSTATE macro name in verbose error reports
Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.
You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:
docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t139732psql -h localhost -U postgresBuilt from patchset v1 (message #1), September 20, 2026 at 12:26 AM.
Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:
git clone --branch t139732_1 https://github.com/hackorum-dev/postgres.gitIn a checkout you already have, add the fork once:
git remote add hackorum https://github.com/hackorum-dev/postgres.gitthen, for this patchset and every later one:
git fetch hackorum t139732_1 && git checkout t139732_1Patchset v1 (message #1) is on t139732_1
Hi hackers,
Currently, when psql displays verbose error messages (\set VERBOSITY
verbose or \errverbose), it prints the 5-character SQLSTATE code. However,
developers often have to manually look up the corresponding C macro name
(e.g. ERRCODE_SYNTAX_ERROR) in the source code when writing C extensions or
dealing with internal backend code.
Attached is a small patch that improves the verbose error output in psql by
appending the symbolic SQLSTATE macro name to the end of the error report.
*Changes included in this patch:*
*1. Created a small Perl script in src/bin/psql to parse
src/backend/utils/errcodes.txt and generate a C header (sqlstate_names.h)
containing a mapping of SQLSTATE codes to their macro names.2. Hooked the
generation of this header into the psql Makefile and meson.build.3. Updated
psql's error reporting (ExecQueryAndProcessResults and
exec_command_errverbose) to perform a lookup and append the macro name if
VERBOSITY is verbose.*
Example output:
ERROR: 42601: syntax error at or near "FRO"
LINE 1: SELECT * FRO;
^
LOCATION: scanner_yyerror, scan.l:1220
SQLSTATE name: ERRCODE_SYNTAX_ERROR
I believe this will be a handy quality-of-life improvement for developers.
Looking forward to any feedback
Best regards,
Aaryan Parik
On Sun, May 24, 2026 at 3:18 AM Aaryan Parik <aaryanparik124@gmail.com> wrote:
Example output:
ERROR: 42601: syntax error at or near "FRO"
LINE 1: SELECT * FRO;
^
LOCATION: scanner_yyerror, scan.l:1220
SQLSTATE name: ERRCODE_SYNTAX_ERRORI believe this will be a handy quality-of-life improvement for developers. Looking forward to any feedback
This seems identical to the patch submitted in the previous email thread, [0]/messages/by-id/CAK3UJRGc2RKfQMjwVDAwsuOJuobq3d4+Qf9KWK1NHmK1vaqyxQ@mail.gmail.com.
[0]: /messages/by-id/CAK3UJRGc2RKfQMjwVDAwsuOJuobq3d4+Qf9KWK1NHmK1vaqyxQ@mail.gmail.com
jian he <jian.universality@gmail.com> writes:
On Sun, May 24, 2026 at 3:18 AM Aaryan Parik <aaryanparik124@gmail.com> wrote:
Example output:
ERROR: 42601: syntax error at or near "FRO"
LINE 1: SELECT * FRO;
^
LOCATION: scanner_yyerror, scan.l:1220
SQLSTATE name: ERRCODE_SYNTAX_ERROR
This seems identical to the patch submitted in the previous email thread, [0].
It's not exactly the same: Josh's patch did the dirty work in libpq
not psql. So this has a somewhat smaller blast radius for client
compatibility problems (but still not zero). The other comments
I posted in the other thread still apply though.
regards, tom lane