Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

sql - Testing PostgreSQL functions that consume and return refcursor

I want to test results of a Postgres function (changing the function is not a possibility).

The function receives as arguments a REFCURSOR and several other things and returns the same RECURSOR.

get_function_that_returns_cursor(ret, 4100, 'SOMETHING', 123465)

Now I want to create a small test in Postgres to get the results of this FUNCTION. Something Like the code below (this is my approach but it is not working):

DO $$ DECLARE
 ret REFCURSOR; 
 row_to_read table_it_will_return%ROWTYPE ;
BEGIN
 PERFORM get_function_that_returns_cursor(ret, 4100, 'SOMETHING', 123465);
-- OR SELECT get_function_that_returns_cursor(ret, 4100, 'SOMETHING', 123465) INTO ret
 FOR row_to_read IN SELECT * FROM ret LOOP
   -- (...)
   RAISE NOTICE 'Row read...';
 END LOOP;
 CLOSE ret;
END $$;

Any suggestion on how to get this to work? A generic solution that can be used for testing this type of functions (that get a Cursor and return a Cursor?

And if we don't know the rowtype that is being returned how could we do it?

What is the best way to debug this kind of things in PostgresQL

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Q1

Your "small test" can be plain SQL:

BEGIN;
SELECT get_function_that_returns_cursor('ret', 4100, 'foo', 123); -- note: 'ret'
FETCH ALL IN ret; -- works for any rowtype

COMMIT;  -- or ROLLBACK;

Execute COMMIT / ROLLBACK after you inspected the results. Most clients only display the result of the lat command.

More in the chapter Returning Cursors of the manual.

Q2

And if we don't know the rowtype that is being returned how could we do it?

Since you only want to inspect the results, you could cast the whole record to text. This way you avoid the problem with dynamic return types for the function altogether.

Consider this demo:

CREATE TABLE a (a_id int PRIMARY KEY, a text);
INSERT INTO a VALUES (1, 'foo'), (2, 'bar');

CREATE OR REPLACE FUNCTION reffunc(INOUT ret refcursor) AS  -- INOUT param :)
$func$
BEGIN
    OPEN ret FOR SELECT * FROM a;
END
$func$ LANGUAGE plpgsql;


CREATE OR REPLACE FUNCTION ctest()
  RETURNS SETOF text AS
$func$
DECLARE
    curs1 refcursor;
    rec   record;
BEGIN
  curs1 := reffunc('ret');   -- simple assignment
  
  LOOP
    FETCH curs1 INTO rec;
    EXIT WHEN NOT FOUND;     -- note the placement!
    RETURN NEXT rec::text;
  END LOOP;
END
$func$ LANGUAGE plpgsql;

-> SQLfiddle


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...