Skip to content

[persist] Read-only scans for postgres consensus #32983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/persist/src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ impl PostgresConsensus {
})
}

fn postgres_tuned(&self) -> bool {
self.mode == PostgresMode::Postgres && USE_POSTGRES_TUNED_QUERIES.get(&self.dyncfg)
}

/// Drops and recreates the `consensus` table in Postgres
///
/// ONLY FOR TESTING
Expand Down Expand Up @@ -412,9 +416,7 @@ impl Consensus for PostgresConsensus {
WHERE last_seq.sequence_number = $4;
";

let q = if USE_POSTGRES_TUNED_QUERIES.get(&self.dyncfg)
&& self.mode == PostgresMode::Postgres
{
let q = if self.postgres_tuned() {
POSTGRES_CAS_QUERY
} else {
CRDB_CAS_QUERY
Expand Down Expand Up @@ -463,7 +465,16 @@ impl Consensus for PostgresConsensus {
limit
)));
};
let rows = {
let rows = if self.postgres_tuned() {
// In serializable mode, postgres behaves better when read-only mode is explicitly
// declared, which requires explicitly initializing a transaction.
let mut client = self.get_connection().await?;
let txn = client.build_transaction().read_only(true).start().await?;
let statement = txn.prepare_cached(q).await?;
let result = txn.query(&statement, &[&key, &from, &limit]).await?;
txn.commit().await?;
result
} else {
let client = self.get_connection().await?;
let statement = client.prepare_cached(q).await?;
client.query(&statement, &[&key, &from, &limit]).await?
Expand Down Expand Up @@ -523,9 +534,7 @@ impl Consensus for PostgresConsensus {
WHERE consensus.ctid = to_lock.ctid;
";

let q = if USE_POSTGRES_TUNED_QUERIES.get(&self.dyncfg)
&& self.mode == PostgresMode::Postgres
{
let q = if self.postgres_tuned() {
POSTGRES_TRUNCATE_QUERY
} else {
CRDB_TRUNCATE_QUERY
Expand Down