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.98.2) (envelope-from ) id 1x7ArD-00000000AUs-3O9a for pgsql-hackers@arkaria.postgresql.org; Thu, 17 Sep 2026 12:04:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1x7ArD-000000049BE-0T1O for pgsql-hackers@arkaria.postgresql.org; Thu, 17 Sep 2026 12:04:51 +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.98.2) (envelope-from ) id 1x7ArC-000000049B6-2y4O for pgsql-hackers@lists.postgresql.org; Thu, 17 Sep 2026 12:04:50 +0000 Received: from relay4-d.mail.gandi.net ([2001:4b98:dc4:8::224]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x7Ar9-000000010Jl-0iwd for pgsql-hackers@postgresql.org; Thu, 17 Sep 2026 12:04:50 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id 957C13EDC0; Thu, 17 Sep 2026 12:04:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=postgresfriends.org; s=gm1; t=1789646685; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4zPikXKs6rZMwpq/JjoTQ7LewarlGbLKHfjZLvgEzbc=; b=e8YYyfGwYUzTZgm6umKbx6PCQqCcrZcR5fLj6rLLdA7g3HNCarIk21btxOLvSPPEbe1VZQ CnKnV0EWYn/sm6YEsnp0jrn7Ht006elVfaF+sgJO/dpEP+Zxl7J04rx86J0m/PR533QIX1 rLr3rxEZimMD1m9QjveJ3UHXtaWy2opiGnhfdQyJRtvIJc1gNnaiZlsmVCn1UpB76LGoyu XMduCFVwT6yVtZjeqkx+JYiYBEbjUvv9rtYNLBbEY/wrfhuf0ufSCdcl1PDYLnx1w2o+dW xhOk6ijfUsVjb9WpeEVqr/VkoFeIPXN4/z8Xvi22aFi7HpLaZwFBECz0JMKdFA== Message-ID: Date: Thu, 17 Sep 2026 14:04:42 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: ON EMPTY clause for aggregate and window functions To: Jeevan Chalke , PostgreSQL Hackers Cc: Tom Lane , Isaac Morland References: Content-Language: en-US From: Vik Fearing In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-GND-Sasl: vik@postgresfriends.org X-GND-Cause: dmFkZTFRHf8U2oiiNjQkIZLttil30M2c6rqgq82RCPgPFIixKy1kTxGr2LmRWX09z41puxQKNLMQnBuQvz9jBjtLdebPGhJMnnWvqlfU0B7k6hZuRc6+qAKS/8K2oZUqUjbRdCa5RmTK5LqDNZiLCgHgZuDr7RJfRs/nf22Nlr9qezC1Qipmc0JhsRvgShcpOHA1cOZi5/3ta9FVZm0pt+aHOofmzw3pKIU65IFoUZHbHV+BFpSu13+6It1tEwJ6IGHNyTcjFa5O5F3BLqjOjLuT5B27zMVwS/vOVEPUQNdpmUSj13TQKSaWwRk89KWxwNSTxpPc1MiR0EsXI26TKU0jrIflnQgcqk8n90SSotnC034jQ5pwSkO74rglfg1F54l81aQRSvNMLJtXbBqgHSJMBYHZ0ec4zDAhzq4fNk1x0IpVf7Lkcr5ZY4pdUCUA2v5Fwc84AOgIBNR4mUNO6Fk/CN3whBAfFoV8Frnw0/x++R58kl7HI+VhUYFXVAP1yyHqctEKS0ORCtRyMlLYrligM4n5isd2oIv9SvL1iAbXTXFoFEk2xNteE7YQLnC3qPQXxv5Bkc0CSFRd1JT7I17tzlu8fl9qz4prqznh5CXU5DHkvUPzWKoW0NW34YlU09jDyuK7arQK2OWD1e8Hlr2/Y3DEutN0nnM9kSeR/Utk+x126A X-GND-State: clean X-GND-Score: -100 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 12/09/2026 15:18, Jeevan Chalke wrote: > > ON EMPTY is now implemented as exactly: > > agg(args, default ON EMPTY)  ==  COALESCE(agg(args), default) I've taken a quick look at this, and I found a few bugs. 1) The first one is that the constant requirement only looks for actual constants and not scoped constants.  For example: CREATE TABLE cust (id INTEGER, name text, def_amount PRIMARY KEY (id)); CREATE TABLE ord (id INTEGER, custid INTEGER, amount INTEGER); INSERT INTO cust SELECT g, 'c' || g, g FROM generate_series(1, 4) AS g (g); INSERT INTO ord VALUES (1,1,100), (2,1,50), (3,3,7); -- rejected: "ON EMPTY expression must be a constant value" SELECT c.id,        (SELECT SUM(o.amount, c.def_amount ON EMPTY)         FROM ord AS o         WHERE o.custid = c.id) FROM cust AS c; Here, the c.def_amount is constant for the subquery and should be accepted.  The example is perhaps a bit contrived, but the logic is sound. 2) Another bug I found is this: CREATE TABLE mm (a INTEGER); INSERT INTO mm SELECT g FROM generate_series(1, 10_000) AS g (g); ANALYZE mm; SELECT COALESCE(MAX(a), -1) FROM mm WHERE a > 100_000;  --  -1 SELECT MAX(a, -1 ON EMPTY)  FROM mm WHERE a > 100_000;  --  -1 CREATE INDEX ON mm (a); SELECT COALESCE(MAX(a), -1) FROM mm WHERE a > 100_000;  -- -1 SELECT MAX(a, -1 ON EMPTY)  FROM mm WHERE a > 100_000;  --  NULL When MAX and MIN get optimized with an index, the ON EMPTY seems to be dropped. 3) The set quantifier is not recognized. SELECT SUM(ALL      a, 0 ON EMPTY) FROM t; SELECT SUM(DISTINCT a, 0 ON EMPTY) FROM t; Neither of those parse. I will keep reviewing this feature. -- Vik Fearing