PG 通过 auto_explain 模块查看函数中的 SQL 的执行计划,从而进行性能优化(瓶颈定位)

重启数据库方式

  • 修改配置文件
	vim /mnt/syncdata/pgsql/data/postgresql.conf
	shared_preload_libraries = 'auto_explain'  

	或者如果 shared_preload_libraries 已经有值的话
	shared_preload_libraries = 'pg_stat_statements , auto_explain'

	然后添加以下 5 条配置(后面四个配置可选) log_min_duration单位为毫秒
	auto_explain.log_min_duration = 100
	auto_explain.log_analyze = true  
	auto_explain.log_verbose = true  
	auto_explain.log_buffers = true  
	auto_explain.log_nested_statements = true  

  • 重启数据库服务器
pg_ctl restart -m fast -D $PGDATA
  • 业务操作或者执行存储过程操作时, 此时会在 pg_log 日志中打印相关 SQL (执行时间超过之前在配置文件中配置的 100ms ) 执行计划

不重启数据库方式

  • 以postgres用户登录数据库
  • 加载模块
postgres=> load 'auto_explain';  
  • 设置 log_min_duration 单位为ms
postges=> set auto_explain.log_min_duration=1000;
  • 业务操作或者执行存储过程操作时, 此时会在 pg_log 日志中打印相关 SQL 执行计划

如果想在屏幕上打印相关执行计划

做如下设置

	set client_min_messages='log';
	set auto_explain.log_min_duration = 1000;
	set auto_explain.log_analyze = true;
	set auto_explain.log_verbose = true;
	set auto_explain.log_buffers = true;
	set auto_explain.log_nested_statements = true;

参考:https://github.com/digoal/blog/blob/master/201611/20161121_02.md

原文地址:https://www.cnblogs.com/yldf/p/11899966.html