What's feeling like it might be the best thing is to go ahead and syntactically convert to the second form of identity 3 as soon as we've determined it's applicable, so that upper C Vars are always marked with both OJ relids. Not sure how much work is involved there.
I'm thinking something that once we've determined identity 3 applies in make_outerjoininfo we record information about how to adjust relids for upper quals and store this info in all upper JoinTreeItems. Then afterwards in distribute_qual_to_rels we check all this info stored in current JoinTreeItem and adjust relids for the qual accordingly.
The info we record should consist of two parts, target_relids and added_relids. Vars and PHVs in quals that belong to target_relids should be adjusted to include added_relids.
Following this idea I come up with attached patch (no comments and test cases yet). It fixes the presented case and passes check-world. Before finishing it I'd like to know whether this idea works. Any comments are appreciated.