前言

最近有客户反馈兼容的dba_triggers视图中,同一个触发器的trigger_event被拆成了多行,和ORACLE中表现不一致,于是我进行了一些分析,发现是在其引用的information_schema.triggers视图中就已经拆开成了INSERT/DELETE/UPDATE,但是这些属性都是通过tgtype这一个int2整型的字段获取的,甚至连before/after/instead of/row/statement 等都是通过这一个字段。一个值存多种信息,这在ORACLE的数据字典视图里很常见,无非就是按二进制位来判断,于是我尝试自己猜一猜,看能不能从这个数字中识别出规律。

测试和分析

先建一堆测试触发器

create schema test1;
create schema test2;

CREATE TABLE test1.test_trigger_src_tbl(id1 INT, id2 INT, id3 INT);

CREATE OR REPLACE FUNCTION test1.tri_test_func() RETURNS TRIGGER AS
           $$
           DECLARE
           BEGIN
                   INSERT INTO test_trigger_des_tbl VALUES(NEW.id1, NEW.id2, NEW.id3);
                   RETURN NEW;
           END
           $$ LANGUAGE PLPGSQL;

--before insert/update row
CREATE TRIGGER test_trigger
           BEFORE insert or update ON test1.test_trigger_src_tbl
           FOR EACH ROW
           EXECUTE PROCEDURE test1.tri_test_func();
           
CREATE TABLE test2.test_trigger_src_tbl(id1 INT, id2 INT, id3 INT);

CREATE OR REPLACE FUNCTION test2.tri_test_func() RETURNS TRIGGER AS
           $$
           DECLARE
           BEGIN
                   INSERT INTO test_trigger_des_tbl VALUES(NEW.id1, NEW.id2, NEW.id3);
                   RETURN NEW;
           END
           $$ LANGUAGE PLPGSQL;
           
--不同schema下的同名触发器
CREATE TRIGGER test_trigger
           BEFORE insert or update ON test2.test_trigger_src_tbl
           FOR EACH ROW
           EXECUTE PROCEDURE test2.tri_test_func();
           
CREATE OR REPLACE FUNCTION test1.tri_test_func1() RETURNS TRIGGER AS
           $$
           DECLARE
           BEGIN
                   INSERT INTO test_trigger_des_tbl VALUES(NEW.id1, NEW.id2, NEW.id3);
                   RETURN NEW;
           END
           $$ LANGUAGE PLPGSQL;

-- before insert row
CREATE TRIGGER test_trigger1
           BEFORE insert  ON test1.test_trigger_src_tbl
           FOR EACH ROW
           EXECUTE PROCEDURE test1.tri_test_func1();

CREATE OR REPLACE FUNCTION test1.tri_test_func2() RETURNS TRIGGER AS
           $$
           DECLARE
           BEGIN
                   INSERT INTO test_trigger_des_tbl VALUES(NEW.id1, NEW.id2, NEW.id3);
                   RETURN NEW;
           END
           $$ LANGUAGE PLPGSQL;

--before insert/delete row
CREATE TRIGGER test_trigger2
           BEFORE insert or DELETE ON test1.test_trigger_src_tbl
           FOR EACH ROW
           EXECUTE PROCEDURE test1.tri_test_func2();
          
CREATE OR REPLACE FUNCTION test1.tri_test_func3() RETURNS TRIGGER AS
           $$
           DECLARE
           BEGIN
                   INSERT INTO test_trigger_des_tbl VALUES(NEW.id1, NEW.id2, NEW.id3);
                   RETURN NEW;
           END
           $$ LANGUAGE PLPGSQL;

--after insert/delete/update row
CREATE TRIGGER test_trigger3
           AFTER insert or delete or UPDATE ON test1.test_trigger_src_tbl
           FOR EACH ROW
           EXECUTE PROCEDURE test1.tri_test_func3();
          
CREATE OR REPLACE FUNCTION test1.tri_test_func4() RETURNS TRIGGER AS
           $$
           DECLARE
           BEGIN
                   INSERT INTO test_trigger_des_tbl VALUES(NEW.id1, NEW.id2, NEW.id3);
                   RETURN NEW;
           END
           $$ LANGUAGE PLPGSQL;
           
create view test1.test_trigger_src_tbl_V as select * from test1.test_trigger_src_tbl;

--instead delete row
CREATE TRIGGER test_trigger4
           instead OF DELETE ON test1.test_trigger_src_tbl_V
           FOR EACH ROW
           EXECUTE PROCEDURE test1.tri_test_func4();

CREATE OR REPLACE FUNCTION test1.tri_test_func5() RETURNS TRIGGER AS
           $$
           DECLARE
           BEGIN
                   INSERT INTO test_trigger_des_tbl VALUES(NEW.id1, NEW.id2, NEW.id3);
                   RETURN NEW;
           END
           $$ LANGUAGE PLPGSQL;

--before truncate statement
CREATE TRIGGER test_trigger5
           before truncate ON test1.test_trigger_src_tbl
           FOR EACH STATEMENT 
           EXECUTE PROCEDURE test1.tri_test_func5();
           
          
CREATE OR REPLACE FUNCTION test1.tri_test_func6() RETURNS TRIGGER AS
           $$
           DECLARE
           BEGIN
                   INSERT INTO test_trigger_des_tbl VALUES(NEW.id1, NEW.id2, NEW.id3);
                   RETURN NEW;
           END
           $$ LANGUAGE PLPGSQL;

--after delete row
CREATE TRIGGER test_trigger6
           AFTER delete  ON test1.test_trigger_src_tbl
           FOR EACH ROW
           EXECUTE PROCEDURE test1.tri_test_func6();
           
CREATE OR REPLACE FUNCTION test1.tri_test_func7() RETURNS TRIGGER AS
           $$
           DECLARE
           BEGIN
                   INSERT INTO test_trigger_des_tbl VALUES(NEW.id1, NEW.id2, NEW.id3);
                   RETURN NEW;
           END
           $$ LANGUAGE PLPGSQL;

--after truncate statement
CREATE TRIGGER test_trigger7
           AFTER truncate ON test1.test_trigger_src_tbl
           FOR EACH STATEMENT 
           EXECUTE PROCEDURE test1.tri_test_func7();

 CREATE OR REPLACE FUNCTION test1.tri_test_func8() RETURNS TRIGGER AS
           $$
           DECLARE
           BEGIN
                   INSERT INTO test_trigger_des_tbl VALUES(NEW.id1, NEW.id2, NEW.id3);
                   RETURN NEW;
           END
           $$ LANGUAGE PLPGSQL;

--after update/delete row
CREATE TRIGGER test_trigger8
           AFTER update or delete  ON test1.test_trigger_src_tbl
           FOR EACH ROW
           EXECUTE PROCEDURE test1.tri_test_func8();

然后查询pg_trigger表,并将tgtype转换成二进制数值显示(注意这里int2无法直接转换成bit类型)

select tgname,tgtype,tgtype::int4::bit(8) from pg_trigger;

得到这个信息,小学二年级都能看出规律。我们在表格中转置一下看看

然后很容易就能对比得到每个二进制位所表示的含义

以上都是纯用SQL查询比较猜出来的,虽然过程也比较简单,但还是有点费时间。但实际上,如果能看懂C语言源码,一眼就能知道应该怎么去解析tgtype
openGauss-server\src\include\catalog\pg_trigger.h

/* Bits within tgtype */
#define TRIGGER_TYPE_ROW                (1 << 0)
#define TRIGGER_TYPE_BEFORE             (1 << 1)
#define TRIGGER_TYPE_INSERT             (1 << 2)
#define TRIGGER_TYPE_DELETE             (1 << 3)
#define TRIGGER_TYPE_UPDATE             (1 << 4)
#define TRIGGER_TYPE_TRUNCATE           (1 << 5)
#define TRIGGER_TYPE_INSTEAD            (1 << 6)

另外,为什么明明有INSERT/DELETE/UPDATE/TRUNCATE四种,但information_schema.triggers里为什么没有truncate的呢?其实在视图里,明确有写

         -- hard-wired refs to TRIGGER_TYPE_INSERT, TRIGGER_TYPE_DELETE,
         -- TRIGGER_TYPE_UPDATE; we intentionally omit TRIGGER_TYPE_TRUNCATE
         (VALUES (4, 'INSERT'),
                 (8, 'DELETE'),
                 (16, 'UPDATE')) AS em (num, text)

至于原作者为什么故意省略,这个就猜不到了。
知道以上规则后,我们可以尝试自己写一个dba_trigger视图,这里对event提供两种写法

case when tgtype&32<>0 then 'TRUNCATE'
     when tgtype&6<>0 and tgtype&16<>0 and tgtype&8<>0 then 'INSERT OR UPDATE OR DELETE'
     when tgtype&6<>0 and tgtype&16<>0 and tgtype&8=0 then 'INSERT OR UPDATE'
     when tgtype&6<>0 and tgtype&16=0 and tgtype&8=0 then 'INSERT'
     when tgtype&6=0 and tgtype&16<>0 and tgtype&8<>0 then 'UPDATE OR DELETE'
     when tgtype&6=0 and tgtype&16<>0 and tgtype&8=0 then 'UPDATE'
     when tgtype&6=0 and tgtype&16=0 and tgtype&8<>0 then 'DELETE'
     when tgtype&6<>0 and tgtype&16=0 and tgtype&8<>0 then 'INSERT OR DELETE'
end
case substring(tgtype::int4::bit(8) from 3 for 5)
      when B'01110' then 'INSERT OR UPDATE OR DELETE'
      when B'01011' then 'INSERT OR UPDATE'
      when B'00011' then 'INSERT'
      when B'01100' then 'UPDATE OR DELETE'
      when B'00100' then 'DELETE'
      when B'00111' then 'INSERT OR DELETE'
      when B'10001' then 'TRUNCATE'
end

视图代码地址:

https://gitee.com/enmotech/cmpat-tools/blob/master/Oracle_Views.sql

结尾

开头有提到,在ORACLE中也经常这样处理属性值,比如以下就是一个oracle的user_triggers视图的一段节选,用这一个property字段表示了很多种属性

decode(bitand(t.property, 8192),
       8192, decode(bitand(t.property, 131072),
                    131072, 'REVERSE', 'FORWARD'), 'NO'),
decode(bitand(t.property, 16384),
             16384, 'YES', 'NO'),
decode(bitand(t.property, 32768),
             32768, 'YES', 'NO'),

好的设计都是相通的,虽然这种方式不利于直接用SQL从数据字典基表中查询明确的属性值,但是能节省很多存储空间,并且在内存中直接判断二进制会更快比字符串更快。

12-09 03:09