postgres=# begin; BEGIN postgres=# create table test (id int, name text); CREATE TABLE postgres=# \dt List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | test | table | postgres (1 row) postgres=# rollback ; ROLLBACK postgres=# \dt No relations found.
postgres=# create table test (id int, name text); CREATE TABLE postgres=# begin ; BEGIN postgres=# alter table test add column score int; ALTER TABLE postgres=# \d test Table "public.test" Column | Type | Modifiers --------+---------+----------- id | integer | name | text | score | integer | postgres=# rollback ; ROLLBACK postgres=# \d test Table "public.test" Column | Type | Modifiers --------+---------+----------- id | integer | name | text |
postgres=# begin; BEGIN postgres=# drop table test ; DROP TABLE postgres=# \dt No relations found. postgres=# rollback ; ROLLBACK postgres=# \dt List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | test | table | postgres (1 row)
postgres=# insert into test values (1, 'a'); INSERT 0 1 postgres=# select * from test ; id | name ----+------ 1 | a (1 row) postgres=# begin ; BEGIN postgres=# truncate test ; TRUNCATE TABLE postgres=# select * from test ; id | name ----+------ (0 rows) postgres=# rollback ; ROLLBACK postgres=# select * from test ; id | name ----+------ 1 | a (1 row)
postgres=# begin ; BEGIN postgres=# create index on test (id); CREATE INDEX postgres=# \d test Table "public.test" Column | Type | Modifiers --------+---------+----------- id | integer | name | text | Indexes: "test_id_idx" btree (id) postgres=# rollback ; ROLLBACK postgres=# \d test Table "public.test" Column | Type | Modifiers --------+---------+----------- id | integer | name | text |
postgres=# create index on test (id); CREATE INDEX postgres=# \d test Table "public.test" Column | Type | Modifiers --------+---------+----------- id | integer | name | text | Indexes: "test_id_idx" btree (id) postgres=# begin ; BEGIN postgres=# drop index test_id_idx ; DROP INDEX postgres=# \d test Table "public.test" Column | Type | Modifiers --------+---------+----------- id | integer | name | text | postgres=# rollback ; ROLLBACK postgres=# \d test Table "public.test" Column | Type | Modifiers --------+---------+----------- id | integer | name | text | Indexes: "test_id_idx" btree (id)
注意如果索引的创建和删除加了CONCURRENTLY,则不能放在事务里
postgres=# drop index test_id_idx ; DROP INDEX postgres=# \d test Table "public.test" Column | Type | Modifiers --------+---------+----------- id | integer | name | text | postgres=# begin ; BEGIN postgres=# CREATE INDEX CONCURRENTLY ON test (id); ERROR: CREATE INDEX CONCURRENTLY cannot run inside a transaction block postgres=# rollback ; ROLLBACK postgres=# \d test Table "public.test" Column | Type | Modifiers --------+---------+----------- id | integer | name | text |
postgres=# select * from test ; id | name ----+------ 1 | a (1 row) postgres=# begin ; BEGIN postgres=# insert into test values (2, 'b'); INSERT 0 1 postgres=# select * from test ; id | name ----+------ 1 | a 2 | b (2 rows) postgres=# rollback ; ROLLBACK postgres=# select * from test ; id | name ----+------ 1 | a (1 row)
postgres=# select * from test ; id | name ----+------ 1 | a (1 row) postgres=# begin ; BEGIN postgres=# update test set id = 2 where id = 1; UPDATE 1 postgres=# select * from test ; id | name ----+------ 2 | a (1 row) postgres=# rollback ; ROLLBACK postgres=# select * from test ; id | name ----+------ 1 | a (1 row)
postgres=# select * from test ; id | name ----+------ 1 | a (1 row) postgres=# begin ; BEGIN postgres=# delete from test where id = 1; DELETE 1 postgres=# select * from test ; id | name ----+------ (0 rows) postgres=# rollback ; ROLLBACK postgres=# select * from test ; id | name ----+------ 1 | a (1 row)