2015-10-16 94 views

回答

11

使用目錄pg_trigger

簡單的查找一個表books

select tgname 
from pg_trigger 
where not tgisinternal 
and tgrelid = 'books'::regclass; 

    tgname  
--------------- 
books_trigger 
(1 row) 

使用pg_proc得到觸發功能的來源:

select pg_get_triggerdef(t.oid) as "trigger declaration" 
from pg_trigger t 
where not tgisinternal 
and tgrelid = 'books'::regclass; 

              trigger declaration     
-------------------------------------------------------------------------------------------------------------- 
CREATE TRIGGER books_trigger BEFORE INSERT OR UPDATE ON books FOR EACH ROW EXECUTE PROCEDURE books_trigger() 
(1 row) 
+0

select tgname, proname, prosrc from pg_trigger join pg_proc p on p.oid = tgfoid where not tgisinternal and tgrelid = 'books'::regclass; tgname | proname | prosrc ---------------+---------------+------------------------------------------------ books_trigger | books_trigger | + | | begin + | | if tg_op = 'UPDATE' then + | | if new.listorder > old.listorder then + | | update books + | | set listorder = listorder- 1 + | | where listorder <= new.listorder + | | and listorder > old.listorder + | | and id <> new.id; + | | else + | | update books + | | set listorder = listorder+ 1 + | | where listorder >= new.listorder + | | and listorder < old.listorder + | | and id <> new.id; + | | end if; + | | else + | | update books + | | set listorder = listorder+ 1 + | | where listorder >= new.listorder + | | and id <> new.id; + | | end if; + | | return new; + | | end (1 row) 

pg_get_triggerdef()功能的使用的實施例我實際上已經嘗試過,並且很困惑。奇怪的是:我得到7'tgname's看起來像這樣'RI_ConstraintTrigger_c_195564',而不是1.我使用postgresql 9.4。 – dredozubov

+1

它們是內部約束觸發器。使用'not tgisinternal'來跳過它們,就像在編輯的答案中一樣。 – klin

+0

謝謝!它幫助! – dredozubov