PostgreSQL存储过程突然停止运行

时间:2014-12-12 14:20:21

标签: postgresql stored-procedures

我在PostgreSQL中有一个存储过程在日程安排上运行得很好。但是,经过' x'天数,它停止正常运作。 ' X'是一个不确定的变量,只要我们注意到它就在3到90之间。一旦它被打破'并且错误地填充表格,它会在未来的计划运行中继续以这种方式运行,直到下次手动执行。

我正在运行PostgreSQL 9.3.4。我可以发布SP的代码,但它肯定能正常工作,直到某些东西让它破裂。

任何想法可能是什么以及我如何能够解决它?

代码:

CREATE OR REPLACE FUNCTION public.sp_oppscreated()
RETURNS integer
LANGUAGE plpgsql
AS $function$
BEGIN

DROP TABLE IF EXISTS bec_oppscreated CASCADE;

CREATE TABLE bec_oppscreated WITH (OIDS=TRUE)
AS

SELECT opportunity.id AS oppid,
   SPLIT_PART(user2.username, '@', 1) AS username,
   recordtype.name AS opptype,
   opportunity.stagename AS stage,
   opportunity.createddate AS createddate,
   opportunity.ve__c AS ve,
   opportunity.amount AS oppamount,
   opportunity.lab AS lab,

   CASE
          WHEN (recordtype.name = 'Proposal' AND opportunity.ve__c <> 'V') THEN opportunity.amount
          WHEN (recordtype.name = 'Proposal' AND opportunity.ve__c = 'V' AND opportunity.lab <> 0 AND opportunity.lab IS NOT NULL) THEN opportunity.lab
          ELSE 0
   END AS amount_prp_created,
   CASE
          WHEN (recordtype.name = 'Amendment' AND opportunity.ve__c <> 'V') THEN opportunity.amount
          WHEN (recordtype.name = 'Amendment' AND opportunity.ve__c = 'V' AND opportunity.lab <> 0 AND opportunity.lab IS NOT NULL) THEN opportunity.lab
          ELSE 0
   END AS amount_amd_created,
   CASE
          WHEN (opportunity.ve__c <> 'V') THEN opportunity.amount
          WHEN (opportunity.ve__c = 'V' AND opportunity.lab <> 0 AND opportunity.lab IS NOT NULL) THEN opportunity.lab
          ELSE 0
   END AS amount_cons_created,

   CASE WHEN (opportunity.ve__c = 'A') THEN opportunity.amount ELSE 0 END AS amount_A_created,
   CASE WHEN (opportunity.ve__c = 'C') THEN opportunity.amount ELSE 0 END AS amount_C_created,
   CASE WHEN (opportunity.ve__c = 'D') THEN opportunity.amount ELSE 0 END AS amount_D_created,
   CASE WHEN (opportunity.ve__c = 'I') THEN opportunity.amount ELSE 0 END AS amount_I_created,
   CASE WHEN (opportunity.ve__c = 'L') THEN opportunity.amount ELSE 0 END AS amount_L_created,
   CASE WHEN (opportunity.ve__c = 'M') THEN opportunity.amount ELSE 0 END AS amount_M_created,
   CASE WHEN (opportunity.ve__c = 'P') THEN opportunity.amount ELSE 0 END AS amount_P_created,
   CASE WHEN (opportunity.ve__c = 'V') THEN opportunity.amount ELSE 0 END AS amount_V_created,
   opportunity.amount AS amount_created,

   CASE
          WHEN (recordtype.name = 'Proposal' AND opportunity.ve__c <> 'V') THEN 1
          WHEN (recordtype.name = 'Proposal' AND opportunity.ve__c = 'V' AND opportunity.lab <> 0 AND opportunity.lab IS NOT NULL) THEN 1
          ELSE 0
   END AS count_prp_created,
   CASE
          WHEN (recordtype.name = 'Amendment' AND opportunity.ve__c <> 'V') THEN 1
          WHEN (recordtype.name = 'Amendment' AND opportunity.ve__c = 'V' AND opportunity.lab <> 0 AND opportunity.lab IS NOT NULL) THEN 1
          ELSE 0
   END AS count_amd_created,
   CASE
          WHEN (opportunity.ve__c <> 'V') THEN 1
          WHEN (opportunity.ve__c = 'V' AND opportunity.lab <> 0 AND opportunity.lab IS NOT NULL) THEN 1
          ELSE 0
   END AS count_cons_created,

   CASE WHEN (opportunity.ve__c = 'A') THEN 1 ELSE 0 END AS count_A_created,
   CASE WHEN (opportunity.ve__c = 'C') THEN 1 ELSE 0 END AS count_C_created,
   CASE WHEN (opportunity.ve__c = 'D') THEN 1 ELSE 0 END AS count_D_created,
   CASE WHEN (opportunity.ve__c = 'I') THEN 1 ELSE 0 END AS count_I_created,
   CASE WHEN (opportunity.ve__c = 'L') THEN 1 ELSE 0 END AS count_L_created,
   CASE WHEN (opportunity.ve__c = 'M') THEN 1 ELSE 0 END AS count_M_created,
   CASE WHEN (opportunity.ve__c = 'P') THEN 1 ELSE 0 END AS count_P_created,
   CASE WHEN (opportunity.ve__c = 'V') THEN 1 ELSE 0 END AS count_V_created,
   1 AS count_created
FROM opportunity
LEFT JOIN user2 ON opportunity.ownerid = user2.id
LEFT JOIN recordtype ON opportunity.recordtypeid = recordtype.id
WHERE opportunity.isdeleted = FALSE
AND   opportunity.createddate > '2007-11-02 03:59:59'
AND   opportunity.lastmodifieddate > '2010-01-11 04:59:59'
ORDER BY username ASC;

GRANT SELECT
ON bec_oppscreated
TO public;
RETURN 0;

EXCEPTION WHEN OTHERS THEN
BEGIN
RAISE notice 'The transaction is in an uncommittable state. '
             'Transaction was rolled back';

RAISE notice '% %', SQLERRM, SQLSTATE;
RETURN -1;
END;

END;
$function$

1 个答案:

答案 0 :(得分:0)

这个问题与cron有关。

无论出于何种原因,postgres用户都无法访问/bin/bash/bin/sh,因此从cron执行的所有命令都会导致“找不到命令”,包括/bin/bash --version

*/5 * * * * postgres /bin/bash --version >> /var/log/mylog 2>&1

/ var / log / mylog中的条目: /bin/bash: postgres: command not found

即使使用SHELL = / bin / bash,也会发生这种情况。

每次监控日志时执行以下操作。 - 为postgres用户crontab添加了PATH语句。没解决。 - 重启/etc/init.d/crond。没解决。 - 已将命令移至root用户crontab。没解决。 - 将cronjob移动到cron.hourly。修复!