PostgreSQL存储过程在异常之上跳过代码?

huangapple go评论95阅读模式
英文:

Postgresql procedure skip code above Exception?

问题

我有一个过程,可以执行插入、删除、截断表等操作在postgresql数据库中。
然后我想监视过程中的作业,然后我对'hungcht_proc_monitoring'做一些插入/更新,使用异常技术,该过程仍在运行,但跳过了所有上面的代码。我进行了一些搜索,但找不到问题和解决方法。希望你们是PLSQL开发专业人士可以修复我下面的代码,谢谢你们,非常感谢。

代码运行良好,但跳过异常上面的代码。

英文:

I have a procedure, to do thing like insert, delete, truncate table in postgresql database.
Then i want to monitoring the job in procedure, then i do some insert/update to 'hungcht_proc_monitoring', by using technique exception, the proc still run but it skips all code above. I make some search but i can't find the issue and solution.
Hope you who are professional in plsql dev can fix my code below, thank you and apreciate so much.

The code run well, but skipp the code above exception.
enter image description here

  1. CREATE OR REPLACE PROCEDURE "y4a_fin_analyst"."hungcht_update_table_database_monitoring"()
  2. AS $BODY$
  3. DECLARE
  4. _sql_state TEXT;
  5. _message TEXT;
  6. BEGIN
  7. -- Step 1: insert into proc monitoring
  8. insert into "y4a_fin_analyst"."hungcht_proc_monitoring"
  9. (proc_name, run_date, end_date, status, err_message, err_sqlstate)
  10. VALUES
  11. ('hungcht_update_table_database_monitoring',
  12. current_timestamp AT TIME ZONE 'Asia/Bangkok',
  13. null,null,null,null);
  14. commit;
  15. -- Step 2: Insert new table created
  16. INSERT into y4a_fin_analyst.hungcht_table_database_monitoring
  17. select * from y4a_fin_analyst.hungcht_check_table A
  18. where not exists (select 1 from y4a_fin_analyst.hungcht_table_database_monitoring b
  19. where a.table_name = b.table_name
  20. and a.schema_name = b.schema_name
  21. and a.owner_name = b.owner_name);
  22. commit;
  23. -- Step 3: Update ROW_COUNT value and updated time
  24. TRUNCATE y4a_fin_analyst.hungcht_table_database_monitoring_tmp;
  25. COMMIT;
  26. INSERT INTO y4a_fin_analyst.hungcht_table_database_monitoring_tmp
  27. select
  28. a.table_name,
  29. a.schema_name,
  30. a.owner_name,
  31. a.row_count,
  32. b."row_count" as last_row_count,
  33. a."row_count" - b."row_count" as row_change_cnt,
  34. COALESCE(b.table_type,a.table_type) as table_type,
  35. b.create_date,
  36. a.update_date
  37. from y4a_fin_analyst.hungcht_check_table A
  38. join y4a_fin_analyst.hungcht_table_database_monitoring b
  39. on a.table_name = b.table_name
  40. and a.schema_name = b.schema_name
  41. and a.owner_name = b.owner_name
  42. and a."row_count" <> b."row_count";
  43. commit;
  44. delete from y4a_fin_analyst.hungcht_table_database_monitoring A
  45. where exists (select 1 from y4a_fin_analyst.hungcht_table_database_monitoring_tmp b
  46. where a."table_name" = b."table_name"
  47. and a."schema_name" = b."schema_name"
  48. and a."owner_name" = b."owner_name");
  49. commit;
  50. insert into y4a_fin_analyst.hungcht_table_database_monitoring
  51. select * from y4a_fin_analyst.hungcht_table_database_monitoring_tmp;
  52. commit;
  53. EXCEPTION WHEN OTHERS THEN
  54. GET STACKED DIAGNOSTICS
  55. _message := MESSAGE_TEXT,
  56. _sql_state := RETURNED_SQLSTATE;
  57. update y4a_fin_analyst.hungcht_proc_monitoring
  58. set
  59. end_date = current_timestamp AT TIME ZONE 'Asia/Bangkok',
  60. status = 'error',
  61. err_message = _message,
  62. err_sqlstate = _sql_state
  63. where proc_name = 'hungcht_update_table_database_monitoring'
  64. and end_date is null;
  65. commit;
  66. END;
  67. $BODY$
  68. LANGUAGE plpgsql

My expect, the code can work with code above exception, and can handle update error to monitoring table when error occurs:
enter image description here

答案1

得分: 0

我认为我可以找到问题的原因,那就是多次提交,所以只需移除所有的“commit”即可解决,但我不知道为什么提交会导致在过程中出现异常时跳过所有代码的基本原因,如果我们移除异常部分,提交就不会影响过程。

英文:

I think i can find the reason of the problem that is multiple 'commit', so just remove all the 'commit' it would be solved, but i dont know the basic inside why commit would cause all skip the code when procedure have exception, if we remove the exception part, the commit wont affect to procedure.

huangapple
  • 本文由 发表于 2023年5月10日 12:17:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定