pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Guo <rguo@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix GROUP BY column removal for NULLS NOT DISTINCT indexes
Date: Fri, 13 Nov 2026 00:54:38 +0000
Message-ID: <E1xRfYs-00000000VC1-1UKE@gemulon.postgresql.org> (raw)

Fix GROUP BY column removal for NULLS NOT DISTINCT indexes

remove_useless_groupby_columns() removes GROUP BY columns that are
functionally dependent on other GROUP BY columns, as proven by a
unique index.  The index columns are normally required to be NOT NULL,
but commit bd10ec529 relaxed that for indexes defined with NULLS NOT
DISTINCT, on the grounds that such an index allows at most one row
with a NULL in the indexed columns.

That reasoning does not hold if the relation is on the nullable side
of an outer join.  Rows that are null-extended by the join also have
NULLs in the indexed columns, so they fall into the same group as the
relation's one real NULL row once the other columns are removed from
the GROUP BY, even though they differ in those columns.  This results
in wrong query results.

To fix, skip the NULLS NOT DISTINCT relaxation for relations whose
GROUP BY Vars can be nulled by an outer join, and insist on NOT NULL
index columns as before.  That case remains safe, since no real row
can then be mistaken for a null-extended one.

Back-patch to v18, where the NULLS NOT DISTINCT relaxation was
introduced.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: wenhui qiu <qiuwenhuifx@gmail.com>
Discussion: https://postgr.es/m/CAMbWs484GmrQNb6nYV8cbV2sq2a7DKDDb=WwJS=2rtS8ma+Mxw@mail.gmail.com
Backpatch-through: 18

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/819ff87c0968d69c1d9d6e8f3201a4aafedd93f5

Modified Files
--------------
src/backend/optimizer/plan/initsplan.c   | 12 ++++++++++--
src/test/regress/expected/aggregates.out | 16 ++++++++++++++++
src/test/regress/sql/aggregates.sql      |  5 +++++
3 files changed, 31 insertions(+), 2 deletions(-)



view thread (3+ messages)

Message-ID: <E1xRfYs-00000000VC1-1UKE@gemulon.postgresql.org>
Permalink:  ../E1xRfYs-00000000VC1-1UKE@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xRfYs-00000000VC1-1UKE@gemulon.postgresql.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-committers@postgresql.org
  Cc: rguo@postgresql.org
  Subject: Re: pgsql: Fix GROUP BY column removal for NULLS NOT DISTINCT indexes
  In-Reply-To: <E1xRfYs-00000000VC1-1UKE@gemulon.postgresql.org>
  References: <E1xRfYs-00000000VC1-1UKE@gemulon.postgresql.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