Sequences Do Not Guarantee Sequential Values in Oracle Database

Categories: Oracle Database · Troubleshooting

Translated from Portuguese. Read the original

Hi everyone, today I bring some content that is quite fundamental and simple, but is often forgotten.
Every now and then I need to discuss with developers how to generate sequential values for the records of a table. This is a rather tiring discussion, because developers often choose to generate this sequential number in the application, but in most cases that performs far worse than doing it in the database, which already has the mechanisms and architecture ready for it; however, you need to be aware of the limitations of using sequences in Oracle Database, for example.
When we choose to use sequences, we need to understand that Oracle Database does not guarantee sequential values even if they are used for a single table and even when using the ORDER, INCREMENT BY 1 and NOCACHE clauses. You will eventually see missing numbers when using such a sequence. This is because transactions that fail or are rolled back have used and consumed a value from the sequence. The transaction that uses the sequence is rolled back, but the value requested from the sequence is not. Let’s look at an example.
First, let’s connect to our database. Here I’m using my Autonomous Transaction Processing Database TESTDB:

[Frankys-MacBook-Pro:~ franky$] /Users/franky/Downloads/sqlcl/sqlcl/bin/sql /nolog

SQLcl: Release 19.4 Production on Mon Feb 8 19:51:21 2020

Copyright (c) 1982, 2020, Oracle.  All rights reserved.

SQL> set cloudconfig /Users/franky/Downloads/Wallet_TESTDB.zip
Operation is successfully completed.
Operation is successfully completed.
Using temp directory:/var/folders/1h/mwvhtfsn48gc06mh8yp9lgfh183jkar/T/oracle_cloud_config706624827909182374
SQL> connect admin@testdb_high
Password? (**********?) *****************
Connected.
SQL> select banner_full from v$version;
BANNER_FULL
--------------------------------------------------------------------------------
Oracle Database 18c Enterprise Edition Release 18.0.0.0.0 - Production
Version 18.4.0.0.0

Then let’s create a table and a sequence to use as an example:

SQL> CREATE TABLE t_tst01 (
  2      c1 NUMBER(5),
  3      PRIMARY KEY ( c1 )
  4  );

Table T_TST01 created.

SQL> CREATE SEQUENCE s_tst01 INCREMENT BY 1 MINVALUE 1 MAXVALUE 10 NOCYCLE ORDER NOCACHE;

Sequence S_TST01 created.

When we try to get the current value of the sequence, the error ORA-08002 is returned, because the sequence has not been initialized yet:

SQL> SELECT
  2      s_tst01.CURRVAL
  3  FROM
  4      dual;

Error starting at line : 1 in command -
SELECT
    s_tst01.CURRVAL
FROM
    dual
Error report -
ORA-08002: sequence S_TST01.CURRVAL is not yet defined in this session

I insert a record using the sequence, which initializes it, and then I roll back:

SQL> INSERT INTO t_tst01 ( c1 ) VALUES ( s_tst01.NEXTVAL );

1 row inserted.

SQL> SELECT
  2      *
  3  FROM
  4      t_tst01;

        C1
----------
         1

SQL> ROLLBACK;

Rollback complete.

I insert a record using the sequence again and then query the inserted value. See that even after the rollback the sequence value was incremented, so we end up without the value 1 in the table:

SQL> INSERT INTO t_tst01 ( c1 ) VALUES ( s_tst01.NEXTVAL );

1 row inserted.

SQL> SELECT
  2      *
  3  FROM
  4      t_tst01;

        C1
----------
         2

SQL> COMMIT;

Commit complete.

When we query the current value we see it is still 2, and when we query the next value we see 3:

SQL> SELECT
  2      s_tst01.CURRVAL
  3  FROM
  4      dual;

   CURRVAL
----------
         2

SQL> SELECT
  2      s_tst01.NEXTVAL
  3  FROM
  4      dual;

   NEXTVAL
----------
         3

When we insert again using a new value, we see that the value 4 was inserted and stored in the table:

SQL> INSERT INTO t_tst01 ( c1 ) VALUES ( s_tst01.NEXTVAL );

1 row inserted.

SQL> SELECT
  2      *
  3  FROM
  4      t_tst01;

        C1
----------
         2
         4

SQL> COMMIT;

Commit complete.

Looking at the examples we can conclude that every time the NEXTVAL function is called the sequence is incremented, and even if a rollback happens it has no effect at all on the sequence increment.
So it is easier to build the application logic so that it does not depend on sequential values, not least because if a record needs to be removed it will also leave a gap in the sequence.
From version 12cR2 on, it is already possible to configure a column to be sequential by default: GENERATED BY DEFAULT AS IDENTITY. But that is a subject for another post.

That’s all for today. I hope you liked it.

Cheers and see you in the next post,

Franky

Comments