Oracle identity column after a crash: the id jumps from 9 to 24 and a sequence from 3 to 22
| | |

Oracle identity columns and sequences: the ISEQ$$ cache and what a crash costs, tested

Oracle identity column and sequences on 26ai Free: the hidden ISEQ$$ sequence, cache of 20, NEXTVAL once per statement and the gap after a crash.

Short answer: I killed the container with podman kill (SIGKILL): an Oracle identity column went from id 9 to 24 and a plain sequence from 3 to 22, which matches the cache of 20 that both use. A clean restart, by contrast, lost nothing. NEXTVAL is evaluated once per statement, so two calls in one query return the same value.

Oracle AI Database 26ai Free 23.26.3 in a container, throw-away schema, Rocky Linux 10.2 host. Part 3 of 5. Every output comes from my own test runs.

Oracle identity column after a crash: the id jumps from 9 to 24 and a sequence from 3 to 22
Oracle 26ai Free: a cache of 20 sits behind the identity column and the sequence.

Oracle identity column or sequence as default

CREATE TABLE t_ident (id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY, v VARCHAR2(20));
CREATE SEQUENCE s1;
CREATE TABLE t_seq (id NUMBER DEFAULT s1.NEXTVAL PRIMARY KEY, v VARCHAR2(20));

INSERT INTO t_ident(v) VALUES ('a') RETURNING id INTO :id;   -- 1
INSERT INTO t_seq(v)   VALUES ('a') RETURNING id INTO :id;   -- 1
SELECT s1.CURRVAL FROM dual;                                  -- 1

The dictionary shows the link:

TABLE_NAME COLUMN_N DATA_DEFAULT
---------- -------- ----------------------------------------
T_IDENT    ID       "SHOP"."ISEQ$$_73219".nextval
T_SEQ      ID       "SHOP"."S1"."NEXTVAL"

user_tab_identity_cols maps each identity column to its hidden sequence and gives ALWAYS or BY DEFAULT. Both sequences above report cache_size 20 in user_sequences: that is also the default for a plain CREATE SEQUENCE.

ALWAYS or BY DEFAULT

With BY DEFAULT, Oracle accepts an explicit value but the counter does not move, exactly as in PostgreSQL:

INSERT INTO t_by(id, v) VALUES (2, 'explicit 2');                       -- ok
INSERT INTO t_by(v) VALUES ('default 1') RETURNING id INTO :id;         -- 1
INSERT INTO t_by(v) VALUES ('default 2') RETURNING id INTO :id;
ORA-00001: unique constraint (SHOP.SYS_C008690) violated on table SHOP.T_BY columns (ID)
INSERT INTO t_by(v) VALUES ('default 3') RETURNING id INTO :id;         -- 3

With ALWAYS:

ORA-32795: cannot insert into a generated always identity column

Rollback and failed inserts consume values

INSERT INTO t_ident(v) VALUES ('rolled back') RETURNING id INTO :id;    -- 2
ROLLBACK;
INSERT INTO t_ident(v) VALUES ('after rollback') RETURNING id INTO :id; -- 3

With a unique violation: id 1, ORA-00001 on the duplicate, then 3.

NEXTVAL is evaluated once per statement

SELECT s_cache.NEXTVAL, s_cache.NEXTVAL FROM dual;     -- 1 1

Two calls in the same statement return the same value, so one statement cannot hand out two ids.

Concurrency: order of ids is not order of commits

A: begin 08:32:11.155        (A inserts first, commits last)
B: begin 08:32:12.658
B got id 2
B: committed 08:32:13.493
A got id 1
A: committed 08:32:15.900

B’s row commits and is visible before A’s row, although its id is higher.

CACHE belongs to the instance, not to the session

CREATE SEQUENCE s_cache CACHE 10;
same session twice : 1 1
new session        : 2
another new session: 3

Unlike PostgreSQL, different sessions get consecutive values: the cache is shared by the instance (observed: consecutive values across sessions). user_sequences.last_number shows the high-water mark written to the dictionary: 11 for s_cache, 21 for a cache of 20 after the first call.

Limits and CYCLE

ORA-08004: Sequence S_SMALL.NEXTVAL exceeds MAXVALUE and cannot be instantiated.
ALTER SEQUENCE s_small CYCLE;
ORA-04013: The CACHE value may not exceed the CYCLE value.
ALTER SEQUENCE s_small NOCACHE CYCLE;
SELECT s_small.NEXTVAL FROM dual;                      -- 1

If the cache is bigger than the cycle length, CYCLE is refused; NOCACHE fixes it. An identity on NUMBER(2) starting at 98 gives 98, 99 and then ORA-01438: value 100 greater than specified precision (2, 0).

Restart and crash

Three rows inserted, the last deleted, clean restart (podman stop: SIGTERM, the image runs a SHUTDOWN IMMEDIATE):

id of the row inserted after the clean restart: 4
sequence before the restart: 1 1 1   (one statement, three calls)
sequence after the restart: 2

No gap. Five more rows (last id 9), then podman kill (SIGKILL), instance recovery (the alert log shows Beginning crash recovery of 1 threads):

ids before the crash: 1,2,4,5,6,7,8,9
sequence before the crash: 3 3
id of the row inserted after the crash: 24
ids now: 1,2,4,5,6,7,8,9,24
sequence after the crash: 22

The Oracle identity column jumps from 9 to 24, the explicit sequence from 3 to 22, but the clean restart before it had lost nothing. The arithmetic is consistent with the cache of 20 starting at the first value handed out after the clean restart (4 + 20 = 24 and 2 + 20 = 22): after the crash, the instance restarts from the high-water mark stored in the dictionary. However, I did not trace the dictionary updates to confirm the mechanism.

Where to read the state

SELECT sequence_name, increment_by, cycle_flag, order_flag, cache_size, last_number FROM user_sequences;
SELECT table_name, column_name, generation_type, sequence_name FROM user_tab_identity_cols;

last_number is the next value that would be handed out when the cache is exhausted, not the last value used.

Official documentation

Oracle AI Database Free, the edition used here

CREATE SEQUENCE, SQL Language Reference, 26ai

CREATE TABLE, identity clause

Pseudocolumns, CURRVAL and NEXTVAL

*Not tested here: ORDER and RAC behaviour, NOCACHE cost, ALTER TABLE ... MODIFY ... RESTART, SYS_GUID() as key, shutdown with ABORT (my crash is a SIGKILL of the container).*

All parts of the series

1/5 PostgreSQL

2/5 MariaDB

3/5 Oracle

4/5 SQL Server (coming 1 Dec)

5/5 TL;DR: the four engines compared (coming 8 Dec)

Related articles

Inside database data files (1/3): InnoDB, from MySQL to MariaDB

Inside database data files (2/3): PostgreSQL

Inside database data files (3/3): Oracle, and all three side by side

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *