From 2f694f7af3b79a9ec1bb7436bdb9dd4d691acb32 Mon Sep 17 00:00:00 2001 From: Lukas Fittl Date: Wed, 4 May 2016 20:23:05 -0700 Subject: [PATCH] Distributed EXPLAIN: Generate valid JSON output. This modifies the EXPLAIN output functions to actually generate valid JSON output when (FORMAT JSON) is being used. Fixes #494. --- .../distributed/planner/multi_explain.c | 20 +++- src/test/regress/expected/multi_explain.out | 96 ++++++++++--------- 2 files changed, 68 insertions(+), 48 deletions(-) diff --git a/src/backend/distributed/planner/multi_explain.c b/src/backend/distributed/planner/multi_explain.c index bb44393df..945bd2d43 100644 --- a/src/backend/distributed/planner/multi_explain.c +++ b/src/backend/distributed/planner/multi_explain.c @@ -196,6 +196,10 @@ MultiExplainOneQuery(Query *query, IntoClause *into, ExplainState *es, es->indent += 1; } + else if (es->format == EXPLAIN_FORMAT_JSON) + { + ExplainOpenGroup("Distributed Query", NULL, true, es); + } routerExecutablePlan = RouterExecutablePlan(multiPlan, TaskExecutorType); @@ -245,6 +249,13 @@ MultiExplainOneQuery(Query *query, IntoClause *into, ExplainState *es, appendStringInfo(es->str, "Master Query\n"); es->indent += 1; } + else if (es->format == EXPLAIN_FORMAT_JSON) + { + ExplainJSONLineEnding(es); + appendStringInfoSpaces(es->str, es->indent * 2); + appendStringInfo(es->str, "\"Master Query\":"); + es->grouping_stack = lcons_int(0, es->grouping_stack); + } ExplainMasterPlan(masterPlan, into, es, queryString, params, &planDuration); @@ -253,6 +264,11 @@ MultiExplainOneQuery(Query *query, IntoClause *into, ExplainState *es, es->indent -= 1; } } + + if (es->format == EXPLAIN_FORMAT_JSON) + { + ExplainCloseGroup("Distributed Query", NULL, true, es); + } } @@ -390,7 +406,7 @@ ExplainJob(Job *job, ExplainState *es) List *taskList = job->taskList; int taskCount = list_length(taskList); - ExplainOpenGroup("Job", NULL, true, es); + ExplainOpenGroup("Job", "Job", true, es); ExplainPropertyInteger("Task Count", taskCount, es); @@ -424,7 +440,7 @@ ExplainJob(Job *job, ExplainState *es) ExplainCloseGroup("Tasks", "Tasks", false, es); } - ExplainCloseGroup("Job", NULL, true, es); + ExplainCloseGroup("Job", "Job", true, es); /* show explain output for depended jobs, if any */ foreach(dependedJobCell, dependedJobList) diff --git a/src/test/regress/expected/multi_explain.out b/src/test/regress/expected/multi_explain.out index 58fb90d64..78c36c8de 100644 --- a/src/test/regress/expected/multi_explain.out +++ b/src/test/regress/expected/multi_explain.out @@ -28,56 +28,59 @@ EXPLAIN (COSTS FALSE, FORMAT JSON) SELECT l_quantity, count(*) count_quantity FROM lineitem GROUP BY l_quantity ORDER BY count_quantity, l_quantity; [ - "Executor": "Real-Time", { - "Task Count": 6, - "Tasks Shown": "One of 6", - "Tasks": [ - { - "Node": "host=localhost port=57637 dbname=regression", - "Remote Plan": [ - [ - { - "Plan": { - "Node Type": "Aggregate", - "Strategy": "Hashed", - "Group Key": ["l_quantity"], - "Plans": [ - { - "Node Type": "Seq Scan", - "Parent Relationship": "Outer", - "Relation Name": "lineitem_102010", - "Alias": "lineitem" - } - ] - } - } - ] - - ] - } - ] - }, - { - "Plan": { - "Node Type": "Sort", - "Sort Key": ["(sum(((sum(intermediate_column_41_1))::bigint)))::bigint", "intermediate_column_41_0"], - "Plans": [ + "Executor": "Real-Time", + "Job": { + "Task Count": 6, + "Tasks Shown": "One of 6", + "Tasks": [ { - "Node Type": "Aggregate", - "Strategy": "Hashed", - "Parent Relationship": "Outer", - "Group Key": ["intermediate_column_41_0"], - "Plans": [ - { - "Node Type": "Seq Scan", - "Parent Relationship": "Outer", - "Relation Name": "pg_merge_job_0041", - "Alias": "pg_merge_job_0041" - } + "Node": "host=localhost port=57637 dbname=regression", + "Remote Plan": [ + [ + { + "Plan": { + "Node Type": "Aggregate", + "Strategy": "Hashed", + "Group Key": ["l_quantity"], + "Plans": [ + { + "Node Type": "Seq Scan", + "Parent Relationship": "Outer", + "Relation Name": "lineitem_102010", + "Alias": "lineitem" + } + ] + } + } + ] + ] } ] + }, + "Master Query": + { + "Plan": { + "Node Type": "Sort", + "Sort Key": ["(sum(((sum(intermediate_column_41_1))::bigint)))::bigint", "intermediate_column_41_0"], + "Plans": [ + { + "Node Type": "Aggregate", + "Strategy": "Hashed", + "Parent Relationship": "Outer", + "Group Key": ["intermediate_column_41_0"], + "Plans": [ + { + "Node Type": "Seq Scan", + "Parent Relationship": "Outer", + "Relation Name": "pg_merge_job_0041", + "Alias": "pg_merge_job_0041" + } + ] + } + ] + } } } ] @@ -150,7 +153,8 @@ EXPLAIN (COSTS FALSE, FORMAT YAML) SELECT l_quantity, count(*) count_quantity FROM lineitem GROUP BY l_quantity ORDER BY count_quantity, l_quantity; Executor: "Real-Time" -- Task Count: 6 +Job: + Task Count: 6 Tasks Shown: "One of 6" Tasks: - Node: "host=localhost port=57637 dbname=regression"