Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Transaction State


TBlockState: 事务块状态

/*
 *	transaction block states - transaction state of client queries
 *
 * Note: the subtransaction states are used only for non-topmost
 * transactions; the others appear only in the topmost transaction.
 */
typedef enum TBlockState
{
	/* not-in-transaction-block states */
	TBLOCK_DEFAULT,				/* idle */
	TBLOCK_STARTED,				/* running single-query transaction */

	/* transaction block states */
	TBLOCK_BEGIN,				/* starting transaction block */
	TBLOCK_INPROGRESS,			/* live transaction */
	TBLOCK_IMPLICIT_INPROGRESS, /* live transaction after implicit BEGIN */
	TBLOCK_PARALLEL_INPROGRESS, /* live transaction inside parallel worker */
	TBLOCK_END,					/* COMMIT received */
	TBLOCK_ABORT,				/* failed xact, awaiting ROLLBACK */
	TBLOCK_ABORT_END,			/* failed xact, ROLLBACK received */
	TBLOCK_ABORT_PENDING,		/* live xact, ROLLBACK received */
	TBLOCK_PREPARE,				/* live xact, PREPARE received */

	/* subtransaction states */
	TBLOCK_SUBBEGIN,			/* starting a subtransaction */
	TBLOCK_SUBINPROGRESS,		/* live subtransaction */
	TBLOCK_SUBRELEASE,			/* RELEASE received */
	TBLOCK_SUBCOMMIT,			/* COMMIT received while TBLOCK_SUBINPROGRESS */
	TBLOCK_SUBABORT,			/* failed subxact, awaiting ROLLBACK */
	TBLOCK_SUBABORT_END,		/* failed subxact, ROLLBACK received */
	TBLOCK_SUBABORT_PENDING,	/* live subxact, ROLLBACK received */
	TBLOCK_SUBRESTART,			/* live subxact, ROLLBACK TO received */
	TBLOCK_SUBABORT_RESTART		/* failed subxact, ROLLBACK TO received */
} TBlockState;

作用: 描述事务块的控制流状态,从SQL语法层面反映用户命令执行流程。

TransState: 事务状态

/*
 *	transaction states - transaction state from server perspective
 */
typedef enum TransState
{
	TRANS_DEFAULT,				/* idle */
	TRANS_START,				/* transaction starting */
	TRANS_INPROGRESS,			/* inside a valid transaction */
	TRANS_COMMIT,				/* commit in progress */
	TRANS_ABORT,				/* abort in progress */
	TRANS_PREPARE				/* prepare in progress */
} TransState;

作用: 描述事务本身的执行状态,从服务器内核角度反映事务进度。


两者的关系

┌─────────────────────────────────────┐
│  BEGIN; INSERT; COMMIT;             │
└─────────────────────────────────────┘
           ↓
┌─────────────────────────────────────┐
│ TBlockState (SQL)                   │
│ ├─ TBLOCK_DEFAULT                   │
│ ├─ TBLOCK_STARTED                   │
│ ├─ TBLOCK_BEGIN (BEGIN)             │
│ ├─ TBLOCK_INPROGRESS (INSERT)       │
│ └─ TBLOCK_END (COMMIT)              │
└─────────────────────────────────────┘
           ↓
┌─────────────────────────────────────┐
│ TransState (SERVER)                 │
│ ├─ TRANS_DEFAULT                    │
│ ├─ TRANS_START (BEGIN)              │
│ ├─ TRANS_INPROGRESS (INSERT)        │
│ └─ TRANS_COMMIT (COMMIT)            │
└─────────────────────────────────────┘

层次划分

层级状态职责视角
SQL 层TBlockState处理 BEGIN、COMMIT、ROLLBACK 等命令用户命令流
内核层TransState管理事务的实际执行进度事务执行进度

draw_trans_state