命令行工具pgcenter
pgCenter 是一个命令行管理工具,用于观察和排除 Postgres 故障。
7.1 主要目标
Postgres 提供有关其运行时的各种活动统计信息,例如连接、语句、数据库操作、复制、资源使用情况等。统计信息的一般目的是帮助 DBA 监视和排除 Postgres 故障。但是,这些统计信息以文本形式提供,从 SQL 函数和视图中检索,并且 Postgres 不提供用于处理统计信息视图的本地工具。
pgCenter 的主要目标是帮助 Postgres DBA 处理统计数据,并提供一种在运行时观察 Postgres 的便捷方式。
7.2 主要特征
-
类似于顶部的界面,可让您随时监控统计数据的变化。在此处查看详细信息。https://github.com/lesovsky/pgcenter/blob/master/doc/pgcenter-top-readme.md
-
配置管理功能允许查看和编辑当前配置文件并在需要时重新加载服务。
-
日志文件功能允许您在不停止统计监控的情况下快速检查 Postgres 日志。
-
“穷人的监控”允许您将 Postgres 统计信息收集到文件中并在以后生成报告。在此处查看详细信息。https://github.com/lesovsky/pgcenter/blob/master/doc/pgcenter-record-readme.md
-
等待事件探查器允许查看查询执行期间发生的等待事件。在此处查看详细信息。https://github.com/lesovsky/pgcenter/blob/master/doc/pgcenter-profile-readme.md
7.3 快速开始
从 DockerHub 拉取 Docker 镜像;运行 pgcenter 并连接到数据库。https://hub.docker.com/r/lesovsky/pgcenter
docker pull lesovsky/pgcenter:latest docker run -it --rm lesovsky/pgcenter:latest pgcenter top -h 1.2.3.4 -U user -d dbname
复制
7.4 支持的统计信息
PostgreSQL 统计
-
summary activity - 来自不同来源的指标的汇集/选择 - postgres 正常运行时间、版本、恢复状态、按状态分组的客户端数量、(自动)vacuum 数量、每秒语句数、最长事务的年龄和最长的vacuum;
-
pg_stat_activity - 连接的客户端和后台进程的活动。
-
pg_stat_database - 数据库范围和会话统计信息,例如提交/回滚的数量、处理的元组、死锁、临时文件等。
-
pg_stat_replication - 复制统计信息,如已连接的备用数据库、它们的活动和复制滞后。
-
pg_stat_user_tables、pg_statio_user_tables - 对表的访问(包括 IO)的统计信息。
-
pg_stat_user_indexes, pg_statio_user_indexes - 对索引的访问(包括 IO)统计。
-
pg_stat_user_functions - 函数执行的统计信息。
-
pg_stat_wal - WAL 使用统计。
-
pg_stat_statements - 执行的 SQL 语句的统计信息,包括时间和资源使用情况。
-
基于 pg_relation_size() 和 pg_total_relation_size() 函数的表大小统计;
-
pg_stat_progress_vacuum - (自动)真空操作的进度。
-
pg_stat_progress_cluster - CLUSTER 和 VACUUM FULL 操作的进度。
-
pg_stat_progress_create_index - CREATE INDEX 和 REINDEX 操作的进度。
-
pg_stat_progress_analyze - ANALYZE 操作的进度。
-
pg_stat_progress_basebackup - basebackup 操作的进度。
-
pg_stat_progress_copy - COPY 操作的进度。
system 统计
pgcenter top 还根据来自 procfs 文件系统的统计信息提供系统使用信息:
-
平均负载和 CPU 使用时间(用户、系统、nice、空闲、iowait、软件和硬件中断、窃取);
-
内存和交换使用情况、缓存和脏内存量、写回活动;
-
存储设备统计:IOPS、吞吐量、延迟、平均队列和请求大小、设备利用率;
-
网络接口统计:字节和数据包的吞吐量、不同类型的错误、饱和度和利用率。
-
挂载文件系统的使用统计:总大小、可用/已用/保留空间量和 inode。
在连接到远程 Postgres 的情况下,有可能使用用于从远程主机检索 /proc 统计信息的附加 SQL 函数。有关详细信息,请参阅此处的详细信息。https://github.com/lesovsky/pgcenter/blob/master/doc/pgcenter-config-readme.md
7.5 安装说明
DEB、RPM、APK 的软件包可在发布页面上找到。
https://github.com/lesovsky/pgcenter/releases
7.6 使用说明
pgCenter 已被开发为在 Linux 上工作,尚未在其他操作系统(操作系统)上进行测试;因此,不建议在替代系统上使用它,因为它无法正常运行。
pgCenter 支持广泛的 PostgreSQL 版本,尽管每个版本之间的统计数据不同。如果 pgCenter 无法读取特定的统计信息,它将显示描述性错误消息。
理想情况下,pgCenter 需要 SUPERUSER 数据库权限,或者至少需要查看统计信息、读取设置、日志文件和向其他后端发送信号的权限。 Postgres 10 中引入了具有此类权限的角色(阅读日志除外);在此处查看详细信息。https://www.postgresql.org/docs/current/static/default-roles.html
建议在运行 Postgres 的同一主机上运行 pgCenter。这是因为对于 Postgres 来说,pgCenter 只是一个简单的客户端应用程序,它可能与其他使用 Postgres 的应用程序存在相同的问题,例如网络相关问题、响应缓慢等。
可以在一台主机上运行 pgCenter 并连接到在另一台主机上运行的 Postgres,但某些功能可能无法正常工作 - 这完全适用于 pgcenter top 命令。
pgCenter 也支持 Amazon RDS for PostgreSQL,但如上所述,由于安全原因,PostgreSQL RDS 实例不支持不受信任的过程语言,因此某些功能将无法使用,系统统计信息也将无法使用。
7.7 使用例子
[postgres@pg14 ~]$ pgcenter --help pgCenter is a command line admin tool for PostgreSQL. Usage: pgcenter [flags] pgcenter [command] [command-flags] [args] Available commands: config installs or uninstalls pgcenter stats schema to Postgres profile wait events profiler record record stats to file report make report based on previously saved statistics top top-like stats viewer Flags: -?, --help show this help and exit --version show version information and exit Use "pgcenter [command] --help" for more information about a command. Report bugs to <https://github.com/lesovsky/pgcenter/issues>.
复制
[postgres@pg14 ~]$ pgcenter top --help 'pgcenter top' is the top-like stats viewer. Usage: pgcenter top [OPTIONS]... [DBNAME [USERNAME]] Options: -d, --dbname DBNAME database name to connect to -h, --host HOSTNAME database server host or socket directory -p, --port PORT database server port (default 5432) -U, --username USERNAME database user name General options: -?, --help show this help and exit Report bugs to <https://github.com/lesovsky/pgcenter/issues>.
复制
[postgres@pg14 ~]$ pgcenter report --help 'pgcenter report' reads statistics from file and prints reports. Usage: pgcenter report [OPTIONS]... Options: -f, --file FILE read stats from file (default: pgcenter.stat.tar) -s, --start TIMESTAMP starting time of the report (format: [YYYY-MM-DD] HH:MM:SS) -e, --end TIMESTAMP ending time of the report (format: [YYYY-MM-DD] HH:MM:SS) -o, --order COLNAME order values by column --desc use descendant order (default) --asc use ascendant order -g, --grep COLNAME:PATTERN filter values in specfied column (format: colname:filtertext) -l, --limit INT print only limited number of rows per sample (default: unlimited) -t, --strlimit INT maximum string size to print (default: 32, 0 disables) Report options: -A, --activity show pg_stat_activity statistics -R, --replication show pg_stat_replication statistics -T, --tables show pg_stat_user_tables statistics -I, --indexes show pg_stat_user_indexes and pg_statio_user_indexes statistics -S, --sizes show statistics about tables sizes -F, --functions show pg_stat_user_functions statistics -W, --wal show pg_stat_wal statistics -D, --databases SELECTOR show pg_stat_database statistics, use additional selector to choose stats: 'g' - general; 's' - sessions -X, --statements SELECTOR show pg_stat_statements statistics, use additional selector to choose stats: 'm' - timings; 'g' - general; 'i' - io; 't' - temp files io; 'l' - local files io; 'w' - wal statistics -P, --progress SELECTOR show pg_stat_progress_* statistics, use additional selector to choose stats: 'v' - vacuum; 'c' - cluster; 'i' - create index; 'a' - analyze; 'b' - basebackup; 'y' - copy -d, --describe show statistics description, combined with one of the report options General options: -?, --help show this help and exit Report bugs to <https://github.com/lesovsky/pgcenter/issues>.
复制
[postgres@pg14 ~]$ pgcenter record --help 'pgcenter record' connects to PostgreSQL and collects stats into local file. Usage: pgcenter record [OPTIONS]... [DBNAME [USERNAME]] Options: -d, --dbname DBNAME database name to connect to -h, --host HOSTNAME database server host or socket directory -p, --port PORT database server port (default 5432) -U, --username USERNAME database user name -i, --interval DURATION statistics recording interval (default: 1s) -c, --count INT number of statistics samples to record -f, --file FILENAME file name where statistics to write to (default: pgcenter.stat.tar) -a, --append append statistics to file (defailt: true) -s, --strlimit INT maximum query length to record (default: 0, no limit) -1, --oneshot append single statistics snapshot and exit (alias for --interval 0 --count 1) General options: -?, --help show this help and exit Report bugs to <https://github.com/lesovsky/pgcenter/issues>. [postgres@pg14 ~]$ pgcenter report --help 'pgcenter report' reads statistics from file and prints reports. Usage: pgcenter report [OPTIONS]... Options: -f, --file FILE read stats from file (default: pgcenter.stat.tar) -s, --start TIMESTAMP starting time of the report (format: [YYYY-MM-DD] HH:MM:SS) -e, --end TIMESTAMP ending time of the report (format: [YYYY-MM-DD] HH:MM:SS) -o, --order COLNAME order values by column --desc use descendant order (default) --asc use ascendant order -g, --grep COLNAME:PATTERN filter values in specfied column (format: colname:filtertext) -l, --limit INT print only limited number of rows per sample (default: unlimited) -t, --strlimit INT maximum string size to print (default: 32, 0 disables) Report options: -A, --activity show pg_stat_activity statistics -R, --replication show pg_stat_replication statistics -T, --tables show pg_stat_user_tables statistics -I, --indexes show pg_stat_user_indexes and pg_statio_user_indexes statistics -S, --sizes show statistics about tables sizes -F, --functions show pg_stat_user_functions statistics -W, --wal show pg_stat_wal statistics -D, --databases SELECTOR show pg_stat_database statistics, use additional selector to choose stats: 'g' - general; 's' - sessions -X, --statements SELECTOR show pg_stat_statements statistics, use additional selector to choose stats: 'm' - timings; 'g' - general; 'i' - io; 't' - temp files io; 'l' - local files io; 'w' - wal statistics -P, --progress SELECTOR show pg_stat_progress_* statistics, use additional selector to choose stats: 'v' - vacuum; 'c' - cluster; 'i' - create index; 'a' - analyze; 'b' - basebackup; 'y' - copy -d, --describe show statistics description, combined with one of the report options General options: -?, --help show this help and exit Report bugs to <https://github.com/lesovsky/pgcenter/issues>.
复制
pgCenter usage
pgCenter’s functionality is splitted among several sub-commands, run specific one to achieve your goals. In most cases, connection setting can be omitted.
-
Run
top
command to connect to Postgres and watching statistics:pgcenter top -h 1.2.3.4 -U postgres production_db
复制 -
Run
profile
command to connect to Postgres and profile backend with PID 12345:pgcenter profile -U postgres -P 12345 production_db
复制 -
Run
profile
command to profile backend with PID 12345 with frequency 50 (every 20ms):pgcenter profile -U postgres -P 12345 -F 50 production_db
复制 -
Run
record
command to connect to Postgres, poll statistics and continuously save to a local file:pgcenter record -f /tmp/stats.tar -U postgres production_db
复制 -
Run
report
command to read the previously written file and build a report:pgcenter report -f /tmp/stats.tar --databases
复制 -
Run
report
command, build activity report with start time 12:30:00 and end time 12:50:00:pgcenter report --activity --start 12:30:00 --end 12:50:00
复制 -
Run
report
command, build tables report order byseq_scan
column and show only 2 tables per single stat snapshot:pgcenter report --tables --order seq_scan --limit 2
复制 -
Run
report
command, build statements report and show statements that haveUPDATE
word inquery
column:pgcenter report --statements m --grep query:UPDATE
复制
Full list of available parameters available in a built-in help for particular command, use --help
parameter.
pgcenter report --help
复制