PostgreSQL serializable read only deferrable事务的用法背景

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 在开始讲serializable read only deferrable用法前,需要先了解一下serializable隔离级别。https://wiki.postgresql.org/wiki/Serializablehttp://www.postgresql.org/docs/9.5/stat.

在开始讲serializable read only deferrable用法前,需要先了解一下serializable隔离级别。
https://wiki.postgresql.org/wiki/Serializable
http://www.postgresql.org/docs/9.5/static/transaction-iso.html#XACT-SERIALIZABLE
http://blog.163.com/digoal@126/blog/static/1638770402013111795219541

serializable是最高的隔离级别,模拟并行事务的串行处理,当不能实现串行一致时,后提交的会话需要回滚,因此serializable事务相互之间是有干扰的。
PostgreSQL在三个地方介绍到了serializable的deferrable用法。
有些时候,用户可能会需要串行一致的视角执行SQL来统计一些数据的报告,这些SQL可能需要运行非常长的时间,但是只需要只读操作。对于运行非常长时间的串行事务,即使是只读的,也可能和之前已经开启并正在执行的其他串行事务产生干扰,从而导致其他串行事务需要回滚。
因此对于这类长时间运行的只读串行事务,我们可以使用一个deferrable模式,这个模式是等待其他的串行事务结束或确保其他正在执行的串行事务不会与当前的串行事务冲突,然后才开始执行只读SQL。
这个时间是不可控的。
但是好处是,当你运行非常长的串行只读事务中的SQL时,不会再感染其他人执行的串行事务了,你爱跑多长时间都行。
在pg_dump中也有用到,看pg_dump 的说明:
(因为pg_dump通常需要长时间运行,使用--serializable-deferrable是指从串行视角来备份数据库,从而得到串行一致的备份数据)
pg_dump默认是使用repeatable read隔离级别来备份的。

       --serializable-deferrable
           Use a serializable transaction for the dump, to ensure that the snapshot used is consistent with later database states; but do this by waiting for a point in the transaction stream at which no anomalies can be present,
           so that there isn't a risk of the dump failing or causing other transactions to roll back with a serialization_failure. See Chapter 13, Concurrency Control, in the documentation for more information about transaction
           isolation and concurrency control.

           This option is not beneficial for a dump which is intended only for disaster recovery. It could be useful for a dump used to load a copy of the database for reporting or other read-only load sharing while the original
           database continues to be updated. Without it the dump may reflect a state which is not consistent with any serial execution of the transactions eventually committed. For example, if batch processing techniques are used,
           a batch may show as closed in the dump without all of the items which are in the batch appearing.

           This option will make no difference if there are no read-write transactions active when pg_dump is started. If read-write transactions are active, the start of the dump may be delayed for an indeterminate length of time.
           Once running, performance with or without the switch is the same.

set transaction语法也支持设置deferrable属性
deferrable值对read only serializable事务生效

http://www.postgresql.org/docs/9.5/static/sql-set-transaction.html

The DEFERRABLE transaction property has no effect unless the transaction is also SERIALIZABLE and READ ONLY. When all three of these properties are selected for a transaction, the transaction may block when first acquiring its snapshot, after which it is able to run without the normal overhead of a SERIALIZABLE transaction and without any risk of contributing to or being canceled by a serialization failure. This mode is well suited for long-running reports or backups.

还有一个参数控制默认的serializable read only事务是否为deferrable。

http://www.postgresql.org/docs/9.5/static/runtime-config-client.html#GUC-DEFAULT-TRANSACTION-DEFERRABLE

default_transaction_deferrable (boolean)
When running at the serializable isolation level, a deferrable read-only SQL transaction may be delayed before it is allowed to proceed. However, once it begins executing it does not incur any of the overhead required to ensure serializability; so serialization code will have no reason to force it to abort because of concurrent updates, making this option suitable for long-running read-only transactions.

This parameter controls the default deferrable status of each new transaction. It currently has no effect on read-write transactions or those operating at isolation levels lower than serializable. The default is off.
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
SQL 缓存 运维
PostgreSQL 事务号回卷分析
## XID 定义 xid 是个啥东西?xid 就是 PostgreSQL 里面的事务号,每个事物都会分配一个 xid。PostgreSQL 数据中每个元组头部都会保存着 插入 或者 删除 这条元组的事务号,即 xid,然后内核通过这个 xid 进行元组的可见性判断。简单理解,比如有两个事务,xid1=200,xid2=201,那么 xid1 中只能看到 t_xmin 200 的元组。 ```c
|
关系型数据库 数据库 PostgreSQL
PostgreSQL 数据库实例只读锁定(readonly) - 硬锁定,软锁定,解锁
PostgreSQL 数据库实例只读锁定(readonly) - 硬锁定,软锁定,解锁
2144 0
|
4月前
|
SQL 存储 关系型数据库
PolarDB-X Operator 基于两次心跳事务的指定时间点恢复方案介绍
本文将介绍,PolarDB-X Operator将在事务策略为XA事务或者TSO事务时,如何实现全局一致的任意时间点恢复,提出了基于两次心跳事务的恢复方案。
|
7月前
|
存储 关系型数据库 数据库
数据库内核那些事|PolarDB X-Engine:如何构建1/10成本的事务存储引擎?
X-Engine引擎是PolarDB为用户提供的低成本,高性价比的解决方案,LSM-tree分层存储结合标准zstd压缩,在性能和成本做到了很好的平衡。在标准sysbench场景下,存储空间相比InnoDB引擎减少60%,读写性能降低10-20%。
数据库内核那些事|PolarDB X-Engine:如何构建1/10成本的事务存储引擎?
|
9月前
|
关系型数据库 PostgreSQL
PostgreSQL事务提交日志与CLOG操作初步认识
PostgreSQL事务提交日志与CLOG操作初步认识
150 0
|
10月前
|
Oracle 关系型数据库 数据库
PostgreSQL技术大讲堂 - 第20讲:事务概述与隔离级别
PostgreSQL从小白到专家,技术大讲堂 - 第20讲:事务概述与隔离级别
188 2
|
11月前
|
SQL 前端开发 关系型数据库
开源分布式数据库PolarDB-X源码解读——PolarDB-X源码解读(十一):事务的一生
开源分布式数据库PolarDB-X源码解读——PolarDB-X源码解读(十一):事务的一生
204 0
|
弹性计算 关系型数据库 数据库
PostgreSQL 数据库实例只读锁定(readonly) - 硬锁定,软锁定,解锁
标签 PostgreSQL , 只读 , 锁定 , readonly , recovery.conf , 恢复模式 , pg_is_in_revoery , default_transaction_read_only 背景 在一些场景中,可能要将数据库设置为只读模式。 例如, 1、云数据库,当使用的容量超过了购买的限制时。切换到只读(锁定)模式,确保用户不会用超。 2、业务上需要对
5247 0
|
SQL 存储 Oracle
PostgreSQL 事务隔离级别的实现和多版本并发控制|学习笔记
快速学习 PostgreSQL 事务隔离级别的实现和多版本并发控制
432 0
PostgreSQL 事务隔离级别的实现和多版本并发控制|学习笔记

相关产品

  • 云原生数据库 PolarDB