BUG #18381: Invalid query generated by postgres_fdw with UNION ALL and ORDER BY

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: michal(at)kleczek(dot)org
Subject: BUG #18381: Invalid query generated by postgres_fdw with UNION ALL and ORDER BY
Date: 2024-03-07 10:22:33
Message-ID: 18381-137456acd168bf93@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 18381
Logged by: Michal Kleczek
Email address: michal(at)kleczek(dot)org
PostgreSQL version: 16.2
Operating system: Linux
Description:

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.

Not really sure where the problem is - the planner or postgres_fdw.
I guess it is postgres_fdw not filtering out ordering keys.

This filtering would also be pretty useful in the following scenario (which
is also why I went through UNION ALL route and discovered this issue):

I have a table partitioned by year, partitions are remote tables.
On remote servers I have a GIST index that does not support ordering ([1])
so I would like to avoid sending ORDER BY year to remote servers.
Ideally redundant ordering should be filtered out.

[1]
https://www.postgresql.org/message-id/B2AC13F9-6655-4E27-BFD3-068844E5DC91%40kleczek.org

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Ronan Dunklau 2024-03-07 11:21:24 Re: FSM Corruption (was: Could not read block at end of the relation)
Previous Message David Rowley 2024-03-07 09:50:54 Re: "type with xxxx does not exist" when doing ExecMemoize()