diff --git a/src/backend/optimizer/plan/planmain.c b/src/backend/optimizer/plan/planmain.c index f9831870142..e98e0e62004 100644 --- a/src/backend/optimizer/plan/planmain.c +++ b/src/backend/optimizer/plan/planmain.c @@ -257,13 +257,6 @@ restart: */ generate_base_implied_equalities(root); - /* - * We have completed merging equivalence sets, so it's now possible to - * generate pathkeys in canonical form; so compute query_pathkeys and - * other pathkeys fields in PlannerInfo. - */ - (*qp_callback) (root, qp_extra); - /* * Examine any "placeholder" expressions generated during subquery pullup. * Make sure that the Vars they need are marked as needed at the relevant @@ -302,6 +295,13 @@ restart: * this point does not need to be able to restart. */ + /* + * We have completed merging equivalence sets, so it's now possible to + * generate pathkeys in canonical form; so compute query_pathkeys and + * other pathkeys fields in PlannerInfo. + */ + (*qp_callback) (root, qp_extra); + /* * Now distribute "placeholders" to base rels as needed. This has to be * done after join removal because removal could change whether a diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out index 64a6773edec..e16646f888d 100644 --- a/src/test/regress/expected/join.out +++ b/src/test/regress/expected/join.out @@ -8883,6 +8883,30 @@ ON sj_t1.id = _t2t3t4.id; -> Seq Scan on sj_t1 (24 rows) +-- Check SJE interaction with aggpresorted optimization. +CREATE TEMP TABLE sje_agg (id int PRIMARY KEY, p int, q int); +INSERT INTO sje_agg SELECT i, i % 3, (i * 7) % 10 FROM generate_series(1, 30) i; +ANALYZE sje_agg; +EXPLAIN (COSTS OFF) +SELECT count(DISTINCT a.p) AS dp, count(DISTINCT a.q) AS dq, + sum(DISTINCT a2.q) AS sq +FROM sje_agg a JOIN sje_agg a2 ON a.id = a2.id; + QUERY PLAN +------------------------------------ + Aggregate + -> Sort + Sort Key: a2.q + -> Seq Scan on sje_agg a2 +(4 rows) + +SELECT count(DISTINCT a.p) AS dp, count(DISTINCT a.q) AS dq, + sum(DISTINCT a2.q) AS sq +FROM sje_agg a JOIN sje_agg a2 ON a.id = a2.id; + dp | dq | sq +----+----+---- + 3 | 10 | 45 +(1 row) + -- -- Test RowMarks-related code -- diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql index 1bd6741ee7f..27c9deaaf8f 100644 --- a/src/test/regress/sql/join.sql +++ b/src/test/regress/sql/join.sql @@ -3462,6 +3462,20 @@ JOIN ( ) _t2t3t4 ON sj_t1.id = _t2t3t4.id; +-- Check SJE interaction with aggpresorted optimization. +CREATE TEMP TABLE sje_agg (id int PRIMARY KEY, p int, q int); +INSERT INTO sje_agg SELECT i, i % 3, (i * 7) % 10 FROM generate_series(1, 30) i; +ANALYZE sje_agg; + +EXPLAIN (COSTS OFF) +SELECT count(DISTINCT a.p) AS dp, count(DISTINCT a.q) AS dq, + sum(DISTINCT a2.q) AS sq +FROM sje_agg a JOIN sje_agg a2 ON a.id = a2.id; + +SELECT count(DISTINCT a.p) AS dp, count(DISTINCT a.q) AS dq, + sum(DISTINCT a2.q) AS sq +FROM sje_agg a JOIN sje_agg a2 ON a.id = a2.id; + -- -- Test RowMarks-related code --