PG查看和设置事务的隔离级别
查看和设置PG的事务隔离级别
事务内容请参考:https://www.xmmup.com/pgzhongdeshiwu.html
PG默认的事务隔离级别为Read Committed。
数据库隔离级别
查看全局事务隔离级别
1 2 3 4 5 6 7 8 9 10 11 | postgres=# select current_setting('default_transaction_isolation'); current_setting ----------------- read committed (1 row) postgres=# SELECT name,setting FROM pg_settings WHERE name='default_transaction_isolation'; name | setting -------------------------------+---------------- default_transaction_isolation | read committed (1 row) |
修改全局事务隔离级别
方法一:修改postgresql.conf文件中的default_transaction_isolation参数。
方法二:通过alter system修改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | postgres=# alter system set default_transaction_isolation to 'REPEATABLE READ'; ALTER SYSTEM postgres=# select pg_reload_conf(); pg_reload_conf ---------------- t (1 row) postgres=# select current_setting('default_transaction_isolation'); current_setting ----------------- repeatable read (1 row) postgres=# alter system set default_transaction_isolation to 'read committed'; ALTER SYSTEM postgres=# postgres=# select pg_reload_conf(); pg_reload_conf ---------------- t (1 row) postgres=# select current_setting('default_transaction_isolation'); current_setting ----------------- read committed (1 row) |
会话的事务隔离级别
查看
1 2 3 4 5 6 7 8 9 10 11 | postgres=# select current_setting('transaction_isolation'); current_setting ----------------- read committed (1 row) postgres=# show transaction_isolation; transaction_isolation ----------------------- read committed (1 row) |
修改
1 2 3 4 5 6 7 8 9 10 11 12 13 | postgres=# set session characteristics as transaction isolation level read uncommitted; SET postgres=# show transaction_isolation; transaction_isolation ----------------------- read uncommitted (1 row) postgres=# select current_setting('transaction_isolation'); current_setting ------------------ read uncommitted (1 row) |
当前事务
可以在启动事务的同时设置事务隔离级别:
1 2 3 4 5 6 7 | postgres=# start transaction isolation level read committed; START TRANSACTION postgres=*# 。。。 。。。 postgres=*# end; COMMIT |
start也可以修改为begin。