I am working with a table in a PostgreSQL database that has several boolean columns that determine some state (e.g. published
, visible
, etc.). I want t
You can apply the bit string functions directly to a bit string without the need to cast from an integer.
If you only have a few variables I would consider keeping separate boolean columns.
boolean
columns allow NULL
values for individual bits if you should need that. You can always define columns NOT NULL
if you don't.If you have more than a hand full variables but no more than 32, an integer column may serve best. (Or a bigint
for up to 64 variables.)
=
operator).varbit
or boolean
.With even more variables, or if you want to manipulate the values a lot, or if you don't have huge tables or disk space / RAM is not an issue, or if you are not sure what to pick, I would consider bit(n) or bit varying(n) (short: varbit(n).
For just 3 bits of information, individual boolean
columns get by with 3 bytes, an integer
needs 4 bytes (maybe additional alignment padding) and a bit string
6 bytes (5 + 1).
For 32 bits of information, an integer
still needs 4 bytes (+ padding), a bit string
occupies 9 bytes for the same (5 + 4) and boolean
columns occupy 32 bytes.
To optimize disk space further you need to understand the storage mechanisms of PostgreSQL, especially data alignment. More in this related answer.
This answer on how to transform the types boolean, bit(n) and integer may be of help, too.