4 CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'purple');
6 -- Did it create the right number of rows?
8 SELECT COUNT(*) FROM pg_enum WHERE enumtypid = 'rainbow'::regtype;
17 SELECT 'red'::rainbow;
23 SELECT 'mauve'::rainbow;
24 ERROR: invalid input value for enum rainbow: "mauve"
25 LINE 1: SELECT 'mauve'::rainbow;
28 -- Basic table creation, row selection
30 CREATE TABLE enumtest (col rainbow);
31 INSERT INTO enumtest values ('red'), ('orange'), ('yellow'), ('green');
32 COPY enumtest FROM stdin;
33 SELECT * FROM enumtest;
45 -- Operators, no index
47 SELECT * FROM enumtest WHERE col = 'orange';
53 SELECT * FROM enumtest WHERE col <> 'orange' ORDER BY col;
63 SELECT * FROM enumtest WHERE col > 'yellow' ORDER BY col;
71 SELECT * FROM enumtest WHERE col >= 'yellow' ORDER BY col;
80 SELECT * FROM enumtest WHERE col < 'green' ORDER BY col;
88 SELECT * FROM enumtest WHERE col <= 'green' ORDER BY col;
100 SELECT 'red'::rainbow::text || 'hithere';
106 SELECT 'red'::text::rainbow = 'red'::rainbow;
115 SELECT min(col) FROM enumtest;
121 SELECT max(col) FROM enumtest;
127 SELECT max(col) FROM enumtest WHERE col < 'green';
134 -- Index tests, force use of index
136 SET enable_seqscan = off;
137 SET enable_bitmapscan = off;
139 -- Btree index / opclass with the various operators
141 CREATE UNIQUE INDEX enumtest_btree ON enumtest USING btree (col);
142 SELECT * FROM enumtest WHERE col = 'orange';
148 SELECT * FROM enumtest WHERE col <> 'orange' ORDER BY col;
158 SELECT * FROM enumtest WHERE col > 'yellow' ORDER BY col;
166 SELECT * FROM enumtest WHERE col >= 'yellow' ORDER BY col;
175 SELECT * FROM enumtest WHERE col < 'green' ORDER BY col;
183 SELECT * FROM enumtest WHERE col <= 'green' ORDER BY col;
192 SELECT min(col) FROM enumtest;
198 SELECT max(col) FROM enumtest;
204 SELECT max(col) FROM enumtest WHERE col < 'green';
210 DROP INDEX enumtest_btree;
212 -- Hash index / opclass with the = operator
214 CREATE INDEX enumtest_hash ON enumtest USING hash (col);
215 SELECT * FROM enumtest WHERE col = 'orange';
221 DROP INDEX enumtest_hash;
225 RESET enable_seqscan;
226 RESET enable_bitmapscan;
228 -- Domains over enums
230 CREATE DOMAIN rgb AS rainbow CHECK (VALUE IN ('red', 'green', 'blue'));
237 SELECT 'purple'::rgb;
238 ERROR: value for domain rgb violates check constraint "rgb_check"
239 SELECT 'purple'::rainbow::rgb;
240 ERROR: value for domain rgb violates check constraint "rgb_check"
245 SELECT '{red,green,blue}'::rainbow[];
251 SELECT ('{red,green,blue}'::rainbow[])[2];
257 SELECT 'red' = ANY ('{red,green,blue}'::rainbow[]);
263 SELECT 'yellow' = ANY ('{red,green,blue}'::rainbow[]);
269 SELECT 'red' = ALL ('{red,green,blue}'::rainbow[]);
275 SELECT 'red' = ALL ('{red,red}'::rainbow[]);
284 SELECT enum_first(NULL::rainbow);
290 SELECT enum_last('green'::rainbow);
296 SELECT enum_range(NULL::rainbow);
298 ---------------------------------------
299 {red,orange,yellow,green,blue,purple}
302 SELECT enum_range('orange'::rainbow, 'green'::rainbow);
304 -----------------------
305 {orange,yellow,green}
308 SELECT enum_range(NULL, 'green'::rainbow);
310 ---------------------------
311 {red,orange,yellow,green}
314 SELECT enum_range('orange'::rainbow, NULL);
316 -----------------------------------
317 {orange,yellow,green,blue,purple}
320 SELECT enum_range(NULL::rainbow, NULL);
322 ---------------------------------------
323 {red,orange,yellow,green,blue,purple}
327 -- User functions, can't test perl/python etc here since may not be compiled.
329 CREATE FUNCTION echo_me(anyenum) RETURNS text AS $$
331 RETURN $1::text || 'omg';
334 SELECT echo_me('red'::rainbow);
341 -- Concrete function should override generic one
343 CREATE FUNCTION echo_me(rainbow) RETURNS text AS $$
345 RETURN $1::text || 'wtf';
348 SELECT echo_me('red'::rainbow);
355 -- If we drop the original generic one, we don't have to qualify the type
356 -- anymore, since there's only one match
358 DROP FUNCTION echo_me(anyenum);
359 SELECT echo_me('red');
365 DROP FUNCTION echo_me(rainbow);
367 -- RI triggers on enum types
369 CREATE TABLE enumtest_parent (id rainbow PRIMARY KEY);
370 NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "enumtest_parent_pkey" for table "enumtest_parent"
371 CREATE TABLE enumtest_child (parent rainbow REFERENCES enumtest_parent);
372 INSERT INTO enumtest_parent VALUES ('red');
373 INSERT INTO enumtest_child VALUES ('red');
374 INSERT INTO enumtest_child VALUES ('blue'); -- fail
375 ERROR: insert or update on table "enumtest_child" violates foreign key constraint "enumtest_child_parent_fkey"
376 DETAIL: Key (parent)=(blue) is not present in table "enumtest_parent".
377 DELETE FROM enumtest_parent; -- fail
378 ERROR: update or delete on table "enumtest_parent" violates foreign key constraint "enumtest_child_parent_fkey" on table "enumtest_child"
379 DETAIL: Key (id)=(red) is still referenced from table "enumtest_child".
381 -- cross-type RI should fail
383 CREATE TYPE bogus AS ENUM('good', 'bad', 'ugly');
384 CREATE TABLE enumtest_bogus_child(parent bogus REFERENCES enumtest_parent);
385 ERROR: foreign key constraint "enumtest_bogus_child_parent_fkey" cannot be implemented
386 DETAIL: Key columns "parent" and "id" are of incompatible types: bogus and rainbow.
391 DROP TABLE enumtest_child;
392 DROP TABLE enumtest_parent;
396 -- Verify properly cleaned up
398 SELECT COUNT(*) FROM pg_type WHERE typname = 'rainbow';
404 SELECT * FROM pg_enum WHERE NOT EXISTS
405 (SELECT 1 FROM pg_type WHERE pg_type.oid = enumtypid);
406 enumtypid | enumlabel
407 -----------+-----------