Reputation: 4852
Where I work we use a postgres database (8.3 soon to migrate to 8.4). There is a small debate currently on the use of enums in the database. Personally I do not like the db enum type. Among other things it puts application logic in the database and creates a posibility for a mismatch between code and data.
I was wondering what exactly are the advantages of postgres enums (besides readablity) and what are the disadvatages?
Upvotes: 57
Views: 45064
Reputation: 509
I prefer a text field with a foreign key to either an ENUM field or a text field without a foreign key.
Advantages of a text field with a foreign key:
Disadvantages:
Example:
create table example_table_example_type (
example_type text primary key
);
create table example_table (
example_type text not null references example_table_example_type(example_type),
...other fields...
);
Upvotes: 1
Reputation: 520
In PostgreSQL 13, btree indexes now support deduplication. If we take the following real-world example of using an ENUM to represent HTTP methods in a log table with 100 million rows:
public | test_http_enum_idx | index | postgres | test | permanent | 789 MB |
public | test_http_test_idx | index | postgres | test | permanent | 789 MB |
We can see that the index size for both are the same. For a denormalized table, saving a few bytes per row doesn't really make up for the disadvantages.
Rule of thumb for PG 13+: Use ENUMs to constrain a column to a fixed/static set of values; do not to use them to save on disk space.
Possible exception: If an ENUM of static values will help you avoid a costly JOIN or a FK --- go for it; just make sure to avoid premature optimization and measure your results in production.
When making your decision, consider that popular BI tools like Metabase did not support filtering on an ENUM, however, they'll work just fine on a TEXT column. @solaris: Reports that Metabase 0.42.1 and higher support filtering on ENUM values.
Upvotes: 14
Reputation: 3063
Reduce Storage: Postgres uses only 1 byte per tuple when 255 or less ENUM elements are defined or 2 bytes for 256~65535 elements. This is because, rather that storing the constant literal of the value, Postgres stores the index in the ordered set of that value. For very large tables, this might prove to be a significant storage space save.
Arbitrary sorting:
CREATE TABLE opening_hours(
week_day ENUM ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'),
opening_time TIME,
closing_time TIME
);
If you sort by week_day it will sort in the order you specified which is convenient in the above case.
Upvotes: 1
Reputation: 13
Point is, if applications are allowed to do DDL, they are more likely to cause blocking or conflict. DDL is best done offline i.e. in single-user mode.
Upvotes: -4
Reputation: 18000
As advantage you have also DB checking, that nothing else enum value couldn't be recorded in column. The big disadvantage for me was, that enum could be modified only by adding value to the end, but since Postgres 9.1 it is in the past: https://stackoverflow.com/a/7834949/548473
Upvotes: 5
Reputation: 5609
The advantages of enums are:
The disadvantages are:
Upvotes: 76
Reputation: 2005
Enums combine the advantages of ints with the advantages of strings: they are small and fast like ints, readable like strings, and have the additional advantage of being safe (you can't mis-spell an enum).
However, if you don't care about readability, an int is as good as an enum.
Upvotes: 11