gpt4 book ai didi

sql - EXPLAIN ANALYZE 在 PL/pgSQL 中给出错误 : "query has no destination for result data"

转载 作者:行者123 更新时间:2023-11-29 11:37:55 26 4
gpt4 key购买 nike

我试图了解 PL/pgSQL 函数中的 select 语句的查询计划,但我不断收到错误。我的问题:如何获取查询计划?

以下是重现问题的简单案例。

有问题的表名为 test_table。

CREATE TABLE test_table
(
name character varying,
id integer
);

函数如下:

DROP FUNCTION IF EXISTS test_function_1(INTEGER);
CREATE OR REPLACE FUNCTION test_function_1(inId INTEGER)
RETURNS TABLE(outName varchar)
AS
$$
BEGIN
-- is there a way to get the explain analyze output?
explain analyze select t.name from test_table t where t.id = inId;

-- return query select t.name from test_table t where t.id = inId;
END;
$$ LANGUAGE plpgsql;

当我运行时

select * from test_function_1(10);

我得到错误:

ERROR:  query has no destination for result data
CONTEXT: PL/pgSQL function test_function_1(integer) line 3 at SQL statement

如果我取消注释注释部分并注释掉 explain analyze,该函数工作正常。

最佳答案

或者您可以将这种更简单的形式用于 RETURN QUERY :

CREATE OR REPLACE FUNCTION f_explain_analyze(int)
RETURNS SETOF text AS
$func$
BEGIN
RETURN QUERY
EXPLAIN ANALYZE SELECT * FROM foo WHERE v = $1;
END
$func$ LANGUAGE plpgsql;

调用:

SELECT * FROM f_explain_analyze(1);

适用于 Postgres 9.3。

关于sql - EXPLAIN ANALYZE 在 PL/pgSQL 中给出错误 : "query has no destination for result data",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22101229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com