2017-03-03 77 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

相關問題