postgresql - 如何知道 SP 的修改日期?

标签 postgresql plpgsql postgresql-9.4 event-triggers

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

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

最佳答案

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;

关于postgresql - 如何知道 SP 的修改日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47435293/

相关文章:

postgresql - CREATE TABLE AS 不允许在 Postgresql 的非 volatile 函数中使用

sql - PL/pgSQL 中的 %% 是什么意思?

sql - PL/Pgsql 子查询返回多行

postgresql - Postgres : Convert TEXT to string or difference between 'abc' vs. 'a' || 'bc'

postgresql - 使用 INSERT INTO 时的错误消息

postgresql - 在不查询数据库的情况下将经纬度转换为 PostGIS 几何

ruby-on-rails - 使用 Postgres 按年分组

python - 要快速从 Python 数组转换为 PostgreSQL?

SQL:在 OneToOne 关系中删除子项(但理论上是父项)时删除父项

sql - Go sql - 准备好的语句范围