# 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`
## PostgreSQL setup Extension can be loaded: In local session with `LOAD 'pg_logqueryid'`;
Using `session_preload_libraries` parameter in a specific connection
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. ## 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)`
`