8000 Change ps_status.c to explicitly track the current logical length of … · danielcode/postgres@dbfa55f · GitHub
[go: up one dir, main page]

Skip to content

Commit dbfa55f

Browse files
committed
Change ps_status.c to explicitly track the current logical length of ps_buffer.
This saves cycles in get_ps_display() on many popular platforms, and more importantly ensures that get_ps_display() will correctly return an empty string if init_ps_display() hasn't been called yet. Per trouble report from Ray Stell, in which log_line_prefix %i produced junk early in backend startup. Back-patch to 8.0. 7.4 doesn't have %i and its version of get_ps_display() makes no pretense of avoiding pad junk anyhow.
1 parent b5285c1 commit dbfa55f

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

src/backend/utils/misc/ps_status.c

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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.26.2.1 2005/11/22 18:23:25 momjian Exp $
8+
* $PostgreSQL: pgsql/src/backend/utils/misc/ps_status.c,v 1.26.2.2 2010/05/27 19:20:06 tgl Exp $
99
*
1010
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
1111
* various details abducted from various places
@@ -88,8 +88,11 @@ static const size_t ps_buffer_size = PS_BUFFER_SIZE;
8888
#else /* PS_USE_CLOBBER_ARGV */
8989
static char *ps_buffer; /* will point to argv area */
9090
static size_t ps_buffer_size; /* space determined at run time */
91+
static size_t last_status_len; /* use to minimize length of clobber */
9192
#endif /* PS_USE_CLOBBER_ARGV */
9293

94+
static size_t ps_buffer_cur_len; /* nominal strlen(ps_buffer) */
95+
9396
static size_t ps_buffer_fixed_size; /* size of the constant prefix */
9497

9598
/* save the original argv[] location here */
@@ -174,7 +177,7 @@ save_ps_display_args(int argc, char **argv)
174177
}
175178

176179
ps_buffer = argv[0];
177-
ps_buffer_size = end_of_area - argv[0];
180+
last_status_len = ps_buffer_size = end_of_area - argv[0];
178181

179182
/*
180183
* move the environment out of the way
@@ -290,7 +293,7 @@ init_ps_display(const char *username, const char *dbname,
290293
username, dbname, host_info);
291294
#endif
292295

293-
ps_buffer_fixed_size = strlen(ps_buffer);
296+
ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer);
294297

295298
#ifdef WIN32
296299
pgwin32_update_ident(ps_buffer);
@@ -321,6 +324,7 @@ set_ps_display(const char *activity)
321324
/* Update ps_buffer to contain both fixed part and activity */
322325
StrNCpy(ps_buffer + ps_buffer_fixed_size, activity,
323326
ps_buffer_size - ps_buffer_fixed_size);
327+
ps_buffer_cur_len = strlen(ps_buffer);
324328

325329
/* Transmit new setting to kernel, if necessary */
326330

@@ -333,7 +337,7 @@ set_ps_display(const char *activity)
333337
union pstun pst;
334338

335339
pst.pst_command = ps_buffer;
336-
pstat(PSTAT_SETCMD, pst, strlen(ps_buffer), 0, 0);
340+
pstat(PSTAT_SETCMD, pst, ps_buffer_cur_len, 0, 0);
337341
}
338342
#endif /* PS_USE_PSTAT */
339343

@@ -343,13 +347,11 @@ set_ps_display(const char *activity)
343347
#endif /* PS_USE_PS_STRINGS */
344348

345349
#ifdef PS_USE_CLOBBER_ARGV
346-
{
347-
int buflen;
348-
349-
/* pad unused memory */
350-
buflen = strlen(ps_buffer);
351-
MemSet(ps_buffer + buflen, PS_PADDING, ps_buffer_size - buflen);
352-
}
350+
/* pad unused memory; need only clobber remainder of old status string */
351+
if (last_status_len > ps_buffer_cur_len)
352+
MemSet(ps_buffer + ps_buffer_cur_len, PS_PADDING,
353+
last_status_len - ps_buffer_cur_len);
354+
last_status_len = ps_buffer_cur_len;
353355
#endif /* PS_USE_CLOBBER_ARGV */
354356

355357
#ifdef WIN32
@@ -369,24 +371,15 @@ const char *
369371
get_ps_display(int *displen)
370372
{
371373
#ifdef PS_USE_CLOBBER_ARGV
372-
size_t offset;
373-
374374
/* If ps_buffer is a pointer, it might still be null */
375375
if (!ps_buffer)
376376
{
377377
*displen = 0;
378378
return "";
379379
}
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);
389380
#endif
390381

382+
*displen = (int) (ps_buffer_cur_len - ps_buffer_fixed_size);
383+
391384
return ps_buffer + ps_buffer_fixed_size;
392385
}

0 commit comments

Comments
 (0)
0