transaction_timeout 是一个 配置参数,用于终止运行时间超过指定时间的事务。
transaction_timeout 在 PostgreSQL 17 中添加。
用法
如果同时设置了 idle_in_transaction_session_timeout 和/或 statement_timeout 以及 transaction_timeout,则最短的超时时间将覆盖其他设置。
不建议在 postgresql.conf 中设置 transaction_timeout。
默认值
默认值为 transaction_timeout: 0 (不设置超时)。
按 PostgreSQL 版本详细信息
transaction_timeout (PostgreSQL 19)
| 设置 | 0 |
| 单位 | 毫秒 |
| 类别 | 客户端连接默认值 / 语句行为 |
| 简短描述 | 设置会话中任何事务(非预备事务)允许的最大持续时间。 |
| 扩展描述 | 0 表示禁用超时。 |
| 上下文 | user |
| 变量类型 | 整数 |
| 来源 | 默认 |
| 最小值 | 0 |
| 最大值 | 2147483647 |
| 枚举值 | |
| 启动值 | 0 |
| 重置值 | 0 |
| 源文件 | |
| 源行 | |
| 需要重启 | false |
transaction_timeout (PostgreSQL 18)
| 设置 | 0 |
| 单位 | 毫秒 |
| 类别 | 客户端连接默认值 / 语句行为 |
| 简短描述 | 设置会话中任何事务(非预备事务)允许的最大持续时间。 |
| 扩展描述 | 0 表示禁用超时。 |
| 上下文 | user |
| 变量类型 | 整数 |
| 来源 | 默认 |
| 最小值 | 0 |
| 最大值 | 2147483647 |
| 枚举值 | |
| 启动值 | 0 |
| 重置值 | 0 |
| 源文件 | |
| 源行 | |
| 需要重启 | false |
transaction_timeout (PostgreSQL 17)
| 设置 | 0 |
| 单位 | 毫秒 |
| 类别 | 客户端连接默认值 / 语句行为 |
| 简短描述 | 设置会话中任何事务(非预备事务)允许的最大持续时间。 |
| 扩展描述 | 值为 0 将关闭超时。 |
| 上下文 | user |
| 变量类型 | 整数 |
| 来源 | 默认 |
| 最小值 | 0 |
| 最大值 | 2147483647 |
| 枚举值 | |
| 启动值 | 0 |
| 重置值 | 0 |
| 源文件 | |
| 源行 | |
| 需要重启 | false |
变更历史
- PostgreSQL 17
- 添加于 (初始提交 51efe38c)
示例
transaction_timeout 的基本用法示例
postgres=# SET transaction_timeout = '1s'; SET postgres=# \timing Timing is on. postgres=# SELECT pg_sleep(2); FATAL: terminating connection due to transaction timeout server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request. The connection to the server was lost. Attempting reset: Succeeded. Time: 1017.288 ms (00:01.017)
preceding timeout event 的相应日志条目是
[2024-02-16 16:54:27 UTC] psql postgres postgres LOG: 00000: statement: SELECT pg_sleep(2); [2024-02-16 16:54:28 UTC] psql postgres postgres FATAL: 25P04: terminating connection due to transaction timeout [2024-02-16 16:54:28 UTC] psql postgres postgres STATEMENT: SELECT pg_sleep(2); [2024-02-16 16:54:28 UTC] psql postgres postgres LOG: 00000: disconnection: session time: 0:00:18.413 user=postgres database=postgres host=127.0.0.1 port=54480
参考资料
- PostgreSQL documentation: transaction_timeout
有用链接
- PostgreSQL 17: transaction_timeout - 2024 年 2 月 Daniel Westermann / dbi services 的博客文章
