> For the complete documentation index, see [llms.txt](https://docs.tibero.com/tmaxopensql/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tibero.com/tmaxopensql/administration/openproxy/connection-pool.md).

# 커넥션 풀(Connection Pool) 관리

## 개요

OpenProxy의 Connection Pooling 기능과 `openproxy.toml` 설정파일을 변경하여 구성하는 방법에 대하여 기술합니다.

### Connection Pool 정의

Connection Pool 을 정의하여 접근할 PostgreSQL 데이터베이스 및 사용자 설정을 관리합니다.

#### **Connection Pool 기본 설정**

* openproxy.toml 에 \[pools.simple\_db] 섹션 작성합니다.

  ```toml
  [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"
  ```
* 지정되지 않은 DB 이름 - 사용자 이름 쌍에 대한 연결을 처리할 Global Default 풀을 `[general.default_pool]` 에 정의할 수 있습니다.

  ```toml
  [general.default_pool]
  pool_mode = "transaction"
  query_parser_enabled = true
  query_parser_read_write_splitting = true
  primary_reads_enabled = true
  auth_query = "SELECT usename, passwd FROM pg_shadow WHERE usename = '$1'"
  auth_query_user = "myuser"
  auth_query_password = "mypassword"
  ```
* `SCRAM-SHA-256` 인증 방식을 `[general]` 설정 혹은 Pool 설정에 사용하면서 `auth_query` 를 함께 사용하는 경우 OpenProxy -> PostgreSQL 연결은 서버의 `pg_hba.conf` 설정에서 반드시 `trust` 로 설정되어야 합니다. `auth_query` 를 통해 PostgreSQL 서버에서 가져온 사용자 비밀번호 해시는 OpenProxy 에 접근하고자 하는 클라이언트에 대한 인증에는 활용할 수 있으나 PostgreSQL 에 접속할 때 사용자 패스워드로 활용할 수 없기 때문입니다.

  ```yaml
  ## Patroni 설정 예시
  postgresql:
    pg_hba:
      ## OpenProxy 대역에 대해서 인증 없이 접속을 허용하는 trust 옵션을 적용합니다.
      - host    all      all          172.18.65.0/24  trust
  ```

#### **Connection Pool 사용자 정보 설정**

* openproxy.toml 에 \[pools.simple\_db.users.0] 섹션 작성합니다.

  ```toml
  [pools.simple_db.users.0]
  username = "simple_user"
  password = "simple_user"
  pool_size = 5
  statement_timeout = 30000
  ```
* 해당 풀에 대해 정의되지 않은 사용자 이름을 가진 클라이언트 요청을 처리할 Wildcard 사용자를 아래와 같이 정의할 수 있습니다.

  ```toml
  [pools.simple_db.users.1]
  username = "*"
  pool_size = 3
  statement_timeout = 5000
  ```

#### **접속할 cluster 주소 및 database 기재**

* openproxy.toml 에 \[pools.simple\_db.shard.0] 섹션 작성합니다.

  ```toml
  [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 생성 설정 파일 전체 예시**

```toml
[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을 확인합니다.

```sql
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)
```

###


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.tibero.com/tmaxopensql/administration/openproxy/connection-pool.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
