Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bgrhj-0001P1-Ey for pgsql-hackers@arkaria.postgresql.org; Mon, 05 Sep 2016 11:04:43 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1bgrhi-0006pI-9P for pgsql-hackers@arkaria.postgresql.org; Mon, 05 Sep 2016 11:04:42 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1bgrhh-0006pB-MO for pgsql-hackers@postgresql.org; Mon, 05 Sep 2016 11:04:41 +0000 Received: from newmail.postgrespro.ru ([93.174.131.138] helo=mail.postgrespro.ru) by magus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bgrhe-00011Q-Kk for pgsql-hackers@postgresql.org; Mon, 05 Sep 2016 11:04:40 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 1AD8D21C279B; Mon, 5 Sep 2016 14:04:38 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru Received: from mail.postgrespro.ru ([127.0.0.1]) by localhost (mail.postgrespro.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id l4I3m9MoPqaQ; Mon, 5 Sep 2016 14:04:38 +0300 (MSK) Received: from localhost (unknown [192.168.27.1]) by mail.postgrespro.ru (Postfix) with ESMTPSA id C21C121C279A; Mon, 5 Sep 2016 14:04:37 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1473073477; bh=JdET/jxsP8hTYN4hRh8LWhsWgj0lYbMc0BGIkU/+Od0=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=qhPYbsG+sgiGCRdGAqMqdYm1rKG61bpZHJYGNdnBuXjRMnP7mByGVhovTDJqBgk6j O/V+Y0UjG3foSm6QBDSCx8RnRlZe5yk752cAh+x8FTi0/OQayUcnLFO15rchkY1ACk EJj6t5xi+pIL4dFTLQ2pdF/bINGjNgdfLwfqT06w= Date: Mon, 5 Sep 2016 14:03:11 +0300 From: Aleksander Alekseev To: Victor Wagner Cc: pgsql-hackers@postgresql.org Subject: Re: Patch: Implement failover on libpq connect level. Message-ID: <20160905110311.GA11351@e733> References: <56E0AD90.5090409@pgmasters.net> <56E9E978.5090103@pgmasters.net> <20160831111330.53a018ed@fafnir.local.vm> <20160905072215.4efd7b8a@wagner.wagner.home> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20160905072215.4efd7b8a@wagner.wagner.home> User-Agent: Mutt/1.6.1 (2016-04-27) X-Pg-Spam-Score: -3.1 (---) 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 Hello, Victor. > As community shows an interest with this patch, and it has been > included in the current commitfest, I've to submit it. Naturally we show interest in this patch. It's a great feature that would be very nice to have! > This version of patch includes > > 1. Most important - a tap test suite (really small enough and has a > room for improvements). > > 2. Compatibility with older versions - now readonly check is skipped if > only one hostname is specified in the connect string > > 3. pg_host and pg_port variables now show host name and port library > was connected to. > > 4. Your fix with compilation of ecpg tests. > > Patch is attached. After a brief examination I've found following ways to improve the patch. 1) It looks like code is not properly formatted. 2) > + readonly > + > + > + If this parameter is 0 (the default), upon successful connection > + library checks if host is in recovery state, and if it is so, > + tries next host in the connect string. If this parameter is > + non-zero, connection to warm standby nodes are considered > + successful. IIRC the are "warm" and "hot" standby's in PostgreSQL terminology. The difference is that you can't execute any queries on a "warm" standby. So the description is probably wrong. I suggest to fix it to just "... connection to standby nodes are...". 3) > + falover_timeout > + > + > + Maximum time to cycilically retry all the hosts in commect string. > + (as decimal integer number of seconds). If not specified, then > + hosts are tried just once. > + Typos: cycilically, commect 4) > static int > connectDBStart(PGconn *conn) > { > + struct nodeinfo > + { > + char *host; > + char *port; > + }; Not sure if all compilers we support can parse this. I suggest to declare nodinfo structure outside of procedure, just to be safe. 5) > + nodes = calloc(sizeof(struct nodeinfo), 2); > + nodes->host = strdup(conn->pghostaddr); > hint.ai_family = AF_UNSPEC; > + /* Parse comma-separated list of host-port pairs into > + function-local array of records */ > + nodes = malloc(sizeof(struct nodeinfo) * 4); > /* pghostaddr and pghost are NULL, so use Unix domain > * socket */ > - node = NULL; > + nodes = calloc(sizeof(struct nodeinfo), 2); > + sprintf(port,"%d", htons(((struct sockaddr_in > *)ai->ai_addr)->sin_port)); > + return strdup(port); You should check return values of malloc, calloc and strdup procedures, or use safe pg_* equivalents. 6) > + for (p = addrs; p->ai_next != NULL; p = > p->ai_next); > + p->ai_next = this_node_addrs; Looks a bit scary. I suggest to add an empty scope ( {} ) and a comment to make our intention clear here. 7) Code compiles with following warnings (CLang 3.8): > 1 warning generated. > fe-connect.c:5239:39: warning: if statement has empty body > [-Wempty-body] > errorMessage, > false, true)); > ^ > fe-connect.c:5239:39: note: put the semicolon on a separate line to > silence this warning > 1 warning generated. 8) get_next_element procedure implementation is way too smart (read "complicated"). You could probably just store current list length and generate a random number between 0 and length-1. -- Best regards, Aleksander Alekseev -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers