]> granicus.if.org Git - postgresql/commitdiff
Add DISCARD SEQUENCES command.
authorRobert Haas <rhaas@postgresql.org>
Thu, 3 Oct 2013 20:17:18 +0000 (16:17 -0400)
committerRobert Haas <rhaas@postgresql.org>
Thu, 3 Oct 2013 20:23:31 +0000 (16:23 -0400)
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
src/backend/commands/discard.c
src/backend/commands/sequence.c
src/backend/parser/gram.y
src/backend/tcop/utility.c
src/bin/psql/tab-complete.c
src/include/commands/sequence.h
src/include/nodes/parsenodes.h
src/test/regress/expected/sequence.out
src/test/regress/sql/sequence.sql

index 65ebbae1385025818fbfa84b891535a10642c5ca..762f86571168b0d34478d953a6bbe68ed5ac3720 100644 (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>
index 76f3ab672adc5938a215120821f5635cef79cc6b..9010368f577d1105e15f5e4ba95b11dd43f6f259 100644 (file)
 #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();
 }
index ddfaf3bd293a9114de59a59aefc656e40b47761d..3ec6b7654aa5c6b74174e3e9d05431a1fc20bdb6 100644 (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;
+       }
+}
index a9812af7cfc422dfc583a77e9a215435eb90c930..0efe1705e2454b28ea45d5cd33c26d66923eb532 100644 (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;
+                               }
+
                ;
 
 
index b1023c4e88221edaaae4b9d76c338c9d88cc0f1d..fffaa35d344d808a713738a92feb9ba9831727fb 100644 (file)
@@ -2190,6 +2190,9 @@ CreateCommandTag(Node *parsetree)
                                case DISCARD_TEMP:
                                        tag = "DISCARD TEMP";
                                        break;
+                               case DISCARD_SEQUENCES:
+                                       tag = "DISCARD SEQUENCES";
+                                       break;
                                default:
                                        tag = "???";
                        }
index b3de387b941ee928804647d7ef599889885fcf4d..255061c1c417835cc3c71e300ffa95195bd4aad9 100644 (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);
        }
index 6bd4892cfc15d88eb0cef7d63883a18b6abc7f30..ffafe6c52f4b69713dc85a987a8c23072347bb78 100644 (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);
index 51fef68ca3cba3833e9e8bb84175478d0352d2c7..e5235cbf40f317bccaa835f75943f26a38db0b19 100644 (file)
@@ -2543,6 +2543,7 @@ typedef enum DiscardMode
 {
        DISCARD_ALL,
        DISCARD_PLANS,
+       DISCARD_SEQUENCES,
        DISCARD_TEMP
 } DiscardMode;
 
index 87feb08b14afa302928bf17f7409bde3a8441ea2..37e9a8cfa1afdea6b749b6813fbd6e5b4d0a08b0 100644 (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;
index a32e0494a026eea6a5f87ae290a9eac0ed89771f..31cbc926c029a468c3407e2ad9d9f4e22bdb04b0 100644 (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;