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.96) (envelope-from ) id 1weCHr-004dGt-1K for pgsql-bugs@arkaria.postgresql.org; Mon, 29 Jun 2026 13:44:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1weCHq-003QB4-0u for pgsql-bugs@arkaria.postgresql.org; Mon, 29 Jun 2026 13:44:34 +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.96) (envelope-from ) id 1weCHp-003QAw-3B for pgsql-bugs@lists.postgresql.org; Mon, 29 Jun 2026 13:44:34 +0000 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1weCHn-00000000nJ0-3jxj for pgsql-bugs@lists.postgresql.org; Mon, 29 Jun 2026 13:44:33 +0000 Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-4938d60c035so17870355e9.0 for ; Mon, 29 Jun 2026 06:44:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1782740671; cv=none; d=google.com; s=arc-20260327; b=gPYR9rlzybK1eox06h1mjfdDKdP0kJzviV+OZwneyF1SIAmlPwdn3bBImqAZArMudr XH625PcWJ4fuBGx8Jrb5xlUARDpmg73Wfeidl52zT9VHHTRtqA2DrWrszZCNnUh7ckpj AuI3/2vAeG07mXr32se/PASzDiicNAWZuSzrLsN6GvjkzEMIMwYGgOs//SRGjRxbGbFn nnMj3as1OJ3DTyzOCsdBjaZvM5mKT4IS413rQh2NhjuZJEI9Kz/mDAeqbuFi0usdHx5a qlup+ym3y4Qflc99at47Y0N39sSXpTqIZIhzgBqJ3gggsKOwc3ttRU1jVE3mgyvBbfNG /16Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=tK8BpEbbbavwS8u4rZ/vFMj/TbhioMkrkwlUvQSYLZA=; fh=eS0kUf44BiLFB53AnGYf/8EsjfNf3ge/Of3TmOYW0oI=; b=PmDBwCd3tWtktN8UP5oe2hzApkSO0cBHS54gtmFq1pq1zlLyeoiH/P35rXKjl+4hzz xIU8cz47C3ZFDBfZTOT+HVgKvnKNNJGIv6/soda/l/ACjP2XaC5Uk4yx7fepKWq8RBzu UwrGR0hv90FSd9bJZAgYvhzoSfItiRufsKNLnRhql0agXI/cjchB5Xi0rVap+Tz1Mw/R 97xuldoo/wvola2VgoQhhYebZtSkOG3Uaql9QKTFWLhT7oYech78ZKHJaT+DNaAqAmt3 r+1cm2IiHHZVBZvlXSsGN+iXPWZ0ry7CZONAiM7aceUmvEIl4eCYTw4cfVycObzvdWtr QhTg==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782740671; x=1783345471; 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=tK8BpEbbbavwS8u4rZ/vFMj/TbhioMkrkwlUvQSYLZA=; b=L8s9/eXwYEGnEC8Ru5m8MVyw+iLQnyA0vWvoJlh6XXcwXDM9yBA+SNX/ygGeYKvFbB VYfE459NTIHos3VwYTUZiqS4dofGDjjyILSGNJURY21tvsUNBkXia81EThMQZ+99itLH 1fOiv5o7dazIqC8OlsaaNo740hWlBf7Iz8WRtn3OtPWNa+bT6ycBOyi3nDSrVJPBlUCZ R5rTV6SMaAb4KBiCL0HtUp+lIcq5Yc5Eh1/VCC9pEC29+BZHsfvvoKGQfZhajXWaYAg8 mGhYGAXeTfGxXhOpi4aCjvyeqTlLJ3rqBKECoWxG+rkg+PMe57swCgQ3Is+6TDWvmV55 XvVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782740671; x=1783345471; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=tK8BpEbbbavwS8u4rZ/vFMj/TbhioMkrkwlUvQSYLZA=; b=aTDeR/zF4VZGcLrd9fzqPHptgKbl1cqw4ZgOYCmSQsnbf+neG0p49jFsujUMO0lIlh gQNZBBywHDGA3aQDrktiKSNnDhJUyvyYBNNXv44ppcvCMGTV0C2dbGjbJmJSg2A5sCkb ZLHjBcu0bveK4dgXPQsKMD3MTAh5bvftEgv/QeS2T0dNEcWqblaOsql+08juHqCG5KRy sCscx3eMEj8ifX0KxpBY1Ta5WI+8JV3V7l/HPYhhCz4x2y5wpMZPbAS5WcVnqOsL8J5E QOP386g4Oo/ckTiqqhAvt87abOraWWQY9CPsJPbnK2RG8s51s4K5bQ8WlbEjrcH0uQkA JrCA== X-Forwarded-Encrypted: i=1; AFNElJ9gQC+Y2+RM8u3I2WUJnQ26vl1wY9vA8kln7dY1fXO7yJs7/9qF7z6Cc6TRtvqfYhGEfBZLCwSOyaoJ@lists.postgresql.org X-Gm-Message-State: AOJu0Yz28VnALuHVywPCzPnHYXRFI30V1by3tYXMhJrZfmTeWzpEl/kQ 1SQYCMEIFJNDdfuV3KaE1lMR0iNb3wa4r7l8dFqA0Of2YO6/GSI/1TVCrue7DNHUKFOTPLOZB4b H9L53AP4szoB3ENgdQEULVD9TtPfJNW8= X-Gm-Gg: AfdE7clvrxW4B02hqa+fQlIn90+Rv/MeCmSTsRjGsPuDOu85N9pNCkw2QtYspDxiva6 acDRKZK9gMut02loaENJj//wgXQ31JEnw09yeqIWX4Qt3nMl5mbp5dYSlpGTtksQMEJm/1gb/de kMrB0siJJuKVsF/NRhJ+3awWT94cc42SOG6cjq+6tDUphGzIMF1B3Npx1DL0rheGIiXahYNmWo9 HZ5eJe9m/h1xvhUzKgS9XeuQkS4QTXPDWihdWzDoAyH1jKhD76ruzJX3u0E+h1ZFAoQu1iICcbp WdMFbO5aO+sZuZvQyiCPz08FqAb2pFxY71L13mXl2IBphOXpQF/qsglSchTpXw== X-Received: by 2002:a05:600c:3b24:b0:490:bd66:db49 with SMTP id 5b1f17b1804b1-49266850100mr271530885e9.12.1782740670628; Mon, 29 Jun 2026 06:44:30 -0700 (PDT) MIME-Version: 1.0 References: <19533-413a1014e5d0e766@postgresql.org> In-Reply-To: From: David Rowley Date: Tue, 30 Jun 2026 01:44:20 +1200 X-Gm-Features: AVVi8CdUEkDyra9_nRRw-nDadhuvbv0kIT7HmLdQXjGbukKBAUeqgrj277iQAkI Message-ID: Subject: Re: BUG #19533: Wrong results from WindowAgg run-condition pushdown on count() with EXCLUDE CURRENT ROW To: Richard Guo Cc: Chengpeng Yan , "imchifan@163.com" , "pgsql-bugs@lists.postgresql.org" Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 29 Jun 2026 at 12:39, Richard Guo wrote: > I wonder if we should still support count(*) with EXCLUDE CURRENT ROW, > as that always removes exactly one row, so it is still monotonic. > > + if ((frameOptions & FRAMEOPTION_EXCLUSION) && > + ((frameOptions & FRAMEOPTION_EXCLUSION) != > FRAMEOPTION_EXCLUDE_CURRENT_ROW || > + req->window_func->winfnoid != F_COUNT_)) > + monotonic = MONOTONICFUNC_NONE; Yeah, I noticed that one too. I think there are a few other cases we could also allow. I'm still looking, but I think COUNT(*) could still be monotonically increasing when frame_end is CURRENT ROW for any of EXCLUDE (CURRENT ROW|GROUP|TIES). In all those cases, since we've not looked at any rows beyond the current row, we're not susceptible to the exclude option excluding rows from a higher-order peer group, which may have more rows to count. I think there might be another with COUNT(ANY): "ORDER BY k RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE GROUP". Here we're excluding the entire group, so the count can't go backwards within that group as we don't count anything due to EXCLUDE GROUP, and since frame_end is CURRENT ROW, we don't have to concern ourselves with counting some later group which has more peer rows making the count go backwards (when that larger number of rows is excluded later when processing that peer group). I think there are also a few equivalences, such as: "order by k rows between unbounded preceding and current row exclude current row" I believe is logically the same as: "order by k rows between unbounded preceding and 1 preceding" Then, if frame_end was 1 PRECEDING with "GROUPS BETWEEN ...", EXCLUDE would never see the current row or peer group as the frame ends before we get to the current group or row. Anyway, I'm still trying to decide how to balance this. On one hand, there's an incentive not to disable this optimisation for cases that are perfectly safe, and on the other hand, there are lots of variables here, and "perfectly safe" is a little harder to fathom than I'd like for a bug fix. I'm also not keen on adding very complex and hard-to-follow logic to the support function. If we can allow a few more cases but do so as a result of boiling down the logic into something simpler, then I'd likely be more in favour of covering some extra cases. I'll continue to look at this. David