Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request citusdata#5882 from citusdata/marcocitus/fix-explain
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocitus authored Apr 7, 2022
2 parents f9bbcb8 + c082770 commit 67fdecf
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/backend/distributed/planner/multi_explain.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ ExplainSubPlans(DistributedPlan *distributedPlan, ExplainState *es)
es->indent += 3;
}

ExplainOpenGroup("Subplan", NULL, true, es);

if (es->analyze)
{
if (es->timing)
Expand Down Expand Up @@ -358,9 +360,14 @@ ExplainSubPlans(DistributedPlan *distributedPlan, ExplainState *es)
}
#endif

ExplainOpenGroup("PlannedStmt", "PlannedStmt", false, es);

ExplainOnePlanCompat(plan, into, es, queryString, params, NULL, &planduration,
(es->buffers ? &bufusage : NULL));

ExplainCloseGroup("PlannedStmt", "PlannedStmt", false, es);
ExplainCloseGroup("Subplan", NULL, true, es);

if (es->format == EXPLAIN_FORMAT_TEXT)
{
es->indent -= 3;
Expand Down
35 changes: 35 additions & 0 deletions src/test/regress/expected/multi_explain.out
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ BEGIN
RETURN result;
END;
$BODY$ LANGUAGE plpgsql;
CREATE FUNCTION explain_analyze_json(query text)
RETURNS jsonb
AS $BODY$
DECLARE
result jsonb;
BEGIN
EXECUTE format('EXPLAIN (ANALYZE TRUE, FORMAT JSON) %s', query) INTO result;
RETURN result;
END;
$BODY$ LANGUAGE plpgsql;
-- Function that parses explain output as XML
CREATE FUNCTION explain_xml(query text)
RETURNS xml
Expand All @@ -29,6 +39,17 @@ BEGIN
RETURN result;
END;
$BODY$ LANGUAGE plpgsql;
-- Function that parses explain output as XML
CREATE FUNCTION explain_analyze_xml(query text)
RETURNS xml
AS $BODY$
DECLARE
result xml;
BEGIN
EXECUTE format('EXPLAIN (ANALYZE true, FORMAT XML) %s', query) INTO result;
RETURN result;
END;
$BODY$ LANGUAGE plpgsql;
-- VACUMM related tables to ensure test outputs are stable
VACUUM ANALYZE lineitem;
VACUUM ANALYZE orders;
Expand Down Expand Up @@ -135,6 +156,13 @@ SELECT true AS valid FROM explain_json($$
SELECT l_quantity, count(*) count_quantity FROM lineitem
GROUP BY l_quantity ORDER BY count_quantity, l_quantity$$);
t
SELECT true AS valid FROM explain_analyze_json($$
WITH a AS (
SELECT l_quantity, count(*) count_quantity FROM lineitem
GROUP BY l_quantity ORDER BY count_quantity, l_quantity LIMIT 10)
SELECT count(*) FROM a
$$);
t
-- Test XML format
EXPLAIN (COSTS FALSE, FORMAT XML)
SELECT l_quantity, count(*) count_quantity FROM lineitem
Expand Down Expand Up @@ -208,6 +236,13 @@ SELECT true AS valid FROM explain_xml($$
SELECT l_quantity, count(*) count_quantity FROM lineitem
GROUP BY l_quantity ORDER BY count_quantity, l_quantity$$);
t
SELECT true AS valid FROM explain_analyze_xml($$
WITH a AS (
SELECT l_quantity, count(*) count_quantity FROM lineitem
GROUP BY l_quantity ORDER BY count_quantity, l_quantity LIMIT 10)
SELECT count(*) FROM a
$$);
t
-- Test YAML format
EXPLAIN (COSTS FALSE, FORMAT YAML)
SELECT l_quantity, count(*) count_quantity FROM lineitem
Expand Down
38 changes: 38 additions & 0 deletions src/test/regress/sql/multi_explain.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ BEGIN
END;
$BODY$ LANGUAGE plpgsql;

CREATE FUNCTION explain_analyze_json(query text)
RETURNS jsonb
AS $BODY$
DECLARE
result jsonb;
BEGIN
EXECUTE format('EXPLAIN (ANALYZE TRUE, FORMAT JSON) %s', query) INTO result;
RETURN result;
END;
$BODY$ LANGUAGE plpgsql;

-- Function that parses explain output as XML
CREATE FUNCTION explain_xml(query text)
RETURNS xml
Expand All @@ -36,6 +47,19 @@ BEGIN
END;
$BODY$ LANGUAGE plpgsql;

-- Function that parses explain output as XML
CREATE FUNCTION explain_analyze_xml(query text)
RETURNS xml
AS $BODY$
DECLARE
result xml;
BEGIN
EXECUTE format('EXPLAIN (ANALYZE true, FORMAT XML) %s', query) INTO result;
RETURN result;
END;
$BODY$ LANGUAGE plpgsql;


-- VACUMM related tables to ensure test outputs are stable
VACUUM ANALYZE lineitem;
VACUUM ANALYZE orders;
Expand Down Expand Up @@ -63,6 +87,13 @@ SELECT true AS valid FROM explain_json($$
SELECT l_quantity, count(*) count_quantity FROM lineitem
GROUP BY l_quantity ORDER BY count_quantity, l_quantity$$);

SELECT true AS valid FROM explain_analyze_json($$
WITH a AS (
SELECT l_quantity, count(*) count_quantity FROM lineitem
GROUP BY l_quantity ORDER BY count_quantity, l_quantity LIMIT 10)
SELECT count(*) FROM a
$$);

-- Test XML format
EXPLAIN (COSTS FALSE, FORMAT XML)
SELECT l_quantity, count(*) count_quantity FROM lineitem
Expand All @@ -73,6 +104,13 @@ SELECT true AS valid FROM explain_xml($$
SELECT l_quantity, count(*) count_quantity FROM lineitem
GROUP BY l_quantity ORDER BY count_quantity, l_quantity$$);

SELECT true AS valid FROM explain_analyze_xml($$
WITH a AS (
SELECT l_quantity, count(*) count_quantity FROM lineitem
GROUP BY l_quantity ORDER BY count_quantity, l_quantity LIMIT 10)
SELECT count(*) FROM a
$$);

-- Test YAML format
EXPLAIN (COSTS FALSE, FORMAT YAML)
SELECT l_quantity, count(*) count_quantity FROM lineitem
Expand Down

0 comments on commit 67fdecf

Please sign in to comment.