共計 6135 個字符,預計需要花費 16 分鐘才能閱讀完成。
本篇內(nèi)容介紹了“PostgreSQL 中 vacuum 主流程分析”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
一、數(shù)據(jù)結(jié)構(gòu)
宏定義
Vacuum 和 Analyze 命令選項
/* ----------------------
* Vacuum and Analyze Statements
* Vacuum 和 Analyze 命令選項
*
* Even though these are nominally two statements, it s convenient to use
* just one node type for both. Note that at least one of VACOPT_VACUUM
* and VACOPT_ANALYZE must be set in options.
* 雖然在這里有兩種不同的語句, 但只需要使用統(tǒng)一的 Node 類型即可.
* 注意至少 VACOPT_VACUUM/VACOPT_ANALYZE 在選項中設置.
* ----------------------
*/
typedef enum VacuumOption
VACOPT_VACUUM = 1 0, /* do VACUUM */
VACOPT_ANALYZE = 1 1, /* do ANALYZE */
VACOPT_VERBOSE = 1 2, /* print progress info */
VACOPT_FREEZE = 1 3, /* FREEZE option */
VACOPT_FULL = 1 4, /* FULL (non-concurrent) vacuum */
VACOPT_SKIP_LOCKED = 1 5, /* skip if cannot get lock */
VACOPT_SKIPTOAST = 1 6, /* don t process the TOAST table, if any */
VACOPT_DISABLE_PAGE_SKIPPING = 1 7 /* don t skip any pages */
} VacuumOption;
VacuumStmt
存儲 vacuum 命令的 option Relation 鏈表
typedef struct VacuumStmt
NodeTag type;//Tag
//VacuumOption 位標記
int options; /* OR of VacuumOption flags */
//VacuumRelation 鏈表, 如為 NIL-- 所有 Relation.
List *rels; /* list of VacuumRelation, or NIL for all */
} VacuumStmt;
VacuumParams
vacuum 命令參數(shù)
/*
* Parameters customizing behavior of VACUUM and ANALYZE.
* 客戶端調(diào)用 VACUUM/ANALYZE 時的定制化參數(shù)
*/
typedef struct VacuumParams
// 最小 freeze age,- 1 表示使用默認
int freeze_min_age; /* min freeze age, -1 to use default */
// 掃描整個 table 的 freeze age
int freeze_table_age; /* age at which to scan whole table */
// 最小的 multixact freeze age,- 1 表示默認
int multixact_freeze_min_age; /* min multixact freeze age, -1 to
* use default */
// 掃描全表的 freeze age,- 1 表示默認
int multixact_freeze_table_age; /* multixact age at which to scan
* whole table */
// 是否強制 wraparound?
bool is_wraparound; /* force a for-wraparound vacuum */
// 以毫秒為單位的最小執(zhí)行閾值
int log_min_duration; /* minimum execution threshold in ms at
* which verbose logs are activated, -1
* to use default */
} VacuumParams;
VacuumRelation
VACUUM/ANALYZE 命令的目標表信息
/*
* Info about a single target table of VACUUM/ANALYZE.
* VACUUM/ANALYZE 命令的目標表信息.
*
* If the OID field is set, it always identifies the table to process.
* Then the relation field can be NULL; if it isn t, it s used only to report
* failure to open/lock the relation.
* 如設置了 OID 字段, 該值通常是將要處理的數(shù)據(jù)表.
* 那么關(guān)系字段可以為空; 如果不是,則僅用于報告未能打開 / 鎖定關(guān)系。 */
typedef struct VacuumRelation
NodeTag type;
RangeVar *relation; /* table name to process, or NULL */
Oid oid; /* table s OID; InvalidOid if not looked up */
List *va_cols; /* list of column names, or NIL for all */
} VacuumRelation;
二、源碼解讀
ExecVacuum 函數(shù), 手工執(zhí)行 VACUUM/ANALYZE 命令時的主入口,vacuum() 函數(shù)的包裝器 (wrapper).
/*
* Primary entry point for manual VACUUM and ANALYZE commands
* 手工執(zhí)行 VACUUM/ANALYZE 命令時的主入口
*
* This is mainly a preparation wrapper for the real operations that will
* happen in vacuum().
* 這是 vacuum() 函數(shù)的包裝器 (wrapper)
*/
ExecVacuum(VacuumStmt *vacstmt, bool isTopLevel)
VacuumParams params;
/* sanity checks on options */
// 驗證 檢查
Assert(vacstmt- options (VACOPT_VACUUM | VACOPT_ANALYZE));
Assert((vacstmt- options VACOPT_VACUUM) ||
!(vacstmt- options (VACOPT_FULL | VACOPT_FREEZE)));
Assert(!(vacstmt- options VACOPT_SKIPTOAST));
/*
* Make sure VACOPT_ANALYZE is specified if any column lists are present.
* 如出現(xiàn)字段列表, 則確保指定了 VACOPT_ANALYZE 選項
*/
if (!(vacstmt- options VACOPT_ANALYZE))
{
ListCell *lc;
foreach(lc, vacstmt- rels)
{ VacuumRelation *vrel = lfirst_node(VacuumRelation, lc);
if (vrel- va_cols != NIL)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg(ANALYZE option must be specified when a column list is provided)));
}
}
/*
* All freeze ages are zero if the FREEZE option is given; otherwise pass
* them as -1 which means to use the default values.
* 如指定了 FREEZE 選項則設置所有 freeze ages 為 0.
* 否則的話, 傳遞 -1(即使用默認值).
*/
if (vacstmt- options VACOPT_FREEZE)
{
// 指定 VACOPT_FREEZE
params.freeze_min_age = 0;
params.freeze_table_age = 0;
params.multixact_freeze_min_age = 0;
params.multixact_freeze_table_age = 0;
}
else
{
params.freeze_min_age = -1;
params.freeze_table_age = -1;
params.multixact_freeze_min_age = -1;
params.multixact_freeze_table_age = -1;
}
/* user-invoked vacuum is never for wraparound */
// 用戶調(diào)用的 vacuum 永遠不會是 wraparound
params.is_wraparound = false;
/* user-invoked vacuum never uses this parameter */
// 用戶調(diào)用 vacuum 永遠不會使用該參數(shù)
params.log_min_duration = -1;
/* Now go through the common routine */
// 調(diào)用 vacuum
vacuum(vacstmt- options, vacstmt- rels, params, NULL, isTopLevel);
}
三、跟蹤分析
測試腳本
17:19:28 (xdb@[local]:5432)testdb=# vacuum t1;
啟動 gdb, 設置斷點
(gdb) b ExecVacuum
Breakpoint 1 at 0x6b99a1: file vacuum.c, line 92.
(gdb) c
Continuing.
Breakpoint 1, ExecVacuum (vacstmt=0x210e9c0, isTopLevel=true) at vacuum.c:92
92 Assert(vacstmt- options (VACOPT_VACUUM | VACOPT_ANALYZE));
(gdb)
輸入?yún)?shù)
options = 1 — VACOPT_VACUUM
(gdb) p *vacstmt
$1 = {type = T_VacuumStmt, options = 1, rels = 0x210e988}
(gdb)
獲取 Relation 相關(guān)信息
gdb) n
93 Assert((vacstmt- options VACOPT_VACUUM) ||
(gdb)
95 Assert(!(vacstmt- options VACOPT_SKIPTOAST));
(gdb)
100 if (!(vacstmt- options VACOPT_ANALYZE))
(gdb)
104 foreach(lc, vacstmt- rels)
(gdb)
106 VacuumRelation *vrel = lfirst_node(VacuumRelation, lc);
(gdb)
108 if (vrel- va_cols != NIL)
(gdb) p *vrel
$3 = {type = T_VacuumRelation, relation = 0x210e8d0, oid = 0, va_cols = 0x0}
(gdb) p *vrel- relation
$4 = {type = T_RangeVar, catalogname = 0x0, schemaname = 0x0, relname = 0x210e8b0 t1 , inh = true,
relpersistence = 112 p , alias = 0x0, location = 7}
(gdb)
設置 vacuum 參數(shù)
(gdb) n
104 foreach(lc, vacstmt- rels)
(gdb)
119 if (vacstmt- options VACOPT_FREEZE)
(gdb)
128 params.freeze_min_age = -1;
(gdb)
129 params.freeze_table_age = -1;
(gdb)
130 params.multixact_freeze_min_age = -1;
(gdb)
131 params.multixact_freeze_table_age = -1;
(gdb)
135 params.is_wraparound = false;
(gdb)
(gdb) n
138 params.log_min_duration = -1;
(gdb)
調(diào)用 vacuum
141 vacuum(vacstmt- options, vacstmt- rels, params, NULL, isTopLevel);
(gdb)
142 }
(gdb)
standard_ProcessUtility (pstmt=0x210ea80, queryString=0x210dec8 vacuum t1; , context=PROCESS_UTILITY_TOPLEVEL, params=0x0,
queryEnv=0x0, dest=0x210ed70, completionTag=0x7fff1d69dea0 ) at utility.c:672
672 break;
“PostgreSQL 中 vacuum 主流程分析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注丸趣 TV 網(wǎng)站,丸趣 TV 小編將為大家輸出更多高質(zhì)量的實用文章!