Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mI67F-0008Gz-M3 for pgsql-general@arkaria.postgresql.org; Mon, 23 Aug 2021 09:19:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mI67E-00019v-Lr for pgsql-general@arkaria.postgresql.org; Mon, 23 Aug 2021 09:19:36 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mI67E-00019m-9g for pgsql-general@lists.postgresql.org; Mon, 23 Aug 2021 09:19:36 +0000 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mI67A-0000Zu-1b for pgsql-general@lists.postgresql.org; Mon, 23 Aug 2021 09:19:35 +0000 Received: by mail-wm1-x332.google.com with SMTP id c8-20020a7bc008000000b002e6e462e95fso13544051wmb.2 for ; Mon, 23 Aug 2021 02:19:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=drzmY5Yt1ev6cjMrIT4B9bLKuIUy0kAxICNcvxg/IFY=; b=xLAfGSwdoXLXzgRwxXm9sbkhLXX5QOrvpZDrqBPznTMEBrK6VMxioz/RBCXJxvctri gR4NkkqbGUqQ6UlAI9od7WyzAg4jQarNc8iKCymUkivg7F8j671b6M4GekwXiFXnW0MA fWW40rO9IWN2TMdr/jZLzNZEP8ezcSxoOo2zytjSweZq35BUWq/G1v4abSbisNXqDDdI 2+E/ogUp5Pkj+KH6tczYg2yVdPtdgYO0FjMA2aU+DXTgnHSDVFKwpc/luAbwAwSFmyqe t0H9I5ruI7MOjaUH2MFLdY1XvxHiwYzM5p6QTxWuXA72EMIFP/tYgpRMDztVG5RKUK7f /WYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=drzmY5Yt1ev6cjMrIT4B9bLKuIUy0kAxICNcvxg/IFY=; b=NuOpYgBk5yGPjdIb06zh649gUUNZeZfEXRTaW4OuieLv3vgJjYcY0TxnEvOFPjwRrb lDersXP4hmAdlFFqwMIRR/GV58aCOoJBleFR1H07GqK+Z21MYL1fJniBklyORncw877y mmS73VGJkV1mfEu4LJopCAjCmVenJxNqLnor/ipFhdWJZFeiKr8jh/71eLxRjdrPZTxy f6U1STRAajfjz8yoJPUzzw7z4MuwvxioDXSx0ipAo0DbYylQYH4GtPiWpl0nbcPEkRsI vV1v4ZgDXCRTQoX+KgiOmCajVffjTare4dlMHoOBsbLRCCVrDKVCiQthZfCGqMDpJUtm 0QQQ== X-Gm-Message-State: AOAM533atIdVmiEB3pUclGB+X4eiP8D/IS0k5VMGzl8vXl4TooQTJa0R LXTJJEMcKnIkRMNPx41pjyu3tg== X-Google-Smtp-Source: ABdhPJz/KmefTUAMs+EWcKI7eVfcC2hxyZurXOrn6H91As/XKRxHZSARC6sLJn1in8648lT+4auPgw== X-Received: by 2002:a05:600c:4ba3:: with SMTP id e35mr7011402wmp.130.1629710369537; Mon, 23 Aug 2021 02:19:29 -0700 (PDT) Received: from localhost.localdomain (dynamic-e2xp7vm4pemrfei63-pd01.res.v6.highway.a1.net. [2001:871:5c:b3d2:30e6:5196:7775:ee7b]) by smtp.gmail.com with ESMTPSA id y6sm3735967wrm.54.2021.08.23.02.19.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Aug 2021 02:19:29 -0700 (PDT) Message-ID: Subject: Re: Connecton timeout issues and JDBC From: Laurenz Albe To: Kelvin Lau , pgsql-general@lists.postgresql.org Date: Mon, 23 Aug 2021 11:19:28 +0200 In-Reply-To: <20d676e7-a2d4-da48-891e-0a1dd4e9048d@hku.hk> References: <20d676e7-a2d4-da48-891e-0a1dd4e9048d@hku.hk> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.40.4 (3.40.4-1.fc34) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 2021-08-23 at 15:34 +0800, Kelvin Lau wrote: > I have been using Python to deal with CRUD of the database. I have discovered that there are > some issues when dealing with long queries (either SELECT or COPY, since it is somewhat big data). The > connection is dropped by the 2~3 hours mark and I have no idea what is wrong. There is no knowledge > on how my workstation is connected to the server.  That sounds like a problem in your network; probably some ill-configured firewall or router that drops idle connections.   > But I managed to work around the issue by putting a few parameters in psycopg2: >   > > conn = psycopg2.connect(host=“someserver.hk”, > > port=12345, > > dbname=“ohdsi”, > > user=“admin”, > > password=“admin1”, > > options="-c search_path="+schema, > > # it seems the below lines are needed to keep the connection alive. > > connect_timeout=10, > > keepalives=1, > > keepalives_idle=5, > > keepalives_interval=2, > > keepalives_count=5) > It looks like that few keepalives* parameter kept the connection alive so the long queries can run day and night. > > The problem now is that, I am forced to use R and JDBC to deal with a bunch of codes, because there > are a lot of analyses written in R. The issue that a long query would be dropped around the 2~3 hours > mark showed up again in R/JDBC. How can I work around that? >   > I have tried putting tcpKeepAlive=true in the link but it seems to have mixed results. > Do I also have to put tcp_keepalives_interval or tcp_keepalives_count? What are some recommend > values in these parameters? You can set "tcp_keepalives_idle" on the database server, then the setting is independent of the client used. I would say that a setting of 5 seconds is way too low. Set it to 600 or so, that would be 10 minutes. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com