pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Vik Fearing <vik@postgresfriends.org>
To: Jeevan Chalke <jeevan.chalke@enterprisedb.com>
To: PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Isaac Morland <isaac.morland@gmail.com>
Subject: Re: ON EMPTY clause for aggregate and window functions
Date: Thu, 17 Sep 2026 14:04:42 +0200
Message-ID: <fc92fc73-0d18-4b2e-bfcb-f9b4c4d6c4b6@postgresfriends.org> (raw)
In-Reply-To: <CAM2+6=ULokLQY+-2FiY95Rp5CwV7nRF0a0dP9DzO6mb1_mKn6g@mail.gmail.com>
References: <CAM2+6=VS=fSKxfimW6Th9iu_xjbxOEAKg4eYwaa=SMg3X8pHaQ@mail.gmail.com>
	<CAMsGm5fxS7+WbQ_mAziiTH8GxqurmVMKUFG5bm2pM7X+JCzpug@mail.gmail.com>
	<CAM2+6=Ws0Z2BmLv=NGe3B7ZZLT3y6RQxXwGKbQErYFmCFEd9Sw@mail.gmail.com>
	<CAM2+6=ULokLQY+-2FiY95Rp5CwV7nRF0a0dP9DzO6mb1_mKn6g@mail.gmail.com>


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







view thread (14+ messages)  latest in thread

Message-ID: <fc92fc73-0d18-4b2e-bfcb-f9b4c4d6c4b6@postgresfriends.org>
Permalink:  ../fc92fc73-0d18-4b2e-bfcb-f9b4c4d6c4b6@postgresfriends.org/
Also on:    postgresql.org/message-id/fc92fc73-0d18-4b2e-bfcb-f9b4c4d6c4b6@postgresfriends.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: vik@postgresfriends.org, jeevan.chalke@enterprisedb.com, tgl@sss.pgh.pa.us, isaac.morland@gmail.com
  Subject: Re: ON EMPTY clause for aggregate and window functions
  In-Reply-To: <fc92fc73-0d18-4b2e-bfcb-f9b4c4d6c4b6@postgresfriends.org>
  References: <CAM2+6=VS=fSKxfimW6Th9iu_xjbxOEAKg4eYwaa=SMg3X8pHaQ@mail.gmail.com> <CAMsGm5fxS7+WbQ_mAziiTH8GxqurmVMKUFG5bm2pM7X+JCzpug@mail.gmail.com> <CAM2+6=Ws0Z2BmLv=NGe3B7ZZLT3y6RQxXwGKbQErYFmCFEd9Sw@mail.gmail.com> <CAM2+6=ULokLQY+-2FiY95Rp5CwV7nRF0a0dP9DzO6mb1_mKn6g@mail.gmail.com> <fc92fc73-0d18-4b2e-bfcb-f9b4c4d6c4b6@postgresfriends.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox