PostgreSQL查詢歷史最大進程數(shù)的多種方法
PostgreSQL 查詢歷史最大進程數(shù)方法
PostgreSQL 提供了多種方式來查詢數(shù)據(jù)庫的歷史最大進程數(shù)(連接數(shù))。以下是幾種有效的方法:
一 使用統(tǒng)計收集器數(shù)據(jù)
1.1 查看當(dāng)前進程信息
SELECT setting as max_connections, (SELECT setting FROM pg_settings WHERE name = 'superuser_reserved_connections') AS reserved_connections FROM pg_settings WHERE name = 'max_connections';
輸出示例:
max_connections | reserved_connections -----------------+---------------------- 100 | 3 (1 row)
1.2 查詢歷史峰值(需要統(tǒng)計收集器開啟)
--當(dāng)前數(shù)據(jù)庫最大連接數(shù)(自統(tǒng)計重置后) SELECT max(numbackends) AS max_connections_ever FROM pg_stat_database; --查看所有數(shù)據(jù)庫最大連接數(shù) SELECT datname, max(numbackends) AS max_connections FROM pg_stat_database GROUP BY datname ORDER BY max_connections DESC;
輸出示例:
white=# SELECT max(numbackends) AS max_connections_ever white-# FROM pg_stat_database; max_connections_ever ---------------------- 2 (1 row) white=# white=# SELECT datname, white-# max(numbackends) AS max_connections white-# FROM pg_stat_database white-# GROUP BY datname white-# ORDER BY max_connections DESC; datname | max_connections -----------+----------------- white | 2 white3 | 0 white2 | 0 postgres | 0 white1 | 0 template0 | 0 | 0 template1 | 0 (8 rows) white=#
二 使用 pg_stat_activity 歷史快照
2.1 創(chuàng)建擴展記錄歷史數(shù)據(jù)
CREATE EXTENSION IF NOT EXISTS pg_stat_statements; CREATE EXTENSION IF NOT EXISTS pg_stat_monitor; -- 更高級的監(jiān)控 -- 創(chuàng)建歷史記錄表 CREATE TABLE connection_history AS SELECT now() AS sample_time, count(*) AS connection_count FROM pg_stat_activity; -- 設(shè)置定時任務(wù)(如pgAgent或cron)定期執(zhí)行 INSERT INTO connection_history SELECT now(), count(*) FROM pg_stat_activity;
2.2 查詢記錄的歷史峰值
SELECT max(connection_count) AS historical_max_connections FROM connection_history; -- 按時間范圍查詢 SELECT date_trunc('hour', sample_time) AS hour, max(connection_count) AS max_connections_per_hour FROM connection_history WHERE sample_time > now() - interval '7 days' GROUP BY 1 ORDER BY 1;
三 使用日志分析
3.1 配置日志記錄連接信息
在 postgresql.conf
中設(shè)置:
log_connections = on log_disconnections = on log_line_prefix = '%m [%p] %q%u@%d '
3.2 使用pgBadger分析日志
# 生成連接數(shù)報告 pgbadger -j 8 /var/log/postgresql/postgresql-*.log -o pgbadger_report.html # 然后查看報告中的"Connections"部分
3.3 手動分析日志
# 統(tǒng)計每日最大連接數(shù) grep "connection authorized" /var/log/postgresql/postgresql-15-main.log | \ awk '{print $1}' | \ sort | uniq -c | sort -n
四 使用監(jiān)控系統(tǒng)數(shù)據(jù)
4.1 Prometheus + Grafana
如果使用Prometheus監(jiān)控:
-- 查詢過去30天最大連接數(shù) max_over_time(pg_stat_activity_count[30d])
4.2 pgMonitor (Crunchy Data)
-- 使用預(yù)置的監(jiān)控視圖 SELECT * FROM monitor.pg_connection_history ORDER BY max_connections DESC LIMIT 10;
五 使用系統(tǒng)視圖組合查詢
5.1 綜合查詢方法
WITH connection_stats AS ( SELECT count(*) AS current_connections, (SELECT setting FROM pg_settings WHERE name = 'max_connections')::int AS max_allowed_connections FROM pg_stat_activity ) SELECT current_connections, max_allowed_connections, round(current_connections * 100.0 / max_allowed_connections, 2) AS percentage_used, (SELECT max(numbackends) FROM pg_stat_database) AS historical_max FROM connection_stats;
5.2 跟蹤連接變化(需要定期執(zhí)行)
-- 創(chuàng)建跟蹤表 CREATE TABLE IF NOT EXISTS connection_tracking ( ts timestamp PRIMARY KEY, connection_count integer, max_since_reset integer ); -- 更新函數(shù) CREATE OR REPLACE FUNCTION update_connection_stats() RETURNS void AS $$ DECLARE current_count integer; historical_max integer; BEGIN SELECT count(*) INTO current_count FROM pg_stat_activity; SELECT max(numbackends) INTO historical_max FROM pg_stat_database; INSERT INTO connection_tracking VALUES (now(), current_count, historical_max) ON CONFLICT (ts) DO NOTHING; END; $$ LANGUAGE plpgsql; -- 設(shè)置定時執(zhí)行(如每分鐘) -- 可以通過pg_cron擴展或外部cron設(shè)置
六 使用 pg_controldata 工具
對于緊急情況分析:
# 查看數(shù)據(jù)庫控制文件信息(包含一些歷史統(tǒng)計) pg_controldata /var/lib/postgresql/15/main # 查找以下行: "Maximum data alignment: "Database block size: "Blocks per segment of large relation: "WAL block size: "Latest checkpoint's MAXIMUM CONNECTIONS:"
最佳實踐建議
- 長期監(jiān)控:設(shè)置定期記錄機制(如每分鐘記錄連接數(shù))
- 警報閾值:當(dāng)連接數(shù)接近
max_connections
的80%時觸發(fā)警報 - 連接池:考慮使用pgBouncer或pgPool-II管理連接
- 定期審查:每月分析連接趨勢,調(diào)整
max_connections
參數(shù)
以上就是PostgreSQL查詢歷史最大進程數(shù)的多種方法的詳細內(nèi)容,更多關(guān)于PostgreSQL查詢最大進程數(shù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
PostgreSQL pg_ctl start啟動超時實例分析
這篇文章主要給大家介紹了關(guān)于PostgreSQL pg_ctl start啟動超時的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01查看postgresql數(shù)據(jù)庫用戶系統(tǒng)權(quán)限、對象權(quán)限的方法
這篇文章主要介紹了查看postgresql數(shù)據(jù)庫用戶系統(tǒng)權(quán)限、對象權(quán)限的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12PostgreSQL連接數(shù)過多報錯:too many clients already的解決
在使用 Navicat 連接 PostgreSQL 數(shù)據(jù)庫時,突然遭遇到了一個報錯:“FATAL: sorry, too many clients already”,這一錯誤提示表明數(shù)據(jù)庫連接數(shù)已經(jīng)達到上限,無法再創(chuàng)建新連接,所以本文給大家介紹了相關(guān)的解決辦法,需要的朋友可以參考下2024-03-03Vcenter清理/storage/archive空間的處理方式
通過SSH登陸到Vcenter并檢查/storage/archive目錄發(fā)現(xiàn)占用過高,該目錄用于存儲歸檔的日志文件和歷史數(shù)據(jù),解決方案是保留近30天的歸檔文件,這篇文章主要給大家介紹了關(guān)于Vcenter清理/storage/archive空間的處理方式,需要的朋友可以參考下2024-11-11postgresql 實現(xiàn)replace into功能的代碼
這篇文章主要介紹了postgresql 實現(xiàn)replace into功能的代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01PostgreSQL流復(fù)制參數(shù)max_wal_senders的用法說明
這篇文章主要介紹了PostgreSQL流復(fù)制參數(shù)max_wal_senders的用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12