Database 2025-06-14

PostgreSQL Memory Configuration

Guidelines for optimizing PostgreSQL memory settings for performance and stability.

Read in: ja
PostgreSQL Memory Configuration

Overview

Proper memory configuration is essential for enhancing database performance and ensuring stable operations. Disk access is significantly slower than memory access, so improving response performance by reading and writing from memory as much as possible is desirable. However, excessive memory allocation increases the risk of OOM (Out Of Memory), potentially leading to a system-wide shutdown. Therefore, it is crucial to carefully configure PostgreSQL's memory management to ensure both stability and performance.

This article summarizes the basic structure of shared and local memory areas, key parameter configuration guidelines, and operational verification procedures, based on PostgreSQL official documentation, practical operational insights, and Gihyo articles.

PostgreSQL Process Structure and Memory Area Division

PostgreSQL adopts a multi-process model, consisting of a master process generated at server startup, background processes responsible for tasks like WAL writing, and backend processes (session processes) created for each client connection. Each process uses memory independently, and the memory allocation of backend processes, which increase in proportion to the number of connections, significantly impacts overall memory consumption.

Memory management is broadly divided into the following two categories:

  1. Shared Memory This area is allocated at server startup and shared among multiple processes. Key settings include shared_buffers, wal_buffers, Free Space Map, and Visibility Map.
  2. Process-Local Memory This is working memory allocated for each backend process, used for operations like sorting, hash joins, and maintenance tasks. Parameters like work_mem, maintenance_work_mem, and temp_buffers fall into this category and can be dynamically set.

Guidelines for Setting shared_buffers

shared_buffers is a parameter that sets the amount of shared memory PostgreSQL uses as a database cache. The default 128MB is small, so if using a dedicated server, start with about 25% of system memory and gradually increase while balancing with OS cache.

Guidelines for Setting work_mem

work_mem sets the upper limit of memory available for temporary operations like sorting and hash joins. The limit applies per query execution process and per operation, so actual consumption depends on factors like work_mem × number of temporary operations × number of parallel workers × number of concurrent sessions. In the worst case, this can lead to significant memory consumption. However, this is a theoretical maximum, and actual usage varies based on query content and timing, so it should be considered a guideline.

Other Related Parameters

Conclusion

PostgreSQL memory management requires a comprehensive design centered around shared_buffers and work_mem, considering process-specific consumption, concurrent connections, and parallel query characteristics. Implement changes gradually, and ensure thorough pre-verification, risk assessment, and continuous monitoring to achieve stable and high-performance operations.

References

Tags: PostgreSQL
Share: 𝕏 Post Facebook Hatena
✏️ View source / Discuss on GitHub
☕ Support

If you enjoy this blog, consider supporting it. Every bit helps keep it running!


Related Articles