当前位置: 首页 > article >正文

Postgresqlddl在事务中可以回滚,truncate时relfilenode在当前会话会改变

Postgresql的事务里面ddl可以回滚,这点和oracle不太一样。其中postgresql alter table事务操作中,包括回滚的整个过程中表对象的relfilenode不变,但是postgresql truncate事务操作中,一旦执行truncate操作表对象的relfilenode在当前会话就变了,其他会话查询表对象的relfilenode没有变化,truncate事务操作回滚后表对象的relfilenode在当前会话中也回滚了

Postgresql的事务里面ddl可以回滚,如下新增字段操作的ddl回滚后,新增的字段就消失了

postgres=# select * from emp;
  id  |  name  | position | managerid | yearmomthday | salary | ticheng | salper
------+--------+----------+-----------+--------------+--------+---------+--------
 7369 | SMITH  | CLERK    |      7902 | 1982-12-17   |    800 |         |     20
 7499 | ALLEN  | SALESMAN |      7698 | 1981-02-20   |   1600 |     300 |     20
 7521 | WARD   | SALESMAN |      7698 | 1981-02-22   |   1250 |     500 |     30
 7566 | JONES  | MANAGER  |      7389 | 1981-04-02   |   2975 |         |     20
 7654 | MARTIN | SALESMAN |      7698 | 1982-09-28   |   1250 |    1400 |     30
(5 rows)

postgres=# select oid,relname,relfilenode from pg_class where relname='emp';
  oid  | relname | relfilenode
-------+---------+-------------
 16412 | emp     |       16412
(1 row)

postgres=# begin;
BEGIN
postgres=# alter table emp add column lx varchar(100) default 'good';
ALTER TABLE
postgres=# select oid,relname,relfilenode from pg_class where relname='emp';
  oid  | relname | relfilenode
-------+---------+-------------
 16412 | emp     |       16412
(1 row)

postgres=# select * from emp;
  id  |  name  | position | managerid | yearmomthday | salary | ticheng | salper |  lx
------+--------+----------+-----------+--------------+--------+---------+--------+------
 7369 | SMITH  | CLERK    |      7902 | 1982-12-17   |    800 |         |     20 | good
 7499 | ALLEN  | SALESMAN |      7698 | 1981-02-20   |   1600 |     300 |     20 | good
 7521 | WARD   | SALESMAN |      7698 | 1981-02-22   |   1250 |     500 |     30 | good
 7566 | JONES  | MANAGER  |      7389 | 1981-04-02   |   2975 |         |     20 | good
 7654 | MARTIN | SALESMAN |      7698 | 1982-09-28   |   1250 |    1400 |     30 | good
(5 rows)

postgres=# rollback;
ROLLBACK
postgres=# select oid,relname,relfilenode from pg_class where relname='emp';
  oid  | relname | relfilenode
-------+---------+-------------
 16412 | emp     |       16412
(1 row)

postgres=# select * from emp;
  id  |  name  | position | managerid | yearmomthday | salary | ticheng | salper
------+--------+----------+-----------+--------------+--------+---------+--------
 7369 | SMITH  | CLERK    |      7902 | 1982-12-17   |    800 |         |     20
 7499 | ALLEN  | SALESMAN |      7698 | 1981-02-20   |   1600 |     300 |     20
 7521 | WARD   | SALESMAN |      7698 | 1981-02-22   |   1250 |     500 |     30
 7566 | JONES  | MANAGER  |      7389 | 1981-04-02   |   2975 |         |     20
 7654 | MARTIN | SALESMAN |      7698 | 1982-09-28   |   1250 |    1400 |     30
(5 rows)

Postgresql的事务里面ddl可以回滚,如下truncate操作,回滚后,truncate的数据又回来了

testdb=# select * from emp;
  id  |  name  | position | managerid | yearmomthday | salary | ticheng | salper
------+--------+----------+-----------+--------------+--------+---------+--------
 7369 | SMITH  | CLERK    |      7902 | 1982-12-17   |    800 |         |     20
 7499 | ALLEN  | SALESMAN |      7698 | 1981-02-20   |   1600 |     300 |     20
 7521 | WARD   | SALESMAN |      7698 | 1981-02-22   |   1250 |     500 |     30
 7566 | JONES  | MANAGER  |      7389 | 1981-04-02   |   2975 |         |     20
 7654 | MARTIN | SALESMAN |      7698 | 1982-09-28   |   1250 |    1400 |     30
(5 rows)

testdb=# select oid,relname,relfilenode from pg_class where relname='emp';
  oid  | relname | relfilenode
-------+---------+-------------
 16400 | emp     |       16400
(1 row)

testdb=# begin;
BEGIN
testdb=# truncate table emp;
TRUNCATE TABLE
testdb=# select * from emp;
 id | name | position | managerid | yearmomthday | salary | ticheng | salper
----+------+----------+-----------+--------------+--------+---------+--------
(0 rows)

testdb=# select oid,relname,relfilenode from pg_class where relname='emp';
  oid  | relname | relfilenode
-------+---------+-------------
 16400 | emp     |       24610
(1 row)

testdb=# rollback;
ROLLBACK
testdb=# select oid,relname,relfilenode from pg_class where relname='emp';
  oid  | relname | relfilenode
-------+---------+-------------
 16400 | emp     |       16400
(1 row)

testdb=# select * from emp;
  id  |  name  | position | managerid | yearmomthday | salary | ticheng | salper
------+--------+----------+-----------+--------------+--------+---------+--------
 7369 | SMITH  | CLERK    |      7902 | 1982-12-17   |    800 |         |     20
 7499 | ALLEN  | SALESMAN |      7698 | 1981-02-20   |   1600 |     300 |     20
 7521 | WARD   | SALESMAN |      7698 | 1981-02-22   |   1250 |     500 |     30
 7566 | JONES  | MANAGER  |      7389 | 1981-04-02   |   2975 |         |     20
 7654 | MARTIN | SALESMAN |      7698 | 1982-09-28   |   1250 |    1400 |     30
(5 rows)

truncate会话执行过程中ll -rt |grep 24610能查到relfilenode 24610

[root@centos7 16386]# ll -rt |grep 24610
-rw-------. 1 postgres postgres      0 Oct 29 20:55 24610

不过会话退出后不久,ll -rt |grep 24610就查不到对应的文件了

[root@centos7 16386]# ll -rt |grep 24610
[root@centos7 16386]# 

在会话1执行truncate的过程中只要还没提交,其他会话查询到表的relfilenode不变

postgres=# select oid,relname,relfilenode from pg_class where relname='emp';
  oid  | relname | relfilenode
-------+---------+-------------
 16412 | emp     |       16412

http://www.kler.cn/a/108993.html

相关文章:

  • Oracle RAC的thread
  • LeetCode【0018】四数之和
  • springboot参数校验
  • 【信号处理】基于联合图像表示的深度学习卷积神经网络
  • Kafka 快速入门(一)
  • HTML 基础架构:理解网页的骨架
  • Linux命令解压多个tar.gz包
  • rust学习
  • 关于错误javax.net.ssl.SSLException: Received close_notify during handshake
  • 腾讯云轻量应用服务器地域怎么选择比较好?
  • 两个list中存放相同的对象,一个是页面导入,一个是从数据库查询,外部传入一个集合存放的是对象的属性名称,根据属性名称处理两个list
  • 程序模拟(Concurrency Simulator, ACM/ICPC World Finals 1991, UVa210)rust解法
  • java集合之List接口实现类常用方法详解
  • Gitee 发行版
  • 【音视频】Linux | FFmpeg源码搭建
  • explain查询sql执行计划返回的字段的详细说明
  • LeetCode——哈希表(Java)
  • uni-app中tab选项卡的实现效果 @click=“clickTab(‘sell‘)“事件可传参数
  • No175.精选前端面试题,享受每天的挑战和学习
  • 【算法与数据结构】--算法应用--算法和数据结构的案例研究
  • ubuntu部署个人网盘nextCloud使用docker-compose方式
  • 性能优化必读 | AntDB-M高性能设计之线程池协程模型
  • Docker底层原理:Cgroup V2的使用
  • centos7 部署 Flink
  • 设计模式——单例模式详解
  • 随笔:使用Python爬取知乎上相关问题的所有回答