commit 8783a78011e3aaa84c2fd32152cc42490eed316b
Author: kuroda.hayato%40jp.fujitsu.com <kuroda.hayato@jp.fujitsu.com>
Date:   Thu Aug 26 05:11:37 2021 +0000

    add application_name to postgres_fdw

diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c
index 82aa14a65d..0dc13f4f58 100644
--- a/contrib/postgres_fdw/connection.c
+++ b/contrib/postgres_fdw/connection.c
@@ -29,6 +29,7 @@
 #include "utils/inval.h"
 #include "utils/memutils.h"
 #include "utils/syscache.h"
+#include "utils/guc.h"
 
 /*
  * Connection cache hash table entry
@@ -79,6 +80,10 @@ static unsigned int prep_stmt_number = 0;
 /* tracks whether any work is needed in callback functions */
 static bool xact_got_connection = false;
 
+/* GUC parameters */
+static char* pgfdw_application_name = NULL;
+void _PG_init(void);
+
 /*
  * SQL functions
  */
@@ -354,7 +359,8 @@ connect_pg_server(ForeignServer *server, UserMapping *user)
 		 * Construct connection params from generic options of ForeignServer
 		 * and UserMapping.  (Some of them might not be libpq options, in
 		 * which case we'll just waste a few array slots.)  Add 3 extra slots
-		 * for fallback_application_name, client_encoding, end marker.
+		 * for application_name or fallback_application_name, client_encoding,
+		 * end marker.
 		 */
 		n = list_length(server->options) + list_length(user->options) + 3;
 		keywords = (const char **) palloc(n * sizeof(char *));
@@ -366,10 +372,20 @@ connect_pg_server(ForeignServer *server, UserMapping *user)
 		n += ExtractConnectionOptions(user->options,
 									  keywords + n, values + n);
 
-		/* Use "postgres_fdw" as fallback_application_name. */
-		keywords[n] = "fallback_application_name";
-		values[n] = "postgres_fdw";
-		n++;
+		if (pgfdw_application_name && *pgfdw_application_name != '\0')
+		{
+			/* Use GUC paramter if set */
+			keywords[n] = "application_name";
+			values[n] = pgfdw_application_name;
+			n++;
+		}
+		else
+		{
+			/* Use "postgres_fdw" as fallback_application_name */
+			keywords[n] = "fallback_application_name";
+			values[n] = "postgres_fdw";
+			n++;
+		}
 
 		/* Set client_encoding so that libpq can convert encoding properly. */
 		keywords[n] = "client_encoding";
@@ -1645,3 +1661,21 @@ disconnect_cached_connections(Oid serverid)
 
 	return result;
 }
+
+/*
+ * Define GUC parameters.
+ */
+void
+_PG_init(void)
+{
+	DefineCustomStringVariable("postgres_fdw.application_name",
+							   "Sets the application name. This is used when connects to the remote server.",
+							   NULL,
+							   &pgfdw_application_name,
+							   NULL,
+							   PGC_USERSET,
+							   GUC_IS_NAME,
+							   NULL,
+							   NULL,
+							   NULL);
+}
diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index 9d443baf02..a4720f9e0a 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -318,6 +318,8 @@ typedef struct
  */
 PG_FUNCTION_INFO_V1(postgres_fdw_handler);
 
+void _PG_init(void);
+
 /*
  * FDW callback routines
  */
diff --git a/doc/src/sgml/postgres-fdw.sgml b/doc/src/sgml/postgres-fdw.sgml
index 0075bc3dbb..b6af6e118d 100644
--- a/doc/src/sgml/postgres-fdw.sgml
+++ b/doc/src/sgml/postgres-fdw.sgml
@@ -105,6 +105,26 @@
   of columns to the remote table is by name, not position.
  </para>
 
+ <sect2>
+  <title> Configuration Parameters </title>
+  <variablelist>
+   <varlistentry>
+    <term>
+     <varname>postgres_fdw.application_name</varname> (<type>string</type>)
+     <indexterm>
+      <primary><varname>postgres_fdw.application_name</varname> configuration parameter</primary>
+     </indexterm>
+    </term>
+    <listitem>
+     <para>
+      Specifies a value for <xref linkend="guc-application-name"/> configuration parameter. This value
+      is used only when a backend process starts to establish the remote connection. 
+     </para>
+    </listitem>
+   </varlistentry>
+  </variablelist>
+ </sect2>
+
  <sect2>
   <title>FDW Options of postgres_fdw</title>
 
