Re: Invalid query generated by postgres_fdw with UNION ALL and ORDER BY

2024-03-11 Thread Ashutosh Bapat
On Fri, Mar 8, 2024 at 7:43 AM David Rowley wrote: > On Fri, 8 Mar 2024 at 00:54, Ashutosh Bapat > wrote: > > > > On Thu, Mar 7, 2024 at 4:39 PM David Rowley > wrote: > >> I think the fix should go in appendOrderByClause(). It's at that > >> point we look for the EquivalenceMember for the

Re: Invalid query generated by postgres_fdw with UNION ALL and ORDER BY

2024-03-10 Thread David Rowley
On Fri, 8 Mar 2024 at 23:14, Richard Guo wrote: > I've looked at this patch a bit. I once wondered why we don't check > pathkey->pk_eclass->ec_has_const with EC_MUST_BE_REDUNDANT to see if the > pathkey is not needed. Then I realized that a child member would not be > marked as constant even if

Re: Invalid query generated by postgres_fdw with UNION ALL and ORDER BY

2024-03-08 Thread Richard Guo
On Fri, Mar 8, 2024 at 10:13 AM David Rowley wrote: > The fix could also be to use deparseConst() in appendOrderByClause() > and have that handle Const EquivalenceMember instead. I'd rather just > skip them. To me, that seems less risky than ensuring deparseConst() > handles all Const types

Re: Invalid query generated by postgres_fdw with UNION ALL and ORDER BY

2024-03-07 Thread David Rowley
On Fri, 8 Mar 2024 at 00:54, Ashutosh Bapat wrote: > > On Thu, Mar 7, 2024 at 4:39 PM David Rowley wrote: >> I think the fix should go in appendOrderByClause(). It's at that >> point we look for the EquivalenceMember for the relation and can >> easily discover if the em_expr is a Const. I

Re: Invalid query generated by postgres_fdw with UNION ALL and ORDER BY

2024-03-07 Thread Ashutosh Bapat
On Thu, Mar 7, 2024 at 4:39 PM David Rowley wrote: > On Thu, 7 Mar 2024 at 19:09, Michał Kłeczek wrote: > > > > The following query: > > > > SELECT * FROM ( > > SELECT 2023 AS year, * FROM remote_table_1 > > UNION ALL > > SELECT 2022 AS year, * FROM remote_table_2 > > ) > > ORDER BY year

Re: Invalid query generated by postgres_fdw with UNION ALL and ORDER BY

2024-03-07 Thread David Rowley
On Thu, 7 Mar 2024 at 19:09, Michał Kłeczek wrote: > > The following query: > > SELECT * FROM ( > SELECT 2023 AS year, * FROM remote_table_1 > UNION ALL > SELECT 2022 AS year, * FROM remote_table_2 > ) > ORDER BY year DESC; > > yields the following remote query: > > SELECT [columns] FROM

Invalid query generated by postgres_fdw with UNION ALL and ORDER BY

2024-03-06 Thread Michał Kłeczek
The following query: SELECT * FROM ( SELECT 2023 AS year, * FROM remote_table_1 UNION ALL SELECT 2022 AS year, * FROM remote_table_2 ) ORDER BY year DESC; yields the following remote query: SELECT [columns] FROM remote_table_1 ORDER BY 2023 DESC and subsequently fails remote execution.