title: How can I use "IF statements" in a postgres trigger
body: <p>I have a trigger function that I only want to fire on certain instances of INSERTS, in this case, <code>if do_backup = true</code>. 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.</p>
  
  <pre><code>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();
  </code></pre>