os: centos 7.4
db: postgresql 10.11
postgresql 默認的 isolation level 爲 read committed,可以調整隔離級別。
版本
# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)
#
#
# su - postgres
Last login: Wed Jan 15 18:34:12 CST 2020 on pts/0
$
$
$ psql -c "select version();"
version
----------------------------------------------------------------------------------------------------------
PostgreSQL 10.11 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
(1 row)
set transaction isolation level
postgres=# select name,setting from pg_settings where name like 'transaction%';
name | setting
------------------------+----------------
transaction_deferrable | off
transaction_isolation | read committed
transaction_read_only | off
(3 rows)
postgres=# \h set transaction
Command: SET TRANSACTION
Description: set the characteristics of the current transaction
Syntax:
SET TRANSACTION transaction_mode [, ...]
SET TRANSACTION SNAPSHOT snapshot_id
SET SESSION CHARACTERISTICS AS TRANSACTION transaction_mode [, ...]
where transaction_mode is one of:
ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
READ WRITE | READ ONLY
[ NOT ] DEFERRABLE
postgres=# set transaction isolation level serializable;
WARNING: SET TRANSACTION can only be used in transaction blocks
SET
serializable
postgres=# begin;
postgres=# set transaction isolation level serializable;
postgres=# show transaction_isolation;
transaction_isolation
-----------------------
serializable
(1 row)
postgres=# rollback;
repeatable read
postgres=# begin;
postgres=# set transaction isolation level repeatable read;
postgres=# show transaction_isolation;
transaction_isolation
-----------------------
repeatable read
(1 row)
postgres=# rollback;
read committed
postgres=# begin;
postgres=# set transaction isolation level read committed;
postgres=# show transaction_isolation;
transaction_isolation
-----------------------
read committed
(1 row)
postgres=# rollback;
read uncommitted
postgres=# begin;
postgres=# set transaction isolation level read uncommitted;
postgres=# show transaction_isolation;
transaction_isolation
-----------------------
read uncommitted
(1 row)
postgres=# rollback;
begin
postgres=# \h begin
Command: BEGIN
Description: start a transaction block
Syntax:
BEGIN [ WORK | TRANSACTION ] [ transaction_mode [, ...] ]
where transaction_mode is one of:
ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
READ WRITE | READ ONLY
[ NOT ] DEFERRABLE
serializable
postgres=# begin transaction isolation level serializable;
postgres=# show transaction_isolation;
transaction_isolation
-----------------------
serializable
(1 row)
postgres=# rollback;
repeatable read
postgres=# begin transaction isolation level repeatable read;
postgres=# show transaction_isolation;
transaction_isolation
-----------------------
repeatable read
(1 row)
postgres=# rollback;
read committed
postgres=# begin transaction isolation level read committed;
postgres=# show transaction_isolation;
transaction_isolation
-----------------------
read committed
(1 row)
postgres=# rollback;
read uncommitted
postgres=# begin transaction isolation level read uncommitted;
postgres=# show transaction_isolation;
transaction_isolation
-----------------------
read uncommitted
(1 row)
postgres=# rollback;
參考:
http://postgres.cn/docs/10/sql-set-transaction.html