Commit 41de93c5 authored by Tom Lane's avatar Tom Lane

Reduce indentation/parenthesization of set operations in rule/view dumps.

A query such as "SELECT x UNION SELECT y UNION SELECT z UNION ..."
produces a left-deep nested parse tree, which we formerly showed in its
full nested glory and with all the possible parentheses.  This does little
for readability, though, and long UNION lists resulting in excessive
indentation are common.  Instead, let's omit parentheses and indent all
the subqueries at the same level in such cases.

This patch skips indentation/parenthesization whenever the lefthand input
of a SetOperationStmt is another SetOperationStmt of the same kind and
ALL/DISTINCT property.  We could teach the code the exact syntactic
precedence of set operations and thereby avoid parenthesization in some
more cases, but it's not clear that that'd be a readability win: it seems
better to parenthesize if the set operation changes.  (As an example,
if there's one UNION in a long list of UNION ALL, it now stands out like
a sore thumb, which seems like a good thing.)

Back-patch to 9.3.  This completes our response to a complaint from Greg
Stark that since commit 62e66640 there's a performance problem in pg_dump
for views containing long UNION sequences (or other types of deeply nested
constructs).  The previous commit 0601cb54
handles the general problem, but this one makes the specific case of UNION
lists look a lot nicer.
parent 0601cb54
...@@ -4714,42 +4714,59 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context, ...@@ -4714,42 +4714,59 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
else if (IsA(setOp, SetOperationStmt)) else if (IsA(setOp, SetOperationStmt))
{ {
SetOperationStmt *op = (SetOperationStmt *) setOp; SetOperationStmt *op = (SetOperationStmt *) setOp;
int subindent;
if (PRETTY_INDENT(context))
{
context->indentLevel += PRETTYINDENT_STD;
appendStringInfoSpaces(buf, PRETTYINDENT_STD);
}
/* /*
* We force parens whenever nesting two SetOperationStmts. There are * We force parens when nesting two SetOperationStmts, except when the
* some cases in which parens are needed around a leaf query too, but * lefthand input is another setop of the same kind. Syntactically,
* those are more easily handled at the next level down (see code * we could omit parens in rather more cases, but it seems best to use
* above). * parens to flag cases where the setop operator changes. If we use
* parens, we also increase the indentation level for the child query.
*
* There are some cases in which parens are needed around a leaf query
* too, but those are more easily handled at the next level down (see
* code above).
*/ */
need_paren = !IsA(op->larg, RangeTblRef); if (IsA(op->larg, SetOperationStmt))
{
SetOperationStmt *lop = (SetOperationStmt *) op->larg;
if (op->op == lop->op && op->all == lop->all)
need_paren = false;
else
need_paren = true;
}
else
need_paren = false;
if (need_paren) if (need_paren)
{
appendStringInfoChar(buf, '('); appendStringInfoChar(buf, '(');
subindent = PRETTYINDENT_STD;
appendContextKeyword(context, "", subindent, 0, 0);
}
else
subindent = 0;
get_setop_query(op->larg, query, context, resultDesc); get_setop_query(op->larg, query, context, resultDesc);
if (need_paren)
appendStringInfoChar(buf, ')');
if (!PRETTY_INDENT(context)) if (need_paren)
appendContextKeyword(context, ") ", -subindent, 0, 0);
else if (PRETTY_INDENT(context))
appendContextKeyword(context, "", -subindent, 0, 0);
else
appendStringInfoChar(buf, ' '); appendStringInfoChar(buf, ' ');
switch (op->op) switch (op->op)
{ {
case SETOP_UNION: case SETOP_UNION:
appendContextKeyword(context, "UNION ", appendStringInfoString(buf, "UNION ");
-PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
break; break;
case SETOP_INTERSECT: case SETOP_INTERSECT:
appendContextKeyword(context, "INTERSECT ", appendStringInfoString(buf, "INTERSECT ");
-PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
break; break;
case SETOP_EXCEPT: case SETOP_EXCEPT:
appendContextKeyword(context, "EXCEPT ", appendStringInfoString(buf, "EXCEPT ");
-PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
break; break;
default: default:
elog(ERROR, "unrecognized set op: %d", elog(ERROR, "unrecognized set op: %d",
...@@ -4758,19 +4775,29 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context, ...@@ -4758,19 +4775,29 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
if (op->all) if (op->all)
appendStringInfoString(buf, "ALL "); appendStringInfoString(buf, "ALL ");
if (PRETTY_INDENT(context)) /* Always parenthesize if RHS is another setop */
appendContextKeyword(context, "", 0, 0, 0); need_paren = IsA(op->rarg, SetOperationStmt);
need_paren = !IsA(op->rarg, RangeTblRef);
/*
* The indentation code here is deliberately a bit different from that
* for the lefthand input, because we want the line breaks in
* different places.
*/
if (need_paren) if (need_paren)
{
appendStringInfoChar(buf, '('); appendStringInfoChar(buf, '(');
subindent = PRETTYINDENT_STD;
}
else
subindent = 0;
appendContextKeyword(context, "", subindent, 0, 0);
get_setop_query(op->rarg, query, context, resultDesc); get_setop_query(op->rarg, query, context, resultDesc);
if (need_paren)
appendStringInfoChar(buf, ')');
if (PRETTY_INDENT(context)) if (PRETTY_INDENT(context))
context->indentLevel -= PRETTYINDENT_STD; context->indentLevel -= subindent;
if (need_paren)
appendContextKeyword(context, ")", 0, 0, 0);
} }
else else
{ {
......
This diff is collapsed.
...@@ -346,13 +346,13 @@ CREATE VIEW v_test2 AS SELECT moo, 2*moo FROM v_test1 UNION ALL SELECT moo, 3*mo ...@@ -346,13 +346,13 @@ CREATE VIEW v_test2 AS SELECT moo, 2*moo FROM v_test1 UNION ALL SELECT moo, 3*mo
moo | integer | | plain | moo | integer | | plain |
?column? | integer | | plain | ?column? | integer | | plain |
View definition: View definition:
SELECT v_test1.moo, SELECT v_test1.moo,
2 * v_test1.moo 2 * v_test1.moo
FROM v_test1 FROM v_test1
UNION ALL UNION ALL
SELECT v_test1.moo, SELECT v_test1.moo,
3 * v_test1.moo 3 * v_test1.moo
FROM v_test1; FROM v_test1;
CREATE MATERIALIZED VIEW mv_test2 AS SELECT moo, 2*moo FROM v_test2 UNION ALL SELECT moo, 3*moo FROM v_test2; CREATE MATERIALIZED VIEW mv_test2 AS SELECT moo, 2*moo FROM v_test2 UNION ALL SELECT moo, 3*moo FROM v_test2;
\d+ mv_test2 \d+ mv_test2
...@@ -362,13 +362,13 @@ CREATE MATERIALIZED VIEW mv_test2 AS SELECT moo, 2*moo FROM v_test2 UNION ALL SE ...@@ -362,13 +362,13 @@ CREATE MATERIALIZED VIEW mv_test2 AS SELECT moo, 2*moo FROM v_test2 UNION ALL SE
moo | integer | | plain | | moo | integer | | plain | |
?column? | integer | | plain | | ?column? | integer | | plain | |
View definition: View definition:
SELECT v_test2.moo, SELECT v_test2.moo,
2 * v_test2.moo 2 * v_test2.moo
FROM v_test2 FROM v_test2
UNION ALL UNION ALL
SELECT v_test2.moo, SELECT v_test2.moo,
3 * v_test2.moo 3 * v_test2.moo
FROM v_test2; FROM v_test2;
CREATE MATERIALIZED VIEW mv_test3 AS SELECT * FROM mv_test2 WHERE moo = 12345; CREATE MATERIALIZED VIEW mv_test3 AS SELECT * FROM mv_test2 WHERE moo = 12345;
SELECT relispopulated FROM pg_class WHERE oid = 'mv_test3'::regclass; SELECT relispopulated FROM pg_class WHERE oid = 'mv_test3'::regclass;
......
This diff is collapsed.
...@@ -300,48 +300,48 @@ SELECT * FROM vsubdepartment ORDER BY name; ...@@ -300,48 +300,48 @@ SELECT * FROM vsubdepartment ORDER BY name;
-- Check reverse listing -- Check reverse listing
SELECT pg_get_viewdef('vsubdepartment'::regclass); SELECT pg_get_viewdef('vsubdepartment'::regclass);
pg_get_viewdef pg_get_viewdef
------------------------------------------------------- -----------------------------------------------
WITH RECURSIVE subdepartment AS ( + WITH RECURSIVE subdepartment AS ( +
SELECT department.id, + SELECT department.id, +
department.parent_department, + department.parent_department, +
department.name + department.name +
FROM department + FROM department +
WHERE (department.name = 'A'::text)+ WHERE (department.name = 'A'::text)+
UNION ALL + UNION ALL +
SELECT d.id, + SELECT d.id, +
d.parent_department, + d.parent_department, +
d.name + d.name +
FROM department d, + FROM department d, +
subdepartment sd + subdepartment sd +
WHERE (d.parent_department = sd.id)+ WHERE (d.parent_department = sd.id)+
) + ) +
SELECT subdepartment.id, + SELECT subdepartment.id, +
subdepartment.parent_department, + subdepartment.parent_department, +
subdepartment.name + subdepartment.name +
FROM subdepartment; FROM subdepartment;
(1 row) (1 row)
SELECT pg_get_viewdef('vsubdepartment'::regclass, true); SELECT pg_get_viewdef('vsubdepartment'::regclass, true);
pg_get_viewdef pg_get_viewdef
----------------------------------------------------- ---------------------------------------------
WITH RECURSIVE subdepartment AS ( + WITH RECURSIVE subdepartment AS ( +
SELECT department.id, + SELECT department.id, +
department.parent_department, + department.parent_department, +
department.name + department.name +
FROM department + FROM department +
WHERE department.name = 'A'::text+ WHERE department.name = 'A'::text+
UNION ALL + UNION ALL +
SELECT d.id, + SELECT d.id, +
d.parent_department, + d.parent_department, +
d.name + d.name +
FROM department d, + FROM department d, +
subdepartment sd + subdepartment sd +
WHERE d.parent_department = sd.id+ WHERE d.parent_department = sd.id+
) + ) +
SELECT subdepartment.id, + SELECT subdepartment.id, +
subdepartment.parent_department, + subdepartment.parent_department, +
subdepartment.name + subdepartment.name +
FROM subdepartment; FROM subdepartment;
(1 row) (1 row)
...@@ -360,11 +360,11 @@ SELECT sum(n) FROM t; ...@@ -360,11 +360,11 @@ SELECT sum(n) FROM t;
sum | bigint | | plain | sum | bigint | | plain |
View definition: View definition:
WITH RECURSIVE t(n) AS ( WITH RECURSIVE t(n) AS (
VALUES (1) VALUES (1)
UNION ALL UNION ALL
SELECT t_1.n + 1 SELECT t_1.n + 1
FROM t t_1 FROM t t_1
WHERE t_1.n < 100 WHERE t_1.n < 100
) )
SELECT sum(t.n) AS sum SELECT sum(t.n) AS sum
FROM t; FROM t;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment