[PATCH] psql: Display SQLSTATE macro name in verbose error reports

From: Aaryan Parik <aaryanparik124(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: [PATCH] psql: Display SQLSTATE macro name in verbose error reports
Date: 2026-05-23 19:18:22
Message-ID: CAFAL27jsbbRkjj=zwudUU7vpmNHDgRCQXYEv_qx6D9uc7LLYvQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Attachment Content-Type Size
psql-sqlstate-macro-name.patch application/octet-stream 6.9 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Julien Rouhaud 2026-05-23 23:25:57 Re: Improve pg_stat_statements scalability
Previous Message Evgeny Kuzin 2026-05-23 19:05:58 Re: [PATCH] libpq: try all addresses for a host before moving to next on target_session_attrs mismatch