CREATEOR REPLACE FUNCTION update_modified_column() RETURNSTRIGGERAS $$ BEGIN NEW.t_update = now(); RETURNNEW; END; $$ language'plpgsql';
三. 创建一个函数,用来给继承了basic_update的表新增一个触发器,怼上第二步的函数
createfunction table_create() returns event_trigger language plpgsql as $$ DECLARE oid INT; table_name VARCHAR; parent_tables VARCHAR []; is_update BOOL;
BEGIN
SELECT object_identity INTO table_name FROM pg_event_trigger_ddl_commands();
SELECT objid INTO oid FROM pg_event_trigger_ddl_commands();
SELECT get_parent_tables_by_oid(oid) INTO parent_tables;
SELECT parent_tables :: TEXT [] @>'{basic_update}' :: TEXT [] INTO is_update;
IF is_update
THEN EXECUTE'CREATE TRIGGER tgr_auto_t_update' ||' BEFORE UPDATE ON ' || table_name ||' FOR EACH ROW EXECUTE PROCEDURE update_modified_column()'; END IF;
END;
四. 创建一个建表时的触发器,接管创建表的时机,怼上第三步的函数
CREATE EVENT TRIGGER etgr_table_create ON ddl_command_end WHEN TAG IN ('CREATE TABLE') EXECUTEPROCEDURE table_create();
五. 所有操作已经完成,可以创建一个表测试了
createtable test ( id varchardefault uuid_generate_v4() notnull constraint test_pkey primary key, text varchar, t_create timestampdefault now() ) inherits (basic_update);