]> granicus.if.org Git - postgresql/commitdiff
Again report a useful error message when walreceiver's connection closes.
authorAndres Freund <andres@anarazel.de>
Thu, 8 Jun 2017 21:42:18 +0000 (14:42 -0700)
committerAndres Freund <andres@anarazel.de>
Thu, 8 Jun 2017 21:51:43 +0000 (14:51 -0700)
Since 7c4f52409a8c (merged in v10), a shutdown master is reported as
  FATAL:  unexpected result after CommandComplete: server closed the connection unexpectedly
by walsender. It used to be
  LOG:  replication terminated by primary server
  FATAL:  could not send end-of-streaming message to primary: no COPY in progress
while the old message clearly is not perfect, it's definitely better
than what's reported now.

The change comes from the attempt to handle finished COPYs without
erroring out, needed for the new logical replication, which wasn't
needed before.

There's probably better ways to handle this, but for now just
explicitly check for a closed connection.

Author: Petr Jelinek
Reviewed-By: Andres Freund
Discussion: https://postgr.es/m/f7c7dd08-855c-e4ed-41f4-d064a6c0665a@2ndquadrant.com
Backpatch: -

src/backend/replication/libpqwalreceiver/libpqwalreceiver.c

index 89c34b822521b266eb6757587c32d876e7f30ba3..7509b4fe60a60ec808a025b644f2cdff334556b0 100644 (file)
@@ -682,12 +682,25 @@ libpqrcv_receive(WalReceiverConn *conn, char **buffer,
                {
                        PQclear(res);
 
-                       /* Verify that there are no more results */
+                       /* Verify that there are no more results. */
                        res = PQgetResult(conn->streamConn);
                        if (res != NULL)
+                       {
+                               PQclear(res);
+
+                               /*
+                                * If the other side closed the connection orderly (otherwise
+                                * we'd seen an error, or PGRES_COPY_IN) don't report an error
+                                * here, but let callers deal with it.
+                                */
+                               if (PQstatus(conn->streamConn) == CONNECTION_BAD)
+                                       return -1;
+
                                ereport(ERROR,
                                                (errmsg("unexpected result after CommandComplete: %s",
                                                                PQerrorMessage(conn->streamConn))));
+                       }
+
                        return -1;
                }
                else if (PQresultStatus(res) == PGRES_COPY_IN)