error handling in pqRowProcessor broken

Peter Eisentraut <peter.eisentraut@enterprisedb.com>

From: Peter Eisentraut <peter.eisentraut@enterprisedb.com>
To: pgsql-hackers <pgsql-hackers@postgresql.org>, Tom Lane <tgl@sss.pgh.pa.us>
Date: 2022-04-19T14:34:36Z
Lists: pgsql-hackers
The error handling for pqRowProcessor is described as

  *    Add the received row to the current async result (conn->result).
  *    Returns 1 if OK, 0 if error occurred.
  *
  * On error, *errmsgp can be set to an error string to be returned.
  * If it is left NULL, the error is presumed to be "out of memory".

I find that this doesn't work anymore.  If you set *errmsgp = "some 
message" and return 0, then psql will just print a result set with zero 
rows.

Bisecting points to

commit 618c16707a6d6e8f5c83ede2092975e4670201ad
Author: Tom Lane <tgl@sss.pgh.pa.us>
Date:   Fri Feb 18 15:35:15 2022 -0500

     Rearrange libpq's error reporting to avoid duplicated error text.

It is very uncommon to get an error from pqRowProcessor().  To 
reproduce, I inserted this code:

diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c
index c7c48d07dc..9c1b33c6e2 100644
--- a/src/interfaces/libpq/fe-exec.c
+++ b/src/interfaces/libpq/fe-exec.c
@@ -1124,6 +1124,12 @@ pqRowProcessor(PGconn *conn, const char **errmsgp)
             return 0;
     }

+   if (nfields == 7)
+   {
+       *errmsgp = "gotcha";
+       goto fail;
+   }
+
     /*
      * Basically we just allocate space in the PGresult for each field and
      * copy the data over.

This will produce assorted failures in the regression tests that 
illustrate the effect.

(Even before the above commit, the handling of the returned message was 
a bit weird:  The error output was just the message string, without any 
prefix like "ERROR:".)



Commits

  1. Fix missed cases in libpq's error handling.

  2. Rearrange libpq's error reporting to avoid duplicated error text.