pgbench 是一个 核心实用程序,用于在 PostgreSQL 上运行基准测试。
pgbench 已在 PostgreSQL 7.0 中添加。
用法
pgbench 能够生成一组样本数据,并对这些数据运行一组默认查询(大致基于 TPC-B);或者,也可以执行用户提供的自定义脚本。
默认数据和查询
如果使用内置的基准测试功能,pgbench 需要执行两次:一次初始化数据,一次运行基准测试查询。
初始化步骤使用 pgbench 执行;将创建四个表(默认情况下,具有相同名称的现有表将被删除)。三个表将预先填充生成的数据;数据量由 -i-s/--scale 选项控制(默认值:1)。下表显示了在不同比例因子下生成的表和插入的行数。
1 |
10 |
100 |
|
pgbench_branches |
1 |
10 |
100 |
pgbench_tellers |
10 |
100 |
1,000 |
pgbench_accounts |
100,000 |
1,000,000 |
10,000,000 |
pgbench_history |
0 |
0 |
0 |
默认情况下,行是作为客户端生成的单个 INSERT 语句插入的。如果 -I/--init-steps 选项包含 G 标志,则数据将使用 generate_series() 创建。
生成数据后,可以第二次执行 pgbench,以多次运行内置的 TPC-B 类事务。每个事务运行以下命令:
\set aid random(1, 100000 * :scale) \set bid random(1, 1 * :scale) \set tid random(1, 10 * :scale) \set delta random(-5000, 5000) BEGIN; UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid; SELECT abalance FROM pgbench_accounts WHERE aid = :aid; UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid; UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid; INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP); END;
变更历史
开发中
- PostgreSQL 18
- PostgreSQL 17
- PostgreSQL 15
- PostgreSQL 14
- PostgreSQL 13
- PostgreSQL 12
- PostgreSQL 11
- 重大脚本语言改进(提交 bc7fa0c1)
- 添加了
pow()/power()函数(提交 7a727c18) - 添加了
\if支持(提交 f67b113a) - 变量名中允许使用非 ASCII 字符(提交 9d36a386)
- 添加了哈希函数
hash()、hash_murmur2()和hash_fnv1a()(提交 e51a0484) - 添加了
random_zipfian()函数(提交 1fcd0ade) - 添加了选项
--init-steps(提交 591c504f) - 添加了选项
--random-seed(提交 64f85894) - 使用
--latency-limit和--rate生成的统计数据的准确性得到提高(提交 c23bb6ba 和 16827d44)
- PostgreSQL 9.5
- PostgreSQL 9.4
- PostgreSQL 9.3
- PostgreSQL 9.2
- PostgreSQL 8.4
- PostgreSQL 8.3
- 移除
-P选项 (提交 4192f2d8)
- 移除
- PostgreSQL 7.0
- 作为 contrib 模块添加(提交 a765db40)
示例
默认测试
pgbench 提供的默认测试需要将一套示例表和数据安装到目标数据库中;这通过以下方式完成:
pgbench -i [other-options]dbname
其中 other-options 应包括 psql 和其他 libpq 客户端使用的相同 连接参数。
再次执行 pgbench 而不带 -i 选项将执行一个基本测试,例如:
postgres:~$ pgbench -U postgres testdb starting vacuum...end. transaction type: TPC-B (sort of) scaling factor: 1 query mode: simple number of clients: 1 number of threads: 1 number of transactions per client: 10 number of transactions actually processed: 10/10 tps = 108.049703 (including connections establishing) tps = 110.750557 (excluding connections establishing)
默认选项未提供特别有用的输出;用于调优 pgbench 最有用的选项是:
-c(客户端数量)-t(事务数量)-T(时间限制)
参考资料
- PostgreSQL 文档: pgbench
有用链接
- PostgreSQL 社区维基: Pgbench
