[PATCH] Replace COUNT(NULL) with '0'::bigint
Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.
Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:
git clone --branch t53170_1 https://github.com/hackorum-dev/postgres.gitIn a checkout you already have, add the fork once:
git remote add hackorum https://github.com/hackorum-dev/postgres.gitthen, for this patchset and every later one:
git fetch hackorum t53170_1 && git checkout t53170_1Patchset v1 (message #1) is on t53170_1
Hi,
In [1]/messages/by-id/CAApHDvrde9DUpQ3DhPd3ia9tchVmhZqewfzxSYWmYFWVj=LPpg@mail.gmail.com, David Rowley noted that COUNT(NULL) can be replaced
with '0'::bigint. The change should be straightforward, and I came
up with the attached patch to implement it.
[1]: /messages/by-id/CAApHDvrde9DUpQ3DhPd3ia9tchVmhZqewfzxSYWmYFWVj=LPpg@mail.gmail.com
--
Regards
Junwang Zhao
On Sun, 25 Jan 2026 at 19:48, Junwang Zhao <zhjwpku@gmail.com> wrote:
In [1], David Rowley noted that COUNT(NULL) can be replaced
with '0'::bigint. The change should be straightforward, and I came
up with the attached patch to implement it.[1] /messages/by-id/CAApHDvrde9DUpQ3DhPd3ia9tchVmhZqewfzxSYWmYFWVj=LPpg@mail.gmail.com
Coming up with the code to do this wasn't the problem. I already
posted it in the patch in [2]/messages/by-id/CAApHDvppFVDdjpYrs=pwgCnp-jv-tneQyfu8rWM8ymHcuJOJYw@mail.gmail.com. The reason I didn't commit that part is
simply that I don't think anyone would ever write COUNT(NULL) in a
query. My opinion has not changed since I wrote [3]/messages/by-id/CAApHDvppFVDdjpYrs=pwgCnp-jv-tneQyfu8rWM8ymHcuJOJYw@mail.gmail.com. The main reason I
even mentioned COUNT(NULL) is because I wanted the API to support
replacing the Aggref with some other Node type, and that was the only
example I could think of to test to ensure it worked. I had hoped that
someone might come up with some ideas to do that which might be more
applicable in the real world, rather than regurgitate ideas I'd
thought of but didn't think were useful.
Do you really feel like this is useful to anyone? or did you just
write the patch because you can?
Corey did mention in [4]/messages/by-id/CADkLM=e2Rb+s3TV3LgYx8O1z2Cs+X2FSoafd6=UzpPD7hiocfw@mail.gmail.com that he is in favour, so maybe it's just me
who thinks it's useless... Perhaps someone else wants to commit it.
David
[2]: /messages/by-id/CAApHDvppFVDdjpYrs=pwgCnp-jv-tneQyfu8rWM8ymHcuJOJYw@mail.gmail.com
[3]: /messages/by-id/CAApHDvppFVDdjpYrs=pwgCnp-jv-tneQyfu8rWM8ymHcuJOJYw@mail.gmail.com
[4]: /messages/by-id/CADkLM=e2Rb+s3TV3LgYx8O1z2Cs+X2FSoafd6=UzpPD7hiocfw@mail.gmail.com
On Sun, Jan 25, 2026 at 8:13 PM David Rowley <dgrowleyml@gmail.com> wrote:
On Sun, 25 Jan 2026 at 19:48, Junwang Zhao <zhjwpku@gmail.com> wrote:
In [1], David Rowley noted that COUNT(NULL) can be replaced
with '0'::bigint. The change should be straightforward, and I came
up with the attached patch to implement it.[1] /messages/by-id/CAApHDvrde9DUpQ3DhPd3ia9tchVmhZqewfzxSYWmYFWVj=LPpg@mail.gmail.com
Coming up with the code to do this wasn't the problem. I already
posted it in the patch in [2].
Ah, I didn't check the v2 details carefully.
The reason I didn't commit that part is
simply that I don't think anyone would ever write COUNT(NULL) in a
query. My opinion has not changed since I wrote [3]. The main reason I
even mentioned COUNT(NULL) is because I wanted the API to support
replacing the Aggref with some other Node type, and that was the only
example I could think of to test to ensure it worked. I had hoped that
someone might come up with some ideas to do that which might be more
applicable in the real world, rather than regurgitate ideas I'd
thought of but didn't think were useful.Do you really feel like this is useful to anyone? or did you just
write the patch because you can?
I did this because of Corey's comment "There is nothing faster than nothing"
and your note "Which is leaving the door open for more aggressive
optimisations that someone might want to do, e.g. the mentioned
COUNT(NULL) replaced with '0'::bigint."
I wasn't following that context and only saw this thread recently,
so I may have misunderstood the point of the comment.
I agree that people are more likely to write COUNT(1) than
COUNT(NULL), so I'm fine with leaving COUNT(NULL) as is.
Corey did mention in [4] that he is in favour, so maybe it's just me
who thinks it's useless... Perhaps someone else wants to commit it.David
[2] /messages/by-id/CAApHDvppFVDdjpYrs=pwgCnp-jv-tneQyfu8rWM8ymHcuJOJYw@mail.gmail.com
[3] /messages/by-id/CAApHDvppFVDdjpYrs=pwgCnp-jv-tneQyfu8rWM8ymHcuJOJYw@mail.gmail.com
[4] /messages/by-id/CADkLM=e2Rb+s3TV3LgYx8O1z2Cs+X2FSoafd6=UzpPD7hiocfw@mail.gmail.com
--
Regards
Junwang Zhao
David Rowley <dgrowleyml@gmail.com> writes:
On Sun, 25 Jan 2026 at 19:48, Junwang Zhao <zhjwpku@gmail.com> wrote:
In [1], David Rowley noted that COUNT(NULL) can be replaced
with '0'::bigint. The change should be straightforward, and I came
up with the attached patch to implement it.
Coming up with the code to do this wasn't the problem. I already
posted it in the patch in [2]. The reason I didn't commit that part is
simply that I don't think anyone would ever write COUNT(NULL) in a
query.
Yeah, I agree that this should be so rare as to not be worth expending
planner cycles to check for, not to mention future code maintenance
costs. The other special cases we optimize COUNT() for are common
real-world usages, but not this.
regards, tom lane
On Mon, Jan 26, 2026 at 12:44 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
David Rowley <dgrowleyml@gmail.com> writes:
On Sun, 25 Jan 2026 at 19:48, Junwang Zhao <zhjwpku@gmail.com> wrote:
In [1], David Rowley noted that COUNT(NULL) can be replaced
with '0'::bigint. The change should be straightforward, and I came
up with the attached patch to implement it.Coming up with the code to do this wasn't the problem. I already
posted it in the patch in [2]. The reason I didn't commit that part is
simply that I don't think anyone would ever write COUNT(NULL) in a
query.Yeah, I agree that this should be so rare as to not be worth expending
planner cycles to check for, not to mention future code maintenance
costs. The other special cases we optimize COUNT() for are common
real-world usages, but not this.
Agreed, good to know, thanks for the explanation.
regards, tom lane
--
Regards
Junwang Zhao