Why can PL/pgSQL functions have side effect, while SQL functions can't?

后端 未结 2 567
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-20 07:32

PostgreSQL document says:

The entire body of a SQL function is parsed before any of it is executed. While a SQL function can contain

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-20 08:03

    Plpgsql functions are parsed and syntax-checked at definition time, then at first execution a plan is generated.

    https://www.postgresql.org/docs/current/static/plpgsql-implementation.html#PLPGSQL-PLAN-CACHING

    then that plan is executed with the given parameters.

    Temporary files seem to work as expected, except those that already exist on the first execution.

    As mentioned in there use of dynamic SQL (EXECUTE) is a way to foil the planner allowing access to arbitrary tables.

    https://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

提交回复
热议问题