Re: Deparsing rewritten query

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Julien Rouhaud <rjuju123(at)gmail(dot)com>
Cc: Gilles Darold <gilles(at)darold(dot)net>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Deparsing rewritten query
Date: 2022-01-31 21:05:44
Message-ID: CAFj8pRBb2bDUqgzv_r1LVUNo6CEQsEA_9NQDTrrk12a5S=EWJA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

po 31. 1. 2022 v 19:09 odesílatel Julien Rouhaud <rjuju123(at)gmail(dot)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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Mark Dilger 2022-01-31 21:09:44 Re: CREATEROLE and role ownership hierarchies
Previous Message Michael Banck 2022-01-31 19:55:56 Re: CREATEROLE and role ownership hierarchies