pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Robert Haas <robertmhaas@gmail.com>
Cc: Jeevan Chalke <jeevan.chalke@enterprisedb.com>
Cc: PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Cc: Vik Fearing <vik@postgresfriends.org>
Cc: Isaac Morland <isaac.morland@gmail.com>
Subject: Re: ON EMPTY clause for aggregate and window functions
Date: Thu, 17 Sep 2026 09:31:44 -0400
Message-ID: <636820.1789651904@sss.pgh.pa.us> (raw)
In-Reply-To: <CA+TgmoYfZW9y_T8A1nykRUcvJ2FQtMpBjgkq=+TCdq0-odF4=w@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>
	<CA+TgmoYfZW9y_T8A1nykRUcvJ2FQtMpBjgkq=+TCdq0-odF4=w@mail.gmail.com>

Robert Haas <robertmhaas@gmail.com> writes:
> This seems pretty useless -- the new syntax is more work for us to
> maintain, and doesn't really add any value over just using COALESCE.

It could add value, in scenarios where substitute-for-NULL doesn't
give quite the behavior you want.  But that's not so for the two
aggregates the spec has bothered to define this for, and I'm having
a hard time coming up with an aggregate for which it would be so.

If somebody actually did have an aggregate that could return null for
more than zero input rows, they could always do

    CASE WHEN count(x) > 0 THEN frobnitz(x) ELSE value_for_zero_rows END

which has the extra benefit that you can choose "count(x)" or
"count(*)" depending on your desires for what to do with null
inputs.

> To be clear, I expect to lose this argument on the grounds that
> apparently this syntax is in the spec and therefore we ought to
> support it. But I don't understand why the spec -- or we -- should
> spend time inventing new ways to spell existing behaviors. That just
> seems confusing (and this particular choice of syntax seems
> extra-confusing).

Yes, this choice of syntax sucks pretty badly.  In the COALESCE
spelling, it's crystal clear that the substitute value is not an
aggregate argument and so is evaluated at most once (per group);
there's no need for gamesmanship around restricting it to be a
constant.

I'd be totally fine with rejecting this as a frammish we do
not care to support.

			regards, tom lane






view thread (14+ messages)  latest in thread

Message-ID: <636820.1789651904@sss.pgh.pa.us>
Permalink:  ../636820.1789651904@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/636820.1789651904@sss.pgh.pa.us

 · 

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: tgl@sss.pgh.pa.us, robertmhaas@gmail.com, jeevan.chalke@enterprisedb.com, vik@postgresfriends.org, isaac.morland@gmail.com
  Subject: Re: ON EMPTY clause for aggregate and window functions
  In-Reply-To: <636820.1789651904@sss.pgh.pa.us>
  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> <CA+TgmoYfZW9y_T8A1nykRUcvJ2FQtMpBjgkq=+TCdq0-odF4=w@mail.gmail.com> <636820.1789651904@sss.pgh.pa.us>

* 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