I would like to know what is the size of an enum variable of PostgreSQL. For instance, if I create an enumeration type A with 100 different items, what would be the size in bytes of it? In addition, when I create a table that contains an attribute of type A, what is the size of this attribute?
I checked the PostgreSQL documentation, but I didn't understand the last part that talk about the enum type sizes.
So the size of an enum type could be the size of an int , char , or any other integer type. As an example, gcc sizes enums as follows: Normally, the type is unsigned int if there are no negative values in the enumeration, otherwise int .
The C standard specifies that enums are integers, but it does not specify the size. Once again, that is up to the people who write the compiler. On an 8-bit processor, enums can be 16-bits wide. On a 32-bit processor they can be 32-bits wide or more or less.
The advantages of enums are: Performance is better. You can just display what you get out of the core table instead of either having a separate lookup table that translates a code to a value or having app logic that translates a code to a value. This can be especially useful in datawarehouse applications.
The size of an enum is 4 byte on disk. Period. This is because enums are implemented as integers or shorts. The label to each integer value is saved in the system catalog pg_enum
. You can have a look at it by simply querying it:
test=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------+---------------+-----------
(0 rows)
test=# create type test_enum_t as enum('a','b','c');
CREATE TYPE
test=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------+---------------+-----------
68850 | 1 | a
68850 | 2 | b
68850 | 3 | c
(3 rows)
test=#
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With