Skip to content

Debug Postgresql

查询客户端连接上的服务进程id

sql
postgres=# select pg_backend_pid();
 pg_backend_pid
----------------
          34255
(1 row)

postgres=# select 1;

开启debug日志

postgresql.conf

bash
#debug_print_parse = off
debug_print_parse = on

#debug_print_rewritten = off
debug_print_rewritten = on

#debug_print_plan = off
debug_print_plan = on

#debug_pretty_print = on
debug_pretty_print = on
cpp
pg_rewrite_query()
{

    // debug_print_parse = on
	if (Debug_print_parse)
		elog_node_display(LOG, "parse tree", query,
						  Debug_pretty_print);

    // debug_print_rewritten = on
    if (Debug_print_rewritten)
		elog_node_display(LOG, "rewritten parse tree", querytree_list,
						  Debug_pretty_print);

}

pg_plan_query(){
    // debug_print_plan = on
	if (Debug_print_plan)
		elog_node_display(LOG, "plan", plan, Debug_pretty_print);
}

GDB

bash
(gdb) call elog_node_display(17, "what ever", Node * var, 0 or 1)
  • 17代表INFO,详见elog.h,比如希望打印在日志里,将17替换为16,
  • 0或1取决于debug_pretty_print为on还是off

pgNodeGraph

将打印信息图形化工具:

https://github.com/shenyuflying/pgNodeGraph

https://github.com/japinli/pg_node2graph

依赖 graphviz

bash
brew install graphviz

例如:

sql
select 1;

说明:

  • 第一个图基于pgNodeGraph
  • 第二个图基于pg_node2graph

parsetree_list

1、parse tree

2、rewritten

3、plan

ref

PostgreSQL数据库查询——exec_simple_query函数分析

https://www.cnblogs.com/feishujun/p/PostgreSQLSourceAnalysis_query001.html