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

store/postgres: warn if locale is not en_US.utf8 #3671

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,4 @@ happens, subgraphs might process inconsistent data. Defaults to 250.
identified as unused, `graph-node` will wait at least this long before
actually deleting the data (value is in minutes, defaults to 360, i.e. 6
hours)
- `GRAPH_STORE_LOCAL`: default is `en_US.utf8`. This is used to mandate the locale for database. This is useful for comparing strings fields.
7 changes: 7 additions & 0 deletions graph/src/env/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ pub struct EnvVarsStore {
/// once the new behavior has run in the hosted service for a few days
/// without issues.
pub disable_error_for_toplevel_parents: bool,

/// This is used to mandate what locale the Database run.
/// Set by the environment variable `GRAPH_STORE_LOCALE`.
pub store_locale: String,
}

// This does not print any values avoid accidentally leaking any sensitive env vars
Expand Down Expand Up @@ -128,6 +132,7 @@ impl From<InnerStore> for EnvVarsStore {
connection_idle_timeout: Duration::from_secs(x.connection_idle_timeout_in_secs),
write_queue_size: x.write_queue_size,
disable_error_for_toplevel_parents: x.disable_error_for_toplevel_parents.0,
store_locale: x.store_locale,
}
}
}
Expand Down Expand Up @@ -173,4 +178,6 @@ pub struct InnerStore {
write_queue_size: usize,
#[envconfig(from = "GRAPH_DISABLE_ERROR_FOR_TOPLEVEL_PARENTS", default = "false")]
disable_error_for_toplevel_parents: EnvVarBoolean,
#[envconfig(from = "GRAPH_STORE_LOCALE", default = "en_US.utf8")]
store_locale: String,
}
18 changes: 18 additions & 0 deletions store/postgres/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ table! {
nspname -> Text,
}
}
// Readonly; only mapping the columns we want
table! {
pg_database(datname) {
datname -> Text,
datcollate -> Text,
}
}

table! {
subgraphs.table_stats {
Expand All @@ -47,6 +54,17 @@ table! {
}
}

/// Get collate for current database
pub fn get_collation(conn: &PgConnection) -> Result<String, StoreError> {
use pg_database as db;

return db::table
.filter(db::datname.eq(diesel::dsl::sql("current_database()")))
.select(db::datcollate)
.get_result(conn)
.map_err(StoreError::from);
}

// In debug builds (for testing etc.) create exclusion constraints, in
// release builds for production, skip them
#[cfg(debug_assertions)]
Expand Down
9 changes: 9 additions & 0 deletions store/postgres/src/connection_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,15 @@ impl PoolInner {
let pool = self.clone();
let conn = self.get().map_err(|_| StoreError::DatabaseUnavailable)?;

let db_locale = catalog::get_collation(&conn)?;

if !db_locale.eq(&ENV_VARS.store.store_locale) {
panic!(
"Database locale is not '{}', but {}",
ENV_VARS.store.store_locale, db_locale
);
}

advisory_lock::lock_migration(&conn)
.unwrap_or_else(|err| die(&pool.logger, "failed to get migration lock", &err));
let result = pool
Expand Down