Skip to main content

连接池

Prisma Postgres 默认提供内置 连接池,由 Prisma 加速 启用。通过使用 Prisma Postgres,你可以享受连接池的优势,而无需进行任何配置。高效的数据库连接管理使数据库能够处理更多查询,而不会耗尽可用的数据库连接,从而使你的应用更具可扩展性。

¥Prisma Postgres provides built-in connection pooling by default, enabled by Prisma Accelerate. By using Prisma Postgres, you get the benefits of connection pooling without having to configure anything. The efficient management of database connections allows the database to process more queries without exhausting the available database connections, making your application more scalable.

然而,在某些情况下,进一步配置连接池可能会对优化应用的性能有所帮助。

¥In some cases, however, it may be beneficial to further configure connection pooling in order to optimize the performance of your application.

注意

本文档重点介绍 Prisma Postgres 的连接池功能。有关 Prisma ORM 内部连接池的更多信息,请参阅我们的 ORM 连接池文档

¥This document focuses on the connection pooling features of Prisma Postgres. For more information about the internal connection pool of Prisma ORM specifically, see our ORM connection pooling documentation.

Prisma Postgres 中的连接池

¥Connection pooling in Prisma Postgres

目前,Prisma Postgres 允许每个 Prisma Schema 最多并发 10 个数据库连接。由于 Prisma Postgres 高效的基于单核的架构,该限制通常足够,从而最大限度地减少了对大型连接池的需求。

¥Currently, Prisma Postgres allows a maximum of 10 concurrent database connections per Prisma Schema. This limit is typically sufficient due to Prisma Postgres's efficient unikernel-based architecture, which minimizes the need for large connection pools.

注意

如果你在 Prisma Accelerate 中使用自己的数据库,连接限制会有所不同:

¥If you're using your own database with Prisma Accelerate, the connection limits differ:

  • 入门版:10 连接数的最大值。这通常足以满足大多数工作负载的需求,但如果你预计流量较大或计算操作密集,则可能需要 增加此限制

    ¥Starter plan: Maximum of 10 connections. This is often sufficient for most workloads, but if you're expecting high traffic or intensive compute operations, you may want to increase this limit.

  • 专业版:最多支持 100 个并发连接。

    ¥Pro plan: Supports up to 100 concurrent connections.

  • 商业计划:最多支持 1000 个并发连接。

    ¥Business plan: Supports up to 1000 concurrent connections.

你可以在 Prisma 定价页面 上比较计划。

¥You can compare plans on the Prisma pricing page.

配置连接池大小

¥Configuring the connection pool size

如果你不使用 Prisma Postgres,则可以通过指定 在连接字符串中 来配置 Prisma ORM 的连接池大小。

¥If you're not using Prisma Postgres, you can configure the connection pool size for Prisma ORM by specifying it in the connection string.

对于 Prisma Postgres,连接限制目前固定为 10,无法更改。

¥For Prisma Postgres, the connection limit is currently fixed at 10 and cannot be changed.

如果你将 Prisma Accelerate 与自己​​的数据库一起使用,则可以在 Accelerate 设置页面 上的项目中通过 Connection limit 设置配置连接池大小。

¥If you're using Prisma Accelerate with your own database, you can configure the connection pool size through the Connection limit setting in your project on the Accelerate setup page.

配置连接池超时

¥Configuring the connection pool timeout

连接池超时是指查询在等待 Prisma Postgres 内部连接池的连接时阻塞的最大秒数。如果并发请求数超过连接限制,就会发生这种情况,导致其他请求排队,直到有空闲的连接可用。如果在池超时时间内没有可用的连接,则会抛出异常。可以通过将值设置为 0 来禁用连接池超时。

¥The connection pool timeout is the maximum number of seconds that a query will block while waiting for a connection from Prisma Postgres's internal connection pool. This occurs if the number of concurrent requests exceeds the connection limit, resulting in queueing of additional requests until a free connection becomes available. An exception is thrown if a free connection does not become available within the pool timeout. The connection pool timeout can be disabled by setting the value to 0.

与连接池大小类似,你也可以通过数据库连接字符串配置连接池超时。要调整此值,你可以将 pool_timeout 参数添加到数据库连接字符串。

¥Similar to the connection pool size, you may also configure the connection pool timeout via the database connection string. To adjust this value, you may add the pool_timeout parameter to the database connection string.

例如:

¥For example:

postgresql://user:password@localhost:5432/db?connection_limit=10&pool_timeout=20
info

pool_timeout 的默认值为 10 秒。

¥The default value for pool_timeout is 10 seconds.

配置查询限制

¥Configuring query limits

在使用 Prisma Accelerate 时,你可以从 Prisma Postgres 项目环境中的“设置”选项卡中配置最小和最大查询响应大小、查询持续时间和事务限制。

¥You can configure the minimum and maximum query response size, query duration, and transaction limits when using Prisma Accelerate from the Settings tab in your Prisma Postgres project environment.

查询超时限制

¥Query timeout limit

Prisma Postgres 的每个查询的默认全局超时为 10s,可根据你的订阅计划,使用标有“查询持续时间”的滑块进行配置:

¥Prisma Postgres has a default global timeout of 10s for each query, configurable using the slider labeled Query duration, based on your subscription plan:

计划入门专业版商业
查询超时最多 10最多 20最多 60

有关更多信息,请参阅 错误参考 和我们的 定价页面

¥See the error reference and our pricing page for more information.

警告

虽然你可以增加查询超时,但如果数据库查询的执行时间超过 10 秒,建议检查并优化它们。这有助于减轻底层数据库的压力,因为长时间运行的查询通常表明需要优化。在 错误参考 中了解更多信息。

¥While you can increase the query timeout, it's recommended to inspect and optimize your database queries if they take longer than 10 seconds. This helps reduce stress on your underlying database, as long-running queries often indicate a need for optimization. Learn more in the error reference.

交互式事务查询超时限制

¥Interactive transactions query timeout limit

Prisma Postgres 的每个 交互式事务 的默认全局超时为 15s,可根据你的订阅计划,使用标有“事务持续时间”的滑块进行配置:

¥Prisma Postgres has a default global timeout of 15s for each interactive transaction, configurable using the slider labeled Transaction duration, based on your subscription plan:

计划入门专业版商业
交互式事务限制最多 15最多 30最多 90

有关更多信息,请参阅 错误参考 和我们的 定价页面

¥See the error reference and our pricing page for more information.

当你在 Prisma 控制台中设置更高的交互式事务超时时,你还必须通过超时 事务选项 在交互式事务查询中指定匹配的 timeout 值。否则,事务仍将以较低的默认值超时(例如,当未指定超时值时,限制为 5 秒)。以下是如何在代码中设置 30 秒超时的示例:

¥When you set a higher interactive transaction timeout in the Prisma Console, you must also specify a matching timeout value in your interactive transaction query via timeout transaction option. Otherwise, transactions will still time out at the lower default (e.g., 5 seconds limit when no timeout value is specified). Here's an example of how to set a 30-second timeout in your code:

await prisma.$transaction(
async (tx) => {
// Your queries go here
},
{
timeout: 30000, // 30s
}
);
警告

虽然你可以增加交互式事务超时限制,但如果数据库事务的执行时间超过 15 秒,建议检查并优化它们。长时间运行的事务会对性能产生负面影响,通常表明需要进行优化。在 错误参考 中了解更多信息,并查看我们文档中的 “交互式事务”部分中的警告

¥While you can increase the interactive transaction timeout limit, it's recommended to inspect and optimize your database transactions if they take longer than 15 seconds. Long-running transactions can negatively impact performance and often signal the need for optimization. Learn more in the error reference and review the warning in the Interactive Transactions section in our documentation.

响应大小限制

¥Response size limit

Prisma Postgres 的默认全局响应大小限制为 5MB,可根据你的订阅计划,使用标有“响应大小”的滑块进行配置:

¥Prisma Postgres has a default global response size limit of 5MB, configurable using the slider labeled Response size, based on your subscription plan:

计划入门专业版商业
查询大小最多 5MB最多 10MB最多 20MB

有关更多信息,请参阅 错误参考 和我们的 定价页面

¥See the error reference and our pricing page for more information.

警告

虽然你可以增加查询响应的大小,但建议将数据检索限制在实际需要的范围内。这可以提高数据库性能,减轻数据库压力,并使你的前端应用响应更快。超过 5 MB 的查询通常表示需要优化。在 错误参考 中了解更多信息。

¥While you can increase the query response size, it’s recommended to limit data retrieval to what you actually need. This improves database performance, reduces stress on your database, and makes your frontend applications more responsive. Queries exceeding 5 MB in size often indicate a need for optimization. Learn more in the error reference.

自动扩展(仅限 Accelerate + 你自己的数据库)

¥Autoscaling (Accelerate + Your own database only)

目前,仅在将 Prisma Accelerate 与你自己的数据库一起使用时,自动扩展功能才可用。它可以根据应用的流量实现动态资源分配。当使用量接近定义的连接限制时,Prisma 将开始配置额外资源来处理负载。如果流量持续增长,系统将进一步扩展。当流量减少时,它会缩减规模,以确保高效利用资源。

¥Autoscaling is currently available only when using Prisma Accelerate with your own database. It enables dynamic resource allocation based on your application's traffic. As usage nears the defined connection limit, Prisma will begin provisioning additional resources to handle the load. If traffic continues to grow, the system will scale out further. When traffic decreases, it scales back down—ensuring efficient use of resources.

工作原理

¥How it works

自动扩展功能由连接池提供支持,该连接池通过在多个查询引擎实例之间分配总可用连接数来水平扩展你的环境。

¥Autoscaling is powered by a connection pooler that horizontally scales your environment by distributing total available connections across multiple Query Engine instances.

以下是实际操作方法:

¥Here’s how this works in practice:

  • 假设你的环境的连接数限制设置为 1000

    ¥Suppose your environment’s connection limit is set to 1000.

  • Prisma Accelerate 可扩展到多个查询引擎实例(例如 100 个实例)。

    ¥Prisma Accelerate will scale up to multiple Query Engine instances (e.g., 100 instances).

  • 每个实例都会分配到一部分连接 - 在本例中,每个实例分配了 10 个连接。

    ¥Each instance is allocated a share of the total—10 connections per instance, in this example.

  • 这就是为什么每个查询引擎实例都报告 10 个限制的原因,即使完整环境支持 1000 个并发连接。

    ¥This is why each Query Engine instance reports a limit of 10, even though the full environment supports 1000 concurrent connections.

这种分布式模型允许你的应用通过启动更多查询引擎实例来处理增加的流量,同时有效地管理连接使用情况。

¥This distributed model allows your application to handle increased traffic by spinning up more Query Engine instances, while efficiently managing connection usage.

启用自动扩缩

¥Enabling Autoscaling

当你的 Accelerate 连接限制高于默认值 (10) 时,自动扩展功能将自动启用。此功能在入门计划中不可用。

¥Autoscaling is automatically enabled when your Accelerate connection limit is set above the default (10). This feature is not available on the Starter plan.

你的环境的最大连接限制取决于你的 Prisma 数据平台计划

¥Your environment's maximum connection limit is based on your Prisma Data Platform plan:

计划最大连接限制
入门10
专业版100
商业1000
企业联系我们