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 number of blocks that a process can read from a single sort run in one read is controlled by the work_mem configuration parameter. The work_mem parameter specifies 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:

  • 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.

About Shiv Iyer 452 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.