PostgreSQL: Track ALL SQL Query Execution Statistics using pg_stat_statements Extension (Day 1/2)

Are you facing performance related issues in PostgreSQL?

Do you want to know all your SQL Query Execution Statistics like: How many times same query executed, What is total and average time for queries and others.

If you are looking for these questions, this post is very helpful for you.

PostgreSQL provides pg_stat_statements module or extension which automatically records different types of statistic all running queries.

During the activity of Performance Optimization, We are always keen for long running queries of our Database Server.

We should configure pg_stat_statements module so that we can easily use require statistics for Performance Tuning.

Steps to configure and enable pg_stat_statements module:

First, Install pg_stat_statements Extension:

Once we install the extension, It starts to log require query execution information in pg_stat_statements table.
Select the data of pg_stat_statements:

If you get a below error, require to change few parameters in postgresql.conf file:

You can solve above error by changing this parameter in postgresql.conf file.
After changes in postgresql.conf, restart PostgreSQL service.

Execute few sample queries to check, how pg_stat_statements works:

Check the tracked statistics in pg_stat_statements:

Using below function, You can also reset pg_stat_statements:

Anvesh Patel

Leave a Reply

Be the First to Comment!

Notify of