From a08edd1abab5dda4ecaafc42bc46cd74a4332231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E6=8C=83?= Date: Tue, 19 May 2020 10:22:01 +0800 Subject: [PATCH v1] Add query characters information to pg_stat_statements which will be helpful to: 1. Find which query is missing index more accuracy 2. Find which query is using subplan/subquery which are usually bad for optimizer. --- .../pg_stat_statements--1.7--1.8.sql | 7 ++++++- .../pg_stat_statements/pg_stat_statements.c | 21 +++++++++++++++++-- src/backend/optimizer/path/allpaths.c | 8 +++++-- src/backend/optimizer/plan/planner.c | 7 +++++++ src/backend/optimizer/plan/subselect.c | 1 + src/include/optimizer/optimizer.h | 11 ++++++++++ 6 files changed, 50 insertions(+), 5 deletions(-) diff --git a/contrib/pg_stat_statements/pg_stat_statements--1.7--1.8.sql b/contrib/pg_stat_statements/pg_stat_statements--1.7--1.8.sql index 0f63f08f7e..b649812b7e 100644 --- a/contrib/pg_stat_statements/pg_stat_statements--1.7--1.8.sql +++ b/contrib/pg_stat_statements/pg_stat_statements--1.7--1.8.sql @@ -44,7 +44,12 @@ CREATE FUNCTION pg_stat_statements(IN showtext boolean, OUT blk_write_time float8, OUT wal_records int8, OUT wal_fpi int8, - OUT wal_bytes numeric + OUT wal_bytes numeric, + OUT subplan int8, + OUT subquery int8, + OUT joincnt int8, + OUT hasagg bool, + OUT hasgroup bool ) RETURNS SETOF record AS 'MODULE_PATHNAME', 'pg_stat_statements_1_8' diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c index 4ce25fb88a..d030097ebd 100644 --- a/contrib/pg_stat_statements/pg_stat_statements.c +++ b/contrib/pg_stat_statements/pg_stat_statements.c @@ -69,6 +69,7 @@ #include "mb/pg_wchar.h" #include "miscadmin.h" #include "optimizer/planner.h" +#include "optimizer/optimizer.h" #include "parser/analyze.h" #include "parser/parsetree.h" #include "parser/scanner.h" @@ -191,6 +192,11 @@ typedef struct Counters int64 wal_records; /* # of WAL records generated */ int64 wal_fpi; /* # of WAL full page images generated */ uint64 wal_bytes; /* total amount of WAL bytes generated */ + int subplan; /* No. of subplan in this query */ + int subquery; /* No. of subquery */ + int joincnt; /* How many relations are joined */ + bool hasagg; /* if we have agg function in this query */ + bool hasgroup; /* has group clause */ } Counters; /* @@ -1434,6 +1440,11 @@ pgss_store(const char *query, uint64 queryId, e->counters.wal_records += walusage->wal_records; e->counters.wal_fpi += walusage->wal_fpi; e->counters.wal_bytes += walusage->wal_bytes; + e->counters.subplan = query_characters.subplan_count; + e->counters.subquery = query_characters.subquery_count; + e->counters.joincnt = query_characters.join_count; + e->counters.hasagg = query_characters.hasagg; + e->counters.hasgroup = query_characters.hasgroup; SpinLockRelease(&e->mutex); } @@ -1481,8 +1492,8 @@ pg_stat_statements_reset(PG_FUNCTION_ARGS) #define PG_STAT_STATEMENTS_COLS_V1_1 18 #define PG_STAT_STATEMENTS_COLS_V1_2 19 #define PG_STAT_STATEMENTS_COLS_V1_3 23 -#define PG_STAT_STATEMENTS_COLS_V1_8 32 -#define PG_STAT_STATEMENTS_COLS 32 /* maximum of above */ +#define PG_STAT_STATEMENTS_COLS_V1_8 37 +#define PG_STAT_STATEMENTS_COLS 37 /* maximum of above */ /* * Retrieve statement statistics. @@ -1834,6 +1845,12 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo, ObjectIdGetDatum(0), Int32GetDatum(-1)); values[i++] = wal_bytes; + + values[i++] = tmp.subplan; + values[i++] = tmp.subquery; + values[i++] = tmp.joincnt; + values[i++] = tmp.hasagg; + values[i++] = tmp.hasgroup; } Assert(i == (api_version == PGSS_V1_0 ? PG_STAT_STATEMENTS_COLS_V1_0 : diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index 255f56b827..277a0ecc46 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -2189,6 +2189,8 @@ set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel, RelOptInfo *sub_final_rel; ListCell *lc; + query_characters.subquery_count++; + /* * Must copy the Query so that planning doesn't mess up the RTE contents * (really really need to fix the planner to not scribble on its input, @@ -2961,6 +2963,8 @@ make_rel_from_joinlist(PlannerInfo *root, List *joinlist) */ levels_needed = list_length(joinlist); + query_characters.join_count += levels_needed; + if (levels_needed <= 0) return NULL; /* nothing to do? */ @@ -3865,8 +3869,8 @@ compute_parallel_worker(RelOptInfo *rel, double heap_pages, double index_pages, /* * generate_partitionwise_join_paths - * Create paths representing partitionwise join for given partitioned - * join relation. + * Create paths representing partitionwise join for given partitioned + * join relation. * * This must not be called until after we are done adding paths for all * child-joins. Otherwise, add_path might delete a path to which some path diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index e664eb18c0..e0f57c6c97 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -70,6 +70,8 @@ double cursor_tuple_fraction = DEFAULT_CURSOR_TUPLE_FRACTION; int force_parallel_mode = FORCE_PARALLEL_OFF; bool parallel_leader_participation = true; +QueryCharacters query_characters = {0}; + /* Hook for plugins to get control in planner() */ planner_hook_type planner_hook = NULL; @@ -290,6 +292,8 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions, ListCell *lp, *lr; + memset(&query_characters, 0, sizeof(QueryCharacters)); + /* * Set up global state for this planner invocation. This data is needed * across all levels of sub-Query that might exist in the given command, @@ -3812,6 +3816,9 @@ create_grouping_paths(PlannerInfo *root, RelOptInfo *grouped_rel; RelOptInfo *partially_grouped_rel; + query_characters.hasagg |= parse->hasAggs; + query_characters.hasgroup |= parse->groupClause != NIL; + /* * Create grouping relation to hold fully aggregated grouping and/or * aggregation paths. diff --git a/src/backend/optimizer/plan/subselect.c b/src/backend/optimizer/plan/subselect.c index b02fcb9bfe..7e3e66b0b8 100644 --- a/src/backend/optimizer/plan/subselect.c +++ b/src/backend/optimizer/plan/subselect.c @@ -1884,6 +1884,7 @@ process_sublinks_mutator(Node *node, process_sublinks_context *context) /* * Now build the SubPlan node and make the expr to return. */ + query_characters.subplan_count++; return make_subplan(context->root, (Query *) sublink->subselect, sublink->subLinkType, diff --git a/src/include/optimizer/optimizer.h b/src/include/optimizer/optimizer.h index 3e4171056e..c38a87bb9e 100644 --- a/src/include/optimizer/optimizer.h +++ b/src/include/optimizer/optimizer.h @@ -51,6 +51,17 @@ struct ParamListInfoData; struct HeapTupleData; +typedef struct +{ + int subplan_count; + int subquery_count; + int join_count; + bool hasagg; + bool hasgroup; +} QueryCharacters; + +extern QueryCharacters query_characters; + /* in path/clausesel.c: */ extern Selectivity clause_selectivity(PlannerInfo *root, -- 2.21.0