---
title: " PostgreSQL 存储过程调试"
author: "haige18"
date: "2012-12-12T16:03:45Z"
tags: ["PostgreSQL"]
categories: ["数据库"]
slug: "postgresql"
draft: false
lastmod: "2012-12-12T16:05:49Z"
id: 1746527
url: "https://haige18.iteye.com/blog/1746527"
---

在PG-2000DBA的培训中学到的一点是可以用pgAdmin3对存储过程进行调试。我们开发过程中用到了很多存储过程，一直苦于不能调试，只是用NOTICE打印出调试信息，非常麻烦。

开启调试，需进行以下配置：

1.编辑postgresql.conf

```
shared_preload_libraries = '$libdir/plugins/plugin_debugger.so' 
```

2.重新启动数据库restart

3.执行/opt/PostgreSQL/9.1/share/postgresql/contrib/pldbgapi.sql

 

如果使用postgres-9.1.2-1.i386.openscg.rpm这个rpm包安装的数据库，执行以上3个步骤即可开启调试。

```
rpm -ivh postgres-9.1.2-1.i386.openscg.rpm 
```

 

由于我使用以下的几个rpm包安装的postgresql9.1.2，

```

rpm -ivh postgresql91-libs-9.1.2-1PGDG.rhel5.i386.rpm   
rpm -ivh postgresql91-server-9.1.2-1PGDG.rhel5.i386.rpm   
rpm -ivh postgresql91-9.1.2-1PGDG.rhel5.i386.rpm    
rpm -ivh postgresql91-contrib-9.1.2-1PGDG.rhel5.i386.rpm 

```

实际配置过程中发现，我的环境中没有plugin\_debugger.so和pldbgapi.sql这两个文件。

直接把postgres-9.1.2-1.i386.openscg.rpm包安装好的so文件拿来使用，可以配置成功。需要plugin\_debugger.so，targetinfo.so和pldbgapi.so(目录可能是 /opt/postgres/9.1/lib/postgresql/)这三个so文件和pldbgapi.sql文件。

进行如下操作：

1.在postgresql9.1的lib目录(如：/usr/pgsql-9.1/lib/)新建plugins文件夹，拷贝plugin\_debugger.so至plugins目录下

2.拷贝targetinfo.so和pldbgapi.so文件至postgresql9.1的lib目录下

3.编辑postgresql.conf

4.执行pldbgapi.sql，

```
psql -U postgres -h localhost -f /home/pldbgapi.sql 
```

5.重启数据库，配置完成

配置完成后，在pgAdmin3中，右键存储过程会出现调试中的选项，

[![](http://img1.51cto.com/attachment/201203/193409463.png)](http://img1.51cto.com/attachment/201203/193409463.png)

点击调试，设置参数的值，开始调试，

[![](http://img1.51cto.com/attachment/201203/193605828.png)](http://img1.51cto.com/attachment/201203/193605828.png)

设置断点，

[![](http://img1.51cto.com/attachment/201203/193637228.png)](http://img1.51cto.com/attachment/201203/193637228.png)

 

在pgfoundry中有一个开源的edb-debugger插件可以用来调试PostgreSQL的PLPGSQL函数：[http://pgfoundry.org/projects/edb-debugger/](http://pgfoundry.org/projects/edb-debugger/) 

对于postgreSQL9.1可能会有问题，[德哥博客中解决方法](http://blog.163.com/digoal@126/blog/static/163877040201222011550296/)。

from : http://blog.csdn.net/binyao02123202/article/details/7746035