PostgreSQL vs Oracle: “compile-time” checking of PL/pgSQL

只谈情不闲聊 提交于 2019-12-03 13:28:56
vyegorov

Yes, this is a known issue.

PL/pgSQL (like any other function, except on SQL) is a “black box” for the PostgreSQL, therefore it is not really possible to detect errors except in runtime.

You can do several things:

  1. wrap your function calling SQL queries into BEGIN / COMMIT statements in order to have better control over errors;
  2. add EXCEPTION blocks to your code to catch and track errors. Note, though, that this will affect function performance;
  3. use plpgsql_check extension, developed by the Pavel Stěhule, who is one of the main contributors to PL/pgSQL development. I suppose eventually this extension will make it into the core of the PostgreSQL, but it'll take some time (now we're in 9.4beta3 state);
  4. You might also look into this related question: postgresql syntax check without running the query

And it really looks like you're in a huge need of a unit testing framework.

Plpgsql language is designed with without semantics checking in compile time. I am not sure how this feature was a intention or side effect of old plpgsql implementation, but in time we found some advantages (but with by you mentioned disadvantage).

plus:

  • there are less issues with dependency between functions and other database objects. It is simple solution of cyclic dependency. Deployment of plpgsql functions is simpler, because you don't need to respect dependency.
  • Some patterns with temporary tables are possible due lazy dependency. It is necessary, because Postgres doesn't support global temporary tables.

Example:

BEGIN
  CREATE TEMP TABLE xx(a int);
  INSERT INTO xx VALUES(10); -- isn't possible with compile time dependency
END;

Minus:

  • There are no possible a compile time deep checking (identifiers checking), although it is possible sometimes.

For some bigger projects a mix of solutions should be used:

  • regress and unit tests - it is base, because some situation cannot be checked statically - dynamic SQL for example.
  • plpgsql_check - it is external, but supported project used some bigger companies and bigger plpgsql users. It can enforce a static check of SQL identifiers validity. You can enforce this check by DDL triggers.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!