Connection Pool 관리

Connection Pool 생성

생성하고자 하는 connection pool 은 openproxy.toml 파일에 기재해야 한다.

simple_db connection pool 생성

connection pool 생성 전에 사용할 PostgreSQL database 를 먼저 생성한다.

해당 예제에서는 some_db database 를 생성하였다.

생성할 Connection Pool : simple_db

  • openproxy.toml 에 [pools.simple_db] 섹션 작성한다.

    [pools.simple_db]
    pool_mode = "session"
    query_parser_enabled = true
    query_parser_read_write_splitting = true
    primary_reads_enabled = true
    sharding_function = "pg_bigint_hash"

Connection Pool 사용자 정보 설정

  • openproxy.toml 에 [pools.simple_db.users.0] 섹션 작성한다.

    [pools.simple_db.users.0]
    username = "simple_user"
    password = "simple_user"
    pool_size = 5
    statement_timeout = 30000

접속할 cluster 주소 및 database 기재

  • openproxy.toml 에 [pools.simple_db.shard.0] 섹션 작성한다.

    [pools.simple_db.shards.0]
    servers = [
      [ "opensql1", 5432, "Auto", ],
      [ "opensql2", 5432, "Auto", ],
      [ "opensql3", 5432, "Auto", ],
    ]
    database = "some_db"
    use_patroni = true

simple_db connection pool 생성 설정 파일 전체 예시

[pools.simple_db]
pool_mode = "session"
default_role = "primary"
query_parser_enabled = true
query_parser_read_write_splitting = true
primary_reads_enabled = true
sharding_function = "pg_bigint_hash"
prepared_statements_cache_size = 500

[pools.simple_db.users.0]
username = "simple_user"
password = "simple_user"
pool_size = 5
statement_timeout = 30000

[pools.simple_db.shards.0]
servers = [
  [ "opensql1", 5432, "Auto", ],
  [ "opensql2", 5432, "Auto", ],
  [ "opensql3", 5432, "Auto", ],
]
database = "some_db"
use_patroni = true


OpenProxy 실행

생성할 connection pool 을 설정한 openproxy.toml 파일로 OpenProxy 를 수행한다.


Connection Pool 확인

psql -h 127.0.0.1 -p 6432 -d openproxy -U postgres 명령어 사용하여 생성된 Connection Pool을 확인한다.

openproxy=> show pools;
  database  |     user      |  pool_mode  | cl_idle | cl_active | cl_waiting | cl_cancel_req | sv_active | sv_idle | sv_used | sv_tested | sv_login | maxwait | maxwait_us 
------------+---------------+-------------+---------+-----------+------------+---------------+-----------+---------+---------+-----------+----------+---------+------------
 simple_db  | simple_user   | session     |       0 |         0 |          0 |             0 |         0 |       0 |       0 |         0 |        0 |       0 |          0
(1 rows)

openproxy=> show databases;
             name             |    host     | port | database |  force_user   | pool_size | min_pool_size | reserve_pool |  pool_mode  | max_connections | current_connections | paused | disabled 
------------------------------+-------------+------+----------+---------------+-----------+---------------+--------------+-------------+-----------------+---------------------+--------+----------
 simple_db_shard_0_replica_0  | 178.176.0.4 | 5432 | some_db  | simple_user   |         5 |             0 |            0 | session     |               5 |                   0 |      0 |        0
 simple_db_shard_0_replica_1  | 178.176.0.2 | 5432 | some_db  | simple_user   |         5 |             0 |            0 | session     |               5 |                   0 |      0 |        0
 simple_db_shard_0_primary    | 178.176.0.3 | 5432 | some_db  | simple_user   |         5 |             0 |            0 | session     |               5 |                   0 |      0 |        0
 (3 rows)

Last updated