如何调试postgres存储过程

906 阅读1分钟
原文链接: zhuanlan.zhihu.com

Raise Notice之于postgres,相当于print之于python。偶尔可以用一下,但如果有正经八百的开发工作,还是得使用调试工具。


1. 安装pgAdmin Debugger

postgres的官方存储过程调试工具是pgAdmin Debugger,它是一个服务器扩展。这个工具并不随postgresql-server一起安装,也不随pgAdmin一起安装,因此你得自己下载安装。

这里以Ubuntu为例介绍其安装: 首先它是不支持apt install安装的。访问https://packages.ubuntu.com/bionic/postgresql-10-pldebugger网页,根据你的操作系统架构,将二进制文件下载到本地。它是一个.deb安装包,然后执行:

sudo dpkg -i postgresql-10-pldebugger_1.0-3-g71ac998-1_amd64.deb
---output---
(Reading database ... 84865 files and directories currently installed.)
Preparing to unpack postgresql-10-pldebugger_1.0-3-g71ac998-1_amd64.deb ...
Unpacking postgresql-10-pldebugger (1:1.0-3-g71ac998-1) over (1:1.0-3-g71ac998-1) ...
Setting up postgresql-10-pldebugger (1:1.0-3-g71ac998-1) ...
Processing triggers for postgresql-common (192.pgdg16.04+1) ...
Building PostgreSQL dictionaries from installed myspell/hunspell packages...
Removing obsolete dictionary files:

2. 配置

修改/etc/postgres/10/main/postgresql.conf如下:

shared_preload_libraries = '$libdir/plugin_debugger'

注意这里的值原样照写就可以了。 然后重启服务器

sudo service postgresql restart

检查/var/log/postgresql下的main*.log,看是否正常启动了。在查询窗口执行

show shared_preload_libraries
---output---
$libdir/plugin_debugger

应该出现如上输出,说明安装成功。

3. 在客户端启用调试

  1. 首先,在pgadmin窗口中加载调试扩展,注意这个扩展的加载范围是数据库级别的:

2. 加载存储过程,启动调试。注意你必须先写好存储过程,存储到服务器端,然后再加载,不可以在query tool窗口中一边写一边调试。