Query system statistics with SQL.

Overview

pg_stat_sysinfo

Collects system statistics.

----
CREATE EXTENSION pg_stat_sysinfo;
CREATE EXTENSION
----
SELECT * FROM pg_stat_sysinfo_collect();
      metric      |   dimensions |              at              |       value
------------------+--------------+------------------------------+--------------------
 load_average     | duration:1m  | 2023-01-17 20:40:24.74495+00 |       4.3427734375
 load_average     | duration:5m  | 2023-01-17 20:40:24.74495+00 |        2.740234375
 load_average     | duration:15m | 2023-01-17 20:40:24.74495+00 |           2.390625
 cpu_usage        |              | 2023-01-17 20:40:24.74495+00 |   0.12653848528862
 memory_usage     |              | 2023-01-17 20:40:24.74495+00 | 10.022946522725185
 memory_size      |              | 2023-01-17 20:40:24.74495+00 |         7966543872
 memory_available |              | 2023-01-17 20:40:24.74495+00 |         7168061440
 swap_usage       |              | 2023-01-17 20:40:24.74495+00 |                  0
 swap_size        |              | 2023-01-17 20:40:24.74495+00 |                  0
 swap_available   |              | 2023-01-17 20:40:24.74495+00 |                  0
 disk_usage       | fs:/         | 2023-01-17 20:40:24.74495+00 |  48.68292833372914
 disk_size        | fs:/         | 2023-01-17 20:40:24.74495+00 |        66404147200
 disk_available   | fs:/         | 2023-01-17 20:40:24.74495+00 |        34076663808
 disk_usage       | fs:/boot/efi | 2023-01-17 20:40:24.74495+00 |  4.986992082951202
 disk_size        | fs:/boot/efi | 2023-01-17 20:40:24.74495+00 |          109422592
 disk_available   | fs:/boot/efi | 2023-01-17 20:40:24.74495+00 |          103965696
(16 rows)

Enabling Caching Collector

Add the extension library to shared_preload_libraries and set the collection interval:

shared_preload_libraries = 'pg_stat_sysinfo.so'
pg_stat_sysinfo.interval = '1s'   # Accepts any time format Postgres recognizes

The cache is stored in Postgres shared memory. Up to 1280 KiB is cached -- over an hour, in most cases, at 1 query per second.

----
CREATE EXTENSION pg_stat_sysinfo;
CREATE EXTENSION
----
SELECT DISTINCT min(at) AS oldest,
       max(at) - min(at) AS during
  FROM pg_stat_sysinfo;
            oldest             |     during
-------------------------------+-----------------
 2023-01-17 20:04:46.220977+00 | 00:55:55.908972
(1 row)

----
SELECT DISTINCT dimensions FROM pg_stat_sysinfo;
   dimensions
----------------

 duration:1m
 duration:5m
 duration:15m
 disk:/
 disk:/boot/efi
(6 rows)

Basic cache statistics are available:

----
SELECT * FROM pg_stat_sysinfo_cache_summary();
 bytes_used | items
------------+-------
     563159 |  3587
(1 row)

Configuration Changes

The pg_stat_sysinfo.interval can be updated by changing postgres.conf and sending SIGHUP to the Postgres server process. The cache worker will use the new interval from that point forward.

If a long enough time has passed between server startup and a SIGHUP, or between one SIGHUP and another, the cache worker will refresh the disk metadata. This will allow it to pick up any disks that have been added to or removed from the system.

You might also like...
TDS 7.2+ (mssql / Microsoft SQL Server) async driver for rust

Tiberius A native Microsoft SQL Server (TDS) client for Rust. Supported SQL Server versions Version Support level Notes 2019 Tested on CI 2017 Tested

FeOphant - A SQL database server written in Rust and inspired by PostreSQL.

A PostgreSQL inspired SQL database written in Rust.

GlueSQL is a SQL database library written in Rust

GlueSQL is a SQL database library written in Rust. It provides a parser (sqlparser-rs), execution layer, and optional storage (sled) packaged into a single library.

Run SQL queries on CSV files

zsql run SQL queries on csv files A terminal utility to easily run SQL queries on CSV files. zsql is shipped as a small single binary powered by rust

Running SQL-like queries on files.

filesql Running SQL-like queries on files. Features Supported: REPL Basic SQL expressions. INSERT clause. (which inserts data into another file) WHERE

Tool to automate the visualisation of UML dependencies from a SQL file
Tool to automate the visualisation of UML dependencies from a SQL file

Doteur A simple tool to draw your mysql relations from exports. Help us If you use the tool, and like it, don't forget to add a star to the project on

Ormlite - An ORM in Rust for developers that love SQL.

ormlite ormlite is an ORM in Rust for developers that love SQL. It provides the following, while staying close to SQL, both in syntax and performance:

Distributed SQL database in Rust, written as a learning project

toyDB Distributed SQL database in Rust, written as a learning project. Most components are built from scratch, including: Raft-based distributed conse

Distributed, version controlled, SQL database with cryptographically verifiable storage, queries and results. Think git for postgres.

SDB - SignatureDB Distributed, version controlled, SQL database with cryptographically verifiable storage, queries and results. Think git for postgres

Owner
PostgresML
PostgresML
Gh-sql - Query GitHub Projects (beta) with SQL

gh-sql: Query GitHub Projects (beta) with SQL Installation gh extension install KOBA789/gh-sql Features SELECT items DELETE items UPDATE item fields

Hidekazu Kobayashi 108 Dec 7, 2022
ReefDB is a minimalistic, in-memory and on-disk database management system written in Rust, implementing basic SQL query capabilities and full-text search.

ReefDB ReefDB is a minimalistic, in-memory and on-disk database management system written in Rust, implementing basic SQL query capabilities and full-

Sacha Arbonel 75 Jun 12, 2023
🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, SQLite, and MSSQL.

SQLx ?? The Rust SQL Toolkit Install | Usage | Docs Built with ❤️ by The LaunchBadge team SQLx is an async, pure Rust† SQL crate featuring compile-tim

launchbadge 7.6k Dec 31, 2022
SQL validator tool for BigQuery standard SQL.

bqvalid What bqvalid does bqvalid is the SQL validator tool for BigQuery standard SQL. bqvalid fails with error message if there's the expression that

null 10 Dec 25, 2022
Query LDAP and AD with SQL

SQLDAP Ever wanted to query AD or LDAP with SQL like queries ? I'm going to answer this question myself: yes ! Why ? Because I never could remember al

null 9 Nov 15, 2022
An object-relational in-memory cache, supports queries with an SQL-like query language.

qlcache An object-relational in-memory cache, supports queries with an SQL-like query language. Warning This is a rather low-level library, and only p

null 3 Nov 14, 2021
Fully typed SQL query builder for Rust [deprecated]

What is Deuterium? Deuterium is a fancy SQL builder for Rust. It's designed to provide a DSL to easily build SQL queries in safe and typed way. Like R

Stanislav Panferov 169 Nov 20, 2022
Rust library to parse, deparse and normalize SQL queries using the PostgreSQL query parser

This Rust library uses the actual PostgreSQL server source to parse SQL queries and return the internal PostgreSQL parse tree.

pganalyze 37 Dec 18, 2022
Type-safe SQL query wrappers

fnsql   The fnsql crate provides simple type-safe optional wrappers around SQL queries. Instead of calling type-less .query() and .execute(), you call

Dan Aloni 9 Apr 29, 2022
A Toy Query Engine & SQL interface

Naive Query Engine (Toy for Learning) ?? This is a Query Engine which support SQL interface. And it is only a Toy for learn query engine only. You can

谭巍 45 Dec 21, 2022