Hi,

On Wed, Feb 02, 2022 at 07:09:35PM +0530, Bharath Rupireddy wrote:
> On Tue, Feb 1, 2022 at 9:08 AM Julien Rouhaud <rjuju...@gmail.com> wrote:
> >
> > Hi,
> 
> Thanks. +1 for this work. Some comments on v3:
> 
> 1) How about pg_get_rewritten_query()?

Argh, I just realized that I sent the patch from the wrong branch.  Per
previous complaint from Tom, I'm not proposing that function anymore (I will
publish an extension for that if the patch gets commits) but only expose
get_query_def().

I'm attaching the correct patch this time, sorry about that.
>From 0485ea1b507e8f2f1df782a97f11184276d7fca7 Mon Sep 17 00:00:00 2001
From: Julien Rouhaud <julien.rouh...@free.fr>
Date: Tue, 29 Jun 2021 00:07:04 +0800
Subject: [PATCH v3] Expose get_query_def()

This function can be useful for external module, for instance if they want to
display a statement after the rewrite stage.

In order to emit valid SQL, make sure that any subquery RTE comes with an
alias.

Author: Julien Rouhaud
Reviewed-by: Gilles Darold
Reviewed-by: Pavel Stehule
Discussion: https://postgr.es/m/20210627041138.zklczwmu3ms4ufnk@nol
---
 src/backend/utils/adt/ruleutils.c | 15 +++++++++++----
 src/include/utils/ruleutils.h     |  3 +++
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/src/backend/utils/adt/ruleutils.c 
b/src/backend/utils/adt/ruleutils.c
index 039b1d2b95..3db2948984 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -389,9 +389,6 @@ static void make_ruledef(StringInfo buf, HeapTuple ruletup, 
TupleDesc rulettc,
                                                 int prettyFlags);
 static void make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
                                                 int prettyFlags, int 
wrapColumn);
-static void get_query_def(Query *query, StringInfo buf, List *parentnamespace,
-                                                 TupleDesc resultDesc,
-                                                 int prettyFlags, int 
wrapColumn, int startIndent);
 static void get_values_def(List *values_lists, deparse_context *context);
 static void get_with_clause(Query *query, deparse_context *context);
 static void get_select_query_def(Query *query, deparse_context *context,
@@ -5344,7 +5341,7 @@ make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc 
rulettc,
  * the view represented by a SELECT query.
  * ----------
  */
-static void
+void
 get_query_def(Query *query, StringInfo buf, List *parentnamespace,
                          TupleDesc resultDesc,
                          int prettyFlags, int wrapColumn, int startIndent)
@@ -10989,6 +10986,16 @@ get_from_clause_item(Node *jtnode, Query *query, 
deparse_context *context)
                        if (strcmp(refname, rte->ctename) != 0)
                                printalias = true;
                }
+               else if (rte->rtekind == RTE_SUBQUERY)
+               {
+                       /*
+                        * For a subquery RTE, always print alias.  A 
user-specified query
+                        * should only be valid if an alias is provided, but 
our view
+                        * expansion doesn't generate aliases, so a rewritten 
query might
+                        * not be valid SQL.
+                        */
+                       printalias = true;
+               }
                if (printalias)
                        appendStringInfo(buf, " %s", quote_identifier(refname));
 
diff --git a/src/include/utils/ruleutils.h b/src/include/utils/ruleutils.h
index e8090c96d7..f512bb6867 100644
--- a/src/include/utils/ruleutils.h
+++ b/src/include/utils/ruleutils.h
@@ -39,6 +39,9 @@ extern List *select_rtable_names_for_explain(List *rtable,
                                                                                
         Bitmapset *rels_used);
 extern char *generate_collation_name(Oid collid);
 extern char *generate_opclass_name(Oid opclass);
+void           get_query_def(Query *query, StringInfo buf, List 
*parentnamespace,
+                                                 TupleDesc resultDesc,
+                                                 int prettyFlags, int 
wrapColumn, int startIndent);
 extern char *get_range_partbound_string(List *bound_datums);
 
 extern char *pg_get_statisticsobjdef_string(Oid statextid);
-- 
2.35.0

Reply via email to