Bit strings are strings of 1's and 0's. They can be used to store or visualize bit masks. There are two SQL bit types: BIT(x) and BIT VARYING(x); where x is a positive integer.
BIT type data must match the length x exactly; it is an error to attempt to store shorter or longer bit strings. BIT VARYING is of variable length up to the maximum length x; longer strings will be rejected. BIT without length is equivalent to BIT(1), BIT VARYING without length specification means unlimited length.
Note: Prior to PostgreSQL 7.2, BIT type data was zero-padded on the right. This was changed to comply with the SQL standard. To implement zero-padded bit strings, a combination of the concatenation operator and the
substringfunction can be used.
In certain circumstances, aggregate versions of bitwise operations are useful. These are missing from the PostgreSQL distribution (as of 7.2), but you can trivially create aggregates for and, or, and xor as follows:
create aggregate bitor (
basetype = bit,
sfunc = bitor,
stype = bit
I find this to be very handy when representing permissions vectors as bit vectors.