aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/backend/optimizer/plan/planner.c14
-rw-r--r--src/test/regress/expected/select_distinct_on.out26
2 files changed, 22 insertions, 18 deletions
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index b5827d39808..62b2354f004 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -5300,8 +5300,11 @@ create_ordered_paths(PlannerInfo *root,
limit_tuples);
}
- /* Add projection step if needed */
- if (sorted_path->pathtarget != target)
+ /*
+ * If the pathtarget of the result path has different expressions from
+ * the target to be applied, a projection step is needed.
+ */
+ if (!equal(sorted_path->pathtarget->exprs, target->exprs))
sorted_path = apply_projection_to_path(root, ordered_rel,
sorted_path, target);
@@ -5378,8 +5381,11 @@ create_ordered_paths(PlannerInfo *root,
root->sort_pathkeys, NULL,
&total_groups);
- /* Add projection step if needed */
- if (sorted_path->pathtarget != target)
+ /*
+ * If the pathtarget of the result path has different expressions
+ * from the target to be applied, a projection step is needed.
+ */
+ if (!equal(sorted_path->pathtarget->exprs, target->exprs))
sorted_path = apply_projection_to_path(root, ordered_rel,
sorted_path, target);
diff --git a/src/test/regress/expected/select_distinct_on.out b/src/test/regress/expected/select_distinct_on.out
index b2978c1114a..958381afe5e 100644
--- a/src/test/regress/expected/select_distinct_on.out
+++ b/src/test/regress/expected/select_distinct_on.out
@@ -81,13 +81,12 @@ select distinct on (1) floor(random()) as r, f1 from int4_tbl order by 1,2;
EXPLAIN (COSTS OFF)
SELECT DISTINCT ON (four) four,two
FROM tenk1 WHERE four = 0 ORDER BY 1;
- QUERY PLAN
-----------------------------------
- Result
- -> Limit
- -> Seq Scan on tenk1
- Filter: (four = 0)
-(4 rows)
+ QUERY PLAN
+----------------------------
+ Limit
+ -> Seq Scan on tenk1
+ Filter: (four = 0)
+(3 rows)
-- and check the result of the above query is correct
SELECT DISTINCT ON (four) four,two
@@ -115,11 +114,10 @@ SELECT DISTINCT ON (four) four,two
EXPLAIN (COSTS OFF)
SELECT DISTINCT ON (four) four,hundred
FROM tenk1 WHERE four = 0 ORDER BY 1,2;
- QUERY PLAN
------------------------------------------------------
- Result
- -> Limit
- -> Index Scan using tenk1_hundred on tenk1
- Filter: (four = 0)
-(4 rows)
+ QUERY PLAN
+-----------------------------------------------
+ Limit
+ -> Index Scan using tenk1_hundred on tenk1
+ Filter: (four = 0)
+(3 rows)