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())
DrLou
  • 649
  • 5
  • 21
  • What output do you get? (E.g. from the .info calls...) – ed. Sep 21 '11 at 16:55
  • what do you mean by noresult no INTRA-module result or no EXTRA-module result ? – Xavier Combelle Sep 21 '11 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. – DrLou Sep 21 '11 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... – ed. Sep 21 '11 at 18:59

2 Answers2

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.

Prodigal Maestro
  • 613
  • 6
  • 11
  • 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! – DrLou Sep 21 '11 at 18:47
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;
DrLou
  • 649
  • 5
  • 21