【问题标题】:Insert statement in plpython function greenplum database在plpython函数greenplum数据库中插入语句
【发布时间】:2018-06-29 06:29:01
【问题描述】:
我正在 greenplum 中创建一个 plpython 函数。
plpy.prepare("INSERT INTO ....") 失败并出现错误:
ERROR: plpy.SPIError: function cannot execute on segment because it issues a non-SELECT statement (plpython.c:4656) (seg4 slice1 den-gp5-seg03:40000 pid=119213) (cdbdisp.c:1322)
plpython函数中不允许插入吗?
我没有看到文档中的插入有太多帮助。
greenplum plpython doc
【问题讨论】:
标签:
greenplum
plpython
postgresql-8.3
【解决方案1】:
您可以使用plpy.execute("INSERT INTO ....") 而不是plpy.prepare("INSERT INTO ....")。
plpy.prepare() 用于prepared statement。如果你想使用它,请参考下面的例子。
postgres=# create table your_table (yourstr char(2), yournum int);
CREATE TABLE
postgres=# do $$
postgres$# plan = plpy.prepare("insert into your_table values($1,$2)", ["text", "int"]);
postgres$# plpy.execute(plan, ["Gx", 7]);
postgres$# $$ language plpython3u;
DO
postgres=# select * from your_table;
yourstr | yournum
---------+---------
Gx | 7
(1 row)
postgres=#
Here are available pypl functions(Greenplum 5.10 doc here)
【解决方案2】:
我不确定插入是否可以在这里工作;
将插入语句放入 gp 函数并从 plpython func 中触发“select my_func(arg1, arg2)”