2

Is there any reason this function call would not return 'result'?

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

result = mymodule.object(input,plpy)
plpy.info(" ========= EXTRA-module result: ===",result)

$$ LANGUAGE plpythonu;

=== Content of mymodule ============

def object(input,plpy):
  import StringIO
  try:
   plan = plpy.prepare("INSERT INTO file VALUES (nextval('primary_sequence'),$1) RETURNING primary_key", ["integer"] )
  except:
   plpy.error(traceback.format_exc())

  try:
   rv = plpy.execute(plan, [ input ])
   result = rv[0]["primary_key"]
   plpy.info(" ========= INTRA-module result: ===",result)
   return result
  except:
   plpy.error(traceback.format_exc())
4
  • What output do you get? (E.g. from the .info calls...) Commented Sep 21, 2011 at 16:55
  • what do you mean by noresult no INTRA-module result or no EXTRA-module result ? Commented Sep 21, 2011 at 16:56
  • @ed.,@Xavier: BOTH of the info calls show the correct result! It's just the 'return' itself which doesn't seem to work as expected. I want the 'wrapping' function to return result - pls note its OUT parameter. Commented Sep 21, 2011 at 18:45
  • Does it work if you use the RETURNS syntax instead of OUT? (I.e. ...myfunction (input int) RETURNS int) AS $$). You'll need a return statement after the "EXTRA-module" .info() call... Commented Sep 21, 2011 at 18:59

2 Answers 2

1

I'm not to familiar with plpython, but if it is throwing an error and that isn't getting printed out or passed further up the chain you would never know.

I don't know if you are testing with a command line or not but try putting a print statement in your except blocks to see if it is just erroring out instead of returning.

Sign up to request clarification or add additional context in comments.

1 Comment

That's a problem in plpy - cannot print as such. But do have the plpy.info call to effect the same purpose. It's the poor man's debugger!
1

@ed. Didn't actually need the RETURNS syntax instead of OUT, but your suggestion put me onto the answer. And yes, I feel like a real dummy. This is the beauty of having others review one's work.

with the return result added, things work out nicely. Key assumption I'd made here was that the result = syntax did not actually finish the return within the scope of the calling function. Doh!

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

result = mymodule.object(input,plpy)
plpy.info(" ========= EXTRA-module result: ===",result)
# This was the key bit:
return result

$$ LANGUAGE plpythonu;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.