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 1uNw7E-00CB4y-V4 for pgsql-bugs@arkaria.postgresql.org; Sat, 07 Jun 2025 16:09:52 +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 1uNw7C-00DzoB-NI for pgsql-bugs@arkaria.postgresql.org; Sat, 07 Jun 2025 16:09:51 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uNw7C-00Dzo2-Eq for pgsql-bugs@lists.postgresql.org; Sat, 07 Jun 2025 16:09:51 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1uNw7B-000lxw-1F for pgsql-bugs@lists.postgresql.org; Sat, 07 Jun 2025 16:09:50 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 557G9m4a015515; Sat, 7 Jun 2025 12:09:48 -0400 From: Tom Lane To: dengwenking@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18948: Equivalent MAX() result in view and inline query yields inconsistent row count In-reply-to: <18948-a05bf0726c2c993a@postgresql.org> References: <18948-a05bf0726c2c993a@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Sat, 07 Jun 2025 02:44:08 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <15513.1749312588.1@sss.pgh.pa.us> Date: Sat, 07 Jun 2025 12:09:48 -0400 Message-ID: <15514.1749312588@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > -- Setup: Create table t0 and view v1 > CREATE TABLE t0 (c0 REAL); > INSERT INTO t0 (c0) VALUES (1.830250668324684e+38); > INSERT INTO t0 (c0) VALUES (-3.29559603270012e+38); > INSERT INTO t0 (c0) VALUES (3.352134689102562e+38); > CREATE VIEW v1 AS SELECT MAX(c0) AS c0 FROM t0; > -- Query 3: Inline MAX() with WHERE (1 = NULL) > db1=# SELECT MAX(c0) FROM t0 WHERE (1 = NULL); > max > ----- > (1 row) > -- Query 4: View value with same WHERE clause > db1=# SELECT (c0) FROM v1 WHERE (1 = NULL); > c0 > ---- > (0 rows) > Both SELECT should return one row with NULL. No, they should not. In your query 3, the WHERE filter applies before aggregation happens, so it removes all the input rows to the MAX(). In your query 4, the WHERE filter applies after the MAX(), that is it acts on the aggregated row(s). To write an exact equivalent of query 4 without using a view or sub-select, you'd need to write the filter condition in HAVING. regards, tom lane