本文共 2479 字,大约阅读时间需要 8 分钟。
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: 2NOTICE: sub xact end if: 2NOTICE: parent xact: 2NOTICE: sub xact: 3NOTICE: sub xact end if: 3NOTICE: parent xact: 3NOTICE: sub xact: 4NOTICE: sub xact end if: 4NOTICE: parent xact: 4NOTICE: sub xact: 5NOTICE: sub xact end if: 5NOTICE: parent xact: 5NOTICE: sub xact: 6NOTICE: sub xact end if: 6NOTICE: parent xact: 6NOTICE: sub xact: 7NOTICE: sub xact end if: 7NOTICE: parent xact: 7NOTICE: sub xact: 8NOTICE: sub xact end if: 8NOTICE: parent xact: 8NOTICE: sub xact: 9NOTICE: sub xact end if: 9NOTICE: parent xact: 9NOTICE: sub xact: 10NOTICE: sub xact end if: 10NOTICE: parent xact: 10NOTICE: sub xact: 11NOTICE: parent xact: 11NOTICE: sub xact: 12NOTICE: parent xact: 12NOTICE: sub xact: 13NOTICE: parent xact: 13NOTICE: sub xact: 14NOTICE: parent xact: 14CREATE 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: 2NOTICE: parent xact: 2NOTICE: sub xact: 3NOTICE: parent xact: 3NOTICE: sub xact: 4NOTICE: parent xact: 4NOTICE: sub xact: 5NOTICE: parent xact: 5NOTICE: sub xact: 6NOTICE: parent xact: 6NOTICE: sub xact: 7NOTICE: parent xact: 7NOTICE: sub xact: 8NOTICE: parent xact: 8NOTICE: sub xact: 9NOTICE: parent xact: 9NOTICE: sub xact: 10NOTICE: parent xact: 10NOTICE: sub xact: 11 test1 ------- 0(1 row)
在PostgreSQL中不存在这个问题。 使用GP时需要注意一下。
转载地址:http://pucgo.baihongyu.com/