轉載自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 列出所有數據庫
參考資料: