PostgreSQL如何检查我的功能是否仍在运行

时间:2014-03-31 12:11:54

标签: postgresql postgresql-9.3

只是一个简单的问题。

我在PostgreSQL 9.3中有一个重函数,如何检查函数是否在几个小时后仍在运行以及如何在psql中运行后台函数(我的连接时常不稳定)

谢谢

3 个答案:

答案 0 :(得分:3)

对于长时间运行的功能,不时让它们RAISE LOGRAISE NOTICE非常有用,表明进度。如果他们循环遍历数百万条记录,您可能会每隔几千条记录发出一条日志消息。

有些人也(ab)使用SEQUENCE,他们在函数中获得序列的nextval,然后直接读取序列值以检查进度。这是粗糙但有效的。我尽可能喜欢伐木。

要处理断开连接,请通过ssh在远程端运行psql,而不是直接通过PostgreSQL协议连接到服务器。正如克里斯蒂安建议的那样,使用screen以便远程psql在ssh会话死亡时不会被杀死。

或者,您可以使用传统的unix命令nohup,该命令随处可用:

nohup psql -f the_script.sql </dev/null &

将在后台运行psql,将所有输出和错误写入名为nohup.out的文件。

您可能还会发现,如果启用TCP Keepalive,则无论如何都不会丢失远程连接。

答案 1 :(得分:1)

pg_stat_activity是一个很好的提示,可以检查您的功能是否仍在运行。还可以在服务器上使用screentmux,以确保它能够在重新连接后继续使用。

答案 2 :(得分:-1)

1 - 登录psql控制台。

$ psql -U user -d database

2-发出\x命令格式化结果。

3- SELECT * from pg_stat_activity;

4-滚动,直到列表中显示您的功能名称。它应该具有活动状态。

5-检查表格中是否存在您的功能所依赖的块:

SELECT k_locks.pid AS pid_blocking, k_activity.usename AS user_blocking, k_activity.query AS query_blocking, locks.pid AS pid_blocked, activity.usename AS user_blocked, activity.query AS query_blocked, to_char(age(now(), activity.query_start), 'HH24h:MIm:SSs') AS blocking_age FROM pg_catalog.pg_locks locks JOIN pg_catalog.pg_stat_activity activity ON locks.pid = activity.pid JOIN pg_catalog.pg_locks k_locks ON locks.locktype = k_locks.locktype and locks.database is not distinct from k_locks.database and locks.relation is not distinct   from   k_locks.relation          and locks.page is not distinct   from   k_locks.page          and locks.tuple is not distinct   from   k_locks.tuple          and locks.virtualxid is not distinct   from   k_locks.virtualxid          and locks.transactionid is not distinct   from   k_locks.transactionid          and locks.classid is not distinct   from   k_locks.classid          and locks.objid is not distinct   from   k_locks.objid          and locks.objsubid is not distinct   from   k_locks.objsubid          and locks.pid <> k_locks.pid    JOIN   pg_catalog.pg_stat_activity k_activity               ON k_locks.pid = k_activity.pid    WHERE   k_locks.granted         and not locks.granted    ORDER BY   activity.query_start;