Skip to content

Commit 94c49d5

Browse files
committed
postgres_fdw: Make postgres_fdw.application_name support more escape sequences.
Commit 6e0cb3d 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
1 parent c476f38 commit 94c49d5

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

contrib/postgres_fdw/expected/postgres_fdw.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10910,6 +10910,26 @@ SELECT pg_terminate_backend(pid, 180000) FROM pg_stat_activity
1091010910
t
1091110911
(1 row)
1091210912

10913+
-- Test %c (session ID) and %C (cluster name) escape sequences.
10914+
SET postgres_fdw.application_name TO 'fdw_%C%c';
10915+
SELECT 1 FROM ft6 LIMIT 1;
10916+
?column?
10917+
----------
10918+
1
10919+
(1 row)
10920+
10921+
SELECT pg_terminate_backend(pid, 180000) FROM pg_stat_activity
10922+
WHERE application_name =
10923+
substring('fdw_' || current_setting('cluster_name') ||
10924+
to_hex(trunc(EXTRACT(EPOCH FROM (SELECT backend_start FROM
10925+
pg_stat_get_activity(pg_backend_pid()))))::integer) || '.' ||
10926+
to_hex(pg_backend_pid())
10927+
for current_setting('max_identifier_length')::int);
10928+
pg_terminate_backend
10929+
----------------------
10930+
t
10931+
(1 row)
10932+
1091310933
--Clean up
1091410934
RESET postgres_fdw.application_name;
1091510935
RESET debug_discard_caches;

contrib/postgres_fdw/option.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,12 @@ process_pgfdw_appname(const char *appname)
489489
case 'a':
490490
appendStringInfoString(&buf, application_name);
491491
break;
492+
case 'c':
493+
appendStringInfo(&buf, "%lx.%x", (long) (MyStartTime), MyProcPid);
494+
break;
495+
case 'C':
496+
appendStringInfoString(&buf, cluster_name);
497+
break;
492498
case 'd':
493499
appendStringInfoString(&buf, MyProcPort->database_name);
494500
break;

contrib/postgres_fdw/sql/postgres_fdw.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3501,6 +3501,17 @@ SELECT pg_terminate_backend(pid, 180000) FROM pg_stat_activity
35013501
substring('fdw_' || current_setting('application_name') ||
35023502
CURRENT_USER || '%' for current_setting('max_identifier_length')::int);
35033503

3504+
-- Test %c (session ID) and %C (cluster name) escape sequences.
3505+
SET postgres_fdw.application_name TO 'fdw_%C%c';
3506+
SELECT 1 FROM ft6 LIMIT 1;
3507+
SELECT pg_terminate_backend(pid, 180000) FROM pg_stat_activity
3508+
WHERE application_name =
3509+
substring('fdw_' || current_setting('cluster_name') ||
3510+
to_hex(trunc(EXTRACT(EPOCH FROM (SELECT backend_start FROM
3511+
pg_stat_get_activity(pg_backend_pid()))))::integer) || '.' ||
3512+
to_hex(pg_backend_pid())
3513+
for current_setting('max_identifier_length')::int);
3514+
35043515
--Clean up
35053516
RESET postgres_fdw.application_name;
35063517
RESET debug_discard_caches;

doc/src/sgml/postgres-fdw.sgml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,20 @@ postgres=# SELECT postgres_fdw_disconnect_all();
984984
<entry><literal>%a</literal></entry>
985985
<entry>Application name on local server</entry>
986986
</row>
987+
<row>
988+
<entry><literal>%c</literal></entry>
989+
<entry>
990+
Session ID on local server
991+
(see <xref linkend="guc-log-line-prefix"/> for details)
992+
</entry>
993+
</row>
994+
<row>
995+
<entry><literal>%C</literal></entry>
996+
<entry>
997+
Cluster name in local server
998+
(see <xref linkend="guc-cluster-name"/> for details)
999+
</entry>
1000+
</row>
9871001
<row>
9881002
<entry><literal>%u</literal></entry>
9891003
<entry>User name on local server</entry>

src/include/utils/guc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ extern int temp_file_limit;
271271

272272
extern int num_temp_buffers;
273273

274-
extern char *cluster_name;
274+
extern PGDLLIMPORT char *cluster_name;
275275
extern PGDLLIMPORT char *ConfigFileName;
276276
extern char *HbaFileName;
277277
extern char *IdentFileName;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy