如何使用数据库PG POOL的正确方法是什么

时间:2019-09-28 07:47:46

标签: node.js postgresql connection-pooling

我正在使用节点Postgress,并且我想将connection pooling与它一起使用。我有一个像这样的简单存储库:

初始化

    const pool = new Pool({
        connectionString: process.env.DATABASE_URL,
    })

    await pool.connect()

示例SELECT代码..

export default (
    {
        pgPool,
    }: Object
) => {
    return { 
        getExampleData: async (uid: string) => {
            const result = await pgPool.query('SELECT data FROM public.results_table WHERE uid = $1::text;', [uid])
            return result.rows
        },
    }
}

我的问题是,在压力(很多请求)下,我遇到以下错误:

  

错误:剩余的连接插槽保留用于非复制   超级用户连接

我不确定是否正确使用了池。

1 个答案:

答案 0 :(得分:0)

我们的pgpool.conf的一部分:

# - Pool size -

num_init_children = 100
                                   # Number of pools
                                   # (change requires restart)
max_pool = 3
                                   # (change requires restart)

# - Life time -

child_life_time = 120
                                   # Pool exits after being idle for this many seconds
child_max_connections = 0
                                   # Pool exits after receiving that many connections
                                   # 0 means no exit
connection_life_time = 90
                                   # Connection to backend closes after being idle for this many seconds
                                   # 0 means no close
client_idle_limit = 0
                                   # Client is disconnected after being idle for that many seconds
                                   # (even inside an explicit transactions!)
                                   # 0 means no disconnection

以下问题/解答也应为您提供帮助:

Heroku “psql: FATAL: remaining connection slots are reserved for non-replication superuser connections”

相信这会有所帮助。