>From cdef775befc2a770e3ca29c434ae42666375358b Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Sun, 29 Jun 2014 11:40:53 +0200 Subject: [PATCH] Add cluster_name GUC which will be 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 instance. Add the cluster_name GUC which will be included as part of the process titles if set. With that processes can more easily identified using tools like 'ps'. Thomas Munro, with some adjustments by me and review by a host of people. --- doc/src/sgml/config.sgml | 23 ++++++++++++++++++++++ doc/src/sgml/monitoring.sgml | 16 +++++++++++++++ src/backend/utils/misc/guc.c | 28 +++++++++++++++++++++++++++ src/backend/utils/misc/postgresql.conf.sample | 3 ++- src/backend/utils/misc/ps_status.c | 22 +++++++++++++++------ src/include/utils/guc.h | 1 + 6 files changed, 86 insertions(+), 7 deletions(-) diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index e3d1c62..11d552e 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -4131,6 +4131,29 @@ local0.* /var/log/postgresql + + cluster_name (string) + + cluster_name configuration parameter + + + + Sets the cluster name that appears in the process title for all + processes in this cluster. The name can be any string of less than + NAMEDATALEN characters (64 characters in a standard + build). Only printable ASCII characters may be used in the + application_name value. Other characters will be + replaced with question marks (?). No name is shown + if this parameter is set to the empty string '' (which is + the default). This parameter can only be set at server start. + + + The process title is typically viewed using programs like + ps or, on Windows, Process Explorer. + + + + debug_print_parse (boolean) diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml index 69d99e7..88f22a1 100644 --- a/doc/src/sgml/monitoring.sgml +++ b/doc/src/sgml/monitoring.sgml @@ -92,6 +92,22 @@ postgres: user database host + If has been configured the + cluster name will also be show in ps output: + +$ psql -c 'SHOW cluster_name' + cluster_name +-------------- + server1 +(1 row) + +$ ps aux|grep server1 +postgres 27093 0.0 0.0 30096 2752 ? Ss 11:34 0:00 postgres: server1 logger process +... + + + + If you have turned off then the activity indicator is not updated; the process title is set only once when a new process is launched. On some platforms this saves a measurable diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 6902c23..3a31a75 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -198,6 +198,7 @@ static void assign_effective_io_concurrency(int newval, void *extra); static void assign_pgstat_temp_directory(const char *newval, void *extra); static bool check_application_name(char **newval, void **extra, GucSource source); static void assign_application_name(const char *newval, void *extra); +static bool check_cluster_name(char **newval, void **extra, GucSource source); static const char *show_unix_socket_permissions(void); static const char *show_log_file_mode(void); @@ -443,6 +444,7 @@ int temp_file_limit = -1; int num_temp_buffers = 1024; +char *cluster_name = ""; char *data_directory; char *ConfigFileName; char *HbaFileName; @@ -3261,6 +3263,17 @@ static struct config_string ConfigureNamesString[] = check_application_name, assign_application_name, NULL }, + { + {"cluster_name", PGC_POSTMASTER, LOGGING_WHAT, + gettext_noop("Sets the name of the cluster which is included in the process title."), + NULL, + GUC_IS_NAME + }, + &cluster_name, + "", + check_cluster_name, NULL, NULL + }, + /* End-of-list marker */ { {NULL, 0, 0, NULL, NULL}, NULL, NULL, NULL, NULL, NULL @@ -9470,6 +9483,21 @@ assign_application_name(const char *newval, void *extra) pgstat_report_appname(newval); } +static bool +check_cluster_name(char **newval, void **extra, GucSource source) +{ + /* Only allow clean ASCII chars in the cluster name */ + char *p; + + for (p = *newval; *p; p++) + { + if (*p < 32 || *p > 126) + *p = '?'; + } + + return true; +} + static const char * show_unix_socket_permissions(void) { diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample index d109394..3f3e706 100644 --- a/src/backend/utils/misc/postgresql.conf.sample +++ b/src/backend/utils/misc/postgresql.conf.sample @@ -435,7 +435,8 @@ # than the specified size in kilobytes; # -1 disables, 0 logs all temp files #log_timezone = 'GMT' - +#cluster_name = '' # added to process titles if nonempty + # (change requires restart) #------------------------------------------------------------------------------ # RUNTIME STATISTICS diff --git a/src/backend/utils/misc/ps_status.c b/src/backend/utils/misc/ps_status.c index 5407d3f..35f6ab6 100644 --- a/src/backend/utils/misc/ps_status.c +++ b/src/backend/utils/misc/ps_status.c @@ -29,6 +29,7 @@ #include "libpq/libpq.h" #include "miscadmin.h" #include "utils/ps_status.h" +#include "utils/guc.h" extern char **environ; bool update_process_title = true; @@ -264,15 +265,24 @@ init_ps_display(const char *username, const char *dbname, * apparently setproctitle() already adds a `progname:' prefix to the ps * line */ - snprintf(ps_buffer, ps_buffer_size, - "%s %s %s ", - username, dbname, host_info); +#define PROGRAM_NAME_PREFIX "" #else - snprintf(ps_buffer, ps_buffer_size, - "postgres: %s %s %s ", - username, dbname, host_info); +#define PROGRAM_NAME_PREFIX "postgres: " #endif + if (*cluster_name == '\0') + { + snprintf(ps_buffer, ps_buffer_size, + PROGRAM_NAME_PREFIX "%s %s %s ", + username, dbname, host_info); + } + else + { + snprintf(ps_buffer, ps_buffer_size, + PROGRAM_NAME_PREFIX "%s %s %s %s ", + cluster_name, username, dbname, host_info); + } + ps_buffer_cur_len = ps_buffer_fixed_size = strlen(ps_buffer); set_ps_display(initial_str, true); diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h index 1493d2c..0a729c1 100644 --- a/src/include/utils/guc.h +++ b/src/include/utils/guc.h @@ -224,6 +224,7 @@ extern int temp_file_limit; extern int num_temp_buffers; +extern char *cluster_name; extern char *data_directory; extern char *ConfigFileName; extern char *HbaFileName; -- 2.0.0.rc2.4.g1dc51c6.dirty