Postgresql存储过程调试:PostgreSQL 之 Function NOTICE

转载自http://zhenghaoju700.blog.163.com/blog/static/13585951820116782843994/html

先安装一个PostgreSQL(见补充知识)sql

比较Oracle PL/SQL shell

PL/SQL 中有 dbms_output.put_line("This is a log"); 能够进行简单的调试数据库

固然咱们PostgreSQL 也有相应的函数 RAISE NOTICE 'This is a log %', param;ubuntu

% 占位符 param 替换的值vim

RAISE 还有其余级别 DEBUG,LOG,INFO,EXCEPTION ide

能够在配置文件中配置它们 postgresql.conf 中的 client_min_messages 和 log_min_messages函数

RAISE EXCEPTION 会暂停函数的运行,其余级别默认应该是显示到客户端。oop

 

举一个简单的例子吧post

举例以前 首先肯定本身的数据库有没有安装 plpgsql

select * from pg_language;

若是没有的话 

create language plpgsql;

 

简单例子:

CREATE OR REPLACE FUNCTION perctl.testraise()
  RETURNS void AS
$$
declare
begin
raise notice '===============';
while(2>1) loop
raise notice '====================================================================================================================================================================================';
raise notice '====================================================================================================================================================================================';
raise notice '====================================================================================================================================================================================';
raise notice '====================================================================================================================================================================================';
raise notice '====================================================================================================================================================================================';
raise notice '====================================================================================================================================================================================';
raise notice '====================================================================================================================================================================================';
raise notice '====================================================================================================================================================================================';
raise notice '====================================================================================================================================================================================';
raise notice '====================================================================================================================================================================================';
end loop;
raise log '===============';
end;
$$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION perctl.fx_test_outofmemory_1()
  OWNER TO root;

测试一下

select TESTRAISE('this is a message');

 

为何只有 Notice 和 INFO 信息呢?

经过 find / -name  postgresql 查找postgresql 安装目录

 

hjzheng@ubuntu:/etc/postgresql/8.4/main$ pwd

/etc/postgresql/8.4/main

 

在etc下查看配置文件  vim postgresql.conf 

 

看到客户端最小显示级别是NOTICE 因此必须大于Notice 才显示 这就是log为何没有显示

DEBUG 没显示 是由于它们只有DEBUG(1-5)

这样就能够放心调试了!!!!

 

 

补充知识:

安装环境 Ubuntu 11.03

1.sudo apt-get install postgreSQL

2.sudo passwd postgres 改密码 这个用户是数据库本身建立的

3.启动服务 service postgresql start

4.su  postgres

5.psql 打开交互shell

6.\l 列出全部数据库

 

 

参考资料:

http://www.postgresonline.com/journal/archives/83-Quick-Guide-to-writing-PLPGSQL-Functions-Part-3-NOTICES,-RECURSION,-and-more.html

相关文章
相关标签/搜索