Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 September PostgreSQL: Track ALL SQL Query Execution Statistics using pg_stat_statements Extension (Day 1/2)

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

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

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:

1
CREATE EXTENSION pg_stat_statements;

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:

1
SELECT *FROM pg_stat_statements;

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

1
ERROR: pg_stat_statements must be loaded via shared_preload_libraries

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

1
2
3
shared_preload_libraries = 'pg_stat_statements'
pg_stat_statements.max = 10000
pg_stat_statements.track = all

Execute few sample queries to check, how pg_stat_statements works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CREATE TABLE tbl_ItemTransactions
(
TranID SERIAL
,TransactionDate TIMESTAMPTZ
,TransactionName TEXT
);
 
INSERT INTO tbl_ItemTransactions
(TransactionDate, TransactionName)
SELECT x, 'dbrnd'
FROM generate_series('2016-01-01 00:00:00'::timestamptz, '2016-12-31 23:59:59'::timestamptz,'2 seconds'::interval) a(x);
 
SELECT COUNT(1) FROM tbl_ItemTransactions;
 
SELECT *FROM tbl_ItemTransactions
WHERE TransactionDate BETWEEN '2016-02-08' AND '2016-04-08';

Check the tracked statistics in pg_stat_statements:

1
SELECT *FROM pg_stat_statements;

Using below function, You can also reset pg_stat_statements:

1
SELECT *FROM pg_stat_statements_reset();

Sep 9, 2016Anvesh Patel
PostgreSQL: Using EXPLAIN ANALYZE, Know your Query Execution PlanPostgreSQL: Script to find TOP 10 Long Running Queries using pg_stat_statements (Day 2/2)
Comments: 2
  1. Darshan Shah
    January 31, 2019 at 1:33 pm

    Hi Avnesh,
    Here one question. Does this logging all statements using pg_stat_statement affect database performance ?

    • Anvesh Patel
      Anvesh Patel
      February 8, 2019 at 6:46 pm

      Yes, up to 2% to 3%

Anvesh Patel
Anvesh Patel

Database Engineer

September 9, 2016 PostgreSQLAnvesh Patel, database, database research and development, dbrnd, log query execution, Performance Optimization, pg_stat_statements, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks, query statistics
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....