Sub topic 15
Domains and composite types
Assume you know how to create tables, define constraints, and run SELECT and INSERT statements. This chapter shows how to package reusable rules and related fields into PostgreSQL types.
Domains: reusable rules for one value
A domain is a named type built on an existing PostgreSQL type. It can add constraints that should apply wherever the domain is used. This keeps a rule in one place instead of repeating it in many table definitions.
CREATE DOMAIN positive_quantity AS integer
NOT NULL
CHECK (VALUE > 0);
CREATE TABLE order_lines (
order_id bigint NOT NULL,
quantity positive_quantity NOT NULL
);
INSERT INTO order_lines (order_id, quantity)
VALUES (1001, 3);
-- This fails because the domain constraint rejects zero.
INSERT INTO order_lines (order_id, quantity)
VALUES (1002, 0);Inside a domain constraint, VALUE means the value being checked. A domain can also be based on text, numeric, dates, or other types.
A domain does not create a new storage format. positive_quantity still stores an integer. It communicates the intended meaning and enforces its rule during assignment and conversion.
Domains are useful for rules such as:
- A percentage must be between 0 and 100.
- A quantity must be positive.
- An identifier must match a text pattern.
- A monetary value must not be negative.
Use a table constraint when a rule applies only to one table or depends on multiple columns. Use a domain when the same single-value rule belongs to several tables.
Composite types: related fields as one value
A composite type is a named collection of fields. It resembles a row and lets one column contain a structured value.
CREATE TYPE postal_address AS (
street text,
city text,
postal_code text
);
CREATE TABLE customers (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name text NOT NULL,
shipping_address postal_address NOT NULL
);
INSERT INTO customers (name, shipping_address)
VALUES (
'Ada Lovelace',
ROW('1 Main Street', 'London', 'N1')::postal_address
);
SELECT
name,
(shipping_address).city AS city
FROM customers;The parentheses in (shipping_address).city distinguish the composite column from a table or column qualifier. The ROW constructor creates a row value, and the cast converts it to the named composite type.
The fields inside a composite value can be null independently. shipping_address NOT NULL prevents the entire composite value from being null, but it does not prevent a null city or postal code. Add validation in the surrounding table, use a domain for individual fields, or use a normalized address table when those rules need stronger control.
Choosing between the two
| Need | Suitable feature |
|---|---|
| One base value with a shared constraint | Domain |
| Several fields treated as one value | Composite type |
| Repeated related rows with independent lifecycle | Separate table |
Common errors
- Defining a domain but forgetting that it permits
NULLunless it includesNOT NULL. - Assuming a composite column validates every field automatically.
- Using a composite type where the fields need separate indexes or foreign keys.
- Omitting an explicit cast when constructing a composite value.
Exercise
You need a reusable type for an email address that must contain @. Should you use a domain or a composite type?
Show answer
Use a domain based on text, because an email address is one value with a reusable validation rule.