绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
在PostgreSQL中语法移进和规约过程
2020-05-19 17:31:37

在PostgreSQL中改写语法时,经常需要查看语法树,或者希望能够语法中的移进和规约过程。

查看语法树,我们可以设置参数[1]

# - What to Log -

#debug_print_parse = off
#debug_print_rewritten = off
#debug_print_plan = off
#debug_pretty_print = on
debug_print_parse (boolean)
debug_print_rewritten (boolean)
debug_print_plan (boolean)
These parameters enable various debugging output to be emitted. When set, they print the resulting parse tree, the query rewriter output, or the execution plan for each executed query. These messages are emitted at LOG message level, so by default they will appear in the server log but will not be sent to the client. You can change that by adjusting client_min_messages and/or log_min_messages. These parameters are off by default.
debug_pretty_print (boolean)
When set, debug_pretty_print indents the messages produced by debug_print_parse, debug_print_rewritten, or debug_print_plan. This results in more readable but much longer output than the “compact” format used when it is off. It is on by default.

查看移进和规约过程

在gram.c中修改以下内容:

/* Copy the first part of user declarations.  */
#line 1 "gram.y" /* yacc.c:339  */


#define YYDEBUG 1 /* 去掉注释 */

/* Nonzero means print parse trace.  It is left uninitialized so that
   multiple parsers can coexist.  */
int yydebug = 1; /* 赋值为1 */

重新启动数据库,查询SQL,得到移进和规约过程:

[postgres@izj6ccelicizfq7z026sowz bin]$ ./pg_ctl -D ../data restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2020-04-04 23:36:00.004 CST [20380] LOG:  starting PostgreSQL 13devel on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
2020-04-04 23:36:00.005 CST [20380] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2020-04-04 23:36:00.005 CST [20380] LOG:  could not bind IPv6 address "::1": Cannot assign requested address
2020-04-04 23:36:00.005 CST [20380] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2020-04-04 23:36:00.007 CST [20380] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-04-04 23:36:00.012 CST [20381] LOG:  database system was shut down at 2020-04-04 23:35:59 CST
2020-04-04 23:36:00.024 CST [20380] LOG:  database system is ready to accept connections
 done
server started
[postgres@izj6ccelicizfq7z026sowz bin]$ ./psql 
psql (13devel)
Type "help" for help.

postgres=# select pg_backend_pid();
Starting parse
Entering state 0
Reading a token: Next token is token SELECT (: )
Shifting token SELECT (: )
Entering state 40
Reading a token: Next token is token IDENT (: )
Reducing stack by rule 1699 (line 11677):
-> $$ = nterm opt_all_clause (: )
Stack now 0 40
Entering state 791
Next token is token IDENT (: )
Shifting token IDENT (: )
Entering state 228
Reading a token: Next token is token '(' (: )
Reducing stack by rule 2286 (line 15128):
   $1 = token IDENT (: )
-> $$ = nterm type_function_name (: )
Stack now 0 40 791
Entering state 599
Reducing stack by rule 2257 (line 14915):
   $1 = nterm type_function_name (: )
-> $$ = nterm func_name (: )
Stack now 0 40 791
Entering state 1312
Next token is token '(' (: )
Shifting token '(' (: )
Entering state 2068
Reading a token: Next token is token ')' (: )
Shifting token ')' (: )
Entering state 1620
Reducing stack by rule 2032 (line 13742):
   $1 = nterm func_name (: )
   $2 = token '(' (: )
   $3 = token ')' (: )
-> $$ = nterm func_application (: )
Stack now 0 40 791
Entering state 1301
Reading a token: Next token is token ';' (: )
Reducing stack by rule 2108 (line 14166):
-> $$ = nterm within_group_clause (: )
Stack now 0 40 791 1301
Entering state 2062
Next token is token ';' (: )
Reducing stack by rule 2110 (line 14171):
-> $$ = nterm filter_clause (: )
Stack now 0 40 791 1301 2062
Entering state 2849
Next token is token ';' (: )
Reducing stack by rule 2118 (line 14214):
-> $$ = nterm over_clause (: )
Stack now 0 40 791 1301 2062 2849
Entering state 3575
Reducing stack by rule 2039 (line 13811):
   $1 = nterm func_application (: )
   $2 = nterm within_group_clause (: )
   $3 = nterm filter_clause (: )
   $4 = nterm over_clause (: )
-> $$ = nterm func_expr (: )
Stack now 0 40 791
Entering state 1302
Reducing stack by rule 2023 (line 13647):
   $1 = nterm func_expr (: )
-> $$ = nterm c_expr (: )
Stack now 0 40 791
Entering state 1300
Reducing stack by rule 1929 (line 13097):
   $1 = nterm c_expr (: )
-> $$ = nterm a_expr (: )
Stack now 0 40 791
Entering state 1299
Next token is token ';' (: )
Reducing stack by rule 2243 (line 14813):
   $1 = nterm a_expr (: )
-> $$ = nterm target_el (: )
Stack now 0 40 791
Entering state 1311
Reducing stack by rule 2239 (line 14785):
   $1 = nterm target_el (: )
-> $$ = nterm target_list (: )
Stack now 0 40 791
Entering state 1318
Next token is token ';' (: )
Reducing stack by rule 2237 (line 14780):
   $1 = nterm target_list (: )
-> $$ = nterm opt_target_list (: )
Stack now 0 40 791
Entering state 1317
Next token is token ';' (: )
Reducing stack by rule 1681 (line 11596):
-> $$ = nterm into_clause (: )
Stack now 0 40 791 1317
Entering state 2071
Next token is token ';' (: )
Reducing stack by rule 1762 (line 11959):
-> $$ = nterm from_clause (: )
Stack now 0 40 791 1317 2071
Entering state 2865
Next token is token ';' (: )
Reducing stack by rule 1822 (line 12396):
-> $$ = nterm where_clause (: )
Stack now 0 40 791 1317 2071 2865
Entering state 3604
Next token is token ';' (: )
Reducing stack by rule 1732 (line 11834):
-> $$ = nterm group_clause (: )
Stack now 0 40 791 1317 2071 2865 3604
Entering state 4159
Next token is token ';' (: )
Reducing stack by rule 1745 (line 11886):
-> $$ = nterm having_clause (: )
Stack now 0 40 791 1317 2071 2865 3604 4159
Entering state 4581
Next token is token ';' (: )
Reducing stack by rule 2112 (line 14180):
-> $$ = nterm window_clause (: )
Stack now 0 40 791 1317 2071 2865 3604 4159 4581
Entering state 4851
Reducing stack by rule 1662 (line 11458):
   $1 = token SELECT (: )
   $2 = nterm opt_all_clause (: )
   $3 = nterm opt_target_list (: )
   $4 = nterm into_clause (: )
   $5 = nterm from_clause (: )
   $6 = nterm where_clause (: )
   $7 = nterm group_clause (: )
   $8 = nterm having_clause (: )
   $9 = nterm window_clause (: )
-> $$ = nterm simple_select (: )
Stack now 0
Entering state 184
Next token is token ';' (: )
Reducing stack by rule 1652 (line 11371):
   $1 = nterm simple_select (: )
-> $$ = nterm select_no_parens (: )
Stack now 0
Entering state 182
Reducing stack by rule 1648 (line 11350):
   $1 = nterm select_no_parens (: )
-> $$ = nterm SelectStmt (: )
Stack now 0
Entering state 180
Reducing stack by rule 119 (line 947):
   $1 = nterm SelectStmt (: )
-> $$ = nterm stmt (: )
Stack now 0
Entering state 54
Reducing stack by rule 3 (line 822):
   $1 = nterm stmt (: )
-> $$ = nterm stmtmulti (: )
Stack now 0
Entering state 53
Next token is token ';' (: )
Shifting token ';' (: )
Entering state 832
Reading a token: Now at end of input.
Reducing stack by rule 129 (line 958):
-> $$ = nterm stmt (: )
Stack now 0 53 832
Entering state 1365
Reducing stack by rule 2 (line 810):
   $1 = nterm stmtmulti (: )
   $2 = token ';' (: )
   $3 = nterm stmt (: )
-> $$ = nterm stmtmulti (: )
Stack now 0
Entering state 53
Now at end of input.
Reducing stack by rule 1 (line 794):
   $1 = nterm stmtmulti (: )
-> $$ = nterm stmtblock (: )
Stack now 0
Entering state 52
Now at end of input.
Shifting token $end (: )
Entering state 831
Stack now 0 52 831
Cleanup: popping token $end (: )
Cleanup: popping nterm stmtblock (: )
 pg_backend_pid 
----------------
          20389
(1 row)

postgres=#

参考

  1. ^https://www.postgresql.org/docs/current/runtime-config-logging.html
分享好友

分享这个小栈给你的朋友们,一起进步吧。

华山论剑
创建时间:2019-02-22 18:53:00
没了烟火气,人生就是一段孤独的旅程·····于是,在ITPUB,我们以武论英雄!
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

栈主、嘉宾

查看更多
  • 栈栈
    栈主
  • ?
    嘉宾

小栈成员

查看更多
  • u_9a3ed7a37f8e4a
  • daisyplay
  • boss_ch
  • Jack2k
戳我,来吐槽~