I personally do these in .NET, I obtain a connection dedicated to that operation, start a transaction, obtain lock and go crazy. Upon completion of the async workflow, the transaction closes and lock releases. I know I'm holding up a connection and putting some pressure on postgres by keeping a transaction open but session management might be harder as the underlying connection provider uses pooling and it is easier to use transactions rather than sessions here.
And if you add something like pgBouncer or whatever, this should still work but a session lock would fuck things up.
And if you add something like pgBouncer or whatever, this should still work but a session lock would fuck things up.