Search code examples
pythonpostgresqlplpythonsql-returning

postgreSQL pl/python how do I get the RETURNING from a query?


in this (vey condensed) query, how would one return the myfile_key from RETURNING?

CREATE OR REPLACE FUNCTION myfunction (src int, OUT result int) AS $$

plan = plpy.prepare("INSERT INTO myfile VALUES (nextval('seq_myfile'),$1,$2)
RETURNING myfile_key", ["integer","integer"] )

$$ LANGUAGE plpythonu;

(where the first field inserted is myfile_key)

The code doesn't return the value to output, and haven't been able to query it as a standard python result, as in:

result = rv[0]["myfile_key"] 

Solution

  • I assume there is a call to plpy.execute missing in your code. You are in principle right to access the RETURNING columns like rv[0]["myfile_key"]. What's not working is the assignment to OUT parameters in PL/Python. I suggest that you rewrite your function without OUT parameters using a normal return statement.