mirror of
https://github.com/postgres/postgres.git
synced 2025-11-09 06:21:09 +03:00
Speedup and increase usability of set proc title functions
The setting of the process title could be seen on profiles of very fast-to-execute queries. In many locations where we call set_ps_display() we pass along a string constant, the length of which is known during compilation. Here we effectively rename set_ps_display() to set_ps_display_with_len() and then add a static inline function named set_ps_display() which calls strlen() on the given string. This allows the compiler to optimize away the strlen() call when dealing with call sites passing a string constant. We can then also use memcpy() instead of strlcpy() to copy the string into the destination buffer. That's significantly faster than strlcpy's byte-at-a-time way of copying. Here we also take measures to improve some code which was adjusting the process title to add a " waiting" suffix to it. Call sites which require this can now just call set_ps_display_suffix() to add or adjust the suffix and call set_ps_display_remove_suffix() to remove it again. Reviewed-by: Andres Freund Discussion: https://postgr.es/m/CAApHDvocBvvk-0gWNA2Gohe+sv9fMcv+fK_G+siBKJrgDG4O7g@mail.gmail.com
This commit is contained in:
@@ -148,8 +148,6 @@ static bool SyncRepQueueIsOrderedByLSN(int mode);
|
||||
void
|
||||
SyncRepWaitForLSN(XLogRecPtr lsn, bool commit)
|
||||
{
|
||||
char *new_status = NULL;
|
||||
const char *old_status;
|
||||
int mode;
|
||||
|
||||
/*
|
||||
@@ -216,15 +214,10 @@ SyncRepWaitForLSN(XLogRecPtr lsn, bool commit)
|
||||
/* Alter ps display to show waiting for sync rep. */
|
||||
if (update_process_title)
|
||||
{
|
||||
int len;
|
||||
char buffer[32];
|
||||
|
||||
old_status = get_ps_display(&len);
|
||||
new_status = (char *) palloc(len + 32 + 1);
|
||||
memcpy(new_status, old_status, len);
|
||||
sprintf(new_status + len, " waiting for %X/%X",
|
||||
LSN_FORMAT_ARGS(lsn));
|
||||
set_ps_display(new_status);
|
||||
new_status[len] = '\0'; /* truncate off " waiting ..." */
|
||||
sprintf(buffer, "waiting for %X/%X", LSN_FORMAT_ARGS(lsn));
|
||||
set_ps_display_suffix(buffer);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -322,12 +315,9 @@ SyncRepWaitForLSN(XLogRecPtr lsn, bool commit)
|
||||
MyProc->syncRepState = SYNC_REP_NOT_WAITING;
|
||||
MyProc->waitLSN = 0;
|
||||
|
||||
if (new_status)
|
||||
{
|
||||
/* Reset ps display */
|
||||
set_ps_display(new_status);
|
||||
pfree(new_status);
|
||||
}
|
||||
/* reset ps display to remove the suffix */
|
||||
if (update_process_title)
|
||||
set_ps_display_remove_suffix();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user