55 * to contain some useful information. Mechanism differs wildly across
66 * platforms.
77 *
8- * $PostgreSQL: pgsql/src/backend/utils/misc/ps_status.c,v 1.40 2010/01/02 16:57:58 momjian Exp $
8+ * $PostgreSQL: pgsql/src/backend/utils/misc/ps_status.c,v 1.41 2010/05/27 19:19:38 tgl Exp $
99 *
1010 * Copyright (c) 2000-2010, PostgreSQL Global Development Group
1111 * various details abducted from various places
@@ -51,7 +51,7 @@ boolupdate_process_title = true;
5151 * (some other BSD systems)
5252 * PS_USE_CLOBBER_ARGV
5353 * write over the argv and environment area
54- * (most SysV-like systems)
54+ * (Linux and most SysV-like systems)
5555 * PS_USE_WIN32
5656 * push the string out as the name of a Windows event
5757 * PS_USE_NONE
@@ -84,7 +84,7 @@ boolupdate_process_title = true;
8484
8585
8686#ifndef PS_USE_CLOBBER_ARGV
87- /* all but oneoptions need a buffer to write their ps line in */
87+ /* all but oneoption need a buffer to write their ps line in */
8888#define PS_BUFFER_SIZE 256
8989static char ps_buffer [PS_BUFFER_SIZE ];
9090static const size_t ps_buffer_size = PS_BUFFER_SIZE ;
@@ -94,6 +94,8 @@ static size_t ps_buffer_size;/* space determined at run time */
9494static size_t last_status_len ;/* use to minimize length of clobber */
9595#endif /* PS_USE_CLOBBER_ARGV */
9696
97+ static size_t ps_buffer_cur_len ;/* nominal strlen(ps_buffer) */
98+
9799static size_t ps_buffer_fixed_size ;/* size of the constant prefix */
98100
99101/* save the original argv[] location here */
@@ -226,6 +228,7 @@ init_ps_display(const char *username, const char *dbname,
226228/* no ps display if you didn't call save_ps_display_args() */
227229if (!save_argv )
228230return ;
231+
229232#ifdef PS_USE_CLOBBER_ARGV
230233/* If ps_buffer is a pointer, it might still be null */
231234if (!ps_buffer )
@@ -270,7 +273,7 @@ init_ps_display(const char *username, const char *dbname,
270273username ,dbname ,host_info );
271274#endif
272275
273- ps_buffer_fixed_size = strlen (ps_buffer );
276+ ps_buffer_cur_len = ps_buffer_fixed_size = strlen (ps_buffer );
274277
275278set_ps_display (initial_str , true);
276279#endif /* not PS_USE_NONE */
@@ -285,11 +288,11 @@ init_ps_display(const char *username, const char *dbname,
285288void
286289set_ps_display (const char * activity ,bool force )
287290{
288-
291+ #ifndef PS_USE_NONE
292+ /* update_process_title=off disables updates, unless force = true */
289293if (!force && !update_process_title )
290294return ;
291295
292- #ifndef PS_USE_NONE
293296/* no ps display for stand-alone backend */
294297if (!IsUnderPostmaster )
295298return ;
@@ -303,6 +306,7 @@ set_ps_display(const char *activity, bool force)
303306/* Update ps_buffer to contain both fixed part and activity */
304307strlcpy (ps_buffer + ps_buffer_fixed_size ,activity ,
305308ps_buffer_size - ps_buffer_fixed_size );
309+ ps_buffer_cur_len = strlen (ps_buffer );
306310
307311/* Transmit new setting to kernel, if necessary */
308312
@@ -315,7 +319,7 @@ set_ps_display(const char *activity, bool force)
315319union pstun pst ;
316320
317321pst .pst_command = ps_buffer ;
318- pstat (PSTAT_SETCMD ,pst ,strlen ( ps_buffer ) ,0 ,0 );
322+ pstat (PSTAT_SETCMD ,pst ,ps_buffer_cur_len ,0 ,0 );
319323}
320324#endif /* PS_USE_PSTAT */
321325
@@ -325,16 +329,11 @@ set_ps_display(const char *activity, bool force)
325329#endif /* PS_USE_PS_STRINGS */
326330
327331#ifdef PS_USE_CLOBBER_ARGV
328- {
329- int buflen ;
330-
331- /* pad unused memory */
332- buflen = strlen (ps_buffer );
333- /* clobber remainder of old status string */
334- if (last_status_len > buflen )
335- MemSet (ps_buffer + buflen ,PS_PADDING ,last_status_len - buflen );
336- last_status_len = buflen ;
337- }
332+ /* pad unused memory; need only clobber remainder of old status string */
333+ if (last_status_len > ps_buffer_cur_len )
334+ MemSet (ps_buffer + ps_buffer_cur_len ,PS_PADDING ,
335+ last_status_len - ps_buffer_cur_len );
336+ last_status_len = ps_buffer_cur_len ;
338337#endif /* PS_USE_CLOBBER_ARGV */
339338
340339#ifdef PS_USE_WIN32
@@ -369,24 +368,15 @@ const char *
369368get_ps_display (int * displen )
370369{
371370#ifdef PS_USE_CLOBBER_ARGV
372- size_t offset ;
373-
374371/* If ps_buffer is a pointer, it might still be null */
375372if (!ps_buffer )
376373{
377374* displen = 0 ;
378375return "" ;
379376}
380-
381- /* Remove any trailing spaces to offset the effect of PS_PADDING */
382- offset = ps_buffer_size ;
383- while (offset > ps_buffer_fixed_size && ps_buffer [offset - 1 ]== PS_PADDING )
384- offset -- ;
385-
386- * displen = offset - ps_buffer_fixed_size ;
387- #else
388- * displen = strlen (ps_buffer + ps_buffer_fixed_size );
389377#endif
390378
379+ * displen = (int ) (ps_buffer_cur_len - ps_buffer_fixed_size );
380+
391381return ps_buffer + ps_buffer_fixed_size ;
392382}