diff -dcrp pgsql.orig/src/interfaces/ecpg/ecpglib/execute.c pgsql.1/src/interfaces/ecpg/ecpglib/execute.c
*** pgsql.orig/src/interfaces/ecpg/ecpglib/execute.c	2010-01-05 18:02:03.000000000 +0100
--- pgsql.1/src/interfaces/ecpg/ecpglib/execute.c	2010-01-18 11:49:17.000000000 +0100
*************** ECPGdo(const int lineno, const int compa
*** 1753,1759 ****
--- 1753,1762 ----
  			stmt->command = ecpg_strdup(command, lineno);
  		}
  		else
+ 		{
  			ecpg_raise(lineno, ECPG_INVALID_STMT, ECPG_SQLSTATE_INVALID_SQL_STATEMENT_NAME, stmt->command);
+ 			return (false);
+ 		}
  	}
  
  	stmt->connection = con;
diff -dcrp pgsql.orig/src/interfaces/ecpg/ecpglib/prepare.c pgsql.1/src/interfaces/ecpg/ecpglib/prepare.c
*** pgsql.orig/src/interfaces/ecpg/ecpglib/prepare.c	2010-01-15 12:55:24.000000000 +0100
--- pgsql.1/src/interfaces/ecpg/ecpglib/prepare.c	2010-01-18 11:32:26.000000000 +0100
*************** ecpg_auto_prepare(int lineno, const char
*** 465,472 ****
--- 465,481 ----
  	/* if not found - add the statement to the cache	*/
  	if (entNo)
  	{
+ 		struct connection *con;
+ 		struct prepared_statement *prep;
+ 
  		ecpg_log("ecpg_auto_prepare on line %d: statement found in cache; entry %d\n", lineno, entNo);
  		*name = ecpg_strdup(stmtCacheEntries[entNo].stmtID, lineno);
+ 
+ 		con = ecpg_get_connection(connection_name);
+ 		prep = ecpg_find_prepared_statement(*name, con, NULL);
+ 		/* This prepared name doesn't exist on this connection. */
+ 		if (!prep && !ECPGprepare(lineno, connection_name, 0, ecpg_strdup(*name, lineno), query))
+ 			return (false);
  	}
  	else
  	{
