*** src/backend/commands/prepare.c	1f53747076d3cb8d83832179c2e8a0ee3d8f2d37
--- src/backend/commands/prepare.c	0b2ffacdca58b5a073fe6a57b3aa2c7d61d317a4
*************** ExecuteQuery(ExecuteStmt *stmt, const ch
*** 174,180 ****
  	ParamListInfo paramLI = NULL;
  	EState	   *estate = NULL;
  	Portal		portal;
! 	char	   *query_string;
  
  	/* Look it up in the hash table */
  	entry = FetchPreparedStatement(stmt->name, true);
--- 174,180 ----
  	ParamListInfo paramLI = NULL;
  	EState	   *estate = NULL;
  	Portal		portal;
! 	char	   *prepared_qs;
  
  	/* Look it up in the hash table */
  	entry = FetchPreparedStatement(stmt->name, true);
*************** ExecuteQuery(ExecuteStmt *stmt, const ch
*** 205,212 ****
  	portal->visible = false;
  
  	/* Copy the plan's saved query string into the portal's memory */
! 	query_string = MemoryContextStrdup(PortalGetHeapMemory(portal),
! 									   entry->plansource->query_string);
  
  	/*
  	 * For CREATE TABLE / AS EXECUTE, we must make a copy of the stored query
--- 205,212 ----
  	portal->visible = false;
  
  	/* Copy the plan's saved query string into the portal's memory */
! 	prepared_qs = MemoryContextStrdup(PortalGetHeapMemory(portal),
! 									  entry->plansource->query_string);
  
  	/*
  	 * For CREATE TABLE / AS EXECUTE, we must make a copy of the stored query
*************** ExecuteQuery(ExecuteStmt *stmt, const ch
*** 256,262 ****
  
  	PortalDefineQuery(portal,
  					  NULL,
! 					  query_string,
  					  entry->plansource->commandTag,
  					  plan_list,
  					  cplan);
--- 256,262 ----
  
  	PortalDefineQuery(portal,
  					  NULL,
! 					  prepared_qs,
  					  entry->plansource->commandTag,
  					  plan_list,
  					  cplan);
