diff --git a/src/backend/optimizer/plan/planmain.c b/src/backend/optimizer/plan/planmain.c
index 68d3409476c..9045c490486 100644
--- a/src/backend/optimizer/plan/planmain.c
+++ b/src/backend/optimizer/plan/planmain.c
@@ -260,13 +260,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
@@ -305,6 +298,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
