Re: Sharing database handles across forked child processes

From: "Greg Sabino Mullane" <greg(at)turnstep(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Sharing database handles across forked child processes
Date: 2007-11-13 18:38:30
Message-ID: 537ea49b39c7ab32c546fea4b7426df4@biglumber.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)turnstep(dot)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-----

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2007-11-13 18:41:23 Re: Sharing database handles across forked child processes
Previous Message Jeff Davis 2007-11-13 18:27:32 Re: Postgres table size