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
This worked for what I wanted:
Q1
Your "small test" can be plain SQL:
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
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:
-> SQLfiddle