pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: 曾满 <zengman@halodbtech.com>
Cc: pgsql-hackers@lists.postgresql.org
Subject: Re: [PATCH] Fixed assertion issues in "pg_get_viewdef"
Date: Tue, 19 Nov 2024 11:21:33 -0500
Message-ID: <3774968.1732033293@sss.pgh.pa.us> (raw)
In-Reply-To: <tencent_7ABF9B1F23B0C77606FC5FE3@qq.com>
References: <tencent_7ABF9B1F23B0C77606FC5FE3@qq.com>

"=?utf-8?B?5pu+5ruh?=" <zengman@halodbtech.com> writes:
> We can comment out the assertion that raises the exception, because I looked up the code and found that the assertion doesn't seem to make a lot
> of sense here.

I looked at the git history and found that I added this assertion
in 07b4c48b6.  Your example shows that indeed it's a thinko, but
I'm not convinced that simply removing it is the best answer.

The real point here is that we'd want to parenthesize if a "leaf"
subquery contains set operations, to ensure that the setop nesting
is represented correctly.  Normally, a "leaf" query would not contain
any set operations, but that can happen if the leaf query also
contains WITH/ORDER BY/FOR UPDATE/LIMIT, because that stops
transformSetOperationTree from merging it into the upper
SetOperationStmt tree.  So the assertion should have been less
"can't have setOperations here" and more "can't have setOperations
here unless there's also one of sortClause etc".

But on reflection I don't see why it needs to be an assert at all.
Let's just make nonempty setOperations another reason to force
need_paren on, as attached.

			regards, tom lane

Attachments:

  [text/x-diff] v2-fix-incorrect-assertion.patch (1.1K, ../3774968.1732033293@sss.pgh.pa.us/2-v2-fix-incorrect-assertion.patch)
  download | inline diff:
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index a39068d1bf..2194ab3dfa 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -6353,13 +6353,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,

view thread (5+ messages)  latest in thread

Message-ID: <3774968.1732033293@sss.pgh.pa.us>
Permalink:  ../3774968.1732033293@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/3774968.1732033293@sss.pgh.pa.us

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, zengman@halodbtech.com, pgsql-hackers@lists.postgresql.org
  Subject: Re: [PATCH] Fixed assertion issues in "pg_get_viewdef"
  In-Reply-To: <3774968.1732033293@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox