aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2024-11-20 12:03:47 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2024-11-20 12:03:47 -0500
commit630520c22fbb03a68c51e150352efcd20ecde728 (patch)
tree5f2fa4b4c0e4b8a6c8ea5b1c0cd1e2b454c9e67a
parent6b66dba3f8d8b5f9fc761749e5f0643829f09307 (diff)
downloadpostgresql-630520c22fbb03a68c51e150352efcd20ecde728.tar.gz
postgresql-630520c22fbb03a68c51e150352efcd20ecde728.zip
Avoid assertion failure if a setop leaf query contains setops.
Ordinarily transformSetOperationTree will collect all UNION/ INTERSECT/EXCEPT steps into the setOperations tree of the topmost Query, so that leaf queries do not contain any setOperations. However, it cannot thus flatten a subquery that also contains WITH, ORDER BY, FOR UPDATE, or LIMIT. I (tgl) forgot that in commit 07b4c48b6 and wrote an assertion in rule deparsing that a leaf's setOperations would always be empty. If it were nonempty then we would want to parenthesize the subquery to ensure that the output represents the setop nesting correctly (e.g. UNION below INTERSECT had better get parenthesized). So rather than just removing the faulty Assert, let's change it into an additional case to check to decide whether to add parens. We don't expect that the additional case will ever fire, but it's cheap insurance. Man Zeng and Tom Lane Discussion: https://postgr.es/m/tencent_7ABF9B1F23B0C77606FC5FE3@qq.com
-rw-r--r--src/backend/utils/adt/ruleutils.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 0191858bd68..c1f246bb354 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -5724,13 +5724,19 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
Query *subquery = rte->subquery;
Assert(subquery != NULL);
- Assert(subquery->setOperations == NULL);
- /* Need parens if WITH, ORDER BY, FOR UPDATE, or LIMIT; see gram.y */
+
+ /*
+ * We need parens if WITH, ORDER BY, FOR UPDATE, or LIMIT; see gram.y.
+ * Also add parens if the leaf query contains its own set operations.
+ * (That shouldn't happen unless one of the other clauses is also
+ * present, see transformSetOperationTree; but let's be safe.)
+ */
need_paren = (subquery->cteList ||
subquery->sortClause ||
subquery->rowMarks ||
subquery->limitOffset ||
- subquery->limitCount);
+ subquery->limitCount ||
+ subquery->setOperations);
if (need_paren)
appendStringInfoChar(buf, '(');
get_query_def(subquery, buf, context->namespaces, resultDesc,