postgres-nio icon indicating copy to clipboard operation
postgres-nio copied to clipboard

Transaction support

Open adam-fowler opened this issue 1 year ago • 2 comments

Does it make sense to add support for transactions in similar manner to this

extension PostgresClient {
    func withTransaction<Value>(logger: Logger, _ process: (PostgresConnection) async throws -> Value) async throws -> Value {
        try await withConnection { connection in
            try await connection.query("BEGIN;", logger: logger)
            do {
                let value = try await process(connection)
                try await connection.query("COMMIT;", logger: logger)
                return value
            } catch {
                try await connection.query("ROLLBACK;", logger: logger)
                throw error
            }
        }
    }
}

adam-fowler avatar Oct 04 '24 12:10 adam-fowler

This was implemented in #519 and #538 but one very important omission I see is that the API doesn't allow specifying an isolation level. While the Postgres default isolation level is really safe, serializable isolation level can be preferred for some applications.

Serializable isolation can require some extra ability to allow retries since serializable errors are not fatal and retries can normally proceed just fine.

robertjpayne avatar Feb 21 '25 08:02 robertjpayne

@robertjpayne I'd be happy to accept a PR that adds this :)

fabianfett avatar Feb 21 '25 19:02 fabianfett