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 1iLT78-00074V-RZ for pgsql-general@arkaria.postgresql.org; Fri, 18 Oct 2019 14:20:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iLT77-0001Us-Ea for pgsql-general@arkaria.postgresql.org; Fri, 18 Oct 2019 14:20:21 +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 1iLT75-0001UH-IZ for pgsql-general@lists.postgresql.org; Fri, 18 Oct 2019 14:20:21 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iLT71-0004GZ-ON for pgsql-general@postgresql.org; Fri, 18 Oct 2019 14:20:17 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 8305122011; Fri, 18 Oct 2019 10:20:14 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Fri, 18 Oct 2019 10:20:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=y LzJGXirmUebFf6kIDrrsKew4rvpygClC3vOhMBkZpA=; b=LvoNlpcQN+RhQ2QgH YTdM1Nd6INRr/owcD1vfXzEroAAAEMnw9s5GAdsYaJ7Xa1B61gFwFNY2y+XgUQyh aU1Yn7l2MAZ2hfsfUPhiTpr+L2XUZzPBvfHyM98SRO8ykrH2rvf14H2YzH5oBQ8E ROlT9dJKiXMel3GchdjaYmpHAq9qcrOgsad8q0Gg0FjSP48U57iq6mJMevbyxDcM VfCYjnYiqg34qe1lunjwGHSIkxFNMBMQ1Ds2XozXoN9GSa2bOsmxEKNjaSxLzciA xEqpB4gt2OGUWFBYFM7rEb0QlFJn1H0PXK3x38K4mnJJ+eDdB/gO3RnaMwyn5TdG iTSsg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=yLzJGXirmUebFf6kIDrrsKew4rvpygClC3vOhMBkZ pA=; b=ZBHrX+WPT3MQfuOdDiryQKMlONEccnHtdVCJZQOOQmdouxfjd8xoaW8qY YqhnPnwdk9uzRczNhrvINYsN0mlxcN7mike8ll1yTB8MapUsX3Gw0UrmPeYgqn1B iy2d51nZ79hVdz3bJw+WZoepqXD0WhQhNcdTI1HxAWn302/QIWJ2lPLtBhqC08Xz N6Kf1HzHp/WdH7TXtANM5ySdqzo8ehtb8TpEBOrmgt2BHvqE5QnmeAp/rMNetBkD 8lDJZ1Rdt5Oc4OlwH4wff4IVdM51Jv70ZJrF/5ljaSKdlnRY4z5R0jyH5PTuM9rO 1S/jvuxAWe7K0EEwFZsb6bpinmOFA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrjeelgdejfecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenucfkphepudejgedrvddu rdduvdekrdegleenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvh gvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.2] (174-21-128-49.tukw.qwest.net [174.21.128.49]) by mail.messagingengine.com (Postfix) with ESMTPA id CA68D8005B; Fri, 18 Oct 2019 10:20:13 -0400 (EDT) Subject: Re: connection timeout with psycopg2 To: Vicente Juan Tomas Monserrat , pgsql-general@postgresql.org References: From: Adrian Klaver Message-ID: <98404060-71d7-f4c6-9059-9d7dd4800a26@aklaver.com> Date: Fri, 18 Oct 2019 07:20:12 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 10/16/19 2:29 AM, Vicente Juan Tomas Monserrat wrote: > Hi there, > > I have been testing out the following architecture for PostgreSQL HA. > > |+---------+ +-----+ VIP +----+ | +---------+ | | | +------v-------+ > +------v-------+ | pgBouncer | | pgBouncer | | + | | + | | keepalived | > | keepalived | +------+-------+ +------+-------+ | | | | | | > +------v-------+ +------v-------+ | | | | | HAProxy | | HAProxy | | | | > | +------+-------+ +------+-------+ | | +--------------------+ | | | | > +----v----+ +----v----+ | | | | | | | | | PG01 | | PG02 | | | | | > |(patroni)| |(patroni)| | | | | +---------+ +---------+ | > > I'm using this python script for checking the failover events in > pgBouncer, HAProxy and Patroni (PostgreSQL HA solution). > > |#! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 import > psycopg2 ISOLEVEL = psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT > import time from datetime import datetime user = 'postgres' password = > 'secretpassword' host = 'localhost' port = '6432' database = 'test' > LIMIT_RETRIES = 10 class DB(): def __init__(self, user, password, host, > port, database, reconnect): self.user = user self.password = password > self.host = host self.port = port self.database = database > self._connection = None self._cursor = None self.reconnect = reconnect > self.init() def connect(self,retry_counter=0): if not self._connection: > try: self._connection = psycopg2.connect(user = self.user, password = > self.password, host = self.host, port = self.port, database = > self.database, connect_timeout = 3) retry_counter = 0 > self._connection.autocommit = True return self._connection except > psycopg2.OperationalError as error: if not self.reconnect or > retry_counter >= LIMIT_RETRIES: raise error else: retry_counter += 1 > print("got error {}. reconnecting {}".format(str(error).strip(), > retry_counter)) time.sleep(5) self.connect(retry_counter) except > (Exception, psycopg2.Error) as error: raise error def cursor(self): if > not self._cursor or self._cursor.closed: if not self._connection: > self.connect() self._cursor = self._connection.cursor() return > self._cursor def execute(self, query, retry_counter=0): try: > self._cursor.execute(query) retry_counter = 0 except > (psycopg2.DatabaseError, psycopg2.OperationalError) as error: if > retry_counter >= LIMIT_RETRIES: raise error else: retry_counter += 1 > print("got error {}. retrying {}".format(str(error).strip(), > retry_counter)) time.sleep(1) self.reset() self.execute(query, > retry_counter) except (Exception, psycopg2.Error) as error: raise error > def reset(self): self.close() self.connect() self.cursor() def > close(self): if self._connection: if self._cursor: self._cursor.close() > self._connection.close() print("PostgreSQL connection is closed") > self._connection = None self._cursor = None def init(self): > self.connect() self.cursor() db = DB(user=user, password=password, > host=host, port=port, database=database, reconnect=True) > db.execute("create table if not exists t1 (id integer);") i = 0 while > True: db.execute("insert into t1(id) values(1);") if i % 100 == 0: > print("%s: %d" % (datetime.now(), i)) i = i+1 | > > When running this python script against the pgBouncer VIP it keeps > inserting data into the database. Then I stop one of the HAProxy > service (where the VIP lives) the connection it hangs and never goes on. > The VIP is on the other node but the client/app it doesn't notice and it > keeps waiting for 5 minutes and finally continues. I've been looking for > some default value of 5min with no luck. Observations and comments: 1) I would point out there is a Psycopg list: https://www.postgresql.org/list/psycopg/ 2) I am not sure where the 5 minutes comes in. I see LIMIT_RETRIES = 10 and a sleep of 5 sec between retries. 3) Where did this: "got error server conn crashed? " come from? I don't see that in the code. > > |$ python insert.py 2019-10-15 10:01:51.817585: 0 2019-10-15 > 10:01:51.901091: 100 2019-10-15 10:01:52.031583: 200 2019-10-15 > 10:01:52.126565: 300 2019-10-15 10:01:52.216502: 400 2019-10-15 > 10:01:52.307157: 500 2019-10-15 10:01:52.400867: 600 2019-10-15 > 10:01:52.497239: 700 2019-10-15 10:01:52.655689: 800 2019-10-15 > 10:01:52.777883: 900 got error server conn crashed? <<<<<<<<<<<<<<<<< > HAProxy stopped manually to force the VIP to move to the other node > server closed the connection unexpectedly This probably means the server > terminated abnormally before or while processing the request.. retrying > 1 PostgreSQL connection is closed ^C^C^C^C^C <<<<<<<<<<<<<<<<<< The > connection gets stuck (kill PID) | > > I've tried exactly the same code logic in Java (using PostgreSQL JDBC) > and dotnet core (using Npgsql) works fine with specifying this > parameters socketTimeout (Java) and 'Command Timeout' (dotnet) respectively. > > |$ dotnet run connection initialized 2019-10-15T08:27:28.843 0 > 2019-10-15T08:27:30.205 100 2019-10-15T08:27:31.566 200 got error: > Exception while reading from stream. Retrying 1 connection closed > connection initialized connection reset 2019-10-15T08:27:42.076 300 > <<<<<<<<<<<< the app notices something wrong with the existing > connection and does a reset 2019-10-15T08:27:43.461 400 > 2019-10-15T08:27:44.843 500 2019-10-15T08:27:46.244 600 > 2019-10-15T08:27:47.637 700 2019-10-15T08:27:49.031 800 ^C | > > In python and using psycopg2 (v2.8.3) I've not been able to inform in > any way the application to reset the connection, retry and keep going on. > > As the psycopg2 it's a wrapper for libpq, I've seen that in libpq (for > postgresql 12) there is a new option called |tcp_user_timeout|. Note > this options doesn't exist in previous versions of libpq. > So I've tried to install psycopg2 (instead of psycopg2-binary) with the > libpq (v12) installed, so the psycopg2 is build against libpq (v12). > Even so, it's not working as expected. > > Environment: > Ubuntu 16.04 LTS > PostgreSQL 9.6.15 > Patroni 1.6.0 > pgBouncer 1.11.0 > keepalived 2.0.18 > HAProxy 1.6.3 > > I've also tried to catch different types of exceptions with no luck. > > I would appreciate any guidance on this matter. I can give you more info > if needed. > > Thank! > -- Adrian Klaver adrian.klaver@aklaver.com