Here's a WIP patch to evaluate. Dilip/Ashutosh, could you perhaps run some benchmarks, to see whether this addresses the performance issues? I guess it'd both be interesting to compare master with master + patch, and this thread's latest patch with the patch additionally applied.
pgsql-hackers by date:
Соглашаюсь с условиями обработки персональных данных