PostgreSQLのexec_simple_query関数について、前半の大まかな処理フローをMermaidで可視化してみる

PostgreSQLのexec_simple_query関数は、クエリ文字列を構文解析する関数などを呼び出しながら、クエリ文字列をparsetree_list、querytree_list、plantree_listに変換していきます。この処理の大まかなフローを、今回は図として描いてみます。図を描くには、Mermaidを使います。

```mermaid
flowchart TB
  subgraph exec_simple_query
    direction TB
    1_([char *query_string])
    2_[pg_parse_query]
    subgraph 6_[List *parse_treelist]
      direction TB
      3_([RawStmt *parsetree\n* NodeTag type\n* Node *stmt -> InsertStmt, DeleteStmt, ...\n* int stmt_location\n* int stmt_len])
      4_([RawStmt *parsetree\n* NodeTag type\n* Node *stmt -> InsertStmt, DeleteStmt, ...\n* int stmt_location\n* int stmt_len])
      5_([...])
    end
    7_[pg_analyze_and_rewrite_fixedparams]
    24_[...]
    subgraph 8_[List *querytree_list]
      direction TB
      9_([Query *query\n* NodeTag type\n* CmdType commandType\n* QuerySource querySource\n * ...])
      10_([Query *query\n* NodeTag type\n* CmdType commandType\n* QuerySource querySource\n * ...])
      11_([...])
    end
    12_[pg_plan_queries]
    subgraph 13_[List *plantree_list]
      direction TB
      14_([PlannedStmt *stmt\n* NodeTag type\n* CmdType commandType\n* uint64 queryId\n * ...])
      15_([PlannedStmt *stmt\n* NodeTag type\n* CmdType commandType\n* uint64 queryId\n * ...])
      16_([...])
    end
    17_[PortalDefineQuery]
    18_([Portal *portal\n* const char *name\n* const char *prepStmtName\n* ...\n* List *stmt -> plantree_list\n* ...])
    subgraph 19_[PortalStart]
      direction LR
      subgraph 20_[PORTAL_ONE_SELECT]
        direction TB
        21_[portal->queryDesc = CreateQueryDesc]
        22_[ExecutorStart]
      end
    end
    23_[PortalRun]

    1_ --> 2_
    2_ --> 6_
    3_ --> 7_
    7_ --> 8_
    8_ --> 12_
    12_ --> 13_
    13_ --> 17_
    17_ --> 18_
    18_ --> 19_
    21_ --> 22_
    19_ --> 23_
    4_ --> 24_
  end
```

## 以下は参考のメモ
```mermaid
flowchart TB
  2_[HeapTupleSatisfiesVisibility]
  3_[heapgetpage]
  4_[heapgettup_pagemode]
  5_[heap_getnextslot]
  6_[table_scan_getnextslot]
  7_[SeqNext]
  8_[ExecScanFetch]
  9_[ExecScan]
  10_[ExecSeqScan]
  11_[ExecProcNodeFirst]
  12_[ExecProcNode]
  13_[ExecutePlan]
  14_[standard_ExecutorRun]
  15_[ExecutorRun]
  16_[PortalRunSelect]
  17_[PortalRun]
  17_ -- Portal--> 16_
  16_ -- QueryDesc: portal->queryDesc --> 15_
  15_ -- QueryDesc: portal->queryDesc --> 14_
  14_ -- PlanState: portal->queryDesc->planstate --> 13_
  13_ -- PlanState: portal->queryDesc->planstate --> 12_
  12_ -- PlanState: portal->queryDesc->planstate --> 11_
  11_ -- PlanState: portal->queryDesc->planstate --> 10_
  10_ -- ScanState:\nportal->queryDesc->planstate\n-> SeqScanState *node\n-> node->ss --> 9_
  9_ -- ScanState --> 8_
  8_ -- SeqScanState: ScanState -> SeqScanState --> 7_
  7_ -- TableScanDesc: scandesc = table_begin_scan --> 6_
  6_ -- TableScanDesc --> 5_
  5_ -- HeapScanDesc: TableScanDesc -> HeapScanDesc --> 4_
  4_ -- TableScanDesc: HeapScanDesc -> TableScanDesc --> 3_
  3_ --> 2_
```

```mermaid
classDiagram
  class Portal{
    const char *name;
	List	   *stmts;
	QueryDesc  *queryDesc;
	Snapshot	portalSnapshot;
	Snapshot	holdSnapshot;
    ...
  }
  class QueryDesc{
    CmdType		operation;
    PlannedStmt *plannedstmt;
    Snapshot	snapshot;
    Snapshot	crosscheck_snapshot;
    EState	   *estate;
    PlanState  *planstate;
    ...
  }
  class PlannedStmt{
    NodeTag		type;
    CmdType		commandType;
    uint64		queryId;
    struct Plan *planTree;
    ...
  }
  class EState{
    NodeTag		type;
    Snapshot	es_snapshot;
    Snapshot	es_crosscheck_snapshot;
    PlannedStmt *es_plannedstmt;
    ...
  }
  class PlanState{
    NodeTag		type;
    Plan	   *plan;
    EState	   *state;
    struct PlanState *lefttree;
    struct PlanState *righttree;
    ...
  }
  class Plan{
    NodeTag		type;
    Cost		startup_cost;
    Cost		total_cost;
    struct Plan *lefttree;
    struct Plan *righttree;
    ...
  }
  class ScanState{
    PlanState	ps;
    struct TableScanDescData *ss_currentScanDesc;
    ...
  }
  class SeqScanState{
    ScanState	ss;
    Size		pscan_len;
  }
  class TableScanDesc{
    Relation	rs_rd;
    struct SnapshotData *rs_snapshot;
    int			rs_nkeys;
    struct ScanKeyData *rs_key;
    ...
  }
  class HeapScanDesc{
    TableScanDescData rs_base;
    BlockNumber rs_nblocks;
    BufferAccessStrategy rs_strategy;
    ...
  }

  Portal --> QueryDesc
  QueryDesc --> PlannedStmt
  QueryDesc --> EState
  QueryDesc --> PlanState
  PlanState --> EState
  PlanState --> PlanState
  EState --> PlannedStmt
  PlanState --> Plan
  PlannedStmt --> Plan
  Plan --> Plan
  ScanState --> PlanState
  SeqScanState --> ScanState
  PlanState ..|> SeqScanState : castNode
  HeapScanDesc --> TableScanDesc
  TableScanDesc ..|> HeapScanDesc : cast
  HeapScanDesc ..|> TableScanDesc : cast
```

```
2   postgres     0x00000001031c8504 HeapTupleSatisfiesVisibility + 132
3   postgres     0x00000001031ad7f4 heapgetpage + 548
4   postgres     0x00000001031aea68 heapgettup_pagemode + 296
5   postgres     0x00000001031aef4b heap_getnextslot + 75
6   postgres     0x00000001034d4d4b table_scan_getnextslot + 299
7   postgres     0x00000001034d4b7c SeqNext + 140
8   postgres     0x000000010348e85b ExecScanFetch + 699
9   postgres     0x000000010348e472 ExecScan + 114
10  postgres     0x00000001034d4735 ExecSeqScan + 53
11  postgres     0x0000000103489bf2 ExecProcNodeFirst + 82
12  postgres     0x00000001034815d2 ExecProcNode + 50
13  postgres     0x000000010347c9e1 ExecutePlan + 209
14  postgres     0x000000010347c87b standard_ExecutorRun + 571
15  postgres     0x000000010347c632 ExecutorRun + 98
16  postgres     0x00000001037643d4 PortalRunSelect + 308
17  postgres     0x0000000103763db9 PortalRun + 809
18  postgres     0x000000010375f1bb exec_simple_query + 1467
19  postgres     0x000000010375e2ad PostgresMain + 2221
20  postgres     0x000000010366b942 BackendRun + 50
21  postgres     0x000000010366921c BackendStartup + 524
22  postgres     0x0000000103666bfe ServerLoop + 350
23  postgres     0x0000000103665850 PostmasterMain + 6496
24  postgres     0x0000000103510742 main + 818
25  dyld         0x00007ff812095310 start + 2432
```