From f35c5f3664005983f9732efc141c0e92a5874b2d Mon Sep 17 00:00:00 2001 From: Harshit Joshi Date: Mon, 26 Aug 2024 23:47:09 +0530 Subject: [PATCH] adds postgres version to the schema cache. --- crates/pg_schema_cache/src/lib.rs | 1 + crates/pg_schema_cache/src/schema_cache.rs | 8 +++- crates/pg_schema_cache/src/versions.rs | 43 ++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 crates/pg_schema_cache/src/versions.rs diff --git a/crates/pg_schema_cache/src/lib.rs b/crates/pg_schema_cache/src/lib.rs index f1e020a5..aed612c6 100644 --- a/crates/pg_schema_cache/src/lib.rs +++ b/crates/pg_schema_cache/src/lib.rs @@ -4,6 +4,7 @@ #![feature(future_join)] mod functions; +mod versions; mod schema_cache; mod schemas; mod tables; diff --git a/crates/pg_schema_cache/src/schema_cache.rs b/crates/pg_schema_cache/src/schema_cache.rs index 639393a2..cf17697c 100644 --- a/crates/pg_schema_cache/src/schema_cache.rs +++ b/crates/pg_schema_cache/src/schema_cache.rs @@ -6,6 +6,7 @@ use crate::functions::Function; use crate::schemas::Schema; use crate::tables::Table; use crate::types::PostgresType; +use crate::versions::Version; #[derive(Debug, Clone, Default)] pub struct SchemaCache { @@ -13,6 +14,7 @@ pub struct SchemaCache { pub tables: Vec, pub functions: Vec, pub types: Vec, + pub versions: Vec, } impl SchemaCache { @@ -21,11 +23,12 @@ impl SchemaCache { } pub async fn load(pool: &PgPool) -> SchemaCache { - let (schemas, tables, functions, types) = join!( + let (schemas, tables, functions, types, versions) = join!( Schema::load(pool), Table::load(pool), Function::load(pool), - PostgresType::load(pool) + PostgresType::load(pool), + Version::load(pool), ) .await; @@ -34,6 +37,7 @@ impl SchemaCache { tables, functions, types, + versions, } } diff --git a/crates/pg_schema_cache/src/versions.rs b/crates/pg_schema_cache/src/versions.rs new file mode 100644 index 00000000..eff70f14 --- /dev/null +++ b/crates/pg_schema_cache/src/versions.rs @@ -0,0 +1,43 @@ +use sqlx::PgPool; + +use crate::schema_cache::SchemaCacheItem; + +#[derive(Debug, Clone, Default)] +pub struct Version { + pub version: Option, + pub version_num: Option, + pub active_connections: Option, + pub max_connections: Option, +} + +impl SchemaCacheItem for Version { + type Item = Version; + + async fn load(pool: &PgPool) -> Vec { + sqlx::query_as!( + Version, + r#"select + version(), + current_setting('server_version_num') :: int8 AS version_num, + ( + select + count(*) :: int8 AS active_connections + FROM + pg_stat_activity + ) AS active_connections, + current_setting('max_connections') :: int8 AS max_connections;"# + ) + .fetch_all(pool) + .await + .unwrap() + } + + /* + Sample Output: + -[ RECORD 1 ]------+-------------------------------------------------------------------------------------------------------------------------- + version | PostgreSQL 15.7 (Debian 15.7-1.pgdg120+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit + version_num | 150007 + active_connections | 8 + max_connections | 100 + */ +}