[proposal] Add an option for returning SQLSTATE in psql error message

Started by didierover 7 years ago25 messageshackers
Jump to latest
#1didier
did447@gmail.com

Hi,

Currently on error psql is printing Postgres' PQerrorMessage text, but
there's no guarantee these messages are constant between Postgres
versions and it's a pain when using psql for writing regression tests,

Prior
Disallow setting client_min_messages higher than ERROR.

a bad workaround was to discarded error, now you have to do something like
CREATE OR REPLACE FUNCTION catch_error(
query text
)
RETURNS void AS $$
DECLARE
BEGIN
EXECUTE query;
EXCEPTION WHEN OTHERS THEN
RAISE 'Query failed: %', SQLSTATE;
END;
$$LANGUAGE plpgsql;

SELECT catch_error('foo');

What about a new \whatever for setting psql error to either PQerrorMessage or
PQresultErrorField(res, PG_DIAG_SQLSTATE) if available?

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: didier (#1)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

ne 2. 12. 2018 v 15:34 odesílatel didier <did447@gmail.com> napsal:

Hi,

Currently on error psql is printing Postgres' PQerrorMessage text, but
there's no guarantee these messages are constant between Postgres
versions and it's a pain when using psql for writing regression tests,

Prior
Disallow setting client_min_messages higher than ERROR.

a bad workaround was to discarded error, now you have to do something like
CREATE OR REPLACE FUNCTION catch_error(
query text
)
RETURNS void AS $$
DECLARE
BEGIN
EXECUTE query;
EXCEPTION WHEN OTHERS THEN
RAISE 'Query failed: %', SQLSTATE;
END;
$$LANGUAGE plpgsql;

SELECT catch_error('foo');

What about a new \whatever for setting psql error to either PQerrorMessage
or
PQresultErrorField(res, PG_DIAG_SQLSTATE) if available?

It looks weird. Maybe we can define a option where only SQL state will be
displayed.

Regards

Pavel

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: didier (#1)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

didier <did447@gmail.com> writes:

Currently on error psql is printing Postgres' PQerrorMessage text, but
there's no guarantee these messages are constant between Postgres
versions and it's a pain when using psql for writing regression tests,

I don't buy that argument. We use psql's normal display in all the
regular regression tests, and it's not a big maintenance problem.
If the error message changes, that's usually interesting in itself.

Also, if it does change, it's often because you're hitting a different
error-detection test, which more than likely is throwing a different
SQLSTATE anyway.

Lastly, because the SQL spec has been rather miserly in assigning
SQLSTATEs in some areas, there are lots of cases where the same
SQLSTATE is used for several distinct errors; for instance
ERRCODE_UNDEFINED_OBJECT covers a lot of ground. Do you really
want your test cases to be unable to distinguish those?

There might be a use-case for showing only SQLSTATE, but writing
regression tests doesn't seem like a good example.

regards, tom lane

#4Pavel Stehule
pavel.stehule@gmail.com
In reply to: Tom Lane (#3)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

ne 2. 12. 2018 v 16:56 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:

didier <did447@gmail.com> writes:

Currently on error psql is printing Postgres' PQerrorMessage text, but
there's no guarantee these messages are constant between Postgres
versions and it's a pain when using psql for writing regression tests,

I don't buy that argument. We use psql's normal display in all the
regular regression tests, and it's not a big maintenance problem.
If the error message changes, that's usually interesting in itself.

our tests are not against different PostgreSQL releases. When you have a
code, that should to support more PostgreSQL releases, then it is sometimes
difficult.

Show quoted text

Also, if it does change, it's often because you're hitting a different
error-detection test, which more than likely is throwing a different
SQLSTATE anyway.

Lastly, because the SQL spec has been rather miserly in assigning
SQLSTATEs in some areas, there are lots of cases where the same
SQLSTATE is used for several distinct errors; for instance
ERRCODE_UNDEFINED_OBJECT covers a lot of ground. Do you really
want your test cases to be unable to distinguish those?

There might be a use-case for showing only SQLSTATE, but writing
regression tests doesn't seem like a good example.

regards, tom lane

#5didier
did447@gmail.com
In reply to: Pavel Stehule (#4)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

Currently with 10 head
ERROR: could not determine polymorphic type because input has type unknown
With 9 head
ERROR: could not determine polymorphic type because input has type "unknown"

Another option could be: set display error to none and let user's
script do some regular expression on pdsl variable.

Show quoted text

On Sun, Dec 2, 2018 at 5:05 PM Pavel Stehule <pavel.stehule@gmail.com> wrote:

ne 2. 12. 2018 v 16:56 odesílatel Tom Lane <tgl@sss.pgh.pa.us> napsal:

didier <did447@gmail.com> writes:

Currently on error psql is printing Postgres' PQerrorMessage text, but
there's no guarantee these messages are constant between Postgres
versions and it's a pain when using psql for writing regression tests,

I don't buy that argument. We use psql's normal display in all the
regular regression tests, and it's not a big maintenance problem.
If the error message changes, that's usually interesting in itself.

our tests are not against different PostgreSQL releases. When you have a code, that should to support more PostgreSQL releases, then it is sometimes difficult.

Also, if it does change, it's often because you're hitting a different
error-detection test, which more than likely is throwing a different
SQLSTATE anyway.

Lastly, because the SQL spec has been rather miserly in assigning
SQLSTATEs in some areas, there are lots of cases where the same
SQLSTATE is used for several distinct errors; for instance
ERRCODE_UNDEFINED_OBJECT covers a lot of ground. Do you really
want your test cases to be unable to distinguish those?

There might be a use-case for showing only SQLSTATE, but writing
regression tests doesn't seem like a good example.

regards, tom lane

#6Andrew Gierth
andrew@tao11.riddles.org.uk
In reply to: Tom Lane (#3)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

"Tom" == Tom Lane <tgl@sss.pgh.pa.us> writes:

Tom> I don't buy that argument. We use psql's normal display in all the
Tom> regular regression tests, and it's not a big maintenance problem.

The regular regression tests have the advantage that they don't need to
work across pg versions.

It is more of a problem for extensions; I just ran into this myself in
fact, with a test failing because "invalid input syntax for integer" got
changed to "invalid input syntax for type integer".

--
Andrew (irc:RhodiumToad)

#7didier
did447@gmail.com
In reply to: Andrew Gierth (#6)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

Attached a POC adding a new variable ECHO_ERROR
\set ECHO_ERROR text|none|psqlstate

On Mon, Dec 3, 2018 at 2:47 AM Andrew Gierth
<andrew@tao11.riddles.org.uk> wrote:

Show quoted text

"Tom" == Tom Lane <tgl@sss.pgh.pa.us> writes:

Tom> I don't buy that argument. We use psql's normal display in all the
Tom> regular regression tests, and it's not a big maintenance problem.

The regular regression tests have the advantage that they don't need to
work across pg versions.

It is more of a problem for extensions; I just ran into this myself in
fact, with a test failing because "invalid input syntax for integer" got
changed to "invalid input syntax for type integer".

--
Andrew (irc:RhodiumToad)

Attachments:

master_psql_error_output.difftext/x-patch; charset=US-ASCII; name=master_psql_error_output.diffDownload+59-4
#8Andrew Gierth
andrew@tao11.riddles.org.uk
In reply to: didier (#7)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

"didier" == didier <did447@gmail.com> writes:

didier> Attached a POC adding a new variable ECHO_ERROR
didier> \set ECHO_ERROR text|none|psqlstate

I wouldn't have called it that. Possibly another option to the existing
VERBOSITY variable? \set VERBOSITY sqlstate_only or something of that
ilk (it's already not unusual to use \set VERBOSITY terse in regression
tests)

--
Andrew (irc:RhodiumToad)

#9Pavel Stehule
pavel.stehule@gmail.com
In reply to: Andrew Gierth (#8)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

po 3. 12. 2018 v 16:49 odesílatel Andrew Gierth <andrew@tao11.riddles.org.uk>
napsal:

"didier" == didier <did447@gmail.com> writes:

didier> Attached a POC adding a new variable ECHO_ERROR
didier> \set ECHO_ERROR text|none|psqlstate

I wouldn't have called it that. Possibly another option to the existing
VERBOSITY variable? \set VERBOSITY sqlstate_only or something of that
ilk (it's already not unusual to use \set VERBOSITY terse in regression
tests)

It is good idea to look to this option.

Pavel

Show quoted text

--
Andrew (irc:RhodiumToad)

#10didier
did447@gmail.com
In reply to: Andrew Gierth (#8)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

Yep, name is bad, but I'm not sure about VERBOSITY, isn't it
controlling output from the server not the client?
You may want to set both VERBOSITY to 'verbose' and ECHO_ERROR to
'none' then in script do
SELECT .... -- no error output
\if :ERROR
-- do something with LAST_ERROR_MESSAGE

On Mon, Dec 3, 2018 at 4:49 PM Andrew Gierth
<andrew@tao11.riddles.org.uk> wrote:

Show quoted text

"didier" == didier <did447@gmail.com> writes:

didier> Attached a POC adding a new variable ECHO_ERROR
didier> \set ECHO_ERROR text|none|psqlstate

I wouldn't have called it that. Possibly another option to the existing
VERBOSITY variable? \set VERBOSITY sqlstate_only or something of that
ilk (it's already not unusual to use \set VERBOSITY terse in regression
tests)

--
Andrew (irc:RhodiumToad)

#11Tom Lane
tgl@sss.pgh.pa.us
In reply to: didier (#10)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

didier <did447@gmail.com> writes:

Yep, name is bad, but I'm not sure about VERBOSITY, isn't it
controlling output from the server not the client?

No, it's in libpq, so you'd have to touch that not the server.

I agree with Andrew's thought, and would further say that just
"\set VERBOSITY sqlstate" would be a reasonable API. Making this
separate from VERBOSITY is weird.

regards, tom lane

#12didier
did447@gmail.com
In reply to: Tom Lane (#11)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

On Mon, Dec 3, 2018 at 5:51 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

No, it's in libpq, so you'd have to touch that not the server.

libpq, not as bad as the server but nonetheless maybe a bit too much for this.
Is adding value in library contract?

Anyway. attached a POC adding a new value to VERBOSITY (hopefully
nobody is using it).

Attachments:

master_verbosity_sqlstate.difftext/x-patch; charset=US-ASCII; name=master_verbosity_sqlstate.diffDownload+16-4
#13Pavel Stehule
pavel.stehule@gmail.com
In reply to: didier (#12)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

po 3. 12. 2018 v 18:57 odesílatel didier <did447@gmail.com> napsal:

On Mon, Dec 3, 2018 at 5:51 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

No, it's in libpq, so you'd have to touch that not the server.

libpq, not as bad as the server but nonetheless maybe a bit too much for
this.
Is adding value in library contract?

Anyway. attached a POC adding a new value to VERBOSITY (hopefully
nobody is using it).

It can works :). Please, assign it to next commitfest.

Regards

Pavel

#14didier
did447@gmail.com
In reply to: Pavel Stehule (#13)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

On Mon, Dec 3, 2018 at 7:02 PM Pavel Stehule <pavel.stehule@gmail.com> wrote:

It can works :). Please, assign it to next commitfest.

Ok

Attachments:

0001-Add-sqlstate-output-mode-to-VERBOSITY_v1.patchtext/x-patch; charset=US-ASCII; name=0001-Add-sqlstate-output-mode-to-VERBOSITY_v1.patchDownload+85-20
In reply to: didier (#14)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

The following review has been posted through the commitfest application:
make installcheck-world: tested, passed
Implements feature: tested, passed
Spec compliant: not tested
Documentation: tested, passed

This is a handy feature, and the implementation looks good to me.

There might be some nit-picking about the vertical whitespace around
the code in added to fe-protocol3.c, but I'll leave that to the committer.

The new status of this patch is: Ready for Committer

#16Peter Eisentraut
peter_e@gmx.net
In reply to: didier (#14)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

On 04/12/2018 13:18, didier wrote:

diff --git a/src/test/regress/sql/psql.sql b/src/test/regress/sql/psql.sql
index 1bb2a6e16d..64628f29a3 100644
--- a/src/test/regress/sql/psql.sql
+++ b/src/test/regress/sql/psql.sql
@@ -1016,3 +1016,22 @@ select 1/(15-unique2) from tenk1 order by unique2 limit 19;
\echo 'last error code:' :LAST_ERROR_SQLSTATE
\unset FETCH_COUNT
+
+-- verbosity error setting
+\set VERBOSITY terse
+SELECT 1 UNION;
+\echo 'error:' :ERROR
+\echo 'error code:' :SQLSTATE
+\echo 'last error message:' :LAST_ERROR_MESSAGE
+
+\set VERBOSITY sqlstate
+SELECT 1 UNION;
+\echo 'error:' :ERROR
+\echo 'error code:' :SQLSTATE
+\echo 'last error message:' :LAST_ERROR_MESSAGE
+
+\set VERBOSITY default
+SELECT 1 UNION;
+\echo 'error:' :ERROR
+\echo 'error code:' :SQLSTATE
+\echo 'last error message:' :LAST_ERROR_MESSAGE
-- 

Why are you not including a test for \set VERBOSITY verbose?

--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#17Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Eisentraut (#16)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:

Why are you not including a test for \set VERBOSITY verbose?

Stability of the output would be a problem ...

regards, tom lane

#18didier
did447@gmail.com
In reply to: Tom Lane (#17)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

On Sat, Jan 5, 2019 at 6:30 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:

Why are you not including a test for \set VERBOSITY verbose?

Stability of the output would be a problem ...

Yes it could moreover the functionality wasn't tested before.

Should I add one ?

#19Michael Paquier
michael@paquier.xyz
In reply to: didier (#18)
Re: [proposal] Add an option for returning SQLSTATE in psql error message

On Mon, Jan 07, 2019 at 10:44:24PM +0100, didier wrote:

On Sat, Jan 5, 2019 at 6:30 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:

Why are you not including a test for \set VERBOSITY verbose?

Stability of the output would be a problem ...

Yes it could moreover the functionality wasn't tested before.

Should I add one ?

Unpredictible outputs mean more maintenance and more alternate
outputs. I have moved the patch to next CF, still ready for
committer.
--
Michael

#20David Steele
david@pgmasters.net
In reply to: Michael Paquier (#19)
Re: Re: [proposal] Add an option for returning SQLSTATE in psql error message

On 2/4/19 5:54 AM, Michael Paquier wrote:

On Mon, Jan 07, 2019 at 10:44:24PM +0100, didier wrote:

On Sat, Jan 5, 2019 at 6:30 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

Peter Eisentraut <peter.eisentraut@2ndquadrant.com> writes:

Why are you not including a test for \set VERBOSITY verbose?

Stability of the output would be a problem ...

Yes it could moreover the functionality wasn't tested before.

Should I add one ?

Unpredictible outputs mean more maintenance and more alternate
outputs. I have moved the patch to next CF, still ready for
committer.

What do you think, Peter? Is the extra test valuable or will it cause
unpredictable outputs as Tom and Michael predict?

Regards,
--
-David
david@pgmasters.net

#21Tom Lane
tgl@sss.pgh.pa.us
In reply to: David Steele (#20)
#22Daniel Gustafsson
daniel@yesql.se
In reply to: Tom Lane (#21)
#23Peter Eisentraut
peter_e@gmx.net
In reply to: David Steele (#20)
#24Tom Lane
tgl@sss.pgh.pa.us
In reply to: Peter Eisentraut (#23)
#25Tom Lane
tgl@sss.pgh.pa.us
In reply to: didier (#14)