Skip to content

Files

Latest commit

Dec 15, 2023
f400df2 · Dec 15, 2023

History

History
119 lines (86 loc) · 4.6 KB

20220221_01.md

File metadata and controls

119 lines (86 loc) · 4.6 KB

PostgreSQL 15 preview - postgres_fdw: Make postgres_fdw.application_name support more escape sequences.

作者

digoal

日期

2022-02-21

标签

PostgreSQL , postgres_fdw , application_name


背景

postgres_fdw application_name 提供更多通配符的支持. session ID (%c) and cluster name (%C) 可以更方便的对外部表会话进行管理.

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=94c49d53402240ad7ddbcae9049ff2840a54b9c6

postgres_fdw: Make postgres_fdw.application_name support more escape sequences.  
author	Fujii Masao <[email protected]>	  
Fri, 18 Feb 2022 02:38:12 +0000 (11:38 +0900)  
committer	Fujii Masao <[email protected]>	  
Fri, 18 Feb 2022 02:38:12 +0000 (11:38 +0900)  
commit	94c49d53402240ad7ddbcae9049ff2840a54b9c6  
tree	bff534c8386626a4278a2dd9ac743ae3a50dc351	tree  
parent	c476f380e296bab57fecada1ea96c86d575bf160	commit | diff  
postgres_fdw: Make postgres_fdw.application_name support more escape sequences.  
  
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/[email protected]  
+        <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>  
+-- 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)  
+  

https://www.postgresql.org/docs/devel/postgres-fdw.html#id-1.11.7.45.11

Escape Effect
%a Application name on local server
%c Session ID on local server (see log_line_prefix for details)
%C Cluster name in local server (see cluster_name for details)
%u User name on local server
%d Database name on local server
%p Process ID of backend on local server
%% Literal %

digoal's wechat