default_transaction_read_only 是一个 配置参数,用于确定事务默认是否为只读。
default_transaction_read_only 已在 PostgreSQL 7.4 中添加。
默认值
default_transaction_read_only 的默认值为:off。
用法
在主服务器上,default_transaction_read_only 默认为 off。在备用服务器上,它始终为 on。
libpq
default_transaction_read_only 是报告给 libpq 连接的参数之一,可以使用以下方式查询:
PQparameterStatus(conn, "default_transaction_read_only")
按 PostgreSQL 版本详细信息
default_transaction_read_only (PostgreSQL 19)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
| 需要重启 |
false |
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 18)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
| 需要重启 |
false |
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 17)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
| 需要重启 |
false |
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 16)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
| 需要重启 |
false |
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 15)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
| 需要重启 |
false |
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 14)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
| 需要重启 |
false |
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 13)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
| 需要重启 |
false |
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 12)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
| 需要重启 |
false |
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 11)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
| 需要重启 |
false |
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 10)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
| 需要重启 |
false |
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 9.6)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
| 需要重启 |
false |
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 9.5)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
| 需要重启 |
false |
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 9.4)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 9.3)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 9.2)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 9.1)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 9.0)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 8.4)
| 设置 |
off |
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
| 枚举值 |
|
| 启动值 |
off |
| 重置值 |
off |
| 源文件 |
|
| 源行 |
|
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 8.3)
| 设置 |
|
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
文档: default_transaction_read_only
default_transaction_read_only (PostgreSQL 8.2)
| 设置 |
|
| 单位 |
|
| 类别 |
客户端连接默认值 / 语句行为 |
| 简短描述 |
设置新事务的默认只读状态。 |
| 扩展描述 |
|
| 上下文 |
user |
| 变量类型 |
bool |
| 来源 |
默认 |
| 最小值 |
|
| 最大值 |
|
文档: default_transaction_read_only
变更历史
示例
default_transaction_read_only 的基本用法示例
postgres=# SHOW transaction_read_only;
transaction_read_only
-----------------------
off
(1 row)
postgres=# BEGIN;
BEGIN
postgres=*# SET TRANSACTION READ ONLY;
SET
postgres=*# SHOW transaction_read_only;
transaction_read_only
-----------------------
on
(1 row)
这相当于(在主服务器上)
postgres=# BEGIN ;
BEGIN
postgres=*# SET transaction_read_only = on;
SET
postgres=*# SHOW transaction_read_only;
transaction_read_only
-----------------------
on
(1 row)
postgres=*# INSERT INTO foo values(1);
ERROR: cannot execute INSERT in a read-only transaction
在备用服务器上,default_transaction_read_only 始终为 on
postgres=# SELECT pg_is_in_recovery();
pg_is_in_recovery
-------------------
t
(1 row)
postgres=# SHOW transaction_read_only;
transaction_read_only
-----------------------
on
(1 row)
无法在备用服务器上设置 default_transaction_read_only
postgres=# SET transaction_read_only = off;
ERROR: cannot set transaction read-write mode during recovery