只是一个快速的问题。
我在 PostgreSQL 9.3 中有一个繁重的函数,我如何检查该函数在几个小时后是否仍在运行以及如何在 psql 的后台运行一个函数(我的连接不时不稳定)
谢谢
只是一个快速的问题。
我在 PostgreSQL 9.3 中有一个繁重的函数,我如何检查该函数在几个小时后是否仍在运行以及如何在 psql 的后台运行一个函数(我的连接不时不稳定)
谢谢
对于长期运行的功能,拥有它们RAISE LOG
或RAISE NOTICE
不时地指示进度可能很有用。如果它们循环数百万条记录,您可能会每隔几千条记录发出一条日志消息。
有些人还(ab)使用 a SEQUENCE
,他们nextval
在函数中获取序列的 ,然后直接读取序列值以检查进度。这是粗略但有效的。我更喜欢尽可能记录日志。
要处理断开连接,psql
请通过 ssh 在远程端运行,而不是直接通过 PostgreSQL 协议连接到服务器。正如 Christian 所建议的那样,当 ssh 会话终止时,使用screen
这样遥控器psql
就不会被杀死。
nohup
或者,您可以使用随处可用的传统 unix 命令:
nohup psql -f the_script.sql </dev/null &
它将psql
在后台运行,将所有输出和错误写入名为nohup.out
.
您可能还会发现,如果启用 TCP keepalives,无论如何您都不会丢失远程连接。
pg_stat_activity
是检查您的函数是否仍在运行的一个很好的提示。还可以在服务器上使用screen
ortmux
以确保它能够在重新连接后继续存在。
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;