跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[问与答] 关于 PostgreSQL 中的 max_connections 和 pg_stat_activity 的疑惑

发表于

问题描述

各位大佬,有个问题请教下 我在本地( Windows11 )安装了一个 PostgreSQL ,一切都是默认值安装

我通过以下语句查到 max connections 的值是 100

show max_connections; -- 返回 100

我猜测这个应该就是 PG 默认的最大连接数,然后我用 Python 写了一个脚本,循环获取 connection:

import psycopg2
import time

def get_db_conn():
    conn = psycopg2.connect(host='localhost',port=5432,database='study_demo',user='postgres',password='123456')
    return conn

def main():
    conn_list = list()
    for i in range(100):
        print(f'获取第 {i} 个连接')
        try:
            conn_list.append(get_db_conn())
        except Exception:
            print(f'获取第 {i} 个连接失败')
    print(f'成功获取 {len(conn_list)} 个连接')
    time.sleep(60)
    
if __name__ == '__main__':
    main()

然后代码显示获取到了 95 个连接,然后我通过以下 SQL 获取连接数

select count(*) from pg_stat_activity; -- 返回 105

我预测的值应该是:

  1. Python 获取到 90 个连接(因为执行 Python 脚本前,就有 10 个连接了,这几个连接可能是开了 Navicat 和 pgAdmin4 的缘故)
  2. pg stat activity 最后应该正好是 100 或者 99 (留一个连接给超级用户?)

但是结果完全不一样,105 明显超过了,这多出来的 5 个是如何得到的?

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.