What happens when I exhaust a bigint generated key? How to handle it?

It won’t run out.

The max bigint is 9223372036854775807 . At 1000 inserts/second that’s 106751991167 days worth. Almost 300 million years, if my maths is right.

Even if you partition it out, using offsets where say 100 servers each have a dedicated sub-range of the values (x*100+0x*100+99), you’re not going to run out. 10,000 machines doing 100,000 inserts/second might get you there in about three centuries. Of course, that’s more transactions per second than the New York Stock Exchange for hundreds of years solid…

If you do exceed the data type size limit of the generated key, new inserts will fail. In PostgreSQL (since you’ve tagged this PostgreSQL) with a bigserial you’ll see:

CREATE TABLE bigserialtest ( id bigserial primary key, dummy text );
SELECT setval('bigserialtest_id_seq', 9223372036854775807);
INSERT INTO bigserialtest ( dummy ) VALUES ('spam');

ERROR:  nextval: reached maximum value of sequence "bigserialtest_id_seq" (9223372036854775807)

For an ordinary serial you’ll get a different error, because the sequence is always 64-bit, so you’ll reach the point where you have to change the key type to bigint or get an error like:

regress=# SELECT setval('serialtest_id_seq', 2147483647);
regress=# INSERT INTO serialtest (dummy) VALUES ('ham');
ERROR:  integer out of range

If you truly believe that it’s possible for your site to reach the limit on a bigint in your application, you could use a composite key – say (shard_id, subkey) – or a uuid key.

Trying to deal with this in a new application is premature optimization. Seriously, from a new application to that kind of growth, will you be using the same schema? Or database engine? Or even codebase?

You might as well worry about GUID collisions in GUID keyed systems. After all, the birthday paradox means that GUID collisions are more likely than you think – at incredibly, insanely unlikely.

Furthermore, as Barry Brown points out in the comments, you’ll never store that much data. This is only a concern for high churn tables with insanely high transaction rates. In those tables, the application just needs to be capable of coping with the key being reset to zero, entries renumbered, or other coping strategies. Honestly, though, even a high traffic message queue table isn’t going to top out.

See:

Seriously, even if you build the next Gootwitfacegram, this won’t be a problem until way past the use-by date of your third application rewrite…

Leave a Comment