Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7bac3ac

Browse files
committed
Add a "SQLSTATE-only" error verbosity option to libpq and psql.
This is intended for use mostly in test scripts for external tools,which could do without cross-PG-version variations in error messagewording. Of course, the SQLSTATE isn't guaranteed stable either, butit should be more so than the error message text.Note: there's a bit of an ABI change for libpq here, but it seemsOK because if somebody compiles against a newer version of libpq-fe.h,and then tries to pass PQERRORS_SQLSTATE to PQsetErrorVerbosity()of an older libpq library, it will be accepted and then act likePQERRORS_DEFAULT, thanks to the way the tests in pqBuildErrorMessage3have historically been phrased. That seems acceptable.Didier Gautheron, reviewed by Dagfinn Ilmari MannsåkerDiscussion:https://postgr.es/m/CAJRYxuKyj4zA+JGVrtx8OWAuBfE-_wN4sUMK4H49EuPed=mOBw@mail.gmail.com
1 parent413ccaa commit7bac3ac

File tree

9 files changed

+98
-25
lines changed

9 files changed

+98
-25
lines changed

‎doc/src/sgml/libpq.sgml

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6014,21 +6014,30 @@ typedef enum
60146014
{
60156015
PQERRORS_TERSE,
60166016
PQERRORS_DEFAULT,
6017-
PQERRORS_VERBOSE
6017+
PQERRORS_VERBOSE,
6018+
PQERRORS_SQLSTATE
60186019
} PGVerbosity;
60196020

60206021
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity);
60216022
</synopsis>
60226023

6023-
<function>PQsetErrorVerbosity</function> sets the verbosity mode, returning
6024-
the connection's previous setting. In <firstterm>TERSE</firstterm> mode,
6025-
returned messages include severity, primary text, and position only;
6026-
this will normally fit on a single line. The default mode produces
6027-
messages that include the above plus any detail, hint, or context
6028-
fields (these might span multiple lines). The <firstterm>VERBOSE</firstterm>
6029-
mode includes all available fields. Changing the verbosity does not
6030-
affect the messages available from already-existing
6031-
<structname>PGresult</structname> objects, only subsequently-created ones.
6024+
<function>PQsetErrorVerbosity</function> sets the verbosity mode,
6025+
returning the connection's previous setting.
6026+
In <firstterm>TERSE</firstterm> mode, returned messages include
6027+
severity, primary text, and position only; this will normally fit on a
6028+
single line. The default mode produces messages that include the above
6029+
plus any detail, hint, or context fields (these might span multiple
6030+
lines). The <firstterm>VERBOSE</firstterm> mode includes all available
6031+
fields. The <firstterm>SQLSTATE</firstterm> mode includes only the
6032+
error severity and the <symbol>SQLSTATE</symbol> error code, if one is
6033+
available (if not, the output is like <firstterm>TERSE</firstterm>
6034+
mode).
6035+
</para>
6036+
6037+
<para>
6038+
Changing the verbosity setting does not affect the messages available
6039+
from already-existing <structname>PGresult</structname> objects, only
6040+
subsequently-created ones.
60326041
(But see <function>PQresultVerboseErrorMessage</function> if you
60336042
want to print a previous error with a different verbosity.)
60346043
</para>
@@ -6061,13 +6070,19 @@ PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibilit
60616070

60626071
<function>PQsetErrorContextVisibility</function> sets the context display mode,
60636072
returning the connection's previous setting. This mode controls
6064-
whether the <literal>CONTEXT</literal> field is included in messages
6065-
(unless the verbosity setting is <firstterm>TERSE</firstterm>, in which
6066-
case <literal>CONTEXT</literal> is never shown). The <firstterm>NEVER</firstterm> mode
6073+
whether the <literal>CONTEXT</literal> field is included in messages.
6074+
The <firstterm>NEVER</firstterm> mode
60676075
never includes <literal>CONTEXT</literal>, while <firstterm>ALWAYS</firstterm> always
60686076
includes it if available. In <firstterm>ERRORS</firstterm> mode (the
6069-
default), <literal>CONTEXT</literal> fields are included only for error
6070-
messages, not for notices and warnings. Changing this mode does not
6077+
default), <literal>CONTEXT</literal> fields are included only in error
6078+
messages, not in notices and warnings.
6079+
(However, if the verbosity setting is <firstterm>TERSE</firstterm>
6080+
or <firstterm>SQLSTATE</firstterm>, <literal>CONTEXT</literal> fields
6081+
are omitted regardless of the context display mode.)
6082+
</para>
6083+
6084+
<para>
6085+
Changing this mode does not
60716086
affect the messages available from
60726087
already-existing <structname>PGresult</structname> objects, only
60736088
subsequently-created ones.

‎doc/src/sgml/ref/psql-ref.sgml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3892,7 +3892,8 @@ bar
38923892
messages from the server. The default is <literal>errors</literal> (meaning
38933893
that context will be shown in error messages, but not in notice or
38943894
warning messages). This setting has no effect
3895-
when <varname>VERBOSITY</varname> is set to <literal>terse</literal>.
3895+
when <varname>VERBOSITY</varname> is set to <literal>terse</literal>
3896+
or <literal>sqlstate</literal>.
38963897
(See also <command>\errverbose</command>, for use when you want a verbose
38973898
version of the error you just got.)
38983899
</para>
@@ -3946,8 +3947,9 @@ bar
39463947
<listitem>
39473948
<para>
39483949
This variable can be set to the values <literal>default</literal>,
3949-
<literal>verbose</literal>, or <literal>terse</literal> to control the verbosity
3950-
of error reports.
3950+
<literal>verbose</literal>, <literal>terse</literal>,
3951+
or <literal>sqlstate</literal> to control the verbosity of error
3952+
reports.
39513953
(See also <command>\errverbose</command>, for use when you want a verbose
39523954
version of the error you just got.)
39533955
</para>

‎src/bin/psql/help.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ helpVariables(unsigned short int pager)
340340
* Windows builds currently print one more line than non-Windows builds.
341341
* Using the larger number is fine.
342342
*/
343-
output=PageOutput(156,pager ?&(pset.popt.topt) :NULL);
343+
output=PageOutput(158,pager ?&(pset.popt.topt) :NULL);
344344

345345
fprintf(output,_("List of specially treated variables\n\n"));
346346

@@ -414,7 +414,7 @@ helpVariables(unsigned short int pager)
414414
fprintf(output,_(" USER\n"
415415
" the currently connected database user\n"));
416416
fprintf(output,_(" VERBOSITY\n"
417-
" controls verbosity of error reports [default, verbose, terse]\n"));
417+
" controls verbosity of error reports [default, verbose, terse, sqlstate]\n"));
418418
fprintf(output,_(" VERSION\n"
419419
" VERSION_NAME\n"
420420
" VERSION_NUM\n"

‎src/bin/psql/startup.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,13 +1110,15 @@ verbosity_hook(const char *newval)
11101110
Assert(newval!=NULL);/* else substitute hook messed up */
11111111
if (pg_strcasecmp(newval,"default")==0)
11121112
pset.verbosity=PQERRORS_DEFAULT;
1113-
elseif (pg_strcasecmp(newval,"terse")==0)
1114-
pset.verbosity=PQERRORS_TERSE;
11151113
elseif (pg_strcasecmp(newval,"verbose")==0)
11161114
pset.verbosity=PQERRORS_VERBOSE;
1115+
elseif (pg_strcasecmp(newval,"terse")==0)
1116+
pset.verbosity=PQERRORS_TERSE;
1117+
elseif (pg_strcasecmp(newval,"sqlstate")==0)
1118+
pset.verbosity=PQERRORS_SQLSTATE;
11171119
else
11181120
{
1119-
PsqlVarEnumError("VERBOSITY",newval,"default, terse,verbose");
1121+
PsqlVarEnumError("VERBOSITY",newval,"default,verbose,terse,sqlstate");
11201122
return false;
11211123
}
11221124

‎src/bin/psql/tab-complete.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3652,7 +3652,7 @@ psql_completion(const char *text, int start, int end)
36523652
elseif (TailMatchesCS("SHOW_CONTEXT"))
36533653
COMPLETE_WITH_CS("never","errors","always");
36543654
elseif (TailMatchesCS("VERBOSITY"))
3655-
COMPLETE_WITH_CS("default","verbose","terse");
3655+
COMPLETE_WITH_CS("default","verbose","terse","sqlstate");
36563656
}
36573657
elseif (TailMatchesCS("\\sf*"))
36583658
COMPLETE_WITH_SCHEMA_QUERY(Query_for_list_of_routines,NULL);

‎src/interfaces/libpq/fe-protocol3.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,24 @@ pqBuildErrorMessage3(PQExpBuffer msg, const PGresult *res,
10171017
val=PQresultErrorField(res,PG_DIAG_SEVERITY);
10181018
if (val)
10191019
appendPQExpBuffer(msg,"%s: ",val);
1020+
1021+
if (verbosity==PQERRORS_SQLSTATE)
1022+
{
1023+
/*
1024+
* If we have a SQLSTATE, print that and nothing else. If not (which
1025+
* shouldn't happen for server-generated errors, but might possibly
1026+
* happen for libpq-generated ones), fall back to TERSE format, as
1027+
* that seems better than printing nothing at all.
1028+
*/
1029+
val=PQresultErrorField(res,PG_DIAG_SQLSTATE);
1030+
if (val)
1031+
{
1032+
appendPQExpBuffer(msg,"%s\n",val);
1033+
return;
1034+
}
1035+
verbosity=PQERRORS_TERSE;
1036+
}
1037+
10201038
if (verbosity==PQERRORS_VERBOSE)
10211039
{
10221040
val=PQresultErrorField(res,PG_DIAG_SQLSTATE);

‎src/interfaces/libpq/libpq-fe.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ typedef enum
112112
{
113113
PQERRORS_TERSE,/* single-line error messages */
114114
PQERRORS_DEFAULT,/* recommended style */
115-
PQERRORS_VERBOSE/* all the facts, ma'am */
115+
PQERRORS_VERBOSE,/* all the facts, ma'am */
116+
PQERRORS_SQLSTATE/* only error severity and SQLSTATE code */
116117
}PGVerbosity;
117118

118119
typedefenum

‎src/test/regress/expected/psql.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4491,6 +4491,26 @@ number of rows: 0
44914491
last error message: table "this_table_does_not_exist" does not exist
44924492
\echo 'last error code:' :LAST_ERROR_SQLSTATE
44934493
last error code: 42P01
4494+
-- nondefault verbosity error settings (except verbose, which is too unstable)
4495+
\set VERBOSITY terse
4496+
SELECT 1 UNION;
4497+
ERROR: syntax error at or near ";" at character 15
4498+
\echo 'error:' :ERROR
4499+
error: true
4500+
\echo 'error code:' :SQLSTATE
4501+
error code: 42601
4502+
\echo 'last error message:' :LAST_ERROR_MESSAGE
4503+
last error message: syntax error at or near ";"
4504+
\set VERBOSITY sqlstate
4505+
SELECT 1/0;
4506+
ERROR: 22012
4507+
\echo 'error:' :ERROR
4508+
error: true
4509+
\echo 'error code:' :SQLSTATE
4510+
error code: 22012
4511+
\echo 'last error message:' :LAST_ERROR_MESSAGE
4512+
last error message: division by zero
4513+
\set VERBOSITY default
44944514
-- working \gdesc
44954515
SELECT 3 AS three, 4 AS four \gdesc
44964516
Column | Type

‎src/test/regress/sql/psql.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,21 @@ DROP TABLE this_table_does_not_exist;
10011001
\echo'last error message:' :LAST_ERROR_MESSAGE
10021002
\echo'last error code:' :LAST_ERROR_SQLSTATE
10031003

1004+
-- nondefault verbosity error settings (except verbose, which is too unstable)
1005+
\set VERBOSITY terse
1006+
SELECT1UNION;
1007+
\echo'error:' :ERROR
1008+
\echo'error code:' :SQLSTATE
1009+
\echo'last error message:' :LAST_ERROR_MESSAGE
1010+
1011+
\set VERBOSITY sqlstate
1012+
SELECT1/0;
1013+
\echo'error:' :ERROR
1014+
\echo'error code:' :SQLSTATE
1015+
\echo'last error message:' :LAST_ERROR_MESSAGE
1016+
1017+
\set VERBOSITY default
1018+
10041019
-- working \gdesc
10051020
SELECT3AS three,4AS four \gdesc
10061021
\echo'error:' :ERROR

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp