bdr: Set application_name for both SPI and libpq connections
authorCraig Ringer <[email protected]>
Sun, 18 May 2014 03:26:59 +0000 (11:26 +0800)
committerAndres Freund <[email protected]>
Thu, 3 Jul 2014 15:55:38 +0000 (17:55 +0200)
Setting application_name makes it much easier to see what bdr is doing
in pg_stat_activity and provides a useful log_line_prefix.

contrib/bdr/bdr.c
contrib/bdr/bdr.h
contrib/bdr/bdr_catalogs.c
contrib/bdr/bdr_init_replica.c

index fd5a02b3b60145799e807fe5bbc03e5c972855d7..b3c4d70bb536a3dd72b59b193325506f2ce18338 100644 (file)
@@ -447,21 +447,25 @@ bdr_worker_init(char *dbname)
  *----------------------
  */
 PGconn*
-bdr_establish_connection_and_slot(BdrConnectionConfig *cfg, Name out_slot_name,
-   uint64 *out_sysid, TimeLineID* out_timeline, Oid *out_dboid, RepNodeId
-   *out_replication_identifier, char **out_snapshot)
+bdr_establish_connection_and_slot(BdrConnectionConfig *cfg,
+   const char *application_name_suffix, Name out_slot_name, uint64 *out_sysid,
+   TimeLineID* out_timeline, Oid *out_dboid,
+   RepNodeId *out_replication_identifier, char **out_snapshot)
 {
-   char        conninfo_repl[MAXCONNINFO + 75];
    char        remote_ident[256];
    PGconn     *streamConn;
+   StringInfoData conninfo_repl;
 
-   snprintf(conninfo_repl, sizeof(conninfo_repl),
-            "%s replication=database fallback_application_name=bdr",
-            cfg->dsn);
+   initStringInfo(&conninfo_repl);
+
+   appendStringInfo(&conninfo_repl,
+                    "%s replication=database fallback_application_name='"BDR_LOCALID_FORMAT": %s'",
+                    cfg->dsn, BDR_LOCALID_FORMAT_ARGS,
+                    application_name_suffix);
 
    /* Establish BDR conn and IDENTIFY_SYSTEM */
    streamConn = bdr_connect(
-       conninfo_repl,
+       conninfo_repl.data,
        remote_ident, sizeof(remote_ident),
        out_slot_name, out_sysid, out_timeline, out_dboid
        );
@@ -537,9 +541,36 @@ bdr_apply_main(Datum main_arg)
    elog(DEBUG1, "%s initialized on %s",
         MyBgworkerEntry->bgw_name, bdr_apply_config->dbname);
 
-   streamConn = bdr_establish_connection_and_slot(
-       bdr_apply_config, &slot_name, &origin_sysid,
-       &origin_timeline, &origin_dboid, &replication_identifier, NULL);
+   /* Set our local application_name for our SPI connections */
+   resetStringInfo(&query);
+   appendStringInfo(&query, BDR_LOCALID_FORMAT": %s", BDR_LOCALID_FORMAT_ARGS, "apply");
+   if (bdr_apply_worker->forward_changesets)
+       appendStringInfoString(&query, " catchup");
+
+   if (bdr_apply_worker->replay_stop_lsn != InvalidXLogRecPtr)
+       appendStringInfo(&query, " up to %X/%X",
+                        (uint32)(bdr_apply_worker->replay_stop_lsn),
+                        (uint32)(bdr_apply_worker->replay_stop_lsn>>32));
+
+   SetConfigOption("application_name", query.data, PGC_USERSET, PGC_S_SESSION);
+
+   /* Form an application_name string to send to the remote end */
+   resetStringInfo(&query);
+   appendStringInfoString(&query, "receive");
+
+   if (bdr_apply_worker->forward_changesets)
+       appendStringInfoString(&query, " catchup");
+
+   if (bdr_apply_worker->replay_stop_lsn != InvalidXLogRecPtr)
+       appendStringInfo(&query, " up to %X/%X",
+                        (uint32)(bdr_apply_worker->replay_stop_lsn),
+                        (uint32)(bdr_apply_worker->replay_stop_lsn>>32));
+
+   /* Make the replication connection to the remote end */
+   streamConn = bdr_establish_connection_and_slot(bdr_apply_config,
+       query.data, &slot_name, &origin_sysid, &origin_timeline,
+       &origin_dboid, &replication_identifier, NULL);
+
 
    /* initialize stat subsystem, our id won't change further */
    bdr_count_set_current_node(replication_identifier);
@@ -980,7 +1011,8 @@ bdr_launch_apply_workers(char *dbname)
                            continue;
 
                        snprintf(apply_worker.bgw_name, BGW_MAXLEN,
-                                "bdr apply: %s", cfg->name);
+                                BDR_LOCALID_FORMAT": %s: apply",
+                                BDR_LOCALID_FORMAT_ARGS, cfg->name);
                        apply_worker.bgw_main_arg = Int32GetDatum(i);
 
                        if (!RegisterDynamicBackgroundWorker(&apply_worker,
@@ -1050,6 +1082,9 @@ bdr_perdb_worker_main(Datum main_arg)
    ListCell         *c;
    BdrPerdbWorker   *bdr_perdb_worker;
    BdrWorker        *bdr_worker_slot;
+   StringInfoData    si;
+
+   initStringInfo(&si);
 
    Assert(IsBackgroundWorker);
 
@@ -1059,6 +1094,9 @@ bdr_perdb_worker_main(Datum main_arg)
 
    bdr_worker_init(NameStr(bdr_perdb_worker->dbname));
 
+   appendStringInfo(&si, BDR_LOCALID_FORMAT": %s", BDR_LOCALID_FORMAT_ARGS, "perdb worker");
+   SetConfigOption("application_name", si.data, PGC_USERSET, PGC_S_SESSION);
+
    CurrentResourceOwner = ResourceOwnerCreate(NULL, "bdr seq top-level resource owner");
    bdr_saved_resowner = CurrentResourceOwner;
 
index c80c4c9c96dc57bcc00d635e56c8d15543513f68..94ad4aa560a97d7656ef6e49a3f48e1ea6f4f47e 100644 (file)
 #define BDR_SLOT_NAME_FORMAT "bdr_%u_%s_%u_%u__%s"
 #define BDR_NODE_ID_FORMAT "bdr_"UINT64_FORMAT"_%u_%u_%u_%s"
 
+/* Right now replication_name isn't used; make it easily found for later */
+#define EMPTY_REPLICATION_NAME ""
+
+/*
+ * BDR_LOCALID_FORMAT is used in fallback_application_name. It's distinct from
+ * BDR_NODE_ID_FORMAT in that it doesn't include the remote dboid as that may
+ * not be known yet, just (sysid,tlid,dboid,replication_name) .
+ *
+ * Use BDR_LOCALID_FORMAT_ARGS to sub it in to format strings.
+ */
+#define BDR_LOCALID_FORMAT "bdr ("UINT64_FORMAT",%u,%u,%s)"
+
+#define BDR_LOCALID_FORMAT_ARGS \
+   GetSystemIdentifier(), ThisTimeLineID, MyDatabaseId, EMPTY_REPLICATION_NAME
+
 #define BDR_INIT_REPLICA_CMD "bdr_initial_load"
 
+
 /*
  * Don't include libpq here, msvc infrastructure requires linking to libpq
  * otherwise.
@@ -342,6 +358,7 @@ extern struct pg_conn* bdr_connect(char *conninfo_repl,
 
 extern struct pg_conn *
 bdr_establish_connection_and_slot(BdrConnectionConfig *cfg,
+                                 const char *application_name_suffix,
                                  Name out_slot_name,
                                  uint64 *out_sysid,
                                  TimeLineID *out_timeline,
index d7a6bf0dced97a7075ba3061f1586f3452530163..a990498bdbfca140c7b74fe0fe5fd7708c2965ee 100644 (file)
@@ -30,6 +30,7 @@
 #include "replication/replication_identifier.h"
 
 #include "utils/builtins.h"
+#include "utils/guc.h"
 #include "utils/syscache.h"
 
 /* GetSysCacheOid equivalent that errors out if nothing is found */
index 305d40644988b0309047ba2ce8513a1b9f84704f..1d6d1e7e49d57c34b6cdbe5a92e11741705e67cf 100644 (file)
@@ -432,7 +432,6 @@ static void
 bdr_drop_slot_and_replication_identifier(BdrConnectionConfig *cfg)
 {
 
-   char        conninfo_repl[MAXCONNINFO + 75];
    char        remote_ident[256];
    PGconn     *streamConn;
    RepNodeId   replication_identifier;
@@ -444,16 +443,18 @@ bdr_drop_slot_and_replication_identifier(BdrConnectionConfig *cfg)
    StringInfoData query;
    char       *sqlstate;
 
+   initStringInfo(&query);
+
    elog(DEBUG1, "bdr %s: Dropping slot and local ident from connection %s",
         cfg->dbname, cfg->name);
 
-   snprintf(conninfo_repl, sizeof(conninfo_repl),
-            "%s replication=database fallback_application_name=bdr",
-            cfg->dsn);
+   appendStringInfo(&query,
+                    "%s replication=database fallback_application_name='"BDR_LOCALID_FORMAT": %s: drop slot'",
+                     cfg->dsn, BDR_LOCALID_FORMAT_ARGS, cfg->name);
 
    /* Establish BDR conn and IDENTIFY_SYSTEM */
    streamConn = bdr_connect(
-       conninfo_repl,
+       query.data,
        remote_ident, sizeof(remote_ident),
        &slot_name, &sysid, &timeline, &dboid
        );
@@ -479,7 +480,7 @@ bdr_drop_slot_and_replication_identifier(BdrConnectionConfig *cfg)
     * whether it exists or not silently over the replication protocol,
     * so we just try it and cope if it's missing.
     */
-   initStringInfo(&query);
+   resetStringInfo(&query);
    appendStringInfo(&query, "DROP_REPLICATION_SLOT %s", NameStr(slot_name));
    res = PQexec(streamConn, query.data);
    if (PQresultStatus(res) == PGRES_COMMAND_OK)
@@ -534,9 +535,13 @@ bdr_exec_init_replica(BdrConnectionConfig *cfg, char *snapshot)
    char  bdr_init_replica_script_path[MAXPGPATH];
    const char *envvar;
    StringInfoData path;
+   StringInfoData origin_dsn;
+   StringInfoData local_dsn;
    int   saved_errno;
 
    initStringInfo(&path);
+   initStringInfo(&origin_dsn);
+   initStringInfo(&local_dsn);
 
    bindir = pstrdup(my_exec_path);
    get_parent_directory(bindir);
@@ -550,8 +555,15 @@ bdr_exec_init_replica(BdrConnectionConfig *cfg, char *snapshot)
             PG_VERSION);
    }
 
+   appendStringInfo(&origin_dsn,
+                    "%s fallback_application_name='"BDR_LOCALID_FORMAT": %s: init_replica dump'",
+                    cfg->dsn, BDR_LOCALID_FORMAT_ARGS, cfg->name);
+
    if (cfg->replica_local_dsn == NULL)
        elog(FATAL, "bdr init_replica: no replica_local_dsn specified");
+   appendStringInfo(&local_dsn,
+                    "%s fallback_application_name='"BDR_LOCALID_FORMAT": %s: init_replica restore'",
+                    cfg->replica_local_dsn, BDR_LOCALID_FORMAT_ARGS, cfg->name);
 
    tmpdir = palloc(strlen(bdr_temp_dump_directory)+32);
    sprintf(tmpdir, "%s/postgres-bdr-%s.%d", bdr_temp_dump_directory,
@@ -591,8 +603,8 @@ bdr_exec_init_replica(BdrConnectionConfig *cfg, char *snapshot)
        char *const argv[] = {
            bdr_init_replica_script_path,
            "--snapshot", snapshot,
-           "--source", cfg->dsn,
-           "--target", cfg->replica_local_dsn,
+           "--source", origin_dsn.data,
+           "--target", local_dsn.data,
            "--tmp-directory", tmpdir,
            NULL
        };
@@ -688,12 +700,12 @@ bdr_init_replica(Name dbname)
    char status;
    XLogRecPtr min_remote_lsn;
    PGconn *nonrepl_init_conn;
-   StringInfoData query;
+   StringInfoData dsn;
    BdrWorker  *init_replica_worker;
    BdrConnectionConfig *init_replica_config;
    int spi_ret;
 
-   initStringInfo(&query);
+   initStringInfo(&dsn);
 
    elog(DEBUG2, "bdr %s: bdr_init_replica",
         NameStr(*dbname));
@@ -784,12 +796,18 @@ bdr_init_replica(Name dbname)
    elog(DEBUG2, "bdr %s: bdr_init_replica init from connection %s",
         NameStr(*dbname), init_replica_config->name);
 
+   resetStringInfo(&dsn);
+   appendStringInfo(&dsn,
+                    "%s fallback_application_name='"BDR_LOCALID_FORMAT": %s: init_replica setup'",
+                    init_replica_config->dsn, BDR_LOCALID_FORMAT_ARGS,
+                    init_replica_config->name);
+
    /*
     * Test to see if there's an entry in the remote's bdr.bdr_nodes for our
     * system identifier. If there is, that'll tell us what stage of startup
     * we are up to and let us resume an incomplete start.
     */
-   nonrepl_init_conn = PQconnectdb(init_replica_config->dsn);
+   nonrepl_init_conn = PQconnectdb(dsn.data);
    if (PQstatus(nonrepl_init_conn) != CONNECTION_OK)
    {
        ereport(FATAL,
@@ -939,8 +957,9 @@ bdr_init_replica(Name dbname)
             * are all discarded; they're not needed here, and will be obtained
             * again by the apply workers when they're launched after init.
             */
-           conn = bdr_establish_connection_and_slot(cfg, &slot_name, &sysid,
-               &timeline, &dboid, &replication_identifier, &snapshot);
+           conn = bdr_establish_connection_and_slot(cfg, "create slot",
+               &slot_name, &sysid, &timeline, &dboid, &replication_identifier,
+               &snapshot);
 
            /* Always throws rather than returning failure */
            Assert(conn);