Here is a more or less straightforward patch to add more use ofpsprintf() in place of hardcoded palloc(N) + sprintf() and the like.