Skip to content

Commit d76481b

Browse files
authored
Merge pull request #141 from harshit54/schema_cache_versions
adds postgres version to the schema cache.
2 parents eeb64f5 + f35c5f3 commit d76481b

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

crates/pg_schema_cache/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![feature(future_join)]
55

66
mod functions;
7+
mod versions;
78
mod schema_cache;
89
mod schemas;
910
mod tables;

crates/pg_schema_cache/src/schema_cache.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ use crate::functions::Function;
66
use crate::schemas::Schema;
77
use crate::tables::Table;
88
use crate::types::PostgresType;
9+
use crate::versions::Version;
910

1011
#[derive(Debug, Clone, Default)]
1112
pub struct SchemaCache {
1213
pub schemas: Vec<Schema>,
1314
pub tables: Vec<Table>,
1415
pub functions: Vec<Function>,
1516
pub types: Vec<PostgresType>,
17+
pub versions: Vec<Version>,
1618
}
1719

1820
impl SchemaCache {
@@ -21,11 +23,12 @@ impl SchemaCache {
2123
}
2224

2325
pub async fn load(pool: &PgPool) -> SchemaCache {
24-
let (schemas, tables, functions, types) = join!(
26+
let (schemas, tables, functions, types, versions) = join!(
2527
Schema::load(pool),
2628
Table::load(pool),
2729
Function::load(pool),
28-
PostgresType::load(pool)
30+
PostgresType::load(pool),
31+
Version::load(pool),
2932
)
3033
.await;
3134

@@ -34,6 +37,7 @@ impl SchemaCache {
3437
tables,
3538
functions,
3639
types,
40+
versions,
3741
}
3842
}
3943

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use sqlx::PgPool;
2+
3+
use crate::schema_cache::SchemaCacheItem;
4+
5+
#[derive(Debug, Clone, Default)]
6+
pub struct Version {
7+
pub version: Option<String>,
8+
pub version_num: Option<i64>,
9+
pub active_connections: Option<i64>,
10+
pub max_connections: Option<i64>,
11+
}
12+
13+
impl SchemaCacheItem for Version {
14+
type Item = Version;
15+
16+
async fn load(pool: &PgPool) -> Vec<Version> {
17+
sqlx::query_as!(
18+
Version,
19+
r#"select
20+
version(),
21+
current_setting('server_version_num') :: int8 AS version_num,
22+
(
23+
select
24+
count(*) :: int8 AS active_connections
25+
FROM
26+
pg_stat_activity
27+
) AS active_connections,
28+
current_setting('max_connections') :: int8 AS max_connections;"#
29+
)
30+
.fetch_all(pool)
31+
.await
32+
.unwrap()
33+
}
34+
35+
/*
36+
Sample Output:
37+
-[ RECORD 1 ]------+--------------------------------------------------------------------------------------------------------------------------
38+
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
39+
version_num | 150007
40+
active_connections | 8
41+
max_connections | 100
42+
*/
43+
}

0 commit comments

Comments
 (0)