PostgreSQL work_mem Tuning – Optimize Sort Performance by Controlling Block Reads

Tuning work_mem to dictate the number of blocks that a process can read from a single sort run in one read in PostgreSQL

In PostgreSQL, the work_mem configuration parameter controls how many blocks a process reads from a single sort run in one read. This parameter defines the amount of memory allocated for each internal sort operation, including sorting during query execution..

By default, the work_mem parameter is set to 4MB. However, you can adjust this value according to your system’s available memory and the requirements of your sorting operations. Here’s how you can dictate the number of blocks read in a single sort run by modifying the work_mem parameter:
By default, PostgreSQL sets work_mem to 4MB. However, you can adjust this value based on your system’s available memory and the needs of your sorting operations. Follow these steps to dictate how many blocks the system reads in a single sort run by tuning work_mem:

  • Determine the Available Memory:

    Assess the amount of memory available on your system. Consider the overall system requirements and the memory needs of other processes running on the server.
  • Evaluate Sort Requirements:

    Analyze the requirements of your sorting operations. Assess the size of the data being sorted, the expected number of rows, and the complexity of the sorting operations.
  • Modify the work_mem Parameter:

    To change the value of the work_mem parameter, you can use one of the following methods:
    • Temporary Change for a Single Session:
      • If you want to modify work_mem temporarily for the current session, you can use the SET command. For example, to set work_mem to 8MB:

        SET work_mem = ‘8MB’;
      • Permanent Change for All Sessions:
        • If you want to permanently change the work_mem parameter for all sessions, you can modify the PostgreSQL configuration file (postgresql.conf) and restart the PostgreSQL service. Locate the work_mem parameter and set it to the desired value. For example:

          work_mem = 8MB
  • Test and Evaluate Performance:

    After modifying the work_mem parameter, test the sorting operations and monitor the system’s performance. Evaluate the impact on memory usage, disk I/O, and overall query execution time. Adjust the value if necessary to achieve the desired balance between memory utilization and sorting performance.

It’s important to note that increasing the work_mem parameter allows for larger sort operations to be performed in memory, potentially reducing the need for disk I/O. However, excessively high values can consume significant memory resources and may impact the performance of other database operations. It’s recommended to carefully monitor system behavior and optimize the work_mem parameter based on the specific requirements and available resources of your PostgreSQL environment.

 

FAQ:

Q: When should I increase work_mem?
A: When the EXPLAIN ANALYZE plan shows “external merge Disk” or temp file logs—indicating sort or hash operations spill to disk.

Q: How much should I set work_mem to?
A: Estimate by measuring spill size (from EXPLAIN or logs) and assign slightly above that per session. Avoid setting it globally too high.

Q: Can indexing replace sort tuning?
A: Yes, if an index can satisfy ORDER BY, your query may skip sorting altogether—reducing work_mem need significantly.

 

Related Reading: For additional performance insights and memory management strategies in PostgreSQL, check out these related posts:

About Shiv Iyer 502 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.