用于监控PostgreSQL服务器活动的命令行工具。
项目描述
用于监控PostgreSQL服务器活动的命令行工具。
安装
从分发包中安装
安装pg_activity最简单的方法是通过您的Linux发行版的包管理器,如果它附带了一个包。例如,在基于Debian的发行版(例如Debian、Ubuntu、Mint...)
$ sudo apt install pg-activity
(在Debian bullseye,当前稳定版本,有一个回滚版本可用:apt install pg-activity/bullseye-backports
)。
PostgreSQL全球开发组(PGDG)还提供了基于RPM(https://yum.postgresql.org/)和Debian的发行版(https://wiki.postgresql.ac.cn/wiki/Apt)的软件包。
注意:发行版软件包可能不会与最新的pg_activity版本同步。在提交错误报告之前
- 请检查软件包版本,将其与我们的最新版本进行比较,然后查看变更日志以确定错误是否已修复;
- 如果问题是关于软件包的,例如缺少依赖项,首先联系软件包维护人员(或PGDG)。
从PyPI
pg_activity可以使用pip在Python 3.8或更高版本上安装,并与psycopg一起使用
$ python3 -m pip install "pg_activity[psycopg]"
或者,可以使用pipx在隔离环境中安装和运行pg_activity
$ pipx install "pg_activity[psycopg]"
如果您的$PATH
中还没有它,完整的路径是
$ ~/.local/bin/pg_activity
从源码,使用git
这仅适用于测试开发版本。首先,克隆存储库
$ git clone https://github.com/dalibo/pg_activity.git
如有必要,更改分支。然后创建一个专用环境,并使用psycopg数据库驱动程序安装pg_activity
$ cd pg_activity
$ python3 -m venv .venv
$ . .venv/bin/activate
(.venv) $ pip install ".[psycopg]"
(.venv) $ pg_activity
要退出此环境并销毁它
$ deactivate
$ rm -r .venv
用法
pg_activity
可以在本地或远程工作。在本地执行上下文中,为了获得显示系统信息所需的权利,运行pg_activity
的系统用户必须与运行postgresql服务器(默认为postgres
)的用户相同,或者具有更多权利,例如root
。用于连接数据库的PostgreSQL用户必须是超级用户,以便获取尽可能多的数据。否则,pg_activity
将回退到降级模式,其中某些数据(如系统信息或临时文件数据)不会显示。
示例
sudo -u postgres pg_activity -U postgres
选项
pg_activity [options] [connection string]
Configuration:
--profile PROFILE Configuration profile matching a PROFILE.conf file in
${XDG_CONFIG_HOME:~/.config}/pg_activity/ or
/etc/pg_activity/.
Options:
--blocksize BLOCKSIZE
Filesystem blocksize (default: 4096).
--rds Enable support for AWS RDS (implies --no-tempfiles and filters out the rdsadmin database from space calculation).
--output FILEPATH Store running queries as CSV.
--no-db-size Skip total size of DB.
--no-tempfiles Skip tempfile count and size.
--no-walreceiver Skip walreceiver checks.
-w, --wrap-query Wrap query column instead of truncating.
--duration-mode DURATION_MODE
Duration mode. Values: 1-QUERY(default), 2-TRANSACTION, 3-BACKEND.
--min-duration SECONDS
Don't display queries with smaller than specified duration (in seconds).
--filter FIELD:REGEX Filter activities with a (case insensitive) regular expression applied on selected fields. Known fields are: dbname.
--debug-file DEBUG_FILE
Enable debug and write it to DEBUG_FILE.
--version show program's version number and exit.
--help Show this help message and exit.
Connection Options:
connection string A valid connection string to the database, e.g.: 'host=HOSTNAME port=PORT user=USER dbname=DBNAME'.
-h HOSTNAME, --host HOSTNAME
Database server host or socket directory.
-p PORT, --port PORT Database server port.
-U USERNAME, --username USERNAME
Database user name.
-d DBNAME, --dbname DBNAME
Database name to connect to.
Process table display options:
These options may be used hide some columns from the processes table.
--no-pid Disable PID.
--no-database Disable DATABASE.
--no-user Disable USER.
--no-client Disable CLIENT.
--no-cpu Disable CPU%.
--no-mem Disable MEM%.
--no-read Disable READ/s.
--no-write Disable WRITE/s.
--no-time Disable TIME+.
--no-wait Disable W.
--no-app-name Disable App.
Header display options:
--no-inst-info Display instance information.
--no-sys-info Display system information.
--no-proc-info Display workers process information.
Other display options:
--hide-queries-in-logs
Disable log_min_duration_statements and log_min_duration_sample for pg_activity.
--refresh REFRESH Refresh rate. Values: 0.5, 1, 2, 3, 4, 5 (default: 2).
配置
pg_activity
可以通过配置文件进行配置,格式为INI,从${XDG_CONFIG_HOME:~/.config}/pg_activity.conf
或/etc/pg_activity.conf
读取,按此顺序。命令行选项可以覆盖配置文件设置。这用于控制进程表中的列如何渲染,或者哪些标题项应该显示,例如
[header]
show_instance = yes
show_system = yes
show_workers = no
[client]
hidden = yes
[database]
width = 9
或者,用户可以在${XDG_CONFIG_HOME:~/.config}/pg_activity/<my-profile>.conf
或/etc/pg_activity/<my-profile>.conf
中定义配置配置文件;然后可以通过--profile <my-profile>
命令行选项使用它们。这些文件的格式与主配置文件相同。
pg_activity
附带了一些内置配置文件
narrow
,提供了一种窄用户界面,其中进程表中的大多数非必要列都被隐藏,wide
,提供了一种宽用户界面(narrow
的逆),以及minimal
,提供了一种更简洁的用户界面,其中隐藏了标题信息
注意
pg_activity
报告的SQL查询文本长度依赖于PostgreSQL参数track_activity_query_size
。默认值为1024
(以字节为单位)。如果您的SQL查询文本看起来被截断,您应增加track_activity_query_size
。
交互式命令
键 | 操作 |
---|---|
r |
按READ/s降序排序 |
w |
按WRITE/s降序排序 |
c |
按CPU%降序排序 |
m |
按MEM%降序排序 |
t |
按TIME+降序排序 |
T |
更改持续时间模式:查询、事务、后端 |
空格 |
暂停/开启 |
v |
更改查询显示模式:完整、缩进、截断 |
上/下箭头 |
滚动进程列表 |
向上/向下箭头 |
滚动进程列表 |
q |
退出 |
+ |
增加刷新时间。最大值:5秒 |
- |
减少刷新时间。最小值:0.5秒 |
F1/1 |
正在运行的查询列表 |
F2/2 |
等待的查询列表 |
F3/3 |
阻塞的查询列表 |
h |
帮助页面 |
R |
刷新 |
D |
刷新数据库大小(包括应用了 --no-db-size 选项的情况) |
s |
在标题中显示系统信息 |
i |
在标题中显示一般实例信息 |
o |
在标题中显示工作信息 |
导航模式
键 | 操作 |
---|---|
UP /k |
移动光标向上 |
DOWN /j |
移动光标向下 |
K |
终止当前后端/标记后的后端 |
C |
取消当前后端/标记后的后端 |
空格 |
标记或取消标记进程 |
q |
退出 |
其他 |
返回活动 |
常见问题解答
我看不到我的查询,只有TPS显示
pg_activity
使用用户定义的刷新时间(介于0.5到5秒之间)扫描 pg_stat_activity
视图。它可以通过界面上的 +
和 -
键进行修改。两次扫描之间的任何查询都不会显示。
此外,pg_activity
使用不同的查询从 pg_settings
获取
- 设置
- 使用
version()
获取版本信息 - 查询和连接数来自
pg_stat_activity
- 锁来自
pg_locks
- 使用
pg_stat_get_db_xact_commit()
和pg_stat_get_db_xact_rollback()
从pg_database
获取 tps - 等等(例如:
pg_cancel_backend()
和pg_terminate_backend()
)
这些查询在查询标签中看不到,因为所有来自 pg_activity
后端的查询都视为噪音且不显示。另一方面,用于获取 pg_activity
报告信息的交易仍然由 postgres 计入 pg_stat_get_db_xact_commit()
和 pg_stat_get_db_xact_rollback()
。因此,即使数据库没有活动,或者屏幕上没有显示活动,pg_activity
也会显示非零的TPS。
我如何指定一个密码进行认证?
pg_activity 使用 libpq 访问 PostgreSQL,因此所有传统方法都可用。
您可以将数据库连接的密码通过密码文件传递。也可以通过 PostgreSQL 的环境变量(PGPASSFILE 或 PGPASSWORD)或通过连接字符串参数提供信息。
由于安全性被推迟到操作系统,因此首选密码文件(更安全)。请尽量避免在连接字符串中使用密码。
黑客攻击
为了在 pg_activity 源代码上工作,特别是运行测试套件,将创建一个临时的 PostgreSQL 数据库集群;因此,需要 PostgreSQL 服务器二进制文件(例如 initdb
、pg_ctl
)。例如,在 Debian 系统上,这意味着只需安装 postgresql
软件包即可。
要设置开发环境,请获取源代码库
$ git clone https://github.com/dalibo/pg_activity
$ cd pg_activity
然后创建一个 虚拟环境,激活它,并安装项目及其开发依赖项
$ python3 -m venv .venv
$ .venv/bin/activate
(.venv) $ pip install -e ".[dev]"
源代码使用 black 和 isort 格式化,并使用 mypy 进行类型检查(所有这些都包含在开发环境中)。请确保在提交更改之前遵守这一点,例如通过配置您的编辑器。或者,您可以通过安装 pre-commit 钩子来自动检查这些。
(.venv) $ pre-commit install
要运行测试套件,只需调用
(.venv) $ pytest
================================ test session starts =================================
platform linux -- Python 3.11.2, pytest-7.3.1, pluggy-1.0.0
psycopg: 3.1.8
configfile: pytest.ini
plugins: cov-4.0.0, accept-0.1.9, postgresql-4.1.1
collected 70 items
pgactivity/activities.py .. [ 2%]
pgactivity/config.py .. [ 5%]
pgactivity/data.py .. [ 8%]
pgactivity/handlers.py ..... [ 15%]
pgactivity/keys.py . [ 17%]
pgactivity/types.py .............. [ 37%]
pgactivity/utils.py ......... [ 50%]
pgactivity/views.py ..... [ 57%]
tests/test_activities.py ... [ 61%]
tests/test_config.py .. [ 64%]
tests/test_data.py ................ [ 87%]
tests/test_scroll.txt . [ 88%]
tests/test_types.py . [ 90%]
tests/test_ui.txt . [ 91%]
tests/test_views.py .... [ 97%]
tests/test_views.txt . [ 98%]
tests/test_widgets.txt . [100%]
================================ 70 passed in 11.89s =================================
变更日志
查看 CHANGELOG.md。
项目详细信息
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。