Index: src/backend/catalog/namespace.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/catalog/namespace.c,v
retrieving revision 1.93
diff -p -u -c -r1.93 namespace.c
*** src/backend/catalog/namespace.c	23 Mar 2007 19:53:51 -0000	1.93
--- src/backend/catalog/namespace.c	30 Mar 2007 12:56:54 -0000
*************** InitTempTableNamespace(void)
*** 1941,1946 ****
--- 1941,1971 ----
  }
  
  /*
+  * Clear all temp tables from out temp namespace.
+  */
+ void
+ ResetTempTableNamespace(void)
+ {
+ 	char		namespaceName[NAMEDATALEN];
+ 	Oid			namespaceId;
+ 
+ 	/* If not allowed to create, no point proceeding */
+ 	if (pg_database_aclcheck(MyDatabaseId, GetUserId(),
+ 							 ACL_CREATE_TEMP) != ACLCHECK_OK)
+ 		return;
+ 
+ 	/* find oid */
+ 	snprintf(namespaceName, sizeof(namespaceName), "pg_temp_%d", MyBackendId);
+ 	namespaceId = GetSysCacheOid(NAMESPACENAME,
+ 								 CStringGetDatum(namespaceName),
+ 								 0, 0, 0);
+ 
+ 	/* clean if exists */
+ 	if (OidIsValid(namespaceId))
+ 		RemoveTempRelations(namespaceId);
+ }
+ 
+ /*
   * End-of-transaction cleanup for namespaces.
   */
  void
Index: src/backend/commands/async.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/commands/async.c,v
retrieving revision 1.135
diff -p -u -c -r1.135 async.c
*** src/backend/commands/async.c	5 Jan 2007 22:19:25 -0000	1.135
--- src/backend/commands/async.c	30 Mar 2007 12:56:54 -0000
*************** static bool unlistenExitRegistered = fal
*** 127,133 ****
  bool		Trace_notify = false;
  
  
- static void Async_UnlistenAll(void);
  static void Async_UnlistenOnExit(int code, Datum arg);
  static void ProcessIncomingNotify(void);
  static void NotifyMyFrontEnd(char *relname, int32 listenerPID);
--- 127,132 ----
*************** Async_Unlisten(const char *relname)
*** 335,341 ****
   *
   *--------------------------------------------------------------
   */
! static void
  Async_UnlistenAll(void)
  {
  	Relation	lRel;
--- 334,340 ----
   *
   *--------------------------------------------------------------
   */
! void
  Async_UnlistenAll(void)
  {
  	Relation	lRel;
Index: src/backend/commands/portalcmds.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/commands/portalcmds.c,v
retrieving revision 1.62
diff -p -u -c -r1.62 portalcmds.c
*** src/backend/commands/portalcmds.c	13 Mar 2007 00:33:39 -0000	1.62
--- src/backend/commands/portalcmds.c	30 Mar 2007 12:56:54 -0000
*************** PerformPortalClose(const char *name)
*** 236,246 ****
  {
  	Portal		portal;
  
  	/*
  	 * Disallow empty-string cursor name (conflicts with protocol-level
  	 * unnamed portal).
  	 */
! 	if (!name || name[0] == '\0')
  		ereport(ERROR,
  				(errcode(ERRCODE_INVALID_CURSOR_NAME),
  				 errmsg("invalid cursor name: must not be empty")));
--- 236,253 ----
  {
  	Portal		portal;
  
+ 	/* NULL means CLOSE ALL */
+ 	if (name == NULL)
+ 	{
+ 		PortalHashTableDeleteAll();
+ 		return;
+ 	}
+ 
  	/*
  	 * Disallow empty-string cursor name (conflicts with protocol-level
  	 * unnamed portal).
  	 */
! 	if (name[0] == '\0')
  		ereport(ERROR,
  				(errcode(ERRCODE_INVALID_CURSOR_NAME),
  				 errmsg("invalid cursor name: must not be empty")));
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	30 Mar 2007 12:56:54 -0000
*************** FetchPreparedStatementTargetList(Prepare
*** 575,581 ****
  void
  DeallocateQuery(DeallocateStmt *stmt)
  {
! 	DropPreparedStatement(stmt->name, true);
  }
  
  /*
--- 575,584 ----
  void
  DeallocateQuery(DeallocateStmt *stmt)
  {
! 	if (stmt->name)
! 		DropPreparedStatement(stmt->name, true);
! 	else
! 		DropAllPreparedStatements();
  }
  
  /*
*************** DropPreparedStatement(const char *stmt_n
*** 602,607 ****
--- 605,635 ----
  }
  
  /*
+  * Drop all cached statements.
+  */
+ 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/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	30 Mar 2007 12:56:55 -0000
*************** ClosePortalStmt:
*** 1666,1671 ****
--- 1666,1677 ----
  					n->portalname = $2;
  					$$ = (Node *)n;
  				}
+ 			| CLOSE ALL
+ 				{
+ 					ClosePortalStmt *n = makeNode(ClosePortalStmt);
+ 					n->portalname = NULL;
+ 					$$ = (Node *)n;
+ 				}
  		;
  
  
*************** DeallocateStmt: DEALLOCATE name
*** 5564,5569 ****
--- 5570,5581 ----
  						n->name = $3;
  						$$ = (Node *) n;
  					}
+ 				| DEALLOCATE ALL
+ 					{
+ 						DeallocateStmt *n = makeNode(DeallocateStmt);
+ 						n->name = NULL;
+ 						$$ = (Node *) n;
+ 					}
  		;
  
  /*****************************************************************************
Index: src/backend/utils/cache/plancache.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/utils/cache/plancache.c,v
retrieving revision 1.5
diff -p -u -c -r1.5 plancache.c
*** src/backend/utils/cache/plancache.c	26 Mar 2007 00:36:19 -0000	1.5
--- src/backend/utils/cache/plancache.c	30 Mar 2007 12:56:55 -0000
*************** PlanCacheCallback(Datum arg, Oid relid)
*** 881,886 ****
--- 881,897 ----
  }
  
  /*
+  * ResetPlanCache: drop all cached plans.
+  *
+  * FIXME: maybe should just walt list unconditionally?
+  */
+ void
+ ResetPlanCache(void)
+ {
+ 	PlanCacheCallback((Datum)0, InvalidOid);
+ }
+ 
+ /*
   * ScanQueryForRelids callback function for PlanCacheCallback
   */
  static void
Index: src/backend/utils/misc/guc.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/utils/misc/guc.c,v
retrieving revision 1.383
diff -p -u -c -r1.383 guc.c
*** src/backend/utils/misc/guc.c	19 Mar 2007 23:38:30 -0000	1.383
--- src/backend/utils/misc/guc.c	30 Mar 2007 12:56:55 -0000
*************** GetPGVariableResultDesc(const char *name
*** 4951,4956 ****
--- 4951,4978 ----
  	return tupdesc;
  }
  
+ static void
+ ResetSession(void)
+ {
+ 	/* ROLLBACK; */
+ 	if (IsTransactionBlock())
+ 		UserAbortTransactionBlock();
+ 	/* DEALLOCATE ALL; */
+ 	DropAllPreparedStatements();
+ 	/* CLOSE ALL */
+ 	PortalHashTableDeleteAll();
+ 	/* RESET ALL; */
+ 	ResetAllOptions();
+ 	/* SET SESSION AUTHORIZATION DEFAULT; */
+ 	SetPGVariable("session_authorization", NIL, false);
+ 	/* UNLISTEN *; */
+ 	Async_UnlistenAll();
+ 	/* RESET TEMPS */
+ 	ResetTempTableNamespace();
+ 	/* RESET PLANS */
+ 	ResetPlanCache();
+ }
+ 
  /*
   * RESET command
   */
*************** ResetPGVariable(const char *name)
*** 4959,4964 ****
--- 4981,4992 ----
  {
  	if (pg_strcasecmp(name, "all") == 0)
  		ResetAllOptions();
+ 	else if (pg_strcasecmp(name, "session") == 0)
+ 		ResetSession();
+ 	else if (pg_strcasecmp(name, "temps") == 0)
+ 		ResetTempTableNamespace();
+ 	else if (pg_strcasecmp(name, "plans") == 0)
+ 		ResetPlanCache();
  	else
  		set_config_option(name,
  						  NULL,
Index: src/backend/utils/mmgr/portalmem.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/utils/mmgr/portalmem.c,v
retrieving revision 1.100
diff -p -u -c -r1.100 portalmem.c
*** src/backend/utils/mmgr/portalmem.c	13 Mar 2007 00:33:42 -0000	1.100
--- src/backend/utils/mmgr/portalmem.c	30 Mar 2007 12:56:55 -0000
*************** PortalDrop(Portal portal, bool isTopComm
*** 452,457 ****
--- 452,482 ----
  	pfree(portal);
  }
  
+ /*
+  * Delete all WITH HOLD cursors
+  *
+  * Commands: CLOSE ALL, RESET SESSION
+  */
+ void
+ PortalHashTableDeleteAll(void)
+ {
+ 	HASH_SEQ_STATUS status;
+ 	PortalHashEnt *hentry;
+ 
+ 	if (PortalHashTable == NULL)
+ 		return;
+ 
+ 	hash_seq_init(&status, PortalHashTable);
+ 	while ((hentry = hash_seq_search(&status)) != NULL)
+ 	{
+ 		Portal portal = hentry->portal;
+ 
+ 		/* Drop only WITH HOLD: portal->cursorOptions & CURSOR_OPT_HOLD ? */
+ 		if (portal->status != PORTAL_ACTIVE)
+ 			PortalDrop(portal, false);
+ 	}
+ }
+ 
  
  /*
   * Pre-commit processing for portals.
Index: src/include/catalog/namespace.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/catalog/namespace.h,v
retrieving revision 1.45
diff -p -u -c -r1.45 namespace.h
*** src/include/catalog/namespace.h	23 Mar 2007 19:53:52 -0000	1.45
--- src/include/catalog/namespace.h	30 Mar 2007 12:56:55 -0000
*************** extern char *namespace_search_path;
*** 100,103 ****
--- 100,105 ----
  
  extern List *fetch_search_path(bool includeImplicit);
  
+ extern void ResetTempTableNamespace(void);
+ 
  #endif   /* NAMESPACE_H */
Index: src/include/commands/async.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/commands/async.h,v
retrieving revision 1.34
diff -p -u -c -r1.34 async.h
*** src/include/commands/async.h	5 Jan 2007 22:19:53 -0000	1.34
--- src/include/commands/async.h	30 Mar 2007 12:56:55 -0000
*************** extern bool Trace_notify;
*** 19,24 ****
--- 19,25 ----
  extern void Async_Notify(const char *relname);
  extern void Async_Listen(const char *relname);
  extern void Async_Unlisten(const char *relname);
+ extern void Async_UnlistenAll(void);
  
  /* perform (or cancel) outbound notify processing at transaction commit */
  extern void AtCommit_Notify(void);
Index: src/include/commands/prepare.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/commands/prepare.h,v
retrieving revision 1.25
diff -p -u -c -r1.25 prepare.h
*** src/include/commands/prepare.h	13 Mar 2007 00:33:43 -0000	1.25
--- src/include/commands/prepare.h	30 Mar 2007 12:56:55 -0000
*************** extern void DropPreparedStatement(const 
*** 59,62 ****
--- 59,64 ----
  extern TupleDesc FetchPreparedStatementResultDesc(PreparedStatement *stmt);
  extern List *FetchPreparedStatementTargetList(PreparedStatement *stmt);
  
+ void DropAllPreparedStatements(void);
+ 
  #endif   /* PREPARE_H */
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	30 Mar 2007 12:56:55 -0000
*************** typedef struct ClosePortalStmt
*** 1451,1456 ****
--- 1451,1457 ----
  {
  	NodeTag		type;
  	char	   *portalname;		/* name of the portal (cursor) */
+ 								/* NULL means CLOSE ALL */
  } ClosePortalStmt;
  
  /* ----------------------
*************** typedef struct DeallocateStmt
*** 1970,1975 ****
--- 1971,1977 ----
  {
  	NodeTag		type;
  	char	   *name;			/* The name of the plan to remove */
+ 								/* NULL means DEALLOCATE ALL */
  } DeallocateStmt;
  
  /*
Index: src/include/utils/plancache.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/utils/plancache.h,v
retrieving revision 1.4
diff -p -u -c -r1.4 plancache.h
*** src/include/utils/plancache.h	23 Mar 2007 19:53:52 -0000	1.4
--- src/include/utils/plancache.h	30 Mar 2007 12:56:56 -0000
*************** extern void ReleaseCachedPlan(CachedPlan
*** 105,108 ****
--- 105,110 ----
  extern TupleDesc PlanCacheComputeResultDesc(List *stmt_list);
  extern bool HaveCachedPlans(void);
  
+ extern void ResetPlanCache(void);
+ 
  #endif   /* PLANCACHE_H */
Index: src/include/utils/portal.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/utils/portal.h,v
retrieving revision 1.74
diff -p -u -c -r1.74 portal.h
*** src/include/utils/portal.h	13 Mar 2007 00:33:43 -0000	1.74
--- src/include/utils/portal.h	30 Mar 2007 12:56:56 -0000
*************** extern void PortalDefineQuery(Portal por
*** 212,216 ****
--- 212,217 ----
  				  CachedPlan *cplan);
  extern Node *PortalListGetPrimaryStmt(List *stmts);
  extern void PortalCreateHoldStore(Portal portal);
+ extern void PortalHashTableDeleteAll(void);
  
  #endif   /* PORTAL_H */
