Re: BUG #19633: Unexpected results of IN (subquery) with a non-deterministic collation
Re: BUG #19633: Unexpected results of IN (subquery) with a non-deterministic collation
От:
Tender Wang <tndrwang@gmail.com>
Дата:
Hi Andrey,
Andrey Rachitskiy 于2026年8月21日周五 04:35写道:
>
>
> чт, 20 авг. 2026 г. в 23:41, PG Bug reporting form :
> Under gdb that path is create_unique_paths from the join search:
>
> #0 create_unique_paths at planner.c:8673
> #1 populate_joinrel_with_paths at joinrels.c:1189
> #2 make_join_rel at joinrels.c:774
> #3 make_rels_by_clause_joins at joinrels.c:300
> #4 join_search_one_level at joinrels.c:123
> #5 standard_join_search at allpaths.c:3987
>
> (gdb) pgprint sjinfo
> SpecialJoinInfo [jointype=JOIN_SEMI semi_can_btree=true
> semi_can_hash=false]
> [semi_operators] OidList: [98]
> [semi_rhs_exprs]
> Var [varno=3 varattno=1 vartype=25 varcollid=100]
>
> Unique/HashAgg take the collation from the RHS expression. Here that
> is the default collation of t_rhs.c0 (varcollid 100), not the join's
> input collation (ci). So Sort+Unique keeps both 'a' and 'A'. Under
> ci those values are equal, and the inner join emits the outer 'a'
> twice (count is 2).
>
> The attached patch labels each semi_rhs_expr with the join operator's
> inputcollid via canonicalize_ec_expression (RelabelType when needed),
> so unique-ification uses the same equality as the join. Sort then
> shows
>
> Sort Key: t_semi_cs.c0 COLLATE case_insensitive
>
> and the count is 1. A regress case is included in collate.icu.utf8.
>
> I am still getting familiar with this part. I am not sure this is the right place or the right approach.
>
> Thoughts?
The approach looks good to me. I'd suggest adjusting the comment as follows:
...
/* so far so good, keep building lists */
semi_operators = lappend_oid(semi_operators, opno);
/*
* Ensure that the RHS expression exposes the join operator's input
* collation. The expression will later be used as a grouping key when
* unique-ifying the RHS, so its collation must agree with the semijoin
* equality semantics.
*/
semi_rhs_exprs =
lappend(semi_rhs_exprs,
canonicalize_ec_expression((Expr *) copyObject(right_expr),
exprType(right_expr),
op->inputcollid));
...
--
Thanks,
Tender Wang
Re: BUG #19633: Unexpected results of IN (subquery) with a non-deterministic collation
От:
Richard Guo <guofenglinux@gmail.com>
Дата:
On Thu, Sep 10, 2026 at 7:40 AM Alexander Korotkov wrote: > I've revised the patch. The v3 changes are: > 1. The test now covers the hash path as well. v2 turned > enable_hashagg off, which is precisely the plan shape that was not > broken in isolation; both strategies are now exercised, each with its > plan and its result. I think it'd better to resue existing tables rather than create new ones for the tests. Maybe we can reuse test1cs/test1ci, test2cs/test2ci, test3cs/test3ci to reproduce this issue? Also on master we have this new GUC enable_groupagg. Maybe it'd better to use that instead of enable_sort? - Richard
Re: BUG #19633: Unexpected results of IN (subquery) with a non-deterministic collation
Hi, Richard!
On Tue, Sep 15, 2026 at 4:48 AM Richard Guo wrote:
>
> On Mon, Sep 14, 2026 at 5:24 PM Richard Guo wrote:
> > On Sat, Sep 12, 2026 at 10:57 PM Alexander Korotkov
> > wrote:
> > > Any objections if I push (and backpatch) this?
>
> > I'll have a look ASAP.
>
> I looked at v6. The fix itself looks right to me. I have some
> comments on the rest of the patch, though.
>
> * Overall, the new comments in this patch are overly verbose. I think
> we should keep them in line with the style of the existing comments.
> Also, the new comment in compute_semijoin_info() talks only about the
> collation, but the call also keeps the expression's original type,
> which I think the comment should mention as well.
>
> * In compute_semijoin_info(), the new comment and the relabeling call
> are inserted between the two appends, right after
>
> /* so far so good, keep building lists */
>
> That comment is about building both lists, so it reads oddly with the
> appends split apart. I'd rather relabel right_expr as a separate step
> before that comment, and then keep the two appends together under it.
>
> * The tests alter tables that other test cases in this file rely on,
> which is really not a good practice.
>
> INSERT INTO test2cs VALUES ('abc');
> ALTER TABLE test2cs ALTER COLUMN x SET (n_distinct = 1);
> ANALYZE test1ci, test2cs;
> ...
> ANALYZE test1cs;
>
> The INSERT is only there so that test2cs holds both 'abc' and 'ABC',
> but test3cs already holds exactly that, and it also has the unique
> index under case_sensitive, so it covers the unique-index case as
> well. We don't need the n_distinct hack or the ANALYZEs either:
> disabling seqscan and material is enough to make the planner
> unique-ify test3cs, since the semijoin's inner index-only scan on
> test3cs can't use the case_insensitive join clause as an index
> condition and would have to be rescanned for every outer row.
>
> * The same query
>
> SELECT count(*) FROM test1ci
> WHERE x COLLATE case_insensitive IN
> (SELECT x FROM test2cs WHERE x IN ('abc', 'ABC'));
>
> is run three times, once under the default settings without an EXPLAIN
> and once under each forced strategy. The first run adds nothing, as
> its plan is one of the other two and we can't even tell which.
>
> * The last test, the one with "ANALYZE test1cs", pins a Hash Right
> Semi Join, which is a plain semijoin that never unique-ifies, so on
> master it doesn't exercise this patch at all. What it does exercise
> is reduce_unique_semijoins(), which was fixed in 5a55ea507 and is
> already covered by the "Ensure that semijoin is not reduced to
> innerjoin" tests earlier in this file. The case the comment describes
> only exists in the back branches, where create_unique_path() still
> consults relation_has_unique_index_for() with semi_rhs_exprs, and
> using test3cs as the RHS covers that with the same query.
>
> * One more thing for the commit message. In the back branches
> create_unique_path() also has the DISTINCT-subquery shortcut, which
> goes through the collation-blind two-list query_is_distinct_for()
> wrapper. This patch happens to close that too, because
> translate_sub_tlist() punts on the RelabelType, so it's worth a
> sentence there.
>
> Attached is v7 with these changes.
Thank you for your corrections. I accept all of them.
I suggest we could change the test query as follows.
SELECT count(*) FROM test1ci
WHERE x COLLATE case_insensitive IN (SELECT x FROM test3cs);
This shape allows to achieve the desired plan both with
enable_groupagg = off and with enable_sort = off. Therefore sql part
for pg19- will only differ with this GUC name. I've checked it works
down to pg14 and keeps distinguishing the fix.
Attached v8 with this change.
------
Regards,
Alexander Korotkov
Supabase
Re: BUG #19633: Unexpected results of IN (subquery) with a non-deterministic collation
От:
Alexander Korotkov <aekorotkov@gmail.com>
Дата:
On Tue, Sep 15, 2026 at 4:05 PM Alexander Korotkov wrote:
> On Tue, Sep 15, 2026 at 4:48 AM Richard Guo wrote:
> >
> > On Mon, Sep 14, 2026 at 5:24 PM Richard Guo wrote:
> > > On Sat, Sep 12, 2026 at 10:57 PM Alexander Korotkov
> > > wrote:
> > > > Any objections if I push (and backpatch) this?
> >
> > > I'll have a look ASAP.
> >
> > I looked at v6. The fix itself looks right to me. I have some
> > comments on the rest of the patch, though.
> >
> > * Overall, the new comments in this patch are overly verbose. I think
> > we should keep them in line with the style of the existing comments.
> > Also, the new comment in compute_semijoin_info() talks only about the
> > collation, but the call also keeps the expression's original type,
> > which I think the comment should mention as well.
> >
> > * In compute_semijoin_info(), the new comment and the relabeling call
> > are inserted between the two appends, right after
> >
> > /* so far so good, keep building lists */
> >
> > That comment is about building both lists, so it reads oddly with the
> > appends split apart. I'd rather relabel right_expr as a separate step
> > before that comment, and then keep the two appends together under it.
> >
> > * The tests alter tables that other test cases in this file rely on,
> > which is really not a good practice.
> >
> > INSERT INTO test2cs VALUES ('abc');
> > ALTER TABLE test2cs ALTER COLUMN x SET (n_distinct = 1);
> > ANALYZE test1ci, test2cs;
> > ...
> > ANALYZE test1cs;
> >
> > The INSERT is only there so that test2cs holds both 'abc' and 'ABC',
> > but test3cs already holds exactly that, and it also has the unique
> > index under case_sensitive, so it covers the unique-index case as
> > well. We don't need the n_distinct hack or the ANALYZEs either:
> > disabling seqscan and material is enough to make the planner
> > unique-ify test3cs, since the semijoin's inner index-only scan on
> > test3cs can't use the case_insensitive join clause as an index
> > condition and would have to be rescanned for every outer row.
> >
> > * The same query
> >
> > SELECT count(*) FROM test1ci
> > WHERE x COLLATE case_insensitive IN
> > (SELECT x FROM test2cs WHERE x IN ('abc', 'ABC'));
> >
> > is run three times, once under the default settings without an EXPLAIN
> > and once under each forced strategy. The first run adds nothing, as
> > its plan is one of the other two and we can't even tell which.
> >
> > * The last test, the one with "ANALYZE test1cs", pins a Hash Right
> > Semi Join, which is a plain semijoin that never unique-ifies, so on
> > master it doesn't exercise this patch at all. What it does exercise
> > is reduce_unique_semijoins(), which was fixed in 5a55ea507 and is
> > already covered by the "Ensure that semijoin is not reduced to
> > innerjoin" tests earlier in this file. The case the comment describes
> > only exists in the back branches, where create_unique_path() still
> > consults relation_has_unique_index_for() with semi_rhs_exprs, and
> > using test3cs as the RHS covers that with the same query.
> >
> > * One more thing for the commit message. In the back branches
> > create_unique_path() also has the DISTINCT-subquery shortcut, which
> > goes through the collation-blind two-list query_is_distinct_for()
> > wrapper. This patch happens to close that too, because
> > translate_sub_tlist() punts on the RelabelType, so it's worth a
> > sentence there.
> >
> > Attached is v7 with these changes.
>
> Thank you for your corrections. I accept all of them.
>
> I suggest we could change the test query as follows.
>
> SELECT count(*) FROM test1ci
> WHERE x COLLATE case_insensitive IN (SELECT x FROM test3cs);
>
> This shape allows to achieve the desired plan both with
> enable_groupagg = off and with enable_sort = off. Therefore sql part
> for pg19- will only differ with this GUC name. I've checked it works
> down to pg14 and keeps distinguishing the fix.
>
> Attached v8 with this change.
Please, let me know if you're OK with this change. As this is major
change for the SQL tests of this patch.
------
Regards,
Alexander Korotkov
Supabase
Re: BUG #19633: Unexpected results of IN (subquery) with a non-deterministic collation
От:
Richard Guo <guofenglinux@gmail.com>
Дата:
On Wed, Sep 16, 2026 at 8:05 AM Alexander Korotkov wrote: > Please, let me know if you're OK with this change. As this is major > change for the SQL tests of this patch. v8 LGTM. - Richard
Re: BUG #19633: Unexpected results of IN (subquery) with a non-deterministic collation
От:
Alexander Korotkov <aekorotkov@gmail.com>
Дата:
On Wed, Sep 16, 2026 at 3:19 AM Richard Guo wrote: > On Wed, Sep 16, 2026 at 8:05 AM Alexander Korotkov wrote: > > Please, let me know if you're OK with this change. As this is major > > change for the SQL tests of this patch. > > v8 LGTM. Thank you. Committed and backpatched. Fingers crossed, I didn't mess up anything during backpatching. WIll keep looking at buildfarm. ------ Regards, Alexander Korotkov Supabase
Re: BUG #19633: Unexpected results of IN (subquery) with a non-deterministic collation
От:
Andrey Rachitskiy <pl0h0yp1@gmail.com>
Дата:
ср, 16 сент. 2026 г. в 15:15, Alexander Korotkov <aekorotkov@gmail.com>:
On Wed, Sep 16, 2026 at 3:19 AM Richard Guo <guofenglinux@gmail.com> wrote:
> On Wed, Sep 16, 2026 at 8:05 AM Alexander Korotkov <aekorotkov@gmail.com> wrote:
> > Please, let me know if you're OK with this change. As this is major
> > change for the SQL tests of this patch.
>
> v8 LGTM.
Thank you. Committed and backpatched.
Fingers crossed, I didn't mess up anything during backpatching. WIll
keep looking at buildfarm.
Richard, Alexander - Thank you for reviewing & pushing.
Regards,
Rachitskiy Andrey
Rachitskiy Andrey