Sub topic 8
Domains, NULL, and NOT NULL
This chapter assumes you know basic CREATE TABLE, INSERT, SELECT, CHECK, and NOT NULL syntax. It focuses on how PostgreSQL domains handle missing values.
A domain is a named type built on an existing base type such as text, integer, or numeric. A domain can attach reusable rules to that base type. For example, an email domain can require a simple email format wherever the domain is used.
A domain constraint is checked when a value enters the domain
Consider this domain:
CREATE DOMAIN short_code AS text
CHECK (length(VALUE) = 4);Inside a domain constraint, VALUE means the value being checked. The following values behave differently:
SELECT 'ABCD'::short_code;
SELECT 'ABC'::short_code;
SELECT NULL::short_code;The first expression succeeds because the text has four characters. The second fails because it has only three characters. The third succeeds and returns NULL.
The important rule is that a CHECK constraint passes when its expression returns either true or NULL. It fails only when the expression returns false.
For the NULL input, length(VALUE) is also NULL. The comparison NULL = 4 is unknown, represented as NULL in this context. Therefore, the domain check does not reject the value.
Domain NOT NULL rejects NULL
Add NOT NULL to the domain when every use of that domain must contain a value:
CREATE DOMAIN non_negative_money AS numeric(12, 2)
DEFAULT 0
NOT NULL
CONSTRAINT non_negative_money_check CHECK (VALUE >= 0);This domain has three rules:
- Its base type is
numeric(12, 2). - An omitted value uses
0as the default. - A supplied value cannot be
NULLor negative.
The domain rejects a null cast:
SELECT NULL::non_negative_money;It also rejects a null value in a table column:
CREATE TABLE accounts (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
balance non_negative_money
);
INSERT INTO accounts DEFAULT VALUES;
INSERT INTO accounts (balance) VALUES (NULL);The first insert uses the domain default and stores 0. The second insert fails because an explicit NULL violates the domain's NOT NULL rule.
The distinction between an omitted value and an explicit NULL matters. A default can supply a value when no value is provided. It does not make an explicitly supplied NULL valid.
Domain NOT NULL versus column NOT NULL
You can place NOT NULL on the domain, on a table column, or both.
| Declaration | Effect |
|---|---|
| `email email_address` | The column may contain `NULL` unless the domain itself rejects it |
| `email email_address NOT NULL` | This particular column rejects `NULL` |
| `CREATE DOMAIN ... NOT NULL` | Every column using the domain rejects `NULL` |
A column-level rule is local. A domain-level rule is reusable and applies everywhere the domain is used.
For example:
CREATE DOMAIN email_address AS text
CHECK (VALUE ~* '^[^@[:space:]]+@[^@[:space:]]+$');
CREATE TABLE customers (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
primary_email email_address NOT NULL,
backup_email email_address
);primary_email cannot be null because the column declares NOT NULL. backup_email can be null because its column does not declare NOT NULL, and the domain itself is nullable.
Both columns still apply the email format check to non-null values. A null backup_email does not fail the check because the check expression evaluates to NULL, not false.
How the rules are applied
When PostgreSQL assigns a value to a domain column, it applies the domain's rules. This happens after the value is converted to the domain type.
A CHECK rule can validate non-null values, while NOT NULL handles the separate question of whether a value must exist at all. Keeping these responsibilities separate makes the rules easier to understand.
For a required, non-negative amount, use both:
CREATE DOMAIN required_quantity AS integer
NOT NULL
CONSTRAINT required_quantity_non_negative
CHECK (VALUE >= 0);This rejects NULL, -1, and other values that fail the base type or check constraint. It accepts 0 and positive integers.
Changing domain rules on a live system
Suppose existing data may contain long email addresses, but new addresses must be no longer than 254 characters. You can add the rule without immediately scanning all existing values:
ALTER DOMAIN email_address
ADD CONSTRAINT email_address_max_length
CHECK (length(VALUE) <= 254) NOT VALID;NOT VALID means PostgreSQL does not check existing values during the ALTER DOMAIN command. The new rule still applies to new values and to values updated after the rule is added.
Later, validate the existing data:
ALTER DOMAIN email_address
VALIDATE CONSTRAINT email_address_max_length;Validation fails if any existing column using email_address contains a value that violates the rule. Fix those rows, then run the validation again.
This two-step process separates enforcement for new writes from the potentially longer cleanup of old data.
Exercise
A domain is defined as CREATE DOMAIN positive_id AS integer CHECK (VALUE > 0);. Will SELECT NULL::positive_id; fail? How would you make NULL fail too?
Show answer
It will succeed because the CHECK expression evaluates to NULL, and a CHECK passes for NULL. Define the domain with NOT NULL, for example CREATE DOMAIN positive_id AS integer NOT NULL CHECK (VALUE > 0);.