>From 69e7c676e3af6f25520990cfb4337fc129741a95 Mon Sep 17 00:00:00 2001
From: Alex Shulgin <ash@commandprompt.com>
Date: Mon, 24 Nov 2014 18:54:01 +0300
Subject: [PATCH 3/3] Allow URI in primary_conninfo line of recovery.conf file.

---
 doc/src/sgml/high-availability.sgml                      |  6 +++---
 doc/src/sgml/recovery-config.sgml                        |  4 ++--
 src/backend/access/transam/recovery.conf.sample          |  3 ++-
 .../replication/libpqwalreceiver/libpqwalreceiver.c      | 16 +++++++---------
 4 files changed, 14 insertions(+), 15 deletions(-)

diff --git a/doc/src/sgml/high-availability.sgml b/doc/src/sgml/high-availability.sgml
new file mode 100644
index d249959..61cf585
*** a/doc/src/sgml/high-availability.sgml
--- b/doc/src/sgml/high-availability.sgml
*************** protocol to make nodes agree on a serial
*** 681,689 ****
  
     <para>
       If you want to use streaming replication, fill in
!      <varname>primary_conninfo</> with a libpq connection string, including
!      the host name (or IP address) and any additional details needed to
!      connect to the primary server. If the primary needs a password for
       authentication, the password needs to be specified in
       <varname>primary_conninfo</> as well.
     </para>
--- 681,689 ----
  
     <para>
       If you want to use streaming replication, fill in
!      <varname>primary_conninfo</> with a libpq connection string or a URI,
!      including the host name (or IP address) and any additional details needed
!      to connect to the primary server. If the primary needs a password for
       authentication, the password needs to be specified in
       <varname>primary_conninfo</> as well.
     </para>
diff --git a/doc/src/sgml/recovery-config.sgml b/doc/src/sgml/recovery-config.sgml
new file mode 100644
index 0f1ff34..9e17e20
*** a/doc/src/sgml/recovery-config.sgml
--- b/doc/src/sgml/recovery-config.sgml
*************** restore_command = 'copy "C:\\server\\arc
*** 342,349 ****
          </term>
          <listitem>
           <para>
!           Specifies a connection string to be used for the standby server
!           to connect with the primary. This string is in the format
            described in <xref linkend="libpq-connstring">. If any option is
            unspecified in this string, then the corresponding environment
            variable (see <xref linkend="libpq-envars">) is checked. If the
--- 342,349 ----
          </term>
          <listitem>
           <para>
!           Specifies a connection string or a URI to be used for the standby
!           server to connect with the primary. This string is in the format
            described in <xref linkend="libpq-connstring">. If any option is
            unspecified in this string, then the corresponding environment
            variable (see <xref linkend="libpq-envars">) is checked. If the
diff --git a/src/backend/access/transam/recovery.conf.sample b/src/backend/access/transam/recovery.conf.sample
new file mode 100644
index 7657df3..b1815be
*** a/src/backend/access/transam/recovery.conf.sample
--- b/src/backend/access/transam/recovery.conf.sample
***************
*** 116,124 ****
  # primary_conninfo
  #
  # If set, the PostgreSQL server will try to connect to the primary using this
! # connection string and receive XLOG records continuously.
  #
  #primary_conninfo = ''		# e.g. 'host=localhost port=5432'
  #
  # If set, the PostgreSQL server will use the specified replication slot when
  # connecting to the primary via streaming replication to control resource
--- 116,125 ----
  # primary_conninfo
  #
  # If set, the PostgreSQL server will try to connect to the primary using this
! # connection string or URI and receive XLOG records continuously.
  #
  #primary_conninfo = ''		# e.g. 'host=localhost port=5432'
+ 				# or 'postgres://localhost:5432/'
  #
  # If set, the PostgreSQL server will use the specified replication slot when
  # connecting to the primary via streaming replication to control resource
diff --git a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
new file mode 100644
index 65e95c5..eebb3bb
*** a/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
--- b/src/backend/replication/libpqwalreceiver/libpqwalreceiver.c
*************** _PG_init(void)
*** 89,106 ****
  static void
  libpqrcv_connect(char *conninfo)
  {
! 	char		conninfo_repl[MAXCONNINFO + 75];
  
  	/*
! 	 * Connect using deliberately undocumented parameter: replication. The
! 	 * database name is ignored by the server in replication mode, but specify
! 	 * "replication" for .pgpass lookup.
  	 */
! 	snprintf(conninfo_repl, sizeof(conninfo_repl),
! 			 "%s dbname=replication replication=true fallback_application_name=walreceiver",
! 			 conninfo);
! 
! 	streamConn = PQconnectdb(conninfo_repl);
  	if (PQstatus(streamConn) != CONNECTION_OK)
  		ereport(ERROR,
  				(errmsg("could not connect to the primary server: %s",
--- 89,104 ----
  static void
  libpqrcv_connect(char *conninfo)
  {
! 	const char	*keys[] = { "dbname", "dbname",      "replication", "fallback_application_name", NULL };
! 	const char	*vals[] = { conninfo, "replication", "true",        "walreceiver",               NULL };
  
  	/*
! 	 * We exploit the expand_dbname parameter to process the connection string
! 	 * (or URI), then override the options that are necessary to establish the
! 	 * replication type of connection.  In particular, we override any
! 	 * database name provided in conninfo with "replication".
  	 */
! 	streamConn = PQconnectdbParams(keys, vals, /* expand_dbname = */ true);
  	if (PQstatus(streamConn) != CONNECTION_OK)
  		ereport(ERROR,
  				(errmsg("could not connect to the primary server: %s",
-- 
2.1.0

