SQL Server identity after kill -9: from 9 to 1004 with IDENTITY_CACHE on, from 9 to 10 with it off
| | |

SQL Server IDENTITY and sequences: SCOPE_IDENTITY, IDENTITY_CACHE and the gap of 1000

SQL Server IDENTITY and SEQUENCE on 2025: SCOPE_IDENTITY versus @@IDENTITY, IDENTITY_INSERT, and the jump from 9 to 1004 after a crash with IDENTITY_CACHE.

Short answer: I ran kill -9 on sqlservr: a SQL Server identity column jumped from 9 to 1004 and a sequence from 3 to 52. After ALTER DATABASE SCOPED CONFIGURATION SET IDENTITY_CACHE = OFF, the identity continued at 10. Read the new value with OUTPUT or SCOPE_IDENTITY(), because with a trigger @@IDENTITY returned 100 instead of 2.

SQL Server 2025 (RTM-CU10, 17.0.5035.5) on Linux, Rocky Linux 10.2, native package, throw-away database. Part 4 of 5. Every output comes from my own test runs.

SQL Server identity after kill -9: from 9 to 1004 with IDENTITY_CACHE on, from 9 to 10 with it off
SQL Server 2025: IDENTITY_CACHE decides the size of the gap.

SQL Server identity, OUTPUT and the three “last value” functions

CREATE TABLE t_ident (id INT IDENTITY(1,1) PRIMARY KEY, v VARCHAR(20));
INSERT INTO t_ident(v) OUTPUT INSERTED.id AS output_id VALUES ('a');     -- 1
SELECT SCOPE_IDENTITY(), @@IDENTITY, IDENT_CURRENT('t_ident');           -- 1 1 1

OUTPUT INSERTED.id is the equivalent of RETURNING. It is also the safest way to get the value back. Then add a trigger that inserts into another table with an identity (seed 100):

CREATE TRIGGER trg_ident ON t_ident AFTER INSERT AS INSERT INTO t_log(msg) VALUES ('from trigger');
INSERT INTO t_ident(v) VALUES ('b');
SELECT SCOPE_IDENTITY() AS scope_identity_with_trigger, @@IDENTITY AS at_at_identity_with_trigger;
scope_identity_with_trigger at_at_identity_with_trigger
--------------------------- ---------------------------
2                           100

SCOPE_IDENTITY() stays in your scope, so it returns 2. @@IDENTITY returns the last identity generated in the session whatever the scope, here the trigger’s 100. Use SCOPE_IDENTITY() or OUTPUT instead of @@IDENTITY. IDENT_CURRENT('t'), by contrast, is the last value for a table across all sessions.

Sequence object

CREATE SEQUENCE s1 AS INT START WITH 1 INCREMENT BY 1;
CREATE TABLE t_seq (id INT PRIMARY KEY DEFAULT (NEXT VALUE FOR s1), v VARCHAR(20));
INSERT INTO t_seq(v) OUTPUT INSERTED.id VALUES ('a');     -- 1
SELECT NEXT VALUE FOR s1;                                 -- 2

Rollback and failed inserts consume values

BEGIN TRAN; INSERT INTO t_ident(v) OUTPUT INSERTED.id VALUES ('rolled back');   -- 3
ROLLBACK;
INSERT INTO t_ident(v) OUTPUT INSERTED.id VALUES ('after rollback');            -- 4

With a unique violation: id 1, Msg 2627 ... Violation of UNIQUE KEY constraint, then 3.

Concurrency: order of ids is not order of commits

A: begin 08:34:29.142        (A inserts first, commits last)
B: begin 08:34:30.645
B got id 2
B: committed 08:34:30.710
A got id 1
A: committed 08:34:33.204

Sequence CACHE, sessions and same-statement calls

CREATE SEQUENCE s_cache AS INT START WITH 1 CACHE 10;
same session twice (one statement): 1 1
new session        : 2
another new session: 3

Like Oracle, two NEXT VALUE FOR in one statement return the same value, while different sessions get consecutive values. (I did not test a restart with CACHE 10.)

Explicit values

INSERT INTO t_x(id, v) VALUES (100, 'explicit without IDENTITY_INSERT');
Msg 544: Cannot insert explicit value for identity column in table 't_x' when IDENTITY_INSERT is set to OFF.
SET IDENTITY_INSERT t_x ON; INSERT INTO t_x(id, v) VALUES (100, 'explicit 100'); SET IDENTITY_INSERT t_x OFF;
INSERT INTO t_x(v) ... ;                                       -- 101
SET IDENTITY_INSERT t_x ON; INSERT INTO t_x(id, v) VALUES (50, 'explicit 50'); SET IDENTITY_INSERT t_x OFF;
INSERT INTO t_x(v) ... ;                                       -- 102
DBCC CHECKIDENT ('t_x', RESEED, 10);
INSERT INTO t_x(v) ... ;                                       -- 11

An explicit value above the counter moves it (101 after 100); one below does not (102 after 50). DBCC CHECKIDENT ... RESEED sets the counter by hand; the next value was 11, and a later NORESEED report shows the identity value 11 against a column maximum of 102: however, SQL Server accepts a counter lower than the data; whether the primary key then refuses the duplicates was not tested here.

Limits and CYCLE

Msg 11728: The sequence object 's_small' has reached its minimum or maximum value. Restart the sequence object to allow new values to be generated.
ALTER SEQUENCE s_small CYCLE;  SELECT NEXT VALUE FOR s_small;   -- 1
Msg 8115: Arithmetic overflow error converting IDENTITY to data type tinyint.

A TINYINT IDENTITY(254,1) gave 254, 255, then Msg 8115. CREATE SEQUENCE ... MAXVALUE 3 printed an informational message that the default cache is larger than the number of available values.

Restart and crash

Test on the default setting (IDENTITY_CACHE = ON, read from sys.database_scoped_configurations). Three rows inserted, the last deleted, clean restart (systemctl stop / start):

IDENT_CURRENT before the restart: 3
IDENT_CURRENT after the restart:  3
id of the row inserted after the clean restart: 4
sequence after the restart: 2

No gap. Five more rows (last id 9), then kill -9 sqlservr, restart:

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: 1004
ids now: 1,2,4,5,6,7,8,9,1004
sequence after the crash: 52

First, the SQL Server identity jumped from 9 to 1004. The SEQUENCE created without an explicit CACHE jumped from 3 to 52. That fits a default cache of 50, but sys.sequences shows a NULL cache_size for sequences created without CACHE, so I did not confirm the value. Now the same test with:

ALTER DATABASE SCOPED CONFIGURATION SET IDENTITY_CACHE = OFF;
id of the row inserted after the clean restart: 4
ids before the crash: 1,2,4,5,6,7,8,9
id of the row inserted after the crash: 10

No gap, but possibly at a cost in insert throughput (not measured here).

Where to read the state

SELECT OBJECT_NAME(object_id), seed_value, increment_value, last_value FROM sys.identity_columns;
SELECT name, current_value, increment, maximum_value, is_cycling, is_cached, cache_size FROM sys.sequences;
SELECT IDENT_CURRENT('t_x');
DBCC CHECKIDENT ('t_x', NORESEED);

Official documentation

SQL Server, product site

CREATE SEQUENCE, Microsoft Learn

IDENTITY property, Microsoft Learn

SCOPE_IDENTITY, Microsoft Learn

ALTER DATABASE SCOPED CONFIGURATION, where IDENTITY_CACHE is documented

*Not tested here: NO CACHE on a sequence, the throughput effect of IDENTITY_CACHE = OFF, failover (Always On), SQL Server on Windows, other versions.*

All parts of the series

1/5 PostgreSQL

2/5 MariaDB

3/5 Oracle

4/5 SQL Server

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 *