Work_mem and effective_cache_size tuning in PostgreSQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When tuning PostgreSQL settings like work_mem and effective_cache_size, we want to understand how query execution time changes as data size grows.
We ask: How does memory allocation affect the speed of sorting and joining operations as input grows?
Analyze the time complexity impact of this query with memory settings:
SET work_mem = '4MB';
SET effective_cache_size = '128MB';
SELECT *
FROM orders
JOIN customers ON orders.customer_id = customers.id
ORDER BY orders.order_date DESC
LIMIT 100;
This query joins two tables and sorts orders by date, using memory settings to control sorting and caching.
Look at what repeats during query execution:
- Primary operation: Scanning and sorting rows from the
orderstable. - How many times: Once per row in
orders, plus matching rows incustomers.
As the number of rows in orders grows, the sorting work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 log 10 (small sorting) |
| 100 | About 100 log 100 (more sorting work) |
| 1000 | About 1000 log 1000 (much more sorting) |
Pattern observation: Sorting work grows a bit faster than the number of rows, roughly proportional to n times log n.
Time Complexity: O(n log n)
This means the time to sort rows grows a little faster than the number of rows, because sorting compares items multiple times.
[X] Wrong: "Increasing work_mem always makes queries run in constant time regardless of data size."
[OK] Correct: More memory helps sorting fit in RAM, but sorting still needs to compare many rows, so time grows with data size.
Understanding how memory settings affect query time helps you explain performance tuning clearly and confidently in real situations.
"What if we increased work_mem enough to hold all rows in memory? How would the time complexity change?"
Practice
work_mem setting control in PostgreSQL?Solution
Step 1: Understand the role of
work_memwork_memis the memory PostgreSQL uses for internal operations like sorting and joining during query execution.Step 2: Differentiate from other memory settings
Other settings likeeffective_cache_sizerelate to cache estimation, not sorting or joining memory.Final Answer:
The amount of memory used for sorting and joining operations during query execution -> Option AQuick Check:
work_mem= sorting/join memory [OK]
- Confusing work_mem with effective_cache_size
- Thinking work_mem controls total server memory
- Assuming work_mem affects connection limits
effective_cache_size to 4GB in PostgreSQL's configuration file?Solution
Step 1: Check PostgreSQL config syntax for memory sizes
PostgreSQL accepts memory sizes with units like KB, MB, GB without quotes.Step 2: Validate each option
effective_cache_size = 4GB uses correct syntax: number + unit without quotes. effective_cache_size = '4GB' uses quotes (invalid). effective_cache_size = 4000MB uses MB but 4000MB is less than 4GB. effective_cache_size = 4g uses lowercase 'g' which is invalid; units must be uppercase.Final Answer:
effective_cache_size = 4GB -> Option DQuick Check:
Config memory size = number + uppercase unit [OK]
- Adding quotes around memory size values
- Using lowercase units like 'g' instead of 'GB'
- Confusing MB and GB values
work_mem = '2MB' and a query performing 3 sorts simultaneously, what is the total memory PostgreSQL may use for sorting?Solution
Step 1: Understand work_mem usage per operation
Each sort operation can use up towork_memmemory independently.Step 2: Calculate total memory for 3 sorts
3 sorts x 2MB each = 6MB total memory used for sorting.Final Answer:
6MB -> Option AQuick Check:
work_mem x number of sorts = total memory [OK]
- Assuming work_mem is total for all operations
- Adding instead of multiplying memory sizes
- Ignoring simultaneous operation count
effective_cache_size set too low. What problem might this cause?Solution
Step 1: Understand effective_cache_size role
This setting helps PostgreSQL estimate how much OS cache is available for data pages.Step 2: Consequence of low effective_cache_size
If set too low, PostgreSQL thinks less cache is available, so it may avoid index scans or other efficient plans, choosing slower ones.Final Answer:
PostgreSQL may underestimate available cache and choose inefficient query plans -> Option CQuick Check:
Low effective_cache_size = conservative query plans [OK]
- Confusing effective_cache_size with work_mem
- Assuming server crashes from low effective_cache_size
- Thinking it limits connection count
work_mem and effective_cache_size for a workload with many concurrent queries doing large sorts. Which is the best tuning approach?Solution
Step 1: Balance work_mem for concurrent large sorts
Settingwork_memtoo high risks memory exhaustion with many queries; moderate value like 16MB balances performance and memory use.Step 2: Set effective_cache_size to reflect OS cache
With 32GB RAM, settingeffective_cache_sizehigh (e.g., 24GB) helps PostgreSQL plan queries assuming ample cache.Step 3: Evaluate other options
Setwork_memvery high (e.g., 1GB) andeffective_cache_sizelow (e.g., 4GB) risks memory overuse; Set bothwork_memandeffective_cache_sizevery low to save memory wastes performance; Setwork_memlow (e.g., 1MB) andeffective_cache_sizemoderate (e.g., 8GB) underestimates cache and limits sort memory.Final Answer:
Set work_mem moderately (e.g., 16MB) and effective_cache_size high (e.g., 24GB) -> Option BQuick Check:
Moderate work_mem + high effective_cache_size = balanced tuning [OK]
- Setting work_mem too high causing memory exhaustion
- Setting effective_cache_size too low causing bad plans
- Ignoring concurrent query memory needs
