demo_of_postgres_fdw_order_by_const_bug.sql

application/octet-stream

Filename: demo_of_postgres_fdw_order_by_const_bug.sql
Type: application/octet-stream
Part: 1
Message: Re: Invalid query generated by postgres_fdw with UNION ALL and ORDER BY
DO $d$
    BEGIN
        EXECUTE $$CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw
            OPTIONS (dbname '$$||current_database()||$$',
                     port '$$||current_setting('port')||$$'
            )$$;
    END;
$d$;

CREATE USER MAPPING FOR CURRENT_USER SERVER loopback;

create table table_1 (a int);
create table table_2 (a int);


create foreign table remote_table_1 (a int) server loopback options (schema_name 'public', table_name 'table_1');
create foreign table remote_table_2 (a int) server loopback options (schema_name 'public', table_name 'table_2');

SELECT * FROM (SELECT 2023 AS year, * FROM remote_table_1 UNION ALL SELECT 2022 AS year, * FROM remote_table_2) ORDER BY year DESC;