Giter Site home page Giter Site logo

legrandlegrand / pg_stat_sql_plans Goto Github PK

View Code? Open in Web Editor NEW
10.0 6.0 1.0 249 KB

pg_stat_sql_plans is a PostgreSQL extension created from pg_stat_statements, pg_store_plans and auto_explain adding a planid and many other features.

License: Other

Makefile 0.99% C 99.01%
extension postgresql performance sql explain plan oracle

pg_stat_sql_plans's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

davidfetter

pg_stat_sql_plans's Issues

do not re-calculate planid for cached plans

with pg_stat_sql_plans

do $$ declare i int;j int; begin for i in 1..1000000 loop select 1 into j; end loop; end; $$;

is slow compared with pg_stat_statements.

This is because an explain command is executed for each query execution even when this query plan is cached. Try to find a way to avoid that.

expose planid for pid

as it is not yet available insique querydesc structure (as queryid),
this could be usefull for:

  • identifying current pid's execution plan,
  • store planid for a query, to be able to use it in many places (planning, execution, error, ...)

check which structure is the more appropriate :

duplicated qpid in logs when pg_stat_sql_plans.explain=true

This appears with
pgbench -c20 -t5 (a kind of query storm ...)
on my MSYS2 (windows 10), but not on linux ...

It also appears in native pg_stat_statements as described here

https://www.postgresql-archive.org/pg-stat-statements-duplicated-external-query-texts-with-MSY2-td6140505.html

In fact, in pgss_store, the query is written in external query text file, before taking the EXCLUSIVE
lock ...

	/* Append new query text to file with only shared lock held */
	stored = qtext_store( query, query_len,
						 &query_offset, &gc_count);

in case of query storm, to prevent this, there should be no action between first hash_search and exclusive lock promotion ...
removing this action (forcing usage of the qtext_store reserved for gc cases under exclusive lock ) seems to help a lot for this issue, but should slow other tests cases (with many distincts queries for exemple).

Ideal solution would be to move this part after hash entry creation, outside exclusive lock,
query length and offset being updated with counters (but this is a big change).

add support for PG13

adapt
pgssp_planner(Query *parse,
const char *query_string,
int cursorOptions, ParamListInfo boundParams)

that has query_text added

normalize_query syntax problems (lisibility)

Each exemple can be verified with pgssp_normalize_query() function.

  1. minus sign "-" is removed more than expected:
    select +1.1, -2, 2-1, b-a, b-1
    SELECT +?, ?, ?-?, ba, b?

  2. parenthesis "(" ")" are removed more than expected:
    insert into a values (null)
    INSERT INTO a VALUES ?
    or
    select f(1)
    SELECT f?

  3. Blank is missing:
    select a from t where b=2 order by c
    SELECT a FROM t WHERE b=?ORDER BY c

  4. non quoted identifiers are case sensitive (they should be lowercase):
    select aB,AB,ab,Ab,"xYz","Xyz" from t
    SELECT aB,AB,ab,Ab,"xYz","Xyz" FROM t

  5. variables from replaced literals should be typed:
    select 1, 1.1, 'aa'
    SELECT ?, ?, ?
    (this opens others questions about NULL consideration ...)

  6. following functions are not common litterals, they should not by replaced:
    select CURRENT_DATE, CURRENT_TIME, LOCALTIME, LOCALTIMESTAMP
    SELECT ?, ?, ?, ?

store search_path for current entry

To be able to retrieve which search_path gave which query (when same objects names are used across many schemas), it is possible to get a pgssp entry per schema by:
track_planid = true;
and
explain used with (verbose on)

note : pg12 explain (SETTINGS on) that adds search_path in explain output should even work better.

sqlplus: SET AUTOTRACE ON EXPLAIN equivalent

set pg_stat_sql_plans.track='all';
set pg_stat_sql_plans.track_planid=true;
set pg_stat_sql_plans.explain=true;
set log_min_error_statement=LOG;

postgres=# select id from A;
LOG: queryid: 7449492610579586729 planid: 7866381122914524383 plan:
Seq Scan on a (cost=0.00..35.50 rows=2550 width=4)
id


(0 rows)

--> to be added in doc

reset previous pgssp_backend_qpid(pid) on parsing error

In one psql session:
SELECT
pgsa.pid,
pgsa.state,
pgsa.query,
pgssp.queryid,
pgssp.planid,
pgssp_normalize_query(pgssp.query)
FROM
pg_stat_activity pgsa
LEFT OUTER JOIN pg_stat_sql_plans pgssp
ON pgssp_backend_qpid( pgsa.pid ) = pgssp.qpid
AND pgsa.datid = pgssp.dbid
AND pgsa.usesysid = pgssp.userid
WHERE pgsa.backend_type='client backend'
and pgsa.pid != pg_backend_pid();
\watch 1

In the second psql session:
select 1;
--> should appear in session one
select a;
ERROR: column "a" does not exist
LINE 1: select a;
--> select a;
appears as query from pg_stat_activity,
but pgssp_backend_qpid(pid) is not updated (see ProcEntryArray[i])

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.