Thread: Why is 'use_alias' hardcoded to true in deparseFromExprForRel() for some cases

Hi everyone,
In the postgrs_fdw deparser code, deparseFromExprForRel() appends an alias to the remote query based on the 'use_alias' boolean flag.

For a simple query, 'use_alias' is determined by `bms_membership(scanrel->relids) == BMS_MULTIPLE` condition. Example: https://github.com/postgres/postgres/blob/master/contrib/postgres_fdw/deparse.c#L1385C2-L1388

But for JOINs, 'use_alias' is always hardcoded to true. Examples:

This seems like an extra-protection in case of joins. But it could happen that the join is across 2 different foreign postgres-servers (means each foreign server will do SCAN only, and the JOIN will happen at the upper layer). In that case, using aliases in the remote queries seem redundant to me.
Please correct me if I am missing something. Can we note pass `bms_membership(foreignrel->relids) == BMS_MULTIPLE` instead?


--
Regards
Rajan Pandey
Software Developer, AWS
Rajan Pandey <rajanpandey2508@gmail.com> writes:
> This seems like an extra-protection in case of joins. But it could happen
> that the join is across 2 different foreign postgres-servers (means each
> foreign server will do SCAN only, and the JOIN will happen at the upper
> layer). In that case, using aliases in the remote queries seem redundant to
> me.
> Please correct me if I am missing something. Can we note pass
> `bms_membership(foreignrel->relids) == BMS_MULTIPLE` instead?

I'd be very very careful about changing that, because presence
of a join alias affects the parser's behavior in non-obvious ways.
Unless you've got some compellingly good reason for messing
with this, I doubt it is worth the investigatory effort to
convince ourselves that it wouldn't introduce any bugs.

            regards, tom lane