How can I use "IF statements" in a postgres trigger
Posted
by Dan B
on Stack Overflow
See other posts from Stack Overflow
or by Dan B
Published on 2010-06-16T19:58:29Z
Indexed on
2010/06/16
20:02 UTC
Read the original article
Hit count: 158
postgresql
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();
© Stack Overflow or respective owner