目錄結構
注:提前言明 本文借鑒了以下博主、書籍或網站的內容,其列表如下:
1、參考書籍:《PostgreSQL數據庫內核分析》
2、參考書籍:《數據庫事務處理的藝術:事務管理與并發控制》
3、PostgreSQL數據庫倉庫鏈接,點擊前往
4、日本著名PostgreSQL數據庫專家 鈴木啟修 網站主頁,點擊前往
5、參考書籍:《PostgreSQL中文手冊》
6、參考書籍:《PostgreSQL指南:內幕探索》,點擊前往
7、Using Transaction Chaining to Reduce Server Round-Trips,點擊前往
1、本文內容全部來源于開源社區 GitHub和以上博主的貢獻,本文也免費開源(可能會存在問題,評論區等待大佬們的指正)
2、本文目的:開源共享 拋磚引玉 一起學習
3、本文不提供任何資源 不存在任何交易 與任何組織和機構無關
4、大家可以根據需要自行 復制粘貼以及作為其他個人用途,但是不允許轉載 不允許商用 (寫作不易,還請見諒 💖)
5、本文內容基于PostgreSQL master源碼開發而成
深入理解PostgreSQL數據庫之transaction chain的使用和實現
- 文章快速說明索引
- 功能使用背景說明
- 功能實現源碼分析
- 源碼調試案例分析

文章快速說明索引
學習目標:
做數據庫內核開發久了就會有一種 少年得志,年少輕狂 的錯覺,然鵝細細一品覺得自己其實不算特別優秀 遠遠沒有達到自己想要的。也許光鮮的表面掩蓋了空洞的內在,每每想到于此,皆有夜半臨淵如履薄冰之感。為了睡上幾個踏實覺,即日起 暫緩其他基于PostgreSQL數據庫的兼容功能開發,近段時間 將著重于學習分享Postgres的基礎知識和實踐內幕。
學習內容:(詳見目錄)
1、深入理解PostgreSQL數據庫之transaction chain的使用和實現
學習時間:
2024年07月01日 20:25:11
學習產出:
1、PostgreSQL數據庫基礎知識回顧 1個
2、CSDN 技術博客 1篇
3、PostgreSQL數據庫內核深入學習
注:下面我們所有的學習環境是Centos8+PostgreSQL master+Oracle19C+MySQL8.0
postgres=# select version();version
------------------------------------------------------------------------------------------------------------PostgreSQL 17devel on x86_64-pc-linux-gnu, compiled by gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-21), 64-bit
(1 row)postgres=##-----------------------------------------------------------------------------#SQL> select * from v$version; BANNER Oracle Database 19c EE Extreme Perf Release 19.0.0.0.0 - Production
BANNER_FULL Oracle Database 19c EE Extreme Perf Release 19.0.0.0.0 - Production Version 19.17.0.0.0
BANNER_LEGACY Oracle Database 19c EE Extreme Perf Release 19.0.0.0.0 - Production
CON_ID 0#-----------------------------------------------------------------------------#mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.27 |
+-----------+
1 row in set (0.06 sec)mysql>
功能使用背景說明
使用 PostgreSQL 或任何關系數據庫實現業務應用程序通常相當于執行一系列事務。給定事務以 COMMIT
還是 ROLLBACK
結束并不重要,因為在這兩種情況下,下一個事務都會在前一個事務完成后立即開始。此外,對于大多數應用程序來說,前一個事務和下一個事務具有相似的特征,例如它們的 ISOLATION LEVEL
。本質上,您最終得到的樣式如下所示:
START TRANSACTION;
-- workload of 1st transaction
COMMIT;
START TRANSACTION;
-- workload of 2nd transaction
ROLLBACK;
START TRANSACTION;
-- workload of 3rd transaction
COMMIT;
在上面的 SQL 腳本中,您會看到三個后續事務和總共六個語句(每個事務兩個語句),但您可以想象更長的事務序列。問題是:每個語句都需要單獨的服務器往返才能執行。沒有辦法解決這個問題,即使三個示例事務為空(不包含任何語句),也沒有什么區別。
監控連接狀態
為了進行此實驗,我們需要與同一 PostgreSQL 服務器建立兩個單獨的連接(例如,兩個 psql
會話)。第一個連接使用 postgres
數據庫,第二個連接使用demo
數據庫。
好了,現在我們可以在第一個連接上使用 pg_stat_activity
(屬于 pg_catalog 模式的內置視圖)來詢問 PostgreSQL 使用演示數據庫的第二個連接的狀態:
postgres=# SELECT state FROM pg_stat_activity WHERE datname='demo';state
-------idle
(1 row)
如您所見,由于沒有執行任何操作,因此該連接當前處于空閑狀態。但是,如果我們在第二個連接上啟動事務,我們可以看到連接的狀態立即變為idle in transaction
。
demo=# START TRANSACTION;
START TRANSACTION
postgres=# SELECT state FROM pg_stat_activity WHERE datname='demo';state
---------------------idle in transaction
(1 row)
這證明 START TRANSACTION
語句導致了與服務器的往返,因為如果沒有,第一個連接將無法看到第二個連接的狀態變化。如果我們使用 COMMIT
或 ROLLBACK
完成事務,情況也是一樣的,在這種情況下,連接會立即恢復到空閑狀態:
demo=*# COMMIT;
COMMIT
postgres=# SELECT state FROM pg_stat_activity WHERE datname='demo';state
-------idle
(1 row)
往返開銷
長話短說,事務的開始和結束可能會帶來巨大的開銷。具體來說,如果滿足以下任何條件:
- 往返服務器的時間很慢。通常,如果客戶端和服務器之間的網絡距離很遠,情況就會如此。
- 許多事務的平均運行時間很短。這是因為對于較短的事務,開銷占總運行時間的百分比較高。
那么,我們可以做些什么來減少開銷并提高性能呢?
事務鏈
SQL 標準有一個由 PostgreSQL 實現的內置解決方案:AND CHAIN
參數。此參數可用于 COMMIT
和 ROLLBACK
語句,并具有以下效果…
如果提供了 AND CHAIN
參數,則提交(或回滾)當前事務,此外,立即啟動具有相同特征(例如,ISOLATION LEVEL
)的后續事務。
因此,如果我們將其應用于原始示例,我們可以將服務器往返次數減少基本上 50%(從 n
減少到 n/2+1
)。
START TRANSACTION;
-- workload of 1st transaction
COMMIT AND CHAIN;
-- workload of 2nd transaction
ROLLBACK AND CHAIN;
-- workload of 3rd transaction
COMMIT;
我們可以運行相同的實驗來證明它按預期工作。我再次使用兩個連接,一個使用 postgres 數據庫,另一個使用演示數據庫。最初,演示連接處于空閑狀態,但是一旦我們開始新的事務,其狀態就會更改為idle in transaction
。
demo=# START TRANSACTION;
START TRANSACTION
postgres=# SELECT state FROM pg_stat_activity WHERE datname='demo';state
---------------------idle in transaction
(1 row)
那么,如果demo
連接執行 COMMIT AND CHAIN
語句會發生什么?正如我所說,PostgreSQL 立即啟動后續事務,因此我們看不到狀態變化。
demo=# COMMIT AND CHAIN;
COMMIT AND CHAIN
postgres=# SELECT state FROM pg_stat_activity WHERE datname='demo';state
---------------------idle in transaction
(1 row)
當我們執行 ROLLBACK AND CHAIN
時,我們得到完全相同的行為 — 沒有明顯的狀態改變。
demo=# ROLLBACK AND CHAIN;
ROLLBACK AND CHAIN
postgres=# SELECT state FROM pg_stat_activity WHERE datname='demo';state
---------------------idle in transaction
(1 row)
最后,當我們發出正常的COMMIT
或ROLLBACK
時,狀態就會變回初始的空閑狀態。
demo=# COMMIT;
COMMIT
postgres=# SELECT state FROM pg_stat_activity WHERE datname='demo';state
-------idle
(1 row)
功能實現源碼分析
其語法格式如下:
// src/backend/parser/gram.y/******************************************************************************* Transactions:** BEGIN / COMMIT / ROLLBACK* (also older versions END / ABORT)******************************************************************************/TransactionStmt:ABORT_P opt_transaction opt_transaction_chain{TransactionStmt *n = makeNode(TransactionStmt);n->kind = TRANS_STMT_ROLLBACK;n->options = NIL;n->chain = $3;n->location = -1;$$ = (Node *) n;}...| COMMIT opt_transaction opt_transaction_chain{TransactionStmt *n = makeNode(TransactionStmt);n->kind = TRANS_STMT_COMMIT;n->options = NIL;n->chain = $3;n->location = -1;$$ = (Node *) n;}| ROLLBACK opt_transaction opt_transaction_chain{TransactionStmt *n = makeNode(TransactionStmt);n->kind = TRANS_STMT_ROLLBACK;n->options = NIL;n->chain = $3;n->location = -1;$$ = (Node *) n;}...;TransactionStmtLegacy:...| END_P opt_transaction opt_transaction_chain{TransactionStmt *n = makeNode(TransactionStmt);n->kind = TRANS_STMT_COMMIT;n->options = NIL;n->chain = $3;n->location = -1;$$ = (Node *) n;};opt_transaction_chain:AND CHAIN { $$ = true; }| AND NO CHAIN { $$ = false; }| /* EMPTY */ { $$ = false; };
示例一,如下:
示例二,如下:
源碼調試案例分析
接下來,我們調試一下 重點看一下上面的示例二,如下:
如上,begin READ ONLY;
是在上圖將guc參數transaction_read_only
設置為真 XactReadOnly = true
,函數堆棧,如下:
set_config_with_handle(const char * name, config_handle * handle, const char * value, GucContext context, GucSource source, Oid srole, GucAction action, _Bool changeVal, int elevel, _Bool is_reload) (\home\postgres\postgres\src\backend\utils\misc\guc.c:3758)
set_config_option(const char * name, const char * value, GucContext context, GucSource source, GucAction action, _Bool changeVal, int elevel, _Bool is_reload) (\home\postgres\postgres\src\backend\utils\misc\guc.c:3361)
SetPGVariable(const char * name, List * args, _Bool is_local) (\home\postgres\postgres\src\backend\utils\misc\guc_funcs.c:320)
standard_ProcessUtility(PlannedStmt * pstmt, const char * queryString, _Bool readOnlyTree, ProcessUtilityContext context, ParamListInfo params, QueryEnvironment * queryEnv, DestReceiver * dest, QueryCompletion * qc) (\home\postgres\postgres\src\backend\tcop\utility.c:619)
ProcessUtility(PlannedStmt * pstmt, const char * queryString, _Bool readOnlyTree, ProcessUtilityContext context, ParamListInfo params, QueryEnvironment * queryEnv, DestReceiver * dest, QueryCompletion * qc) (\home\postgres\postgres\src\backend\tcop\utility.c:523)
PortalRunUtility(Portal portal, PlannedStmt * pstmt, _Bool isTopLevel, _Bool setHoldSnapshot, DestReceiver * dest, QueryCompletion * qc) (\home\postgres\postgres\src\backend\tcop\pquery.c:1158)
PortalRunMulti(Portal portal, _Bool isTopLevel, _Bool setHoldSnapshot, DestReceiver * dest, DestReceiver * altdest, QueryCompletion * qc) (\home\postgres\postgres\src\backend\tcop\pquery.c:1315)
PortalRun(Portal portal, long count, _Bool isTopLevel, _Bool run_once, DestReceiver * dest, DestReceiver * altdest, QueryCompletion * qc) (\home\postgres\postgres\src\backend\tcop\pquery.c:791)
exec_simple_query(const char * query_string) (\home\postgres\postgres\src\backend\tcop\postgres.c:1274)
PostgresMain(const char * dbname, const char * username) (\home\postgres\postgres\src\backend\tcop\postgres.c:4680)
BackendMain(char * startup_data, size_t startup_data_len) (\home\postgres\postgres\src\backend\tcop\backend_startup.c:105)
postmaster_child_launch(BackendType child_type, char * startup_data, size_t startup_data_len, ClientSocket * client_sock) (\home\postgres\postgres\src\backend\postmaster\launch_backend.c:265)
BackendStartup(ClientSocket * client_sock) (\home\postgres\postgres\src\backend\postmaster\postmaster.c:3593)
ServerLoop() (\home\postgres\postgres\src\backend\postmaster\postmaster.c:1674)
PostmasterMain(int argc, char ** argv) (\home\postgres\postgres\src\backend\postmaster\postmaster.c:1372)
main(int argc, char ** argv) (\home\postgres\postgres\src\backend\main\main.c:197)
接下來,這里將直接INSERT,報錯如下:
后續處理,如下:
此時的函數堆棧,如下:
AtEOXact_GUC(_Bool isCommit, int nestLevel)
AbortTransaction()
AbortCurrentTransactionInternal()
AbortCurrentTransaction()
PostgresMain(const char * dbname, const char * username)
BackendMain(char * startup_data, size_t startup_data_len)
postmaster_child_launch(BackendType child_type, char * startup_data, size_t startup_data_len, ClientSocket * client_sock)
BackendStartup(ClientSocket * client_sock)
ServerLoop()
PostmasterMain(int argc, char ** argv)
main(int argc, char ** argv)
注:如上 在INSERT報錯之后,該事務對應的上述GUC被重置,如下:
// src/backend/utils/misc/guc.c/** Do GUC processing at transaction or subtransaction commit or abort, or* when exiting a function that has proconfig settings, or when undoing a* transient assignment to some GUC variables. (The name is thus a bit of* a misnomer; perhaps it should be ExitGUCNestLevel or some such.)* During abort, we discard all GUC settings that were applied at nesting* levels >= nestLevel. nestLevel == 1 corresponds to the main transaction.* * 在事務或子事務提交或中止時,或在退出具有 proconfig 設置的函數時,或在撤消對某些 GUC 變量的臨時分配時,執行 GUC 處理* (因此,這個名字有點用詞不當;也許應該是 ExitGUCNestLevel 或類似的名字)* 在中止期間,我們會丟棄在嵌套級別 >= nestLevel 處應用的所有 GUC 設置* nestLevel == 1 對應于主事務*/
void
AtEOXact_GUC(bool isCommit, int nestLevel);
于是在接下來的commit and chain;
中,XactReadOnly
仍是假,如下:
此時函數堆棧,如下:
EndTransactionBlock(_Bool chain)
standard_ProcessUtility(PlannedStmt * pstmt, const char * queryString, _Bool readOnlyTree, ProcessUtilityContext context, ParamListInfo params, QueryEnvironment * queryEnv, DestReceiver * dest, QueryCompletion * qc)
ProcessUtility(PlannedStmt * pstmt, const char * queryString, _Bool readOnlyTree, ProcessUtilityContext context, ParamListInfo params, QueryEnvironment * queryEnv, DestReceiver * dest, QueryCompletion * qc)
PortalRunUtility(Portal portal, PlannedStmt * pstmt, _Bool isTopLevel, _Bool setHoldSnapshot, DestReceiver * dest, QueryCompletion * qc)
PortalRunMulti(Portal portal, _Bool isTopLevel, _Bool setHoldSnapshot, DestReceiver * dest, DestReceiver * altdest, QueryCompletion * qc)
PortalRun(Portal portal, long count, _Bool isTopLevel, _Bool run_once, DestReceiver * dest, DestReceiver * altdest, QueryCompletion * qc)
exec_simple_query(const char * query_string)
...
接下來,我們調試一下上面的示例二的另一種情況,因為當前會話已經設置該GUC參數為真(將要被rollback或者commit的事務),接下來的rollback and chain
的處理 如下:
UserAbortTransactionBlock(_Bool chain) (\home\postgres\postgres\src\backend\access\transam\xact.c:4262)
standard_ProcessUtility(PlannedStmt * pstmt, const char * queryString, _Bool readOnlyTree, ProcessUtilityContext context, ParamListInfo params, QueryEnvironment * queryEnv, DestReceiver * dest, QueryCompletion * qc)
ProcessUtility(PlannedStmt * pstmt, const char * queryString, _Bool readOnlyTree, ProcessUtilityContext context, ParamListInfo params, QueryEnvironment * queryEnv, DestReceiver * dest, QueryCompletion * qc)
PortalRunUtility(Portal portal, PlannedStmt * pstmt, _Bool isTopLevel, _Bool setHoldSnapshot, DestReceiver * dest, QueryCompletion * qc)
PortalRunMulti(Portal portal, _Bool isTopLevel, _Bool setHoldSnapshot, DestReceiver * dest, DestReceiver * altdest, QueryCompletion * qc)
PortalRun(Portal portal, long count, _Bool isTopLevel, _Bool run_once, DestReceiver * dest, DestReceiver * altdest, QueryCompletion * qc)
exec_simple_query(const char * query_string)
...
在UserAbortTransactionBlock函數中的處理,如下:
s->blockState: TBLOCK_INPROGRESS -> TBLOCK_ABORT_PENDING
s->chain: false -> true
然后進入如下的處理邏輯:
CommitTransactionCommandInternal()
CommitTransactionCommand()
finish_xact_command()
exec_simple_query(const char * query_string)
...
/** Here we were in a perfectly good transaction block but the user* told us to ROLLBACK anyway. We have to abort the transaction* and then clean up.*/case TBLOCK_ABORT_PENDING:AbortTransaction();CleanupTransaction();s->blockState = TBLOCK_DEFAULT;if (s->chain){StartTransaction();s->blockState = TBLOCK_INPROGRESS;s->chain = false;RestoreTransactionCharacteristics(&savetc);}break;
首先進入AbortTransaction
函數,因為在這種情況下guc_stack_list != NIL
,在如下的堆棧處理中 該參數XactReadOnly
被置為假:
AtEOXact_GUC(_Bool isCommit, int nestLevel)
AbortTransaction()
CommitTransactionCommandInternal()
CommitTransactionCommand()
finish_xact_command()
exec_simple_query(const char * query_string)
...
接著進入CleanupTransaction
函數進行清理!
接下來進入今天的重點,如下:
if (s->chain){StartTransaction();s->blockState = TBLOCK_INPROGRESS;s->chain = false;RestoreTransactionCharacteristics(&savetc);}
因為chain為真,這里還是再重啟一個事務。不過這種類似于XactReadOnly
的guc參數在StartTransaction
過程中仍被賦值默認值!該參數的重新修改如下:
至此,rollback and chain 操作回滾了上一個事務,并開啟新的事務 且XactReadOnly = true
,得以保留!
同上面rollback and chain操作一樣,其他幾種 如下:
// src/backend/access/transam/xact.c/** CommitTransactionCommandInternal - a function doing an iteration of work* regarding handling the commit transaction command. In the case of* subtransactions more than one iterations could be required. Returns* true when no more iterations required, false otherwise.*/
static bool
CommitTransactionCommandInternal(void)
{.../** We are completing a "COMMIT" command. Do it and return to the* idle state.*/case TBLOCK_END:CommitTransaction();s->blockState = TBLOCK_DEFAULT;if (s->chain) // here{StartTransaction();s->blockState = TBLOCK_INPROGRESS;s->chain = false;RestoreTransactionCharacteristics(&savetc);}break;.../** Here we were in an aborted transaction block and we just got* the ROLLBACK command from the user, so clean up the* already-aborted transaction and return to the idle state.*/case TBLOCK_ABORT_END:CleanupTransaction();s->blockState = TBLOCK_DEFAULT;if (s->chain) // here{StartTransaction();s->blockState = TBLOCK_INPROGRESS;s->chain = false;RestoreTransactionCharacteristics(&savetc);}break;.../** Here we were in a perfectly good transaction block but the user* told us to ROLLBACK anyway. We have to abort the transaction* and then clean up.*/case TBLOCK_ABORT_PENDING:AbortTransaction();CleanupTransaction();s->blockState = TBLOCK_DEFAULT;if (s->chain) // here{StartTransaction();s->blockState = TBLOCK_INPROGRESS;s->chain = false;RestoreTransactionCharacteristics(&savetc);}break;.../** The user issued a COMMIT, so we end the current subtransaction* hierarchy and perform final commit. We do this by rolling up* any subtransactions into their parent, which leads to O(N^2)* operations with respect to resource owners - this isn't that* bad until we approach a thousands of savepoints but is* necessary for correctness should after triggers create new* resource owners.*/case TBLOCK_SUBCOMMIT:do{CommitSubTransaction();s = CurrentTransactionState; /* changed by pop */} while (s->blockState == TBLOCK_SUBCOMMIT);/* If we had a COMMIT command, finish off the main xact too */if (s->blockState == TBLOCK_END){Assert(s->parent == NULL);CommitTransaction();s->blockState = TBLOCK_DEFAULT;if (s->chain) // here{StartTransaction();s->blockState = TBLOCK_INPROGRESS;s->chain = false;RestoreTransactionCharacteristics(&savetc);}}else if (s->blockState == TBLOCK_PREPARE){Assert(s->parent == NULL);PrepareTransaction();s->blockState = TBLOCK_DEFAULT;}elseelog(ERROR, "CommitTransactionCommand: unexpected state %s",BlockStateAsString(s->blockState));break;...
}
對應非特殊的guc參數,能否可以繼承呢?如下:
[postgres@localhost:~/test/bin]$ ./psql
psql (17beta2)
Type "help" for help.postgres=# show timezone;TimeZone
---------------------America/Los_Angeles
(1 row)postgres=# begin;
BEGIN
postgres=*# set timezone = 'PRC';
SET
postgres=*# show timezone;TimeZone
----------PRC
(1 row)postgres=*# commit and chain; ## commit 提交 && 繼承
COMMIT
postgres=*# show timezone;TimeZone
----------PRC
(1 row)postgres=*# rollback and chain; ## 無東西可以回滾
ROLLBACK
postgres=*# show timezone;TimeZone
----------PRC
(1 row)postgres=*#
[postgres@localhost:~/test/bin]$ ./psql
psql (17beta2)
Type "help" for help.postgres=# show timezone;TimeZone
---------------------America/Los_Angeles
(1 row)postgres=# begin;
BEGIN
postgres=*# desc a error;
2024-07-01 06:19:56.969 PDT [34810] ERROR: syntax error at or near "desc" at character 1
2024-07-01 06:19:56.969 PDT [34810] STATEMENT: desc a error;
ERROR: syntax error at or near "desc"
LINE 1: desc a error;^
postgres=!# show timezone;
2024-07-01 06:20:00.721 PDT [34810] ERROR: current transaction is aborted, commands ignored until end of transaction block
2024-07-01 06:20:00.721 PDT [34810] STATEMENT: show timezone;
ERROR: current transaction is aborted, commands ignored until end of transaction block
postgres=!#
postgres=!# commit and chain; ## commit 這里相當于先回滾 && 繼承
ROLLBACK
postgres=*# show timezone;TimeZone
---------------------America/Los_Angeles
(1 row)postgres=*# set timezone = 'PRC';
SET
postgres=*# show timezone;TimeZone
----------PRC
(1 row)postgres=*# commit and chain; ## commit 提交 && 繼承
COMMIT
postgres=*# show timezone;TimeZone
----------PRC
(1 row)postgres=*# rollback and chain; ## 無東西可以回滾
ROLLBACK
postgres=*# show timezone;TimeZone
----------PRC
(1 row)postgres=*# reset timezone;
RESET
postgres=*# show timezone;TimeZone
---------------------America/Los_Angeles
(1 row)postgres=*# rollback and chain; ## rollback 這里相當于先回滾 && 繼承
ROLLBACK
postgres=*# show timezone;TimeZone
----------PRC
(1 row)postgres=*#