views:

41

answers:

1

I have a trigger function that I only want to fire on certain instances of INSERTS, in this case, if do_backup = true. If it fires in all instances, I get an infinite loop. The logic seems pretty simple to me, and the rest of the function works. But the trigger function does not seem to register my conditional and always runs, even when backup = true.

CREATE OR REPLACE FUNCTION table_styles_backup() RETURNS
 TRIGGER AS $table_styles_backup$

 DECLARE
 ...
 do_backup  boolean;

    BEGIN
 SELECT backup INTO do_backup FROM table_details WHERE id=NEW.table_meta_id;

        IF (do_backup = true) THEN 

  ...


            INSERT INTO table_styles_versions 
   (
   ...
   )

  VALUES (
   ...
   );
 END IF;

    RETURN NULL;

    END;

$table_styles_backup$  LANGUAGE plpgsql;

CREATE TRIGGER table_styles_backup AFTER INSERT ON table_styles

FOR EACH ROW EXECUTE PROCEDURE table_styles_backup();
+1  A: 

Per the documentation, you don't need to explicitly test boolean data types in an IF statement - they default to test for being true so you'd use:

IF do_backup THEN
  ...
  INSERT INTO table_styles_versions (...
ELSE
  -- omit this & the ELSE keyword if nothing needs to happen if do_backup is false
END IF;
OMG Ponies
that seems to do the trick, thanks!
Dan B