Christoph Schiessl sorts things out:
Custom
ENUM
types in PostgreSQL are an excellent tool for enforcing certain database constraints, but you must be careful if you useSELECT
queries and want toORDER BY
these columns. Recently, I had to fix a bug whose root cause was a misunderstanding of this behavior. It’s just a contrived example, but imagine a table of people with their marital status, which is implemented as a customENUM
type.
Read on to learn more about the misunderstanding and some of the unexpected trickiness involved in getting a good query plan.