Skip to content
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

fix: only update wrappers_fdw_stats table in a read-write transaction #188

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion wrappers/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 39 additions & 28 deletions wrappers/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,35 @@ fn get_stats_table() -> String {
// increase stats value
#[allow(dead_code)]
pub(crate) fn inc_stats(fdw_name: &str, metric: Metric, inc: i64) {
let sql = format!(
"insert into {} as s (fdw_name, {}) values($1, $2)
if !is_txn_read_only() {
imor marked this conversation as resolved.
Show resolved Hide resolved
let sql = format!(
"insert into {} as s (fdw_name, {}) values($1, $2)
on conflict(fdw_name)
imor marked this conversation as resolved.
Show resolved Hide resolved
do update set
{} = coalesce(s.{}, 0) + excluded.{},
updated_at = timezone('utc'::text, now())",
get_stats_table(),
metric,
metric,
metric,
metric
);
Spi::run_with_args(
&sql,
Some(vec![
(PgBuiltInOids::TEXTOID.oid(), fdw_name.into_datum()),
(PgBuiltInOids::INT8OID.oid(), inc.into_datum()),
]),
)
.unwrap();
get_stats_table(),
metric,
metric,
metric,
metric
);
Spi::run_with_args(
&sql,
Some(vec![
(PgBuiltInOids::TEXTOID.oid(), fdw_name.into_datum()),
(PgBuiltInOids::INT8OID.oid(), inc.into_datum()),
]),
)
.unwrap();
}
}

fn is_txn_read_only() -> bool {
imor marked this conversation as resolved.
Show resolved Hide resolved
let read_only_txn = Spi::get_one("show transaction_read_only")
.unwrap_or(Some("on"))
.unwrap_or("on");
read_only_txn == "on"
}

// get metadata
Expand All @@ -82,20 +91,22 @@ pub(crate) fn get_metadata(fdw_name: &str) -> Option<JsonB> {
// set metadata
#[allow(dead_code)]
pub(crate) fn set_metadata(fdw_name: &str, metadata: Option<JsonB>) {
let sql = format!(
"insert into {} as s (fdw_name, metadata) values($1, $2)
if !is_txn_read_only() {
let sql = format!(
"insert into {} as s (fdw_name, metadata) values($1, $2)
on conflict(fdw_name)
do update set
metadata = $2,
updated_at = timezone('utc'::text, now())",
get_stats_table()
);
Spi::run_with_args(
&sql,
Some(vec![
(PgBuiltInOids::TEXTOID.oid(), fdw_name.into_datum()),
(PgBuiltInOids::JSONBOID.oid(), metadata.into_datum()),
]),
)
.unwrap();
get_stats_table()
);
Spi::run_with_args(
&sql,
Some(vec![
(PgBuiltInOids::TEXTOID.oid(), fdw_name.into_datum()),
(PgBuiltInOids::JSONBOID.oid(), metadata.into_datum()),
]),
)
.unwrap();
}
}
Loading