psql工具默认开启自动提交功能(AUTOCOMMIT = 'on')
1. 测试
postgres=# select version();
version
-----------------------------------------------------------------------------
PostgreSQL 10.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-28), 64-bit
(1 row)
postgres=# \d test
Table "public.test"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
id | integer | | |
postgres=# insert into test values (1);
INSERT 0 1
postgres=# select * from test;
id
----
1
(1 row)
postgres=# insert into test values (2);
INSERT 0 1
postgres=# rollback;
WARNING: there is no transaction in progress
ROLLBACK
postgres=# select * from test;
id
----
1
2
(2 rows)
2. 查看自动提交功能状态
postgres=# \set
AUTOCOMMIT = 'on'
COMP_KEYWORD_CASE = 'preserve-upper'
DBNAME = 'postgres'
ECHO = 'none'
3. 关闭自动提交功能
postgres=# \set AUTOCOMMIT off
postgres=# \set
AUTOCOMMIT = 'off'
COMP_KEYWORD_CASE = 'preserve-upper'
DBNAME = 'postgres'
ECHO = 'none'
4. 验证
postgres=# insert into test values (3);
INSERT 0 1
postgres=# select * from test;
id
----
1
2
3
(3 rows)
postgres=# rollback;
ROLLBACK
postgres=# select * from test;
id
----
1
2
(2 rows)
postgres=#