GithubHelp home page GithubHelp logo

pierreforstmann / pg_logqueryid Goto Github PK

View Code? Open in Web Editor NEW
7.0 4.0 1.0 32 KB

PostgreSQL extension to display pg_stat_statements queryid with auto_explain

Home Page: https://github.com/pierreforstmann

Makefile 5.32% C 94.68%
c postgresql postgresql-extension

pg_logqueryid's Introduction

pg_logqueryid

PostgreSQL extension to display pg_stat_statements queryid with auto_explain

Installation

Compiling

This module can be built using the standard PGXS infrastructure. For this to work, the pg_config program must be available in your $PATH:

git clone https://github.com/pierreforstmann/pg_logqueryid.git
cd pg_logqueryid
make
make install

This extension has been validated with PostgreSQL 9.5, 9.6, 10, 11, 12, 13, 14, 15 and 16.

PostgreSQL setup

Extension can be loaded:

  1. in local session with LOAD 'pg_logqueryid';
  2. using session_preload_libraries parameter in a specific connection
  3. at server level with shared_preload_libraries parameter.

Usage

pg_logqueryid has no specific GUC. To use it pg_stat_statements and auto_explain extensions must be loaded and configured. If this is not the case pg_logqueryid can be loaded but is not enabled. (parameter logging_collector must also be set on to on).

Example

In postgresql.conf:

shared_preload_libraries = 'pg_stat_statements,auto_explain'
pg_stat_statements.max = 10000
pg_stat_statements.track = all
auto_explain.log_min_duration=0

In the current database connection:

pierre=# load 'pg_logqueryid';
LOAD

In this setup all SQL statements are auto explained and server log displays for current database session:

2020-03-28 14:47:08.633 CET [19735] LOG: pg_logqueryid: queryId=5917340101676597114
2020-03-28 14:47:08.633 CET [19735] STATEMENT: SELECT e.extname AS "Name", e.extversion AS "Version", n.nspname AS "Schema", c.description AS "Description"
FROM pg_catalog.pg_extension e LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace LEFT JOIN
pg_catalog.pg_description c ON c.objoid = e.oid AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass ORDER BY 1;
2020-03-28 14:47:08.633 CET [19735] LOG: duration: 0.066 ms plan:
Query Text: SELECT e.extname AS "Name", e.extversion AS "Version", n.nspname AS "Schema", c.description AS "Description"
FROM pg_catalog.pg_extension e LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace LEFT JOIN
pg_catalog.pg_description c ON c.objoid = e.oid AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass
ORDER BY 1;
Sort (cost=10.46..10.47 rows=1 width=158)
Sort Key: e.extname
-> Nested Loop Left Join (cost=0.28..10.45 rows=1 width=158)
Join Filter: (n.oid = e.extnamespace)
-> Nested Loop Left Join (cost=0.28..9.32 rows=1 width=98)
-> Seq Scan on pg_extension e (cost=0.00..1.01 rows=1 width=76)
-> Index Scan using pg_description_o_c_o_index on pg_description c (cost=0.28..8.30 rows=1 width=30)
Index Cond: ((objoid = e.oid) AND (classoid = '3079'::oid))
-> Seq Scan on pg_namespace n (cost=0.00..1.06 rows=6 width=68)
2020-03-28 14:47:08.633 CET [19735] LOG: duration: 0.739 ms statement: SELECT e.extname AS "Name", e.extversion AS "Version", n.nspname AS "Schema", c.description AS "Description"
FROM pg_catalog.pg_extension e LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass
ORDER BY 1;

For this example, queryId can be checked in pg_stat_statements view:

pierre=# select queryid, query from pg_stat_statements where queryId=5917340101676597114;
queryid |
query
--------------------+-------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
-----------
917340101676597114 | SELECT e.extname AS "Name", e.extversion AS "Version", n.nspname AS "Schema", c.description AS "Description"

+
| FROM pg_catalog.pg_extension e LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid AND c.classoid = $1::pg_cata log.regclass+
| ORDER BY 1
1 row)
`

pg_logqueryid's People

Contributors

pierreforstmann avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

hunleyd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.