In general, you shouldn’t be responding from a transaction, because if the client is on slow connection you’re blocking database for a period of transfer. Instead, calculate a value in a transaction, and when you are done, release the database and start responding.