1 Xata agent简介
DeepSeek发布以来,很多人在想DeepSeek能够数据库管理提供哪些帮助,比如将性能数据或者是日志提供给DeepSeek,DeepSeek可以出具有一定专业水平的分析报告及优化建议。如果再进一步,DeepSeek能不能主动获取数据库的性能数据进行分析呢?
通过AI代理可以主动获取数据库的性能数据并进行分析,今天介绍一款数据库代理,Xata agent,这是一个开源的DB代理,项目托管在GitHub上, Xata团队使用它监控和操作活跃的Postgres数据库。Xata Agent免费开放源码,支持扩展功能。它不会执行任何可能破坏数据库的操作,所有的诊断和建议都基于预设的SQL命令和分析逻辑。支持ChatGPT、DeepSeek和Claude,数据库目前只支持Postgresql,基础平台支持亚马逊的AWS,对AWS的RDS支持比较全面,可以同时采集操作数据库和操作系统的性能数据库,对其它的PG数据库则只能采集数据库的性能数据,分析的就不那么全面了。
2 Xata agent安装
Xata agent推荐以docker 容器的方式运行,安装配置也比较简单,后台需要一个Postgres数据库存储数据,这个Postgres数据库也是以docker容器的方式运行的。
2.1 下载或者克隆Xata agent仓库
可以使用浏览器下载Xata agent的zip文件解压,也可以用cit命令克隆git仓库,命令如下
git clone https://github.com/xataio/agent.git
2.2 更换docker仓库地址
Xata agent安装的过程中需要拉取agent和Postgresql的docker映像,官网的网站速度慢而且不稳定,可以将docker仓库设置为内地的镜像仓库,编辑/etc/docker/目录下的daemon.json文件(如果没有,可以新建一个),加入下面内容后重启docker后台进程
{
"registry-mirrors": [
"https://docker.1ms.run",
"https://docker.xuanyuan.me"
]
}
2.3 创建并运行Xata agent
进入agent仓库目录,编辑一下.env.production文件(这个是隐含文件,用ls -a可以看到),填入代理监听地址和端口、DEEPSEEK_API_KEY或者OPENAI_API_KEY,文件内容如下
# Public URL of the app
PUBLIC_URL=http://192.168.**.****:8080
# LLM API credentials
OPENAI_API_KEY=
DEEPSEEK_API_KEY='sk-27c34d5a12264**********7802715'
ANTHROPIC_API_KEY=
然后运行docker compose up命令
[root@localhost agent]# docker compose up
[+] Running 11/11
✔ xata-agent Pulled 431.3s
✔ f18232174bc9 Pull complete 5.8s
✔ cb2bde55f71f Pull complete 42.5s
✔ 9d0e0719fbe0 Pull complete 42.8s
✔ 6f063dbd7a5d Pull complete 42.9s
✔ 40791d3b9036 Pull complete 44.4s
✔ ea5ee7f5db49 Pull complete 44.5s
✔ 4f4fb700ef54 Pull complete 44.7s
✔ 6d9b59e144b8 Pull complete 44.9s
✔ 1e09065861b6 Pull complete 340.1s
✔ 06a2048e915a Pull complete 421.4s
[+] Running 4/4
✔ Network agent_default Created 0.2s
✔ Volume "agent_agent_postgres_data" Created 0.0s
✔ Container agent-postgres-1 Created 4.5s
✔ Container agent-xata-agent-1 Created 0.1s
Attaching to postgres-1, xata-agent-1
postgres-1 | The files belonging to this database system will be owned by user "postgres".
postgres-1 | This user must also own the server process.
postgres-1 |
postgres-1 | The database cluster will be initialized with locale "en_US.utf8".
postgres-1 | The default database encoding has accordingly been set to "UTF8".
postgres-1 | The default text search configuration will be set to "english".
postgres-1 |
postgres-1 | Data page checksums are disabled.
postgres-1 |
postgres-1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
postgres-1 | creating subdirectories ... ok
postgres-1 | selecting dynamic shared memory implementation ... posix
postgres-1 | selecting default "max_connections" ... 100
postgres-1 | selecting default "shared_buffers" ... 128MB
postgres-1 | selecting default time zone ... Etc/UTC
postgres-1 | creating configuration files ... ok
postgres-1 | running bootstrap script ... ok
postgres-1 | performing post-bootstrap initialization ... ok
xata-agent-1 | ! Corepack is about to download https://registry.npmjs.org/pnpm/-/pnpm-10.5.2.tgz
postgres-1 | syncing data to disk ... ok
postgres-1 |
postgres-1 |
postgres-1 | Success. You can now start the database server using:
postgres-1 |
postgres-1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
postgres-1 |
postgres-1 | initdb: warning: enabling "trust" authentication for local connections
postgres-1 | initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
postgres-1 | waiting for server to start....2025-04-02 07:06:09.364 UTC [48] LOG: starting PostgreSQL 17.4 (Debian 17.4-1.pgdg120+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
postgres-1 | 2025-04-02 07:06:09.373 UTC [48] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres-1 | 2025-04-02 07:06:09.397 UTC [51] LOG: database system was shut down at 2025-04-02 07:06:06 UTC
postgres-1 | 2025-04-02 07:06:09.434 UTC [48] LOG: database system is ready to accept connections
postgres-1 | done
postgres-1 | server started
postgres-1 | CREATE DATABASE
postgres-1 |
postgres-1 |
postgres-1 | /usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sql
postgres-1 | ?column?
postgres-1 | ----------
postgres-1 | (0 rows)
postgres-1 |
postgres-1 |
postgres-1 |
postgres-1 | 2025-04-02 07:06:09.863 UTC [48] LOG: received fast shutdown request
postgres-1 | waiting for server to shut down....2025-04-02 07:06:09.891 UTC [48] LOG: aborting any active transactions
postgres-1 | 2025-04-02 07:06:09.893 UTC [48] LOG: background worker "logical replication launcher" (PID 54) exited with exit code 1
postgres-1 | 2025-04-02 07:06:09.900 UTC [49] LOG: shutting down
postgres-1 | 2025-04-02 07:06:09.911 UTC [49] LOG: checkpoint starting: shutdown immediate
postgres-1 | .2025-04-02 07:06:11.063 UTC [49] LOG: checkpoint complete: wrote 921 buffers (5.6%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.189 s, sync=0.920 s, total=1.163 s; sync files=301, longest=0.034 s, average=0.004 s; distance=4238 kB, estimate=4238 kB; lsn=0/1908970, redo lsn=0/1908970
postgres-1 | 2025-04-02 07:06:11.072 UTC [48] LOG: database system is shut down
postgres-1 | done
postgres-1 | server stopped
postgres-1 |
postgres-1 | PostgreSQL init process complete; ready for start up.
postgres-1 |
postgres-1 | 2025-04-02 07:06:11.173 UTC [1] LOG: starting PostgreSQL 17.4 (Debian 17.4-1.pgdg120+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
postgres-1 | 2025-04-02 07:06:11.174 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
postgres-1 | 2025-04-02 07:06:11.174 UTC [1] LOG: listening on IPv6 address "::", port 5432
postgres-1 | 2025-04-02 07:06:11.202 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres-1 | 2025-04-02 07:06:11.230 UTC [66] LOG: database system was shut down at 2025-04-02 07:06:11 UTC
postgres-1 | 2025-04-02 07:06:11.247 UTC [1] LOG: database system is ready to accept connections
xata-agent-1 | No config path provided, using default 'drizzle.config.ts'
xata-agent-1 | Reading config file '/app/apps/dbagent/drizzle.config.ts'
xata-agent-1 | Using 'pg' driver for database querying
[✓] migrations applied successfully! ▲ Next.js 15.2.2
xata-agent-1 | - Local: http://localhost:8080
xata-agent-1 | - Network: http://172.18.0.3:8080
xata-agent-1 |
xata-agent-1 | ✓ Starting...
xata-agent-1 | Starting scheduler with 60s interval (60000ms)
xata-agent-1 | ✓ Ready in 1201ms
xata-agent安装好了,打开浏览器,输入地址,显示如下界面

3 接入Postgresql数据库
3.1 配置pg数据库接受Xata连接
编辑pg data目录下文件pg_hba.conf,加入一行允许Xata agent访问数据库,例如
host all all 172.18.0.0/16 scram-sha-256
3.2 安装并启用pg_stat_statements
Xata agent的慢SQL分析功能依赖pg_stat_statements扩展,这个扩展在默认的情况下不会安装,这里介绍一下使用rpm安装的步骤
安装PG官方yum源
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
搜索并安装可用contrib版本
pg的扩展不在pg的数据库服务器rpm包内,二是随contri包一起提供,安装PG 官网yum源后,使用下面命令搜索可用的contrib包
[root@localhost yum.repos.d]# yum search contrib
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
======================================================== N/S matched: contrib ========================================================
ant-contrib-javadoc.noarch : Javadoc for ant-contrib
cvs-contrib.noarch : Unsupported contributions collected by CVS developers
jna-contrib.noarch : Contrib for jna
m17n-contrib.noarch : Contributed multilingualization datafiles for m17n-lib
m17n-contrib-extras.noarch : Extra m17n-contrib files
postgresql12-contrib.x86_64 : Contributed source and binaries distributed with PostgreSQL
postgresql13-contrib.x86_64 : Contributed source and binaries distributed with PostgreSQL
postgresql14-contrib.x86_64 : Contributed source and binaries distributed with PostgreSQL
postgresql15-contrib.x86_64 : Contributed source and binaries distributed with PostgreSQL
根据数据库版本安装响应的包,我这里的数据库是postgresql15,就选择postgresql15-contrib.x86_64安装
yum install postgresql15-contrib.x86_64
加载pg_stat_statements包
使用pg_stat_statements扩展需要提前加载相应的共享包,编辑postgresql.conf文件,加入下面的行
shared_preload_libraries = 'pg_stat_statements'
加入后重启数据库使更改生效
创建pg_stat_statements扩展
登陆数据库,运行下面命令创建扩展
CREATE extension pg_stat_statements;
--检查以安装的扩展
postgres=# SELECT * FROM pg_extension;
oid | extname | extowner | extnamespace | extrelocatable | extversion | extconfig | extcondition
-------+--------------------+----------+--------------+----------------+------------+-----------+--------------
14267 | plpgsql | 10 | 11 | f | 1.0 | |
32772 | pg_stat_statements | 10 | 2200 | t | 1.10 | |
(2 rows)
3.3 将数据库接入Xata Agent
点击上图中的Create Project,连接新的Postgresql数据库,在下面的界面输入连接名称和数据库连接串

4 Xata的功能简介

Xata实现的功能包含在它的几个Playbook内,点击Playbook右边的三角形就可以执行相应的Playbook,转到chats页面,主要选择一下使用的大模型和数据库,发送提示词就可以执行分析了。
除了上面的方式外,也可以在chats页面的直接和大模型对话(支持中文对话),进行分析,详见下一章
5 执行慢SQL分析
Current context: prod database
--提示词
分析一下当前数据库的慢SQL
--agent响应
Tool called: getSlowQueries
当前数据库中最慢的SQL是:
查询: UPDATE pgbench_tellers SET tbalance = tbalance + $1 WHERE tid = $2
调用次数: 7086
最大执行时间: 2秒
平均执行时间: 0秒
总执行时间: 831秒
需要进一步分析该查询的执行计划吗?
--提示词
嗯
--agent响应
Tool called: explainQuery
该更新查询的执行计划如下:
Bitmap Heap Scan 在 pgbench_tellers 表上执行,使用 tid = 1 作为条件。
Bitmap Index Scan 使用主键索引 pgbench_tellers_pkey 快速定位符合条件的行。
执行计划显示查询使用了索引,效率较高。但实际执行时间较长(最大2秒),可能原因包括:
表数据量大或索引碎片化。
锁竞争或事务冲突。
系统资源(如CPU、I/O)瓶颈。
需要进一步检查表大小、锁情况或系统资源吗?
--提示词
是的
--agent响应
Tool called: describeTable --调用工具
Tool called: getVacuumStats
Tool called: getInstanceMetric
Tool called: getInstanceMetric
分析结果:
表结构:
pgbench_tellers 表有主键索引 pgbench_tellers_pkey,列包括 tid(主键)、bid、tbalance 和 filler。
Vacuum 状态:
pgbench_tellers 表:
最近手动 Vacuum: 2025-04-08 01:52:21
最近自动 Vacuum: 2025-04-08 01:54:09
死元组: 886
活跃元组: 10
修改次数: 1229
Vacuum 操作频繁,死元组较少,表状态良好。
系统资源:
无法获取 CPU 和 I/O 指标(AWS 凭据未配置)。
建议:
检查是否有锁竞争或事务冲突(如长时间运行的事务)。
确认系统资源(CPU、I/O)是否充足(需配置 AWS 凭据以获取指标)。
如果问题持续,考虑优化查询或调整 Vacuum
6 小结
这款AI代理基本实现了PG管理数据库的功能,但是在大模型的选择、数据库类型和底层平台的支持方面局限较大,如果能进一步扩展,能够使用本地模型就更好了。
另外,这个代理也有一个缺点,在连接大模型时,如果因为某种原因(如api key无效,大模型欠费)连接不上,chat只会等待,不会超时退出。在运行相应函数出错时,也不会退出,只能新开一个chat。




