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

相关推荐
一屉大大大花卷19 分钟前
初识Neo4j之入门介绍(一)
数据库·neo4j
周胡杰1 小时前
鸿蒙arkts使用关系型数据库,使用DB Browser for SQLite连接和查看数据库数据?使用TaskPool进行频繁数据库操作
前端·数据库·华为·harmonyos·鸿蒙·鸿蒙系统
wkj0011 小时前
navicate如何设置数据库引擎
数据库·mysql
赵渝强老师1 小时前
【赵渝强老师】Oracle RMAN的目录数据库
数据库·oracle
暖暖木头1 小时前
Oracle注释详解
数据库·oracle
御控工业物联网1 小时前
御控网关如何实现MQTT、MODBUS、OPCUA、SQL、HTTP之间协议转换
数据库·sql·http
GJCTYU3 小时前
spring中@Transactional注解和事务的实战理解附代码
数据库·spring boot·后端·spring·oracle·mybatis
MicroTech20253 小时前
微算法科技(NASDAQ: MLGO)探索Grover量子搜索算法,利用量子叠加和干涉原理,实现在无序数据库中快速定位目标信息的效果。
数据库·科技·算法
Code季风3 小时前
SQL关键字快速入门:CASE 实现条件逻辑
javascript·数据库·sql
weixin_478689763 小时前
操作系统【2】【内存管理】【虚拟内存】【参考小林code】
数据库·nosql