triggerspartitiongreenplum

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


i got errors when i created the partition tables automatically via trigger which is created in advance.

enviorment info:

GP version :4.3.8.1

error:

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

function code:

    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 code:

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

it is ok when the funtion and trigger are executed , but when the values are inserted into the table T_FM_HISTORYALARM , the parent table ,the error occures.

if i execute the create 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)

it works well.

can you give me some advice? thank you very much.


Solution

  • This is the expected behavior; please see point 8) in the Greenplum Docs.

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