How big can Django primary keys get?
Asked Answered
U

2

8

Is there a maximum value as to how high pk values for a model can get? For example, for something like an activity feed model, the pk's can get really large.

Ex, "Kyle liked this post", "Alex commented on this post" etc..As you can see, for every action, an activity feed object is created. Is there some sort of maximum threshold that will be reached? I've done some research but haven't found a concise answer. If there is some limit, how can one overcome this?

I currently use PostgreSQL as my database.

Undermine answered 19/11, 2013 at 20:54 Comment(1)
Two solutions: [a] don't make primary keys integers. Having a string value with 50 spaces will be sufficient; or [b] this kind of data isn't suited well for relational databases; perhaps a graph db would be better.Boutwell
P
12

Django's auto-incrementing IDs are AutoFields, a subclass of IntegerField. It will generate the primary keys as PostgreSQL integer, which are signed 32-bit integers with a maximum value of 2147483647, a little over 2 billion.

Pencil answered 10/12, 2013 at 9:43 Comment(4)
let's say the limit is reached, what would be the next step? sharding?Undermine
Lots of possibilities, starting from using bigints instead and using a custom Django field.Pencil
Why is it (4294967295 / 2)?Hessenassau
A signed integer can be negative, however primary keys can only be positive.Tanagra
V
-3

I'm gonna take a guess and assume that postgreSQL stores primary keys as 64 bits unsigned integers. If this is the case, then you can have up to 2^64 different values. Even with a 32 bits integer, that leaves us with 4294967296 possibilities. Unless you are twitter or facebook, you should never be annoyed with this kind of limit.

Valval answered 10/12, 2013 at 8:11 Comment(2)
check out RemcoGerlich's answer, thanks for trying to tackle the problem, appreciate itUndermine
Isn't it 4294967295? Since 2^32 - 1. #5772020Hessenassau

© 2022 - 2024 — McMap. All rights reserved.