Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

debugging postgresql trigger

I have this Trigger in Postgresql that I can't just get to work (does nothing). For understanding, there's how I defined it:

CREATE TABLE documents (     ...     modification_time timestamp with time zone DEFAULT now() );  CREATE FUNCTION documents_update_mod_time() RETURNS trigger AS $$     begin     new.modification_time := now();     return new;     end $$     LANGUAGE plpgsql;  CREATE TRIGGER documents_modification_time     BEFORE INSERT OR UPDATE ON documents     FOR EACH ROW     EXECUTE PROCEDURE documents_update_mod_time(); 

Now to make it a bit more interesting.. How do you debug triggers?

like image 931
Dave Vogt Avatar asked Nov 20 '08 18:11

Dave Vogt


People also ask

How do I test triggers in PostgreSQL?

We can check available triggers by running the following query. SELECT * FROM information_schema. triggers; We can also use EXPLAIN to show triggers which are executed for an event by running relevant queries.

How do I call a trigger in PostgreSQL?

Syntax. CREATE TRIGGER trigger_name [BEFORE|AFTER|INSTEAD OF] event_name ON table_name [ -- Trigger logic goes here.... ]; Here, event_name could be INSERT, DELETE, UPDATE, and TRUNCATE database operation on the mentioned table table_name. You can optionally specify FOR EACH ROW after table name.

How do I debug a Postgres function in PgAdmin?

To set a breakpoint at the first line of a program, right-click the name of the object you would like to debug, and select Set breakpoint from the Debugging sub-menu. The debugger window will open, waiting for another session to invoke the program.


1 Answers

  1. Use the following code within a trigger function, then watch the 'messages' tab in pgAdmin3 or the output in psql:

    RAISE NOTICE 'myplpgsqlval is currently %', myplpgsqlval;       -- either this RAISE EXCEPTION 'failed';  -- or that 
  2. To see which triggers actually get called, how many times etc, the following statement is the life-saver of choice:

    EXPLAIN ANALYZE UPDATE table SET foo='bar'; -- shows the called triggers 

    Note that if your trigger is not getting called and you use inheritance, it may be that you've only defined a trigger on the parent table, whereas triggers are not inherited by child tables automatically.

  3. To step through the function, you can use the debugger built into pgAdmin3, which on Windows is enabled by default; all you have to do is execute the code found in ...\8.3\share\contrib\pldbgapi.sql against the database you're debugging, restart pgAdmin3, right-click your trigger function, hit 'Set Breakpoint', and then execute a statement that would cause the trigger to fire, such as the UPDATE statement above.

like image 74
Kev Avatar answered Oct 18 '22 09:10

Kev