Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, yifei.

Asked: May 12, 2025 - 9:18 am UTC

Last updated: May 14, 2025 - 6:11 am UTC

Version: 11g 11.2.0.4

Viewed 100+ times

You Asked

Hi, I have a sequence skip number problem.
I have this sequence. The cache is 20 and noorder.
create sequence seq_serial_no minvalue 1 maxvalue 999 start with 2 increment by 1 cycle

The current situation that has arisen is:
I have two servers A and B. They connect to the same database instance.
Sometimes the sequence is incremented, but sometimes it jumps, and every time it jumps, it jumps to the last_number. Here is two examples.
The first example:
server value -- comment
B 201 2025/04/23 9:14
A 202 2025/04/23 9:22
A 221 2025/04/23 10:35 --skip
B 222 2025/04/23 11:08
A 241 2025/04/23 13:22 --skip
B 242 2025/04/23 15:13
A 261 2025/04/23 17:41 --skip
The second examples:
server value -- comment
A 541 2025/05/08 14:36
B 542 2025/05/08 15:12
A 561 2025/05/09 9:28 -- skip
B 562 2025/05/09 10:18
A 563 2025/05/09 10:46
A 581 2025/05/12 9:17 -- skip
B 582 2025/05/12 9:23
A 583 2025/05/12 10:30
B 601 2025/05/12 14:53 -- skip
A 602 2025/05/12 15:19
The skipping of numbers looks like it has something to do with the cache being cleared. May I ask if my guess is correct? If it is right, then what would clear the cache for SGA?

and Connor said...

A sequence can never be guaranteed to not miss a number, independent of whether there is caching or not.

Caching may increase the chances that you might miss some, but there will *always* be gaps in sequences, eg

SQL> create sequence seq nocache;

Sequence created.

SQL> create table t ( x varchar2(5), y int );

Table created.

SQL> insert into t values ('x', seq.nextval);

1 row created.

SQL> insert into t values ('xxxxxxx', seq.nextval);
insert into t values ('xxxxxxx', seq.nextval)
                      *
ERROR at line 1:
ORA-12899: value too large for column "T"."X" (actual: 7, maximum: 5)


SQL> insert into t values ('x', seq.nextval);

1 row created.

SQL> select * from t;

X              Y
----- ----------
x              1
x              3


Bottom line - don't worry about gaps, but some recommended viewing here






Rating

  (1 rating)

Comments

What are the conditions under which the counter resets

yifei, May 13, 2025 - 8:06 am UTC

Thank you Tom, the first video you provided gave me another insight into sequences, it turns out it's cached using a counter implementation. However, I'm pretty sure that there are no issues with rollbacks or database instance restarts, etc. in my application. It seems that getting the sequence after a longer period of time gives me the “last_number”. You can observe in my example above (I missed 203 in the second example, which happened around 9:30) that when there is a gap, it always gets “last_number”. I also made an attempt, if I run “alter system flush shared_pool” or “exec sys.dbms_shared_pool.purge(‘SEQUENCE NAME’,' q')”, then when I fetch the sequence again, it returns ‘last_number’, which seems to cause the counter to be updated directly to the ‘last_number’ position. This seems to cause the counter to be updated directly to the “last_number” position. So, I have two more questions:
1. Why does “alter system flush shared_pool” and alter system flush shared_pool” cause a sequence gap?
2. Is there any oracle feature that causes the counter to automatically update to the “last_number” position?
Connor McDonald
May 14, 2025 - 6:11 am UTC

1. Why does “alter system flush shared_pool” and alter system flush shared_pool” cause a sequence gap?


Let's say the cache is 20 and you are currently at value 13. You flush the shared pool so that "13" is lost. We'll resume from 21.

2. Is there any oracle feature that causes the counter to automatically update to the “last_number” position?

Anything that means we lose that cache value (flush, memory pressure, etc etc) will mean we go back to the dictionary (and hence update the LAST_NUMBER)