diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 2bd89102686e..8d3739298afe 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -38,6 +38,7 @@
 #include "commands/async.h"
 #include "commands/event_trigger.h"
 #include "commands/explain_state.h"
+#include "commands/portalcmds.h"
 #include "commands/prepare.h"
 #include "common/pg_prng.h"
 #include "jit/jit.h"
@@ -2330,13 +2331,14 @@ exec_execute_message(const char *portal_name, long max_rows)
 
 			/*
 			 * We completed fetching from an unnamed portal.  There is no need
-			 * for it beyond this point, so drop it now rather than wait for
+			 * for it beyond this point, so clean it now rather than wait for
 			 * the next Bind message to do this cleanup.  This ensures that
 			 * the correct statement is logged when cleaning up temporary file
-			 * usage.
+			 * usage with the executor shutdown.
 			 */
-			if (portal->name[0] == '\0')
-				PortalDrop(portal, false);
+			if (portal->name[0] == '\0' &&
+				portal->cleanup)
+				PortalCleanup(portal);
 		}
 
 		/* Send appropriate CommandComplete to client */
