Re: getpid() function - Mailing list pgsql-hackers

From Tom Lane
Subject Re: getpid() function
Date
Msg-id 15488.1028319906@sss.pgh.pa.us
Whole thread Raw
In response to Re: getpid() function  (Bruce Momjian <pgman@candle.pha.pa.us>)
Responses Re: getpid() function  (Hannu Krosing <hannu@tm.ee>)
Re: getpid() function  (Bruce Momjian <pgman@candle.pha.pa.us>)
List pgsql-hackers
Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > Perhaps a more relevant question is why are we cluttering the namespace
> > with any such function at all?  What's the use case for it?

> It was requested because it is exposed in libpq and people need it to
> generate unique names and stuff like that from within psql and
> functions.  Seems like a valid use for the pid.

The sole reason libpq exposes it is so that you can tell a self-notify
from an incoming notify.  (ie, given you are LISTENing on a condition
that both you and other clients send NOTIFYs for, is this particular
message one that you sent yourself, or not?  Compare the originator PID
in the NOTIFY message to your backend_pid to find out.)  I put that
feature in back around 6.4, because it allowed some important
optimizations in an app I had that used LISTEN/NOTIFY a lot.

Since NOTIFY messages aren't even visible at the SQL level, the above is
not a reason for making PIDs visible at the SQL level.

I'm really dubious about using backend PID for the sort of purpose you
suggest.  Unique names would be *much* more safely handled with, say,
a sequence generator.  If you are not using libpq or another client
library that can give you a backend-PID API call, then very likely you
don't have a lot of control over the backend connection either, and
shouldn't assume that backend PID is going to be stable for you.
(Think about pooled connections in a webserver, etc.)

Finally, the most legitimate uses of PID (like discovering a backend PID
to send SIGINT to, when some client query is running wild) are not
supported at all by a function that can only return your own backend's
PID, because that's seldom the PID you need to know.  The
pg_stat_activity view handles this much better.

So I'm still unconvinced that we need or want this ...
        regards, tom lane


pgsql-hackers by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: getpid() function
Next
From: "J. R. Nield"
Date:
Subject: Re: PITR, checkpoint, and local relations