Sub topic 16
Casting Enums and Domains
This chapter assumes you know SELECT, INSERT, CREATE TABLE, and common PostgreSQL types. You should also understand that PostgreSQL checks a value's type before it chooses operators and functions.
Enums and domains are user-defined types, but they behave differently:
- An enum is a fixed list of named values.
- A domain is an existing type with optional constraints.
Casting enum values
Create an enum with CREATE TYPE:
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
CREATE TABLE profiles (
username text,
current_mood mood
);
INSERT INTO profiles VALUES
('Ada', 'happy'),
('Lin', 'ok');Enum labels are values of the enum type, not ordinary text. PostgreSQL compares enum values according to their declaration order:
SELECT 'sad'::mood < 'happy'::mood AS declared_order;The result is true. This ordering is not alphabetical.
To convert an enum to text, use an explicit cast:
SELECT current_mood::text
FROM profiles
WHERE username = 'Ada';To convert text to the enum, the text must match a label exactly:
SELECT 'happy'::mood AS converted_mood;An invalid label raises an error:
SELECT 'excited'::mood;Enum values are not automatically interchangeable with text in every expression. Cast explicitly when calling a text function, comparing with text, or storing a label outside the enum column.
Casting domains
A domain keeps the behavior of a base type while adding constraints:
CREATE DOMAIN positive_int AS integer
CHECK (VALUE > 0);
CREATE TABLE inventory (
item_name text,
quantity positive_int
);
INSERT INTO inventory VALUES ('notebook', 12);Casting a value to the domain checks its constraints:
SELECT 12::positive_int AS valid_quantity;This fails because the value does not satisfy the check:
SELECT 0::positive_int;A domain does not create a new storage format. positive_int still stores an integer, but PostgreSQL validates the value whenever it is converted to or assigned to the domain. Casting a domain value back to its base type removes the domain wrapper:
SELECT
12::positive_int::integer AS base_value,
pg_typeof(12::positive_int::integer) AS base_type;Operators usually work with the base type after domain values enter an expression:
SELECT
12::positive_int + 3 AS total,
pg_typeof(12::positive_int + 3) AS result_type;The result is an integer, not a positive_int. If the result must satisfy the domain constraint, cast it back:
SELECT (12::positive_int + 3)::positive_int AS checked_total;Enum and domain decisions
| Requirement | Suitable type | Main consideration |
|---|---|---|
| Small, stable named set | Enum | Labels and ordering are schema-defined |
| Base type with validation | Domain | Constraints run on assignment or casting |
| Frequently changing values with attributes | Lookup table | Rows can hold metadata and relationships |
The conversion path is easier to visualize:
Exercise
What happens when PostgreSQL evaluates 0::positive_int, and how can you obtain the underlying integer from a valid domain value?
Show answer
The cast raises a domain check-constraint error because zero is not greater than zero. A valid domain value can be converted with an explicit cast such as 12::positive_int::integer.