PostgreSQL

[PostgreSQL] pgbench 를 통한 파라미터 튜닝 성능 개선 방법

리거니 2023. 3. 15. 15:05
pgbench 란 
 

PostgreSQL이 기본적으로 제공하는 Benchmark Tool, 일정 패턴의 SQL 구문들을 반복 수행하여
이를 통해 얻어지는 초 당 Transaction 횟수로 성능을 평가합니다.
 
  • 스크립트 파일 수정을 통해 트랜잭션의 커스터마이징이 가능합니다.

 

  • 테스트 환경 
  • OS : CentOS Linux 7.9
  • Memory : 2.4G
  • CPU : 2 Core
  • PostgreSQL Versioin : 14.5
  • postgres.conf : 기본값
 

 
① test 할 임시 데이터베이스 생성
postgres =# create database [DB Name];
CREATE DATABASE
 

 

② 생성한 데이터베이스의 table 및 row 수 설정 후 생성

[postgres@localhost bin]$ pgbench -U [User Name] -i -s 10 [DB Name];

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)...

1000000 of 1000000 tuples (100%) done (elapsed 1.08 s, remaining 0.00 s)

vacuuming...

creating primary keys...

done in 1.84 s (drop tables 0.00 s, create tables 0.02 s, client-side generate 1.17 s, vacuum 0.34 s, primary keys 0.31 s).

 
  • -i = create table option
  • -s [N] = N의 배수만큼 tuple 생성 ( 기본 100,000 개 )
 
③ 생성된 table 및 크기 확인
pgbench_test=# \dt+
                                         List of relations
 Schema |       Name       | Type  |  Owner   | Persistence | Access method |  Size   | Description
--------+------------------+-------+----------+-------------+---------------+---------+-------------
 public | pgbench_accounts | table | experdba | permanent   | heap          | 128 MB  |
 public | pgbench_branches | table | experdba | permanent   | heap          | 40 kB   |
 public | pgbench_history  | table | experdba | permanent   | heap          | 0 bytes |
 public | pgbench_tellers  | table | experdba | permanent   | heap          | 40 kB   |
(4 rows)
 
 
④ 데이터 건수(row) 확인
pgbench_test=# select count(*) from pgbench_accounts ;
  count
---------
 1000000
(1 row)
 
  • 약 100만 row Data 를 가지고 있는 128MB 크기에 대한 테이블을 대상으로 테스트 진행함. 

[파라미터 튜닝 ]                                                                                      
 
max_connections = 300                                                                                    
shared_buffers = 512MB                                                                                                                    
wal_buffers = 16MB                                                                                         

 

maintenance_work_mem = 128MB                                                                  
         ...                                                                                                                                     ...
 
[파라미터 튜닝 ]

max_connections = 50

shared_buffers = 1024MB     

 wal_buffers = 32MB

maintenance_work_mem = 256MB

 

⑤ option(pgbench --help)를 통한 파라미터 설정 전/후 성능 개선 test 

[postgres@localhost data]$ pgbench -c 32 -j 8 -t 5000 [DB Name]              ( 설정 전 )           

pgbench (14.5)                                                                                             

starting vacuum...end.                                                                                     

transaction type: <builtin: TPC-B (sort of)>                                                                           

scaling factor: 10                                                                                                        

query mode: simple                                                                                                     .

number of clients: 32                                                                                                    .

number of threads: 8                                                                                                    .

number of transactions per client: 5000

number of transactions actually processed: 160000/160000

latency average = 19.950 ms                                                                          

initial connection time = 42.443 ms                                                                  

tps = 1603.977238 (without initial connection time)                                              

 
[postgres@localhost data]$ pgbench -c 32 -j 8 -t 5000 [DB Name]         ( 설정 후 )
 
latency average = 14.243 ms

 initial connection time = 36.732 ms

 tps = 2246.693142 (without initial ...)

 
  • -c = DB에 접속하는 가상 Client 수
  • -j = 동작 시킬 가상 thread 수 
  • -t = 가상 transaction 수 

 


테스트 측정 결과 및 파라미터 성능 개선 확인

latency average(대기 시간 평균) | 19초 -> 14초
initial connection time(초기 연결 시간) | 42초 -> 36초
tps(초 당 트랜잭션 수) | 1603 -> 2246
 

성능 개선이 미미하지만, 실제 운영DB과 유사한 환경에서 test 진행 시 개선 폭이 클 것으로 예상

다른 option ( --foreign-key , --index-tablespace ... ) 을 사용해 실제 DB 환경과 유사하게 test 가능

기본 내장된 벤치마크 툴이기때문에 사용법이 간편하고 빠르게 test 가능

다만, DB와 연동하는 Application들 의 특성 등은 고려하지 않는 단점이 있습니다.

※ 참고 사이트