溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PostgreSQL在執(zhí)行邏輯優(yōu)化中相關(guān)的數(shù)據(jù)結(jié)構(gòu)有哪些

發(fā)布時(shí)間:2021-11-09 11:06:51 來(lái)源:億速云 閱讀:121 作者:iii 欄目:關(guān)系型數(shù)據(jù)庫(kù)

這篇文章主要介紹“PostgreSQL在執(zhí)行邏輯優(yōu)化中相關(guān)的數(shù)據(jù)結(jié)構(gòu)有哪些”,在日常操作中,相信很多人在PostgreSQL在執(zhí)行邏輯優(yōu)化中相關(guān)的數(shù)據(jù)結(jié)構(gòu)有哪些問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”P(pán)ostgreSQL在執(zhí)行邏輯優(yōu)化中相關(guān)的數(shù)據(jù)結(jié)構(gòu)有哪些”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

PostgreSQL在執(zhí)行邏輯優(yōu)化中相關(guān)的數(shù)據(jù)結(jié)構(gòu)包括RangeSubselect/Alias/RangeVar/ResTarget/ColumnRef.

一、數(shù)據(jù)結(jié)構(gòu)

RangeSubselect
出現(xiàn)在FROM語(yǔ)句中的子查詢

/*
 * RangeSubselect - subquery appearing in a FROM clause
 * 出現(xiàn)在FROM語(yǔ)句中的子查詢
 */
typedef struct RangeSubselect
{
  NodeTag   type;
  //是否有LATERAL?
  bool    lateral;    /* does it have LATERAL prefix? */
  //未轉(zhuǎn)換的子查詢語(yǔ)句
  Node     *subquery;   /* the untransformed sub-select clause */
  //表別名和可選的列別名
  Alias    *alias;      /* table alias & optional column aliases */
} RangeSubselect;

Alias
為RangeVar指定別名.別名可能同時(shí)重命名了表列.

/*
 * Alias -
 *    specifies an alias for a range variable; the alias might also
 *    specify renaming of columns within the table.
 *    為RangeVar指定別名.別名可能同時(shí)重命名了表列.
 *
 * Note: colnames is a list of Value nodes (always strings).  In Alias structs
 * associated with RTEs, there may be entries corresponding to dropped
 * columns; these are normally empty strings ("").  See parsenodes.h for info.
 * 注意:colnames是Value節(jié)點(diǎn)(通常是字符串)鏈表.
 *      在與RTEs相關(guān)的Alias結(jié)構(gòu)體中,可能有跟已刪除的列對(duì)應(yīng)的條目.
 *    這些通常是空字符串("").詳細(xì)可參考parsenodes.h
 */
typedef struct Alias
{
  NodeTag   type;
  //別名
  char     *aliasname;    /* aliased rel name (never qualified) */
  //列別名鏈表
  List     *colnames;   /* optional list of column aliases */
} Alias;

RangeVar
range variable,用于FROM語(yǔ)句中.

/*
 * RangeVar - range variable, used in FROM clauses
 * range variable,用于FROM語(yǔ)句中.
 *
 * Also used to represent table names in utility statements; there, the alias
 * field is not used, and inh tells whether to apply the operation
 * recursively to child tables.  In some contexts it is also useful to carry
 * a TEMP table indication here.
 * 在工具語(yǔ)句中,用于表示表名,這時(shí)alias字段沒(méi)有使用.
 * inh用于判斷是否在子表中遞歸應(yīng)用相關(guān)的操作.
 */
typedef struct RangeVar
{
  NodeTag   type;
  char     *catalogname;  /* the catalog (database) name, or NULL */
  char     *schemaname;   /* the schema name, or NULL */
  char     *relname;    /* the relation/sequence name */
  bool    inh;      /* expand rel by inheritance? recursively act
                 * on children? */
  char    relpersistence; /* see RELPERSISTENCE_* in pg_class.h */
  Alias    *alias;      /* table alias & optional column aliases */
  int     location;   /* token location, or -1 if unknown */
} RangeVar;

ResTarget
結(jié)果目標(biāo)列(用于先前已轉(zhuǎn)換的解析樹(shù)的目標(biāo)鏈表中)

/*
 * ResTarget -
 *    result target (used in target list of pre-transformed parse trees)
 *    結(jié)果目標(biāo)列(用于先前已轉(zhuǎn)換的解析樹(shù)的目標(biāo)鏈表中)
 *
 * In a SELECT target list, 'name' is the column label from an
 * 'AS ColumnLabel' clause, or NULL if there was none, and 'val' is the
 * value expression itself.  The 'indirection' field is not used.
 * 在SELECT的目標(biāo)鏈表中,
 *   'name'是'AS ColumnLabel'語(yǔ)句中的列標(biāo)簽,如無(wú)則為NULL,
 *   'val'是value表達(dá)式本身.
 *   'indirection'未使用.
 *
 * INSERT uses ResTarget in its target-column-names list.  Here, 'name' is
 * the name of the destination column, 'indirection' stores any subscripts
 * attached to the destination, and 'val' is not used.
 * INSERT在target-column-names鏈表中使用ResTarget.
 * 這里'name'是目標(biāo)列名稱,'indirection'存儲(chǔ)了所有與目標(biāo)相關(guān)的子腳本,'val'未使用.
 *
 * In an UPDATE target list, 'name' is the name of the destination column,
 * 'indirection' stores any subscripts attached to the destination, and
 * 'val' is the expression to assign.
 * 在UPDATE目標(biāo)鏈表中,'name'是目標(biāo)列名稱,'indirection'存儲(chǔ)了所有與目標(biāo)相關(guān)的子腳本,
 * 'val'是與賦值相關(guān)的表達(dá)式.
 *
 * See A_Indirection for more info about what can appear in 'indirection'.
 * 詳細(xì)參見(jiàn)A_Indirection('indirection')
 */
typedef struct ResTarget
{
  NodeTag   type;
  //列名或NULL
  char     *name;     /* column name or NULL */
  //子腳本,字段名稱,'*'或NIL
  List     *indirection;  /* subscripts, field names, and '*', or NIL */
  //需要計(jì)算或賦值的值表達(dá)式
  Node     *val;      /* the value expression to compute or assign */
  //token的位置,-1表示未知
  int     location;   /* token location, or -1 if unknown */
} ResTarget;

ColumnRef
指定對(duì)列或整個(gè)元組的引用

/*
 * ColumnRef - specifies a reference to a column, or possibly a whole tuple
 * 指定對(duì)列或整個(gè)元組的引用
 *
 * The "fields" list must be nonempty.  It can contain string Value nodes
 * (representing names) and A_Star nodes (representing occurrence of a '*').
 * Currently, A_Star must appear only as the last list element --- the grammar
 * is responsible for enforcing this!
 * "fields"鏈表不能為空.可能包含字符串Value節(jié)點(diǎn)和A_Star節(jié)點(diǎn)(表示出現(xiàn)了*).
 * 截止到目前為止,A_Star必須出現(xiàn)在最后一個(gè)鏈表元素中.
 *
 * Note: any container subscripting or selection of fields from composite columns
 * is represented by an A_Indirection node above the ColumnRef.  However,
 * for simplicity in the normal case, initial field selection from a table
 * name is represented within ColumnRef and not by adding A_Indirection.
 */
typedef struct ColumnRef
{
  NodeTag   type;
  //字段名稱(字符串值)鏈表或A_Star
  List     *fields;     /* field names (Value strings) or A_Star */
  //token位置
  int     location;   /* token location, or -1 if unknown */
} ColumnRef;

二、源碼解讀

N/A

三、跟蹤分析

RangeSubselect/Alias

(gdb) p *(Node *)($stmt->fromClause->head.data->ptr_value)
$15 = {type = T_RangeSubselect} #實(shí)際類(lèi)型是范圍子查詢RangeSubselect
(gdb) set $fromclause=(RangeSubselect *)($stmt->fromClause->head.data->ptr_value)
(gdb) p *$fromclause
$16 = {type = T_RangeSubselect, lateral = false, subquery = 0x1666c18, alias = 0x1666d40}
(gdb) p *($fromclause->subquery) #subquery,子查詢是SelectStmt類(lèi)型的節(jié)點(diǎn)
$17 = {type = T_SelectStmt}
(gdb) p *($fromclause->alias) #alias,別名,實(shí)際值是字符串ret
$18 = {type = T_Alias, aliasname = 0x1666d28 "ret", colnames = 0x0}

RangeVar

...
$43 = {type = T_RangeVar}
(gdb) p *(RangeVar *)((JoinExpr *)($joinexpr->larg))->larg
$44 = {type = T_RangeVar, catalogname = 0x0, schemaname = 0x0, relname = 0x1643380 "t_dwxx", inh = true, 
  relpersistence = 112 'p', alias = 0x0, location = 82}
...

ResTarget

...
(gdb) p *(Node *)($subquerylarg->targetList->head.data->ptr_value)
$26 = {type = T_ResTarget}
(gdb) set $subvar=(ResTarget *)($subquerylarg->targetList->head.data->ptr_value)
(gdb) p *$subvar 
{type = T_ResTarget, name = 0x0, indirection = 0x0, val = 0x1642c70, location = 23}
...

ColumnRef

...
(gdb) p *$restarget->val
$25 = {type = T_ColumnRef}
(gdb) p *(ColumnRef *)$restarget->val
$26 = {type = T_ColumnRef, fields = 0x1a47a08, location = 7}
(gdb) p *((ColumnRef *)$restarget->val)->fields
$27 = {type = T_List, length = 2, head = 0x1a47a88, tail = 0x1a479e8}
(gdb) p *(Node *)(((ColumnRef *)$restarget->val)->fields)->head.data->ptr_value
$32 = {type = T_String}
#fields鏈表的第1個(gè)元素是數(shù)據(jù)表,第2個(gè)元素是數(shù)據(jù)列
(gdb) p *(Value *)(((ColumnRef *)$restarget->val)->fields)->head.data->ptr_value
$37 = {type = T_String, val = {ival = 27556248, str = 0x1a47998 "t_dwxx"}}
(gdb) p *(Value *)(((ColumnRef *)$restarget->val)->fields)->tail.data->ptr_value
$38 = {type = T_String, val = {ival = 27556272, str = 0x1a479b0 "dwmc"}}
...

到此,關(guān)于“PostgreSQL在執(zhí)行邏輯優(yōu)化中相關(guān)的數(shù)據(jù)結(jié)構(gòu)有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI