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 1lhxCp-0003gE-1d for pgsql-bugs@arkaria.postgresql.org; Sat, 15 May 2021 16:31:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lhxCn-0002ig-Nc for pgsql-bugs@arkaria.postgresql.org; Sat, 15 May 2021 16:31:57 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lhwsQ-0007CL-V7 for pgsql-bugs@lists.postgresql.org; Sat, 15 May 2021 16:10:55 +0000 Received: from forward101j.mail.yandex.net ([2a02:6b8:0:801:2::101]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lhwsN-00077I-F6 for pgsql-bugs@lists.postgresql.org; Sat, 15 May 2021 16:10:53 +0000 Received: from iva4-3f047a428f41.qloud-c.yandex.net (iva4-3f047a428f41.qloud-c.yandex.net [IPv6:2a02:6b8:c0c:128e:0:640:3f04:7a42]) by forward101j.mail.yandex.net (Yandex) with ESMTP id 927171BE2197; Sat, 15 May 2021 19:10:46 +0300 (MSK) Received: from iva5-057a0d1fbbd8.qloud-c.yandex.net (iva5-057a0d1fbbd8.qloud-c.yandex.net [2a02:6b8:c0c:7f1c:0:640:57a:d1f]) by iva4-3f047a428f41.qloud-c.yandex.net (mxback/Yandex) with ESMTP id fVH3etwSSW-AkJ4jUoF; Sat, 15 May 2021 19:10:46 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1621095046; bh=YwYNltL8aN+LPSw24UcCnBhwFxJ1NI1ObRTwwc5jVdo=; h=In-Reply-To:Subject:To:Message-ID:From:References:Date:CC; b=TqMEJsVbefgPClvqhkK7mqcHJaDF8g4ljQWoaVDcSjPCLI20RJ7UMNrl0wxk+dFVt b/6twYB4E6CFrUDnsM8CEOZ8lg7vxOUidZzhHwt4BOTdAdiMgCUw8G4OkYKrANvP0k 9RfL7ZClcxrN4OvnqSIZP0sBAKqaDbjcLi1X3tk4= Authentication-Results: iva4-3f047a428f41.qloud-c.yandex.net; dkim=pass header.i=@yandex.ru Received: by iva5-057a0d1fbbd8.qloud-c.yandex.net (smtp/Yandex) with ESMTPSA id u8Kpg0TO5k-AiLG2fUF; Sat, 15 May 2021 19:10:45 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) Date: Sat, 15 May 2021 19:10:44 +0300 From: Eugen Konkov Message-ID: <515893534.20210515191044@yandex.ru> To: David Rowley CC: PG Bug reporting form , PostgreSQL mailing lists , pgsql-performance , ladayaroslav@yandex.ru Subject: Re: BUG #16968: Planner does not recognize optimization In-Reply-To: References: <16968-9c8a1708c1c04355@postgresql.org> <846547866.20210416222735@yandex.ru> <226821618.20210513163513@yandex.ru> <1035195294.20210515173416@yandex.ru> MIME-Version: 1.0 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 Hello David, Saturday, May 15, 2021, 5:52:47 PM, you wrote: > On Sun, 16 May 2021 at 02:34, Eugen Konkov wrote: >> I found a case when `not assigning a ressortgroupref to the whole-row va= r` cause >> wrong window function calculations. >> >> I use same query. The difference come when I wrap my query into >> function. (see full queries in attachment) >> >> 1. >> SELECT * >> FROM agreement_totals( tstzrange( '2020-07-01', '2020-08-01' ) ) >> WHERE agreement_id =3D 161::int AND (o).period_id =3D 10::int >> >> 2. >> SELECT * >> sum( .... ) over wagreement >> FROM .... >> WHERE agreement_id =3D 161::int AND (o).period_id =3D 10::int >> WINDOW wagreement AS ( PARTITION BY agreement_id ) >> >> For first query window function calculates SUM over all agreements, >> then some are filtered out by (o).period_id condition. > This is unrelated to the optimisation that you were asking about before. > All that's going on here is that WHERE is evaluated before SELECT. > This means that your filtering is done before the window functions are > executed. This is noted in the docs in [1]: >> The rows considered by a window function are those of the =E2=80=9Cvirtu= al table=E2=80=9D produced by the query's FROM clause as filtered by its WH= ERE, GROUP BY, and HAVING clauses if any. For example, a row removed becaus= e it does not meet the WHERE condition is not seen by any window function. = A query can contain multiple window functions that slice up the data in dif= ferent ways using different OVER clauses, but they all act on the same coll= ection of rows defined by this virtual table. > If you want to filter rows after the window functions are evaluated > then you'll likely want to use a subquery. > David > [1] https://www.postgresql.org/docs/13/tutorial-window.html Sorry, I miss that WHERE works first and after it window function. >This is unrelated to the optimisation that you were asking about before. So, yes, unrelated. Thank you for your answers. --=20 Best regards, Eugen Konkov