pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: michal@kleczek.org
Subject: BUG #18381: Invalid query generated by postgres_fdw with UNION ALL and ORDER BY
Date: Thu, 07 Mar 2024 10:22:33 +0000
Message-ID: <18381-137456acd168bf93@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      18381
Logged by:          Michal Kleczek
Email address:      michal@kleczek.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



view thread (2+ messages)  latest in thread

Message-ID: <18381-137456acd168bf93@postgresql.org>
Permalink:  ../18381-137456acd168bf93@postgresql.org/
Also on:    postgresql.org/message-id/18381-137456acd168bf93@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, pgsql-bugs@lists.postgresql.org, michal@kleczek.org
  Subject: Re: BUG #18381: Invalid query generated by postgres_fdw with UNION ALL and ORDER BY
  In-Reply-To: <18381-137456acd168bf93@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox