I tried to investigate the bug, but the complicated logic here completely messed up my mind...
Anyway, this patch can fix it and make the regress test happy. But I think it's better to get the author's advice - I copied this email to Alvaro, hope it doesn't offend him...