Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a postgreSQL database function written in plperlu that's behaving rather strangely. The function works thus: a sub process is forked off using  open($handle, '-|'). .The child does some concurrent processing and writes the results to STDOUT (i.e. the pipe). The parent reads from $handle. All appears well until I run the function a number of times in the same database session. On the first invocation all is well. The second time it is run, when I read from the child process I get the data that was read the first time again, followed by the output of the second invocation. If I run it a third time when I read I get the 1st, 2nd , 3rd concatonated together, and so on.
E.g. 

SELECT myFunction();
11111

SELECT myFunction();
1111122222

SELECT myFunction();
111112222233333

and so on... 

I am using autoflush in the child, and I am closing $handle before exiting. In the child I am also closing $handle. All of the data that is sent by the child appears to have been read by the parent. I'm totally bewildered by this one. I know it's a bit off topic but any suggestions would be very welcome.

Replies are listed 'Best First'.
Re: Wierd pipe behaviour in plperlu
by pc88mxer (Vicar) on Jun 04, 2008 at 20:42 UTC
    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.

      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.