ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

lightdb关闭事务自动提交

2022-06-11 21:03:06  阅读:133  来源: 互联网

标签:lut postgres zjh 提交 关闭 test id select lightdb


  在lightdb/pg中,事务是自动提交的,也就是除非明确通过BEGIN;开启事务,否则任何语句都是独立事务。在ltsql客户端,可以通过\echo :AUTOCOMMIT查看自动提交模式,如下:

[zjh@hs-10-20-30-193 ~]$ ltsql -p9999
ltsql (13.3-22.2)
Type "help" for help.

zjh@postgres=# \echo :AUTOCOMMIT
on
zjh@postgres=# \set AUTOCOMMIT off
zjh@postgres=# \echo :AUTOCOMMIT
off
zjh@postgres=# 

  因为ltsql是客户端工具,不是libpq/ltjdbc实现,所以它不具典型意义。

  强制自动提交,无参数控制有个缺点,对于一些oracle/mysql不会提交的场景也会自动提交,而我们认为它不会提交。如explain analyze,如下:

zjh@postgres=*# select count(1) from scott.departments where dept_id<10000;
 count 
-------
    64
(1 row)

zjh@postgres=*# explain analyze update scott.departments set dept_id=dept_id+10000 where dept_id<10000;
                                                       QUERY PLAN                                                        
-------------------------------------------------------------------------------------------------------------------------
 Update on departments  (cost=0.00..54236.67 rows=396542 width=236) (actual time=688.890..688.892 rows=0 loops=1)
   ->  Seq Scan on departments  (cost=0.00..54236.67 rows=396542 width=236) (actual time=0.015..688.719 rows=64 loops=1)
         Filter: (dept_id < 10000)
         Rows Removed by Filter: 6024736
 Planning Time: 0.089 ms
 Execution Time: 689.190 ms
(6 rows)

zjh@postgres=*# select count(1) from scott.departments where dept_id<10000;
 count 
-------
     0
(1 row)

  explain analyze实际上被提交了,而如果数据被修改后和原来的数据不唯一,就可能无法恢复。这是非常麻烦的。

  要正确执行更新,需要先BEGIN,然后rollback,如下:

zjh@postgres=# begin;
BEGIN
zjh@postgres=*# select count(1) from scott.departments where dept_id<20000;
 count 
-------
    64
(1 row)

zjh@postgres=*# explain analyze update scott.departments set dept_id=dept_id+10000 where dept_id<20000;
                                                        QUERY PLAN                                                         
---------------------------------------------------------------------------------------------------------------------------
 Update on departments  (cost=0.00..54236.67 rows=396542 width=236) (actual time=694.907..694.908 rows=0 loops=1)
   ->  Seq Scan on departments  (cost=0.00..54236.67 rows=396542 width=236) (actual time=694.636..694.654 rows=64 loops=1)
         Filter: (dept_id < 20000)
         Rows Removed by Filter: 6024736
 Planning Time: 0.064 ms
 Execution Time: 695.004 ms
(6 rows)

zjh@postgres=*# rollback;
ROLLBACK
zjh@postgres=# select count(1) from scott.departments where dept_id<20000;
 count 
-------
    64
(1 row)

  因为这很难控制,因此lightdb在内核中基于隐藏字段实现自动更新时间戳,能够记录行最后更新时间,这样能够做到恢复。如下:

zjh@postgres=# create table test_lut(id int) WITH UPDATE CURRENT_TIMESTAMP;
WARNING:  LightDB DDL check warn! keyword not allowed with [column name]:id
WARNING:  LightDB DDL check warn! no primary key!
WARNING:  LightDB DDL check warn! no gmt_create or no gmt_modified!
CREATE TABLE
zjh@postgres=# insert into test_lut values(1);
INSERT 0 1
zjh@postgres=# select * from test_lut ;
 id 
----
  1
(1 row)

zjh@postgres=# \dS+ test_lut
                                                Table "public.test_lut"
 Column |            Type             | Collation | Nullable |      Default      | Storage | Stats target | Description 
--------+-----------------------------+-----------+----------+-------------------+---------+--------------+-------------
 id     | integer                     |           |          |                   | plain   |              | 
 ltaut  | timestamp without time zone |           |          | CURRENT_TIMESTAMP | plain   |              | 
Access method: heap

zjh@postgres=# select id,ltaut from test_lut ;
 id |           ltaut            
----+----------------------------
  1 | 2022-06-11 20:53:47.295945
(1 row)

zjh@postgres=# update test_lut set id=id+1;
UPDATE 1
zjh@postgres=# select id,ltaut from test_lut ;
 id |           ltaut            
----+----------------------------
  2 | 2022-06-11 20:54:33.041471
(1 row)

 

标签:lut,postgres,zjh,提交,关闭,test,id,select,lightdb
来源: https://www.cnblogs.com/zhjh256/p/16366789.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有