From d13e903beaecd45a3721e4c2a7f9ff842ce94a79 Mon Sep 17 00:00:00 2001 From: Neil Conway Date: Thu, 12 Apr 2007 06:53:49 +0000 Subject: RESET SESSION, plus related new DDL commands. Patch from Marko Kreen, reviewed by Neil Conway. This patch adds the following DDL command variants: RESET SESSION, RESET TEMP, RESET PLANS, CLOSE ALL, and DEALLOCATE ALL. RESET SESSION is intended for use by connection pool software and the like, in order to reset a client session to something close to its initial state. Note that while most of these command variants can be executed inside a transaction block (but are not transaction-aware!), RESET SESSION cannot. While this is inconsistent, it is intended to catch programmer mistakes: RESET SESSION in an open transaction block is probably unintended. --- src/backend/utils/cache/plancache.c | 11 ++++++++++- src/backend/utils/misc/guc.c | 37 +++++++++++++++++++++++++++++++++++-- src/backend/utils/mmgr/portalmem.c | 25 ++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 4 deletions(-) (limited to 'src/backend/utils') diff --git a/src/backend/utils/cache/plancache.c b/src/backend/utils/cache/plancache.c index f02a58e29b1..6165e59c6e6 100644 --- a/src/backend/utils/cache/plancache.c +++ b/src/backend/utils/cache/plancache.c @@ -33,7 +33,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/cache/plancache.c,v 1.5 2007/03/26 00:36:19 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/cache/plancache.c,v 1.6 2007/04/12 06:53:47 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -880,6 +880,15 @@ PlanCacheCallback(Datum arg, Oid relid) } } +/* + * ResetPlanCache: drop all cached plans. + */ +void +ResetPlanCache(void) +{ + PlanCacheCallback((Datum) 0, InvalidOid); +} + /* * ScanQueryForRelids callback function for PlanCacheCallback */ diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index f921c75a60b..385411c0582 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -10,7 +10,7 @@ * Written by Peter Eisentraut . * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.383 2007/03/19 23:38:30 wieck Exp $ + * $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.384 2007/04/12 06:53:47 neilc Exp $ * *-------------------------------------------------------------------- */ @@ -32,6 +32,7 @@ #include "access/xact.h" #include "catalog/namespace.h" #include "commands/async.h" +#include "commands/prepare.h" #include "commands/vacuum.h" #include "commands/variable.h" #include "commands/trigger.h" @@ -61,6 +62,7 @@ #include "utils/memutils.h" #include "utils/pg_locale.h" #include "utils/plancache.h" +#include "utils/portal.h" #include "utils/ps_status.h" #include "utils/tzparser.h" #include "utils/xml.h" @@ -4951,14 +4953,45 @@ GetPGVariableResultDesc(const char *name) return tupdesc; } +/* + * RESET SESSION command. + */ +static void +ResetSession(bool isTopLevel) +{ + /* + * Disallow RESET SESSION in a transaction block. This is arguably + * inconsistent (we don't make a similar check in the command + * sequence that RESET SESSION is equivalent to), but the idea is + * to catch mistakes: RESET SESSION inside a transaction block + * would leave the transaction still uncommitted. + */ + PreventTransactionChain(isTopLevel, "RESET SESSION"); + + SetPGVariable("session_authorization", NIL, false); + ResetAllOptions(); + DropAllPreparedStatements(); + PortalHashTableDeleteAll(); + Async_UnlistenAll(); + ResetPlanCache(); + ResetTempTableNamespace(); +} + /* * RESET command */ void -ResetPGVariable(const char *name) +ResetPGVariable(const char *name, bool isTopLevel) { if (pg_strcasecmp(name, "all") == 0) ResetAllOptions(); + else if (pg_strcasecmp(name, "session") == 0) + ResetSession(isTopLevel); + else if (pg_strcasecmp(name, "temp") == 0 || + pg_strcasecmp(name, "temporary") == 0) + ResetTempTableNamespace(); + else if (pg_strcasecmp(name, "plans") == 0) + ResetPlanCache(); else set_config_option(name, NULL, diff --git a/src/backend/utils/mmgr/portalmem.c b/src/backend/utils/mmgr/portalmem.c index 043ea1e57a4..eeba207dc94 100644 --- a/src/backend/utils/mmgr/portalmem.c +++ b/src/backend/utils/mmgr/portalmem.c @@ -12,7 +12,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.100 2007/03/13 00:33:42 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.101 2007/04/12 06:53:48 neilc Exp $ * *------------------------------------------------------------------------- */ @@ -452,6 +452,29 @@ PortalDrop(Portal portal, bool isTopCommit) pfree(portal); } +/* + * Delete all declared cursors. + * + * Used by 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; + if (portal->status != PORTAL_ACTIVE) + PortalDrop(portal, false); + } +} + /* * Pre-commit processing for portals. -- cgit v1.2.3