diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c
index ea4b586984..038bafe777 100644
--- a/src/backend/executor/execMain.c
+++ b/src/backend/executor/execMain.c
@@ -1683,7 +1683,15 @@ ExecutePlan(EState *estate,
 			 * end the loop.
 			 */
 			if (!dest->receiveSlot(slot, dest))
+			{
+				/*
+				 * If we know we won't need to back up, we can release
+				 * resources at this point.
+				 */
+				if (!(estate->es_top_eflags & EXEC_FLAG_BACKWARD))
+					(void) ExecShutdownNode(planstate);
 				break;
+			}
 		}
 
 		/*
