Skip to content

Commit 880f555

Browse files
committed
Scan in a read-only transaction when hitting PG
1 parent 8cfc776 commit 880f555

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/persist/src/postgres.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,16 @@ impl Consensus for PostgresConsensus {
465465
limit
466466
)));
467467
};
468-
let rows = {
468+
let rows = if self.postgres_tuned() {
469+
// In serializable mode, postgres behaves better when read-only mode is explicitly
470+
// declared, which requires explicitly initializing a transaction.
471+
let mut client = self.get_connection().await?;
472+
let txn = client.build_transaction().read_only(true).start().await?;
473+
let statement = txn.prepare_cached(q).await?;
474+
let result = txn.query(&statement, &[&key, &from, &limit]).await?;
475+
txn.commit().await?;
476+
result
477+
} else {
469478
let client = self.get_connection().await?;
470479
let statement = client.prepare_cached(q).await?;
471480
client.query(&statement, &[&key, &from, &limit]).await?

0 commit comments

Comments
 (0)