为什么auto_explain只在第一次调用PL / pgSQL函数时记录嵌套语句?

问题描述 投票:1回答:1

在我的Postgres服务器上,我使用带有auto_explainlog_nested_statements模块来记录PL / pgSQL函数中的其他函数调用。

594 session_preload_libraries = 'auto_explain'
595
596 auto_explain.log_min_duration = 0
597 auto_explain.log_nested_statements = true
598 auto_explain.sample_rate = 1.0

我有一个玩具PL / pgSQL函数baz(count int)

Schema              | public
Name                | baz
Result data type    | text
Argument data types | count integer
Type                | normal
Volatility          | volatile
Parallel            | unsafe
Owner               | aerust
Security            | invoker
Access privileges   |
Language            | plpgsql
Source code         |                                      +
                    | DECLARE                              +
                    |     i int := 0;                      +
                    |     result text := '';               +
                    | BEGIN                                +
                    |                                      +
                    |     IF (count < 1) THEN              +
                    |     RETURN result;                   +
                    |     END IF;                          +
                    |                                      +
                    |     LOOP                             +
                    |     EXIT WHEN i = count;             +
                    |     i := i + 1;                      +
                    |     result := result || ' ' || foo();+
                    |     END LOOP;                        +
                    |                                      +
                    |     RETURN result;                   +
                    | END;                                 +
                    |
Description         |

哪个调用SQL函数foo()

Schema              | public
Name                | foo
Result data type    | text
Argument data types |
Type                | normal
Volatility          | immutable
Parallel            | unsafe
Owner               | aerust
Security            | invoker
Access privileges   |
Language            | sql
Source code         |  SELECT 'foo ' || bar()
Description         |

在数据库连接中第一次调用函数baz(1)时,我看到作为计划的一部分记录的每个嵌套语句:

2019-03-19 15:25:05.765 PDT [37616] LOG:  statement: select baz(1);
2019-03-19 15:25:05.768 PDT [37616] LOG:  duration: 0.002 ms  plan:
    Query Text:  SELECT 'bar'
    Result  (cost=0.00..0.01 rows=1 width=32)
2019-03-19 15:25:05.768 PDT [37616] CONTEXT:  SQL function "bar" statement 1
    SQL function "foo" during startup
    SQL statement "SELECT result || ' ' || foo()"
    PL/pgSQL function foo(integer) line 14 at assignment
2019-03-19 15:25:05.768 PDT [37616] LOG:  duration: 0.001 ms  plan:
    Query Text:  SELECT 'foo ' || bar()
    Result  (cost=0.00..0.01 rows=1 width=32)
2019-03-19 15:25:05.768 PDT [37616] CONTEXT:  SQL function "foo" statement 1
    SQL statement "SELECT result || ' ' || foo()"
    PL/pgSQL function foo(integer) line 14 at assignment
2019-03-19 15:25:05.768 PDT [37616] LOG:  duration: 0.952 ms  plan:
    Query Text: select baz(1);
    Result  (cost=0.00..0.26 rows=1 width=32)

但是,当我在同一个连接上再次调用该函数时,我没有在日志中看到嵌套语句:

2019-03-19 15:29:06.608 PDT [37616] LOG:  statement: select baz(1);
2019-03-19 15:29:06.608 PDT [37616] LOG:  duration: 0.046 ms  plan:
    Query Text: select baz(1);
    Result  (cost=0.00..0.26 rows=1 width=32)

为什么是这样?如何在同一数据库连接期间在函数的后续调用中记录嵌套语句?

postgresql logging sql-execution-plan
1个回答
0
投票

您可以这样做以便每次都记录语句:

ALTER FUNCTION foo() STABLE;

但那时你的表现会受到一点影响。

解开这个谜团:

  • 在PL / pgSQL函数内部调用的SQL语句的执行计划在数据库会话的生命周期中被缓存,因此它们仅在首次执行该函数时被解析一次。
  • 在解析SQL语句并将其转换为常量时,将评估IMMUTABLE表达式。

仅在第一次记录的语句是仅在解析baz中的SQL语句时评估的语句。

© www.soinside.com 2019 - 2024. All rights reserved.