Skip site navigation (1) Skip section navigation (2)

Detecting libpq connections improperly shared via fork()

From: Daniel Farina <daniel(at)heroku(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Detecting libpq connections improperly shared via fork()
Date: 2012-10-03 22:08:18
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-hackers

On Wed, Oct 3, 2012 at 9:41 AM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> To bring that closer to home, suppose you have a program with an open
> database connection in libpq, and you fork(), and then parent and child
> both try to use the connection.  How well would that work?  Is it the
> fault of fork()?

This brings to mind a lot of issues we see reported among our users:

I see the problem of database connections shared among processes as a
reported problem *all the time*, because of the popularity of
fork-based web servers.  If you do something just a tiny bit wrong you
end up establishing the connection before the fork and then two things
can happen:

* If SSL is off, you never notice until you get some really bizarre
issues that result from an unfortunate collision of protocol traffic.
Since many applications have idle-time, this happens rarely enough to
be subtle that some people never take notice.  A tell-tell sign is an
error reported from something that looks like one query jammed into

* When SSL is enabled this strangeness is seen more or less
immediately, but shows up as cryptic OpenSSL complaints, to which no
earthly person is going to know what to do.

It would be fantastic for libpq to somehow monitor use of a connection
from multiple PIDs that share a parent and deliver an error indicating
what is wrong.  Unfortunately detecting that would require either a
file or some kind of shared memory map, AFAIK, and I don't know how
keen anyone is on accepting that patch.  So, may I ask: how keen is
anyone on accepting such a patch, and under what conditions of

As a minor functional downside, it would hurt a hypothetical user that
is carefully sharing a backend file descriptor between processes using
libpq and synchronizing them very carefully (notably, with encryption
this becomes almost comically difficult and brittle).  I conjecture
such a person is almost entirely hypothetical in nature.



pgsql-hackers by date

Next:From: Phil SorberDate: 2012-10-03 22:09:00
Subject: Re: PQping command line tool
Previous:From: Andres FreundDate: 2012-10-03 21:53:52
Subject: Re: Support for REINDEX CONCURRENTLY

Privacy Policy | About PostgreSQL
Copyright © 1996-2018 The PostgreSQL Global Development Group