DROP TABLE IF EXISTS trigtest;

CREATE TABLE trigtest(x INTEGER);

CREATE OR REPLACE FUNCTION trigtest_new_func1()
RETURNS TRIGGER AS $$
BEGIN
	NEW.x = NEW.x + 10;
	RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION trigtest_new_func2()
RETURNS TRIGGER AS $$
BEGIN
	NEW.x = NEW.x + 100;
	RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigtest_trigger1
BEFORE INSERT ON trigtest
FOR EACH ROW EXECUTE PROCEDURE trigtest_new_func1();

CREATE TRIGGER trigtest_trigger2
AFTER INSERT ON trigtest
FOR EACH ROW EXECUTE PROCEDURE trigtest_new_func2();

CREATE OR REPLACE FUNCTION trigtest_old_func1()
RETURNS TRIGGER AS $$
BEGIN
	OLD.x = OLD.x + 1000;
	RETURN OLD;
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION trigtest_old_func2()
RETURNS TRIGGER AS $$
BEGIN
	OLD.x = OLD.x + 10000;
	RETURN OLD;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trigtest_trigger3
BEFORE DELETE ON trigtest
FOR EACH ROW EXECUTE PROCEDURE trigtest_old_func1();

CREATE TRIGGER trigtest_trigger4
AFTER DELETE ON trigtest
FOR EACH ROW EXECUTE PROCEDURE trigtest_old_func2();

INSERT INTO trigtest VALUES (1) RETURNING x;

SELECT * FROM trigtest;

DELETE FROM trigtest RETURNING x + 1000000;
