Insert statement in plpython function greenplum database

1.4k Views Asked by At

I am creating a plpython function in greenplum. for plpy.prepare("INSERT INTO ....") fails with error:
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)
Are inserts not allowed in plpython functions? I don't see much help with inserts in documentation. greenplum plpython doc

2

There are 2 best solutions below

0
Gurupreet Singh Bhatia On

I'm not sure if insert will work here; put insert statement in a gp function and fire a "select my_func(arg1, arg2)" from plpython func

0
C.C. Hsu On

You can use plpy.execute("INSERT INTO ....") rather than plpy.prepare("INSERT INTO ....").

plpy.prepare() is used for prepared statement. If you want to use it, please refer to the following example.

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)