Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qMy93-003jOc-Vu for pgsql-jdbc@arkaria.postgresql.org; Fri, 21 Jul 2023 21:58:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qMy91-0096az-HX for pgsql-jdbc@arkaria.postgresql.org; Fri, 21 Jul 2023 21:58:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qMy91-0096ar-9m for pgsql-jdbc@lists.postgresql.org; Fri, 21 Jul 2023 21:58:39 +0000 Received: from pgintl.fastcrypt.com ([149.56.129.164]) by magus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qMy8s-0010PW-Kw for pgsql-jdbc@lists.postgresql.org; Fri, 21 Jul 2023 21:58:38 +0000 Received: from mail-lf1-f44.google.com (mail-lf1-f44.google.com [209.85.167.44]) by pgintl.fastcrypt.com (Postfix) with ESMTPSA id D48062072B for ; Fri, 21 Jul 2023 17:58:28 -0400 (EDT) Received: by mail-lf1-f44.google.com with SMTP id 2adb3069b0e04-4fb73ba3b5dso3946345e87.1 for ; Fri, 21 Jul 2023 14:58:28 -0700 (PDT) X-Gm-Message-State: ABy/qLY6huDFQb6uHZ3uJra24fv+7MWEauPy2MnGVwxoIwV7nZGEUfxX OEDLJznO0LzGULilhmO9gwlT1f7vd1LD/wVUKeI= X-Google-Smtp-Source: APBJJlHZEs8XPDqGiWFmcxmUJ8DEkpO7w6wPh8eN+/l4fRRp4X6TrtM8g92nTTYf7hpodJsZywh1FypxfyVHf6xfStE= X-Received: by 2002:a19:7110:0:b0:4f8:7551:7485 with SMTP id m16-20020a197110000000b004f875517485mr2022850lfc.5.1689976707409; Fri, 21 Jul 2023 14:58:27 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Dave Cramer Date: Fri, 21 Jul 2023 15:58:10 -0600 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: Executing the same query multiple times gets slow To: Blake McBride Cc: pgsql-jdbc@lists.postgresql.org, Tom Lane Content-Type: multipart/alternative; boundary="00000000000052ebf10601065dfe" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000052ebf10601065dfe Content-Type: text/plain; charset="UTF-8" This is somewhat of a known issue, although it should not get this bad. After 5 iterations of the same query the driver will switch to a named statement. However this isn't the reason you are having problems. After 5 iterations of the same named prepared statement the backend will switch to a generic plan, which apparently is much slower. In newer versions of Postgres you can force it not to see PostgreSQL: Documentation: 15: 20.7. Query Planning So I think if you set plan_cache_mode to force_custom_mode you should be fine +Tom just for visibility. Dave Cramer www.postgres.rocks On Thu, 20 Jul 2023 at 10:33, Blake McBride wrote: > Greetings, > > I have a complex query that returns about 5,000 records and only a few > columns. If I run it in psql repeatedly, it's always fast. If I run it > through JDBC it runs fast at first but then it gets real slow (> 50 > seconds). I can't understand why it would get slow after 30 runs of the > exact same query. > > I am running: > > Linux / 64GB RAM > PostgreSQL 15.1 > postgresql-42.5.4.jar > > I am using a prepared statement but a new one each time even though it is > the same query (there are reasons for this). > > I found the line that's having the delay is: pstat.executeQuery(); > > where: PreparedStatement pstat; > > Sure appreciate any pointers! > > Thanks. > > Blake > > --00000000000052ebf10601065dfe Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
This is somewhat of a kn= own=C2=A0issue, although it should not get this bad.

Aft= er 5 iterations of the same query the driver will switch to a named stateme= nt. However this isn't the reason you are having problems. After 5 iter= ations of the same named=C2=A0prepared statement=C2=A0 the backend will swi= tch to a generic plan, which apparently is much slower. In newer versions o= f Postgres you can force it not to see=C2=A0PostgreS= QL: Documentation: 15: 20.7.=C2=A0Query Planning=C2=A0So I think if you= set=C2=A0plan_cache_mode to force= _custom_mode you should be fine

+Tom just f= or visibility.

Dave Cramer
www.postgres.rocks


=
On Thu, 20 Jul 2023 at 10:33, Blake M= cBride <blake1024@gmail.com&g= t; wrote:
Greetings,

I have a complex query that return= s about 5,000 records and only a few columns.=C2=A0 If I run it in psql rep= eatedly, it's always fast.=C2=A0 If I run it through=C2=A0JDBC it runs = fast at first but then it gets real slow (> 50 seconds).=C2=A0 I can'= ;t understand why it would get slow after 30 runs of the exact same query.<= /div>

I am running:

Linux / 64G= B RAM
PostgreSQL 15.1
postgresql-42.5.4.jar

I am using a prepared statement but a new one each time e= ven though it is the same query (there are reasons=C2=A0for this).

I found the line that's having the delay is:=C2=A0 pst= at.executeQuery();

where:=C2=A0 PreparedStatement = pstat;

Sure appreciate any pointers!
Thanks.

Blake

--00000000000052ebf10601065dfe--