Eya*_*oth 4 sql postgresql transactions autocommit
我刚刚设置了一个新的PostgreSQL 9.5.2,似乎我的所有事务都是自动提交的.
运行以下SQL:
CREATE TABLE test (id NUMERIC PRIMARY KEY);
INSERT INTO test (id) VALUES (1);
ROLLBACK;
Run Code Online (Sandbox Code Playgroud)
导致警告:
WARNING: there is no transaction in progress
ROLLBACK
Run Code Online (Sandbox Code Playgroud)
在不同的事务上,以下查询:
SELECT * FROM test;
Run Code Online (Sandbox Code Playgroud)
实际上返回行1(就像提交了插件一样).
我试图autocommit出发,但似乎这个功能不再存在(我得到了unrecognized configuration parameter错误).
这到底是怎么回事?
Postgres中的autocommit由SQL 客户端控制,而不是在服务器上.
在psql你可以使用
\set AUTOCOMMIT off
Run Code Online (Sandbox Code Playgroud)
详细信息在手册中:http:
//www.postgresql.org/docs/9.5/static/app-psql.html#APP-PSQL-VARIABLES
在这种情况下,您执行的每个语句都会启动一个事务,直到您运行commit(包括select语句!)
其他SQL客户端具有启用/禁用自动提交的其他方法.
或者,您可以使用begin手动启动事务.
http://www.postgresql.org/docs/current/static/sql-begin.html
psql (9.5.1)
Type "help" for help.
postgres=> \set AUTCOMMIT on
postgres=> begin;
BEGIN
postgres=> create table test (id integer);
CREATE TABLE
postgres=> insert into test values (1);
INSERT 0 1
postgres=> rollback;
ROLLBACK
postgres=> select * from test;
ERROR: relation "test" does not exist
LINE 1: select * from test;
^
postgres=>
Run Code Online (Sandbox Code Playgroud)