Resending this patch rebased on latest master. Please apply or tell meif it looks wrong.
Original patch mail:
http://www.postgresql.org/message-id/CAB5ZCnSrt-b0SEBCU8d5O6TL7rFxo1zWFWDj86fGzhg0N4tZog@mail.gmail.com
Thanks,David