PostgreSQL 10.0 preview 功能增强 - 匿名、自治事务(Oracle 兼容性)

简介:

标签

PostgreSQL , 10.0 , 匿名事务 , 自治事务


背景

PostgreSQL 10.0 通过session backendground实现了匿名事务,从此可以愉快的支持Oracle存储过程的自治事务了。

此前,我们需要通过dblink实现,或者通过匿名块+exception来实现,比较繁琐。

《PostgreSQL Oracle 兼容性之 - plpgsql 自治事务(autonomous_transaction)补丁》

《PostgreSQL Oracle 兼容性之 - 函数 自治事务 的写法和实现》

I would like to propose the attached patch implementing autonomous  
transactions for discussion and review.  
  
This work was mostly inspired by the discussion about pg_background a  
while back [0].  It seemed that most people liked the idea of having  
something like that, but couldn't perhaps agree on the final interface.  
Most if not all of the preliminary patches in that thread were  
committed, but the user interface portions were then abandoned in favor  
of other work.  (I'm aware that rebased versions of pg_background  
existing.  I have one, too.)  
  
The main use case, in a nutshell, is to be able to commit certain things  
independently without having it affected by what happens later to the  
current transaction, for example for audit logging.  
  
My patch consists of three major pieces.  (I didn't make them three  
separate patches because it will be clear where the boundaries are.)  
  
- A API interface to open a "connection" to a background worker, run  
queries, get results: AutonomousSessionStart(), AutonomousSessionEnd(),  
AutonomousSessionExecute(), etc.  The communication happens using the  
client/server protocol.  
  
- Patches to PL/pgSQL to implement Oracle-style autonomous transaction  
blocks:  
  
AS $$  
DECLARE  
  PRAGMA AUTONOMOUS_TRANSACTION;  
BEGIN  
  FOR i IN 0..9 LOOP  
    START TRANSACTION;  
    INSERT INTO test1 VALUES (i);  
    IF i % 2 = 0 THEN  
        COMMIT;  
    ELSE  
        ROLLBACK;  
    END IF;  
  END LOOP;  
  
  RETURN 42;  
END;  
$$;  
  
This is very incomplete and has some open technical issues that I will  
discuss below.  But those are all issues of PL/pgSQL, not really issues  
of how autonomous sessions work.  
  
Basically, a block that is declared with that pragma uses the autonomous  
C API instead of SPI to do its things.  
  
- Patches to PL/Python to implement a context manager for autonomous  
sessions (similar to how subtransactions work there):  
  
with plpy.autonomous() as a:  
    for i in range(0, 10):  
        a.execute("BEGIN")  
        a.execute("INSERT INTO test1 (a) VALUES (%d)" % i)  
        if i % 2 == 0:  
            a.execute("COMMIT")  
        else:  
            a.execute("ROLLBACK")  
  
This works quite well, except perhaps some tuning with memory management  
and some caching and some refactoring.  
  
While the PL/pgSQL work is more of a top-level goal, I added the  
PL/Python implementation because it is easier to map the C API straight  
out to something more accessible, so testing it out is much easier.  
  
  
The main technical problem I had with PL/pgSQL is how to parse named  
parameters.  If you're in PL/Python, say, you do  
  
    plan = a.prepare("INSERT INTO test1 (a, b) VALUES ($1, $2)",  
                     ["int4", "text"])  
  
and that works fine, because it maps straight to the client/server  
protocol.  But in PL/pgSQL, you will want something like  
  
    DECLARE  
      x, y ...  
    BEGIN  
      INSERT INTO test1 (a, b) VALUES (x, y)  
  
When running in-process (SPI), we install parser hooks that allow the  
parser to check back into PL/pgSQL about whether x, y are variables and  
what they mean.  When we run in an autonomous session, we don't have  
that available.  So my idea was to extend the protocol Parse message to  
allow sending a symbol table instead of parameter types.  So instead of  
saying, there are two parameters and here are their types, I would send  
a list of symbols and types, and the server would respond to the Parse  
message with some kind of information about which symbols it found.  I  
think that would work, but I got lost in the weeds and didn't get very  
far.  But you can see some of that in the code.  If anyone has other  
ideas, I'd be very interested.  
  
  
Other than that, I think there are also other bits and pieces that are  
worth looking at, and perhaps have some overlap with other efforts, such as:  
  
- Refining the internal APIs for running queries, with more flexibility  
than SPI.  There have recently been discussions about that.  I just used  
whatever was in tcop/postgres.c directly, like pg_background does, and  
that seems mostly fine, but if there are other ideas, they would be  
useful for this, too.  
  
- An exception to the "mostly fine" is that the desirable handling of  
log_statement, log_duration, log_min_duration_statement for  
non-top-level execution is unclear.  
  
- The autonomous session API could also be useful for other things, such  
as perhaps implementing a variant of pg_background on top of them, or  
doing other asynchronous or background execution schemes.  So input on  
that is welcome.  
  
- There is some overlap with the protocol handling for parallel query,  
including things like error propagation, notify handling, encoding  
handling.  I suspect that other background workers will need similar  
facilities, so we could simplify some of that.  
  
- Client encoding in particular was recently discussed for parallel  
query.  The problem with the existing solution is that it makes  
assign_client_encoding() require hardcoded knowledge of all relevant  
background worker types.  So I tried a more general solution, with a hook.  
  
- I added new test files in the plpgsql directory.  The main test for  
plpgsql runs as part of the main test suite.  Maybe we want to move that  
to the plpgsql directory as well.  
  
- More guidance for using some of the background worker and shared  
memory queue facilities.  For example, I don't know what a good queue  
size would be.  
  
- Both PL/pgSQL and PL/Python expose some details of SPI in ways that  
make it difficult to run some things not through SPI.  For example,  
return codes are exposed directly by PL/Python.  PL/pgSQL is heavily  
tied to the API flow of SPI.  It's fixable, but it will be some work.  I  
had originally wanted to hide the autonomous session API inside SPI or  
make it fully compatible with SPI, but that was quickly thrown out.  
PL/Python now contains some ugly code to make certain things match up so  
that existing code can be used.  It's not always pretty.  
  
- The patch "Set log_line_prefix and application name in test drivers"  
(https://commitfest.postgresql.org/10/717/) is helpful in testing and  
debugging this.  
  
  
[0]:  
https://www.postgresql.org/message-id/flat/CA+Tgmoam66dTzCP8N2cRcS6S6dBMFX+JMba+mDf68H=KAkNjPQ(at)mail(dot)gmail(dot)com  
  
--   
Peter Eisentraut              http://www.2ndQuadrant.com/  
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services  

这个patch的讨论,详见邮件组,本文末尾URL。

PostgreSQL社区的作风非常严谨,一个patch可能在邮件组中讨论几个月甚至几年,根据大家的意见反复的修正,patch合并到master已经非常成熟,所以PostgreSQL的稳定性也是远近闻名的。

参考

https://commitfest.postgresql.org/13/873/

https://www.postgresql.org/message-id/flat/659a2fce-b6ee-06de-05c0-c8ed6a01979e@2ndquadrant.com#659a2fce-b6ee-06de-05c0-c8ed6a01979e@2ndquadrant.com

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

相关产品

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

    更多