mirror of
https://github.com/postgres/postgres.git
synced 2025-11-29 23:43:17 +03:00
Add support for an application_name parameter, which is displayed in
pg_stat_activity and recorded in log entries. Dave Page, reviewed by Andres Freund
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.54 2009/06/11 14:49:04 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.55 2009/11/28 23:38:07 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -416,7 +416,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
|
||||
|
||||
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
||||
|
||||
tupdesc = CreateTemplateTupleDesc(10, false);
|
||||
tupdesc = CreateTemplateTupleDesc(11, false);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "datid", OIDOID, -1, 0);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "procpid", INT4OID, -1, 0);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "usesysid", OIDOID, -1, 0);
|
||||
@@ -427,6 +427,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 8, "backend_start", TIMESTAMPTZOID, -1, 0);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 9, "client_addr", INETOID, -1, 0);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 10, "client_port", INT4OID, -1, 0);
|
||||
TupleDescInitEntry(tupdesc, (AttrNumber) 11, "application_name", TEXTOID, -1, 0);
|
||||
|
||||
funcctx->tuple_desc = BlessTupleDesc(tupdesc);
|
||||
|
||||
@@ -478,8 +479,8 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
|
||||
if (funcctx->call_cntr < funcctx->max_calls)
|
||||
{
|
||||
/* for each row */
|
||||
Datum values[10];
|
||||
bool nulls[10];
|
||||
Datum values[11];
|
||||
bool nulls[11];
|
||||
HeapTuple tuple;
|
||||
PgBackendStatus *beentry;
|
||||
SockAddr zero_clientaddr;
|
||||
@@ -599,6 +600,12 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
|
||||
nulls[9] = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* application name */
|
||||
if (beentry->st_appname)
|
||||
values[10] = CStringGetTextDatum(beentry->st_appname);
|
||||
else
|
||||
nulls[10] = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -610,6 +617,7 @@ pg_stat_get_activity(PG_FUNCTION_ARGS)
|
||||
nulls[7] = true;
|
||||
nulls[8] = true;
|
||||
nulls[9] = true;
|
||||
nulls[10] = true;
|
||||
}
|
||||
|
||||
tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
|
||||
|
||||
@@ -42,7 +42,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.218 2009/10/17 00:24:50 mha Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.219 2009/11/28 23:38:07 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -68,6 +68,7 @@
|
||||
#include "storage/ipc.h"
|
||||
#include "storage/proc.h"
|
||||
#include "tcop/tcopprot.h"
|
||||
#include "utils/guc.h"
|
||||
#include "utils/memutils.h"
|
||||
#include "utils/ps_status.h"
|
||||
|
||||
@@ -1798,6 +1799,16 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
|
||||
/* process the option */
|
||||
switch (Log_line_prefix[i])
|
||||
{
|
||||
case 'a':
|
||||
if (MyProcPort)
|
||||
{
|
||||
const char *appname = application_name;
|
||||
|
||||
if (appname == NULL || *appname == '\0')
|
||||
appname = _("[unknown]");
|
||||
appendStringInfo(buf, "%s", appname);
|
||||
}
|
||||
break;
|
||||
case 'u':
|
||||
if (MyProcPort)
|
||||
{
|
||||
@@ -2103,6 +2114,11 @@ write_csvlog(ErrorData *edata)
|
||||
appendCSVLiteral(&buf, msgbuf.data);
|
||||
pfree(msgbuf.data);
|
||||
}
|
||||
appendStringInfoCharMacro(&buf, ',');
|
||||
|
||||
/* application name */
|
||||
if (application_name)
|
||||
appendCSVLiteral(&buf, application_name);
|
||||
|
||||
appendStringInfoChar(&buf, '\n');
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
* Written by Peter Eisentraut <peter_e@gmx.net>.
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.523 2009/10/21 20:38:58 tgl Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.524 2009/11/28 23:38:07 tgl Exp $
|
||||
*
|
||||
*--------------------------------------------------------------------
|
||||
*/
|
||||
@@ -168,6 +168,7 @@ static bool assign_maxconnections(int newval, bool doit, GucSource source);
|
||||
static bool assign_autovacuum_max_workers(int newval, bool doit, GucSource source);
|
||||
static bool assign_effective_io_concurrency(int newval, bool doit, GucSource source);
|
||||
static const char *assign_pgstat_temp_directory(const char *newval, bool doit, GucSource source);
|
||||
static const char *assign_application_name(const char *newval, bool doit, GucSource source);
|
||||
|
||||
static char *config_enum_get_options(struct config_enum * record,
|
||||
const char *prefix, const char *suffix,
|
||||
@@ -378,6 +379,8 @@ char *pgstat_temp_directory;
|
||||
|
||||
char *default_do_language;
|
||||
|
||||
char *application_name;
|
||||
|
||||
int tcp_keepalives_idle;
|
||||
int tcp_keepalives_interval;
|
||||
int tcp_keepalives_count;
|
||||
@@ -2534,6 +2537,16 @@ static struct config_string ConfigureNamesString[] =
|
||||
"plpgsql", NULL, NULL
|
||||
},
|
||||
|
||||
{
|
||||
{"application_name", PGC_USERSET, LOGGING,
|
||||
gettext_noop("Sets the application name to be reported in statistics and logs."),
|
||||
NULL,
|
||||
GUC_IS_NAME | GUC_NOT_IN_SAMPLE
|
||||
},
|
||||
&application_name,
|
||||
"", assign_application_name, NULL
|
||||
},
|
||||
|
||||
/* End-of-list marker */
|
||||
{
|
||||
{NULL, 0, 0, NULL, NULL}, NULL, NULL, NULL, NULL
|
||||
@@ -7717,4 +7730,28 @@ assign_pgstat_temp_directory(const char *newval, bool doit, GucSource source)
|
||||
return newval;
|
||||
}
|
||||
|
||||
static const char *
|
||||
assign_application_name(const char *newval, bool doit, GucSource source)
|
||||
{
|
||||
if (doit)
|
||||
{
|
||||
/* Only allow clean ASCII chars in the application name */
|
||||
char *repval = guc_strdup(ERROR, newval);
|
||||
char *p;
|
||||
|
||||
for (p = repval; *p; p++)
|
||||
{
|
||||
if (*p < 32 || *p > 126)
|
||||
*p = '?';
|
||||
}
|
||||
|
||||
/* Update the pg_stat_activity view */
|
||||
pgstat_report_appname(repval);
|
||||
|
||||
return repval;
|
||||
}
|
||||
else
|
||||
return newval;
|
||||
}
|
||||
|
||||
#include "guc-file.c"
|
||||
|
||||
@@ -334,6 +334,7 @@
|
||||
#log_duration = off
|
||||
#log_hostname = off
|
||||
#log_line_prefix = '' # special values:
|
||||
# %a = application name
|
||||
# %u = user name
|
||||
# %d = database name
|
||||
# %r = remote host and port
|
||||
|
||||
Reference in New Issue
Block a user