Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i7NKZ-0008Ao-FA for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Sep 2019 17:20:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i7NKY-0003U4-AT for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Sep 2019 17:19:58 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i7NKX-00033y-7E for pgsql-hackers@lists.postgresql.org; Mon, 09 Sep 2019 17:19:57 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1i7NKU-0003wi-0p for pgsql-hackers@postgresql.org; Mon, 09 Sep 2019 17:19:56 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id A0D5C221DF; Mon, 9 Sep 2019 13:19:52 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Mon, 09 Sep 2019 13:19:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; bh=0vra3lXrJgM4X+EEsqBuHvLCFhtSP6I24WJ6NKq2qkQ=; b=nEsLizyi /aLCJhzSKw84kAoGuxlJKFUVGqNfahywFR2jYO9ZnN3X/xZtfMam5QU80DoLix91 F0MY3W+uPVdTLwPQ/WEMihDcG/eWHahmPeBgy1L3cHbb/JlIdKOeGmDa0z9gXwhp N98xh8FJtR1mXHgc5bv3pJSJbFqCIH8jKl1lG2Mhr43ZyN7tEFo7na2Gi3D3CduO 1er5Y5FU6ut4/NQR4Wl1H5+QH/fHqcqEIreL1PouzrkO8ekaqxCgnNpl5X2MylLn IPFF8LuAxlC/lKiCQ+qMbejr1S/ztxQBGRUMgGcE0nPrqx6KpO+M5x4iig82y66R vXBbfDWIFxIDGg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrudekiedguddutdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvuffkgggtugfgjggfsehmkeerredtredunecuhfhrohhmpeetlhhv rghrohcujfgvrhhrvghrrgcufhhrohhmucdvnhgusfhurggurhgrnhhtuceorghlvhhhvg hrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgheqnecuffhomhgrihhnpedvnhguqhhurggu rhgrnhhtrdgtohhmnecukfhppeduledtrdduvddurddvledrfeenucfrrghrrghmpehmrg hilhhfrhhomheprghlvhhhvghrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrghenucevlhhu shhtvghrufhiiigvpedt X-ME-Proxy: Received: from nimloth.alvh.no-ip.org (unknown [190.121.29.3]) by mail.messagingengine.com (Postfix) with ESMTPA id BC6C980061; Mon, 9 Sep 2019 13:19:51 -0400 (EDT) Received: by nimloth.alvh.no-ip.org (Postfix, from userid 1000) id 01FCF123A90; Mon, 9 Sep 2019 14:19:49 -0300 (-03) Date: Mon, 9 Sep 2019 14:19:49 -0300 From: Alvaro Herrera from 2ndQuadrant To: Haribabu Kommi Cc: "Tsunakawa, Takayuki" , Robert Haas , Laurenz Albe , Tom Lane , Dave Cramer , Jing Wang , PostgreSQL-development Subject: Re: Libpq support to connect to standby server as priority Message-ID: <20190909171949.GA31491@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="SUOF0GtieIMvvwua" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --SUOF0GtieIMvvwua Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Question about 0001. In the CONNECTION_SETENV state, you end by falling through to the CONNECTION_CHECK_TARGET case; but in that switch it seems a bit unnatural to do that. I think doing "goto keep_trying" is another way of doing the same thing, but more in line with what every other piece of code does. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --SUOF0GtieIMvvwua Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Restructure-the-code-to-remove-duplicate-code.patch" From 30fbb4a913697fe35a195dd41ddd5bcfeec53c0e Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Fri, 6 Sep 2019 16:26:01 -0400 Subject: [PATCH] Restructure the code to remove duplicate code The duplicate code logic of checking for the server version before issuing the SHOW transaction_read_only to find out whether the server is read-write or not is restructured under a new connection status, so that duplicate code is removed. This is required for the next set of patches Author: Hari Babu Kommi Discussion: https://postgr.es/m/CAJrrPGe_qgdbbN+yBgEVpd+YLHXXjTruzk6RmTMhqrFig+32ag@mail.gmail.com --- src/interfaces/libpq/fe-connect.c | 87 ++++++++++++------------------- src/interfaces/libpq/libpq-fe.h | 3 +- 2 files changed, 34 insertions(+), 56 deletions(-) diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c index f03d43376c..91316709a5 100644 --- a/src/interfaces/libpq/fe-connect.c +++ b/src/interfaces/libpq/fe-connect.c @@ -3434,6 +3434,13 @@ keep_going: /* We will come back to here until there is return PGRES_POLLING_WRITING; } + /* Almost there now ... */ + conn->status = CONNECTION_CHECK_TARGET; + goto keep_going; + } + + case CONNECTION_CHECK_TARGET: + { /* * If a read-write connection is required, see if we have one. * @@ -3476,67 +3483,37 @@ keep_going: /* We will come back to here until there is } case CONNECTION_SETENV: - - /* - * Do post-connection housekeeping (only needed in protocol 2.0). - * - * We pretend that the connection is OK for the duration of these - * queries. - */ - conn->status = CONNECTION_OK; - - switch (pqSetenvPoll(conn)) { - case PGRES_POLLING_OK: /* Success */ - break; - - case PGRES_POLLING_READING: /* Still going */ - conn->status = CONNECTION_SETENV; - return PGRES_POLLING_READING; - - case PGRES_POLLING_WRITING: /* Still going */ - conn->status = CONNECTION_SETENV; - return PGRES_POLLING_WRITING; - - default: - goto error_return; - } - - /* - * If a read-write connection is required, see if we have one. - * (This should match the stanza in the CONNECTION_AUTH_OK case - * above.) - * - * Servers before 7.4 lack the transaction_read_only GUC, but by - * the same token they don't have any read-only mode, so we may - * just skip the test in that case. - */ - if (conn->sversion >= 70400 && - conn->target_session_attrs != NULL && - strcmp(conn->target_session_attrs, "read-write") == 0) - { - if (!saveErrorMessage(conn, &savedMessage)) - goto error_return; - + /* + * Do post-connection housekeeping (only needed in protocol 2.0). + * + * We pretend that the connection is OK for the duration of these + * queries. + */ conn->status = CONNECTION_OK; - if (!PQsendQuery(conn, - "SHOW transaction_read_only")) + + switch (pqSetenvPoll(conn)) { - restoreErrorMessage(conn, &savedMessage); - goto error_return; + case PGRES_POLLING_OK: /* Success */ + break; + + case PGRES_POLLING_READING: /* Still going */ + conn->status = CONNECTION_SETENV; + return PGRES_POLLING_READING; + + case PGRES_POLLING_WRITING: /* Still going */ + conn->status = CONNECTION_SETENV; + return PGRES_POLLING_WRITING; + + default: + goto error_return; } - conn->status = CONNECTION_CHECK_WRITABLE; - restoreErrorMessage(conn, &savedMessage); - return PGRES_POLLING_READING; + + /* Almost there now ... */ + conn->status = CONNECTION_CHECK_TARGET; + goto keep_going; } - /* We can release the address list now. */ - release_conn_addrinfo(conn); - - /* We are open for business! */ - conn->status = CONNECTION_OK; - return PGRES_POLLING_OK; - case CONNECTION_CONSUME: { conn->status = CONNECTION_OK; diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index 22c4954f2b..5f65db30e4 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -67,7 +67,8 @@ typedef enum * connection. */ CONNECTION_CONSUME, /* Wait for any pending message and consume * them. */ - CONNECTION_GSS_STARTUP /* Negotiating GSSAPI. */ + CONNECTION_GSS_STARTUP, /* Negotiating GSSAPI. */ + CONNECTION_CHECK_TARGET /* Check if we have a proper target connection */ } ConnStatusType; typedef enum -- 2.17.1 --SUOF0GtieIMvvwua--