Here's an updated version taking into account the discussion so far.It's still a net addition of code (about +200 lines according todiffstat), but I think the consolidation of logic is probably worththat.
Any further comments?
regards, tom lane