diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index b5827d3980..95914a1784 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -5301,7 +5301,8 @@ create_ordered_paths(PlannerInfo *root,
 		}
 
 		/* Add projection step if needed */
-		if (sorted_path->pathtarget != target)
+		if (sorted_path->pathtarget != target &&
+		    memcmp(sorted_path->pathtarget, target, sizeof(PathTarget)) != 0)
 			sorted_path = apply_projection_to_path(root, ordered_rel,
 												   sorted_path, target);
 
@@ -5379,7 +5380,8 @@ create_ordered_paths(PlannerInfo *root,
 										 &total_groups);
 
 			/* Add projection step if needed */
-			if (sorted_path->pathtarget != target)
+			if (sorted_path->pathtarget != target &&
+				memcmp(sorted_path->pathtarget, target, sizeof(PathTarget)) != 0)
 				sorted_path = apply_projection_to_path(root, ordered_rel,
 													   sorted_path, target);
 