Re: psql - add special variable to reflect the last query status

From: Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: PostgreSQL Developers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: psql - add special variable to reflect the last query status
Date: 2017-05-22 07:40:45
Message-ID: alpine.DEB.2.20.1705220830030.3161@lancre
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


Hello Pavel,

>>> After some discussions about what could be useful since psql scripts now
>>> accepts tests, this patch sets a few variables which can be used by psql
>>> after a "front door" (i.e. actually typed by the user) query:
>>>
>>> - RESULT_STATUS: the status of the query
>>> - ERROR: whether the query failed
>>> - ERROR_MESSAGE: ...
>>> - ROW_COUNT: #rows affected
>>>
>>> SELECT * FROM ;
>>> \if :ERROR
>>> \echo oops
>>> \q
>>> \endif
>>>
>>> I'm not sure that the names are right. Maybe STATUS would be better than
>>> RESULT_STATUS.
>>
> I am sending review of this patch:
>
> 1. I agree so STATUS is better name, than RESULT status.

Ok, looks simpler.

> Currently it returns values with prefix PGRES (like PGRES_FATAL_ERROR,
> PGRES_TUPLES_OK). Maybe we should to cut this prefix. FATAL_ERROR,
> TUPLES_OK looks better for custom level. The PGRES prefix has not sense
> in psql.

Indeed. I skipped "PGRES_".

> 2. I propose availability to read ERROR_CODE - sometimes it can be more
> practical than parsing error possible translated message

Ok.

> 3. The fields ERROR_MESSAGE and ROW_COUNT are set only when it has sense.
> This behave is maybe too strict for psql and the processing needs more
> nesting \if command. What do you think about -1 or 0 for ROW_COUNT (for
> DDL) and "" for ERROR_MESSAGE when there are not any error? It will be
> consistent with already implemented LASTOID variable (and other state psql
> variables). Using default values are not strict clean, but it can reduce
> complexity of psql scripts.

My intention was that it could be tested with the "is defined" syntax,
which is yet to be agreed upon and implemented, so maybe generating empty
string is a better option.

For ROW_COUNT, I think that it should be consistent with what PL/pgSQL
does, so it think that 0 should be the default.

> 4. all regress tests passed
> 5. there are not any problem with doc building

Please find attached a v2 which hopefully takes into account all your
points above.

Open question: should it gather more PQerrorResultField, or the two
selected one are enough? If more, which should be included?

--
Fabien.

Attachment Content-Type Size
psql-result-status-2.patch text/x-diff 6.8 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message amul sul 2017-05-22 08:53:31 Re: [POC] hash partitioning
Previous Message Magnus Hagander 2017-05-22 07:10:55 Re: Fix a typo in hash.c