Introduction to PostgreSQL Performance Tuning
PostgreSQL performance tuning is defined as tune the database for better performance and quick access of data; we can tune the PostgreSQL database performance by tuning the query and database performance related parameter. For better performance, we need to find a slow and long-running query from the database by using the pg_stat_activity catalog table; after finding a slow query, we need to find an explain plan of the query. For better database performance, we need to tune the performance-related parameter like shared_buffers, wal_buffers, effective_cache_size, work_mem, maintainance_work_mem, and synchronous_commit also; we need to change this parameter based on resources.
How to perform Performance Tuning in PostgreSQL?
We can perform performance tuning in PostgreSQL by using the following conditions. Below is the parameter which was we have to tune to increase the performance of the database.
- Query tuning
- Index tuning
- Memory tuning
- Storage tuning
- Operating system tuning
- Network tuning
- Application tuning
- Configuration parameter tuning
1. Query tuning
- To tune the database performance, we need to find a log running a query that was running on the database server.
- We can find a long-running query by using the pg_stat_activity catalog table. The below example shows that find the long-running or slow query in PostgreSQL.
select * from pg_stat_activity;
- In the above example state that is using the exact start time, we can find the long-running query.
- After finding a slow query, we need to create the explain plan of that query; explain plan will show the below information related to the slow query.
- Nodes
- Cost
- Actual time
- Rows
- Width
- Index scan
- Loop
- Index condition
- Using the above parameter, we are tuning the query to increase the performance of the database.
- The below example shows that find explains and explains analyze the plan of query in PostgreSQL.
explain select * from performance_table;
explain analyze select * from performance_table;
- We have also used explain analysis with the query; it will show the additional information related to the query’s execution time.
- Nodes are defined as the block of code that was executing at the time of query execution in PostgreSQL.
- Node in explain plan consist of the logical unit of work.
- Cost is defined as how much work will be required to execute the query. Cost is defined as two number first number contains the startup cost, and the second contains the incurred process cost.
- The actual time is defined as how much time is required to execute the query and retrieve its output. This time is shown in milliseconds.
- Rows are defined as the number of rows which was defined in the query. The row is important to factor in a while finding and explain the plan of the query.
2. Index tuning
- Index tuning is also important in PostgreSQL because the index will increase the performance of the select query.
- For tuning an index in PostgreSQL, we need to turn the statistics collector. The statistics collector will collect the performance statistics.
- Also, we have tuning missing indexes from the query. After finding the missing index, we need to create the same on the column.
3. Storage tuning
- Storage tuning is also important for the great performance of the PostgreSQL database. We need to define the right file system, and the right RAID level will increase the performance of the database.
- The right file system and right raid level will increase the I/O of the database server.
4. Memory tuning
- Memory tuning is also important while tuning a PostgreSQL database system. We can tune the performance-related parameter related to memory.
5. Network tuning
- We have also checked the connectivity between the application server and the client-server. We have to configure the proper link of the network while tuning a database server in PostgreSQL.
6. Operating system tuning
- To increase the performance of the query, we need to choose the proper operating system of our database in PostgreSQL.
- Choosing an accurate operating system for the database is also an important task to tune the PostgreSQL database.
7. Application tuning
- In PostgreSQL performance tuning, we need to tune the application in PostgreSQL. We have to check the proper compatibility of the application with the database.
8. Configuration parameter tuning
- We are tuning below the configuration parameter below to increase the performance of the PostgreSQL database in PostgreSQL.
- Shared_buffers
- Effective_cache_size
- Work_mem
- Maintainance_work_mem
- Wal_buffers
- Syncronous_commit
- We have tuned the above configuration parameter as per resources are allocated to the PostgreSQL database system.
- Increasing the performance of the database configuration parameter plays a very important role.
Examples of PostgreSQL Performance Tuning
Below is the example of PostgreSQL Performance Tuning:
- Below is an example of performance tuning in PostgreSQL. We are using performance_table to describe examples of PostgreSQL performance tuning.
- Below is the count and structure of the performance_table.
select count(*) from performance_table;
\d+ performance_table;
1. Create an index to increase performance –
- The below example shows that create an index to increase the performance of the query.
- In the above example, after creating an index query, performance is automatically increased.
explain analyze select * from performance_table where id = 5;
create index test_pkey on performance_table (id);
explain analyze select * from performance_table where id = 5;
2. Create an explain plan of the query to increase performance
- The below example shows that create an explanation plan of the query to increase the performance of the query.
explain select * from performance_table where id = 5;
explain analyze select * from performance_table where id = 5;
3. Change the configuration parameter to increase performance
- The below example shows that change the configuration parameter to increase the performance of the database.
- We have changing configuration parameters as per the resources allocated to the database server.
free–m
vi /var/lib/pgsql/10/data/postgresql.conf
max_connections = 200
shared_buffers = 256MB
effective_cache_size = 768MB
maintenance_work_mem = 64MB
checkpoint_completion_target = 0.7
wal_buffers = 7864kB
random_page_cost = 1.1
effective_io_concurrency = 200
work_mem = 1310kB
min_wal_size = 1GB
max_wal_size = 4GB
Conclusion
We can increase the performance by using Query tuning, Index tuning, Memory tuning, Storage tuning, Operating system tuning, Network tuning, Application tuning, Configuration parameter tuning. The explain plan is used to show that the query’s execution plan will show the proper execution time of our query.
Recommended Articles
This is a guide to PostgreSQL Performance Tuning. Here we discuss How to perform Performance Tuning in PostgreSQL. You can also look at the following article to learn more –