Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1riCKw-00FfMd-O3 for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Mar 2024 11:54:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1riCKu-0056d3-G7 for pgsql-hackers@arkaria.postgresql.org; Thu, 07 Mar 2024 11:54:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1riCKu-0056cv-3t for pgsql-hackers@lists.postgresql.org; Thu, 07 Mar 2024 11:54:56 +0000 Received: from mail-ua1-x930.google.com ([2607:f8b0:4864:20::930]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1riCKr-003G0b-Rv for pgsql-hackers@lists.postgresql.org; Thu, 07 Mar 2024 11:54:55 +0000 Received: by mail-ua1-x930.google.com with SMTP id a1e0cc1a2514c-7d5fce59261so663503241.3 for ; Thu, 07 Mar 2024 03:54:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709812492; x=1710417292; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=11cF1t1IY2KnIxRV1n9Lndp4cew8Jl5y4EjZs11Wl14=; b=bUQ7wBlCtjNtbZ8xwhaD2Ij0NyU1DAkV6ZyQZa2ULhRrQ7n2OqCiNwLVaptSlLodag dlBC6Dxlfq2ci2mt1nsHWRr7aEIUruQvVD0hVCfyFJ4FTVitF77T9W8OG6tmZK/wfTez BogHVEmFMyxo8zfwWi/v6gKNT9c04oSOLaWlO708+r3pEiJBrfoLnW8J8fR1My7RlWLk yz+tYsMhkePKQoglB9cowIW4iqTQKDJ+enQz7pP+tr2VkboTM/znNXd5q5vv0UUtZMBy ZKDWcdCE2mapb3+C/Hbn0niIY0NyKUxYQfqBc7mFcADbh9v2iEvI3sqYEsUJCI27XYHW x1AA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709812492; x=1710417292; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=11cF1t1IY2KnIxRV1n9Lndp4cew8Jl5y4EjZs11Wl14=; b=xEcKAtf0WFgFNGy5snpcjH8yhp6zBJRf92gSqSbxmGsWgiiFZxaIsQo2K8bNMio4V0 RNEj2+EK4S/1vQSF9+p8eYg6vNg+EixatJD6gV/WLAkN2XAhE8S9G2V9Ugk0kV6BXlEM W86Eic99lCqkIv5jBuGGK/LbVECWU7B2q/xQDA7iFApMP8Lo6rLy5Wp27AIa13cnYqi6 GI8k+KOrRhHVnwcyf9h/LrzbWDv7ab/ky8dCukgp7S76RB2cop+Lgiei7c7d/asFGF7X hFIrUZl5d2eg4Xt/+hGAZ7e3SzCvg/obSAXoAS7pUS38nzSiQAm+Ir0C7cgoYL6/PrWZ goQA== X-Forwarded-Encrypted: i=1; AJvYcCXGQHDqz42dfRiOVrDUUcKuGzifCZUunulknf8vFy/jgB87KQ1rwqV9VT8MFMADCHF/fx3QcQaVtjxkpSzv6+SG2h8ihyzEtkQC+CdXix13ntJz X-Gm-Message-State: AOJu0YwgdzGu8+FbyTAC7nR7qv/90zoDAQGN1xOwxdVC0IpN5SZTRpwP WtM/V1GqniqZqB+wWxxnDN1I8C17WG4bI2jsEfnEddX5aEh0uyAxBpYFFKcVXTHDJ9QodwPq7j2 GY8gzpyY/TESl0vLwBZjeEseke4g= X-Google-Smtp-Source: AGHT+IHxCf9Ifo0YEp9xSyRofN/DBeiBJgaPuBwHoM5eyk9YxPuVWjRzBf60bd/v015W0VU05GkuXJeqafh75sARaFA= X-Received: by 2002:a05:6102:45b:b0:472:b0b3:8ac2 with SMTP id e27-20020a056102045b00b00472b0b38ac2mr7957975vsq.16.1709812491965; Thu, 07 Mar 2024 03:54:51 -0800 (PST) MIME-Version: 1.0 References: <0714C8B8-8D82-4ABB-9F8D-A0C3657E7B6E@kleczek.org> In-Reply-To: From: Ashutosh Bapat Date: Thu, 7 Mar 2024 17:24:40 +0530 Message-ID: Subject: Re: Invalid query generated by postgres_fdw with UNION ALL and ORDER BY To: David Rowley Cc: =?UTF-8?B?TWljaGHFgiBLxYJlY3plaw==?= , PostgreSQL Hackers Content-Type: multipart/alternative; boundary="000000000000375a63061310be71" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000375a63061310be71 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Mar 7, 2024 at 4:39=E2=80=AFPM David Rowley = wrote: > On Thu, 7 Mar 2024 at 19:09, Micha=C5=82 K=C5=82eczek 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 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. > > Interesting. I've attached a self-contained recreator for the casual > passerby. > > 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 think we can safely just > skip doing any ORDER BY stuff and not worry about if the > literal format of the const will appear as a reference to an ordinal > column position in the ORDER BY clause. > deparseSortGroupClause() calls deparseConst() with showtype =3D 1. appendOrderByClause() may want to do something similar for consistency. Or remove it from deparseSortGroupClause() as well? > > Something like the attached patch I think should work. > > I wonder if we need a test... > Yes. --=20 Best Wishes, Ashutosh Bapat --000000000000375a63061310be71 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Thu, Mar 7, 2024 at 4:39=E2=80=AFP= M David Rowley <dgrowleyml@gmail= .com> wrote:
On Thu, 7 Mar 2024 at 19:09, Micha=C5=82 K=C5=82eczek <michal@kleczek.org> wrote= :
>
> The following query:
>
> SELECT * FROM (
>=C2=A0 =C2=A0SELECT 2023 AS year, * FROM remote_table_1
>=C2=A0 =C2=A0UNION ALL
>=C2=A0 =C2=A0SELECT 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.

Interesting.=C2=A0 I've attached a self-contained recreator for the cas= ual passerby.

I think the fix should go in appendOrderByClause().=C2=A0 It's at that<= br> point we look for the EquivalenceMember for the relation and can
easily discover if the em_expr is a Const.=C2=A0 I think we can safely just=
skip doing any ORDER BY <const> stuff and not worry about if the
literal format of the const will appear as a reference to an ordinal
column position in the ORDER BY clause.

deparseSortGroupClause() calls deparseConst() with showtype =3D 1. appendO= rderByClause() may want to do something similar for consistency. Or remove = it from deparseSortGroupClause() as well?
=C2=A0

Something like the attached patch I think should work.

I wonder if we need a test...

=
Yes.

--
Best Wishes,
Ashutosh Bapat
--000000000000375a63061310be71--