Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refcursor parameter becomes unknown when "stealing the cursor" #514

Closed
sr-ix opened this issue Feb 24, 2017 · 3 comments
Closed

Refcursor parameter becomes unknown when "stealing the cursor" #514

sr-ix opened this issue Feb 24, 2017 · 3 comments
Labels

Comments

@sr-ix
Copy link

sr-ix commented Feb 24, 2017

I am following the very basic example in the docs for Server-side cursors and encountering the following error:

function reffunc(unknown) does not exist

HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

So far as I can find there is no way to translate from a python type to refcursor, not even in the extras module.

Am I missing something, or are the docs incorrect for this usage?
Thanks

@dvarrazzo
Copy link
Member

dvarrazzo commented Feb 24, 2017

No, the refcursor type doesn't have any specific representation in psycopg.

Maybe you haven't committed the transaction where you created the sample reffunc() function?

In [1]: import psycopg2
In [2]: cnn = psycopg2.connect('')
In [3]: cur = cnn.cursor()

In [4]: cur.execute("""create function woah(refcursor) returns refcursor as $$
begin
    open $1 for select i from generate_series(1,10) s(i);
    return $1;
end
$$ language plpgsql""")

In [5]: cur1 = cnn.cursor()
In [6]: cur1.execute("select woah('hithere')")

In [7]: cur2 = cnn.cursor('hithere')

In [8]: cur2.fetchone()
Out[8]: (1,)

In [9]: cur2.fetchmany(5)
Out[9]: [(2,), (3,), (4,), (5,), (6,)]

In [10]: cur2.fetchall()
Out[10]: [(7,), (8,), (9,), (10,)]

@dvarrazzo
Copy link
Member

Note that the creation of the stored procedure is just a way to handle the name of the cursor as a parameter. A much simpler example is:

In [1]: import psycopg2
In [2]: cnn = psycopg2.connect('')

In [3]: cur = cnn.cursor()
In [4]: cur.execute("declare wat cursor for select i from generate_series(1,10) s(i)")

In [5]: cur2 = cnn.cursor('wat')
In [6]: cur2.fetchone()
Out[6]: (1,)

@sr-ix
Copy link
Author

sr-ix commented Feb 24, 2017

Yeah, that was my issue. Thanks for the rapid suggestions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants