Quick revision
The short answer
I would start with shared_buffers at about 25 percent of RAM, 16 GB, effective_cache_size around 48 GB because it is only a planner hint, a modest work_mem such as 32 MB because it is used per sort or hash operation and multiplies across queries and connections, and maintenance_work_mem around 1 to 2 GB for vacuum and index builds. Alongside those, I would set random_page_cost near 1.1 for SSDs and keep max_connections modest, relying on a connection pool.
What to remember
In this answer, we saw that PostgreSQL uses shared memory, private per-query memory and the OS page cache. For a 64 GB server we started with shared_buffers at 16 GB, effective_cache_size at about 48 GB as a planner hint, work_mem low at around 32 MB because it multiplies per operation and per connection, and maintenance_work_mem at 1 to 2 GB, plus random_page_cost near 1.1 on SSDs and a modest max_connections behind a pool. We also saw how temporary files, in EXPLAIN, pg_stat_database and log_temp_files, tell us when work_mem is too low for specific queries.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would start with shared_buffers at about 25 percent of RAM, 16 GB, effective_cache_size around 48 GB because it is only a planner hint, a modest work_mem such as 32 MB because it is used per sort or hash operation and multiplies across queries and connections, and maintenance_work_mem around 1 to 2 GB for vacuum and index builds. Alongside those, I would set random_page_cost near 1.1 for SSDs and keep max_connections modest, relying on a connection pool.
Why this question is asked
The defaults that ship with PostgreSQL are deliberately tiny, so a new 64 GB server running on them uses a small fraction of its memory. But raising settings blindly can be worse than the defaults, because one of them, work_mem, can multiply into an out-of-memory crash. Interviewers ask this to check that we know what each setting actually controls, which ones allocate memory and which are hints, and how the numbers relate. In this answer, we will recap how PostgreSQL uses memory, go through the four main settings with starting values for 64 GB, look at two related settings, and see how to tell when work_mem is too low.
1A Quick Recap of How PostgreSQL Uses Memory
PostgreSQL uses memory in two very different ways. The first is shared memory, allocated once at startup and shared by all connections. Its main part is shared_buffers, PostgreSQL's own cache of 8 KB data pages. The second is private memory, allocated by each backend process, one per connection, while it runs queries: memory for sorts, hash tables, and maintenance work.
There is also a third cache that PostgreSQL does not manage: the operating system's page cache. PostgreSQL reads files through the operating system, so pages that fall out of shared_buffers are often still in the OS cache. That is why PostgreSQL does not try to take all the RAM for itself.
- shared_buffers16 GB, PostgreSQL's page cache, shared by all connections
- Per-query memorywork_mem per sort or hash node, times active queries
- Maintenance memorymaintenance_work_mem for vacuum and index builds
- OS page cachemost of the rest; PostgreSQL reads through it
- OS and other processesa few GB for the kernel and agents
5 layers, from the top down. Step through them one at a time.
effective_cache_size describes the first and fourth layers to the planner; it allocates nothing.
2Shared_buffers and effective_cache_size
shared_buffers is the cache PostgreSQL manages itself. A common starting point is 25 percent of RAM, so 16 GB on our server. Going much higher often helps less than expected, because the OS cache would hold many of the same pages twice, and a very large shared_buffers makes checkpoints and some operations heavier. Changing it requires a restart, so it is worth getting a sensible value at the start.
effective_cache_size is different: it allocates nothing. It tells the planner how much data it can expect to find cached in shared_buffers and the OS cache together. A higher value makes index scans look cheaper, because the planner assumes repeated page reads will hit memory. On a dedicated 64 GB server, 50 to 75 percent of RAM, for example 48 GB, is a reasonable starting value.
3Work_mem: The Setting That Multiplies
work_mem is the memory one sort or one hash operation may use before it spills to temporary files on disk. The key word is one operation. A single complex query can have several sort and hash nodes in its plan, each with its own work_mem, and every connection running such a query uses its own copy. Hash operations can even use more, up to work_mem times hash_mem_multiplier, which defaults to 2.
So the worst case is roughly work_mem times operations per query times active connections. With work_mem = 256 MB, 4 such nodes per query and 50 busy connections, that is 50 GB on top of shared_buffers, which can push the server into the out-of-memory killer. That is why we start low, 16 to 64 MB, say 32 MB, and raise it only where it is needed: per session, per transaction or per role, such as a reporting role that runs big sorts.
4Maintenance_work_mem and the Other Settings
maintenance_work_mem is used by VACUUM, CREATE INDEX and ALTER TABLE ADD FOREIGN KEY. Only a few of these run at once, so we can be generous: 1 to 2 GB makes index builds faster and lets vacuum remember more dead tuples per pass. Autovacuum workers use autovacuum_work_mem if it is set, otherwise this value, once per worker, so with 3 workers and 2 GB the peak is 6 GB.
Two more settings belong in the first pass even though they are not memory. random_page_cost tells the planner how expensive a random read is compared to a sequential one. The default of 4 dates from spinning disks; on SSDs, values near 1.1 make the planner choose index scans when they really are cheaper. And max_connections should stay modest, for example 200, because every connection is a process with its own memory. The Spring Boot service's HikariCP pool, perhaps 20 connections per instance, and a pooler such as PgBouncer when there are many instances, keep the real number small.
| Setting | Value | What it is |
|---|---|---|
| shared_buffers | 16 GB | shared page cache, allocated at start |
| effective_cache_size | 48 GB | planner hint, allocates nothing |
| work_mem | 32 MB | per sort or hash node, per query |
| maintenance_work_mem | 1 to 2 GB | vacuum and index builds |
| random_page_cost | 1.1 | SSD: random reads are nearly as cheap |
| max_connections | about 200 | each one is a process; use a pool |
5Detecting That work_mem Is Too Low
When an operation needs more than work_mem, PostgreSQL writes temporary files. We can see this in three places. EXPLAIN (ANALYZE) shows Sort Method: external merge with a Disk size for sorts, and Batches greater than 1 for hash joins. The statistics view pg_stat_database counts temp_files and temp_bytes per database. And with log_temp_files set, the server logs every temporary file above a size, with the query that created it.
With pg_stat_statements installed, its temp_blks_written column shows which queries spill the most. Then we raise work_mem for those queries or their role, rather than for everyone.
6Follow-up questions
How would you detect that work_mem is too low?
Look for temporary files. EXPLAIN ANALYZE shows external merge sorts with a disk size and hash joins with several batches; pg_stat_database shows temp_files and temp_bytes climbing; log_temp_files logs each spilling query; and pg_stat_statements shows temp_blks_written per query. Once we know the queries, we raise work_mem for them or their role and check that the spill disappears.
Why not give shared_buffers 75 percent of RAM?
Because PostgreSQL also relies on the OS page cache, so very large shared_buffers mostly holds pages twice and leaves less room for the OS cache and for per-query memory. It can also make checkpoints heavier. 25 percent is a starting point; we can test 30 or 40 percent against the real workload.
Which of these settings need a restart?
shared_buffers and max_connections need a restart. effective_cache_size, work_mem, maintenance_work_mem and random_page_cost take effect on reload, and work_mem can also be set per session or role.
7Conclusion
In this answer, we saw that PostgreSQL uses shared memory, private per-query memory and the OS page cache. For a 64 GB server we started with shared_buffers at 16 GB, effective_cache_size at about 48 GB as a planner hint, work_mem low at around 32 MB because it multiplies per operation and per connection, and maintenance_work_mem at 1 to 2 GB, plus random_page_cost near 1.1 on SSDs and a modest max_connections behind a pool. We also saw how temporary files, in EXPLAIN, pg_stat_database and log_temp_files, tell us when work_mem is too low for specific queries.
Open with
I would start with four settings. shared_buffers at about 25 percent of RAM, so 16 GB; PostgreSQL also reads through the OS page cache, so it does not need most of the memory itself, and this one needs a restart. effective_cache_size at around 48 GB, 50 to 75 percent of RAM, which is only a hint to the planner about how much is cached and allocates nothing. work_mem is the one to be careful with: it applies per sort or hash node, per query, per connection, and hash nodes can use twice that, so a big value multiplied by 50 busy connections can exhaust memory.
Then, if they want more
I would start at around 32 MB and raise it per role or with SET LOCAL for reporting queries. maintenance_work_mem can be 1 to 2 GB, because only a few vacuums and index builds run at once, keeping in mind each autovacuum worker uses it.
Beyond memory, I would set random_page_cost near 1.1 on SSDs and keep max_connections modest, around 200, since the Spring Boot service has a HikariCP pool and we can add PgBouncer if instances multiply. Then I would measure: log_temp_files and temp_bytes in pg_stat_database show if work_mem is too low, and the buffer hit ratio tells me how shared_buffers is doing.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A new PostgreSQL 17 server has 64 GB of RAM and is used by a Spring Boot service. Which...": https://scrivio-interview-library.netlify.app/databases/postgresql/3630e38e3e/