[PATCH] psql: Display SQLSTATE macro name in verbose error reports
[PATCH] psql: Display SQLSTATE macro name in verbose error reports
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
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
Re: [PATCH] psql: Display SQLSTATE macro name in verbose error reports
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
jian he writes: > On Sun, May 24, 2026 at 3:18 AM Aaryan Parik 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