Enum support functions

Updated at:

PolarDB for PostgreSQL (Compatible with Oracle) provides built-in functions for working with enum types. These functions let you query enum boundaries and ranges without hard-coding specific enum values.

The following examples use this enum type:

CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'purple');

Enum support functions

FunctionDescriptionExample
enum_first(anyenum) → anyenumReturns the first value of the input enum type.enum_first(null::rainbow) → red
enum_last(anyenum) → anyenumReturns the last value of the input enum type.enum_last(null::rainbow) → purple
enum_range(anyenum) → anyarrayReturns all values of the input enum type in an ordered array.enum_range(null::rainbow) → {red,orange,yellow,green,blue,purple}
enum_range(anyenum, anyenum) → anyarrayReturns the range between two enum values as an ordered array. Both values must be from the same enum type. Pass null as the first argument to start from the first value of the type, or as the second argument to end at the last value.enum_range('orange'::rainbow, 'green'::rainbow) → {orange,yellow,green} enum_range(NULL, 'green'::rainbow) → {red,orange,yellow,green} enum_range('orange'::rainbow, NULL) → {orange,yellow,green,blue,purple}
Except for the two-argument form of enum_range, these functions disregard the specific value passed to them — they only examine the declared data type. Passing null or any value of the same type produces the same result. In practice, apply these functions to a table column or function argument rather than a hard-coded type name.