Thread: proctitle patch (useful)

proctitle patch (useful)

From
Jim Mercer
Date:
when trying to diagnose problems, it is sometimes difficult to identify which
backend process is connected to what client process.

here is a patch for src/backend/commands/variable.c

this patch adds a new variable PROCTITLE.

this allows client processes to set the backend proctitle to something useful:

$ ps auxww | grep post
11080 ??  Is     0:00.06 postmaster -S -i -d 3 -o -F (postgres)
11125 ??  I      0:00.42 (postgres)

$ psql database
database=> SET PROCTITLE = 'testing proctitle 123';
SET VARIABLE
database=>

$ ps ax | grep post
11080 ??  Is     0:00.06 postmaster -S -i -d 3 -o -F (postgres)
11125 ??  I      0:00.21 postmaster: testing proctitle 123 (postgres)


i'm not overly familiar with GNU configure, so i'll leave it up to the people
who manage the source to figure out a clean portable way of enabling the
feature.

also note: not all systems have setproctitle(), although there is usually
a way to do it.

--
[ Jim Mercer                 jim@reptiles.org              +1 416 506-0654 ]
[          Reptilian Research -- Longer Life through Colder Blood          ]
[  Don't be fooled by cheap Finnish imitations; BSD is the One True Code.  ]

*** variable.c.orig    Fri Dec 24 11:23:42 1999
--- variable.c    Fri Dec 24 11:23:10 1999
***************
*** 21,26 ****
--- 21,32 ----
  #include "mb/pg_wchar.h"
  #endif

+ #define PROCTITLE
+ #ifdef PROCTITLE
+ static bool show_title(void);
+ static bool reset_title(void);
+ static bool parse_title(const char *);
+ #endif
  static bool show_date(void);
  static bool reset_date(void);
  static bool parse_date(const char *);
***************
*** 304,309 ****
--- 310,370 ----
      return TRUE;
  }

+ #ifdef PROCTITLE
+ /*
+  *
+  * PROCTITLE
+  *
+  */
+ static char *ProcTitle = NULL;
+ static bool
+ parse_title(const char *value)
+ {
+     if (value == NULL)
+     {
+         reset_title();
+         return TRUE;
+     }
+
+     if (ProcTitle != NULL)
+         free(ProcTitle);
+
+     ProcTitle = strdup(value);
+     setproctitle(ProcTitle);
+
+     return TRUE;
+ }
+
+ static bool
+ show_title()
+ {
+     char        buf[64];
+
+     strcpy(buf, "DateStyle is ");
+     if (ProcTitle != NULL)
+         strcat(buf, ProcTitle);
+     else
+         strcat(buf, "NULL");
+
+     elog(NOTICE, buf, NULL);
+
+     return TRUE;
+ }
+
+ static bool
+ reset_title()
+ {
+     if (ProcTitle != NULL)
+         {
+         free(ProcTitle);
+         ProcTitle = NULL;
+         }
+
+     setproctitle(ProcTitle);
+     return TRUE;
+ }
+ #endif
+
  /*
   *
   * DATE_STYLE
***************
*** 539,544 ****
--- 600,610 ----
  }            VariableParsers[] =

  {
+ #ifdef PROCTITLE
+     {
+         "proctitle", parse_title, show_title, reset_title
+     },
+ #endif
      {
          "datestyle", parse_date, show_date, reset_date
      },


Re: [GENERAL] proctitle patch (useful)

From
Bruce Momjian
Date:
The problem is that we already have a complex system of displaying the
user, database, and SQL command being executed.

You should be seeing this on your platform already.  What OS and pgsql
version?

In other words, you should not be seeing this:

> 11080 ??  Is     0:00.06 postmaster -S -i -d 3 -o -F (postgres)



--
  Bruce Momjian                        |  http://www.op.net/~candle
  maillist@candle.pha.pa.us            |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Re: [GENERAL] proctitle patch (useful)

From
Jim Mercer
Date:
On Fri, Dec 24, 1999 at 12:09:31PM -0500, Bruce Momjian wrote:
> The problem is that we already have a complex system of displaying the
> user, database, and SQL command being executed.

the current proctitle doesn't allow me to find the PID of the client process,
without guessing.

it becomes even worse when the client process is on a different machine.

using ProcTitle, i can have the client process set the proctitle to something
meaningful like "%s:%s:%d:%s", hostname, prgname, pid, status.

in my case, i'm actually opening multiple sessions on the same database,
so i'd like to know which backends are for which sessions.

> You should be seeing this on your platform already.  What OS and pgsql
> version?
>
> In other words, you should not be seeing this:
>
> > 11080 ??  Is     0:00.06 postmaster -S -i -d 3 -o -F (postgres)

that example was from NetBSD 1.4.1 and pgsql 6.5.3

on freebsd it looks like:
bigbird% ps auxww | grep post
pgsql   16429  0.0  0.5  4004 1496  ??  Ss   Mon11PM   0:41.06 /usr/local/pgsql/bin/postmaster -i -S -o -F
-D/usr/local/pgsql/data(postgres) 
pgsql   10317  0.0  0.8  4468 2672  ??  S    12:43PM   0:00.03 /usr/local/pgsql/bin/postgres dbadmin localhost nagoss
idle

(for some reason, i can't get my NetBSD box to do the same thing)

now that i think about it, the hack needs a little refining, to basically tell
the other code not to overwrite the ProcTitle.

--
[ Jim Mercer                 jim@reptiles.org              +1 416 506-0654 ]
[          Reptilian Research -- Longer Life through Colder Blood          ]
[  Don't be fooled by cheap Finnish imitations; BSD is the One True Code.  ]

Re: [GENERAL] proctitle patch (useful)

From
Bruce Momjian
Date:
> On Fri, Dec 24, 1999 at 12:09:31PM -0500, Bruce Momjian wrote:
> > The problem is that we already have a complex system of displaying the
> > user, database, and SQL command being executed.
>
> the current proctitle doesn't allow me to find the PID of the client process,
> without guessing.
>
> it becomes even worse when the client process is on a different machine.
>
> using ProcTitle, i can have the client process set the proctitle to something
> meaningful like "%s:%s:%d:%s", hostname, prgname, pid, status.
>
> in my case, i'm actually opening multiple sessions on the same database,
> so i'd like to know which backends are for which sessions.

A larger question is whether we want SQL clients to be able to control
this display.  I certainly makes spoofing possible, where I look like
one person, but am actually someone else.

>
> > You should be seeing this on your platform already.  What OS and pgsql
> > version?
> >
> > In other words, you should not be seeing this:
> >
> > > 11080 ??  Is     0:00.06 postmaster -S -i -d 3 -o -F (postgres)
>
> that example was from NetBSD 1.4.1 and pgsql 6.5.3
>
> on freebsd it looks like:
> bigbird% ps auxww | grep post
> pgsql   16429  0.0  0.5  4004 1496  ??  Ss   Mon11PM   0:41.06 /usr/local/pgsql/bin/postmaster -i -S -o -F
-D/usr/local/pgsql/data(postgres) 
> pgsql   10317  0.0  0.8  4468 2672  ??  S    12:43PM   0:00.03 /usr/local/pgsql/bin/postgres dbadmin localhost nagoss
idle
>
> (for some reason, i can't get my NetBSD box to do the same thing)
>
> now that i think about it, the hack needs a little refining, to basically tell
> the other code not to overwrite the ProcTitle.

I don't understand why NetBSD doesn't do it, while FreeBSD and BSDI do.

--
  Bruce Momjian                        |  http://www.op.net/~candle
  maillist@candle.pha.pa.us            |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Re: [GENERAL] proctitle patch (useful)

From
Bruce Momjian
Date:
>
> when trying to diagnose problems, it is sometimes difficult to identify which
> backend process is connected to what client process.
>
> here is a patch for src/backend/commands/variable.c
>
> this patch adds a new variable PROCTITLE.
>
> this allows client processes to set the backend proctitle to something useful:
>
> $ ps auxww | grep post
> 11080 ??  Is     0:00.06 postmaster -S -i -d 3 -o -F (postgres)
> 11125 ??  I      0:00.42 (postgres)
>
> $ psql database
> database=> SET PROCTITLE = 'testing proctitle 123';
> SET VARIABLE
> database=>
>
> $ ps ax | grep post
> 11080 ??  Is     0:00.06 postmaster -S -i -d 3 -o -F (postgres)
> 11125 ??  I      0:00.21 postmaster: testing proctitle 123 (postgres)
>
>
> i'm not overly familiar with GNU configure, so i'll leave it up to the people
> who manage the source to figure out a clean portable way of enabling the
> feature.

If we decide this is a nice feature addition, I would like it to use our
main PS_SET_STATUS() code rather than proctitle().

--
  Bruce Momjian                        |  http://www.op.net/~candle
  maillist@candle.pha.pa.us            |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Re: [GENERAL] proctitle patch (useful)

From
Jim Mercer
Date:
On Fri, Dec 24, 1999 at 04:08:40PM -0500, Bruce Momjian wrote:
> > i'm not overly familiar with GNU configure, so i'll leave it up to the
> > people who manage the source to figure out a clean portable way of
> > enabling the feature.
>
> If we decide this is a nice feature addition, I would like it to use our
> main PS_SET_STATUS() code rather than proctitle().

my implementation used the most expeditious method at hand.

the PS_SET_STATUS() code is likely more portable across platforms.
(although one might look at using setproctitle inside PS_SET_STATUS(), if
it is available).

--
[ Jim Mercer                 jim@reptiles.org              +1 416 506-0654 ]
[          Reptilian Research -- Longer Life through Colder Blood          ]
[  Don't be fooled by cheap Finnish imitations; BSD is the One True Code.  ]