Re: getpid() function

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
Cc: Thomas Lockhart <lockhart(at)fourpalms(dot)org>, Karel Zak <zakkr(at)zf(dot)jcu(dot)cz>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: getpid() function
Date: 2002-08-02 20:25:06
Message-ID: 15488.1028319906@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message J Smith 2002-08-02 20:39:01 Creating GiST Indices?
Previous Message Bruce Momjian 2002-08-02 20:09:55 Re: getpid() function

Browse pgsql-hackers by date

  From Date Subject
Next Message J. R. Nield 2002-08-02 20:45:10 Re: PITR, checkpoint, and local relations
Previous Message Bruce Momjian 2002-08-02 20:09:55 Re: getpid() function