sean.johnston@edgeintelligence.com writes: > select col1, col1 from loop_tbl group by 1, 2; > psql:t.sql:7: ERROR: targetlist item has multiple sortgroupref labels
This is the fault of commit 7012b132d ("Push down aggregates to remote servers"), which imagined that postgres_fdw can use apply_pathtarget_labeling_to_tlist() for situations well beyond that function's limited abilities. I kinda suspect that foreign_grouping_ok() has got more bugs than this with complicated sortgroupref situations, too; its willingness to scribble on the sortgrouprefs doesn't look like a great idea to me.
In the attached patch, I have removed apply_pathtarget_labeling_to_tlist() call. Since we are anyways checking that whether the GROUP BY expression is entirely present in a target list or not, we can safely create a tle for that GROUP BY expression and append that to the new targetlist by making sure that its sortgroupref is also transferred, eliminating the need for relabeling afterwords.
Now with these changes i.e. as we have moved setting sortgroupref for tle inside the if block itself, we don't have to reset the sortgroupref for any ORDER BY expression. That code is removed. This also eliminated the need for copying the pathtarget.
Please have a look at the changes and let me know if I missed any.
Thanks
regards, tom lane
--
Jeevan Chalke Technical Architect, Product Development EnterpriseDB Corporation The Enterprise PostgreSQL Company