gp 函数无法在段上执行,因为它发出了非 SELECT 语句

gp function cannot execute on segment because it issues a non-SELECT statement

我通过预先创建的触发器自动创建分区 tables 时出错。

环境信息:

GP 版本:4.3.8.1

错误:

NOTICE:  Found 2 data formatting errors (2 or more input rows). Rejected related input data.
ERROR:  function cannot execute on segment because it issues a non-SELECT statement (functions.c:135)  (seg13 ZHJK-HADOOP-SLAVE6:40001 pid=19959) (cdbdisp.c:1326)
DETAIL:  
SQL statement "CREATE TABLE T_FM_HISTORYALARM_20160101(CONSTRAINT T_FM_HISTORYALARM_20160101_eventtime_check CHECK (EVENTTIME >= timestamp '20160101000000' AND EVENTTIME < (timestamp '20160101000000' + interval '1 day')))INHERITS (T_FM_HISTORYALARM)"
PL/pgSQL function "history_alarm_partition" line 36 at execute statement
********** 错误 **********

ERROR: function cannot execute on segment because it issues a non-SELECT statement (functions.c:135)  (seg13 ZHJK-HADOOP-SLAVE6:40001 pid=19959) (cdbdisp.c:1326)
SQL 状态: XX000
详细:
SQL statement "CREATE TABLE T_FM_HISTORYALARM_20160101(CONSTRAINT T_FM_HISTORYALARM_20160101_eventtime_check CHECK (EVENTTIME >= timestamp '20160101000000' AND EVENTTIME < (timestamp '20160101000000' + interval '1 day')))INHERITS (T_FM_HISTORYALARM)"
PL/pgSQL function "history_alarm_partition" line 36 at execute statement

功能码:

    CREATE OR REPLACE FUNCTION history_alarm_partition()  
  RETURNS trigger AS  
$BODY$  
DECLARE tbl_name CHARACTER VARYING;  
    tbl_name_parent CHARACTER VARYING := 'T_FM_HISTORYALARM';
    tbl_name_salve CHARACTER VARYING;  
    tbl_year INTEGER;  
    tbl_month INTEGER;  
    tbl_day INTEGER;
    create_tbl_sql CHARACTER VARYING;  
    insert_tbl_sql CHARACTER VARYING;  
    index_sql CHARACTER VARYING;  
BEGIN   
    SELECT date_part('year',NEW.EVENTTIME)  INTO tbl_year;  
    SELECT date_part('month',NEW.EVENTTIME) INTO tbl_month;  
    SELECT date_part('day',NEW.EVENTTIME) INTO tbl_day;
    IF(tbl_month<10)THEN  
        tbl_name_salve=tbl_year||'0'||tbl_month;  
    ELSE   
        tbl_name_salve=tbl_year||tbl_month;  
    END IF;
    IF(tbl_day<10)THEN
        tbl_name_salve=tbl_name_salve||'0'||tbl_day;
    ELSE
        tbl_name_salve=tbl_name_salve||tbl_day;
    END IF;  
    tbl_name=tbl_name_parent||'_'||tbl_name_salve;  

    --判断表 时间分段表是否存在  
    IF ( (SELECT count(relname) FROM pg_class WHERE relname=tbl_name)>0) THEN  
        insert_tbl_sql='INSERT INTO '||tbl_name ||' VALUES(NEW.*)';  
        EXECUTE insert_tbl_sql;  
    ELSE   
        create_tbl_sql='CREATE TABLE '||tbl_name  
        ||'('  
        ||  'CONSTRAINT '||tbl_name||'_EVENTTIME_CHECK'||' CHECK (EVENTTIME >= timestamp '''||tbl_name_salve||'000000''' ||' AND EVENTTIME < (timestamp '''||tbl_name_salve||'000000'' + interval ''1 day''))'  
        ||')INHERITS ('||tbl_name_parent||')';  
        EXECUTE create_tbl_sql;  

        index_sql='CREATE INDEX '||tbl_name||'_EVENTTIME_INDEX ON '||tbl_name||'EVENTTIME';  
        EXECUTE index_sql;  

        insert_tbl_sql='INSERT INTO '||tbl_name ||' VALUES(NEW.*)';  
        EXECUTE insert_tbl_sql;  
    END IF;  
    RETURN NULL;  
END;  
$BODY$  
  LANGUAGE plpgsql VOLATILE 

创建触发代码:

create trigger history_alarm_trigger before insert or update on t_fm_historyalarm for each row execute procedure history_alarm_partition();

执行function和trigger时是可以的,但是当值被插入到table T_FM_HISTORYALARM,parent table时,出现错误。

如果我执行创建 table statmenet :

CREATE TABLE T_FM_HISTORYALARM_20160101(CONSTRAINT T_FM_HISTORYALARM_20160101_eventtime_check CHECK (EVENTTIME >= timestamp '20160101000000' AND EVENTTIME < (timestamp '20160101000000' + interval '1 day')))INHERITS (T_FM_HISTORYALARM)

效果很好。

你能给我一些建议吗? 非常感谢。

这是预期的行为;请参阅 Greenplum Docs.

中的第 8) 点

Triggers are not supported since they typically rely on the use of VOLATILE functions.