ENUM types in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using ENUM types in PostgreSQL, it's important to understand how the system handles these values internally.
We want to know how the time to work with ENUM values changes as the number of ENUM labels grows.
Analyze the time complexity of querying a table with an ENUM column.
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
CREATE TABLE person (
id SERIAL PRIMARY KEY,
name TEXT,
current_mood mood
);
SELECT * FROM person WHERE current_mood = 'happy';
This code defines an ENUM type, creates a table using it, and queries rows matching a specific ENUM value.
Look at what happens when the query runs:
- Primary operation: Comparing ENUM values during row filtering.
- How many times: Once per row scanned in the table.
As the number of rows grows, the database compares the ENUM value for each row to the target.
| Input Size (n rows) | Approx. Operations |
|---|---|
| 10 | 10 comparisons |
| 100 | 100 comparisons |
| 1000 | 1000 comparisons |
Pattern observation: The number of comparisons grows directly with the number of rows scanned.
Time Complexity: O(n)
This means the time to find matching rows grows in a straight line as the table gets bigger.
[X] Wrong: "ENUM types make comparisons instant no matter how many rows there are."
[OK] Correct: While ENUM values are stored efficiently, the database still checks each row's value when scanning, so time grows with row count.
Understanding how ENUM types affect query time helps you explain data type choices and performance in real projects.
What if we added an index on the ENUM column? How would the time complexity change?
Practice
ENUM types in PostgreSQL?Solution
Step 1: Understand ENUM type purpose
ENUM types define a list of allowed values for a column, ensuring data consistency.Step 2: Compare with other options
Other options describe unrelated features like text storage, temporary tables, or indexing.Final Answer:
To restrict a column to a fixed set of allowed values -> Option AQuick Check:
ENUM = fixed allowed values [OK]
- Thinking ENUM stores large text data
- Confusing ENUM with temporary tables
- Assuming ENUM improves indexing speed
mood with values 'happy', 'sad', and 'neutral'?Solution
Step 1: Recall ENUM creation syntax
The correct syntax isCREATE TYPE name AS ENUM (values);with values in parentheses and single quotes.Step 2: Check each option
CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral'); matches the correct syntax exactly. Others have wrong keywords or brackets.Final Answer:
CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral'); -> Option DQuick Check:
CREATE TYPE ... AS ENUM (values) [OK]
- Using CREATE ENUM instead of CREATE TYPE
- Using square brackets instead of parentheses
- Omitting AS keyword
CREATE TYPE colors AS ENUM ('red', 'green', 'blue');
CREATE TABLE items (id SERIAL PRIMARY KEY, color colors);
INSERT INTO items (color) VALUES ('green'), ('blue'), ('red');
SELECT color FROM items ORDER BY color;Solution
Step 1: Understand ENUM ordering
ENUM values are ordered by their declaration order: 'red' < 'green' < 'blue'.Step 2: Apply ORDER BY on color column
Ordering by color sorts rows as per ENUM order, so 'red', 'green', 'blue'.Final Answer:
red, green, blue -> Option AQuick Check:
ENUM order = declaration order [OK]
- Assuming alphabetical order instead of ENUM order
- Confusing insertion order with sort order
- Expecting default text sorting
CREATE TYPE status AS ENUM ('new', 'in_progress', 'done');
CREATE TABLE tasks (
id SERIAL PRIMARY KEY,
task_status status DEFAULT 'pending'
);Solution
Step 1: Check ENUM values and default
ENUM 'status' has values 'new', 'in_progress', 'done'. Default 'pending' is not listed.Step 2: Understand default value constraints
Default must be one of ENUM values; otherwise, it causes an error.Final Answer:
The default value 'pending' is not in the ENUM list -> Option CQuick Check:
Default must be ENUM member [OK]
- Assuming any string can be default
- Thinking ENUM can't be column type
- Ignoring missing semicolon errors
status with values ('new', 'in_progress', 'done'). Which statement correctly adds 'archived' after 'done'?Solution
Step 1: Recall how to add ENUM values
PostgreSQL usesALTER TYPE ... ADD VALUE 'new_value' [BEFORE|AFTER existing_value]syntax.Step 2: Identify correct position
To add 'archived' after 'done', useAFTER 'done'.Step 3: Check options
ALTER TYPE status ADD VALUE 'archived' AFTER 'done'; matches correct syntax and position. Others use BEFORE (wrong position) or invalid keywords.Final Answer:
ALTER TYPE status ADD VALUE 'archived' AFTER 'done'; -> Option BQuick Check:
ALTER TYPE ADD VALUE ... AFTER ... [OK]
- Using MODIFY instead of ADD VALUE
- Placing new value BEFORE wrong existing value
- Using wrong position like BEFORE 'done'
