in reply to Wierd pipe behaviour in plperlu

Is your database handle being shared between the parent and child? I wouldn't be surprised if that's the problem.

Generally, after you fork things like database connections cannot be shared. In fact, it is also unsafe to call any library routine like gethostbyname() which maintains a network connection.

If you are sharing the database handle, try having the child establish its own db connection, and see if that fixes it.

Replies are listed 'Best First'.
Re^2: Wierd pipe behaviour in plperlu
by Anonymous Monk on Jun 04, 2008 at 21:27 UTC
    The child doesn't actually access the database. It runs a few backtick commands and processes the results. All of the database access is done from the parent. I have another function that works in a similar way but the parent pushes data to the child, and it works OK.
      Can you post some code that exhibits the problem? Thanks.
        The original code is too big to post, but I'll see if I can cobble together a test case.
        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.