Обсуждение: [HACKERS] signed logging format for pid in log_line_prefix?

Поиск
Список
Период
Сортировка

[HACKERS] signed logging format for pid in log_line_prefix?

От
Greg Stark
Дата:
Both the text and csv logging seem to use %d on for logging the server pid:

appendStringInfo(buf, "%d", MyProcPid);

Am I missing something or wouldn't this mean we print pids with large
values as negative numbers? Isn't that strange? Wouldn't we rather use
%u here?


-- 
greg



Re: [HACKERS] signed logging format for pid in log_line_prefix?

От
Tom Lane
Дата:
Greg Stark <stark@mit.edu> writes:
> Both the text and csv logging seem to use %d on for logging the server pid:
> appendStringInfo(buf, "%d", MyProcPid);

> Am I missing something or wouldn't this mean we print pids with large
> values as negative numbers? Isn't that strange? Wouldn't we rather use
> %u here?

pid_t is a signed type; see for example waitpid(2):
      The value of pid can be:
      < -1   meaning  wait  for  any  child process whose process group ID is             equal to the absolute value
ofpid.
 
      -1     meaning wait for any child process.
      0      meaning wait for any child process whose  process  group  ID  is             equal to that of the calling
process.
      > 0    meaning  wait  for  the  child  whose process ID is equal to the             value of pid.

So I think using %d is fine.  Someday we might wish that we were using
a wider type than int to hold PIDs, but I don't think it'll ever be
unsigned.
        regards, tom lane



Re: [HACKERS] signed logging format for pid in log_line_prefix?

От
Alvaro Herrera
Дата:
Greg Stark wrote:
> Both the text and csv logging seem to use %d on for logging the server pid:
> 
> appendStringInfo(buf, "%d", MyProcPid);
> 
> Am I missing something or wouldn't this mean we print pids with large
> values as negative numbers? Isn't that strange? Wouldn't we rather use
> %u here?

MyProcPid is an int, so %d is the natural choice.  the sys_types.h
manpage says:
       *  blksize_t, pid_t, and ssize_t shall be signed integer types.

and also:      The implementation shall support one or more  programming  environments      in  which  the  widths of
blksize_t,pid_t, size_t, ssize_t, and susec‐      onds_t are no greater than the width of type long.
 

I wonder if we could just adopt pid_t for PIDs.

-- 
Álvaro Herrera                https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services



Re: [HACKERS] signed logging format for pid in log_line_prefix?

От
Tom Lane
Дата:
Alvaro Herrera <alvherre@alvh.no-ip.org> writes:
> I wonder if we could just adopt pid_t for PIDs.

We could (if somebody is willing to find and change all the relevant
declarations).  But that doesn't do anything at all to clarify which
printf format code to use for them.

Note that the POSIX snippet you quote doesn't actually guarantee that
pid_t is not wider than "long".  So while we could convert all these
places toprintf("...%ld...", (long) pid_t_variable_here);
that's still not formally correct.

Since we have yet to see a platform where our current habit of casting
pids to int doesn't work just as well, I'm inclined not to bother
changing anything here.
        regards, tom lane