查找postgresql数据库

时间:2017-03-03 17:17:12

标签: postgresql

所以,我现在知道我找不到postgresql tabes(PostgreSQL: Table creation time)的表创建时间。)我希望仍能获得在某个日期之后创建的表的代理。我的数据库中的大多数表都有created_at日期。有没有办法检查所有表格中的min(created_at)日期是> x,没有逐桌查询?

1 个答案:

答案 0 :(得分:0)

免责声明:实际上它不是整个问题的答案,而只是

  

所以,我现在知道我找不到postgresql tabes的表创建时间

是的,但您可以使用event triggers自行收集此类数据。

有例子:

/*
drop table if exists t1;
drop table if exists public.t2;
drop event trigger if exists tg_table_created_at;
drop function if exists fn_table_created_at(); 
drop table if exists table_created_at;
*/    

create table table_created_at(
  toid oid,
  tname text,
  created_at timestamptz default clock_timestamp());

create function fn_table_created_at()
  returns event_trigger
  language plpgsql
  as $$
begin
  insert into table_created_at(toid, tname)
    select objid, object_identity
    from pg_event_trigger_ddl_commands()
    where command_tag = 'CREATE TABLE';
end $$;

create event trigger tg_table_created_at on ddl_command_end
  execute procedure fn_table_created_at();

create temp table t1();
select pg_sleep(1);
create table public.t2();

select *, toid::regclass from table_created_at;

结果:

╔═══════╤════════════╤═══════════════════════════════╤══════════════╗
║ toid  │   tname    │          created_at           │     toid     ║
╠═══════╪════════════╪═══════════════════════════════╪══════════════╣
║ 87803 │ pg_temp.t1 │ 2017-03-03 20:05:44.339811+02 │ pg_temp_3.t1 ║
║ 87806 │ public.t2  │ 2017-03-03 20:05:45.344503+02 │ public.t2    ║
╚═══════╧════════════╧═══════════════════════════════╧══════════════╝

以及其他几个链接:Event Trigger FunctionsEvent Trigger Firing MatrixObject Identifier Types