2014-03-31 42 views
0

只是一個簡單的問題。的PostgreSQL如何檢查是否我的功能仍在運行

我有一個重功能的PostgreSQL 9.3,我怎麼能檢查功能,幾個小時後仍在運行以及如何在後臺在psql裏運行的函數(我的連接不穩定,不時)

由於

+0

退房'pg_stat_activity':http://www.postgresql.org/docs/current/static/監視爲stats.html#PG-STAT-活動-VIEW –

回答

1

pg_stat_activity是一個很好的提示檢查你的功能仍在運行。也可以在服務器上使用screentmux以確保它能夠在重新連接後存活。

3

對於長時間運行的功能,可能會不時用RAISE LOGRAISE NOTICE表示進度。如果他們循環了數百萬條記錄,則可能每隔幾千條記錄發出一條日誌消息。

某些人也(ab)使用SEQUENCE,他們在那裏得到其功能序列的nextval,然後直接讀取序列值以檢查進度。這是粗糙而有效的。我希望儘可能記錄日誌。

爲了處理斷開,上通過ssh而不是連接到服務器直接通過的PostgreSQL協議的遠程端運行psql。正如基督教徒所建議的那樣,使用screen,這樣當ssh會話死亡時,遠程psql不會被殺死。

或者,您也可以使用傳統的UNIX命令nohup,這也是隨處可見:

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

將在後臺運行psql,將所有的輸出和錯誤到一個名爲nohup.out文件。

您也可能會發現,如果啓用TCP持久,不會丟失遠程連接反正。

+0

慣於'psql'被'SIGHUP'當遠程連接死殺? – ckruse

+0

不是,如果背景是背景,則不在bash中,因爲後臺作業會在不同的進程組中進行。在其他shell中,你可能需要'nohup'。 –

+0

呃...或不。我的錯。 –

-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; 
相關問題