Greenplum报错--query plan with multiple segworker groups is not supported

报错信息:

ERROR:  query plan with multiple segworker groups is not supported
HINT:  likely caused by a function that reads or modifies data in a distributed table
CONTEXT:  PL/pgSQL function actv_comp(date,date) line 4 at RETURN QUERY

 

我们可能经常会遇到需要在query调用自定义函数的情况,但是在Greenplum中,如果函数中有query,然后又在query中调用该函数则会报错。

 

例子:

创建函数

iap=# create or replace function f1() returns text as $$
declare
c1 text;
begin
execute 'select info from tt1 limit 1' into c1;
return c1;
end;
$$ language plpgsql;

query中调用:

iap=# select f1() from tt1;
ERROR: query plan with multiple segworker groups is not supported
HINT: likely caused by a function that reads or modifies data in a distributed table
CONTEXT: SQL statement 'select info from tt1 limit 1'
PL/pgSQL function f1() line 5 at EXECUTE statement

这是由于greenplum中MPP的特性,每个节点中只保存部分数据,GP6开始支持复制表,那么我们需要将该表改成复制表,保证每个节点中都有一份完整的数据。

除此之外,我们还需要将该函数修改成immutable类型。

 

—修改表分布为REPLICATED类型

alter table tt1 set Distributed REPLICATED;

 

二修改函数为immutable类型

iap=# create or replace function f1() returns text as $$
declare
c1 text;
begin
execute 'select info from tt1 limit 1' into c1;
return c1;
end;
$$ language plpgsql immutable;
CREATE FUNCTION

 

再次调用:

iap=# select f1() from tt1 limit 1;
f1
----------------------------------
d810ed19ec188ddf3af8a14dbd341c3c
(1 row)

 

总结:
如果你需要在query中调用UDF函数,碰到“ERROR: query plan with multiple segworker groups is not supported”报错,那么解决方案如下:

  1. 修改表为复制表
  2. 修改函数为immutable类型

 

 

 

posted @ 2022-01-11 09:35  zhengsongsong  阅读(1017)  评论(0编辑  收藏  举报