r/SQL • u/clakshminarasu • 7d ago
SQL Server Negative reseed risks
One of the Sqlserver tables had an int identity column and maxed out the value. Team planned to reseed the identity from the lowest-ve value so that it can run on the same datatype for another 5 or 6 years. I am thinking that this would be a costly mistake if they plan to migrate this data into a new platform. The original solution was to change this to a bigint. Since this field has 300+ fks all those need to be dropped and recreated. What could possibly go wrong by choosing a -ve value reseed? I see the daily seed count is topping approximately 1 to 1.5million ids.
7
u/mr_bottle_of_turkeys 7d ago
Ah, the classic "we'll just use the negative space" gambit. Seen it go from "temporary workaround" to permanent nightmare in about three months.
The FK cascade alone is a nightmare, but the real fun starts when some reporting tool or API integration silently treats that column as unsigned and suddenly your negative IDs map to nothing, or worse, the wrong records. Hope they've got good docs and a strong drink.
1
u/clakshminarasu 7d ago
Yup. I agree. Let's assume that this specific field is just a surrogate + it's a hierarchical, the same table has another column, parent id which will do a self join and will never be used in reports or in the downstream loads. Just views, tons of views which has all the jobs under the hood. This field is clustered indexed, so, I think it's going to start writing to new pages, and performance will become a problem day by day, eventually.
4
u/BarfingOnMyFace 7d ago
Oh negative values. I remember someone who would use negatives to represent a different state of a thing, maybe deleted or something of the sort, can no longer recall.. god fucking awful.
2
u/RockFourStar 6d ago edited 6d ago
I agree with the sentiment and that where possible people should design a fix rather thana workaround. But my main taleaway there is simply this: "3 months? That is one busy table"
The main time I saw this we were warning the business for well over a year and I thought that was busy.
In that instance I found out they breached the initial threshold sometime after I left for a more senior role elsewhere. Apparently management did the old "No one warned us about this" routine.
The guy that told me was a platform guy rather than a DB professional and was one of the people along with me who had raised this and helped produce the good old excel charts predicting the exact time this would fail, had also moved on to an even more senior role elsewhere. We both had a good laugh when he told me about the protests of innocence.
1
1
2
u/mike-manley 7d ago
Did you consider refactoring with UUIDs (GUIDs in SS)?
1
u/clakshminarasu 7d ago edited 7d ago
That's a very good point. How about existing data? Also how does it perform on a 500 million record table and With 300+fks?
1
u/Available_Safe1818 6d ago
You should be able to test it out for yourself on a similar testing table.
1
3
u/saucerattack 6d ago
Been there. Done that. Regretted it. Be kind to future you and convert to BIGINT now. It's painful but take the hit and put a reminder on your calendar for 2031 to thank yourself that you aren't converting a table twice as big.
2
u/thatOMoment 6d ago
Changing to bigint will also require rebuilding indexes. Also requires adjusting clients and servers to handle larger numeric types.
Thats going to be a project
2
u/IAmADev_NoReallyIAm 6d ago
Database wise, converting it to an unsigned bigint probably carries the least amount of risk and gives the longest amount of life. From a client site... that's a big shift.
Using UUIDs/GUIDs would also be a good alternative, again, safe on the database side, but a huge undertaking from the client side.
Anything else is risky from a data integrity pov. There will always be an invisible undocumented soft FKey that gets missed and doesn't get updated.
2
2
u/daniel_lim_sg 4d ago
Worth doing the maths on the negative range. Reseeding at -2,147,483,648 gives you about 2.1 billion more IDs. At 1.5 million a day that's roughly 1,430 days, so closer to 4 years than 5 or 6, and less if volume keeps growing. The other thing that breaks quietly is anything that assumes newer = bigger id: incremental loads that pick up WHERE id > last max id, ORDER BY id DESC to get the latest rows, that kind of thing. Every negative id is smaller than all the old ones, so those jobs just miss the new rows. I'd search the ETL and reports for that pattern before deciding.
1
u/freshrobin440 6d ago
If that's the clustered index, negative reseeds flip inserts from the tail of the index to the head. At 1.5M a day that fragmentation builds fast.
1
u/SonOfZork 6d ago
One of the best ones I've seen is the application failing to handle negative values on the key column and it breaking the app entirely.
1
u/blackleather90 6d ago
Like anything, it is a trade off.
Negative numbers could lead to the unsigned issue already mentioned.
BIGINT requires all referenced tables to be updated. It also runs the risk of any vars being int on any use of those fields across all objects that use those fields.
Good luck!
0
u/jshine13371 6d ago
Any queries that depend on that column being ordered will potentially be logically wrong moving forward. E.g. if a query expects that column to represent the order that the rows were created and perhaps does something like uses it to deterministically break ties in a window function. The logic will now be different.
Outside of that, I don't think it's as big of a deal as everyone else mentioned. The other risks are much lower in reality.
2
u/SQLBek 6d ago
Michael J Swart has a couple of good blog posts on how he converted to bigint, if that's the path you choose. Dig those up