booleans, enums and domains
a boolean with three values, enums you can grow but not shrink, and domains as reusable rules.
- address
- everything.types.booleans_enums_domains
- part
- part 6, types and values
- chapter
- chapter 6 of 11
- reading time
- 7 min
a boolean has three values: true, false and NULL. WHERE flag = true and WHERE flag IS TRUE agree, but their opposites don't: IS NOT TRUE keeps the NULLs, <> true drops them.
an enum is a fixed list of labels that sorts in the order you wrote it. you can add labels, even in the middle, but never drop one. a domain is a type plus rules, checked every time a value becomes that type.
the picture
you know how a checkbox on a form is either ticked or not? a boolean column isn't a checkbox. it's a checkbox that can also be missing, and the missing one answers "unknown" to every question you ask with =.
think of a light switch with a third position: no switch at all (NULL). "is it on?" gets yes, no, or a shrug. only questions phrased with IS get a straight answer out of the empty wall plate.
| x | x = true | x IS TRUE | x IS NOT TRUE | NOT x | x IS NOT FALSE |
|---|---|---|---|---|---|
| t | t | t | f | f | t |
| f | f | f | t | t | f |
| null | null | f | t | null | t |
booleans
a boolean is one byte, and postgres accepts a few spellings of it on the way in: true/false, 't'/'f', 'yes'/'no', 'on'/'off', '1'/'0'. it always prints t or f:
SELECT 'yes'::boolean AS yes, 'off'::boolean AS off, '1'::boolean AS one, pg_column_size(true) AS bytes;| yesbool | offbool | onebool | bytesint4 |
|---|---|---|---|
| t | f | t | 1 |
anything else is an error, not a guess:
SELECT 'maybe'::boolean;in WHERE, write the column alone: WHERE paid, WHERE NOT paid. = true adds nothing. the trouble starts with the negative, because WHERE keeps only true, and NOT unknown is still unknown (NULL and three-valued logic has the whole story):
SELECT count(*)
FROM (VALUES (true), (false), (NULL)) AS t (paid)
WHERE paid IS NOT TRUE;SELECT count(*) FILTER (WHERE NOT paid) AS not_paid,
count(*) FILTER (WHERE paid IS NOT TRUE) AS is_not_true
FROM (VALUES (true), (false), (NULL)) AS t (paid);| not_paidint8 | is_not_trueint8 |
|---|---|
| 1 | 2 |
the cleanest fix is upstream: paid boolean NOT NULL DEFAULT false. a flag that can't be NULL has two values, like you meant.
enums
an enum is a type whose values are a list of labels:
CREATE TYPE order_status AS ENUM ('placed', 'paid', 'shipped', 'delivered');
SELECT 'paid'::order_status < 'shipped'::order_status AS in_order,
'paid' < 'shipped' AS as_text,
enum_range(NULL::order_status) AS all_labels;| in_orderbool | as_textbool | all_labelsorder_status[] |
|---|---|---|
| t | t | {placed,paid,shipped,delivered} |
the enum compares by its list order: paid before shipped. as text, 'paid' < 'shipped' happens to agree, but 'delivered' would sort first alphabetically and last as a status. a label that isn't on the list is refused, and each value takes 4 bytes however long its label is.
you grow an enum with ALTER TYPE ... ADD VALUE, at the end or BEFORE/AFTER an existing label. postgres doesn't renumber anything; the new label gets a sort position between its neighbours:
- 1CREATE TYPE order_status AS ENUM ('placed', 'paid', 'shipped', 'delivered');
- 2SELECT enumlabel AS label, enumsortorder AS sort_position
- 3FROM pg_enum WHERE enumtypid = 'order_status'::regtype
- 4ORDER BY enumsortorder;
| labelname | sort_positionfloat4 |
|---|---|
| placed | 1 |
| paid | 2 |
| shipped | 3 |
| delivered | 4 |
an enum with four labels. each gets a sort position, in the order they were written.
what you can't do is take one away. there's no syntax for it that works:
CREATE TYPE mood AS ENUM ('happy', 'meh', 'grumpy');
ALTER TYPE mood DROP VALUE 'meh';"not implemented", and the manual says it plainly: removing a label, or reordering labels, means dropping and recreating the type. you can rename one (RENAME VALUE). and a value added inside a transaction can't be used until that transaction commits:
CREATE TYPE mood AS ENUM ('happy', 'grumpy');
BEGIN;
ALTER TYPE mood ADD VALUE 'meh';
SELECT 'meh'::mood;transaction aborted: ROLLBACK to continue
domains
a domain is an existing type with constraints attached, given a name, so the rule lives in one place instead of on every column:
CREATE DOMAIN email AS text CHECK (VALUE ~ '^[^@\s]+@[^@\s]+\.[^@\s]+$');
CREATE DOMAIN qty AS int NOT NULL CHECK (VALUE > 0);
SELECT 'ada@example.com'::email AS ok, pg_typeof('ada@example.com'::email) AS type;| oktext | typeregtype |
|---|---|
| ada@example.com |
the check runs whenever a value becomes the domain type, wherever that happens:
- 1SELECT 'not an email'::email;
a cast to the domain checks the value.
'not an email' (and one NULL). pick one. every path into the type runs the CHECK; a NULL passes, because a CHECK only refuses false.arithmetic or functions on a domain value give you the base type back (address || '!' is plain text), so the rule is checked on the way in, not carried along. add a constraint later and postgres checks every existing column of that domain first, unless you say NOT VALID:
CREATE DOMAIN email AS text CHECK (VALUE ~ '@');
CREATE TABLE subscribers (address email);
INSERT INTO subscribers VALUES ('Ada@Example.com');
ALTER DOMAIN email ADD CONSTRAINT lowercase CHECK (VALUE = lower(VALUE));keylines
- a boolean is true, false or NULL. IS TRUE and IS NOT TRUE always answer; = and NOT pass NULL along.
- make flags NOT NULL with a default, and the third value goes away.
- an enum sorts in declaration order. you can add labels anywhere, never drop them.
- a domain is a type plus constraints, checked whenever a value becomes that type.
- a CHECK lets NULL through, and a NOT NULL domain can be bypassed; NOT NULL belongs on the column.