Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d13e903

Browse files
author
Neil Conway
committed
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.
1 parent e6e47f2 commit d13e903

File tree

25 files changed

+479
-38
lines changed

25 files changed

+479
-38
lines changed

doc/src/sgml/ref/close.sgml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/close.sgml,v 1.24 2006/09/16 00:30:17 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/close.sgml,v 1.25 2007/04/12 06:53:45 neilc Exp $
33
PostgreSQL documentation
44
-->
55

@@ -25,7 +25,7 @@ PostgreSQL documentation
2525

2626
<refsynopsisdiv>
2727
<synopsis>
28-
CLOSE <replaceable class="PARAMETER">name</replaceable>
28+
CLOSE { <replaceable class="PARAMETER">name</replaceable> | ALL }
2929
</synopsis>
3030
</refsynopsisdiv>
3131

@@ -44,10 +44,10 @@ CLOSE <replaceable class="PARAMETER">name</replaceable>
4444
transaction is terminated by <command>COMMIT</command> or
4545
<command>ROLLBACK</command>. A holdable cursor is implicitly
4646
closed if the transaction that created it aborts via
47-
<command>ROLLBACK</command>. If the creating transaction successfully
48-
commits, the holdable
49-
cursor remains open until an explicit <command>CLOSE</command> is
50-
executed, or the client disconnects.
47+
<command>ROLLBACK</command>. If the creating transaction
48+
successfully commits, the holdable cursor remains open until an
49+
explicit <command>CLOSE</command> is executed, or the client
50+
disconnects.
5151
</para>
5252
</refsect1>
5353

@@ -63,6 +63,16 @@ CLOSE <replaceable class="PARAMETER">name</replaceable>
6363
</para>
6464
</listitem>
6565
</varlistentry>
66+
67+
<varlistentry>
68+
<term><literal>ALL</literal></term>
69+
<listitem>
70+
<para>
71+
Close all open cursors.
72+
</para>
73+
</listitem>
74+
</varlistentry>
75+
6676
</variablelist>
6777
</refsect1>
6878

@@ -98,7 +108,9 @@ CLOSE liahona;
98108
<title>Compatibility</title>
99109

100110
<para>
101-
<command>CLOSE</command> is fully conforming with the SQL standard.
111+
<command>CLOSE</command> is fully conforming with the SQL
112+
standard. <command>CLOSE ALL</> is a <productname>PostgreSQL</>
113+
extension.
102114
</para>
103115
</refsect1>
104116

doc/src/sgml/ref/deallocate.sgml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/deallocate.sgml,v 1.9 2006/09/16 00:30:18 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/deallocate.sgml,v 1.10 2007/04/12 06:53:46 neilc Exp $
33
PostgreSQL documentation
44
-->
55

@@ -25,7 +25,7 @@ PostgreSQL documentation
2525

2626
<refsynopsisdiv>
2727
<synopsis>
28-
DEALLOCATE [ PREPARE ] <replaceable class="parameter">name</replaceable>
28+
DEALLOCATE [ PREPARE ] { <replaceable class="parameter">name</replaceable> | ALL }
2929
</synopsis>
3030
</refsynopsisdiv>
3131

@@ -65,6 +65,15 @@ DEALLOCATE [ PREPARE ] <replaceable class="parameter">name</replaceable>
6565
</para>
6666
</listitem>
6767
</varlistentry>
68+
69+
<varlistentry>
70+
<term><literal>ALL</literal></term>
71+
<listitem>
72+
<para>
73+
Deallocate all prepared statements.
74+
</para>
75+
</listitem>
76+
</varlistentry>
6877
</variablelist>
6978
</refsect1>
7079

doc/src/sgml/ref/reset.sgml

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/reset.sgml,v 1.32 2006/09/16 00:30:19 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/reset.sgml,v 1.33 2007/04/12 06:53:46 neilc Exp $
33
PostgreSQL documentation
44
-->
55

@@ -22,9 +22,10 @@ PostgreSQL documentation
2222
<synopsis>
2323
RESET <replaceable class="PARAMETER">configuration_parameter</replaceable>
2424
RESET ALL
25+
RESET { PLANS | SESSION | TEMP | TEMPORARY }
2526
</synopsis>
2627
</refsynopsisdiv>
27-
28+
2829
<refsect1>
2930
<title>Description</title>
3031

@@ -41,7 +42,7 @@ SET <replaceable class="parameter">configuration_parameter</replaceable> TO DEFA
4142

4243
<para>
4344
The default value is defined as the value that the parameter would
44-
have had, had no <command>SET</> ever been issued for it in the
45+
have had, if no <command>SET</> ever been issued for it in the
4546
current session. The actual source of this value might be a
4647
compiled-in default, the configuration file, command-line options,
4748
or per-database or per-user default settings. See <xref
@@ -52,6 +53,15 @@ SET <replaceable class="parameter">configuration_parameter</replaceable> TO DEFA
5253
See the <command>SET</> reference page for details on the
5354
transaction behavior of <command>RESET</>.
5455
</para>
56+
57+
<para>
58+
<command>RESET</> can also be used to release internal resources
59+
that are usually released at the end of session. <command>RESET
60+
TEMP</> drops all temporary tables created in the current session.
61+
<command>RESET PLANS</> releases all internally cached plans.
62+
<command>RESET SESSION</> releases all externally visible temporary
63+
resources associated with the current session.
64+
</para>
5565
</refsect1>
5666

5767
<refsect1>
@@ -76,9 +86,56 @@ SET <replaceable class="parameter">configuration_parameter</replaceable> TO DEFA
7686
</para>
7787
</listitem>
7888
</varlistentry>
89+
90+
<varlistentry>
91+
<term><literal>TEMP, TEMPORARY</literal></term>
92+
<listitem>
93+
<para>
94+
Drops all temporary tables created in the current session.
95+
</para>
96+
</listitem>
97+
</varlistentry>
98+
99+
<varlistentry>
100+
<term><literal>PLANS</literal></term>
101+
<listitem>
102+
<para>
103+
Releases all cached query plans.
104+
</para>
105+
</listitem>
106+
</varlistentry>
107+
108+
<varlistentry>
109+
<term><literal>SESSION</literal></term>
110+
<listitem>
111+
<para>
112+
Releases all temporary resources associated with the current
113+
session. This has the same effect as executing the following
114+
command sequence:
115+
<synopsis>
116+
SET SESSION AUTHORIZATION DEFAULT;
117+
RESET ALL;
118+
DEALLOCATE ALL;
119+
CLOSE ALL;
120+
UNLISTEN *;
121+
RESET PLANS;
122+
RESET TEMP;
123+
</synopsis>
124+
</para>
125+
</listitem>
126+
</varlistentry>
127+
79128
</variablelist>
80129
</refsect1>
81130

131+
<refsect1>
132+
<title>Notes</title>
133+
134+
<para>
135+
<command>RESET SESSION</> cannot be executed inside a transaction block.
136+
</para>
137+
</refsect1>
138+
82139
<refsect1>
83140
<title>Examples</title>
84141

src/backend/catalog/namespace.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
1515
* IDENTIFICATION
16-
* $PostgreSQL: pgsql/src/backend/catalog/namespace.c,v 1.93 2007/03/23 19:53:51 tgl Exp $
16+
* $PostgreSQL: pgsql/src/backend/catalog/namespace.c,v 1.94 2007/04/12 06:53:46 neilc Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -1940,6 +1940,26 @@ InitTempTableNamespace(void)
19401940
baseSearchPathValid = false; /* need to rebuild list */
19411941
}
19421942

1943+
/*
1944+
* Remove all temp tables from the temporary namespace.
1945+
*/
1946+
void
1947+
ResetTempTableNamespace(void)
1948+
{
1949+
char namespaceName[NAMEDATALEN];
1950+
Oid namespaceId;
1951+
1952+
/* find oid */
1953+
snprintf(namespaceName, sizeof(namespaceName), "pg_temp_%d", MyBackendId);
1954+
namespaceId = GetSysCacheOid(NAMESPACENAME,
1955+
CStringGetDatum(namespaceName),
1956+
0, 0, 0);
1957+
1958+
/* clean if exists */
1959+
if (OidIsValid(namespaceId))
1960+
RemoveTempRelations(namespaceId);
1961+
}
1962+
19431963
/*
19441964
* End-of-transaction cleanup for namespaces.
19451965
*/

src/backend/commands/async.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/commands/async.c,v 1.135 2007/01/05 22:19:25 momjian Exp $
10+
* $PostgreSQL: pgsql/src/backend/commands/async.c,v 1.136 2007/04/12 06:53:46 neilc Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -127,7 +127,6 @@ static bool unlistenExitRegistered = false;
127127
bool Trace_notify = false;
128128

129129

130-
static void Async_UnlistenAll(void);
131130
static void Async_UnlistenOnExit(int code, Datum arg);
132131
static void ProcessIncomingNotify(void);
133132
static void NotifyMyFrontEnd(char *relname, int32 listenerPID);
@@ -335,7 +334,7 @@ Async_Unlisten(const char *relname)
335334
*
336335
*--------------------------------------------------------------
337336
*/
338-
static void
337+
void
339338
Async_UnlistenAll(void)
340339
{
341340
Relation lRel;

src/backend/commands/portalcmds.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $PostgreSQL: pgsql/src/backend/commands/portalcmds.c,v 1.62 2007/03/13 00:33:39 tgl Exp $
17+
* $PostgreSQL: pgsql/src/backend/commands/portalcmds.c,v 1.63 2007/04/12 06:53:46 neilc Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -236,11 +236,18 @@ PerformPortalClose(const char *name)
236236
{
237237
Portal portal;
238238

239+
/* NULL means CLOSE ALL */
240+
if (name == NULL)
241+
{
242+
PortalHashTableDeleteAll();
243+
return;
244+
}
245+
239246
/*
240247
* Disallow empty-string cursor name (conflicts with protocol-level
241248
* unnamed portal).
242249
*/
243-
if (!name || name[0] == '\0')
250+
if (name[0] == '\0')
244251
ereport(ERROR,
245252
(errcode(ERRCODE_INVALID_CURSOR_NAME),
246253
errmsg("invalid cursor name: must not be empty")));

src/backend/commands/prepare.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Copyright (c) 2002-2007, PostgreSQL Global Development Group
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.70 2007/03/13 00:33:39 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.71 2007/04/12 06:53:46 neilc Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -575,7 +575,10 @@ FetchPreparedStatementTargetList(PreparedStatement *stmt)
575575
void
576576
DeallocateQuery(DeallocateStmt *stmt)
577577
{
578-
DropPreparedStatement(stmt->name, true);
578+
if (stmt->name)
579+
DropPreparedStatement(stmt->name, true);
580+
else
581+
DropAllPreparedStatements();
579582
}
580583

581584
/*
@@ -601,6 +604,31 @@ DropPreparedStatement(const char *stmt_name, bool showError)
601604
}
602605
}
603606

607+
/*
608+
* Drop all cached statements.
609+
*/
610+
void
611+
DropAllPreparedStatements(void)
612+
{
613+
HASH_SEQ_STATUS seq;
614+
PreparedStatement *entry;
615+
616+
/* nothing cached */
617+
if (!prepared_queries)
618+
return;
619+
620+
/* walk over cache */
621+
hash_seq_init(&seq, prepared_queries);
622+
while ((entry = hash_seq_search(&seq)) != NULL)
623+
{
624+
/* Release the plancache entry */
625+
DropCachedPlan(entry->plansource);
626+
627+
/* Now we can remove the hash table entry */
628+
hash_search(prepared_queries, entry->stmt_name, HASH_REMOVE, NULL);
629+
}
630+
}
631+
604632
/*
605633
* Implements the 'EXPLAIN EXECUTE' utility statement.
606634
*/

src/backend/parser/gram.y

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.587 2007/04/08 00:26:34 momjian Exp $
14+
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.588 2007/04/12 06:53:46 neilc Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -1667,6 +1667,12 @@ ClosePortalStmt:
16671667
n->portalname = $2;
16681668
$$ = (Node *)n;
16691669
}
1670+
| CLOSE ALL
1671+
{
1672+
ClosePortalStmt *n = makeNode(ClosePortalStmt);
1673+
n->portalname = NULL;
1674+
$$ = (Node *)n;
1675+
}
16701676
;
16711677

16721678

@@ -5591,6 +5597,18 @@ DeallocateStmt: DEALLOCATE name
55915597
n->name = $3;
55925598
$$ = (Node *) n;
55935599
}
5600+
| DEALLOCATE ALL
5601+
{
5602+
DeallocateStmt *n = makeNode(DeallocateStmt);
5603+
n->name = NULL;
5604+
$$ = (Node *) n;
5605+
}
5606+
| DEALLOCATE PREPARE ALL
5607+
{
5608+
DeallocateStmt *n = makeNode(DeallocateStmt);
5609+
n->name = NULL;
5610+
$$ = (Node *) n;
5611+
}
55945612
;
55955613

55965614
/*****************************************************************************

0 commit comments

Comments
 (0)