Commit d90ced8b authored by Robert Haas's avatar Robert Haas

Add DISCARD SEQUENCES command.

DISCARD ALL will now discard cached sequence information, as well.

Fabrízio de Royes Mello, reviewed by Zoltán Böszörményi, with some
further tweaks by me.
parent c64e68fd
......@@ -21,7 +21,7 @@ PostgreSQL documentation
<refsynopsisdiv>
<synopsis>
DISCARD { ALL | PLANS | TEMPORARY | TEMP }
DISCARD { ALL | PLANS | SEQUENCES | TEMPORARY | TEMP }
</synopsis>
</refsynopsisdiv>
......@@ -66,6 +66,15 @@ DISCARD { ALL | PLANS | TEMPORARY | TEMP }
</listitem>
</varlistentry>
<varlistentry>
<term><literal>SEQUENCES</literal></term>
<listitem>
<para>
Discards all cached sequence values.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>ALL</literal></term>
<listitem>
......@@ -83,6 +92,7 @@ UNLISTEN *;
SELECT pg_advisory_unlock_all();
DISCARD PLANS;
DISCARD TEMP;
DISCARD SEQUENCES;
</programlisting></para>
</listitem>
</varlistentry>
......
......@@ -18,13 +18,14 @@
#include "commands/async.h"
#include "commands/discard.h"
#include "commands/prepare.h"
#include "commands/sequence.h"
#include "utils/guc.h"
#include "utils/portal.h"
static void DiscardAll(bool isTopLevel);
/*
* DISCARD { ALL | TEMP | PLANS }
* DISCARD { ALL | SEQUENCES | TEMP | PLANS }
*/
void
DiscardCommand(DiscardStmt *stmt, bool isTopLevel)
......@@ -39,6 +40,10 @@ DiscardCommand(DiscardStmt *stmt, bool isTopLevel)
ResetPlanCache();
break;
case DISCARD_SEQUENCES:
ResetSequenceCaches();
break;
case DISCARD_TEMP:
ResetTempTableNamespace();
break;
......@@ -69,4 +74,5 @@ DiscardAll(bool isTopLevel)
LockReleaseAll(USER_LOCKMETHOD, true);
ResetPlanCache();
ResetTempTableNamespace();
ResetSequenceCaches();
}
......@@ -1602,3 +1602,19 @@ seq_redo(XLogRecPtr lsn, XLogRecord *record)
pfree(localpage);
}
/*
* Flush cached sequence information.
*/
void
ResetSequenceCaches(void)
{
SeqTableData *next;
while (seqtab != NULL)
{
next = seqtab->next;
free(seqtab);
seqtab = seqtab->next;
}
}
......@@ -1674,7 +1674,7 @@ CheckPointStmt:
/*****************************************************************************
*
* DISCARD { ALL | TEMP | PLANS }
* DISCARD { ALL | TEMP | PLANS | SEQUENCES }
*
*****************************************************************************/
......@@ -1703,6 +1703,13 @@ DiscardStmt:
n->target = DISCARD_PLANS;
$$ = (Node *) n;
}
| DISCARD SEQUENCES
{
DiscardStmt *n = makeNode(DiscardStmt);
n->target = DISCARD_SEQUENCES;
$$ = (Node *) n;
}
;
......
......@@ -2190,6 +2190,9 @@ CreateCommandTag(Node *parsetree)
case DISCARD_TEMP:
tag = "DISCARD TEMP";
break;
case DISCARD_SEQUENCES:
tag = "DISCARD SEQUENCES";
break;
default:
tag = "???";
}
......
......@@ -2378,7 +2378,7 @@ psql_completion(char *text, int start, int end)
else if (pg_strcasecmp(prev_wd, "DISCARD") == 0)
{
static const char *const list_DISCARD[] =
{"ALL", "PLANS", "TEMP", NULL};
{"ALL", "PLANS", "SEQUENCES", "TEMP", NULL};
COMPLETE_WITH_LIST(list_DISCARD);
}
......
......@@ -74,6 +74,7 @@ extern Datum pg_sequence_parameters(PG_FUNCTION_ARGS);
extern Oid DefineSequence(CreateSeqStmt *stmt);
extern Oid AlterSequence(AlterSeqStmt *stmt);
extern void ResetSequence(Oid seq_relid);
extern void ResetSequenceCaches(void);
extern void seq_redo(XLogRecPtr lsn, XLogRecord *rptr);
extern void seq_desc(StringInfo buf, uint8 xl_info, char *rec);
......
......@@ -2543,6 +2543,7 @@ typedef enum DiscardMode
{
DISCARD_ALL,
DISCARD_PLANS,
DISCARD_SEQUENCES,
DISCARD_TEMP
} DiscardMode;
......
......@@ -163,6 +163,9 @@ SELECT nextval('sequence_test'::text);
99
(1 row)
DISCARD SEQUENCES;
SELECT currval('sequence_test'::regclass);
ERROR: currval of sequence "sequence_test" is not yet defined in this session
DROP SEQUENCE sequence_test;
-- renaming sequences
CREATE SEQUENCE foo_seq;
......
......@@ -72,6 +72,8 @@ SELECT setval('sequence_test'::regclass, 32);
SELECT nextval('sequence_test'::text);
SELECT setval('sequence_test'::regclass, 99, false);
SELECT nextval('sequence_test'::text);
DISCARD SEQUENCES;
SELECT currval('sequence_test'::regclass);
DROP SEQUENCE sequence_test;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment