![[res_memory.png|64]] Postgres uses memory at several distinct layers: the shared buffer pool (cross-session), per-session work memory, and the OS's own page cache (outside Postgres control). - **Tuning**: [19.4.1 Memory](https://www.postgresql.org/docs/current/runtime-config-resource.html#RUNTIME-CONFIG-RESOURCE-MEMORY) - **Visibility**: wait events of type [[LWLock]] (buffer pool locks), [[IPC]] (hash/sort coordination), [[BufferPin]] ## Key Concepts ## Shared Buffer Pool - **Description**: A fixed shared memory region holding recently accessed data pages. All backends share it. Managed by the buffer manager - **Impact**: `BufferContent` LWLock waits when many sessions access the same page; `BufferPin` waits when VACUUM needs exclusive access ## work_mem - **Description**: Per-sort/per-hash-operation memory limit. A single query can use `work_mem` many times simultaneously (e.g. multiple sort nodes) - **Impact**: Too low → spill to [[Disk IO]] (`BufFileRead`/`BufFileWrite`); too high → OOM under concurrency ## Dynamic Shared Memory (DSM) - **Description**: Per-query shared memory segments allocated for parallel query workers to exchange data - **Impact**: `DSMAllocate` IO waits and `ParallelQueryDSA` LWLock waits during allocation ## SLRU Caches - **Description**: Simple LRU caches for transaction status, multixact, commit timestamps, etc. Backed by disk when evicted - **Impact**: `XactSLRU`, `CommitTsSLRU`, `MultiXactMemberSLRU` LWLock waits under high transaction rate ## Key Config Parameters | Parameter | Default | Purpose | |---|---|---| | `shared_buffers` | 128 MB | Shared page cache (see also [[Disk IO]]) | | `work_mem` | 4 MB | Per-operation sort/hash memory | | `maintenance_work_mem` | 64 MB | Memory for VACUUM, CREATE INDEX, etc. | | `hash_mem_multiplier` | 2.0 | Multiplier on `work_mem` for hash tables | | `huge_pages` | `try` | Use OS huge pages for shared memory | | `huge_page_size` | OS default | Size of huge pages to request | | `max_prepared_transactions` | 0 | Max simultaneously prepared transactions (uses shared memory) | | `temp_buffers` | 8 MB | Per-session buffer for temporary tables | | `shared_memory_type` | `mmap` | Shared memory implementation (`mmap`, `sysv`, `windows`) | ## Related Workloads - [[LWLock]] (`BufferContent`, `BufferMapping`, `ProcArray`, `ShmemIndex`) - [[BufferPin]] - [[IPC]] (`HashBuildAllocate`, `ParallelBitmapScan`, `SharedTupleStore`)