Skip to content

Commit 51adcaa

Browse files
committed
Add cluster_name GUC which is included in process titles if set.
When running several postgres clusters on one OS instance it's often inconveniently hard to identify which "postgres" process belongs to which postgres instance. Add the cluster_name GUC, whose value will be included as part of the process titles if set. With that processes can more easily identified using tools like 'ps'. To avoid problems with encoding mismatches between postgresql.conf, consoles, and individual databases replace non-ASCII chars in the name with question marks. The length is limited to NAMEDATALEN to make it less likely to truncate important information at the end of the status. Thomas Munro, with some adjustments by me and review by a host of people.
1 parent a6d488c commit 51adcaa

File tree

6 files changed

+86
-7
lines changed

6 files changed

+86
-7
lines changed

doc/src/sgml/config.sgml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4131,6 +4131,29 @@ local0.* /var/log/postgresql
41314131
</listitem>
41324132
</varlistentry>
41334133

4134+
<varlistentry id="guc-cluster-name" xreflabel="cluster_name">
4135+
<term><varname>cluster_name</varname> (<type>string</type>)</term>
4136+
<indexterm>
4137+
<primary><varname>cluster_name</> configuration parameter</primary>
4138+
</indexterm>
4139+
<listitem>
4140+
<para>
4141+
Sets the cluster name that appears in the process title for all
4142+
processes in this cluster. The name can be any string of less than
4143+
<symbol>NAMEDATALEN</> characters (64 characters in a standard
4144+
build). Only printable ASCII characters may be used in the
4145+
<varname>application_name</varname> value. Other characters will be
4146+
replaced with question marks (<literal>?</literal>). No name is shown
4147+
if this parameter is set to the empty string <literal>''</> (which is
4148+
the default). This parameter can only be set at server start.
4149+
</para>
4150+
<para>
4151+
The process title is typically viewed using programs like
4152+
<application>ps</> or, on Windows, <application>Process Explorer</>.
4153+
</para>
4154+
</listitem>
4155+
</varlistentry>
4156+
41344157
<varlistentry>
41354158
<term><varname>debug_print_parse</varname> (<type>boolean</type>)
41364159
<indexterm>

doc/src/sgml/monitoring.sgml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ postgres: <replaceable>user</> <replaceable>database</> <replaceable>host</> <re
9191
system view to determine who is blocking whom.)
9292
</para>
9393

94+
<para>
95+
If <xref linkend="guc-cluster-name"> has been configured the
96+
cluster name will also be show in <command>ps</> output:
97+
<screen>
98+
$ psql -c 'SHOW cluster_name'
99+
cluster_name
100+
--------------
101+
server1
102+
(1 row)
103+
104+
$ ps aux|grep server1
105+
postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: server1: writer process
106+
...
107+
</screen>
108+
</para>
109+
94110
<para>
95111
If you have turned off <xref linkend="guc-update-process-title"> then the
96112
activity indicator is not updated; the process title is set only once

src/backend/utils/misc/guc.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ static void assign_effective_io_concurrency(int newval, void *extra);
198198
static void assign_pgstat_temp_directory(const char *newval, void *extra);
199199
static bool check_application_name(char **newval, void **extra, GucSource source);
200200
static void assign_application_name(const char *newval, void *extra);
201+
static bool check_cluster_name(char **newval, void **extra, GucSource source);
201202
static const char *show_unix_socket_permissions(void);
202203
static const char *show_log_file_mode(void);
203204

@@ -443,6 +444,7 @@ int temp_file_limit = -1;
443444

444445
int num_temp_buffers = 1024;
445446

447+
char *cluster_name = "";
446448
char *data_directory;
447449
char *ConfigFileName;
448450
char *HbaFileName;
@@ -3261,6 +3263,17 @@ static struct config_string ConfigureNamesString[] =
32613263
check_application_name, assign_application_name, NULL
32623264
},
32633265

3266+
{
3267+
{"cluster_name", PGC_POSTMASTER, LOGGING_WHAT,
3268+
gettext_noop("Sets the name of the cluster which is included in the process title."),
3269+
NULL,
3270+
GUC_IS_NAME
3271+
},
3272+
&cluster_name,
3273+
"",
3274+
check_cluster_name, NULL, NULL
3275+
},
3276+
32643277
/* End-of-list marker */
32653278
{
32663279
{NULL, 0, 0, NULL, NULL}, NULL, NULL, NULL, NULL, NULL
@@ -9470,6 +9483,21 @@ assign_application_name(const char *newval, void *extra)
94709483
pgstat_report_appname(newval);
94719484
}
94729485

9486+
static bool
9487+
check_cluster_name(char **newval, void **extra, GucSource source)
9488+
{
9489+
/* Only allow clean ASCII chars in the cluster name */
9490+
char *p;
9491+
9492+
for (p = *newval; *p; p++)
9493+
{
9494+
if (*p < 32 || *p > 126)
9495+
*p = '?';
9496+
}
9497+
9498+
return true;
9499+
}
9500+
94739501
static const char *
94749502
show_unix_socket_permissions(void)
94759503
{

src/backend/utils/misc/postgresql.conf.sample

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,8 @@
435435
# than the specified size in kilobytes;
436436
# -1 disables, 0 logs all temp files
437437
#log_timezone = 'GMT'
438-
438+
#cluster_name = '' # added to process titles if nonempty
439+
# (change requires restart)
439440

440441
#------------------------------------------------------------------------------
441442
# RUNTIME STATISTICS

src/backend/utils/misc/ps_status.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "libpq/libpq.h"
3030
#include "miscadmin.h"
3131
#include "utils/ps_status.h"
32+
#include "utils/guc.h"
3233

3334
extern char **environ;
3435
bool update_process_title = true;
@@ -264,15 +265,24 @@ init_ps_display(const char *username, const char *dbname,
264265
* apparently setproctitle() already adds a `progname:' prefix to the ps
265266
* line
266267
*/
267-
snprintf(ps_buffer, ps_buffer_size,
268-
"%s %s %s ",
269-
username, dbname, host_info);
268+
#define PROGRAM_NAME_PREFIX ""
270269
#else
271-
snprintf(ps_buffer, ps_buffer_size,
272-
"postgres: %s %s %s ",
273-
username, dbname, host_info);
270+
#define PROGRAM_NAME_PREFIX "postgres: "
274271
#endif
275272

273+
if (*cluster_name == '\0')
274+
{
275+
snprintf(ps_buffer, ps_buffer_size,
276+
PROGRAM_NAME_PREFIX "%s %s %s ",
277+
username, dbname, host_info);
278+
}
279+
else
280+
{
281+
snprintf(ps_buffer, ps_buffer_size,
282+
PROGRAM_NAME_PREFIX "%s: %s %s %s ",
283+
cluster_name, username, dbname, host_info);
284+
}
285+
276286
ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer);
277287

278288
set_ps_display(initial_str, true);

src/include/utils/guc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ extern int temp_file_limit;
224224

225225
extern int num_temp_buffers;
226226

227+
extern char *cluster_name;
227228
extern char *data_directory;
228229
extern char *ConfigFileName;
229230
extern char *HbaFileName;

0 commit comments

Comments
 (0)