PostgreSQL 连接数限制错误

问题描述

GitLab 组件日志(例如 sidekiq、webservice、praefect)显示以下数据库错误:pq: sorry, too many clients already

根本原因

GitLab 打开的并发 PostgreSQL 连接数超过了数据库允许的数量(超出 PostgreSQL max_connections)。

故障排查

检查 GitLab Deployments 的日志。确认是否存在 pq: sorry, too many clients already 错误消息。

kubectl -n <NAMESPACE> logs <RELEASE>-praefect-xxxxx
kubectl -n <NAMESPACE> logs <RELEASE>-sidekiq-all-in-1-v2-xxxxx
kubectl -n <NAMESPACE> logs <RELEASE>-webservice-default-xxxxx

解决方案

如果 PostgreSQL 的 max_connections 对于您的 GitLab 规模而言过低,请在 PostgreSQL 端提高该值。具体步骤取决于您管理 PG 的方式,不在本文讨论范围内。

您可以使用以下命令检查 PostgreSQL 容量和当前使用情况:

-- Show the global upper limit on concurrent connections allowed by this PostgreSQL instance.
-- Note: the effective limit available to non-superusers ≈ max_connections - superuser_reserved_connections.
SHOW max_connections;

-- Show how many connections are reserved for superusers,
-- so administrators can still log in when all regular slots are taken.
SHOW superuser_reserved_connections;

-- Count the total number of currently open backend connections across all databases and states
-- (e.g., active, idle, idle in transaction, autovacuum, replication).
SELECT count(*) AS current_connections FROM pg_stat_activity;

GitLab 是 GitLab Inc. 的商标。

Alauda 是一家独立供应商。本产品与 GitLab Inc. 没有关联,也未获其认可或赞助。所有商标均为其各自所有者的财产,此处使用这些商标仅用于标识目的。