1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-27 12:41:57 +03:00

postgres_fdw: Allow postgres_fdw.application_name to include escape sequences.

application_name that used when postgres_fdw establishes a connection to
a foreign server can be specified in either or both a connection parameter
of a server object and GUC postgres_fdw.application_name. This commit
allows those parameters to include escape sequences that begins with
% character. Then postgres_fdw replaces those escape sequences with
status information. For example, %d and %u are replaced with user name
and database name in local server, respectively. This feature enables us
to add information more easily to track remote transactions or queries,
into application_name of a remote connection.

Author: Hayato Kuroda
Reviewed-by: Kyotaro Horiguchi, Masahiro Ikeda, Hou Zhijie, Fujii Masao
Discussion: https://postgr.es/m/TYAPR01MB5866FAE71C66547C64616584F5EB9@TYAPR01MB5866.jpnprd01.prod.outlook.com
Discussion: https://postgr.es/m/TYCPR01MB5870D1E8B949DAF6D3B84E02F5F29@TYCPR01MB5870.jpnprd01.prod.outlook.com
This commit is contained in:
Fujii Masao
2021-12-24 16:55:11 +09:00
parent 94226d4506
commit 6e0cb3dec1
6 changed files with 218 additions and 1 deletions

View File

@ -18,6 +18,7 @@
#include "catalog/pg_user_mapping.h"
#include "commands/defrem.h"
#include "commands/extension.h"
#include "libpq/libpq-be.h"
#include "postgres_fdw.h"
#include "utils/builtins.h"
#include "utils/guc.h"
@ -445,6 +446,67 @@ ExtractExtensionList(const char *extensionsString, bool warnOnMissing)
return extensionOids;
}
/*
* Replace escape sequences beginning with % character in the given
* application_name with status information, and return it.
*
* This function always returns a palloc'd string, so the caller is
* responsible for pfreeing it.
*/
char *
process_pgfdw_appname(const char *appname)
{
const char *p;
StringInfoData buf;
Assert(MyProcPort != NULL);
initStringInfo(&buf);
for (p = appname; *p != '\0'; p++)
{
if (*p != '%')
{
/* literal char, just copy */
appendStringInfoChar(&buf, *p);
continue;
}
/* must be a '%', so skip to the next char */
p++;
if (*p == '\0')
break; /* format error - ignore it */
else if (*p == '%')
{
/* string contains %% */
appendStringInfoChar(&buf, '%');
continue;
}
/* process the option */
switch (*p)
{
case 'a':
appendStringInfoString(&buf, application_name);
break;
case 'd':
appendStringInfoString(&buf, MyProcPort->database_name);
break;
case 'p':
appendStringInfo(&buf, "%d", MyProcPid);
break;
case 'u':
appendStringInfoString(&buf, MyProcPort->user_name);
break;
default:
/* format error - ignore it */
break;
}
}
return buf.data;
}
/*
* Module load callback
*/