Sub topic 5
Enum storage size and label limits
Assume you know basic CREATE TABLE, INSERT, and SELECT statements, and that you have already seen how PostgreSQL enum values are declared and compared. This chapter focuses on the physical size of enum values and the length limit on their labels.
An enum has two different representations:
- The value stored in a table row is a compact internal value.
- The label, such as
smallordelivered, is stored in PostgreSQL's system catalogs and displayed when the value is selected.
The label is not copied into every table row.
Enum values use four bytes in table rows
PostgreSQL stores an enum value as a four-byte internal value. The row does not contain the full text of the label. This keeps enum columns compact even when their labels are moderately long.
CREATE TYPE delivery_size_demo AS ENUM ('small', 'medium', 'large');
CREATE TABLE parcels (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
size delivery_size_demo NOT NULL
);
INSERT INTO parcels (size)
VALUES ('small'), ('large');
SELECT
id,
size,
pg_column_size(size) AS stored_value_bytes
FROM parcels;The pg_column_size function reports the size of a value in its PostgreSQL storage representation. For these enum values, it normally reports 4.
This result describes the enum datum, not the complete table row. A row also has tuple headers, alignment padding, null-bitmap space, and the storage used by its other columns. An index has its own overhead as well.
The four-byte size has several practical consequences:
- A table with many rows does not repeat the label text in each row.
- Changing a label does not make every row larger or smaller.
- A long label can make queries and user interfaces more verbose, but it does not increase the enum datum from four bytes.
- The number of labels affects catalog metadata and application behavior, not the per-row size of the enum value.
The internal value is still associated with a label in PostgreSQL's catalogs. You can inspect the labels and their ordering through the pg_enum catalog.
SELECT
e.enumlabel,
e.enumsortorder,
length(e.enumlabel) AS characters,
octet_length(e.enumlabel) AS bytes
FROM pg_enum AS e
WHERE e.enumtypid = 'delivery_size_demo'::regtype
ORDER BY e.enumsortorder;The regtype cast resolves the type name to its internal type identifier. The pg_enum catalog contains one row for each label of an enum type.
Enum labels have a byte limit
On a standard PostgreSQL build, an enum label can contain at most 63 bytes. This limit comes from PostgreSQL's NAMEDATALEN setting, whose usual value is 64. The usable limit is NAMEDATALEN - 1.
The limit is measured in bytes, not characters. With an encoding such as UTF-8, one character can use multiple bytes. Therefore, a label containing non-ASCII characters might reach the limit with fewer than 63 visible characters.
Use octet_length when checking the limit:
SELECT
octet_length(repeat('x', 63)) AS sixty_three_bytes,
octet_length(repeat('x', 64)) AS sixty_four_bytes;The first expression produces 63 bytes, while the second produces 64 bytes. A label with the second size cannot be created on a standard build.
You can create a label at the usual boundary with dynamic SQL. The example below is intended for a fresh schema because it creates a type with a fixed name.
DO $$
BEGIN
EXECUTE format(
'CREATE TYPE label_boundary_demo AS ENUM (%L)',
repeat('x', 63)
);
END
$$;
SELECT
enumlabel,
octet_length(enumlabel) AS label_bytes
FROM pg_enum
WHERE enumtypid = 'label_boundary_demo'::regtype;The %L format pattern turns the generated text into a safely quoted SQL literal. The resulting enum has one label that uses 63 ASCII bytes.
A multibyte example shows why length and octet_length answer different questions:
SELECT
length('é') AS characters,
octet_length('é') AS bytes;In a UTF-8 database, length usually returns 1, while octet_length returns 2. Always check the byte count when validating a possible enum label.
Storage limits are different from design limits
The 63-byte maximum is a hard technical limit, but shorter labels are usually easier to maintain. Enum labels appear in query results, error messages, logs, client libraries, and migration files. A label that is technically valid can still be inconvenient if it is verbose or tied to wording that may change.
Treat labels as stable identifiers rather than sentences. For example, these labels are easier to use consistently:
pendingin_reviewapprovedrejected
If the business meaning needs a long description, keep that description in application documentation or in a separate lookup table. Do not use the enum label as a replacement for user-facing text.
Enum labels also have identity. Renaming a label changes the value's displayed name everywhere that value is read, although the four-byte storage size remains the same. Removing a label requires replacing the enum type, which can be a substantial migration. A short, stable label reduces the cost of these changes.
How the pieces fit together
When planning an enum, separate these questions:
| Question | What it controls |
|---|---|
| How many bytes does one value use? | Usually four bytes in each row |
| How long can a label be? | Usually 63 bytes per label |
| What wording should the label use? | API stability, readability, and migration cost |
| Does the value need descriptions or metadata? | Whether a lookup table may fit better |
A large number of labels does not make each row wider, but it can make schema changes, client generation, and application logic harder to manage. If labels are frequently added, removed, translated, or described with extra attributes, a lookup table with a foreign key often provides more flexibility.
Exercise
In a UTF-8 database, why should you use octet_length rather than length when checking whether an enum label fits the usual PostgreSQL limit?
Show answer
The limit is measured in bytes. length counts characters, while octet_length counts bytes. A character such as é can occupy more than one byte.