Discussions
Categories
- 385.5K All Categories
- 4.9K Data
- 2.5K Big Data Appliance
- 2.4K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Sequence Cache reset in 12c

Hi Friends,
Wishing you a Good Day !!
I have a query on a sequence cache reset in 12c Database. As I know the sequence cache is at DB level in 12c , but what I am looking for is that the reasons where the cache is reset
Sequence
CREATE SEQUENCE DOC_SEQ MINVALUE 1 MAXVALUE 9999999999 INCREMENT BY 1 START WITH 3000175009 CACHE 5000 NOORDER NOCYCLE NOPARTITION ;
As you can see the CACHE value is 5000 , but we are seeing in our DB the sequence's CACHE is getting lost for some reasons . From our analysis it is found that any restart of DB will reset the CACHE , but even there is no restart in DB CACHE is getting lost . Is there any values (reset time) at DB level which will reset the CACHE value
Note: I am not looking for the nextval function where the number will be lost , if the number is not used
Regards,
SH
Best Answer
-
Shamed H wrote:Thank you for your reply ... And you are almost to the problem statemnt that I have posted !! The shared pool flush also will reset the CACHE for sequence I have one more , If we provide any GRANT on the seqience to other schema , will that also reset the CACHE ? We did a test on this and we found it is retesting the CACHE , if the first CACHED sequence is used atleast by one. I am not sure this is also a cuase for the sequence GAP
Yes ALTER SYSTEM FLUSH SHARED POOL will also loose the cached values. Are you flushing the shared pool frequently? Why?
I would not expect a GRANT on a sequence to loose the sequence cache. I did NOT test it thou. Can you share how you tested it?
Answers
-
The way sequences work, when you use the CACHE option, the first time you use a number for the sequence, Oracle will return the first number to you, it will be incremented by 5000 (in your example) the lates used value on the sequence definition, and will store the 4999 remaining unused numbers in memory (cache).
The next time you ask for a number to the sequence, rather than searching from it on the definition adding one, and saving again the sequence definition, Oracle just return the next available value from the cache, making thing notable quicker.
The "bad" thing is, since those values are in memory, if the instance is shut down (normally or abnormally) those values are lost. That's the expected behavior.
The next time the sequence is used after an instance restart, it will get the value from the definition, add 5000 and save it, return the first one and store the other 4999 in the cache.
Instance shutdown is not something frequent and you should not worry about that, also sequence should be used (my opinion) to create PK (surrogate keys) values that are not shown to final users, so there should be no issues in having some gaps from time to time (the same would happen if you rollback a transaction, the sequence value is lost and gap is generated). Sequences are not gap-less.
-
You know that a shutdown and startup will lose cached sequences. Export and Import can also reload the sequence cache. But why do you care? Oracle makes clear that values generated by sequences can be generated out of order and can skip numbers. The only guarantee is that you get a new unique number every time you read NEXTVAL. Which is perfect for generating surrogate primary keys. If you care, then DON'T use sequences, but accept the performance hit that you'll take for using any other method for generating sequential values.
-
L. Fernigrini wrote:...The "bad" thing is, since those values are in memory, if the instance is shut down (normally or abnormally) those values are lost. That's the expected behavior....
That is not correct.
A normal shutdown, a shutdown transactional and a shutdown immediate will NOT loose the cached sequence values (unless you are in a very old database version like Oracle 7).
Only a SHUTDOWN ABORT will loose the cache.
To the OP: I recently wrote about some further ideas why IDs sometimes are or seems to be lost:
https://svenweller.wordpress.com/2019/08/20/some-quick-facts-about-sequence-caches-and-gaps-in-ids/
The most common reason is when a sequence ages out of the shared pool. You can either keep the sequence pinned in the shared pool or increase your shared pool size.
-
Well, i kind of remember seeing that on 9i (9.2.0.7), but I may be wrong, I never cared a lot about gaps.
If normal shutdowns save the last used value then the chances of having gaps with sequences is not very common.
I've read you post, it's really interesting, I will test the pin option ASAP
-
L. Fernigrini wrote:Well, i kind of remember seeing that on 9i (9.2.0.7), but I may be wrong, I never cared a lot about gaps.If normal shutdowns save the last used value then the chances of having gaps with sequences is not very common.I've read you post, it's really interesting, I will test the pin option ASAP
I know the behaviour during shutdown is documented somewhere in detail, but I just looked for 15 min and could not find it.
My point is we should not care about small gaps, but we should care about frequent large gaps.
Those kind of gaps are an indication that something is sub-optimal with our system.
And this is excatly what OP seems to be doing.
-- Edit:
Also I feel he need to add. If you do a normal or transactional shutdown on a RAC, then cached values probably might be lost.
The reason is, that during shutdown the last value needs to be persisted. And in that case I expect that the highest used value from one node is used. Which also means the cached values from the other node will be lost.
-
Thank you for your reply ... And you are almost to the problem statemnt that I have posted !! The shared pool flush also will reset the CACHE for sequence I have one more , If we provide any GRANT on the seqience to other schema , will that also reset the CACHE ? We did a test on this and we found it is retesting the CACHE , if the first CACHED sequence is used atleast by one. I am not sure this is also a cuase for the sequence GAP
-
Shamed H wrote:Thank you for your reply ... And you are almost to the problem statemnt that I have posted !! The shared pool flush also will reset the CACHE for sequence I have one more , If we provide any GRANT on the seqience to other schema , will that also reset the CACHE ? We did a test on this and we found it is retesting the CACHE , if the first CACHED sequence is used atleast by one. I am not sure this is also a cuase for the sequence GAP
Yes ALTER SYSTEM FLUSH SHARED POOL will also loose the cached values. Are you flushing the shared pool frequently? Why?
I would not expect a GRANT on a sequence to loose the sequence cache. I did NOT test it thou. Can you share how you tested it? -
Thanks Sven. I was using the Alter command to forward the sequence to some speficif number and then I was granting to other users. I was thinking the grant is making the CACHED lost , but I understand from testing that the ALTER comment is making the cache lose , rather then the grant
-
Depending on your exact database version here is a nice little (undocumented) trick to reset a sequence:
ALTER SEQUENCE mySeq RESTART START WITH yourNewValue;
This should work in 12.2.0.1 already.