This patch filters out the junk columns created for ORDER BY/GROUP BY, and retains the junk columns created for RowLocks. I'm afraid this may have a problem about out-of-order resnos. For instance,
create table mytable (foo text, bar text);
# explain select foo from mytable order by bar for update of mytable; server closed the connection unexpectedly
This query triggers another Assert in apply_tlist_labeling:
Assert(dest_tle->resno == src_tle->resno);
At first there are three TargetEntry items: foo, bar and ctid, with resnos being 1, 2 and 3. And then the second item 'bar' is removed, leaving only two items: foo and ctid, with resnos being 1 and 3. So now we have a missing resno, and finally hit the Assert.