Re: psql COPY vs. ON_ERROR_ROLLBACK, multi-command strings

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Noah Misch <noah(at)leadboat(dot)com>
Cc: Alvaro Herrera <alvherre(at)commandprompt(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: psql COPY vs. ON_ERROR_ROLLBACK, multi-command strings
Date: 2012-03-07 21:57:12
Message-ID: CA+TgmoZucAMSNog9XAzeiedEXJpfr==h5thBy1Ve_aktWZNtVQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, Feb 25, 2012 at 12:57 AM, Noah Misch <noah(at)leadboat(dot)com> wrote:
> Thanks.  While testing a crashing function, I noticed that my above patch
> added some noise to psql output when the server crashes:
>
> [local] test=# select crashme();
> The connection to the server was lost. Attempting reset: Failed.
> The connection to the server was lost. Attempting reset: Failed.
> unexpected transaction status (4)
> Time: 6.681 ms
>  !> \q
>
> Status 4 is PQTRANS_UNKNOWN, which is expected when the connection is not
> CONNECTION_OK.  The double message arrives because ProcessResult() now calls
> CheckConnection() at least twice, for the benefit of COPY.  (Incidentally, the
> reconnect fails because the server has not yet finished recovering; that part
> is nothing new.)
>
> The attached small patch has SendQuery() keep quiet about PQTRANS_UNKNOWN when
> the connection is down.  It makes ProcessResult() skip the second
> CheckConnection() when the command string had no COPY results.  This restores
> the pre-08146775acd8bfe0fcc509c71857abb928697171 psql output:
>
> [local] test=# select crashme();
> The connection to the server was lost. Attempting reset: Failed.
> Time: 4.798 ms
>  !> \q

Committed, but... why do we EVER need to call CheckConnection() at the
bottom of ProcessResult(), after exiting that function's main loop? I
don't see any way to get out of that loop without first calling
AcceptResult on every PGresult we've seen, and that function already
calls CheckConnection() if any failure is observed.

As a side note, the documentation for PQexec() is misleading about
what will happen if COPY is present in a multi-command string. It
says: "Note however that the returned PGresult structure describes
only the result of the last command executed from the string. Should
one of the commands fail, processing of the string stops with it and
the returned PGresult describes the error condition. It does not
explain that it also stops if it hits a COPY. I had to read the
source code for libpq to understand why this psql logic was coded the
way it is.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Robert Haas 2012-03-07 21:59:37 Re: NULL's support in SP-GiST
Previous Message Alexander Shulgin 2012-03-07 21:54:06 Re: WIP: URI connection string support for libpq