diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 01b6cc1f7d..7ad11b0a65 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -1530,6 +1530,17 @@ exec_parse_message(const char *query_string,	/* string to execute */
 	}
 	else
 	{
+		/*
+		 * If we are in an aborted transacion, do not create plan cache entry
+		 * for empty input string.
+		 */
+		if (IsAbortedTransactionBlockState())
+			ereport(ERROR,
+					(errcode(ERRCODE_IN_FAILED_SQL_TRANSACTION),
+					 errmsg("current transaction is aborted, "
+							"commands ignored until end of transaction block"),
+					 errdetail_abort()));
+
 		/* Empty input string.  This is legal. */
 		raw_parse_tree = NULL;
 		psrc = CreateCachedPlan(raw_parse_tree, query_string,
