skip to content

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.

xx = truex IS TRUEx IS NOT TRUENOT xx IS NOT FALSE
tttfft
fffttf
nullnullftnullt
(3 rows)
real output, postgres 18.6 (PGlite 0.5.12) · booleans-enums-domains
a boolean's three values under five tests, each answer postgres's own. the NULL row is the one to read: comparisons pass the unknown along, IS turns it into a definite true or false.

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:

sqlempty db
SELECT 'yes'::boolean AS yes, 'off'::boolean AS off, '1'::boolean AS one, pg_column_size(true) AS bytes;
output, from postgres 18.6
SELECT 1
yesbooloffbooloneboolbytesint4
tft1
(1 row)

anything else is an error, not a guess:

sqlempty db
SELECT 'maybe'::boolean;
output, from postgres 18.6
ERROR 22P02: invalid input syntax for type boolean: "maybe"
1 │ SELECT 'maybe'::boolean;
at line 1, column 8

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):

predict the output
sql
SELECT count(*)
FROM (VALUES (true), (false), (NULL)) AS t (paid)
WHERE paid IS NOT TRUE;
sqlempty db
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);
output, from postgres 18.6
SELECT 1
not_paidint8is_not_trueint8
12
(1 row)

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:

sqlempty db
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;
output, from postgres 18.6
#1 · CREATE TYPE
#2 · SELECT 1
in_orderboolas_textboolall_labelsorder_status[]
tt{placed,paid,shipped,delivered}
(1 row)

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:

order_status =
enum.sql
  1. 1CREATE TYPE order_status AS ENUM ('placed', 'paid', 'shipped', 'delivered');
  2. 2SELECT enumlabel AS label, enumsortorder AS sort_position
  3. 3FROM pg_enum WHERE enumtypid = 'order_status'::regtype
  4. 4ORDER BY enumsortorder;
the result for as created
labelnamesort_positionfloat4
placed1
paid2
shipped3
delivered4
(4 rows)

an enum with four labels. each gets a sort position, in the order they were written.

real output, postgres 18.6 (PGlite 0.5.12) · booleans-enums-domains
pg_enum for the type, before and after three ADD VALUEs. switch between them. a label added in the middle gets a fractional position, 2.5, so no existing value has to change.

what you can't do is take one away. there's no syntax for it that works:

sqlempty db
CREATE TYPE mood AS ENUM ('happy', 'meh', 'grumpy');
ALTER TYPE mood DROP VALUE 'meh';
output, from postgres 18.6
#1 · CREATE TYPE
#2 · ERROR 0A000
ERROR 0A000: dropping an enum value is not implemented
2 │ ALTER TYPE mood DROP VALUE 'meh';
at line 2, column 17

"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:

sqlempty db
CREATE TYPE mood AS ENUM ('happy', 'grumpy');
BEGIN;
ALTER TYPE mood ADD VALUE 'meh';
SELECT 'meh'::mood;
output, from postgres 18.6
#1 · CREATE TYPE
#2 · BEGIN
#3 · ALTER TYPE
#4 · ERROR 55P04
ERROR 55P04: unsafe use of new value "meh" of enum type mood
4 │ SELECT 'meh'::mood;
at line 4, column 8
HINT: New enum values must be committed before they can be used.

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:

sqlempty db
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;
output, from postgres 18.6
#1 · CREATE DOMAIN
#2 · CREATE DOMAIN
#3 · SELECT 1
oktexttyperegtype
ada@example.comemail
(1 row)

the check runs whenever a value becomes the domain type, wherever that happens:

via =
domain.sql
  1. 1SELECT 'not an email'::email;
ERROR: value for domain email violates check constraint "email_check"SQLSTATE 23514

a cast to the domain checks the value.

real output, postgres 18.6 (PGlite 0.5.12) · booleans-enums-domains
five ways to hand a domain the string '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:

sqlempty db
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));
output, from postgres 18.6
#1 · CREATE DOMAIN
#2 · CREATE TABLE
#3 · INSERT 0 1
#4 · ERROR 23514
ERROR 23514: column "address" of table "subscribers" contains values that violate the new constraint

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.