Richard Guo <guofengli...@gmail.com> writes: > However, for 'above_sjinfo' case, we should not use > othersj->syn_righthand, because othersj->syn_righthand contains relids > in sjinfo's righthand which should not be nulled by othersj after the > commutation. It seems what we should use here is sjinfo->syn_lefthand.
I had a hard time wrapping my brain around that to start with, but now I think you're right. othersj is syntactically above the current join, so its syn_righthand will cover all of the current join, but we only want to add nulling bits to Vars of the current join's LHS. (That is, we need to transform Pbc to Pb*c, not Pb*c*.) I also realized that there was a fairly critical nearby bug: make_outerjoininfo was failing to check whether the upper join's qual is actually of the form "Pbc", without any references to the lower join's LHS. So that led us to setting commute bits in some cases where we shouldn't, further confusing deconstruct_distribute_oj_quals. (I think this snuck in because its other code path doesn't need to make such a check, it being syntactically impossible to have such a reference if we start from the other form of the identity.) Fix pushed. This seems to take care of Robins' latest example in the bug #17781 thread [1], too. regards, tom lane [1] https://postgr.es/m/caep4nax9c5gxnbfea0jbfz7b+5f1bawt-rwqwyhev-wdps8...@mail.gmail.com