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


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

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

    比较Oracle PL/SQL 

    PL/SQL 中有 dbms_output.put_line("This is a log"); 可以进行简单的调试

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

    % 占位符 param 替换的值

    RAISE 还有其他级别 DEBUG,LOG,INFO,EXCEPTION 

    可以在配置文件中配置它们 postgresql.conf 中的 client_min_messages 和 log_min_messages

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

     

    举一个简单的例子吧

    举例之前 首先确定自己的数据库有没有安装 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

  • 相关阅读:
    hdu4622(后缀自动机模板)
    51nod1445(最短路)
    Java原子类--框架
    Java锁--Semaphore
    Java锁--CyclicBarrier
    Java锁--CountDownLatch
    Java锁--共享锁和ReentrantReadWriteLock
    Java锁--LockSupport
    Java锁--Condition
    Java锁--非公平锁
  • 原文地址:https://www.cnblogs.com/wenwujuncheng/p/3960211.html
Copyright © 2020-2023  润新知