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.
This commit is contained in:
Robert Haas 2013-10-03 16:17:18 -04:00
parent c64e68fd9f
commit d90ced8bb2
10 changed files with 53 additions and 4 deletions

View File

@ -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>

View File

@ -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();
}

View File

@ -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;
}
}

View File

@ -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;
}
;

View File

@ -2190,6 +2190,9 @@ CreateCommandTag(Node *parsetree)
case DISCARD_TEMP:
tag = "DISCARD TEMP";
break;
case DISCARD_SEQUENCES:
tag = "DISCARD SEQUENCES";
break;
default:
tag = "???";
}

View File

@ -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);
}

View File

@ -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);

View File

@ -2543,6 +2543,7 @@ typedef enum DiscardMode
{
DISCARD_ALL,
DISCARD_PLANS,
DISCARD_SEQUENCES,
DISCARD_TEMP
} DiscardMode;

View File

@ -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;

View File

@ -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;