kvdb/postgres: use readonly db transaction if possible

This commit is contained in:
Joost Jager 2021-12-23 11:19:37 +01:00
parent ed511bb37f
commit 99566b768e
No known key found for this signature in database
GPG key ID: A61B9D4C393C59C7

View file

@ -4,6 +4,7 @@
package postgres
import (
"context"
"database/sql"
"sync"
@ -39,7 +40,16 @@ func newReadWriteTx(db *db, readOnly bool) (*readWriteTx, error) {
}
locker.Lock()
tx, err := db.db.Begin()
// Start the transaction. Don't use the timeout context because it would
// be applied to the transaction as a whole. If possible, mark the
// transaction as read-only to make sure that potential programming
// errors cannot cause changes to the database.
tx, err := db.db.BeginTx(
context.Background(),
&sql.TxOptions{
ReadOnly: readOnly,
},
)
if err != nil {
locker.Unlock()
return nil, err