You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Prisma2连接Azure Postgres14执行seed报连接槽预留错误如何排查

问题环境
  • Postgres 14
  • Prisma 2
  • Azure Database
问题现象

对开发数据库执行seeding(数据填充)操作时触发报错,错误信息为:remaining connection slots are reserved for non-replication superuser connections。
初步判断错误由连接池资源不足导致,多轮排查后问题仍未解决,已尝试操作如下:

  • 检查会话泄漏问题,未发现异常
  • 修改/etc/postgresql/14/postgresql.conf配置文件,调大max_connections参数值
  • 重启数据库服务
  • 重启连接该远程数据库的本地Node.js应用
完整报错日志
PrismaClientUnknownRequestError: 
  Invalid `prisma.problem.create()` invocation in
  /myproject/api/prisma/seed.ts:448:43
  
    445 
    446 await Promise.all(
    447   problemData.map(async (data) => {
  → 448     const newOne = await prisma.problem.create(
    Error in connector: Error querying the database: db error: FATAL: remaining connection slots are reserved for non-replication superuser connections
      at RequestHandler.request (/myproject/api/node_modules/@prisma/client/runtime/index.js:49026:15)
      at async PrismaClient._request (/myproject/api/node_modules/@prisma/client/runtime/index.js:49919:18)
      at async /myproject/api/prisma/seed.ts:448:22
      at async Promise.all (index 11)
      at async main (/myproject/api/prisma/seed.ts:446:3) {
    clientVersion: '3.15.2'
  }
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

An error occured while running the seed command:
Error: Command failed with exit code 1: yarn seed
根因分析

两个核心问题直接触发报错:

  1. 调整max_connections的操作无效:Azure Database for PostgreSQL是托管PaaS服务,直接登录服务器修改系统内的postgresql.conf配置会被平台管控逻辑覆盖,不会持久化生效。同时Azure Postgres的连接数上限和定价层强绑定,基础层实例默认最大连接数仅为50,其中还预留了5个给超级用户的专用连接,业务侧可用连接配额本身就很低。
  2. seed脚本存在无限制并发:从报错栈可以看到,代码直接使用Promise.all遍历全量数据执行单条create操作,没有任何并发控制,短时间内会创建大量数据库连接,直接打满Prisma连接池和数据库的连接配额。
修复方案

按以下步骤调整即可解决问题:

  • 正确配置数据库连接上限
    登录Azure门户,进入对应PostgreSQL实例的「服务器参数」配置页,找到max_connections参数调整到当前定价层支持的合理值,保存后平台会自动重启实例使配置生效。如果当前使用基础层实例,建议升级到通用层,基础层的连接数上限无法支撑多客户端连接+批量数据导入的场景。
  • 优化seed脚本写入逻辑
    优先使用Prisma提供的批量写入接口替代循环单条创建,该方式单次请求仅占用1个连接,写入效率也远高于单条循环:
    // 替换原有Promise.all + 单条create的逻辑
    await prisma.problem.createMany({
      data: problemData,
      skipDuplicates: true // 可根据业务需求选择是否跳过重复数据
    })
    
    如果因为关联数据处理等特殊需求必须单条写入,不要直接用原生Promise.all发起全量并发请求,引入p-limit等并发控制工具,将同时执行的写入请求数限制在3-5个即可。
  • 显式配置Prisma连接池大小
    在Prisma使用的数据库连接串末尾追加connection_limit参数,取值不要超过数据库业务可用连接数的80%,预留部分连接给其他调试工具、本地服务使用,连接串示例:
    postgresql://<数据库用户名>:<密码>@<Azure实例访问地址>:5432/<库名>?schema=public&connection_limit=10
    
  • 排查闲置连接占用
    配置调整完成后,可在Azure门户的实例监控页查看实时连接数指标,关闭其他不必要的数据库连接客户端,清理长期闲置的僵尸连接,避免无效占用配额。

内容的提问来源于stack exchange,提问作者user14082

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 09:01:10