Skip to content

Commit 62e0020

Browse files
committed
Fix misprocessing of equivalence classes involving record_eq().
canonicalize_ec_expression() is supposed to agree with coerce_type() as to whether a RelabelType should be inserted to make a subexpression be valid input for the operators of a given opclass. However, it did the wrong thing with named-composite-type inputs to record_eq(): it put in a RelabelType to RECORDOID, which the parser doesn't. In some cases this was harmless because all code paths involving a particular equivalence class did the same thing, but in other cases this would result in failing to recognize a composite-type expression as being a member of an equivalence class that it actually is a member of. The most obvious bad effect was to fail to recognize that an index on a composite column could provide the sort order needed for a mergejoin on that column, as reported by Teodor Sigaev. I think there might be other, subtler, cases that result in misoptimization. It also seems possible that an unwanted RelabelType would sometimes get into an emitted plan --- but because record_eq and friends don't examine the declared type of their input expressions, that would not create any visible problems. To fix, just treat RECORDOID as if it were a polymorphic type, which in some sense it is. We might want to consider formalizing that a bit more someday, but for the moment this seems to be the only place where an IsPolymorphicType() test ought to include RECORDOID as well. This has been broken for a long time, so back-patch to all supported branches. Discussion: https://postgr.es/m/a6b22369-e3bf-4d49-f59d-0c41d3551e81@sigaev.ru
1 parent 32453bc commit 62e0020

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/backend/optimizer/path/equivclass.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,9 @@ canonicalize_ec_expression(Expr *expr, Oid req_type, Oid req_collation)
424424

425425
/*
426426
* For a polymorphic-input-type opclass, just keep the same exposed type.
427+
* RECORD opclasses work like polymorphic-type ones for this purpose.
427428
*/
428-
if (IsPolymorphicType(req_type))
429+
if (IsPolymorphicType(req_type) || req_type == RECORDOID)
429430
req_type = expr_type;
430431

431432
/*

src/test/regress/expected/join.out

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,6 +2582,36 @@ select * from a left join b on i = x and i = y and x = i;
25822582
---+---+---
25832583
(0 rows)
25842584

2585+
rollback;
2586+
--
2587+
-- test handling of merge clauses using record_ops
2588+
--
2589+
begin;
2590+
create type mycomptype as (id int, v bigint);
2591+
create temp table tidv (idv mycomptype);
2592+
create index on tidv (idv);
2593+
explain (costs off)
2594+
select a.idv, b.idv from tidv a, tidv b where a.idv = b.idv;
2595+
QUERY PLAN
2596+
----------------------------------------------------------
2597+
Merge Join
2598+
Merge Cond: (a.idv = b.idv)
2599+
-> Index Only Scan using tidv_idv_idx on tidv a
2600+
-> Materialize
2601+
-> Index Only Scan using tidv_idv_idx on tidv b
2602+
(5 rows)
2603+
2604+
set enable_mergejoin = 0;
2605+
explain (costs off)
2606+
select a.idv, b.idv from tidv a, tidv b where a.idv = b.idv;
2607+
QUERY PLAN
2608+
----------------------------------------------------
2609+
Nested Loop
2610+
-> Seq Scan on tidv a
2611+
-> Index Only Scan using tidv_idv_idx on tidv b
2612+
Index Cond: (idv = a.idv)
2613+
(4 rows)
2614+
25852615
rollback;
25862616
--
25872617
-- test NULL behavior of whole-row Vars, per bug #5025

src/test/regress/sql/join.sql

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,26 @@ select * from a left join b on i = x and i = y and x = i;
632632

633633
rollback;
634634

635+
--
636+
-- test handling of merge clauses using record_ops
637+
--
638+
begin;
639+
640+
create type mycomptype as (id int, v bigint);
641+
642+
create temp table tidv (idv mycomptype);
643+
create index on tidv (idv);
644+
645+
explain (costs off)
646+
select a.idv, b.idv from tidv a, tidv b where a.idv = b.idv;
647+
648+
set enable_mergejoin = 0;
649+
650+
explain (costs off)
651+
select a.idv, b.idv from tidv a, tidv b where a.idv = b.idv;
652+
653+
rollback;
654+
635655
--
636656
-- test NULL behavior of whole-row Vars, per bug #5025
637657
--

0 commit comments

Comments
 (0)