Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/postgres_fdw/option.c')
-rw-r--r--contrib/postgres_fdw/option.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/contrib/postgres_fdw/option.c b/contrib/postgres_fdw/option.c
index 36555398ecb..c2c4e36802c 100644
--- a/contrib/postgres_fdw/option.c
+++ b/contrib/postgres_fdw/option.c
@@ -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"
@@ -446,6 +447,67 @@ ExtractExtensionList(const char *extensionsString, bool warnOnMissing)
}
/*
+ * 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
*/
void