Re: Sharing database handles across forked child processes - Mailing list pgsql-general

From Greg Sabino Mullane
Subject Re: Sharing database handles across forked child processes
Date
Msg-id 537ea49b39c7ab32c546fea4b7426df4@biglumber.com
Whole thread Raw
In response to Re: Sharing database handles across forked child processes  (dan@sidhe.org)
Responses Re: Sharing database handles across forked child processes  (dan@sidhe.org)
List pgsql-general
-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160


> Yep, this is a fork without exec. And the child processes often aren't
> even doing any database access -- the database connection's opened and
> held, then a child is forked off, and the child 'helpfully' closes the
> handle during the child's global destruction phase.
>
> Am I at any risk in the parent process?

Yes. But there is an easy solution, asuming you are using DBI:

$dbh->{InactiveDestroy} = 1;

This tells DBI not to do anything special when inside of DESTROY. Set
on the kids immediately after forking.

> "the child processes often aren't even doing any database access"
                       ^^^^^^^^^^^^

Often aren't? This should be "never", period, unless the parent contracts
to stop doing database access after the fork. You can't have two processes
sharing a handle.

Note also that InactiveDestroy should not be your first choice. Far
better to do the forking before the database connection whenever possible.
If they both need access, you can also disconnect, fork, and have both
reconnect afterwards.

- --
Greg Sabino Mullane greg@turnstep.com
End Point Corporation
PGP Key: 0x14964AC8 200711131332
http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8
-----BEGIN PGP SIGNATURE-----

iD8DBQFHOe7qvJuQZxSWSsgRA/BUAJ4tfyoZja93h3q6EtJ3lHiGRRODOACg/M2Y
5VlkKiSZNfstdgrD5Ru+Q/c=
=OjGF
-----END PGP SIGNATURE-----



pgsql-general by date:

Previous
From: Jeff Davis
Date:
Subject: Re: Postgres table size
Next
From: Martijn van Oosterhout
Date:
Subject: Re: Sharing database handles across forked child processes