pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: dave madden <dhm@paradigm.webvision.com>
To: grim@argh.demon.co.uk
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] Low level socket locking...
Date: Mon, 3 Aug 1998 11:01:51 -0700
Message-ID: <199808031801.LAA22883@paradigm.webvision.com> (raw)
In-Reply-To: <199808031735.SAA20880@argh.demon.co.uk>

 =>From: M Simms <grim@argh.demon.co.uk>
 =>...
 =>I am writing a C program to work as an interface between postgres and a
 =>web page. This page will get a LOT of activity.
 =>...
 =>I was planning on simply fork()ing the handler process. The database would be
 =>open on each child, and I could happily process them all easilly. However
 =>it occurred to me that I do not know how the socket code between the
 =>postgres libraries and the postgres backend is handled? Is there a socket
 =>locking process in place, to prevent two children passing data down the same
 =>socket at the same time, thus causing garbled data to be received at the other
 =>end? If I fork() and send data from one of say, three child processes, am
 =>I guarenteed to get the data back at the same child?
 =>(this all assumes that I open the database connection and then fork() after)

I've written a similar program for Oracle and Sybase, and (while I
can't speak directly to the question of whether you can do what you
want in PostgreSQL), I'd strongly suggest opening the DB connection
*after* you fork().  You won't have to worry about multiple children
confusing the back end, and it'll work for other databases too, if you
need that capability later.  The sequence you want to do is:

* Prepare to become a daemon (lose controlling TTY, chdir("/"), close
  all open descriptors, etc)
* Open the socket you're going to receive requests on.
* Fork() as many children as you need
> In each child, connect to the database, then do
  accept/read/write/close on the socket to respond to the client's
  requests
> In the parent, watch for children dying, and spawn new ones to
  replace them.

You can make this even nicer by having the parent & children collude
to do load balancing -- if all the children have clients, the parent
should start some more.  If many children are idle for a while, they
should die off.  You can even have the parent serve requests on a
"control channel" to change the number of children or to retrieve
statistics about the daemon's activity.

d.



view thread (3+ messages)  latest in thread

Message-ID: <199808031801.LAA22883@paradigm.webvision.com>
Permalink:  ../199808031801.LAA22883@paradigm.webvision.com/
Also on:    postgresql.org/message-id/199808031801.LAA22883@paradigm.webvision.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: dhm@paradigm.webvision.com, grim@argh.demon.co.uk
  Subject: Re: [SQL] Low level socket locking...
  In-Reply-To: <199808031801.LAA22883@paradigm.webvision.com>
  References: <199808031735.SAA20880@argh.demon.co.uk> <199808031801.LAA22883@paradigm.webvision.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox