Home / Databases / SQL Server

DatabasesRunbookAdvanced

Sev1: SQL Server inserts fail with error 8115. The INT identity column reached its maximum

Every insert into a busy table fails with Msg 8115 because its INT identity hit 2,147,483,647. Restore inserts in minutes with a negative reseed, then move to BIGINT without a long outage.

Deepak Bhardwaj7 min read

Applies toSQL Server 2016-2025, Azure SQL Database, Azure SQL Managed Instance, Amazon RDS for SQL Server

Symptoms

  • Every insert into one table fails at the same time with: Msg 8115, Level 16, State 1 - Arithmetic overflow error converting IDENTITY to data type int.
  • Reads, updates and deletes on the table still work. Only new rows fail.
  • Child tables, queues and audit tables that use the same key pattern may fail soon after.
  • If the key comes from a SEQUENCE object instead of an IDENTITY column, the error is Msg 11728: The sequence object '...' has reached its minimum or maximum value. The fixes below apply in the same way.

1. Confirm the table and find the next ones

SELECT IDENT_CURRENT('dbo.Orders') AS current_identity,
       IDENT_INCR('dbo.Orders')    AS increment;

A value of 2147483647 (or 32767 for smallint, 255 for tinyint) confirms the problem. Then check every identity column in the database, because related tables often run out within days of each other:

SELECT OBJECT_SCHEMA_NAME(ic.object_id) AS schema_name,
       OBJECT_NAME(ic.object_id)        AS table_name,
       ic.name                          AS column_name,
       TYPE_NAME(ic.system_type_id)     AS data_type,
       CAST(ic.last_value AS bigint)    AS last_value,
       CAST(100.0 * CAST(ic.last_value AS bigint) /
            CASE TYPE_NAME(ic.system_type_id)
                 WHEN 'tinyint'  THEN 255
                 WHEN 'smallint' THEN 32767
                 WHEN 'int'      THEN 2147483647
                 WHEN 'bigint'   THEN 9223372036854775807
            END AS decimal(6,2))        AS pct_used
FROM sys.identity_columns AS ic
JOIN sys.objects AS o ON o.object_id = ic.object_id
WHERE o.type = 'U'
  AND TYPE_NAME(ic.system_type_id) IN ('tinyint','smallint','int','bigint')
  AND ic.last_value IS NOT NULL
ORDER BY pct_used DESC;

For sequences, check sys.sequences (current_value against maximum_value).

2. Pick the mitigation

Situation Fastest safe option Time to restore inserts
Application accepts negative IDs and nothing sorts by ID for "latest" Reseed to the negative range (step 3a) Seconds
Append-only log or event table, no foreign keys point at it Swap in a new BIGINT table (step 3b) Seconds to minutes
Small table (a few GB) with a short maintenance window Rebuild the key as BIGINT in place (step 3c) Minutes to an hour, table blocked
Large OLTP table that must keep all rows Reseed now (3a), then an online copy to BIGINT (step 4) Seconds now, migration later

Most teams combine 3a and step 4: the reseed ends the outage, and the BIGINT migration happens on a planned date.

3. Restore inserts

3a. Reseed into the negative range

A signed int has the same number of unused values below zero. Reseed to the minimum and the next row gets -2147483647, counting up toward zero:

DBCC CHECKIDENT ('dbo.Orders', RESEED, -2147483648);

-- check the next value without inserting
SELECT IDENT_CURRENT('dbo.Orders');

This gives you about 2.1 billion more values with no table rebuild.

Warning

Before you reseed, confirm with the application owner that nothing rejects negative IDs, treats the ID as "newest = highest", or stores the ID in an unsigned or positive-only field (URLs, check constraints, downstream systems). Search for check constraints on the column with SELECT name, definition FROM sys.check_constraints WHERE parent_object_id = OBJECT_ID('dbo.Orders');.

If any negative IDs already exist, start the reseed below the lowest existing value or pick a range you know is empty. Otherwise the primary key rejects duplicates.

3b. Table swap for log-style tables

BEGIN TRAN;
CREATE TABLE dbo.Events_new (
    EventId  bigint IDENTITY(2147483648, 1) NOT NULL
             CONSTRAINT PK_Events_new PRIMARY KEY CLUSTERED,
    Created  datetime2 NOT NULL,
    Payload  nvarchar(max) NULL
);
EXEC sp_rename 'dbo.Events', 'Events_old';
EXEC sp_rename 'dbo.Events_new', 'Events';
COMMIT;

Copy the real column list, defaults, indexes, triggers and permissions from the old table (script it from SSMS: Script Table as > CREATE To). Seeding at 2147483648 keeps IDs unique across the old and new tables. sp_rename needs a schema modification lock, so long-running queries on the table delay it.

3c. In-place change for small tables

You cannot alter a column that is part of a primary key, index or foreign key. Drop those first, change the type, then recreate them:

ALTER TABLE dbo.OrderLines DROP CONSTRAINT FK_OrderLines_Orders;
ALTER TABLE dbo.Orders     DROP CONSTRAINT PK_Orders;

ALTER TABLE dbo.Orders ALTER COLUMN OrderId bigint NOT NULL;
ALTER TABLE dbo.OrderLines ALTER COLUMN OrderId bigint NOT NULL;

ALTER TABLE dbo.Orders ADD CONSTRAINT PK_Orders PRIMARY KEY CLUSTERED (OrderId);
ALTER TABLE dbo.OrderLines ADD CONSTRAINT FK_OrderLines_Orders
    FOREIGN KEY (OrderId) REFERENCES dbo.Orders (OrderId);

The type change rewrites every row and is fully logged. Check log space first (DBCC SQLPERF(LOGSPACE)), and expect redo lag on availability group secondaries.

Caution

Do not run 3c on a large table during the incident. A multi-hour ALTER can fill the transaction log, block the whole table, and roll back for just as long if you cancel it. Use 3a now and step 4 later.

List the foreign keys that reference the table before you start:

SELECT fk.name, OBJECT_NAME(fk.parent_object_id) AS child_table
FROM sys.foreign_keys AS fk
WHERE fk.referenced_object_id = OBJECT_ID('dbo.Orders');

4. Planned migration to BIGINT

For large tables, build a new table with a bigint key and copy rows in batches while the old one stays online:

SET IDENTITY_INSERT dbo.Orders_new ON;

DECLARE @from bigint = (SELECT ISNULL(MAX(OrderId), -2147483649) FROM dbo.Orders_new);
INSERT INTO dbo.Orders_new (OrderId, CustomerId, Created, Total)
SELECT TOP (50000) OrderId, CustomerId, Created, Total
FROM dbo.Orders
WHERE OrderId > @from
ORDER BY OrderId;

SET IDENTITY_INSERT dbo.Orders_new OFF;

Run the batch in a loop from SQL Agent, and keep it out of busy hours. Handle updates and deletes on rows you already copied with a trigger, Change Tracking or CDC. If you reseeded in step 3a, new rows arrive in the negative range behind the copy position, so capture those inserts the same way. In a short final window, copy the last rows, reseed the new table above the highest copied value, swap the names with sp_rename, and recreate the foreign keys. If CDC or transactional replication is enabled on the table, plan to recreate the capture instance or publication article afterwards.

5. Find why the counter ran out

SELECT COUNT_BIG(*) AS row_count,
       MIN(OrderId) AS min_id,
       MAX(OrderId) AS max_id
FROM dbo.Orders;

SELECT name, value FROM sys.database_scoped_configurations
WHERE name = 'IDENTITY_CACHE';
Cause Sign
Failed or rolled-back inserts Large gaps; identity values are not returned on rollback
Identity cache lost on restart or failover Jumps of about 1,000 after each restart (SQL Server 2012 and later with IDENTITY_CACHE = ON)
Bulk loads that delete and reload max_id far above row_count
Someone reseeded upward Sudden jump in values; check the default trace or your DDL audit

6. Verify recovery

  • Application inserts succeed, and IDENT_CURRENT moves in the expected direction.
  • Run the capacity query again and confirm the fixed table no longer tops the list.
  • Check availability group or replication health: SELECT database_id, synchronization_state_desc, redo_queue_size FROM sys.dm_hadr_database_replica_states;
  • Confirm client code handles bigint after step 4. C# int, Java Integer and JavaScript numbers above 2^53 all cause bugs.

Prevent it next time

  • Use bigint for identity keys on any table that grows without limit. Add a code review or schema linter rule for this.
  • Alert from the capacity query at 70% used. That gives you months to run the online migration.
  • Store the identity check in your daily DBA health job next to file growth and backup checks.
  • The same failure happens on other engines: MySQL AUTO_INCREMENT at INT maximum and PostgreSQL sequence exhausted.