enum_first
PG 8.3+→ anyenumReturns the first value of the enum type. Pass any value of the enum type (or NULL::enum_type) to specify which enum.
Signature
enum_first ( anyenum ) → anyenumParameters
| Parameter | Type | Description |
|---|---|---|
| anyenum | anyenum | Any value of the enum type (the value itself is ignored — only the type matters) |
Examples
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
SELECT enum_first(NULL::mood);sadSELECT enum_first(status) FROM orders LIMIT 1;First value of the orders.status enum typeSELECT col, enum_first(NULL::priority_level) AS lowest_priority
FROM config;Adds lowest_priority column showing the first enum valueSELECT count(*) FROM tasks
WHERE priority = enum_first(NULL::task_priority);Count of tasks at the lowest priority levelUse `enum_first(NULL::enum_type)` and `enum_last(NULL::enum_type)` to get the boundary values of an enum without hardcoding them. Useful in validation functions that should adapt when the enum is extended.
SELECT * FROM tasks WHERE priority BETWEEN enum_first(NULL::task_priority) AND enum_last(NULL::task_priority);All valid priority values (always true, useful for documentation/type safety)