Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf260990

Browse files
committed
Improve EXPLAIN to print the grouping columns in Agg and Group nodes.
Per request from Kevin Grittner.
1 parent8693559 commitf260990

File tree

6 files changed

+92
-30
lines changed

6 files changed

+92
-30
lines changed

‎doc/src/sgml/ref/explain.sgml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,14 @@ PREPARE query(int, int) AS SELECT sum(bar) FROM test
395395

396396
EXPLAIN ANALYZE EXECUTE query(100, 200);
397397

398-
QUERY PLAN
399-
-------------------------------------------------------------------------------------------------------------------------
400-
HashAggregate (cost=39.53..39.53 rows=1 width=8) (actual time=0.661..0.672 rows=7 loops=1)
401-
-> Index Scan using test_pkey on test (cost=0.00..32.97 rows=1311 width=8) (actual time=0.050..0.395 rows=99 loops=1)
398+
QUERY PLAN
399+
------------------------------------------------------------------------------------------------------------------------
400+
HashAggregate (cost=9.54..9.54 rows=1 width=8) (actual time=0.156..0.161 rows=11 loops=1)
401+
Group Key: foo
402+
-> Index Scan using test_pkey on test (cost=0.29..9.29 rows=50 width=8) (actual time=0.039..0.091 rows=99 loops=1)
402403
Index Cond: ((id > $1) AND (id < $2))
403-
Total runtime: 0.851 ms
404-
(4 rows)
404+
Total runtime: 0.225 ms
405+
(5 rows)
405406
</programlisting>
406407
</para>
407408

‎src/backend/commands/explain.c

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,13 @@ static void show_sort_keys(SortState *sortstate, List *ancestors,
7676
ExplainState*es);
7777
staticvoidshow_merge_append_keys(MergeAppendState*mstate,List*ancestors,
7878
ExplainState*es);
79-
staticvoidshow_sort_keys_common(PlanState*planstate,
80-
intnkeys,AttrNumber*keycols,
81-
List*ancestors,ExplainState*es);
79+
staticvoidshow_agg_keys(AggState*astate,List*ancestors,
80+
ExplainState*es);
81+
staticvoidshow_group_keys(GroupState*gstate,List*ancestors,
82+
ExplainState*es);
83+
staticvoidshow_sort_group_keys(PlanState*planstate,constchar*qlabel,
84+
intnkeys,AttrNumber*keycols,
85+
List*ancestors,ExplainState*es);
8286
staticvoidshow_sort_info(SortState*sortstate,ExplainState*es);
8387
staticvoidshow_hash_info(HashState*hashstate,ExplainState*es);
8488
staticvoidshow_instrumentation_count(constchar*qlabel,intwhich,
@@ -1341,7 +1345,14 @@ ExplainNode(PlanState *planstate, List *ancestors,
13411345
planstate,es);
13421346
break;
13431347
caseT_Agg:
1348+
show_agg_keys((AggState*)planstate,ancestors,es);
1349+
show_upper_qual(plan->qual,"Filter",planstate,ancestors,es);
1350+
if (plan->qual)
1351+
show_instrumentation_count("Rows Removed by Filter",1,
1352+
planstate,es);
1353+
break;
13441354
caseT_Group:
1355+
show_group_keys((GroupState*)planstate,ancestors,es);
13451356
show_upper_qual(plan->qual,"Filter",planstate,ancestors,es);
13461357
if (plan->qual)
13471358
show_instrumentation_count("Rows Removed by Filter",1,
@@ -1693,9 +1704,9 @@ show_sort_keys(SortState *sortstate, List *ancestors, ExplainState *es)
16931704
{
16941705
Sort*plan= (Sort*)sortstate->ss.ps.plan;
16951706

1696-
show_sort_keys_common((PlanState*)sortstate,
1697-
plan->numCols,plan->sortColIdx,
1698-
ancestors,es);
1707+
show_sort_group_keys((PlanState*)sortstate,"Sort Key",
1708+
plan->numCols,plan->sortColIdx,
1709+
ancestors,es);
16991710
}
17001711

17011712
/*
@@ -1707,14 +1718,56 @@ show_merge_append_keys(MergeAppendState *mstate, List *ancestors,
17071718
{
17081719
MergeAppend*plan= (MergeAppend*)mstate->ps.plan;
17091720

1710-
show_sort_keys_common((PlanState*)mstate,
1711-
plan->numCols,plan->sortColIdx,
1712-
ancestors,es);
1721+
show_sort_group_keys((PlanState*)mstate,"Sort Key",
1722+
plan->numCols,plan->sortColIdx,
1723+
ancestors,es);
17131724
}
17141725

1726+
/*
1727+
* Show the grouping keys for an Agg node.
1728+
*/
1729+
staticvoid
1730+
show_agg_keys(AggState*astate,List*ancestors,
1731+
ExplainState*es)
1732+
{
1733+
Agg*plan= (Agg*)astate->ss.ps.plan;
1734+
1735+
if (plan->numCols>0)
1736+
{
1737+
/* The key columns refer to the tlist of the child plan */
1738+
ancestors=lcons(astate,ancestors);
1739+
show_sort_group_keys(outerPlanState(astate),"Group Key",
1740+
plan->numCols,plan->grpColIdx,
1741+
ancestors,es);
1742+
ancestors=list_delete_first(ancestors);
1743+
}
1744+
}
1745+
1746+
/*
1747+
* Show the grouping keys for a Group node.
1748+
*/
1749+
staticvoid
1750+
show_group_keys(GroupState*gstate,List*ancestors,
1751+
ExplainState*es)
1752+
{
1753+
Group*plan= (Group*)gstate->ss.ps.plan;
1754+
1755+
/* The key columns refer to the tlist of the child plan */
1756+
ancestors=lcons(gstate,ancestors);
1757+
show_sort_group_keys(outerPlanState(gstate),"Group Key",
1758+
plan->numCols,plan->grpColIdx,
1759+
ancestors,es);
1760+
ancestors=list_delete_first(ancestors);
1761+
}
1762+
1763+
/*
1764+
* Common code to show sort/group keys, which are represented in plan nodes
1765+
* as arrays of targetlist indexes
1766+
*/
17151767
staticvoid
1716-
show_sort_keys_common(PlanState*planstate,intnkeys,AttrNumber*keycols,
1717-
List*ancestors,ExplainState*es)
1768+
show_sort_group_keys(PlanState*planstate,constchar*qlabel,
1769+
intnkeys,AttrNumber*keycols,
1770+
List*ancestors,ExplainState*es)
17181771
{
17191772
Plan*plan=planstate->plan;
17201773
List*context;
@@ -1748,7 +1801,7 @@ show_sort_keys_common(PlanState *planstate, int nkeys, AttrNumber *keycols,
17481801
result=lappend(result,exprstr);
17491802
}
17501803

1751-
ExplainPropertyList("Sort Key",result,es);
1804+
ExplainPropertyList(qlabel,result,es);
17521805
}
17531806

17541807
/*

‎src/test/regress/expected/aggregates.out

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,12 +659,13 @@ explain (costs off)
659659
QUERY PLAN
660660
---------------------------------------------------------------------
661661
HashAggregate
662+
Group Key: $0
662663
InitPlan 1 (returns $0)
663664
-> Limit
664665
-> Index Only Scan Backward using tenk1_unique2 on tenk1
665666
Index Cond: (unique2 IS NOT NULL)
666667
-> Result
667-
(6 rows)
668+
(7 rows)
668669

669670
select distinct max(unique2) from tenk1;
670671
max
@@ -806,6 +807,7 @@ explain (costs off)
806807
QUERY PLAN
807808
----------------------------------------------------------------------------------------------
808809
HashAggregate
810+
Group Key: $0, $1
809811
InitPlan 1 (returns $0)
810812
-> Limit
811813
-> Merge Append
@@ -831,7 +833,7 @@ explain (costs off)
831833
-> Index Only Scan Backward using minmaxtest3i on minmaxtest3 minmaxtest3_1
832834
Index Cond: (f1 IS NOT NULL)
833835
-> Result
834-
(26 rows)
836+
(27 rows)
835837

836838
select distinct min(f1), max(f1) from minmaxtest;
837839
min | max

‎src/test/regress/expected/matview.out

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ EXPLAIN (costs off)
2222
QUERY PLAN
2323
---------------------
2424
HashAggregate
25+
Group Key: type
2526
-> Seq Scan on t
26-
(2 rows)
27+
(3 rows)
2728

2829
CREATE MATERIALIZED VIEW tm AS SELECT type, sum(amt) AS totamt FROM t GROUP BY type WITH NO DATA;
2930
SELECT relispopulated FROM pg_class WHERE oid = 'tm'::regclass;
@@ -59,8 +60,9 @@ EXPLAIN (costs off)
5960
Sort
6061
Sort Key: t.type
6162
-> HashAggregate
63+
Group Key: t.type
6264
-> Seq Scan on t
63-
(4 rows)
65+
(5 rows)
6466

6567
CREATE MATERIALIZED VIEW tvm AS SELECT * FROM tv ORDER BY type;
6668
SELECT * FROM tvm;
@@ -82,8 +84,9 @@ EXPLAIN (costs off)
8284
---------------------------
8385
Aggregate
8486
-> HashAggregate
87+
Group Key: t.type
8588
-> Seq Scan on t
86-
(3 rows)
89+
(4 rows)
8790

8891
CREATE MATERIALIZED VIEW tvvm AS SELECT * FROM tvv;
8992
CREATE VIEW tvvmv AS SELECT * FROM tvvm;

‎src/test/regress/expected/union.out

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,12 +494,13 @@ explain (costs off)
494494
QUERY PLAN
495495
---------------------------------------------------
496496
HashAggregate
497+
Group Key: ((t1.a || t1.b))
497498
-> Append
498499
-> Index Scan using t1_ab_idx on t1
499500
Index Cond: ((a || b) = 'ab'::text)
500501
-> Index Only Scan using t2_pkey on t2
501502
Index Cond: (ab = 'ab'::text)
502-
(6 rows)
503+
(7 rows)
503504

504505
reset enable_seqscan;
505506
reset enable_indexscan;
@@ -552,17 +553,18 @@ SELECT * FROM
552553
SELECT 2 AS t, 4 AS x) ss
553554
WHERE x < 4
554555
ORDER BY x;
555-
QUERY PLAN
556-
--------------------------------
556+
QUERY PLAN
557+
--------------------------------------------------------
557558
Sort
558559
Sort Key: ss.x
559560
-> Subquery Scan on ss
560561
Filter: (ss.x < 4)
561562
-> HashAggregate
563+
Group Key: (1), (generate_series(1, 10))
562564
-> Append
563565
-> Result
564566
-> Result
565-
(8 rows)
567+
(9 rows)
566568

567569
SELECT * FROM
568570
(SELECT 1 AS t, generate_series(1,10) AS x

‎src/test/regress/expected/window.out

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -619,12 +619,13 @@ explain (costs off)
619619
select first_value(max(x)) over (), y
620620
from (select unique1 as x, ten+four as y from tenk1) ss
621621
group by y;
622-
QUERY PLAN
623-
-------------------------------
622+
QUERY PLAN
623+
---------------------------------------------
624624
WindowAgg
625625
-> HashAggregate
626+
Group Key: (tenk1.ten + tenk1.four)
626627
-> Seq Scan on tenk1
627-
(3 rows)
628+
(4 rows)
628629

629630
-- test non-default frame specifications
630631
SELECT four, ten,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp