如何知道修改SP的日期?

时间:2017-11-22 12:54:36

标签: postgresql plpgsql postgresql-9.4 event-triggers

是否可以在PostgreSQL 9.4中了解修改和/或创建SP的日期?

我需要识别它们以便在下次部署时上传它们.-

1 个答案:

答案 0 :(得分:3)

PostgreSQL没有这个功能。您可以创建自己的表格并从event triggers更新它。

create table updates(proc regprocedure primary key, t timestamp);

create or replace function event_trigger_for_ddl_command_end()
returns event_trigger as $$
declare obj record;
begin
  for obj in select * from pg_event_trigger_ddl_commands()
  loop
    if obj.classid = 'pg_proc'::regclass then
      insert into updates values(obj.objid, current_timestamp)
          on conflict (proc) do update set t = current_timestamp
                             where updates.proc = excluded.proc;
    end if;
  end loop;
end;
$$ language plpgsql;

create event trigger trigger_for_ddl_command_end
  on ddl_command_end
  execute procedure event_trigger_for_ddl_command_end();

create or replace function fx(a int) returns int as $$ select 1 $$ language sql;

postgres=# select * from updates ;
+-------------+----------------------------+
|    proc     |             t              |
+-------------+----------------------------+
| fx(integer) | 2017-11-22 14:21:11.367036 |
+-------------+----------------------------+
(1 row)

-- alternative code without INSERT ON CONFLICT
create or replace function event_trigger_for_ddl_command_end()
returns event_trigger as $$
declare obj record;
begin
  for obj in select * from pg_event_trigger_ddl_commands()
  loop
    if obj.classid = 'pg_proc'::regclass then
      begin
        update updates set t = current_timestamp
           where proc = obj.objid;
        if not found then
          begin
            insert into updates values(obj.objid, current_timestamp);
          exception when unique_violation then
            update updates set t = current_timestamp
               where proc = obj.objid;
          end;
        end if;
    end if;
  end loop;
end;
$$ language plpgsql;