Today,I defined a function,using the insert statements in a loop. But HAWQ returned an error:
今天,我使用循环中的insert语句定义了一个函数。但是HAWQ犯了一个错误:
ERROR: could not serialize unrecognized node type: 43983632 (outfast.c:4742)
CONTEXT: SQL statement "insert into t(id,value) values(1,0.1)"
PL/pgSQL function "test_function" line 6 at SQL statement
I did some testing and found that when I use the 'insert statements' in the loop,it will be reported as a mistake. If I delete the relevant 'insert statements',It can run properly.
我做了一些测试,发现当我在循环中使用“insert语句”时,它会被报告为一个错误。如果我删除相关的“插入语句”,它可以正常运行。
Here is an example of a test:
这里有一个测试的例子:
CREATE OR REPLACE FUNCTION test_function()
RETURNS int AS
$BODY$
declare
number int;
begin
number := 1;
while number <= 10 loop
insert into t(id,value) values(1,0.1);
number := number+1;
end loop;
return number;
end
$BODY$
LANGUAGE plpgsql ;
Then I use 'select test_function();' to call the function.It will returned an error mentioned above.
然后使用'select test_function();'调用函数。它将返回上面提到的错误。
Does this mean that I can not use the SQL statements in a loop with plpgsql ?
这是否意味着我不能使用plpgsql循环中的SQL语句?
Thanks. Best regards.
谢谢。致以最亲切的问候。
2 个解决方案
#1
1
You'll want to avoid singleton statements with HAWQ but I am a little surprised it doesn't work. You'll want to use set based operations instead.
您将希望避免使用HAWQ中的单例语句,但我有点惊讶它不起作用。您将希望使用基于集合的操作。
CREATE TABLE t (id int, value numeric);
CREATE OR REPLACE FUNCTION test_function() RETURNS int AS
$BODY$
declare
number int;
begin
insert into t (id, value) select 1, 0.1 from generate_series(1, 10);
return 10;
end
$BODY$
LANGUAGE plpgsql;
For such a simple example, you could use a sql function instead which has less overhead than plpgsql.
对于这样一个简单的示例,您可以使用一个sql函数,它的开销比plpgsql小。
DROP FUNCTION test_function();
CREATE OR REPLACE FUNCTION test_function() RETURNS void AS
$BODY$
insert into t (id, value) select 1, 0.1 from generate_series(1, 10);
$BODY$
LANGUAGE sql;
Both of these functions do all of the work in a single statement rather than executing 10 separate ones. I tested both in HAWQ and both work.
这两个函数在一个语句中完成所有的工作,而不是执行10个单独的语句。我在HAWQ和这两项工作中都进行了测试。
And here is a workaround if you must do singleton insert statements in a loop with HAWQ.
如果您必须使用HAWQ在循环中执行单例插入语句,这里有一个解决方案。
CREATE OR REPLACE FUNCTION test_function()
RETURNS int AS
$BODY$
declare
number int;
v_sql text;
begin
number := 1;
while number <= 10 loop
v_sql := 'insert into t(id,value) values(1,0.1)';
execute v_sql;
number := number+1;
end loop;
return number;
end
$BODY$
LANGUAGE plpgsql ;
#2
1
This is actually an issue that has been addressed in hawq 2.0. You may refer to latest https://github.com/apache/incubator-hawq for reference.
这实际上是hawq 2.0中已经解决的问题。您可以参考最新的https://github.com/apache/incubator-hawq。
Here are the result:
这里是结果:
CREATE TABLE t (id INT, value DOUBLE PRECISION);
CREATE TABLE
CREATE OR REPLACE FUNCTION test_function()
RETURNS int AS
$BODY$
declare
number int;
begin
number := 1;
while number <= 10 loop
insert into t(id, value) values(1, 0.1);
number := number+1;
end loop;
return number;
end
$BODY$
LANGUAGE plpgsql;
CREATE FUNCTION
SELECT test_function();
test_function
---------------
11
(1 row)
SELECT * FROM t;
id | value
----+-------
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
(10 rows)
SELECT * FROM test_function();
test_function
---------------
11
(1 row)
SELECT * FROM t;
id | value
----+-------
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
(10 rows)
#1
1
You'll want to avoid singleton statements with HAWQ but I am a little surprised it doesn't work. You'll want to use set based operations instead.
您将希望避免使用HAWQ中的单例语句,但我有点惊讶它不起作用。您将希望使用基于集合的操作。
CREATE TABLE t (id int, value numeric);
CREATE OR REPLACE FUNCTION test_function() RETURNS int AS
$BODY$
declare
number int;
begin
insert into t (id, value) select 1, 0.1 from generate_series(1, 10);
return 10;
end
$BODY$
LANGUAGE plpgsql;
For such a simple example, you could use a sql function instead which has less overhead than plpgsql.
对于这样一个简单的示例,您可以使用一个sql函数,它的开销比plpgsql小。
DROP FUNCTION test_function();
CREATE OR REPLACE FUNCTION test_function() RETURNS void AS
$BODY$
insert into t (id, value) select 1, 0.1 from generate_series(1, 10);
$BODY$
LANGUAGE sql;
Both of these functions do all of the work in a single statement rather than executing 10 separate ones. I tested both in HAWQ and both work.
这两个函数在一个语句中完成所有的工作,而不是执行10个单独的语句。我在HAWQ和这两项工作中都进行了测试。
And here is a workaround if you must do singleton insert statements in a loop with HAWQ.
如果您必须使用HAWQ在循环中执行单例插入语句,这里有一个解决方案。
CREATE OR REPLACE FUNCTION test_function()
RETURNS int AS
$BODY$
declare
number int;
v_sql text;
begin
number := 1;
while number <= 10 loop
v_sql := 'insert into t(id,value) values(1,0.1)';
execute v_sql;
number := number+1;
end loop;
return number;
end
$BODY$
LANGUAGE plpgsql ;
#2
1
This is actually an issue that has been addressed in hawq 2.0. You may refer to latest https://github.com/apache/incubator-hawq for reference.
这实际上是hawq 2.0中已经解决的问题。您可以参考最新的https://github.com/apache/incubator-hawq。
Here are the result:
这里是结果:
CREATE TABLE t (id INT, value DOUBLE PRECISION);
CREATE TABLE
CREATE OR REPLACE FUNCTION test_function()
RETURNS int AS
$BODY$
declare
number int;
begin
number := 1;
while number <= 10 loop
insert into t(id, value) values(1, 0.1);
number := number+1;
end loop;
return number;
end
$BODY$
LANGUAGE plpgsql;
CREATE FUNCTION
SELECT test_function();
test_function
---------------
11
(1 row)
SELECT * FROM t;
id | value
----+-------
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
(10 rows)
SELECT * FROM test_function();
test_function
---------------
11
(1 row)
SELECT * FROM t;
id | value
----+-------
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
1 | 0.1
(10 rows)