postgres_fdw: Make postgres_fdw.application_name support more escape sequences.
authorFujii Masao <fujii@postgresql.org>
Fri, 18 Feb 2022 02:38:12 +0000 (11:38 +0900)
committerFujii Masao <fujii@postgresql.org>
Fri, 18 Feb 2022 02:38:12 +0000 (11:38 +0900)
Commit 6e0cb3dec1 allowed postgres_fdw.application_name to include
escape sequences %a (application name), %d (database name), %u (user name)
and %p (pid). In addition to them, this commit makes it support
the escape sequences for session ID (%c) and cluster name (%C).
These are helpful to investigate where each remote transactions came from.

Author: Fujii Masao
Reviewed-by: Ryohei Takahashi, Kyotaro Horiguchi
Discussion: https://postgr.es/m/1041dc9a-c976-049f-9f14-e7d94c29c4b2@oss.nttdata.com

contrib/postgres_fdw/expected/postgres_fdw.out
contrib/postgres_fdw/option.c
contrib/postgres_fdw/sql/postgres_fdw.sql
doc/src/sgml/postgres-fdw.sgml
src/include/utils/guc.h

index b2e02caefe496d104f778d9cce7ac2f0d9573439..057342083c69b1b5a3f5ebb40ebe97be6b69d094 100644 (file)
@@ -10910,6 +10910,26 @@ SELECT pg_terminate_backend(pid, 180000) FROM pg_stat_activity
  t
 (1 row)
 
+-- Test %c (session ID) and %C (cluster name) escape sequences.
+SET postgres_fdw.application_name TO 'fdw_%C%c';
+SELECT 1 FROM ft6 LIMIT 1;
+ ?column? 
+----------
+        1
+(1 row)
+
+SELECT pg_terminate_backend(pid, 180000) FROM pg_stat_activity
+  WHERE application_name =
+    substring('fdw_' || current_setting('cluster_name') ||
+      to_hex(trunc(EXTRACT(EPOCH FROM (SELECT backend_start FROM
+      pg_stat_get_activity(pg_backend_pid()))))::integer) || '.' ||
+      to_hex(pg_backend_pid())
+      for current_setting('max_identifier_length')::int);
+ pg_terminate_backend 
+----------------------
+ t
+(1 row)
+
 --Clean up
 RESET postgres_fdw.application_name;
 RESET debug_discard_caches;
index fc3ce6a53a286f190f82ded19e18a8d7abc00e1f..af38e956e70fcdd14ea031a4624c25fbc0caad1b 100644 (file)
@@ -489,6 +489,12 @@ process_pgfdw_appname(const char *appname)
            case 'a':
                appendStringInfoString(&buf, application_name);
                break;
+           case 'c':
+               appendStringInfo(&buf, "%lx.%x", (long) (MyStartTime), MyProcPid);
+               break;
+           case 'C':
+               appendStringInfoString(&buf, cluster_name);
+               break;
            case 'd':
                appendStringInfoString(&buf, MyProcPort->database_name);
                break;
index e050639b57247f27a26835fe1a50920bebe8cbd2..6c9f579c41d76758304e76da65173d121e218281 100644 (file)
@@ -3501,6 +3501,17 @@ SELECT pg_terminate_backend(pid, 180000) FROM pg_stat_activity
     substring('fdw_' || current_setting('application_name') ||
       CURRENT_USER || '%' for current_setting('max_identifier_length')::int);
 
+-- Test %c (session ID) and %C (cluster name) escape sequences.
+SET postgres_fdw.application_name TO 'fdw_%C%c';
+SELECT 1 FROM ft6 LIMIT 1;
+SELECT pg_terminate_backend(pid, 180000) FROM pg_stat_activity
+  WHERE application_name =
+    substring('fdw_' || current_setting('cluster_name') ||
+      to_hex(trunc(EXTRACT(EPOCH FROM (SELECT backend_start FROM
+      pg_stat_get_activity(pg_backend_pid()))))::integer) || '.' ||
+      to_hex(pg_backend_pid())
+      for current_setting('max_identifier_length')::int);
+
 --Clean up
 RESET postgres_fdw.application_name;
 RESET debug_discard_caches;
index 7bb6e525a4623546759fd7a6ec6c75dc1352ced4..dc57fe4b0d111972543b0ce4822a25764c1287d8 100644 (file)
@@ -984,6 +984,20 @@ postgres=# SELECT postgres_fdw_disconnect_all();
          <entry><literal>%a</literal></entry>
          <entry>Application name on local server</entry>
         </row>
+        <row>
+         <entry><literal>%c</literal></entry>
+         <entry>
+          Session ID on local server
+          (see <xref linkend="guc-log-line-prefix"/> for details)
+         </entry>
+        </row>
+        <row>
+         <entry><literal>%C</literal></entry>
+         <entry>
+          Cluster name in local server
+          (see <xref linkend="guc-cluster-name"/> for details)
+         </entry>
+        </row>
         <row>
          <entry><literal>%u</literal></entry>
          <entry>User name on local server</entry>
index 6bb81707b09d8b59accf957ac8029f8e48e0c13d..f1bfe79feb81bf6ed6bafc81390452e587cdf538 100644 (file)
@@ -271,7 +271,7 @@ extern int  temp_file_limit;
 
 extern int num_temp_buffers;
 
-extern char *cluster_name;
+extern PGDLLIMPORT char *cluster_name;
 extern PGDLLIMPORT char *ConfigFileName;
 extern char *HbaFileName;
 extern char *IdentFileName;