How to check if trigger exists in PostgreSQL?

前端 未结 1 1225
梦毁少年i
梦毁少年i 2020-12-11 01:27

I want to verify correctness of database migrations which add triggers to some tables. I\'m using sqitch, so I\'d like to find a way to check it with SQL queries. I believe

相关标签:
1条回答
  • 2020-12-11 02:08

    Use the catalog pg_trigger.

    A simple lookup for a table books:

    select tgname
    from pg_trigger
    where not tgisinternal
    and tgrelid = 'books'::regclass;
    
        tgname     
    ---------------
     books_trigger
    (1 row)
    

    Using pg_proc to get the source of the trigger function:

    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)
    

    Example of the pg_get_triggerdef() function usage:

    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) 
    

    In a Sqitch verify script you can use an anonymous code block, e.g.:

    do $$
    begin
        perform tgname
        from pg_trigger
        where not tgisinternal
        and tgrelid = 'books'::regclass;
        if not found then 
            raise exception 'trigger not found';
        end if;
    end $$;
    
    0 讨论(0)
提交回复
热议问题