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 1qNoVQ-00620k-Pw for pgsql-jdbc@arkaria.postgresql.org; Mon, 24 Jul 2023 05:53:16 +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 1qNoVO-00809z-AK for pgsql-jdbc@arkaria.postgresql.org; Mon, 24 Jul 2023 05:53:14 +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 1qNoVO-00809q-2t for pgsql-jdbc@lists.postgresql.org; Mon, 24 Jul 2023 05:53:14 +0000 Received: from mail-wm1-x331.google.com ([2a00:1450:4864:20::331]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qNoVK-001SFa-H2 for pgsql-jdbc@lists.postgresql.org; Mon, 24 Jul 2023 05:53:13 +0000 Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-3fbc5d5742bso37950775e9.2 for ; Sun, 23 Jul 2023 22:53:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20221208.gappssmtp.com; s=20221208; t=1690177989; x=1690782789; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=v1bIdFG37RDMkSUTV9lEd7TRQ6l4dmdzjsPBQN9VSSM=; b=mSLsucHmShjFrhh8wMg/vs+CQMZB6yNagC0lO41/TEPVXljxQAeVFbPN/HyUPAyupj Bc/ykHKSSZQ7wM1zBqOmzQun1n4Ocl/j8HWHLgLc0S7nAbD6BmgF63dloBkAS1rpvloe v4YtFK46FFFbzwj7FUn5lzLzG2KE7D67sFtSdRA46PbGET/7pPFQPvjaml3n18TKFUWv sbo8EWu7qlhq+x+a/hyKeJXbJytp0i/yTxu8Ljn1FvbRU56MMjq56rlxxh3d4Abz7g0d O5Snb3tvkJ56VaxnxdHdv8S0d+BbAE9fSw4KyxTCG+v63uiFNW9RzQGh6y+DF7LBgQRy 7fhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690177989; x=1690782789; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=v1bIdFG37RDMkSUTV9lEd7TRQ6l4dmdzjsPBQN9VSSM=; b=h7R7LkAFQ9O4+udmEj1VRGR85qoJ4ZjFpsL/fouhWCBPYTWX4hubtq7AwNLF7pEpVC 48vPpkYALqRAIgxTp2M7QQI9/PsXk3g+fJl2PehESY1jSpxa4+WJbzXPTpZB39aMCrg5 lzN6AErS0JUCudCKgCS+6G/yUthJ0EVNn1W6mdOm/6+urxSArTVkaJvLuCDqqUKkieYW 9YyAyesTAcNRgp2EEtne8WANmGFoN4bpGB68siOnSQXIRpK2dVMtC4mJ6m5cXNJdT0RN PFCdmjCfH4UFXzxhw+Aqs3/zB7IrKbuOoAOaPQiN7UPtgvswUBhTaN0chRMdwqfoDfS3 wkYw== X-Gm-Message-State: ABy/qLax0/YYzAMQQrBs0BMVFZlF1LZjVQxrjt1mAxn8ePa25CbuxCQM s7epdaHo+lFsRM5SoCgyE4AH8Q== X-Google-Smtp-Source: APBJJlEEFa7zMPee5hWY8wgiwkrpppCROca3//mDEeMxpFSI/UBNRxMf9Al+IQyYIRXYfvO05ATUqQ== X-Received: by 2002:a7b:c40e:0:b0:3fa:973e:2995 with SMTP id k14-20020a7bc40e000000b003fa973e2995mr6465797wmi.12.1690177989233; Sun, 23 Jul 2023 22:53:09 -0700 (PDT) Received: from localhost.localdomain ([41.66.96.65]) by smtp.gmail.com with ESMTPSA id n11-20020a7bcbcb000000b003fba92fad35sm11883452wmi.26.2023.07.23.22.53.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Jul 2023 22:53:08 -0700 (PDT) Message-ID: <1b139ffdf4bf5d8d131483f9bfe3568dfc5bac03.camel@cybertec.at> Subject: Re: Executing the same query multiple times gets slow From: Laurenz Albe To: Blake McBride , Dave Cramer Cc: pgsql-jdbc@lists.postgresql.org Date: Mon, 24 Jul 2023 07:53:07 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.46.4 (3.46.4-1.fc37) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sat, 2023-07-22 at 10:25 -0500, Blake McBride wrote: > I have narrowed the problem down quite a bit.=C2=A0 Here is what I found: >=20 > 1.=C2=A0 My original select was in error.=C2=A0 I fixed it.=C2=A0 Same pr= oblem. >=20 > 2.=C2=A0 If I do not use a prepared statement, it is fast all of the time= .=C2=A0 If I use a prepared > statement it runs fast most of the time and then after repeated=C2=A0= use starts getting > really slow as I previously reported. >=20 > For me, in this instance, the solution is to not use a prepared statement= . >=20 > In my opinion, this is a major=C2=A0flaw in either PostgreSQL or the JDBC= driver. > My application has 10,000 Java classes and uses prepared statements every= where.=C2=A0=C2=A0 >=20 > My solution in this instance will not work generally because there is too= much > code to test and adjust.=C2=A0 It is unreasonable for prepared statements= to work this > significantly slower. >=20 > In my opinion, this is a huge problem and should be top priority. There is an alternative to blindly disabling PostgreSQL's use of a generic = plan: you could examine the generic plan and try to improve it. In PostgreSQL v16, you can use EXPLAIN (GENERIC_PLAN) SELECT /* query with $1, $2 etc. as parameters */ to get the generic plan. With older versions, you can (using the appropria= te number and type for the parameters) PREPARE stmt(integer,text) AS SELECT /* query with $1, $2 etc. as paramet= ers */ EXPLAIN (ANALYZE, BUFFERS) stmt(1, 'something'); Repeat the EXECUTE at least 5 times, until PostgreSQL switches to the gener= ic plan. Then you can analyze why the generic plan is so slow. It is often possible= to improve PostgreSQL's estimates to either make it pick a better generic plan= or to make it *not* switch to a generic plan by itself. Ask for help if you c= annot do that alone. That way, you don't have to disable generic plans. Yours, Laurenz Albe