diff --git a/src/bin/pg_basebackup/receivelog.c b/src/bin/pg_basebackup/receivelog.c
index a7a79e824a3912d60b07b2840c17288e1b2afc66..3a46767417eea2bc214396fab1b4da67e4c49b06 100644
--- a/src/bin/pg_basebackup/receivelog.c
+++ b/src/bin/pg_basebackup/receivelog.c
@@ -612,6 +612,7 @@ ReceiveXlogStream(PGconn *conn, XLogRecPtr startpos, uint32 timeline,
 		fprintf(stderr,
 				_("%s: unexpected termination of replication stream: %s"),
 				progname, PQresultErrorMessage(res));
+		PQclear(res);
 		goto error;
 	}
 	PQclear(res);