0
0
PostgreSQLquery~10 mins

Work_mem and effective_cache_size tuning in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Work_mem and effective_cache_size tuning
Start Query Execution
Check work_mem setting
Sort/Hash operations use work_mem
If work_mem too low -> Disk spill
Check effective_cache_size
Planner estimates cached data
Choose query plan based on cache estimate
Execute query with chosen plan
End Query Execution
The database uses work_mem for sorting and hashing during query execution. It uses effective_cache_size to estimate how much data is cached in memory to choose the best query plan.
Execution Sample
PostgreSQL
SET work_mem = '4MB';
SET effective_cache_size = '128MB';
EXPLAIN SELECT * FROM sales ORDER BY date;
This sets work_mem and effective_cache_size, then explains the query plan for sorting sales by date.
Execution Table
StepActionSetting/ValueEffect on QueryNotes
1Set work_mem4MBLimits memory for sort/hashSmall memory may cause disk spill
2Set effective_cache_size128MBPlanner estimates cache sizeHelps planner choose index scan or seq scan
3Planner analyzes queryUses effective_cache_sizeChooses plan based on cache estimateIf cache large, prefers index scan
4Execute sort operationUses work_memSort fits in memoryFast in-memory sort
5Query completes--Result returned quickly
6Increase work_mem to 64MB64MBMore memory for sortLess chance of disk spill
7Re-execute query-Faster sort, less disk I/OImproved performance
8Increase effective_cache_size to 1GB1GBPlanner assumes more cacheMay choose index scan more often
9Re-execute query-Potentially better planDepends on actual cache availability
10End--Tuning complete
💡 Execution ends after query runs with tuned settings
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 6After Step 8Final
work_memdefault (e.g. 4MB)4MB4MB64MB64MB64MB
effective_cache_sizedefault (e.g. 128MB)128MB128MB128MB1GB1GB
query_planunknownplan with 4MB work_memplan with 4MB work_mem and 128MB cacheplan with 64MB work_memplan with 64MB work_mem and 1GB cachefinal chosen plan
Key Moments - 3 Insights
Why does increasing work_mem reduce disk usage during sorting?
Because work_mem sets the memory available for sorting. If it's too small (see step 1 and 4), sorting spills to disk which is slower. Increasing it (step 6) allows sorting to happen fully in memory.
How does effective_cache_size influence the query planner's decisions?
Effective_cache_size tells the planner how much data it expects to be cached in RAM. A larger value (step 8) makes the planner more likely to choose index scans assuming data is cached, improving performance.
Can setting work_mem too high cause problems?
Yes. If many queries run simultaneously, each using large work_mem, total memory usage can exceed system RAM causing swapping and slowdowns. So balance is needed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what happens when work_mem is set to 4MB?
ASort operation spills to disk causing slow down
BSort operation fits in memory and is fast
CQuery planner ignores work_mem
DEffective_cache_size is increased automatically
💡 Hint
Check the 'Effect on Query' and 'Notes' columns at step 4 in execution_table
At which step does the planner start assuming a larger cache size?
AStep 8
BStep 6
CStep 2
DStep 10
💡 Hint
Look at the 'Setting/Value' and 'Effect on Query' columns in execution_table rows
If work_mem is set too high for many concurrent queries, what is a likely outcome?
AQueries run faster without issues
BEffective_cache_size automatically adjusts
CSystem may run out of memory and slow down
DDisk usage increases for sorting
💡 Hint
Refer to key_moments about work_mem and memory usage
Concept Snapshot
work_mem sets memory for sorting and hashing operations during query execution.
effective_cache_size tells the planner how much memory is available for caching data.
Too low work_mem causes disk spills; too high risks memory exhaustion.
Effective_cache_size influences planner's choice of index vs sequential scans.
Tune both for balanced performance based on workload and system RAM.
Full Transcript
This visual execution shows how PostgreSQL uses work_mem and effective_cache_size settings during query execution. First, work_mem limits memory for sorting and hashing. If too small, sorting spills to disk, slowing queries. Increasing work_mem allows faster in-memory operations but must be balanced to avoid memory exhaustion when many queries run concurrently. Effective_cache_size is a planner hint about how much data is cached in RAM. A larger value encourages the planner to choose index scans assuming data is cached, improving performance. The execution table traces setting these values, planner decisions, and query execution steps. Key moments clarify why tuning these parameters matters and the risks of improper settings. The quiz tests understanding of how these settings affect query plans and performance.