diff --git a/.gitignore b/.gitignore
index 81c4d5e..2230d62 100644
--- a/.gitignore
+++ b/.gitignore
@@ -28,3 +28,4 @@ win32ver.rc
 /pgsql.sln.cache
 /Debug/
 /Release/
+tags
diff --git a/src/backend/optimizer/prep/prepjointree.c b/src/backend/optimizer/prep/prepjointree.c
index 5d16329..91d6fa0 100644
--- a/src/backend/optimizer/prep/prepjointree.c
+++ b/src/backend/optimizer/prep/prepjointree.c
@@ -877,9 +877,23 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
 	parse->hasSubLinks |= subquery->hasSubLinks;
 
 	/*
-	 * subquery won't be pulled up if it hasAggs or hasWindowFuncs, so no work
-	 * needed on those flags
+	 * For group clauses, reassign the indexes to the targetList, by
+	 * adding new junk ones (for now).
 	 */
+	parse->hasAggs = subquery->hasAggs;
+	parse->groupClause = copyObject(subquery->groupClause);
+	foreach (lc, parse->groupClause)
+	{
+		SortGroupClause *sort = (SortGroupClause *) lfirst(lc);
+		TargetEntry	   *tle = get_tle_by_resno(subquery->targetList, sort->tleSortGroupRef);
+		TargetEntry	   *newtle;
+		AttrNumber		resno = list_length(parse->targetList);
+
+		Assert(tle != NULL);
+		newtle = makeTargetEntry(tle->expr, resno + 1, tle->resname, true);
+		parse->targetList = lappend(parse->targetList, newtle);
+		sort->tleSortGroupRef = assignSortGroupRef(newtle, parse->targetList);
+	}
 
 	/*
 	 * Return the adjusted subquery jointree to replace the RangeTblRef entry
@@ -1071,10 +1085,14 @@ is_simple_subquery(Query *subquery)
 	 * that case the locking was originally declared in the upper query
 	 * anyway.
 	 */
-	if (subquery->hasAggs ||
+	/*
+	 * In some cases aggregate query can be pulled up. Need to add code
+	 * to make decision for more precise conditions.
+	 */
+	if (/*subquery->hasAggs ||*/
 		subquery->hasWindowFuncs ||
-		subquery->groupClause ||
-		subquery->havingQual ||
+		/*subquery->groupClause ||
+		subquery->havingQual ||*/
 		subquery->sortClause ||
 		subquery->distinctClause ||
 		subquery->limitOffset ||
