Using EDB Postgres Tuner
You can use EDB Postgres Tuner in two ways: to automatically apply all tuning recommendations or to manually apply selected tuning recommendations.
Automatic tuning
To automatically apply all tuning recommendations, set the edb_pg_tuner.autotune
parameter to true
. Restart Postgres to apply the change. EDB Postgres Tuner then begins applying tuning recommendations.
Note
Automatic tuning isn't currently supported for BigAnimal.
Manual tuning
To manually apply selected tuning recommendations, make sure the edb_pg_tuner.autotune
parameter is set to false
, which is the default. Then, use the edb_pg_tuner_recommendations
function to inspect the tuning recommendations from the output.
You can set the format of the tuning recommendations to either conf
or sql
. For example:
Where:
conf
provides an output likeparameter = value
, which you can copy and paste into your configuration file. This is the default.sql
provides an output likeALTER SYSTEM SET parameter = value
, which you can execute in your preferred Postgres client tool.
Example
This example shows how to manually apply tuning recommendations to a postgresql.conf
file.
Here's an example of settings in a postgresql.conf
file:
The following command provides tuning recommendations for the postgresql.conf
file in the default conf
format:
The following command provides an output in the sql
format:
You can copy and paste the tuning recommendations in the conf
format into the postgresql.conf
file:
You can execute the tuning recommendations in the sql
format in your preferred Postgres client tool:
Auto-tuning work_mem
For Postgres 14 and higher, you can use EDB Postgres Tuner to optimize query performance by proactively adjusting the work_mem
parameter based on historical execution data. This can reduce disk I/O and improve overall query performance.
Memory Pool and Usage Limits:
Before a query is planned and executed, EDB Postgres Tuner will check the hash table to determine if a previous execution of the query resulted in disk spills. If there was a spill, a new work_mem
value is calculated using the following formula:
- This calculation aims to allocate sufficient memory to avoid disk spills for the current execution.
- In-memory sorts and hash aggregates require more memory than when run with disk spills.
- While calculating the exact amount is non-trivial (if possible at all), the values of 1.75 and 5 have worked well in test cases.
Requirements and Limitations for auto-tuning work_mem
The new
work_mem
value will be subject to the memory pool usage limits.A memory pool of
edb_pg_tuner.work_mem_pool
size is allocated to address these additional memory requirements.A particular query can utilize a maximum of 25% of this memory pool.
If the required memory exceeds either the remaining space in the pool or the 25% limit, the
work_mem
will not be increased, and the query will execute with the defaultwork_mem
setting.Because
work_mem
is allocated from a pool on a per-query basis, based on the highest disk spill previously seen for the query, it's possible for memory to be over allocated, because each query might use that amount of memory in each of multiple sort or hash aggregate nodes in the execution plan.The following example shows
work_mem
automatically bumped based on a disk spill for the previous query execution:
- The following example shows that when the disk spill is more than 25% of the reserved memory,
work_mem
is not increased:
Logging
For Postgres 14 and higher, you can use EDB Postgres Tuner to log query statistics.
Requirements and Limitations for logging:
- The queries that exceed the
edb_pg_tuner.log_min_duration
execution time are logged. - The sort and hash aggregate nodes of the query plan that caused the biggest spill to disk including the parallel workers are added.
- This data along with the number of times executed and query id is stored in a hash table in shared memory.
- If the
edb_pg_tuner.log_min_duration
is disabled, the auto tuning will still occur for already logged queries. - If the
edb_pg_tuner.tune_work_mem
is disabled, only statistics will be logged for eligible queries. - If both are disabled, then neither function is performed.
Monitoring
You can use the following SQL functions to monitor statistics information:
edb_pg_tuner_global_stats()
autotuner_query_stats()
edb_pg_tuner_global_stats()
The edb_pg_tuner_global_stats()
shows:
- The total number of queries that have executed regardless of whether or not were logged.
- The size of the query hash table in shared memory (
edb_pg_tuner.buffer_size
). - The number of unique queries in the shared memory buffer (capped at
edb_pg_tuner.buffer_size
). - The number of entries that have been deallocated from the shared memory buffer.
- The size of the reserved
work_mem
pool (edb_pg_tuner.work_mem_pool
). - The amount of the memory pool (in KiB) that is currently allocated to running queries.
autotuner_query_stats()
The autotuner_query_stats()
function shows for each query in the buffer:
- The query ID (the same as
pg_stat_statements
uses). - The number of times the query has executed.
- The maximum amount of sort disk spill recorded in any node in any invocation of the query.
- The maximum amount of sort nodes in the execution plan of any invocation of the query.
- The maximum amount of hash aggregate disk spill recorded in any node in any invocation of the query.
- The maximum amount of hash aggregate nodes in the execution plan of any invocation of the query.
Warning
The edb_pg_tuner_global_stats()
and edb_pg_tuner_query_stats()
functions are supported for Postgres 14 and higher versions. For Postgres 13 and lower versions, the functions will return the following error:
export const _frontmatter = {"title":"Using EDB Postgres Tuner","navTitle":"Using"}
Could this page be better? Report a problem or suggest an addition!