This commit is contained in:
2026-01-17 01:25:57 +01:00
commit 69b42ff74f
9 changed files with 132 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
[target.'cfg(target_os="macos")']
# Postgres symbols won't be available until runtime
rustflags = ["-Clink-arg=-Wl,-undefined,dynamic_lookup"]
+9
View File
@@ -0,0 +1,9 @@
.DS_Store
.idea/
/target
*.iml
**/*.rs.bk
Cargo.lock
tests/pg_regress/results
tests/pg_regress/regression.diffs
tests/pg_regress/regression.out
+38
View File
@@ -0,0 +1,38 @@
[package]
name = "pgerr"
version = "0.0.0"
edition = "2021"
[lib]
crate-type = ["cdylib", "lib"]
[[bin]]
name = "pgrx_embed_pgerr"
path = "./src/bin/pgrx_embed.rs"
[features]
default = ["pg13"]
pg13 = ["pgrx/pg13", "pgrx-tests/pg13" ]
pg14 = ["pgrx/pg14", "pgrx-tests/pg14" ]
pg15 = ["pgrx/pg15", "pgrx-tests/pg15" ]
pg16 = ["pgrx/pg16", "pgrx-tests/pg16" ]
pg17 = ["pgrx/pg17", "pgrx-tests/pg17" ]
pg18 = ["pgrx/pg18", "pgrx-tests/pg18" ]
pg_test = []
[dependencies]
pgrx = "=0.16.1"
serde_json = "1.0.149"
sysinfo = "0.37.2"
[dev-dependencies]
pgrx-tests = "=0.16.1"
[profile.dev]
panic = "unwind"
[profile.release]
panic = "unwind"
opt-level = 3
lto = "fat"
codegen-units = 1
+6
View File
@@ -0,0 +1,6 @@
comment = 'pgerr: Created by pgrx'
default_version = '@CARGO_VERSION@'
module_pathname = 'pgerr'
relocatable = false
superuser = true
trusted = false
+2
View File
@@ -0,0 +1,2 @@
- Install `cargo`, `pgrx` and initialize
- run with `cargo pgrx run p17`
+1
View File
@@ -0,0 +1 @@
::pgrx::pgrx_embed!();
+67
View File
@@ -0,0 +1,67 @@
use std::{thread, time::Duration};
use pgrx::prelude::*;
use sysinfo::System;
::pgrx::pg_module_magic!(name, version);
#[pg_extern]
fn pgerr() -> String {
String::from("pgerr extension running!")
}
pub struct SystemMetrics {
pub cpu_usage: f32,
pub used_memory: f32,
pub total_memory: f32,
}
fn get_metrics() -> SystemMetrics {
let mut sys = System::new_all();
sys.refresh_all();
thread::sleep(Duration::from_millis(500));
sys.refresh_all();
SystemMetrics {
cpu_usage: sys.global_cpu_usage(),
used_memory: sys.used_memory() as f32,
total_memory: sys.total_memory() as f32,
}
}
fn convert_bytes(byte_value: &mut f32, iters: Option<i32>) {
let iters = iters.unwrap_or(3);
for _ in 0..iters {
*byte_value /= 1024.0;
}
}
#[pg_extern]
fn sys_stats(json: bool) -> String {
let mut stats = get_metrics();
convert_bytes(&mut stats.used_memory, None);
convert_bytes(&mut stats.total_memory, None);
let result = if json {
format!(
"
{{
\"cpu\": {},
\"ram_used\": {},
\"ram_total\": {}
}}
",
stats.cpu_usage, stats.used_memory, stats.total_memory
)
} else {
format!(
"cpu={},ram used={}GB, ram total={}GB",
stats.cpu_usage, stats.used_memory, stats.total_memory
)
};
result
}
+3
View File
@@ -0,0 +1,3 @@
-- this setup file is run immediately after the regression database is (re)created
-- the file is optional but you likely want to create the extension
CREATE EXTENSION pgerr;
+3
View File
@@ -0,0 +1,3 @@
-- this setup file is run immediately after the regression database is (re)created
-- the file is optional but you likely want to create the extension
CREATE EXTENSION pgerr;