PostgreSQL 子事务 id & command id 溢出问题分析

本文涉及的产品
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
RDS AI 助手,专业版
简介: PostgreSQL 需要为每个savepoint或者函数的exception section分配子事务号,递增。 即使这个exception没有被触发,也需要一个子事务号。 PushTransaction@src/backend/access/transam/xact.c         /

PostgreSQL 需要为每个savepoint或者函数的exception section分配子事务号,递增。

即使这个exception没有被触发,也需要一个子事务号。

PushTransaction@src/backend/access/transam/xact.c

        /*

         * Assign a subtransaction ID, watching out for counter wraparound.

         */

        currentSubTransactionId += 1;

        if (currentSubTransactionId == InvalidSubTransactionId)

        {

                currentSubTransactionId -= 1;

                pfree(s);

                ereport(ERROR,

                                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),

                                 errmsg("cannot have more than 2^32-1 subtransactions in a transaction")));

        }


command id则是记录一个事务中产生写操作(例如ddl,dml)的SQL ID,递增。

CommandCounterIncrement@src/backend/access/transam/xact.c

        if (currentCommandIdUsed)

        {

                currentCommandId += 1;

                if (currentCommandId == InvalidCommandId)

                {

                        currentCommandId -= 1;

                        ereport(ERROR,

                                        (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),

                                         errmsg("cannot have more than 2^32-2 commands in a transaction")));

                }

                currentCommandIdUsed = false;


子事务 id和command id都是unsigned int类型,最大允许分配2^32-1个子事务,单个事务中最大允许分配2^32-2条COMMAND。

typedef uint32 SubTransactionId;

typedef uint32 CommandId;


子事务什么情况下可能溢出呢?

1. 在事务中累计使用的savepoint = n。

2. 在事务中有exception的函数,每个exception需要申请一个子事务,如果函数被多次调用,则需要计算多个子事务。假设函数exception需要的子事务个数=m。

如果n+m大于2^32-1,溢出。


command id什么情况下可能溢出呢?

一个事务中,包含的ddl,dml SQL超过2^32-2时。


跟踪方法:

                currentCommandId += 1;

// 添加如下

                ereport(NOTICE,

                        (errmsg("currentCommandId: %d", currentCommandId)));

                if (currentCommandId == InvalidCommandId)

                {

                        currentCommandId -= 1;

                        ereport(ERROR,

                                        (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),

                                         errmsg("cannot have more than 2^32-2 commands in a transaction")));

                }

                currentCommandIdUsed = false;


...

        /*

         * Assign a subtransaction ID, watching out for counter wraparound.

         */

        currentSubTransactionId += 1;

// 添加如下

        ereport(NOTICE,

                (errmsg("currentSubTransactionId: %d", currentSubTransactionId)));

        if (currentSubTransactionId == InvalidSubTransactionId)

        {

                currentSubTransactionId -= 1;

                pfree(s);

                ereport(ERROR,

                                (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),

                                 errmsg("cannot have more than 2^32-1 subtransactions in a transaction")));

        }


重新编译安装,重启数据库。

psql

设置notice消息级别

postgres=# set client_min_messages='notice';

SET

创建测试函数

postgres=# create or replace function f() returns void as $$

declare

begin

exception           

  when others then

  raise exception 'a';

end;                  

$$ language plpgsql;

测试子事务号申请。

postgres=# select f();

NOTICE:  currentSubTransactionId: 2

CONTEXT:  PL/pgSQL function f() line 3 during statement block entry

 f 

---

 

(1 row)

volatile函数,每条tuple都会触发调用

postgres=# select f() from generate_series(1,10);

NOTICE:  currentSubTransactionId: 2

CONTEXT:  PL/pgSQL function f() line 3 during statement block entry

NOTICE:  currentSubTransactionId: 3

CONTEXT:  PL/pgSQL function f() line 3 during statement block entry

NOTICE:  currentSubTransactionId: 4

CONTEXT:  PL/pgSQL function f() line 3 during statement block entry

NOTICE:  currentSubTransactionId: 5

CONTEXT:  PL/pgSQL function f() line 3 during statement block entry

NOTICE:  currentSubTransactionId: 6

CONTEXT:  PL/pgSQL function f() line 3 during statement block entry

NOTICE:  currentSubTransactionId: 7

CONTEXT:  PL/pgSQL function f() line 3 during statement block entry

NOTICE:  currentSubTransactionId: 8

CONTEXT:  PL/pgSQL function f() line 3 during statement block entry

NOTICE:  currentSubTransactionId: 9

CONTEXT:  PL/pgSQL function f() line 3 during statement block entry

NOTICE:  currentSubTransactionId: 10

CONTEXT:  PL/pgSQL function f() line 3 during statement block entry

NOTICE:  currentSubTransactionId: 11

CONTEXT:  PL/pgSQL function f() line 3 during statement block entry

没有exception的话,不会产生子事务。

postgres=# select * from generate_series(1,10);

 generate_series 

-----------------

               1

               2

               3

               4

               5

               6

               7

               8

               9

              10

(10 rows)

postgres=# create or replace function f1() returns void as $$

postgres$# declare

postgres$# begin

postgres$# end;

postgres$# $$ language plpgsql;

NOTICE:  currentCommandId: 1

CREATE FUNCTION

postgres=# select f1() from generate_series(1,10);

 f1 

----

 

 

 

 

 

 

 

 

 

 

(10 rows)


接下来跟踪一下command id:

DDL,DML会产生command

postgres=# create table t(id int);

NOTICE:  currentCommandId: 1

CREATE TABLE

postgres=# insert into t values (1);

NOTICE:  currentCommandId: 2

INSERT 0 1

postgres=# insert into t values (1);

NOTICE:  currentCommandId: 3

INSERT 0 1

查询不需要分配command id

postgres=# select 1;

 ?column? 

----------

        1

(1 row)

savepoint 产生子事务

postgres=# savepoint a;

NOTICE:  currentSubTransactionId: 12

SAVEPOINT

postgres=# savepoint a;

NOTICE:  currentSubTransactionId: 13

SAVEPOINT

postgres=# savepoint a;

NOTICE:  currentSubTransactionId: 14

SAVEPOINT

postgres=# savepoint a;

NOTICE:  currentSubTransactionId: 15

SAVEPOINT

rollback to savepoint 产生子事务

postgres=# rollback to savepoint a;

NOTICE:  currentSubTransactionId: 16

ROLLBACK

postgres=# rollback to savepoint a;

NOTICE:  currentSubTransactionId: 17

ROLLBACK

postgres=# rollback to savepoint a;

NOTICE:  currentSubTransactionId: 18

ROLLBACK

postgres=# rollback to savepoint a;

NOTICE:  currentSubTransactionId: 19

ROLLBACK

postgres=# rollback to savepoint a;

NOTICE:  currentSubTransactionId: 20

ROLLBACK

postgres=# rollback to savepoint a;

NOTICE:  currentSubTransactionId: 21

ROLLBACK

postgres=# rollback to savepoint a;

NOTICE:  currentSubTransactionId: 22

ROLLBACK

postgres=# rollback to savepoint a;

NOTICE:  currentSubTransactionId: 23

ROLLBACK

postgres=# rollback to savepoint a;

NOTICE:  currentSubTransactionId: 24

ROLLBACK

postgres=# rollback to savepoint a;

NOTICE:  currentSubTransactionId: 25

ROLLBACK

postgres=# rollback to savepoint a;

NOTICE:  currentSubTransactionId: 26

ROLLBACK

postgres=# rollback to savepoint a;

NOTICE:  currentSubTransactionId: 27

ROLLBACK

postgres=# rollback to savepoint a;

NOTICE:  currentSubTransactionId: 28

ROLLBACK

postgres=# rollback to savepoint a;

NOTICE:  currentSubTransactionId: 29

ROLLBACK

postgres=# rollback to savepoint a;

NOTICE:  currentSubTransactionId: 30

ROLLBACK

postgres=# end;

COMMIT


没有exception的函数不产生子事务:

postgres=# create or replace function f() returns void as $$

declare

begin

end;                          

$$ language plpgsql;

NOTICE:  currentCommandId: 1

CREATE FUNCTION

postgres=# select f();

 f 

---

 

(1 row)

每个exception都需要分配一个子事务:

create or replace function f() returns void as $$

declare

begin


begin

exception when others then

return; 

end;   


begin

exception when others then

return; 

end;   


exception when others then

return;

end;

$$ language plpgsql;

postgres=# select f();

NOTICE:  currentSubTransactionId: 2

CONTEXT:  PL/pgSQL function f() line 3 during statement block entry

NOTICE:  currentSubTransactionId: 3

CONTEXT:  PL/pgSQL function f() line 6 during statement block entry

NOTICE:  currentSubTransactionId: 4

CONTEXT:  PL/pgSQL function f() line 11 during statement block entry

 f 

---

 

(1 row)


溢出的例子:

postgres=# select count(*) from (select f(),f(),f(),f(),f(),f(),f(),f(),f(),f(),f(),f(),f(),f(),f(),f(),f(),f(),f(),f(),f(),f(),f() from generate_series(1,500000000))t;

ERROR:  cannot have more than 2^32-1 subtransactions in a transaction

CONTEXT:  PL/pgSQL function f() line 3 during statement block entry


顺带讲一下函数稳定性,以前写过分享。

stable和volatile在一条SQL中,每条tuple都会被触发(实际上stable当传参一样时,不应该被多次触发,这是PG的一个问题)。

immutable则在任何情况下都只调用一次,和stable区别还有,在使用绑定变量时,immutable会自动转换成常量。

postgres=# alter function f() immutable;

ALTER FUNCTION

仅仅触发一次

postgres=# select f() from generate_series(1,100);

NOTICE:  currentSubTransactionId: 2

CONTEXT:  PL/pgSQL function f() line 3 during statement block entry

NOTICE:  currentSubTransactionId: 3

CONTEXT:  PL/pgSQL function f() line 6 during statement block entry

NOTICE:  currentSubTransactionId: 4

CONTEXT:  PL/pgSQL function f() line 11 during statement block entry

 f 

---

改为stable触发多次

postgres=# alter function f() stable;

ALTER FUNCTION

postgres=# select f() from generate_series(1,100);

NOTICE:  currentSubTransactionId: 2

CONTEXT:  PL/pgSQL function f() line 3 during statement block entry

NOTICE:  currentSubTransactionId: 3

CONTEXT:  PL/pgSQL function f() line 6 during statement block entry

NOTICE:  currentSubTransactionId: 4

CONTEXT:  PL/pgSQL function f() line 11 during statement block entry

NOTICE:  currentSubTransactionId: 5

CONTEXT:  PL/pgSQL function f() line 3 during statement block entry

NOTICE:  currentSubTransactionId: 6

,,,,,,




相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
4月前
|
SQL 关系型数据库 MySQL
MySQL锁机制:并发控制与事务隔离
本文深入解析了MySQL的锁机制与事务隔离级别,涵盖锁类型、兼容性、死锁处理及性能优化策略,助你掌握高并发场景下的数据库并发控制核心技巧。
|
5月前
|
存储 监控 Oracle
MySQL事务
MySQL事务具有ACID特性,包括原子性、一致性、隔离性和持久性。其默认隔离级别为可重复读,通过MVCC和间隙锁解决幻读问题,确保事务间数据的一致性和并发性。
MySQL事务
|
6月前
|
存储 SQL 关系型数据库
mysql底层原理:索引、慢查询、 sql优化、事务、隔离级别、MVCC、redolog、undolog(图解+秒懂+史上最全)
mysql底层原理:索引、慢查询、 sql优化、事务、隔离级别、MVCC、redolog、undolog(图解+秒懂+史上最全)
mysql底层原理:索引、慢查询、 sql优化、事务、隔离级别、MVCC、redolog、undolog(图解+秒懂+史上最全)
|
3月前
|
关系型数据库 MySQL 数据库
【赵渝强老师】MySQL的事务隔离级别
数据库并发访问时易引发数据不一致问题。如客户端读取到未提交的事务数据,可能导致“脏读”。MySQL通过四种事务隔离级别(读未提交、读已提交、可重复读、可序列化)控制并发行为,默认为“可重复读”,以平衡性能与数据一致性。
317 0
|
4月前
|
关系型数据库 MySQL 数据库
MySql事务以及事务的四大特性
事务是数据库操作的基本单元,具有ACID四大特性:原子性、一致性、隔离性、持久性。它确保数据的正确性与完整性。并发事务可能引发脏读、不可重复读、幻读等问题,数据库通过不同隔离级别(如读未提交、读已提交、可重复读、串行化)加以解决。MySQL默认使用可重复读级别。高隔离级别虽能更好处理并发问题,但会降低性能。
208 0
|
6月前
|
安全 关系型数据库 MySQL
mysql事务隔离级别
事务隔离级别用于解决脏读、不可重复读和幻读问题。不同级别在安全与性能间权衡,如SERIALIZABLE最安全但性能差,READ_UNCOMMITTED性能高但易导致数据不一致。了解各级别特性有助于合理选择以平衡并发性与数据一致性需求。
205 1
|
SQL 安全 关系型数据库
【MySQL基础篇】事务(事务操作、事务四大特性、并发事务问题、事务隔离级别)
事务是MySQL中一组不可分割的操作集合,确保所有操作要么全部成功,要么全部失败。本文利用SQL演示并总结了事务操作、事务四大特性、并发事务问题、事务隔离级别。
5267 56
【MySQL基础篇】事务(事务操作、事务四大特性、并发事务问题、事务隔离级别)
|
SQL 关系型数据库 MySQL
MySQL事务日志-Undo Log工作原理分析
事务的持久性是交由Redo Log来保证,原子性则是交由Undo Log来保证。如果事务中的SQL执行到一半出现错误,需要把前面已经执行过的SQL撤销以达到原子性的目的,这个过程也叫做"回滚",所以Undo Log也叫回滚日志。
707 7
MySQL事务日志-Undo Log工作原理分析
|
存储 SQL 关系型数据库
MySQL的事务隔离级别
【10月更文挑战第17天】MySQL的事务隔离级别
308 43
|
存储 缓存 关系型数据库
MySQL事务日志-Redo Log工作原理分析
事务的隔离性和原子性分别通过锁和事务日志实现,而持久性则依赖于事务日志中的`Redo Log`。在MySQL中,`Redo Log`确保已提交事务的数据能持久保存,即使系统崩溃也能通过重做日志恢复数据。其工作原理是记录数据在内存中的更改,待事务提交时写入磁盘。此外,`Redo Log`采用简单的物理日志格式和高效的顺序IO,确保快速提交。通过不同的落盘策略,可在性能和安全性之间做出权衡。
2426 14
MySQL事务日志-Redo Log工作原理分析

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版
  • 推荐镜像

    更多