Just ran across this somewhat randomly: there's an addon to the `pg` module that might make this whole thing simpler: https://github.com/brianc/node-pg-cursor.