Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the size of the enum PostgreSQL type?

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.

like image 819
jaumevn Avatar asked Dec 04 '13 14:12

jaumevn


People also ask

How do you find the enum size?

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 .

Does enum have size?

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.

Should you use enum in PostgreSQL?

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.


1 Answers

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=# 
like image 66
ckruse Avatar answered Oct 11 '22 09:10

ckruse