KingbaseES 通过触发器实现查看表的创建时间

从oracle迁移至KingbaseES的用户,经常会问在KingbaseES中怎么查询表的创建时间。
由于KingbaseES本身并不直接存储表的创建时间,所以获取这一信息通常需要依赖于间接方法或日志记录。
如果确实有这种需求,可以通过创建事件触发器的将相关的信息保存下来。

建立保存信息的数据表

CREATE TABLE  table_ddl_info(
        id serial PRIMARY KEY,
        object_type varchar(100),     
        schema_name VARCHAR(50),     
        action_name name NOT NULL,     
        object_name varchar(100),     
        statime timestamp with time zone );

创建事件触发器将

CREATE OR REPLACE FUNCTION get_ddl_time_func() RETURNS event_trigger
LANGUAGE plpgsql AS $$ 
DECLARE     
obj record; 
BEGIN
     FOR obj IN SELECT * FROM  sys_event_trigger_ddl_commands() LOOP
        INSERT INTO table_ddl_info (object_type, schema_name,action_name,object_name,statime) 
        SELECT obj.object_type, obj.schema_name, obj.command_tag,obj.object_identity,now();   
     END LOOP; 
END; 
$$;

CREATE EVENT TRIGGER get_tab_ddl_trigger ON ddl_command_end 
EXECUTE PROCEDURE get_ddl_time_func();   

CREATE OR REPLACE FUNCTION get_tab_for_drops() RETURNS event_trigger 
LANGUAGE plpgsql AS $$ 
DECLARE     
obj record; 
BEGIN     
    FOR obj IN SELECT * FROM sys_event_trigger_dropped_objects() LOOP
        INSERT INTO table_ddl_info (object_type, schema_name,action_name,object_name,statime)
        SELECT obj.object_type, obj.schema_name,tg_tag,obj.object_identity,now();    
END LOOP; 
END; 
$$; 

CREATE EVENT TRIGGER get_tab_trigger_for_drops ON sql_drop
EXECUTE PROCEDURE get_tab_for_drops();

测试

test=# create table create_table_test (id int);
CREATE TABLE
test=# alter table create_table_test add (name varchar(10));
ALTER TABLE
test=# grant select on create_table_test to u1;
GRANT

test=# select * from table_ddl_info;
 id | object_type | schema_name | action_name  |       object_name        |            statime
----+-------------+-------------+--------------+--------------------------+-------------------------------
  2 | table       | public      | CREATE TABLE | public.create_table_test | 2023-12-20 15:35:07.757425+08
  3 | table       | public      | ALTER TABLE  | public.create_table_test | 2023-12-20 15:35:27.994804+08
  4 | TABLE       |             | GRANT        |                          | 2023-12-20 15:36:51.347843+08
(3 行记录)

test=# drop table create_table_test;
DROP TABLE
test=# select * from table_ddl_info;
 id | object_type | schema_name | action_name  |        object_name         |            statime
----+-------------+-------------+--------------+----------------------------+-------------------------------
  2 | table       | public      | CREATE TABLE | public.create_table_test   | 2023-12-20 15:35:07.757425+08
  3 | table       | public      | ALTER TABLE  | public.create_table_test   | 2023-12-20 15:35:27.994804+08
  4 | TABLE       |             | GRANT        |                            | 2023-12-20 15:36:51.347843+08
  5 | table       | public      | DROP TABLE   | public.create_table_test   | 2023-12-20 15:48:37.146019+08
  6 | type        | public      | DROP TABLE   | public.create_table_test   | 2023-12-20 15:48:37.146019+08
  7 | type        | public      | DROP TABLE   | public.create_table_test[] | 2023-12-20 15:48:37.146019+08
(6 行记录)
posted @ 2024-03-28 15:32  KINGBASE研究院  阅读(73)  评论(0编辑  收藏  举报