如何测试PostgreSQL数据库的性能

在这篇博客里,我将展示如何测量 PostgreSQL 数据库服务器的性能,并指导你如何运行一个基准测试。

基准测试旨在评估和比较不同配置、拓扑结构、系统和组件的性能。为此,我将使用 pgbench 工具。

你可能会问,为什么要使用一个单独的工具来测量 PostgreSQL 数据库的性能呢?毕竟,我们通常有一个特定的应用程序或系统访问数据库并操作数据。通过以不同模式运行现有应用程序并模拟不同的负载场景,我们可以检查数据库的性能。

然而,这种方法并不总是有效,因为应用程序本身可能有架构或系统限制。因此,生成足够的负载并客观评估数据库的性能可能非常困难和具有挑战性。当我们谈论测量数据库性能时,最具挑战性的事情是生成足够的负载。

pgbench 是一个针对 PostgreSQL 数据库的基准测试工具。它允许模拟多个客户端在 PostgreSQL 数据库上执行事务的工作负载。pgbench 实用程序在不同场景下测量数据库的性能。

假设我们已经安装了 PostgreSQL 数据库服务器:

bash 复制代码
dmi@dmi-VirtualBox:~$ psql -h 127.0.0.1 -p 5444 -U postgres -d postgres  
Password for user postgres:  
psql (15.1 (Ubuntu 15.1-1.pgdg22.04+1))  
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)  
Type "help" for help.  
  
postgres=#

为了运行基准测试,我将创建一个新数据库。

bash 复制代码
create database my_benchmark_test_db;

通过运行 \list 命令,我可以验证数据库已成功创建:

bash 复制代码
postgres=# \l  
                                                  List of databases  
         Name         |  Owner   | Encoding | Collate | Ctype | ICU Locale | Locale Provider |   Access privileges  
----------------------+----------+----------+---------+-------+------------+-----------------+-----------------------  
 my_benchmark_test_db | postgres | UTF8     | en_IL   | en_IL |            | libc            |  
 postgres             | postgres | UTF8     | en_IL   | en_IL |            | libc            |  
 template0            | postgres | UTF8     | en_IL   | en_IL |            | libc            | =c/postgres          +  
                      |          |          |         |       |            |                 | postgres=CTc/postgres  
 template1            | postgres | UTF8     | en_IL   | en_IL |            | libc            | =c/postgres          +  
                      |          |          |         |       |            |                 | postgres=CTc/postgres  
(4 rows)  
  
postgres=#

为了让 pgbench 与新创建的数据库 my_benchmark_test_db 一起工作,运行以下命令:

bash 复制代码
pgbench -i -s 50 my_benchmark_test_db -h <db_hostname> -p <db_port> -U <db_user>

例如:

bash 复制代码
dmi@dmi-VirtualBox:~$ pgbench -i -s 50 my_benchmark_test_db -h 127.0.0.1 -p 5444 -U postgres  
Password:  
dropping old tables...  
NOTICE:  table "pgbench_accounts" does not exist, skipping  
NOTICE:  table "pgbench_branches" does not exist, skipping  
NOTICE:  table "pgbench_history" does not exist, skipping  
NOTICE:  table "pgbench_tellers" does not exist, skipping  
creating tables...  
generating data (client-side)...  
5000000 of 5000000 tuples (100%) done (elapsed 10.19 s, remaining 0.00 s)  
vacuuming...  
creating primary keys...  
done in 30.29 s (drop tables 0.05 s, create tables 0.04 s, client-side generate 10.64 s, vacuum 4.75 s, primary keys 14.81 s).  
dmi@dmi-VirtualBox:~$

-i(初始化)选项告诉 pgbench 初始化指定的数据库。

连接到 PostgreSQL 数据库 my_benchmark_test_db,我们可以看到 pgbench 创建了四个表:pgbench_accounts、pgbench_branches、pgbench_history、pgbench_tellers

bash 复制代码
dmi@dmi-VirtualBox:~$ psql -h 127.0.0.1 -p 5444 -U postgres -d my_benchmark_test_db  
Password for user postgres:  
psql (15.1 (Ubuntu 15.1-1.pgdg22.04+1))  
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)  
Type "help" for help.  
  
my_benchmark_test_db=# \d  
              List of relations  
 Schema |       Name       | Type  |  Owner  
--------+------------------+-------+----------  
 public | pgbench_accounts | table | postgres  
 public | pgbench_branches | table | postgres  
 public | pgbench_history  | table | postgres  
 public | pgbench_tellers  | table | postgres  
(4 rows)  
my_benchmark_test_db=#

让我们检查每个表中的行数:

bash 复制代码
my_benchmark_test_db=# select count(1) from pgbench_accounts;  
  count  
---------  
 5000000  
(1 row)  
  
my_benchmark_test_db=# select count(1) from pgbench_branches;  
 count  
-------  
    50  
(1 row)  
my_benchmark_test_db=# select count(1) from pgbench_history;  
 count  
-------  
     0  
(1 row)  
my_benchmark_test_db=# select count(1) from pgbench_tellers;  
 count  
-------  
   500  
(1 row)  
my_benchmark_test_db=#

数据库 my_benchmark_test_db 现在已经准备好用于测量我们的 PostgreSQL 数据库服务器的性能了。

当我们想要确定一个系统的性能时,我们通常会将其指标与某个基准进行比较。让我们运行 pgbench 实用程序并设置一个基准。

为此,我们使用以下参数运行 pgbench:

bash 复制代码
pgbench -c <要连接的客户端数量> -j <工作进程的数量> -t <要执行的事务数量> <样本数据库名称>

例如:

bash 复制代码
pgbench -c 10 -j 2 -t 1000 my_benchmark_test_db -h 127.0.0.1 -p 5444 -U postgres  
  
dmi@dmi-VirtualBox:~$ pgbench -c 10 -j 2 -t 1000 my_benchmark_test_db -h 127.0.0.1 -p 5444 -U postgres  
Password:  
pgbench (15.1 (Ubuntu 15.1-1.pgdg22.04+1))  
starting vacuum...end.  
transaction type: <builtin: TPC-B (sort of)>  
scaling factor: 50  
query mode: simple  
number of clients: 10  
number of threads: 2  
maximum number of tries: 1  
number of transactions per client: 1000  
number of transactions actually processed: 10000/10000  
number of failed transactions: 0 (0.000%)  
latency average = 75.438 ms  
initial connection time = 160.700 ms  
tps = 132.559344 (without initial connection time)  
dmi@dmi-VirtualBox:~$

输出信息有很多,其中最有趣的是:

复制代码
latency average = 75.438 ms  
initial connection time = 160.700 ms  
tps = 132.559344 (without initial connection time)

现在,让我们增加 PostgreSQL DB 服务器可用于缓存的内存量。

目的是将表和索引的内容存储在内存中。

为此,我们将更改 PostgreSQL 参数 shared_buffers。

当前设置为:

复制代码
my_benchmark_test_db=# show shared_buffers;  
 shared_buffers  
----------------  
 128MB  
(1 row)  
  
my_benchmark_test_db=#

让我们将其更改为 2GB:

bash 复制代码
sudo vi /etc/postgresql/15/main/postgresql.conf  
  
...  
#------------------------------------------------------------------------------  
# RESOURCE USAGE (except WAL)  
#------------------------------------------------------------------------------  
# - Memory -  
shared_buffers = 1GB                    # min 128kB  
                                        # (change requires restart)  
...

更改需要重启 PostgreSQL 服务:

bash 复制代码
sudo systemctl restart postgresql

检查更改后的 PostgreSQL 参数 shared_buffers:

bash 复制代码
dmi@dmi-VirtualBox:~$ psql -h 127.0.0.1 -p 5444 -U postgres -d my_benchmark_test_db  
Password for user postgres:  
psql (15.1 (Ubuntu 15.1-1.pgdg22.04+1))  
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)  
Type "help" for help.  
  
my_benchmark_test_db=# show shared_buffers;  
 shared_buffers  
----------------  
 1GB  
(1 row)  
my_benchmark_test_db=#

在增加 shared_buffers PostgreSQL 参数后,重新运行 pgbench 实用程序:

bash 复制代码
pgbench -c 10 -j 2 -t 1000 my_benchmark_test_db -h 127.0.0.1 -p 5444 -U postgres  
  
dmi@dmi-VirtualBox:~$ pgbench -c 10 -j 2 -t 1000 my_benchmark_test_db -h 127.0.0.1 -p 5444 -U postgres  
Password:  
pgbench (15.1 (Ubuntu 15.1-1.pgdg22.04+1))  
starting vacuum...end.  
transaction type: <builtin: TPC-B (sort of)>  
scaling factor: 50  
query mode: simple  
number of clients: 10  
number of threads: 2  
maximum number of tries: 1  
number of transactions per client: 1000  
number of transactions actually processed: 10000/10000  
number of failed transactions: 0 (0.000%)  
latency average = 47.632 ms  
initial connection time = 148.379 ms  
tps = 209.944478 (without initial connection time)  
dmi@dmi-VirtualBox:~$

latency average = 47.632 ms

initial connection time = 148.379 ms

tps = 209.944478 (without initial connection time)

在基准测试中,我们达到了每秒 132 次事务的速率。在这次运行中,增加 shared_buffers 参数后,我们达到了每秒 209 次事务,增加了约 58%。

最佳实践建议将 shared_buffers 值设置为系统内存的四分之一(1/4)。

通过使用 pgbench 实用程序测量数据库性能,我们可以继续进行。例如,我们可以比较不同版本的 PostgreSQL 引擎的性能。这样,我们可以确定 PostgreSQL 引擎的一个版本的性能是优于还是劣于另一个版本。

相关推荐
TDengine (老段)3 分钟前
TDengine 时间函数 TODAY() 用户手册
大数据·数据库·物联网·oracle·时序数据库·tdengine·涛思数据
码界奇点12 分钟前
KingbaseES一体化架构与多层防护体系如何保障企业级数据库的持续稳定与弹性扩展
数据库·架构·可用性测试
悟乙己34 分钟前
数据科学家如何更好地展示自己的能力
大数据·数据库·数据科学家
皆过客,揽星河1 小时前
mysql进阶语法(视图)
数据库·sql·mysql·mysql基础语法·mysql进阶语法·视图创建修改删除
tuokuac2 小时前
Redis 的相关文件作用
数据库·redis·缓存
鹧鸪云光伏与储能软件开发3 小时前
投资储能项目能赚多少钱?小程序帮你测算
运维·数据库·小程序·光伏·光伏设计软件·光伏设计
2301_779503764 小时前
MySQL主从同步--主从复制进阶
数据库·mysql
beijingliushao4 小时前
58-正则表达式
数据库·python·mysql·正则表达式
诗句藏于尽头5 小时前
DJANGO后端服务启动报错及解决
数据库·笔记·django
手握风云-5 小时前
MySQL数据库精研之旅第十五期:索引的 “潜规则”(下)
数据库