diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 42af768045..d06a2e6d1a 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -2257,13 +2257,20 @@ exec_execute_message(const char *portal_name, long max_rows)
 	if (max_rows <= 0)
 		max_rows = FETCH_ALL;
 
-	completed = PortalRun(portal,
-						  max_rows,
-						  true, /* always top level */
-						  !execute_is_fetch && max_rows == FETCH_ALL,
-						  receiver,
-						  receiver,
-						  &qc);
+	if (portal->atEnd)
+	{
+		CopyQueryCompletion(&qc, &portal->qc);
+		qc.nprocessed = 0;
+		completed = true;
+	}
+	else
+		completed = PortalRun(portal,
+							  max_rows,
+							  true, /* always top level */
+							  !execute_is_fetch && max_rows == FETCH_ALL,
+							  receiver,
+							  receiver,
+							  &qc);
 
 	receiver->rDestroy(receiver);
 
