Wrong results from GROUP BY column removal

Started by Richard Guo13 days ago4 messageshackers
Jump to latest
#1Richard Guo
guofenglinux@gmail.com

I was reloading my memory of the various uniqueness proofs in our
code, in order to pick the UniqueKeys work back up, and I noticed a
thinko in remove_useless_groupby_columns().

/*
* We must insist that the index columns are all defined NOT
* NULL otherwise duplicate NULLs could exist. However, we
* can relax this check when the index is defined with NULLS
* NOT DISTINCT as there can only be 1 NULL row, therefore
* functional dependency on the unique columns is maintained,
* despite the NULL.
*/

This reasoning doesn't hold when the relation is on the nullable side
of an outer join. The rows null-extended by the join have NULLs in
the indexed columns too, so once the other columns are removed from
the GROUP BY, they end up in the same group as the relation's one real
NULL row, even though they differ from it in those columns. It's not
hard to come up with a query that gives wrong results because of this.

create table t1 (a int);
create table t2 (a int, b int not null);
create unique index on t2 (a) nulls not distinct;
insert into t1 values (1), (9);
insert into t2 values (null, 1);

select t2.a, t2.b, count(*) from t1 left join t2 on t1.a = t2.b group
by t2.a, t2.b;
a | b | count
---+---+-------
| 1 | 2
(1 row)

There should be two groups here, (NULL, 1) and (NULL, NULL).

I think we can fix this by not applying the NULLS NOT DISTINCT
relaxation when the relation's GROUP BY Vars can be nulled by an outer
join, which we can tell from their varnullingrels. See attached.

I think this needs to be back-patched to v18.

- Richard

Attachments:

t258463_1
v1-0001-Fix-GROUP-BY-column-removal-for-NULLS-NOT-DISTINC.patchapplication/octet-stream; name=v1-0001-Fix-GROUP-BY-column-removal-for-NULLS-NOT-DISTINC.patchDownload+31-3
#2wenhui qiu
qiuwenhuifx@gmail.com
In reply to: Richard Guo (#1)
Re: Wrong results from GROUP BY column removal

Hi Richard,

Thanks for working on this! The problem analysis is spot-on. In an
outer join scenario, null-extended rows have NULLs not only in the indexed
column but also across other columns of the nullable table. Once those other
columns are removed from GROUP BY, the null-extended rows and the table's
actual NULL row get mistakenly merged into the same group, leading to wrong
query results. This is indeed a subtle bug introduced by commit bd10ec529,
and the fix looks very solid.

Just a very minor thought on the implementation: while nullable_relids is
allocated in the planner context and will be reclaimed anyway, perhaps we
could consider adding a bms_free(nullable_relids) before exiting
remove_useless_groupby_columns(), just to stay consistent with how other
temporary bitmapsets are cleaned up in that function?It is not a big deal at
all, so feel free to ignore it if you prefer keeping it as-is.

Regards

#3Richard Guo
guofenglinux@gmail.com
In reply to: wenhui qiu (#2)
Re: Wrong results from GROUP BY column removal

On Wed, Nov 11, 2026 at 6:17 PM wenhui qiu <qiuwenhuifx@gmail.com> wrote:

Just a very minor thought on the implementation: while nullable_relids is allocated in the planner context and will be reclaimed anyway, perhaps we could consider adding a bms_free(nullable_relids) before exiting remove_useless_groupby_columns(), just to stay consistent with how other temporary bitmapsets are cleaned up in that function?

I disagree. This function doesn't free any of its other bitmapsets,
so I'm not sure where you get that conclusion. Freeing just this one
would look odd. Also, I don't see the point: this one small bitmapset
has no real impact on memory usage, and it will be cleaned up along
with the planner's memory context anyway.

I've pushed 0001 as-is and back-patched it to v18.

- Richard

#4wenhui qiu
qiuwenhuifx@gmail.com
In reply to: Richard Guo (#3)
Re: Wrong results from GROUP BY column removal

Hi Richard

I disagree. This function doesn't free any of its other bitmapsets,
so I'm not sure where you get that conclusion. Freeing just this one
would look odd. Also, I don't see the point: this one small bitmapset
has no real impact on memory usage, and it will be cleaned up along
with the planner's memory context anyway.

Sorry for the noise — I misremembered how remove_useless_groupby_columns()
handles its bitmapsets. You're right, there's no need to free
nullable_relids here.

Thanks