Analyzing execution plans is made easier with curating execution plan, statistics of database objects such as tables, indexes, or columns involved in the actual runtime execution plan, all within a single report. This makes it easier to share among team members or external forum and reduces the need for additional information requests.
This tool automates the curation of object statistics when analyzing problematic execution plans in PostgreSQL using an HTML template embded in sql file and thepsql
command line.
Execution plan is generated either byExplain Analyze Buffers
or only withExplain
and stored in a plantable.
Using thepsql
command line, the current statistics of all database objects involved in Execution plan are fetched and included in the HTML output. The output can also be integrated with thePEV2 visualiser.
Setup Databases with necessary wrappers code that generate execution plan and stored it in plantable.
This tool uses thepg_stat_statements
view to extract runtime information of problematic SQL usingqueryid
.It can also be used as filters to gather Generic Plan frompg_stat_statements
.Thepgstattuple
extension is used to extract bloat-related information, though it is optional.
Name | Description
--------------------+------------------------------------------------------------------------
pg_stat_statements | track planning and execution statistics of all SQL statements executed
pgstattuple | show tuple-level statistics
Gathering statistics requires traversing all execution plan steps and extracting the objects involved. We have built all necessary wrappers with functions and views within theplanstats
schema. You need to set it up on the concerned databases.
PGPASSWORD=********psql -h<<PostgresHost>>-U <<SuperUser>> -d <<Databases>> -f pg_sqltxplan/initialsetup.sql
We have multiple options to generate report either directly from pg_stat_statements using GENERIC_PLAN supported since PostgreSQL 16 or run SQL within wrapper functions(run_plan_analyze/run_plan_explain)
Using Dollar Quoting enclosed problematic SQL as input and run it using functionrun_plan_analyze
defined inplanstats
schema.
plantest=#select planstats.run_plan_analyze($$select count(1) from emp$$);
run_plan_analyze
-------------------------
(1,7335632667878063635)
(1row)
It will return internal planid and queryid for further references.
In next steps, we will generate pg_sqltxplain report usingpsql
command line.If no Filter is provided by default it will generate report on last plan analyzed(max-planid).
PGPASSWORD=*********psql -h<<PostgresHost>>-U <<PGuser>> -d <<Databases>> -q -v ON_ERROR_STOP=1 -v query_id=7335632667878063635 -f pg_sqltxplain.sql
Gathering Database Object Stats for Query ID(7335632667878063635)
Underlying Statistics curated for Query(7335632667878063635) - Output File Stats_Via_Explain_Analyze_7335632667878063635.html
Please note - Replace Host, DBname and Password as per your DB instances.
Using Dollar Quoting enclosed problematic SQL as input and run it using functionrun_plan_explain
defined inplanstats
schema.
plantest=#select planstats.run_plan_explain($$select count(1) from emp$$);
run_plan_analyze
-------------------------
(1,7335632667878063635)
(1row)
In next steps, we will generate pg_sqltxplain report usingpsql
command line.If no Filter is provided by default it will generate report on last plan analyzed(max-planid).
PGPASSWORD=*********psql -h<<PostgresHost>>-U <<PGuser>> -d <<Databases>> -q -v ON_ERROR_STOP=1 -v query_id=7335632667878063635 -f pg_sqltxplain.sql
Gathering Database Object Stats for Query ID(7335632667878063635)
Underlying Statistics curated for Query(7335632667878063635) - Output File pg_sqltxplain_7335632667878063635.html
Using -v option ofpsql
,we can passqueryid
filters along withpg_stat_statements
to use internal performance views to extract query metadata. It internally usedGENERIC_PLAN
plan options to generate underlying explain plan usingquery
column.
PGPASSWORD=*********psql -h<<PostgresHost>>-U <<PGuser>> -d <<Databases>> -q -v ON_ERROR_STOP=1 -v query_id=8192079375982646892 -v pg_stat_statements= -f pg_sqltxplain.sql
Integrate Execution plan objects statistics withPEV2 visualisera graphical vizualization of a PostgreSQL execution plan.
With any of the options mentioned previously, we can choose to get underlying stats of Objects with auto integrated it with PEV2. Internally it use two sql file to generate couple of html report as we are using iframe html tag to take care of different stylesheet.
Please note that we will need to share both generated HTML files.
PGPASSWORD=*********psql -h<<PostgresHost>>-U <<PGuser>> -d <<Databases>> -q -v ON_ERROR_STOP=1 -f explain_dalibo.sql -f pg_sqltxplain_with_dalibo.sql
Check out sample html report created using pg_sqltxplan utility.
Feel free to mail us([email protected]
) for any issues or consulting on PostgreSQL performance Tuning.