PostgreSQL's read only transaction mode

让数据库变成只读模式,目前PostgreSQL没有严格意义上的只读模式(如临时表在只读事务中还是可以使用的)。通过调整参数或设置事务模式可以将后续登录的SESSION或者当前事务设置为只读模式。
在只读模式下,PostgreSQL不允许如下SQL:
When a transaction is read-only, the following SQL commands are disallowed: INSERT , UPDATE , DELETE , and COPY FROM if the table they would write to is not a temporary table; all CREATE , ALTER , and DROP commands; COMMENT , GRANT , REVOKE , TRUNCATE ; and EXPLAIN ANALYZE and EXECUTE if the command they would execute is among those listed. This is a high-level notion of read-only that does not prevent all writes to disk.

在SQL模式下进入只读事务的方法:
digoal=> begin;
BEGIN
digoal=> set transaction read only;
SET

参数配置 :
default_transaction_read_only = on
配置完后pg_ctl reload -D $PGDATA
配置完参数后,不影响已经连接的SESSION,仅仅对后续连接上来的SESSION生效。新建的SESSION进来后事务就是read only模式。
digoal=> show default_transaction_read_only
digoal-> ;
 default_transaction_read_only
-------------------------------
 on
digoal=> delete from tbl_test;
ERROR:  cannot execute DELETE in a read-only transaction

# 可以设置事务级WRITE覆盖这个默认值
digoal=> begin;
BEGIN
digoal=> set transaction read write;
SET
digoal=> delete from tbl_test;
DELETE 1008

# 或者设置SESSION级参数,覆盖之
digoal=> set session default_transaction_read_only=off;
SET
digoal=> delete from tbl_test;
DELETE 1008

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值