Your database will break in 2038 (or sooner)
MySQL's 2038 problem is due to the way timestamps are stored. In this video we'll take a look at the problem and some of the potential solutions.
Everything I said
MySQL has a 2038 problem. And if you don't know what the 2038 problem is, it's kind of like the Y2K problem. I was 10 at the time, and I remember people lost their minds. At the stroke of midnight, elevators may stop. Airplanes and trains may come to a halt. Heat may vanish. Heat may vanish. The 2038 problem is not that serious.
I can promise you that. You don't need to stock up on anything. You might hear this problem referred to as Y2K38, which feels a little bit derivative to me. You might hear it called the Y2038 problem, which is a little straightforward. You might hear it called the epochalypse, which earns my highest marks because it is exceedingly clever with the wordplay, because this whole issue revolves around the Unix epoch.
So MySQL's problem here is that the timestamp column is a four-byte column, and the legal range is from the Unix epoch, which is the very beginning of 1970, all the way to 2038, which is where we get the name, 2038-01-19 at 03:14:07 UTC. That is the legal range of the timestamp column in MySQL.
So if you try to store anything beyond that legal range, you get an error that says this value is out of bounds. This is the 2038 problem. Let's see if we can prove it. Let's create a table called epochs, and in this table, we're gonna put two columns. We're gonna put a timestamp, and we're gonna put a date time. Now, the date time is similar to the timestamp.
A timestamp is four bytes. A date time is eight bytes. And there is another difference, and that relates to how they treat time zones, but we'll talk about that in a second. So let's create this table and then insert into epochs the timestamp and the date time. And for these values, let's just start by doing 2038-01-01 in both columns.
So hopefully, this is going to work, and that will just insert. We can do select star from epochs, and it looks like we're good so far. Now, here's the issue. If we change these to 2039 and we run that, we see that for column TS, it failed. That's because it's outside of the legal range that is acceptable.
So let's change this one back and see if it works for date time, and sure enough, it does. So now if we read this back, we see that date time, you can store beyond 2038. I think you can store up to 9,000-- the year 9999, so you should be good there. Um, but with a timestamp, you are capped at 2038. This is the most impactful place that this problem presents itself is in this timestamp column.
This is true up until the time of recording, and I don't think there are any plans to fix this as of yet. This column is four bytes, and it stops at 2038. As of MySQL 8.0.28, they did fix some of the functions that work with timestamps to allow broader ranges, but the column itself still doesn't work.
Let me show you these functions. So here we are in MySQL 8.0.27, and if we were to say select UNIX timestamp, we can pass in a date, and that will tell us the number of seconds since the epoch. So we'll say 2038-01-01, and we'll just run it that way. And so we get this huge, massive number of seconds since the epoch. Now, if we were to do something beyond what the timestamp column can support, 2038, we get, wah, wah, we get zero.
And that's of, that's as of 8.0.27. If we were to open another connection that is on 8.0.28 and we were to run that over here, we'd see that we do get the correct value. So those functions have been updated, but the column type is still bound to four bytes, which ends at 2038.
Now that we know that this problem exists and we've kind of proven how it manifests itself, the question is, what do you do? What do you do about this problem? How do you work around it? My first suggestion would be try to retire. Try to make it somebody else's problem. That's what I'm trying to do. I got 15 years to get out of the game. I think I can do it. I need you to like and subscribe so that I can get out earlier.
But here's, here's why I don't think it'll be a problem until 2038. If you only store values that are, uh, events or, like, point-in-time values like created at, updated at, modified, deleted, archived, all of those, um, point-in-time references, you're not gonna be storing values that are far in the future.
You are recording when something happened, and so it's not gonna be a problem until you hit 2038. So what I would say is don't use a timestamp column to store far future values because then you can get in trouble a lot earlier, right? So if you have a, if you have a column where the user can input a value ten or twenty years out, you, you could get in trouble in the next year or two, right?
You could get in trouble now. So my recommendation would be only store a-as of right now or earlier values in the timestamp, and you can put it off, honestly, for another fifteen years. The second option would be to switch to date time, and then your range just explodes. You have up to the year 9999, which should cover your use cases.
So that would be, that would be my other recommendation. If you do need to store far future values, switch to a date time. There is a little bit of a difference between how a timestamp and a date time treat time zones, and it's pretty important that we talk about it, so let me show you back in that epochs table. Let's just select the first row because that's the one where they both match.
So we've got 2038-01-01, zeros all the way out in both columns. Now- The way that MySQL treats a timestamp column is whenever you put a value in, it tries to help you out. It's not, it's not very helpful. It tries to help you out by converting that value to UTC. And then when you read that value back out, it tries to convert it to whatever your time zone is.
And that can be really surprising. With a date time column, it doesn't touch time zones at all. If you put a value in, you're gonna get a value back no matter what. You're gonna get that exact same value. It'll-- it will never convert for you. So if we look at this, we'll see that we have both of them set to 00:00, right? But if I explicitly change my time zone to, to be UTC, so I'm gonna change that to UTC and read this again, you'll see that this one, the timestamp
changed to be six hours off because when I inserted it, my connection was set to negative six. So if I change this to negative six, which is, uh, Central Time in the United States, and then I read it back out, you'll see that it comes back out as 00. And so that's something that you have to be aware of.
Hopefully, your application framework or whatever you're using to connect to the database is set to UTC. But that is something that you definitely need to be aware of is, is that MySQL is gonna muck with your time zones. And if you weren't expecting it, it's gonna be pretty frustrating. The 2038 problem, the epocalypse, time zones, timestamps, date times, there's a lot to remember, right?
But now you know what the 2038 problem is and find your neighborhood grandpa and talk to him about Y2K, and you guys can bond over 2038 and Y2K. Kind of the same, kind of different. If you liked this, I've got a full free MySQL course. It's 64 videos, seven and a half hours long, and it's just me talking about MySQL the entire time.
You can find that at planetscale.com/mysql. If you don't wanna go over there, stay tuned to this channel. We're gonna be putting out videos every couple of days. And I read every single comment. So if you get a, a funny reply, it's from me. If you get a lame reply, it's probably from somebody else. But I read every single comment, so please leave a comment.
Tell me what you liked, what you also liked, um, and what you wanna see next. And hopefully I'll talk to you in a few days. See ya.