SQLCREATE TABLE test( id_test integer DEFAULT NEXTVAL('sq_test'::regclass), test character varying(50))WITH ( OIDS=FALSE); CREATE OR REPLACE RULE testinsert AS ON INSERT TO test DO INSERT INTO test_history (id_test, test) VALUES (currval('sq_test'::regclass), 'INSERT'::character varying);
SQLCREATE TABLE test ( id bigserial, value integer ); CREATE TABLE test_history ( time timestamp DEFAULT CURRENT_TIMESTAMP, test_id bigint, action text); CREATE FUNCTION test_trigger() returns TRIGGER AS ' begin if TG_OP=''DELETE'' then insert into test_history values(default, OLD.id, TG_OP); return OLD; else insert into test_history values(default, NEW.id,TG_OP); return NEW; end if; end;' LANGUAGE plpgsql; CREATE TRIGGER test_trigger before INSERT OR UPDATE OR DELETE ON testFOR each row execute procedure test_trigger();