如何测试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 引擎的一个版本的性能是优于还是劣于另一个版本。

相关推荐
小宇成长录4 分钟前
Mysql:数据库和表增删查改基本语句
数据库·mysql·数据库备份
团儿.1 小时前
解锁MySQL高可用新境界:深入探索MHA架构的无限魅力与实战部署
数据库·mysql·架构·mysql之mha架构
程序猿小D1 小时前
第二百六十七节 JPA教程 - JPA查询AND条件示例
java·开发语言·前端·数据库·windows·python·jpa
权^2 小时前
MySQL--聚合查询、联合查询、子查询、合并查询(上万字超详解!!!)
大数据·数据库·学习·mysql
OLDERHARD2 小时前
Java - MyBatis(上)
java·oracle·mybatis
Code成立2 小时前
1、深入理解Redis线程模型
数据库·redis·bootstrap
缘友一世4 小时前
macos安装mongodb
数据库·mongodb·macos
万事大吉CC6 小时前
mysql单表查询·3
数据库·mysql
bin91536 小时前
【EXCEL数据处理】000010 案列 EXCEL文本型和常规型转换。使用的软件是微软的Excel操作的。处理数据的目的是让数据更直观的显示出来,方便查看。
大数据·数据库·信息可视化·数据挖掘·数据分析·excel·数据可视化
Miqiuha7 小时前
lock_guard和unique_lock学习总结
java·数据库·学习