When reverse-compiling a query, ruleutils.c has some complicated code to
handle the join output columns of a JOIN USING join. There used to be
no way to qualify those columns, and so if there was a naming conflict
anywhere in the query, those output columns had to be renamed to be
unique throughout the query.
Since PostgreSQL 14, we have a new feature that allows adding an alias
to a JOIN USING clause. This provides a better solution to this
problem. This patch changes the logic in ruleutils.c so that if naming
conflicts with JOIN USING output columns are found in the query, these
JOIN USING aliases with generated names are attached everywhere and the
columns are then qualified everywhere.
I made it so that new JOIN USING aliases are only created if needed in
the query, since we already have the logic of has_dangerous_join_using()
to compute when that is needed. We could probably do away with that too
and always use them, but I think that would be surprising and not what
people want.
The regression test changes illustrate the effects very well.
This is PoC-level right now. You will find blatant code duplication in
set_rtable_names(), and for now I have only commented out some code that
could be removed, not actually removed it.