2 8.7. Enumerated Types #
4 8.7.1. Declaration of Enumerated Types
7 8.7.4. Implementation Details
9 Enumerated (enum) types are data types that comprise a static, ordered
10 set of values. They are equivalent to the enum types supported in a
11 number of programming languages. An example of an enum type might be
12 the days of the week, or a set of status values for a piece of data.
14 8.7.1. Declaration of Enumerated Types #
16 Enum types are created using the CREATE TYPE command, for example:
17 CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
19 Once created, the enum type can be used in table and function
20 definitions much like any other type:
21 CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
26 INSERT INTO person VALUES ('Moe', 'happy');
27 SELECT * FROM person WHERE current_mood = 'happy';
35 The ordering of the values in an enum type is the order in which the
36 values were listed when the type was created. All standard comparison
37 operators and related aggregate functions are supported for enums. For
39 INSERT INTO person VALUES ('Larry', 'sad');
40 INSERT INTO person VALUES ('Curly', 'ok');
41 SELECT * FROM person WHERE current_mood > 'sad';
43 -------+--------------
48 SELECT * FROM person WHERE current_mood > 'sad' ORDER BY current_mood;
50 -------+--------------
57 WHERE current_mood = (SELECT MIN(current_mood) FROM person);
65 Each enumerated data type is separate and cannot be compared with other
66 enumerated types. See this example:
67 CREATE TYPE happiness AS ENUM ('happy', 'very happy', 'ecstatic');
68 CREATE TABLE holidays (
72 INSERT INTO holidays(num_weeks,happiness) VALUES (4, 'happy');
73 INSERT INTO holidays(num_weeks,happiness) VALUES (6, 'very happy');
74 INSERT INTO holidays(num_weeks,happiness) VALUES (8, 'ecstatic');
75 INSERT INTO holidays(num_weeks,happiness) VALUES (2, 'sad');
76 ERROR: invalid input value for enum happiness: "sad"
77 SELECT person.name, holidays.num_weeks FROM person, holidays
78 WHERE person.current_mood = holidays.happiness;
79 ERROR: operator does not exist: mood = happiness
81 If you really need to do something like that, you can either write a
82 custom operator or add explicit casts to your query:
83 SELECT person.name, holidays.num_weeks FROM person, holidays
84 WHERE person.current_mood::text = holidays.happiness::text;
91 8.7.4. Implementation Details #
93 Enum labels are case sensitive, so 'happy' is not the same as 'HAPPY'.
94 White space in the labels is significant too.
96 Although enum types are primarily intended for static sets of values,
97 there is support for adding new values to an existing enum type, and
98 for renaming values (see ALTER TYPE). Existing values cannot be removed
99 from an enum type, nor can the sort ordering of such values be changed,
100 short of dropping and re-creating the enum type.
102 An enum value occupies four bytes on disk. The length of an enum
103 value's textual label is limited by the NAMEDATALEN setting compiled
104 into PostgreSQL; in standard builds this means at most 63 bytes.
106 The translations from internal enum values to textual labels are kept
107 in the system catalog pg_enum. Querying this catalog directly can be