From d90ced8bb22194cbb45f58beb0961251103aeff5 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Thu, 3 Oct 2013 16:17:18 -0400 Subject: [PATCH] Add DISCARD SEQUENCES command. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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. --- doc/src/sgml/ref/discard.sgml | 12 +++++++++++- src/backend/commands/discard.c | 8 +++++++- src/backend/commands/sequence.c | 16 ++++++++++++++++ src/backend/parser/gram.y | 9 ++++++++- src/backend/tcop/utility.c | 3 +++ src/bin/psql/tab-complete.c | 2 +- src/include/commands/sequence.h | 1 + src/include/nodes/parsenodes.h | 1 + src/test/regress/expected/sequence.out | 3 +++ src/test/regress/sql/sequence.sql | 2 ++ 10 files changed, 53 insertions(+), 4 deletions(-) diff --git a/doc/src/sgml/ref/discard.sgml b/doc/src/sgml/ref/discard.sgml index 65ebbae138..762f865711 100644 --- a/doc/src/sgml/ref/discard.sgml +++ b/doc/src/sgml/ref/discard.sgml @@ -21,7 +21,7 @@ PostgreSQL documentation -DISCARD { ALL | PLANS | TEMPORARY | TEMP } +DISCARD { ALL | PLANS | SEQUENCES | TEMPORARY | TEMP } @@ -66,6 +66,15 @@ DISCARD { ALL | PLANS | TEMPORARY | TEMP } + + SEQUENCES + + + Discards all cached sequence values. + + + + ALL @@ -83,6 +92,7 @@ UNLISTEN *; SELECT pg_advisory_unlock_all(); DISCARD PLANS; DISCARD TEMP; +DISCARD SEQUENCES; diff --git a/src/backend/commands/discard.c b/src/backend/commands/discard.c index 76f3ab672a..9010368f57 100644 --- a/src/backend/commands/discard.c +++ b/src/backend/commands/discard.c @@ -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(); } diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c index ddfaf3bd29..3ec6b7654a 100644 --- a/src/backend/commands/sequence.c +++ b/src/backend/commands/sequence.c @@ -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; + } +} diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index a9812af7cf..0efe1705e2 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -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; + } + ; diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index b1023c4e88..fffaa35d34 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -2190,6 +2190,9 @@ CreateCommandTag(Node *parsetree) case DISCARD_TEMP: tag = "DISCARD TEMP"; break; + case DISCARD_SEQUENCES: + tag = "DISCARD SEQUENCES"; + break; default: tag = "???"; } diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index b3de387b94..255061c1c4 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -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); } diff --git a/src/include/commands/sequence.h b/src/include/commands/sequence.h index 6bd4892cfc..ffafe6c52f 100644 --- a/src/include/commands/sequence.h +++ b/src/include/commands/sequence.h @@ -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); diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index 51fef68ca3..e5235cbf40 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -2543,6 +2543,7 @@ typedef enum DiscardMode { DISCARD_ALL, DISCARD_PLANS, + DISCARD_SEQUENCES, DISCARD_TEMP } DiscardMode; diff --git a/src/test/regress/expected/sequence.out b/src/test/regress/expected/sequence.out index 87feb08b14..37e9a8cfa1 100644 --- a/src/test/regress/expected/sequence.out +++ b/src/test/regress/expected/sequence.out @@ -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; diff --git a/src/test/regress/sql/sequence.sql b/src/test/regress/sql/sequence.sql index a32e0494a0..31cbc926c0 100644 --- a/src/test/regress/sql/sequence.sql +++ b/src/test/regress/sql/sequence.sql @@ -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; -- 2.40.0