I found the problem while creating a test case for you to look at! It's amazing how stripping everything back to the bare bones can help!
Turns out to be nothing to do with pipes, file handles or forking. The problem was down to the scope of the buffer variable. I had declared it as 'our' and the value seemed to persist between invocations of the function. Wasn't expecting that - I thought %_SHARED was used for that purpose! I have now changed it to 'local' and the problem goes away.
Have a look at the following test case:
CREATE OR REPLACE FUNCTION myFunction() RETURNS TEXT
LANGUAGE PLPERLU AS
$BODY$
our $data;
$data .= 'xx';
return $data;
$BODY$
scratch=# select myFunction();
myfunction
------------
xx
(1 row)
scratch=# select myFunction();
myfunction
------------
xxxx
(1 row)
scratch=# select myFunction();
myfunction
------------
xxxxxx
(1 row)
Changing to 'local $data' solves the problem. Thanks for your suggestions and interest.
|