iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >怎么理解PostgreSQL事务管理
  • 444
分享到

怎么理解PostgreSQL事务管理

2024-04-02 19:04:59 444人浏览 泡泡鱼
摘要

本篇内容介绍了“怎么理解postgresql事务管理”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、Th

本篇内容介绍了“怎么理解postgresql事务管理”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

一、The Transaction System

README

src/backend/access/transam/README
The Transaction System
======================
事务系统
Postgresql's transaction system is a three-layer system.  The bottom layer
implements low-level transactions and subtransactions, on top of which rests
the mainloop's control code, which in turn implements user-visible
transactions and savepoints.
PostgreSQL的事务分为3层,底层实现了低层次的事务和子事务,在其顶上驻留主循环控制代码,
而主循环实现了用户可见性事务和保存点.
The middle layer of code is called by postgres.c before and after the
processing of each query, or after detecting an error:
        StartTransactionCommand
        CommitTransactionCommand
        AbortCurrentTransaction
Meanwhile, the user can alter the system's state by issuing the SQL commands
BEGIN, COMMIT, ROLLBACK, SAVEPOINT, ROLLBACK TO or RELEASE.  The traffic cop
redirects these calls to the toplevel routines
        BeginTransactionBlock
        EndTransactionBlock
        UserAbortTransactionBlock
        DefineSavepoint
        RollbackToSavepoint
        ReleaseSavepoint
respectively.  Depending on the current state of the system, these functions
call low level functions to activate the real transaction system:
        StartTransaction
        CommitTransaction
        AbortTransaction
        CleanupTransaction
        StartSubTransaction
        CommitSubTransaction
        AbortSubTransaction
        CleanupSubTransaction
在处理查询的前后或者检测到错误时,postgres.c会调用中间层的代码:
        StartTransactionCommand
        CommitTransactionCommand
        AbortCurrentTransaction
在此期间,通过执行BEGIN/COMMIT/ROLLBACK/SAVEPOINT/ROLLBACK TO/RELEASE命令改变系统状态.
调度程序会把这些调用重定向至相应的顶层例程上.
        BeginTransactionBlock
        EndTransactionBlock
        UserAbortTransactionBlock
        DefineSavepoint
        RollbackToSavepoint
        ReleaseSavepoint
依赖于当前的系统状态,这些函数调用底层函数激活真正的事务系统:
        StartTransaction
        CommitTransaction
        AbortTransaction
        CleanupTransaction
        StartSubTransaction
        CommitSubTransaction
        AbortSubTransaction
        CleanupSubTransaction
Additionally, within a transaction, CommandCounterIncrement is called to
increment the command counter, which allows future commands to "see" the
effects of previous commands within the same transaction.  Note that this is
done automatically by CommitTransactionCommand after each query inside a
transaction block, but some utility functions also do it internally to allow
some operations (usually in the system catalogs) to be seen by future
operations in the same utility command.  (For example, in DefineRelation it is
done after creating the heap so the pg_class row is visible, to be able to
lock it.)
另外,在事务内,调用CommandCounterIncrement增加命令计数,这可以让未来的命令可以看到
在同一个事务中先前命令的影响.
注意该动作由CommitTransactionCommand在事务块内部完成每个查询后自动完成,
但某些工具函数同样会内部实现此功能以允许某些操作(通常在系统目录中)可被未来同样的工具命令看到.
(比如,在DefineRelation,在创建堆后已完成,因此pg_class中的行已可见,并能执行定)
For example, consider the following sequence of user commands:
举个例子,考虑下面一组用户命令:
1)        BEGIN
2)        SELECT * FROM foo
3)        INSERT INTO foo VALUES (...)
4)        COMMIT
In the main processing loop, this results in the following function call
sequence:
在主处理循环,会形成下面函数调用序列:
     /  StartTransactionCommand;     -- middle
    /       StartTransaction;         -- bottom
1) <    ProcessUtility;                 << BEGIN
    \       BeginTransactionBlock;    -- top
     \  CommitTransactionCommand;    -- middle
    /   StartTransactionCommand;    -- middle
2) /    PortalRunSelect;                << SELECT ...
   \    CommitTransactionCommand;    -- middle
    \       CommandCounterIncrement;
    /   StartTransactionCommand;    -- middle
3) /    ProcessQuery;                   << INSERT ...
   \    CommitTransactionCommand;    -- middle
    \       CommandCounterIncrement;
     /  StartTransactionCommand;    -- middle
    /   ProcessUtility;                 << COMMIT
4) <        EndTransactionBlock;    -- top
    \   CommitTransactionCommand;    -- middle
     \      CommitTransaction;        -- bottom
The point of this example is to demonstrate the need for
StartTransactionCommand and CommitTransactionCommand to be state smart -- they
should call CommandCounterIncrement between the calls to BeginTransactionBlock
and EndTransactionBlock and outside these calls they need to do nORMal start,
commit or abort processing.
该例子想表达的意思是StartTransactionCommand和CommitTransactionCommand需要具备状态智能
-- 在BeginTransactionBlock/EndTransactionBlock之间需调用CommandCounterIncrement,
在这些调用之外,它们需要执行常规的start,commit或abort处理.
Furthermore, suppose the "SELECT * FROM foo" caused an abort condition. In
this case AbortCurrentTransaction is called, and the transaction is put in
aborted state.  In this state, any user input is ignored except for
transaction-termination statements, or ROLLBACK TO <savepoint> commands.
而且,假定"SELECT * FROM foo"出错,导致需要abort,那么会调用AbortCurrentTransaction(bottom),
事务状态为aborted状态.事务处于这个状态,除了事务终止语句或者ROLLBACK TO <savepoint>命令外,所有用户输入都会被忽略.
Transaction aborts can occur in two ways:
事务取消的情况有两种:
1) system dies from some internal cause  (syntax error, etc)
   内部原因,如语法错误等.
2) user types ROLLBACK
   用户类型的ROLLBACK.
The reason we have to distinguish them is illustrated by the following two
situations:
区分事务取消的原因如下两例所示:
        case 1                                  case 2
        ------                                  ------
1) user types BEGIN                     1) user types BEGIN
2) user does something                  2) user does something
3) user does not like what              3) system aborts for some reason
   she sees and types ABORT                (syntax error, etc)
In case 1, we want to abort the transaction and return to the default state.
In case 2, there may be more commands coming our way which are part of the
same transaction block; we have to ignore these commands until we see a COMMIT
or ROLLBACK.
第一种情况,用户希望取消事务并返回到默认状态.
第二种情况,在同一个事务块中,可能会有更多的命令进入,需要忽略这些命令直至COMMIT/ROLLBACK.
Internal aborts are handled by AbortCurrentTransaction, while user aborts are
handled by UserAbortTransactionBlock.  Both of them rely on AbortTransaction
to do all the real work.  The only difference is what state we enter after
AbortTransaction does its work:
* AbortCurrentTransaction leaves us in TBLOCK_ABORT,
* UserAbortTransactionBlock leaves us in TBLOCK_ABORT_END
内部的事务取消通过AbortCurrentTransaction(bottom)处理,而用户取消通过UserAbortTransactionBlock(top)处理.
它们都需要依赖AbortTransaction(bottom)来处理实际的工作,不同的地方是在AbortTransaction后进入的状态不同:
* AbortCurrentTransaction进入TBLOCK_ABORT
* UserAbortTransactionBlock进入TBLOCK_ABORT_END
Low-level transaction abort handling is divided in two phases:
* AbortTransaction executes as soon as we realize the transaction has
  failed.  It should release all shared resources (locks etc) so that we do
  not delay other backends unnecessarily.
* CleanupTransaction executes when we finally see a user COMMIT
  or ROLLBACK command; it cleans things up and gets us out of the transaction
  completely.  In particular, we mustn't destroy TopTransactionContext until
  this point.
底层事务取消处理分为两个阶段:
* 一旦感知事务已失败,则马上执行AbortTransaction,需要释放所有的共享资源(比如锁等)以便不影响其他后台进程.
* 在用户发出COMMIT/ROLLBACK时执行CleanupTransaction;清理现场并完整的跳出事务.
  特别地,在这个点上才需要销毁TopTransactionContext
Also, note that when a transaction is committed, we don't close it right away.
Rather it's put in TBLOCK_END state, which means that when
CommitTransactionCommand is called after the query has finished processing,
the transaction has to be closed.  The distinction is subtle but important,
because it means that control will leave the xact.c code with the transaction
open, and the main loop will be able to keep processing inside the same
transaction.  So, in a sense, transaction commit is also handled in two
phases, the first at EndTransactionBlock and the second at
CommitTransactionCommand (which is where CommitTransaction is actually
called).
同时,注意如果事务已提交,必须要马上关闭,而是进入TBLOCK_END状态,
这意味着在查询完成后执行CommitTransactionCommand,事务才会关闭.
这种区别很微妙,但很重要,因为控制已在事务开启的情况下从xact.c代码中跳出,主循环仍在相同的主事务中.
因此,在某种意义上来说,事务提交存在两个阶段,首先EndTransactionBlock(top),其次CommitTransactionCommand(middle).
(CommitTransactionCommand是实际调用CommitTransaction的地方)
The rest of the code in xact.c are routines to support the creation and
finishing of transactions and subtransactions.  For example, AtStart_Memory
takes care of initializing the memory subsystem at main transaction start.
xact.c的剩余代码是用于支持创建和结束事务和子事务的例程.
比如AtStart_Memory在主事务开启时处理初始化内存子系统.

TransactionState结构体


typedef enum TransState
{
    TRANS_DEFAULT,                
    TRANS_START,                
    TRANS_INPROGRESS,            
    TRANS_COMMIT,                
    TRANS_ABORT,                
    TRANS_PREPARE                
} TransState;

typedef enum TBlockState
{
    
    TBLOCK_DEFAULT,                
    TBLOCK_STARTED,                
    
    TBLOCK_BEGIN,                
    TBLOCK_INPROGRESS,            
    TBLOCK_IMPLICIT_INPROGRESS, 
    TBLOCK_PARALLEL_INPROGRESS, 
    TBLOCK_END,                    
    TBLOCK_ABORT,                
    TBLOCK_ABORT_END,            
    TBLOCK_ABORT_PENDING,        
    TBLOCK_PREPARE,                
    
    TBLOCK_SUBBEGIN,            
    TBLOCK_SUBINPROGRESS,        
    TBLOCK_SUBRELEASE,            
    TBLOCK_SUBCOMMIT,            
    TBLOCK_SUBABORT,            
    TBLOCK_SUBABORT_END,        
    TBLOCK_SUBABORT_PENDING,    
    TBLOCK_SUBRESTART,            
    TBLOCK_SUBABORT_RESTART        
} TBlockState;

typedef struct TransactionStateData
{
    FullTransactionId fullTransactionId;    
    SubTransactionId subTransactionId;    
    char       *name;            
    int            savepointLevel; 
    TransState    state;            
    TBlockState blockState;        
    int            nestingLevel;    
    int            gucNestLevel;    
    MemoryContext curTransactionContext;    
    ResourceOwner curTransactionOwner;    
    TransactionId *childXids;    
    int            nChildXids;        
    int            maxChildXids;    
    Oid            prevUser;        
    int            prevSecContext; 
    bool        prevXactReadOnly;    
    bool        startedInRecovery;    
    bool        didLogXid;        
    int            parallelModeLevel;    
    bool        chain;            
    struct TransactionStateData *parent;    
} TransactionStateData;
typedef TransactionStateData *TransactionState;

“怎么理解PostgreSQL事务管理”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!

您可能感兴趣的文档:

--结束END--

本文标题: 怎么理解PostgreSQL事务管理

本文链接: https://www.lsjlt.com/news/63615.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 怎么理解PostgreSQL事务管理
    本篇内容介绍了“怎么理解PostgreSQL事务管理”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!一、Th...
    99+
    2022-10-18
  • 怎么理解PostgreSQL事务管理中的子事务
    本篇内容主要讲解“怎么理解PostgreSQL事务管理中的子事务”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么理解PostgreSQL事务管理中的子事务”吧...
    99+
    2022-10-18
  • PostgreSQL事务处理机制原理
    本篇内容主要讲解“PostgreSQL事务处理机制原理”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“PostgreSQL事务处理机制原理”吧!背景说明:以ACI...
    99+
    2022-10-18
  • Spring中怎么管理事务
    今天小编给大家分享一下Spring中怎么管理事务的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。什么是事务一个数据库事务是一个...
    99+
    2023-07-02
  • spring怎么实现事务管理
    Spring框架提供了多种方式来实现事务管理。下面是几种常见的方式:1. 声明式事务管理:使用Spring的事务管理器和注解或XML...
    99+
    2023-09-28
    spring
  • 怎么理解Oracle事务
    本篇内容主要讲解“怎么理解Oracle事务”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么理解Oracle事务”吧!oracle事务1概述事务由一个或多个DM...
    99+
    2022-10-19
  • 怎么理解MySQL事务
    这篇“怎么理解MySQL事务”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“怎么理解MySQL事务”文章吧。事务指逻辑上的一组...
    99+
    2023-06-26
  • 怎么理解PostgreSQL表继承
    这篇文章主要介绍“怎么理解PostgreSQL表继承”,在日常操作中,相信很多人在怎么理解PostgreSQL表继承问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么理解Po...
    99+
    2022-10-18
  • mysql事务管理操作详解
    本文实例讲述了mysql事务管理操作。分享给大家供大家参考,具体如下: 本文内容: 什么是事务管理 事务管理操作 回滚点 默认的事务管理 首发日期:2018-04-18 什么是事务管理: ...
    99+
    2022-05-23
    mysql 事务管理
  • Spring详细解读事务管理
    目录什么是事务Spring事务配置Spring事务传播行为1. PROPAGATION_REQUIRED2. PROPAGATION_SUPPORTS3. PROPAGATION_R...
    99+
    2022-11-13
  • Spring事务管理详细讲解
    目录事务回顾spring事务操作基于注解声明事务@Transactional注解使用事务传播机制事务隔离级别@Transactional其他属性基于XML 声明式事务完全注解开发说明...
    99+
    2022-11-13
    Spring事务管理 Spring事务原理 Spring事务管理机制
  • postgresql事务处理与并发控制
    postgresql事务处理与并发控制:事务是postgresql中的基本工作单元,是用户定义的一个数据库操作序列。这些操作要么全做,要么全不做,是一个不可分割的工作单位。在postgresql中,事务管理...
    99+
    2022-10-18
  • MySQL 事务管理
      事务的4个特性(ACID) 原子性 Atomicity。每个事务中的操作,要么都成功,要么都失败 一致性 Consistency。事务执行前后,数据库中的数据应该保持一致 隔离性 Isolation。事务之间应该是隔离的,事...
    99+
    2015-09-09
    MySQL 事务管理
  • 怎么理解PostgreSQL DBA settings选项
    本篇内容介绍了“怎么理解PostgreSQL DBA settings选项”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,...
    99+
    2022-10-18
  • 怎么理解PostgreSQL的分区表
    本篇内容主要讲解“怎么理解PostgreSQL的分区表”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么理解PostgreSQL的分区表”吧!在PG中,分区表通...
    99+
    2022-10-18
  • 怎么理解PostgreSQL的PG Index Properties
    本篇内容介绍了“怎么理解PostgreSQL的PG Index Properties”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家...
    99+
    2022-10-18
  • Spring事务管理怎么正确使用
    这篇文章主要介绍“Spring事务管理怎么正确使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Spring事务管理怎么正确使用”文章能帮助大家解决问题。事务(Transaction)是访问数据库的...
    99+
    2023-07-05
  • springboot怎么配置mybatis和事务管理
    本篇内容主要讲解“springboot怎么配置mybatis和事务管理”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“springboot怎么配置mybatis和事务管理”吧!一、spring b...
    99+
    2023-07-05
  • 如何理解Spring的Hibernate事务管理机制
    如何理解Spring的Hibernate事务管理机制,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。主要讲了Spring 声明式事务让我们从复杂的Hibernate事务处理中...
    99+
    2023-06-17
  • MySQL事务管理的作用详解
    目录1.为何使用事务管理2.数据库事务的原理3.什么是事务3.1 事务的特性ACID3.2 事务的并发问题3.3 隔离级别4.Spring事务管理1.为何使用事务管理 可以保证数据的完整性。事务(Transaction)...
    99+
    2022-08-25
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作