Enter your email below to watch this video
The course "Mastering Postgres" helped me practice query commands and understand why they're important. Inspiring me to explore the 'why' and 'how' behind each one. Truly a great course!Nakolus
Shorten dev cycles with branching and zero-downtime schema migrations.
This one is going to be maybe the easiest one, we're talking about Booleans. You may be thinking, yeah, it's just true/false. You're right, it is just true/false, there are some interesting things about it, if you can believe that. Some other databases have a Boolean type that is actually an alias to satisfy the SQL standard. Under the hood, they just use a tiny integer. Not with Postgres. As we talked about in the beginning, Postgres has a data type for everything, including Boolean.
Let's take a look. The status here, we're going to make it as a BOOLEAN. That's it, we're just going to say BOOLEAN. You could declare it as null or not null. We haven't talked too much about nulls yet, null does represent an unknown state, you could have true, false, or unknown. That's not the interesting part, that you kind of expect. This, I think, is the interesting part to me. Postgres will gladly accept, obviously, true false, then it will accept a string of t or a string of true. It will, let's just do both, why not? Let's do this then we'll say t or true, f or false. It will accept one or zero, on or off, yes or no, null.
If we run that then we read it back, select * from boolean_example, we do see that they have all been converted. They've all been converted to their Boolean representations, which is, it is quite nice. Another thing that is quite nice about Boolean, if we did select one cast boolean, explicit cast, you don't have to do the strings. If we were to come back here do that, that doesn't actually work. If you explicitly cast it to a Boolean, you can cast a string of one you can just use an integer of one. If you are inserting it, you can't just insert an integer of one.
Coming back to where we were, let's just clear it. Select one cast boolean, explicit cast of an integer totally works explicit cast of a string totally works. That's fine. We can also, of course cast as an integer, which makes enough sense. If we were to do pg_typeof, you would get the thing that you expect. You would get boolean here you would get integer here. The thing I want to show you is pg_column_size. That's the good stuff right there.
A Boolean type in Postgres is one singular byte. It's very, very, very compact. Use the type that represents your data most correctly. Even if you casted this. Casted? Even if you cast this to a smallint, or another name is int2, you're still twice as large you still have a range of up to 32,000, which you don't actually need. You just need zero or one. You use Boolean for that case. Where is the instance you might not want to use Boolean? If you have more than zero, one, or unknown, or you're doing some actual bitwise operations, there's a better type for that.
If you're trying to store true, false, yes, no, than use a Boolean you can pass in many different input types that will get coerced into Boolean. True false being the best by far, because those are Boolean already.