diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c index 82aa14a65d..9c15bc3957 100644 --- a/contrib/postgres_fdw/connection.c +++ b/contrib/postgres_fdw/connection.c @@ -353,10 +353,11 @@ 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. + * which case we'll just waste a few array slots.) Add 4 extra slots + * for application_name, fallback_application_name, client_encoding, + * end marker. */ - n = list_length(server->options) + list_length(user->options) + 3; + n = list_length(server->options) + list_length(user->options) + 4; keywords = (const char **) palloc(n * sizeof(char *)); values = (const char **) palloc(n * sizeof(char *)); @@ -366,7 +367,19 @@ connect_pg_server(ForeignServer *server, UserMapping *user) n += ExtractConnectionOptions(user->options, keywords + n, values + n); - /* Use "postgres_fdw" as fallback_application_name. */ + /* + * Use pgfdw_application_name as application_name. + * + * Note that this check must be behind constructing generic options + * because pgfdw_application_name has higher priority. + */ + if (pgfdw_application_name && *pgfdw_application_name != '\0') + { + keywords[n] = "application_name"; + values[n] = pgfdw_application_name; + n++; + } + /* Use "postgres_fdw" as fallback_application_name */ keywords[n] = "fallback_application_name"; values[n] = "postgres_fdw"; n++; diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out index e3ee30f1aa..c3dc3c0565 100644 --- a/contrib/postgres_fdw/expected/postgres_fdw.out +++ b/contrib/postgres_fdw/expected/postgres_fdw.out @@ -10761,3 +10761,50 @@ ERROR: invalid value for integer option "fetch_size": 100$%$#$# CREATE FOREIGN TABLE inv_bsz (c1 int ) SERVER loopback OPTIONS (batch_size '100$%$#$#'); ERROR: invalid value for integer option "batch_size": 100$%$#$# +-- =================================================================== +-- test postgres_fdw.application_name +-- =================================================================== +-- Disconnect once because the value is used only when establishing connections +DO $$ + BEGIN + PERFORM postgres_fdw_disconnect_all(); + END +$$; +-- Set postgres_fdw.application_name +SET postgres_fdw.application_name TO 'guc_application_name'; +-- Add application_name server option only to loopback2 +ALTER SERVER loopback2 OPTIONS (ADD application_name 'loopback2'); +-- Start transaction because make sure remote connections are alive +BEGIN; +-- Make new connection and check pg_stat_activity +SELECT FROM ft1 LIMIT 1; +-- +(1 row) + +SELECT COUNT(*) FROM pg_stat_activity WHERE application_name LIKE 'guc_application_name'; + count +------- + 1 +(1 row) + +-- application_name will be overwritten by postgres_fdw.application_name +SELECT FROM ft6 LIMIT 1; +-- +(1 row) + +SELECT COUNT(*) FROM pg_stat_activity WHERE application_name LIKE 'loopback2'; + count +------- + 0 +(1 row) + +SELECT COUNT(*) FROM pg_stat_activity WHERE application_name LIKE 'guc_application_name'; + count +------- + 1 +(1 row) + +COMMIT; +--Clean up +ALTER SERVER loopback2 OPTIONS (DROP application_name); +RESET postgres_fdw.application_name; diff --git a/contrib/postgres_fdw/option.c b/contrib/postgres_fdw/option.c index c574ca2cf3..3bf03c4643 100644 --- a/contrib/postgres_fdw/option.c +++ b/contrib/postgres_fdw/option.c @@ -1,7 +1,7 @@ /*------------------------------------------------------------------------- * * option.c - * FDW option handling for postgres_fdw + * FDW and GUC option handling for postgres_fdw * * Portions Copyright (c) 2012-2021, PostgreSQL Global Development Group * @@ -18,6 +18,7 @@ #include "catalog/pg_user_mapping.h" #include "commands/defrem.h" #include "commands/extension.h" +#include "common/string.h" #include "postgres_fdw.h" #include "utils/builtins.h" #include "utils/guc.h" @@ -52,6 +53,16 @@ static void InitPgFdwOptions(void); static bool is_valid_option(const char *keyword, Oid context); static bool is_libpq_option(const char *keyword); +/* + * GUC parameters + */ +char* pgfdw_application_name = NULL; + +/* + * _PG_init() + */ +void _PG_init(void); + #include "miscadmin.h" /* @@ -435,3 +446,21 @@ ExtractExtensionList(const char *extensionsString, bool warnOnMissing) list_free(extlist); return extensionOids; } + +/* + * 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, + 0, + NULL, + NULL, + NULL); +} diff --git a/contrib/postgres_fdw/postgres_fdw.h b/contrib/postgres_fdw/postgres_fdw.h index ca83306af9..90b72e9ec5 100644 --- a/contrib/postgres_fdw/postgres_fdw.h +++ b/contrib/postgres_fdw/postgres_fdw.h @@ -158,6 +158,7 @@ extern int ExtractConnectionOptions(List *defelems, const char **values); extern List *ExtractExtensionList(const char *extensionsString, bool warnOnMissing); +extern char *pgfdw_application_name; /* in deparse.c */ extern void classifyConditions(PlannerInfo *root, diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql index 30b5175da5..3bc19236a2 100644 --- a/contrib/postgres_fdw/sql/postgres_fdw.sql +++ b/contrib/postgres_fdw/sql/postgres_fdw.sql @@ -3422,3 +3422,33 @@ CREATE FOREIGN TABLE inv_fsz (c1 int ) -- Invalid batch_size option CREATE FOREIGN TABLE inv_bsz (c1 int ) SERVER loopback OPTIONS (batch_size '100$%$#$#'); + +-- =================================================================== +-- test postgres_fdw.application_name +-- =================================================================== +-- Disconnect once because the value is used only when establishing connections +DO $$ + BEGIN + PERFORM postgres_fdw_disconnect_all(); + END +$$; +-- Set postgres_fdw.application_name +SET postgres_fdw.application_name TO 'guc_application_name'; +-- Add application_name server option only to loopback2 +ALTER SERVER loopback2 OPTIONS (ADD application_name 'loopback2'); + +-- Start transaction because make sure remote connections are alive +BEGIN; +-- Make new connection and check pg_stat_activity +SELECT FROM ft1 LIMIT 1; +SELECT COUNT(*) FROM pg_stat_activity WHERE application_name LIKE 'guc_application_name'; + +-- application_name will be overwritten by postgres_fdw.application_name +SELECT FROM ft6 LIMIT 1; +SELECT COUNT(*) FROM pg_stat_activity WHERE application_name LIKE 'loopback2'; +SELECT COUNT(*) FROM pg_stat_activity WHERE application_name LIKE 'guc_application_name'; +COMMIT; + +--Clean up +ALTER SERVER loopback2 OPTIONS (DROP application_name); +RESET postgres_fdw.application_name; diff --git a/doc/src/sgml/postgres-fdw.sgml b/doc/src/sgml/postgres-fdw.sgml index 0075bc3dbb..9afb080786 100644 --- a/doc/src/sgml/postgres-fdw.sgml +++ b/doc/src/sgml/postgres-fdw.sgml @@ -105,6 +105,34 @@ of columns to the remote table is by name, not position. + + Configuration Parameters + + + + postgres_fdw.application_name (string) + + + postgres_fdw.application_name configuration parameter + + + + + + Specifies a value for + configuration parameter. This value is used only when a backend process + starts to establish the remote connection. + + + Note that this parameter has higher priority than sever-level options. + If application_name is specified as the + option of the server object, and set this parameter at the same time, + postgres_fdw.application_name will be used. + + + + + FDW Options of postgres_fdw