Index: doc/src/sgml/ref/deallocate.sgml
===================================================================
RCS file: /projects/cvsroot/pgsql/doc/src/sgml/ref/deallocate.sgml,v
retrieving revision 1.9
diff -p -u -c -r1.9 deallocate.sgml
*** doc/src/sgml/ref/deallocate.sgml	16 Sep 2006 00:30:18 -0000	1.9
--- doc/src/sgml/ref/deallocate.sgml	27 Mar 2007 13:55:10 -0000
*************** PostgreSQL documentation
*** 26,31 ****
--- 26,32 ----
   <refsynopsisdiv>
  <synopsis>
  DEALLOCATE [ PREPARE ] <replaceable class="parameter">name</replaceable>
+ DEALLOCATE ALL
  </synopsis>
   </refsynopsisdiv>
  
*************** DEALLOCATE [ PREPARE ] <replaceable clas
*** 39,44 ****
--- 40,50 ----
    </para>
  
    <para>
+    <command>DEALLOCATE ALL</command> deallocates all prepares SQL
+    statements for current session.
+   </para>
+ 
+   <para>
     For more information on prepared statements, see <xref
     linkend="sql-prepare" endterm="sql-prepare-title">.
    </para>
*************** DEALLOCATE [ PREPARE ] <replaceable clas
*** 65,70 ****
--- 71,85 ----
       </para>
      </listitem>
     </varlistentry>
+ 
+    <varlistentry>
+     <term><literal>ALL</literal></term>
+     <listitem>
+      <para>
+       Deallocates all prepares statements in current session.
+      </para>
+     </listitem>
+    </varlistentry>
    </variablelist>
   </refsect1>
  
Index: src/include/nodes/parsenodes.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/nodes/parsenodes.h,v
retrieving revision 1.343
diff -p -u -c -r1.343 parsenodes.h
*** src/include/nodes/parsenodes.h	19 Mar 2007 23:38:32 -0000	1.343
--- src/include/nodes/parsenodes.h	27 Mar 2007 13:55:11 -0000
*************** typedef struct DeallocateStmt
*** 1970,1975 ****
--- 1970,1976 ----
  {
  	NodeTag		type;
  	char	   *name;			/* The name of the plan to remove */
+ 								/* Or NULL for DEALLOCATE ALL */
  } DeallocateStmt;
  
  /*
Index: src/backend/parser/gram.y
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/parser/gram.y,v
retrieving revision 2.584
diff -p -u -c -r2.584 gram.y
*** src/backend/parser/gram.y	26 Mar 2007 16:58:39 -0000	2.584
--- src/backend/parser/gram.y	27 Mar 2007 13:55:11 -0000
*************** execute_param_clause: '(' expr_list ')'	
*** 5548,5554 ****
  /*****************************************************************************
   *
   *		QUERY:
!  *				DEALLOCATE [PREPARE] <plan_name>
   *
   *****************************************************************************/
  
--- 5548,5554 ----
  /*****************************************************************************
   *
   *		QUERY:
!  *				DEALLOCATE [PREPARE] <plan_name>|ALL
   *
   *****************************************************************************/
  
*************** DeallocateStmt: DEALLOCATE name
*** 5564,5569 ****
--- 5564,5581 ----
  						n->name = $3;
  						$$ = (Node *) n;
  					}
+ 				| DEALLOCATE ALL
+ 					{
+ 						DeallocateStmt *n = makeNode(DeallocateStmt);
+ 						n->name = NULL;
+ 						$$ = (Node *) n;
+ 					}
+ 				| DEALLOCATE PREPARE ALL
+ 					{
+ 						DeallocateStmt *n = makeNode(DeallocateStmt);
+ 						n->name = NULL;
+ 						$$ = (Node *) n;
+ 					}
  		;
  
  /*****************************************************************************
Index: src/backend/commands/prepare.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/commands/prepare.c,v
retrieving revision 1.70
diff -p -u -c -r1.70 prepare.c
*** src/backend/commands/prepare.c	13 Mar 2007 00:33:39 -0000	1.70
--- src/backend/commands/prepare.c	27 Mar 2007 13:55:11 -0000
*************** static void InitQueryHashTable(void);
*** 46,51 ****
--- 46,52 ----
  static ParamListInfo EvaluateParams(PreparedStatement *pstmt, List *params,
  									const char *queryString, EState *estate);
  static Datum build_regtype_array(Oid *param_types, int num_params);
+ static void DropAllPreparedStatements(void);
  
  /*
   * Implements the 'PREPARE' utility statement.
*************** FetchPreparedStatementTargetList(Prepare
*** 575,581 ****
  void
  DeallocateQuery(DeallocateStmt *stmt)
  {
! 	DropPreparedStatement(stmt->name, true);
  }
  
  /*
--- 576,586 ----
  void
  DeallocateQuery(DeallocateStmt *stmt)
  {
! 	/* NULL means DEALLOCATE ALL */
! 	if (stmt->name == NULL)
! 		DropAllPreparedStatements();
! 	else
! 		DropPreparedStatement(stmt->name, true);
  }
  
  /*
*************** DropPreparedStatement(const char *stmt_n
*** 602,607 ****
--- 607,637 ----
  }
  
  /*
+  * Drop all cached statements.
+  */
+ static void
+ DropAllPreparedStatements(void)
+ {
+ 	HASH_SEQ_STATUS seq;
+ 	PreparedStatement *entry;
+ 
+ 	/* nothing cached */
+ 	if (!prepared_queries)
+ 		return;
+ 
+ 	/* walk over cache */
+ 	hash_seq_init(&seq, prepared_queries);
+ 	while ((entry = hash_seq_search(&seq)) != NULL)
+ 	{
+ 		/* Release the plancache entry */
+ 		DropCachedPlan(entry->plansource);
+ 
+ 		/* Now we can remove the hash table entry */
+ 		hash_search(prepared_queries, entry->stmt_name, HASH_REMOVE, NULL);
+ 	}
+ }
+ 
+ /*
   * Implements the 'EXPLAIN EXECUTE' utility statement.
   */
  void
Index: src/test/regress/sql/prepare.sql
===================================================================
RCS file: /projects/cvsroot/pgsql/src/test/regress/sql/prepare.sql,v
retrieving revision 1.4
diff -p -u -c -r1.4 prepare.sql
*** src/test/regress/sql/prepare.sql	15 Jan 2006 22:18:47 -0000	1.4
--- src/test/regress/sql/prepare.sql	27 Mar 2007 13:55:11 -0000
*************** PREPARE q7(unknown) AS
*** 68,70 ****
--- 68,76 ----
  
  SELECT name, statement, parameter_types FROM pg_prepared_statements
      ORDER BY name;
+ 
+ -- test DEALLOCATE ALL
+ DEALLOCATE ALL;
+ SELECT name, statement, parameter_types FROM pg_prepared_statements
+     ORDER BY name;
+ 
Index: src/test/regress/expected/prepare.out
===================================================================
RCS file: /projects/cvsroot/pgsql/src/test/regress/expected/prepare.out,v
retrieving revision 1.12
diff -p -u -c -r1.12 prepare.out
*** src/test/regress/expected/prepare.out	21 Dec 2006 16:05:16 -0000	1.12
--- src/test/regress/expected/prepare.out	27 Mar 2007 13:55:11 -0000
*************** SELECT name, statement, parameter_types 
*** 166,168 ****
--- 166,176 ----
        :     SELECT * FROM road WHERE thepath = $1;                         
  (5 rows)
  
+ -- test DEALLOCATE ALL
+ DEALLOCATE ALL;
+ SELECT name, statement, parameter_types FROM pg_prepared_statements
+     ORDER BY name;
+  name | statement | parameter_types 
+ ------+-----------+-----------------
+ (0 rows)
+ 