Greenplum plpgsql函数中exit存在无法跳出循环的BUG

Greenplum中如果使用循环,并且内部嵌套了子块,在子块中的exit只能跳出子块,不能跳出子块外面的循环。

CREATE OR REPLACE FUNCTION test1(i integer) RETURNS 
integer AS 

$$

DECLARE count int;
BEGIN
    count := 1;
    LOOP
        count := count + 1;

        begin
            raise notice 'sub xact: %', count;
            EXECUTE 'select 1';
            IF count > 10 THEN
                EXIT;  -- BUG在这里, 只跳出了begin, 没有跳出LOOP
                raise notice 'sub xact if: %', count;
            END IF;
            raise notice 'sub xact end if: %', count;
        exception when others then
        end;

        raise notice 'parent xact: %', count;
    END LOOP;
    return 1;
END

$$
 LANGUAGE plpgsql;

postgres=# select test1(1);
NOTICE:  sub xact: 2
NOTICE:  sub xact end if: 2
NOTICE:  parent xact: 2
NOTICE:  sub xact: 3
NOTICE:  sub xact end if: 3
NOTICE:  parent xact: 3
NOTICE:  sub xact: 4
NOTICE:  sub xact end if: 4
NOTICE:  parent xact: 4
NOTICE:  sub xact: 5
NOTICE:  sub xact end if: 5
NOTICE:  parent xact: 5
NOTICE:  sub xact: 6
NOTICE:  sub xact end if: 6
NOTICE:  parent xact: 6
NOTICE:  sub xact: 7
NOTICE:  sub xact end if: 7
NOTICE:  parent xact: 7
NOTICE:  sub xact: 8
NOTICE:  sub xact end if: 8
NOTICE:  parent xact: 8
NOTICE:  sub xact: 9
NOTICE:  sub xact end if: 9
NOTICE:  parent xact: 9
NOTICE:  sub xact: 10
NOTICE:  sub xact end if: 10
NOTICE:  parent xact: 10
NOTICE:  sub xact: 11
NOTICE:  parent xact: 11
NOTICE:  sub xact: 12
NOTICE:  parent xact: 12
NOTICE:  sub xact: 13
NOTICE:  parent xact: 13
NOTICE:  sub xact: 14
NOTICE:  parent xact: 14

CREATE OR REPLACE FUNCTION test1(i integer) RETURNS 
integer AS 

$$

DECLARE count int;
BEGIN
    count := 1;
    LOOP
        count := count + 1;

        begin
            raise notice 'sub xact: %', count;
            EXECUTE 'select 1';
            IF count > 10 THEN
                return 0;  -- 改成return, 退出整个函数, 如果要跳出loop, 应该在loop内控制。不能放在LOOP内的sub block执行。
            END IF;
        exception when others then
        end;

        raise notice 'parent xact: %', count;
    END LOOP;
    return 1;
END

$$
 LANGUAGE plpgsql;

postgres=# select test1(1);
NOTICE:  sub xact: 2
NOTICE:  parent xact: 2
NOTICE:  sub xact: 3
NOTICE:  parent xact: 3
NOTICE:  sub xact: 4
NOTICE:  parent xact: 4
NOTICE:  sub xact: 5
NOTICE:  parent xact: 5
NOTICE:  sub xact: 6
NOTICE:  parent xact: 6
NOTICE:  sub xact: 7
NOTICE:  parent xact: 7
NOTICE:  sub xact: 8
NOTICE:  parent xact: 8
NOTICE:  sub xact: 9
NOTICE:  parent xact: 9
NOTICE:  sub xact: 10
NOTICE:  parent xact: 10
NOTICE:  sub xact: 11
 test1 
-------
     0
(1 row)

在PostgreSQL中不存在这个问题。 使用GP时需要注意一下。

上一篇:Gradle: The New Android Build System


下一篇:ubuntu中文字符集格式转换