DROP TABLE trig_tst1;
CREATE TABLE trig_tst1(
	pk	serial,
	vli	int4,
	vlt	text
	);

DROP TABLE trig_tst2;
CREATE TABLE trig_tst2(
	opr	int4,
	npk	int4,
	opk	int4,
	vli	int4,
	vlt	text
	);

CREATE OR REPLACE FUNCTION tf_ins() RETURNS TRIGGER AS '
BEGIN
	INSERT INTO trig_tst2(opr,npk,vli,vlt) VALUES (1,NEW.pk,NEW.vli,NEW.vlt);
	RETURN NEW;
END;
' LANGUAGE 'plpgsql' VOLATILE;

CREATE OR REPLACE FUNCTION tf_updt() RETURNS TRIGGER AS '
BEGIN
	INSERT INTO trig_tst2(opr,npk,opk,vli,vlt) VALUES (2,NEW.pk,OLD.pk,NEW.vli,NEW.vlt);
	RETURN NEW;
END;
' LANGUAGE 'plpgsql' VOLATILE;

CREATE OR REPLACE FUNCTION tf_DEL() RETURNS TRIGGER AS '
BEGIN
	INSERT INTO trig_tst2(opr,opk) VALUES (3,OLD.pk);
	RETURN NEW;
END;
' LANGUAGE 'plpgsql' VOLATILE;

INSERT INTO trig_tst1 (vli,vlt) VALUES (1,'tst');
INSERT INTO trig_tst1 (vli,vlt) VALUES (2,'abc');
INSERT INTO trig_tst1 (vli,vlt) VALUES (3,'xyz');
INSERT INTO trig_tst1 (vli,vlt) VALUES (0,'bob');
INSERT INTO trig_tst1 (vli,vlt) SELECT vli+4,vlt FROM trig_tst1;
INSERT INTO trig_tst1 (vli,vlt) SELECT vli+8,vlt FROM trig_tst1;
INSERT INTO trig_tst1 (vli,vlt) SELECT vli+16,vlt FROM trig_tst1;
INSERT INTO trig_tst1 (vli,vlt) SELECT vli+32,vlt FROM trig_tst1;
INSERT INTO trig_tst1 (vli,vlt) SELECT vli+64,vlt FROM trig_tst1;
INSERT INTO trig_tst1 (vli,vlt) SELECT vli+128,vlt FROM trig_tst1;
INSERT INTO trig_tst1 (vli,vlt) SELECT vli+256,vlt FROM trig_tst1;
INSERT INTO trig_tst1 (vli,vlt) SELECT vli+512,vlt FROM trig_tst1;
INSERT INTO trig_tst1 (vli,vlt) SELECT vli+1024,vlt FROM trig_tst1;
INSERT INTO trig_tst1 (vli,vlt) SELECT vli+2048,vlt FROM trig_tst1;
INSERT INTO trig_tst1 (vli,vlt) SELECT vli+4096,vlt FROM trig_tst1;
INSERT INTO trig_tst1 (vli,vlt) SELECT vli+8192,vlt FROM trig_tst1;
INSERT INTO trig_tst1 (vli,vlt) SELECT vli+16384,vlt FROM trig_tst1;
INSERT INTO trig_tst1 (vli,vlt) SELECT vli+32768,vlt FROM trig_tst1;

CREATE TRIGGER tst_tf_ins AFTER INSERT ON trig_tst1 FOR EACH ROW EXECUTE PROCEDURE tf_ins();
CREATE TRIGGER tst_tf_updt AFTER UPDATE ON trig_tst1 FOR EACH ROW EXECUTE PROCEDURE tf_updt();
CREATE TRIGGER tst_tf_del AFTER DELETE ON trig_tst1 FOR EACH ROW EXECUTE PROCEDURE tf_del();

INSERT INTO trig_tst1 (vli,vlt) SELECT vli+65536,vlt FROM trig_tst1;

SELECT count(*) FROM trig_tst2;

INSERT INTO trig_tst1 (vli,vlt) SELECT vli+131072,vlt FROM trig_tst1;

SELECT count(*) FROM trig_tst2;

TRUNCATE trig_tst2;

UPDATE trig_tst1 SET vli=vli*2;

TRUNCATE trig_tst2;


