我对 PostgreSQL tidscan的理解

PostgreSQL中,有一种 tidscan。当我在使用cursor的时候,会用到此种scan:

[postgres@lex pgsql]$ cat ./data/test.sqlCREATE OR REPLACE Function FindCourse
   ( name_in IN varchar )
   RETURNS integer LANGUAGE plpgsql AS $$
DECLARE
    cnumber integer;
    c1 CURSOR
    FOR
       SELECT course_number, instructor
        from course_tbl
        where course_name = name_in
        FOR UPDATE;

BEGIN

BEGIN
open c1;
fetch c1 into cnumber;

IF not found THEN
     cnumber := 9999;
ELSE
     UPDATE course_tbl
        SET instructor = 'SMITH'
        WHERE CURRENT OF c1;
    COMMIT;
END IF;

close c1;

EXCEPTION
WHEN OTHERS THEN
END;
RETURN cnumber;
END;$$;
[postgres@lex pgsql]$ 

 下面再进行一下修正:

在PostgreSQL的 function 里面,是不允许进行commit的。所以其实,上面的程序需要改成:

[postgres@lex pgsql]$ cat ./data/test.sqlCREATE OR REPLACE Function FindCourse
   ( name_in IN varchar )
   RETURNS integer LANGUAGE plpgsql AS $$
DECLARE
    cnumber integer;
    c1 CURSOR
    FOR
       SELECT course_number, instructor
        from course_tbl
        where course_name = name_in
        FOR UPDATE;

BEGIN

BEGIN
open c1;
fetch c1 into cnumber;

IF not found THEN
     cnumber := 9999;
ELSE
     UPDATE course_tbl
        SET instructor = 'SMITH'
        WHERE CURRENT OF c1;
END IF;

close c1;

EXCEPTION
WHEN OTHERS THEN
END;
RETURN cnumber;
END;$$;
[postgres@lex pgsql]$ 

 

posted @ 2013-06-10 12:24  健哥的数据花园  阅读(537)  评论(0编辑  收藏  举报