如何在PostgreSQL中调试plpgsql存储过程(pldebugger, pldbgapi)

标签

PostgreSQL , 调试plpgsql


背景

PostgreSQL支持多种存储过程语言,例如plpgsql, C, plpython, plperl, pltcl, pljava, 等等。

用户可以使用这些存储过程语言,创建对应的函数或存储过程(returns void)。

那么如何调试PostgreSQL的存储过程呢?社区提供了一个插件pldebugger,可用于调试存储过程。

https://git.postgresql.org/gitweb/?p=pldebugger.git;a=summary

pldebugger介绍

  16 Installation
  17 ------------
  18
  19 - Copy this directory to contrib/ in your PostgreSQL source tree.
  20
  21 - Run 'make; make install'
  22
  23 - Edit your postgresql.conf file, and modify the shared_preload_libraries config
  24   option to look like:
  25
  26   shared_preload_libraries = '$libdir/plugin_debugger'
  27
  28 - Restart PostgreSQL for the new setting to take effect.
  29
  30 - Run the following command in the database or databases that you wish to
  31   debug functions in:
  32
  33   CREATE EXTENSION pldbgapi;
  34
  35   (on server versions older than 9.1, you must instead run the pldbgapi--1.0.sql
  36   script directly using psql).
  37
  38
  39 Usage
  40 -----
  41
  42 Connect pgAdmin to the database containing the functions you wish to debug.
  43 Right-click the function to debug, and select Debugging->Debug to execute and
  44 debug the function immediately, or select Debugging->Set Global Breakpoint to
  45 set a breakpoint on the function. This will cause the debugger to wait for
  46 another session (such as a backend servicing a web app) to execute the function
  47 and allow you to debug in-context.
  48
  49 For further information, please see the pgAdmin documentation.
  50
  51
  52 Troubleshooting
  53 ---------------
  54
  55 The majority of problems we've encountered with the plugin are caused by
  56 failing to add (or incorrectly adding) the debugger plugin library to the
  57 shared_preload_libraries configuration directive in postgresql.conf (following
  58 which, the server *must* be restarted). This will prevent global breakpoints
  59 working on all platforms, and on some (notably Windows) may prevent the
  60 pldbgapi.sql script from executing correctly.
  61
  62
  63 Architecture
  64 ------------
  65
  66 The debugger consists of three parts:
  67
  68 1. The client. This is typically a GUI displays the source code, current
  69    stack frame, variables etc, and allows the user to set breakpoints and
  70    step throught the code. The client can reside on a different host than
  71    the database server.
  72
  73 2. The target backend. This is the backend that runs the code being debugged.
  74    The plugin_debugger.so library must be loaded into the target backend.
  75
  76 3. Debugging proxy. This is another backend process that the client is
  77    connected to. The API functions, pldbg_* in pldbgapi.so library, are
  78    run in this backend.
  79
  80 The client is to connected to the debugging proxy using a regular libpq
  81 connection. When a debugging session is active, the proxy is connected
  82 to the target via a socket. The protocol between the proxy and the target
  83 backend is not visible to others, and is subject to change. The pldbg_*
  84 API functions form the public interface to the debugging facility.
  85
  86
  87 debugger client  *------ libpq --------* Proxy backend
  88   (pgAdmin)                                 *
  89                                             |
  90                                   pldebugger socket connection
  91                                             |
  92                                             *
  93 application client *----- libpq -------* Target backend

如果在编译pldebugger时遇到如下告警,可以修改一下pldbgapi.c

pldbgapi.c: In function ‘pldbg_get_stack’:
pldbgapi.c:790:25: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘uint64 {aka long unsigned int}’ [-Wformat=]
   sprintf( callCount, "%d", srf->call_cntr );
                         ^  

修改如下  

                /*
                 * frameString points to a string like:
                 *      targetName:funcOID:lineNumber:arguments
                 */
                sprintf( callCount, "%zu", srf->call_cntr );

pldebugger安装

1. 编译软件

git clone git://git.postgresql.org/git/pldebugger.git  

cd pldebugger  

export PATH=/home/digoal/pgsql9.6/bin:$PATH  

USE_PGXS=1 make clean
USE_PGXS=1 make
USE_PGXS=1 make install

2. 修改配置

cd $PGDATA
vi postgresql.conf  

shared_preload_libraries = '$libdir/plugin_debugger'

3. 重启数据库

pg_ctl restart -m fast

如何调试存储过程

1. 在需要调试存储过程的目标数据库中,安装pldbgapi插件

postgres=# create extension pldbgapi ;
CREATE EXTENSION

2. 创建被调试的测试代码(如果已经有目标函数了,请忽略此步骤)

create or replace function debugger_test (i int) returns int as $$
declare
v_result int;
begin
v_result := 0;
if i<0 then
  raise notice 'Please enter i >=0.';
  raise exception '';
end if;
for x in 0..i loop
v_result := v_result + x;
end loop;
return v_result;
exception
when others then
  v_result := 0;
  return v_result;
end;
$$ language plpgsql;

3. 打开pgAdmin客户端,使用pgAdmin登陆到这个数据库, 右键点击函数,点击调试选项。

参考

https://git.postgresql.org/gitweb/?p=pldebugger.git;a=summary

时间: 2024-08-02 10:59:37

如何在PostgreSQL中调试plpgsql存储过程(pldebugger, pldbgapi)的相关文章

如何在ACCESS中调用后台存储过程

ACCESS是一个Client/Server的优秀前端开发工具,具有易学易用,界面友好,开发简单,和其他数据库接口灵活.但是,它要对大量数据处理时,速度比较慢.当有大量数据需要处理时,不能在Client端处理,而必须在Server端处理. 但ACCESS和Server端之间多数通过ODBC来连接,这样就增加了调用后台存储过程的难度.笔者通过在实际工作中长期的摸索,根据不同的业务需要,可以用下面三种方法去调用后台存储过程. 一. Access 向后台提交作业,这一个个作业对应一个个的存储过程.在S

如何在aspx中得到在存储过程中的的数值

这里的存储过程是在SQL Server 下的存储过程,由于目前豆腐没有使用存储过程,所以豆腐在举例子的时候使用了SQL2K,估计在Oracle下应该也是一样的,欢迎大家 和豆腐一起来探讨这个问题我们知道在存储过程中有个被称做output 的参数,他可以在不同的存储过程之中互相的传递参数,在使用output 的时候要注意下面的情况 OUTPUT 变量必须在创建表和使用该变量时都进行定义.参数名和变量名不一定要匹配,不过数据类型和参数位置必须匹配我们创建一个最简单的存储过程CREATE PROCED

如何在Oracle中使用Java存储过程(详解)

其实,这篇短文,我早就应该写了.因为,Java存储过程今后在各大数据库厂商中越来越流行,功能也越来越强大.这里以Oracle为例,介绍一下java存储过程的具体用法. 一.如何创建java存储过程? 通常有三种方法来创建java存储过程. 1.使用oracle的sql语句来创建: e.g. 使用create or replace and compile java source named "<name>" as 后边跟上java源程序.要求类的方法必须是public sta

如何在BCB中调试Active Form

1. Project-->Options-->Compiler-->点击Full Debug 2. Project-->Build All Projects 3. 在需要调试的代码处加断点. 4. Project-->Web Deployment Options-->Directories and URLs (1)Target dir(Full path of the deployed OCX) 填写你的OCX发布的地址,如C:\ccrun\www\ (2)Target

如何在c#中调试被调用的VC动态库?

问题描述 求助. 解决方案 解决方案二:顶上去一直没弄明白

如何在postgresql中模拟oracle的dual表,来测试数据库最基本的连接功能?

还好,网上弄到的,,没有dual的数据库,可以试图用select函数不带from数据表的方式来实现返回值. 一段测试代码:   try: conn = psycopg2.connect(database=db.service_name, user=db.username, password=password, host=db.ip, port=db.port) cursor = conn.cursor() except Exception, e: context_dict = {'msg': e

在Eclipse中调试Maven项目

使用maven的一个方便之处是可以使用Jetty Plugin来运行web项目. 只要maven jetty:run就可以把web项目跑起来了.只是很多时候我们都需要在IDE中进行调试. 那如何在Eclipse中调试使用jetty Plugin的web项目呢? 下面我们就来配置一下. 首先在Run->Externel Tools->Open Externel Tools Dialog.. 打开配置对话框,选中左边的Program节点,右键选择New然后再右边的配置里面输入Name信息, 在Ma

在PL/SQL 开发中调试存储过程和函数的一般性方法

存储过程|函数 在PL/SQL 开发中调试存储过程和函数的一般性方法摘要: Oracle 在PLSQL中提供的强大特性使得数据库开发人员可以在数据库端完成功能足够复杂的任务, 本文将结合Oracle提供的相关程序包(package)以及一个非常优秀的第三方开发工具来介绍在PLSQL中开发及调试存储过程的方法,当然也适用于函数. 版权声明: 本文可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息.原文出处: http://www.aiview.com/notes/ora_using_

在VB.NET中调试存储过程

存储过程 调试是编写应用程序的一个主要部分.Visual Studio .NET为自带的调试器提供了大量的增强性能,包括统一的调试界面.Web service调试以及跨语言调试.这种调试器的一个最有用的新功能体现在对存储过程的调试性能上.本文我将介绍在VS.NET中对SQL Server 2000存储过程进行调试的几种可选方法,以及你可能会遇到的一些配置问题. 许多商家使用SQL Server作为数据库,并将存储过程作为将数据返回VB中的机制,因此对存储过程进行逐步调试的能力成为开发的关键.Vi