This site is currently read-only as we are migrating to Oracle Forums for an improved community experience. You will not be able to initiate activity until January 31st, when you will be able to use this site as normal.

    Forum Stats

  • 3,890,899 Users
  • 2,269,649 Discussions
  • 7,916,821 Comments

Discussions

Sequence Cache reset in 12c

Shamed H
Shamed H Member Posts: 85 Red Ribbon
edited Aug 29, 2019 7:35AM in SQL & PL/SQL

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

Tagged:
L. FernigriniShamed H

Best Answer

  • Sven W.
    Sven W. GermanyMember Posts: 10,562 Gold Crown
    edited Aug 27, 2019 11:05AM 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?

    Shamed HShamed H

Answers

  • L. Fernigrini
    L. Fernigrini Database Practice Lead Rosario, ArgentinaMember Posts: 4,196 Gold Crown
    edited Aug 27, 2019 8:26AM

    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.

  • jflack
    jflack Senior Java Full Stack Developer Alexandria, VA, USAMember Posts: 1,524 Bronze Trophy
    edited Aug 27, 2019 8:30AM

    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.

  • Sven W.
    Sven W. GermanyMember Posts: 10,562 Gold Crown
    edited Aug 27, 2019 8:39AM
    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.

    Shamed H
  • L. Fernigrini
    L. Fernigrini Database Practice Lead Rosario, ArgentinaMember Posts: 4,196 Gold Crown
    edited Aug 27, 2019 8:48AM

    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

  • Sven W.
    Sven W. GermanyMember Posts: 10,562 Gold Crown
    edited Aug 27, 2019 9:29AM
    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.

    L. FernigriniShamed H
  • Shamed H
    Shamed H Member Posts: 85 Red Ribbon
    edited Aug 27, 2019 10:32AM

    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

  • Sven W.
    Sven W. GermanyMember Posts: 10,562 Gold Crown
    edited Aug 27, 2019 11:05AM 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?

    Shamed HShamed H
  • Shamed H
    Shamed H Member Posts: 85 Red Ribbon
    edited Aug 29, 2019 7:12AM

    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

  • Sven W.
    Sven W. GermanyMember Posts: 10,562 Gold Crown
    edited Aug 29, 2019 7:35AM

    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.