Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TyOVZ-0004O8-Rb for pgsql-hackers@arkaria.postgresql.org; Thu, 24 Jan 2013 15:14:30 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TyOVZ-0006bx-3v for pgsql-hackers@arkaria.postgresql.org; Thu, 24 Jan 2013 15:14:29 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TyOVV-0006ZW-7J for pgsql-hackers@postgresql.org; Thu, 24 Jan 2013 15:14:25 +0000 Received: from szxga01-in.huawei.com ([119.145.14.64]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TyOVR-0003q3-Ap for pgsql-hackers@postgresql.org; Thu, 24 Jan 2013 15:14:24 +0000 Received: from 172.24.2.119 (EHLO szxeml207-edg.china.huawei.com) ([172.24.2.119]) by szxrg01-dlp.huawei.com (MOS 4.3.4-GA FastPath queued) with ESMTP id AWQ56396; Thu, 24 Jan 2013 23:14:06 +0800 (CST) Received: from SZXEML413-HUB.china.huawei.com (10.82.67.152) by szxeml207-edg.china.huawei.com (172.24.2.56) with Microsoft SMTP Server (TLS) id 14.1.323.7; Thu, 24 Jan 2013 23:14:02 +0800 Received: from blrprnc07ns (10.18.96.96) by szxeml413-hub.china.huawei.com (10.82.67.152) with Microsoft SMTP Server id 14.1.323.7; Thu, 24 Jan 2013 23:13:57 +0800 From: Hari Babu To: "'Magnus Hagander'" , "'Heikki Linnakangas'" CC: "'Amit Kapila'" , "'Boszormenyi Zoltan'" , "'Fujii Masao'" , "'PostgreSQL-development'" References: <507597E2.8090701@vmware.com> <5076DD38.8020804@vmware.com> <507BE1AD.2060709@vmware.com> <507e9adc.43db440a.5351.fffff4d0SMTPIN_ADDED@mx.google.com> <6C0B27F7206C9E4CA54AE035729E9C382853BBED@szxeml509-mbs> <509B6E66.9070408@vmware.com> <509c9cd1.494f420a.3fc7.4362SMTPIN_ADDED@mx.google.com> <6C0B27F7206C9E4CA54AE035729E9C38285499FA@szxeml509-mbx> <50E31370.5030405@cybertec.at> <50EADA46.60802@cybertec.at> <50F93A71.8010307@vmware.com> In-Reply-To: Subject: Re: Passing connection string to pg_basebackup Date: Thu, 24 Jan 2013 20:43:50 +0530 Message-ID: <005501cdfa45$6b0eec80$412cc580$@kommi@huawei.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0056_01CDFA73.84C72880" X-Mailer: Microsoft Office Outlook 12.0 Thread-Index: Ac32P0QhJxq74GyiTtKDbK2hdP+mngCQ58hgAG19+yA= Content-Language: en-us X-Originating-IP: [10.18.96.96] X-CFilter-Loop: Reflected X-Pg-Spam-Score: -3.2 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org ------=_NextPart_000_0056_01CDFA73.84C72880 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit On Tue, Jan 22, 2013 3:27 PM Hari Babu wrote: >On Saturday, January 19, 2013 5:49 PM Magnus Hagander wrote: >>On Fri, Jan 18, 2013 at 1:05 PM, Heikki Linnakangas >> wrote: >>> On 18.01.2013 13:41, Amit Kapila wrote: >>>> >>>> On Friday, January 18, 2013 3:46 PM Heikki Linnakangas wrote: >>>>> >>>>> On 18.01.2013 08:50, Amit Kapila wrote: >>>> So to solve this problem below can be done: >>>> 1. Support connection string in pg_basebackup and mention keepalives or >>>> connection_timeout >>>> 2. Support recv_timeout separately to provide a way to users who are not >>>> comfortable tcp keepalives >>>> >>>> a. 1 can be done alone >>>> b. 2 can be done alone >>>> c. both 1 and 2. >>> >>> >>> Right. Let's do just 1 for now. An general application level, non-TCP, >>> keepalive message at the libpq level might be a good idea, but that's a much >>> larger patch, definitely not 9.3 material. >> >>+1 for doing 1 now. But actually, I think we can just keep it that way >>in the future as well. If you need to specify these fairly advanced >>options, using a connection string really isn't a problem. >> >>I think it would be more worthwhile to go through the rest of the >>tools in bin/ and make sure they *all* support connection strings. >>And, an important point, do it the same way. > >Presently I am trying to implement the option-1 by adding an extra command line >Option -C "connection_string" to pg_basebackup and pg_receivexlog. >This option can be used with all the tools in bin folder. > >The existing command line options to the tools are not planned to remove as of now. > >To handle both options, we can follow these approaches. > >1. To make the code simpler, the connection string is formed inside with the existing >command line options, if the user is not provided the "connection_string" option. >which is used for further processing. > >2. The connection_string and existing command line options are handled separately. > >I feel approach-1 is better. Please provide your suggestions on the same. Here is the patch which handles taking of connection string as an argument to pg_basebackup and pg_receivexlog. Description of changes: 1. New command line "-C connection-string"option is added for passing the connection string. 2. Used "PQconnectdb" function for connecting to server instead of existing function "PQconnectdbParams". 3. The existing command line parameters are formed in a string and passed to "PQconnectdb" function. 4. With the connection string, if user provides additional options with existing command line options, higher priority is given for the additional options. 5. "conninfo_parse" function is modified to handle of single quote in the password provided as input. please provide your suggestions. Regards, Hari babu. ------=_NextPart_000_0056_01CDFA73.84C72880 Content-Type: application/octet-stream; name="pg_basebkup_recvxlog_conn_string_v1.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="pg_basebkup_recvxlog_conn_string_v1.patch" *** a/doc/src/sgml/ref/pg_basebackup.sgml=0A= --- b/doc/src/sgml/ref/pg_basebackup.sgml=0A= ***************=0A= *** 357,362 **** PostgreSQL documentation=0A= --- 357,375 ----=0A= =0A= The following command-line options control the database connection pa= rameters.=0A= =20=20=0A= + =0A= + =0A= + =0A= + =0A= + =0A= + =0A= + Specifies connection string options, used for connecting to serve= r.=0A= + These option can be used along with other user supplied options.= =0A= + In case of conflicting option user supplied option is choosen.=0A= + =0A= + =0A= + =0A= +=20=0A= =0A= =0A= =0A= *** a/doc/src/sgml/ref/pg_receivexlog.sgml=0A= --- b/doc/src/sgml/ref/pg_receivexlog.sgml=0A= ***************=0A= *** 121,126 **** PostgreSQL documentation=0A= --- 121,139 ----=0A= =0A= The following command-line options control the database connection pa= rameters.=0A= =20=20=0A= + =0A= + =0A= + =0A= + =0A= + =0A= + =0A= + Specifies connection string options, used for connecting to serve= r.=0A= + These option can be used along with other user supplied options.= =0A= + In case of conflicting option user supplied option is choosen.=0A= + =0A= + =0A= + =0A= +=20=0A= =0A= =0A= =0A= *** a/src/bin/pg_basebackup/pg_basebackup.c=0A= --- b/src/bin/pg_basebackup/pg_basebackup.c=0A= ***************=0A= *** 126,131 **** usage(void)=0A= --- 126,132 ----=0A= printf(_(" -V, --version output version information, then exit= \n"));=0A= printf(_(" -?, --help show this help, then exit\n"));=0A= printf(_("\nConnection options:\n"));=0A= + printf(_(" -C, --connection-string=3D\"OPTIONS\" Connect to server usin= g OPTIONS\n"));=0A= printf(_(" -h, --host=3DHOSTNAME database server host or socket dire= ctory\n"));=0A= printf(_(" -p, --port=3DPORT database server port number\n"));= =0A= printf(_(" -s, --status-interval=3DINTERVAL\n"=0A= ***************=0A= *** 1104,1125 **** ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int= rownum)=0A= }=0A= =20=20=0A= /*=0A= - * Escape single quotes used in connection parameters=0A= - */=0A= - static char *=0A= - escape_quotes(const char *src)=0A= - {=0A= - char *result =3D escape_single_quotes_ascii(src);=0A= -=20=0A= - if (!result)=0A= - {=0A= - fprintf(stderr, _("%s: out of memory\n"), progname);=0A= - exit(1);=0A= - }=0A= - return result;=0A= - }=0A= -=20=0A= - /*=0A= * Create a recovery.conf file in memory using a PQExpBuffer=0A= */=0A= static void=0A= --- 1105,1110 ----=0A= ***************=0A= *** 1532,1537 **** main(int argc, char **argv)=0A= --- 1517,1523 ----=0A= {"pgdata", required_argument, NULL, 'D'},=0A= {"format", required_argument, NULL, 'F'},=0A= {"checkpoint", required_argument, NULL, 'c'},=0A= + {"connection-string", required_argument, NULL, 'C'},=0A= {"write-recovery-conf", no_argument, NULL, 'R'},=0A= {"xlog", no_argument, NULL, 'x'},=0A= {"xlog-method", required_argument, NULL, 'X'},=0A= ***************=0A= *** 1570,1576 **** main(int argc, char **argv)=0A= }=0A= }=0A= =20=20=0A= ! while ((c =3D getopt_long(argc, argv, "D:F:RxX:l:zZ:c:h:p:U:s:wWvP",=0A= long_options, &option_index)) !=3D -1)=0A= {=0A= switch (c)=0A= --- 1556,1562 ----=0A= }=0A= }=0A= =20=20=0A= ! while ((c =3D getopt_long(argc, argv, "D:F:RxX:l:zZ:C:c:h:p:U:s:wWvP",= =0A= long_options, &option_index)) !=3D -1)=0A= {=0A= switch (c)=0A= ***************=0A= *** 1661,1666 **** main(int argc, char **argv)=0A= --- 1647,1655 ----=0A= exit(1);=0A= }=0A= break;=0A= + case 'C':=0A= + connection_string =3D pg_strdup(optarg);=0A= + break;=0A= case 'h':=0A= dbhost =3D pg_strdup(optarg);=0A= break;=0A= *** a/src/bin/pg_basebackup/pg_receivexlog.c=0A= --- b/src/bin/pg_basebackup/pg_receivexlog.c=0A= ***************=0A= *** 58,63 **** usage(void)=0A= --- 58,64 ----=0A= printf(_(" -V, --version output version information, then exit= \n"));=0A= printf(_(" -?, --help show this help, then exit\n"));=0A= printf(_("\nConnection options:\n"));=0A= + printf(_(" -C, --connection-string=3D\"OPTIONS\" Connect to server usin= g OPTIONS\n"));=0A= printf(_(" -h, --host=3DHOSTNAME database server host or socket dire= ctory\n"));=0A= printf(_(" -p, --port=3DPORT database server port number\n"));= =0A= printf(_(" -s, --status-interval=3DINTERVAL\n"=0A= ***************=0A= *** 305,310 **** main(int argc, char **argv)=0A= --- 306,312 ----=0A= static struct option long_options[] =3D {=0A= {"help", no_argument, NULL, '?'},=0A= {"version", no_argument, NULL, 'V'},=0A= + {"connection-string", required_argument, NULL, 'C'},=0A= {"directory", required_argument, NULL, 'D'},=0A= {"host", required_argument, NULL, 'h'},=0A= {"port", required_argument, NULL, 'p'},=0A= ***************=0A= *** 338,348 **** main(int argc, char **argv)=0A= }=0A= }=0A= =20=20=0A= ! while ((c =3D getopt_long(argc, argv, "D:h:p:U:s:nwWv",=0A= long_options, &option_index)) !=3D -1)=0A= {=0A= switch (c)=0A= {=0A= case 'D':=0A= basedir =3D pg_strdup(optarg);=0A= break;=0A= --- 340,353 ----=0A= }=0A= }=0A= =20=20=0A= ! while ((c =3D getopt_long(argc, argv, "C:D:h:p:U:s:nwWv",=0A= long_options, &option_index)) !=3D -1)=0A= {=0A= switch (c)=0A= {=0A= + case 'C':=0A= + connection_string =3D pg_strdup(optarg);=0A= + break;=0A= case 'D':=0A= basedir =3D pg_strdup(optarg);=0A= break;=0A= *** a/src/bin/pg_basebackup/streamutil.c=0A= --- b/src/bin/pg_basebackup/streamutil.c=0A= ***************=0A= *** 24,29 **** char *dbport =3D NULL;=0A= --- 24,47 ----=0A= int dbgetpassword =3D 0; /* 0=3Dauto, -1=3Dnever, 1=3Dalways */=0A= static char *dbpassword =3D NULL;=0A= PGconn *conn =3D NULL;=0A= + char *connection_string =3D NULL;=0A= +=20=0A= +=20=0A= + /*=0A= + * Escape single quotes used in connection parameters=0A= + */=0A= + char *=0A= + escape_quotes(const char *src)=0A= + {=0A= + char *result =3D escape_single_quotes_ascii(src);=0A= +=20=0A= + if (!result)=0A= + {=0A= + fprintf(stderr, _("%s: out of memory\n"), progname);=0A= + exit(1);=0A= + }=0A= + return result;=0A= + }=0A= =20=20=0A= /*=0A= * strdup() and malloc() replacements that print an error and exit=0A= ***************=0A= *** 71,119 **** PGconn *=0A= GetConnection(void)=0A= {=0A= PGconn *tmpconn;=0A= - int argcount =3D 4; /* dbname, replication, fallback_app_name,=0A= - * password */=0A= - int i;=0A= - const char **keywords;=0A= - const char **values;=0A= char *password =3D NULL;=0A= const char *tmpparam;=0A= !=20=0A= ! if (dbhost)=0A= ! argcount++;=0A= ! if (dbuser)=0A= ! argcount++;=0A= ! if (dbport)=0A= ! argcount++;=0A= !=20=0A= ! keywords =3D pg_malloc0((argcount + 1) * sizeof(*keywords));=0A= ! values =3D pg_malloc0((argcount + 1) * sizeof(*values));=0A= !=20=0A= ! keywords[0] =3D "dbname";=0A= ! values[0] =3D "replication";=0A= ! keywords[1] =3D "replication";=0A= ! values[1] =3D "true";=0A= ! keywords[2] =3D "fallback_application_name";=0A= ! values[2] =3D progname;=0A= ! i =3D 3;=0A= if (dbhost)=0A= ! {=0A= ! keywords[i] =3D "host";=0A= ! values[i] =3D dbhost;=0A= ! i++;=0A= ! }=0A= if (dbuser)=0A= ! {=0A= ! keywords[i] =3D "user";=0A= ! values[i] =3D dbuser;=0A= ! i++;=0A= ! }=0A= if (dbport)=0A= ! {=0A= ! keywords[i] =3D "port";=0A= ! values[i] =3D dbport;=0A= ! i++;=0A= ! }=0A= =20=20=0A= while (true)=0A= {=0A= --- 89,122 ----=0A= GetConnection(void)=0A= {=0A= PGconn *tmpconn;=0A= char *password =3D NULL;=0A= const char *tmpparam;=0A= ! char conninfo[MAXCONNINFO];=0A= ! int conn_len =3D 0;=0A= ! char *conninfo_ptr =3D conninfo;=0A= !=20=0A= ! /*=0A= ! * Connect using deliberately undocumented parameter: replication. The= =0A= ! * database name is ignored by the server in replication mode.=0A= ! */=0A= ! conn_len +=3D snprintf(conninfo_ptr, (sizeof(conninfo) - conn_len),=0A= ! "%s dbname=3Dreplication replication=3Dtrue fallback_application_name=3D= '%s' ",=0A= ! connection_string ? connection_string : "", progname);=0A= !=20=0A= ! /*=0A= ! * Extra options provided are added at the end of connection string.=0A= ! */=0A= if (dbhost)=0A= ! conn_len +=3D snprintf((conninfo_ptr + conn_len), (sizeof(conninfo) - c= onn_len),=0A= ! "host=3D'%s' ", dbhost);=0A= !=20=0A= if (dbuser)=0A= ! conn_len +=3D snprintf((conninfo_ptr + conn_len), (sizeof(conninfo) - c= onn_len),=0A= ! "user=3D%s ", dbuser);=0A= !=20=0A= if (dbport)=0A= ! conn_len +=3D snprintf((conninfo_ptr + conn_len), (sizeof(conninfo) - c= onn_len),=0A= ! "port=3D%s ", dbport);=0A= =20=20=0A= while (true)=0A= {=0A= ***************=0A= *** 127,144 **** GetConnection(void)=0A= * meaning this is the call for a second session to the same=0A= * database, so just forcibly reuse that password.=0A= */=0A= ! keywords[argcount - 1] =3D "password";=0A= ! values[argcount - 1] =3D dbpassword;=0A= dbgetpassword =3D -1; /* Don't try again if this fails */=0A= }=0A= else if (dbgetpassword =3D=3D 1)=0A= {=0A= password =3D simple_prompt(_("Password: "), 100, false);=0A= ! keywords[argcount - 1] =3D "password";=0A= ! values[argcount - 1] =3D password;=0A= }=0A= =20=20=0A= ! tmpconn =3D PQconnectdbParams(keywords, values, true);=0A= =20=20=0A= /*=0A= * If there is too little memory even to allocate the PGconn object=0A= --- 130,147 ----=0A= * meaning this is the call for a second session to the same=0A= * database, so just forcibly reuse that password.=0A= */=0A= ! snprintf((conninfo_ptr + conn_len), (sizeof(conninfo) - conn_len),=0A= ! "password=3D'%s' ", escape_quotes(dbpassword));=0A= dbgetpassword =3D -1; /* Don't try again if this fails */=0A= }=0A= else if (dbgetpassword =3D=3D 1)=0A= {=0A= password =3D simple_prompt(_("Password: "), 100, false);=0A= ! snprintf((conninfo_ptr + conn_len), (sizeof(conninfo) - conn_len),=0A= ! "password=3D'%s' ", escape_quotes(password));=0A= }=0A= =20=20=0A= ! tmpconn =3D PQconnectdb(conninfo);=0A= =20=20=0A= /*=0A= * If there is too little memory even to allocate the PGconn object=0A= ***************=0A= *** 165,179 **** GetConnection(void)=0A= fprintf(stderr, _("%s: could not connect to server: %s\n"),=0A= progname, PQerrorMessage(tmpconn));=0A= PQfinish(tmpconn);=0A= - free(values);=0A= - free(keywords);=0A= return NULL;=0A= }=0A= =20=20=0A= - /* Connection ok! */=0A= - free(values);=0A= - free(keywords);=0A= -=20=0A= /*=0A= * Ensure we have the same value of integer timestamps as the server=0A= * we are connecting to.=0A= --- 168,176 ----=0A= *** a/src/bin/pg_basebackup/streamutil.h=0A= --- b/src/bin/pg_basebackup/streamutil.h=0A= ***************=0A= *** 5,10 **** extern char *dbhost;=0A= --- 5,11 ----=0A= extern char *dbuser;=0A= extern char *dbport;=0A= extern int dbgetpassword;=0A= + extern char *connection_string;=0A= =20=20=0A= /* Connection kept global so we can disconnect easily */=0A= extern PGconn *conn;=0A= ***************=0A= *** 15,20 **** extern PGconn *conn;=0A= --- 16,22 ----=0A= exit(code); \=0A= }=0A= =20=20=0A= + extern char *escape_quotes(const char *src);=0A= =20=20=0A= extern char *pg_strdup(const char *s);=0A= extern void *pg_malloc0(size_t size);=0A= *** a/src/include/pg_config_manual.h=0A= --- b/src/include/pg_config_manual.h=0A= ***************=0A= *** 200,205 ****=0A= --- 200,211 ----=0A= #endif=0A= =20=20=0A= /*=0A= + * MAXCONNINFO: maximum size of a connection string.=0A= + */=0A= + #define MAXCONNINFO 1024=0A= +=20=0A= +=20=0A= + /*=0A= *------------------------------------------------------------------------= =0A= * The following symbols are for enabling debugging code, not for=0A= * controlling user-visible features or resource limits.=0A= *** a/src/include/replication/walreceiver.h=0A= --- b/src/include/replication/walreceiver.h=0A= ***************=0A= *** 23,35 **** extern int wal_receiver_status_interval;=0A= extern int wal_receiver_timeout;=0A= extern bool hot_standby_feedback;=0A= =20=20=0A= - /*=0A= - * MAXCONNINFO: maximum size of a connection string.=0A= - *=0A= - * XXX: Should this move to pg_config_manual.h?=0A= - */=0A= - #define MAXCONNINFO 1024=0A= -=20=0A= /* Can we allow the standby to accept replication connection from another= standby? */=0A= #define AllowCascadeReplication() (EnableHotStandby && max_wal_senders > = 0)=0A= =20=20=0A= --- 23,28 ----=0A= *** a/src/interfaces/libpq/fe-connect.c=0A= --- b/src/interfaces/libpq/fe-connect.c=0A= ***************=0A= *** 4216,4224 **** conninfo_parse(const char *conninfo, PQExpBuffer errorMe= ssage,=0A= }=0A= if (*cp =3D=3D '\'')=0A= {=0A= - *cp2 =3D '\0';=0A= cp++;=0A= ! break;=0A= }=0A= *cp2++ =3D *cp++;=0A= }=0A= --- 4216,4227 ----=0A= }=0A= if (*cp =3D=3D '\'')=0A= {=0A= cp++;=0A= ! if (*cp !=3D '\'')=0A= ! {=0A= ! *cp2 =3D '\0';=0A= ! break;=0A= ! }=0A= }=0A= *cp2++ =3D *cp++;=0A= }=0A= ------=_NextPart_000_0056_01CDFA73.84C72880 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers ------=_NextPart_000_0056_01CDFA73.84C72880--