2017-03-03 72 views
1

因此,我现在知道我无法找到postgresql表的表创建时间(PostgreSQL: Table creation time)。我仍然希望能够获得表的代理这是在某个日期之后创建的。我的数据库中的大多数表都有一个created_at日期。有没有办法检查所有表中min(created_at)日期> x的表,而不用按表查询表?为postgresql数据库中的所有表查找min(created_at)

+0

这篇文章似乎有你要求的信息。 http://stackoverflow.com/questions/5350088/how-to-search-a-specific-value-in-all-tables-postgresql –

回答

0

免责声明:其实这是没有答案的全部问题,但只

所以,我现在知道,我无法找到创建表时对PostgreSQL脊髓痨

是的,但您可以使用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

相关问题