Sub topic 3
Listing Enum Values with `enum_range`, `enum_first`, and `enum_last`
This chapter assumes that you know basic CREATE TABLE, SELECT, and WHERE syntax, and that you already understand PostgreSQL enum types. An enum is a type whose values come from a fixed, ordered list. The functions in this chapter help you inspect that list and use its declared order in queries.
Create an enum to inspect
The examples use an order-status enum. The labels are declared in business order:
CREATE TYPE order_status AS ENUM (
'pending',
'paid',
'shipped',
'delivered',
'cancelled'
);
CREATE TABLE orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
status order_status NOT NULL DEFAULT 'pending'
);
INSERT INTO orders (status)
VALUES
('paid'),
('shipped'),
('delivered');The order of the labels is part of the enum definition. PostgreSQL does not sort these values alphabetically. It stores each label as one of the enum's ordered values.
Get every value with `enum_range`
Call enum_range with one typed NULL argument to retrieve every value in the enum:
SELECT enum_range(NULL::order_status);The result is one PostgreSQL array:
{pending,paid,shipped,delivered,cancelled}The cast on NULL is important. PostgreSQL needs the cast to determine which enum type to inspect. An untyped NULL does not identify an enum.
You can give the result a clearer column name:
SELECT enum_range(NULL::order_status) AS all_statuses;If an application needs one row per label instead of one array, expand the array with unnest:
SELECT status
FROM unnest(enum_range(NULL::order_status)) AS status;The result is ordered according to the enum declaration:
| status |
|---|
| pending |
| paid |
| shipped |
| delivered |
| cancelled |
This is useful for generating a menu, checking a migration, or comparing the database definition with application configuration.
Get part of the enum with `enum_range`
The two-argument form returns an inclusive range between two enum values:
SELECT enum_range(
'paid'::order_status,
'delivered'::order_status
) AS delivery_path;The result is:
{paid,shipped,delivered}Both endpoints are included. The function follows the enum's declared order, so the range is meaningful even though the labels are text.
Either endpoint can be NULL. A NULL first endpoint means “start at the first enum value”:
SELECT enum_range(
NULL::order_status,
'shipped'::order_status
) AS early_statuses;This returns:
{pending,paid,shipped}A NULL second endpoint means “continue through the last enum value”:
SELECT enum_range(
'shipped'::order_status,
NULL::order_status
) AS later_statuses;This returns:
{shipped,delivered,cancelled}These calls use NULL as a boundary instruction. They do not mean that NULL is an enum label. The enum still contains only the five declared labels.
You can use a range in a query with ANY:
SELECT id, status
FROM orders
WHERE status = ANY (
enum_range(
'paid'::order_status,
'delivered'::order_status
)
)
ORDER BY id;This selects orders whose status is paid, shipped, or delivered.
Get the first and last values
enum_first returns the first label in an enum, and enum_last returns the last label. Both functions need a typed argument so PostgreSQL knows which enum type to inspect:
SELECT
enum_first(NULL::order_status) AS first_status,
enum_last(NULL::order_status) AS last_status;The result is:
| first_status | last_status |
|---|---|
| pending | cancelled |
The argument is normally written as typed NULL because the function needs the enum type, not a particular row value. These functions are useful when a process must identify the initial or terminal state without repeating the labels in application code.
For example, this query finds orders at or after the shipped stage and before the final enum value:
SELECT id, status
FROM orders
WHERE status >= 'shipped'::order_status
AND status < enum_last(NULL::order_status)
ORDER BY status, id;The comparison uses enum order. It does not compare the labels alphabetically.
Visualize the inspection workflow
Common errors
- Calling
enum_range(NULL)without a cast leaves PostgreSQL unable to infer the enum type. - Assuming the result is alphabetic can produce incorrect reports or filters.
- Forgetting that the two-argument range includes both endpoints can select one extra status.
- Treating the result of
enum_rangeas rows instead of an array requiresunnestwhen one label per row is needed. - Assuming these functions inspect table data is incorrect. They inspect the type definition, including labels that no row currently uses.
Exercise
Write a query that returns one row for every order_status label from pending through delivered, including both endpoints.
Show answer
SELECT status FROM unnest(enumrange('pending'::orderstatus, 'delivered'::order_status)) AS status;