On 14/02/2016 5:11 pm, "Tom Lane" <t...@sss.pgh.pa.us> wrote: > > David Rowley <david.row...@2ndquadrant.com> writes: > > On 12/02/2016 12:01 am, "Tom Lane" <t...@sss.pgh.pa.us> wrote: > > I can't quite understand what you're seeing here. > > The second loop is iterating through the original GROUP BY list, so it > will see again any outer Vars that were excluded by the first loop. > It needs to exclude them exactly the same, because they are outside > the scope of its data structures. Consider something like (admittedly > pretty silly, but legal SQL) > > create table up (u1 int, u2 int, u3 int); > create table down (f1 int primary key, f2 int); > > select * from othertable, up > where u1 in (select f2 from down group by f1, f2, up.u3); > > up.u3 would have varlevelsup = 1, varno = 2, varattno = 3. > If you don't skip it then the surplusvars[var->varno] access > will be trying to fetch off the end of the surplusvars array, > because there is only one RTE in the subquery's rangetable > though there are two in the outer query's rangetable.
Thanks for explaining this. Clearly I missed the case of the varno pointing off the end of the array. Thanks for noticing and fixing.