Re: Deparsing rewritten query - Mailing list pgsql-hackers

From Pavel Stehule
Subject Re: Deparsing rewritten query
Date
Msg-id CAFj8pRBb2bDUqgzv_r1LVUNo6CEQsEA_9NQDTrrk12a5S=EWJA@mail.gmail.com
Whole thread Raw
In response to Re: Deparsing rewritten query  (Julien Rouhaud <rjuju123@gmail.com>)
Responses Re: Deparsing rewritten query  (Julien Rouhaud <rjuju123@gmail.com>)
List pgsql-hackers


po 31. 1. 2022 v 19:09 odesílatel Julien Rouhaud <rjuju123@gmail.com> napsal:
Hi,

On Mon, Jan 31, 2022 at 06:46:37PM +0100, Pavel Stehule wrote:
>
> I checked the last patch.  I think it is almost trivial. I miss just
> comment, why this alias is necessary
>
> + if (!rte->alias)
> + rte->alias = makeAlias(get_rel_name(rte->relid), NULL);

Thanks for looking at it Pavel!

The alias is necessary because otherwise queries involving views won't produce
valid SQL, as aliases for subquery is mandatory.  This was part of the v1
regression tests:

+-- test pg_get_query_def()
+SELECT pg_get_query_def('SELECT * FROM shoe') as def;
+                          def
+--------------------------------------------------------
+  SELECT shoename,                                     +
+     sh_avail,                                         +
+     slcolor,                                          +
+     slminlen,                                         +
+     slminlen_cm,                                      +
+     slmaxlen,                                         +
+     slmaxlen_cm,                                      +
+     slunit                                            +
+    FROM ( SELECT sh.shoename,                         +
+             sh.sh_avail,                              +
+             sh.slcolor,                               +
+             sh.slminlen,                              +
+             (sh.slminlen * un.un_fact) AS slminlen_cm,+
+             sh.slmaxlen,                              +
+             (sh.slmaxlen * un.un_fact) AS slmaxlen_cm,+
+             sh.slunit                                 +
+            FROM shoe_data sh,                         +
+             unit un                                   +
+           WHERE (sh.slunit = un.un_name)) shoe;       +

the mandatory "shoe" alias is added with that change.

I looked for other similar problems and didn't find anything, but given the
complexity of the SQL standard it's quite possible that I missed some other
corner case.

I don't feel good about forcing an alias. relname doesn't ensure uniqueness. You can have two views with the same name from different schemas. Moreover this field is necessary only when a deparsed query is printed, not always.

Isn't possible to compute the correct subquery alias in print time when it is missing?

Regards

Pavel
 

pgsql-hackers by date:

Previous
From: Michael Banck
Date:
Subject: Re: CREATEROLE and role ownership hierarchies
Next
From: Mark Dilger
Date:
Subject: Re: CREATEROLE and role ownership hierarchies