Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nvRQi-0006Ja-Mh for pgsql-bugs@arkaria.postgresql.org; Sun, 29 May 2022 22:30:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nvRQh-0002W7-Bj for pgsql-bugs@arkaria.postgresql.org; Sun, 29 May 2022 22:30:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nvRQh-0002Vx-1z for pgsql-bugs@lists.postgresql.org; Sun, 29 May 2022 22:30:35 +0000 Received: from mail-ot1-x335.google.com ([2607:f8b0:4864:20::335]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nvRQa-0006Rf-PI for pgsql-bugs@lists.postgresql.org; Sun, 29 May 2022 22:30:34 +0000 Received: by mail-ot1-x335.google.com with SMTP id t22-20020a0568301e3600b0060b333f7a1eso6719937otr.0 for ; Sun, 29 May 2022 15:30:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=GxCLGr5M14/TBXTJJdNG88/urKvDFkxL1M0zB2VwlxA=; b=bFf9Dy52jz5ngkJfvB4YK/qwB7EXA5J8TZBLiEhYXqfMPaJXjXkl2A2X3muP9EmDl2 upz3jK3ztNRerXiwzXl6Ln6OPeqvOCSWQzN+olnHVIEtZeMIf/H7ahoVPniX6163V1qN D7a672ixpNyGrrwjMKhQ9wIdXoW+errccEy0Iw+mPSTGQFC8THpcM9CFhVXs7fGi/7Q9 n4zfRWKDFggrJY8X6lMECUegkv94cypCn5QL1PkpmsHeg423iEmKNKDbffhjKzfjQ7Kt d92pB1E+Sgt2LLMgy4qWQXL8+nfe1eMVKAHf/uDKZTZG4uCAM17AbmuO9uKxVyNXoSlf oIvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=GxCLGr5M14/TBXTJJdNG88/urKvDFkxL1M0zB2VwlxA=; b=pGAGt9cr15NBBtdR3wZZQ6fWNRttk3Gz+5fL2IJDyKLKxeR1aCCEx8XAmifGUUvl6A r7ssix/7SIv1hnAy92Fl+zdKEQQDCeG+oRHktl3HxvcYvoF/DZUvSOGFWCgiR/rlgqLv 0KbXxhnv74AsTHCYD9+z0RP/gUlGSzioCkoqRPYgx1VWj7zlODzgXOfWVBpxFFioHswH mi9yv+TFR+fTbdgez49Uf3gLl83FBg0LYZxUrtSULczhsMf5/cYCmd14gllTntgVw8ta VrdmDWnY+EWWVzdRJtcipECYqFer2djXWEIboZawZNTsHdkVGfs9TKQq/naBDekbwnqT VJoQ== X-Gm-Message-State: AOAM530YpgcahXqiezgTUzc3ZbtdDhA0ELzapIpxSbidX8kAnupLgPl1 QI2+4Nfc3PxJ+hV9yO8FO31eq4Nl+4TOB/sxQQRjEB+6kBQ= X-Google-Smtp-Source: ABdhPJxDl7NxIP6GhPU5hCbf+/bGml9/R6IjYtB95ToVBLJHJ8Y8zlI7EEmOsdQadO8lJz+M5kERwNUuGPm1Cm+0+Sk= X-Received: by 2002:a05:6830:25c2:b0:606:ed20:da5c with SMTP id d2-20020a05683025c200b00606ed20da5cmr20273595otu.234.1653863426897; Sun, 29 May 2022 15:30:26 -0700 (PDT) MIME-Version: 1.0 References: <17502-281a7aaacfaa872a@postgresql.org> In-Reply-To: From: David Rowley Date: Mon, 30 May 2022 10:30:14 +1200 Message-ID: Subject: Re: BUG #17502: View based on window functions returns wrong results when queried To: =?UTF-8?Q?Daniel_Farka=C5=A1?= Cc: Magnus Hagander , PostgreSQL mailing lists Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 30 May 2022 at 06:58, Daniel Farka=C5=A1 wrote: > SELECT metric_name FROM analytics_view; > metric_name| > -----------+ > metric_1 | > metric_1 | > metric_1 | > metric_1 | This is certainly a bug. Thanks for reporting it. The problem seems to be down to the fact that remove_unused_subquery_outputs() does not check if the to-be-removed target entry references WindowClauses which contain set-returning functions. We only seem to check if the target entry itself is an SRF, per: /* * If it contains a set-returning function, we can't remove it since * that could change the number of rows returned by the subquery. */ if (subquery->hasTargetSRFs && expression_returns_set(texpr)) continue; This ensures queries such as the following don't have SRF columns removed: postgres=3D# explain verbose select a from (select a,generate_series(1,2) as b from t) t; QUERY PLAN ---------------------------------------------------------------------------= - Subquery Scan on t (cost=3D0.00..131.13 rows=3D5100 width=3D4) Output: t.a -> ProjectSet (cost=3D0.00..80.13 rows=3D5100 width=3D8) Output: t_1.a, generate_series(1, 2) -> Seq Scan on public.t t_1 (cost=3D0.00..35.50 rows=3D2550 widt= h=3D4) Output: t_1.a (6 rows) I'm a little bit uncertain if the correct fix is to have expression_returns_set() look deeper into WindowFuncs to check if the WindowClause that the function belongs to has any SRFs in the PARTITION BY / ORDER BY clause. Unfortunately, doing that means having to pass the PlannerInfo to expression_returns_set(). I don't quite see how that could be made to work in the back branches. The other fix would be to make remove_unused_subquery_outputs() pull out all WindowFuncs from the texpr and check if any of the WindowClauses have SRFs in the PARTITION BY / ORDER BY clause. I'll need to look a bit deeper into the usages of expression_returns_set() to know which of the fixes is correct. There might be some other bugs lurking due to expression_returns_set() not checking WindowClauses for SRFs. David