Oracle sequence caching Oracle sequence caching database database

Oracle sequence caching


No, this will not be the case.

Your sequence will continue at 101, the values between 50 and 100 will be missing.

The only reason to disable sequence caching is when trying to avoid gaps in your sequence, which is not relevant for most Primary Keys.

You might be interested in this article, which states that

The downside of creating a sequence with a cache is that if a system failure occurs, all cached sequence values that have not be used, will be "lost". This results in a "gap" in the assigned sequence values. When the system comes back up, Oracle will cache new numbers from where it left off in the sequence, ignoring the so called "lost" sequence values.


Say the cache has the values 101-200.The value written on disk is 201.Your insert uses 101-150.Instance goes down.Instance starts up.Next time the sequence is used 201-300 will be cached.


Turns out that this is not (or no longer true). Shut down and restart of instance does not lose cached values. Simple test with cache = 1000.

SQL> select ordered.currval from dual;

CURRVAL

    22

SQL> select unordered.currval from dual

CURRVAL

    24

SQL> SHUTDOWN IMMEDIATESQL> STARTUP

NEXTVAL

    23

SQL> select unordered.nextval from dual;

NEXTVAL

    25

Also, ALL_SEQUENCES.LAST_NUMBER does not hold the last last number provided by the sequence except on startup and before first NEXTVAL. After first NEXTVAL, it holds last number served plus CACHE_SIZE. This does not change until new cache is generated. However, on shutdown, it apparently gets reset to just the last number served.