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 1nCcaB-0003PG-51 for pgsql-performance@arkaria.postgresql.org; Wed, 26 Jan 2022 07:19:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nCca9-00061R-0g for pgsql-performance@arkaria.postgresql.org; Wed, 26 Jan 2022 07:19:05 +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 1nCca8-00061I-MR for pgsql-performance@lists.postgresql.org; Wed, 26 Jan 2022 07:19:04 +0000 Received: from mout.gmx.net ([212.227.15.15]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nCca6-0004eX-4c for pgsql-performance@postgresql.org; Wed, 26 Jan 2022 07:19:04 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1643181540; bh=dpzfc4BbvjpofMF2sSVsvd7uI8YHCMrSRHtJVyoNpZo=; h=X-UI-Sender-Class:To:From:Subject:Date; b=ddibZF5bSUgryxxt/0Xx/8tDIhHCUafXLYB5cMfqA21Ro+AqVOfyVodLsJhBucbc9 Ve19fARHF2smdpgZvoy5TBpv1JBDEwdbCCCjezDF0K8pjVJeP9mRkoi94OztKmvbZh rQM3BkkusS8bJJah0hK1S4aftLSjDhlcy3bGkUbc= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [10.1.34.201] ([185.40.248.10]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1N3KTo-1mDKsb41Ok-010M3r for ; Wed, 26 Jan 2022 08:19:00 +0100 To: pgsql-performance@postgresql.org From: Thomas Kellerer Subject: Query runs slower as prepared statement - identical execution plans Message-ID: <919dfbb7-8704-d93d-07c1-51446b3c07bf@gmx.net> Date: Wed, 26 Jan 2022 08:18:59 +0100 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:3AYhDQYlLSbueDQw862c7tWa8HqHEJz2uULti44df4qdz07suA7 kg0dWnJ7ColeR1+SQdikdQFAu61Yk2u1illPzvGYuxUGKz7X0MN0Z/t/5Ae3JUYAg6W68AX OOYrTFxdMKd1rHemV8PmB5Owozl/iNe+L71wplBTsdgGYKmfHSdbzECwa0LCvQDZMqnZjZV SXCYpfgrXhxSXdxpicWuQ== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:DlNdlWbVOoY=:2A0xdOznG7jzr8jlCP5gUv gRKv//w1rlil6LeVG/Fk631BrOVxR1VVaW9XDTufdfinyKMQoU1OZm6oruyKO61ZcUDwlOK1+ Ji4xdw8OWQQxNrznZjtOQdnK6uq24UhtNqroV8Gvujo7csEGrXCxzS1aFkH15eh+3+5siM1Vs efuQoyM5ePYSTCu57BaS/HHRmGZIc6epHVe6uFUKtfRJq4oixvTDyjFpIDrQwhdssoOTScp45 epnu6KxDRhxzZXnIXLi+TP7WOCC4cMbmFdXEXM7WXVp0q6Fe/b9dhYaId4c1nxmCpxuVvzK9E 5MuCnHfTuBu/BeSzVNbssqjIpTRBfjUaN+XMy1fKggmR+FHd4coodBNJPKhHPULjaqYzfbgUR VVHOuu5D47mOHbMc14LAK8cg9+OzuFDKKx2jh3H2BLaR2Z0Vda2uULLGK6E0elSJZPmAr36KD fMmSm5ti33Ly7oN8fEiXhxQTFikHYdQVZ96Zc0vDZ/IqJUq9q+4t2dcv+jqNJhkVm34hzpaly OVKmBdqcPAqTxFhoMLzSyk8/TnpXrKmlUECaWTZX+rJFj25xMJUWTGZ4cDN5a/SjIzFO2mDQV C9hOyxi4MOfExa6X7cJ68V3kPQdMLLQzJj/8ouBeTlg5EeX31aDCop5WoMwBVkTDmxjbT/gXG CjoeclkhyONlCsJi+Gf+z5Bxc5E2MltfWAf2ZKQgrv53KHY+41dAm8coSTcUVSob0vkSGlnSv hSTgu7zhg+r3o2mlEx5FHHmf8ZdaGHo7BtnYPc/0tPHlARVYv/Bm3LBY1Ih0Hewtsv59HajJo rmuSIebzh/LzELUVlLXoJz2mxMMqwev0x29OJu/6Nd7FlpNGO/BRSH+3z4+Nbe+8Z6MloQnxQ Hbc2luXrL5juD9eWNp5flLsDiEI7xPEmHwdwXfyRs9lZzDvbC13xsd31MWb4fKc72Drg3VCXu g5CjKNROe1zexy1lvp7aJSg4ZsmOWQb+n7F9z6poxqMt33yZXXSHE4yWN5oSPdcKPvN+9RhfK jun3eFzhpAIZooQzC1WBXzjavCVTZW41KlUWXgri/+hPWJH6FkUYdzDdOBNAu8XlTrTjWA2V+ QP8WUXr5a6sD9U= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello, I have a strange case of a query that runs substantially slower when run a= s a Java PreparedStatement with placeholders, compared to using constant value= s in the SQL string. In my experience, the reason for this is usually a different execution pla= n for the prepared statement. However in this case, the plans are identical but the prepared statements = runs substantially slower than the "non-prepared" plan: 1800ms to 2000ms vs. 250ms to 350ms I can't disclose the query, but the basic structure is this: select ... from some_table where jsonb_column #>> $1 =3D ANY ($2) and some_uuid_column =3D ANY (.....) For various reasons the list of values for the some_uuid_column =3D ANY(..= ) condition is always passed as constant values. The plan is quite reasonable using a Bitmap Heap Scan in both cases on "so= me_uuid_column" I uploaded the (anonymized) plans to explain.depesz: Fast execution: https://explain.depesz.com/s/QyFR Slow execution: https://explain.depesz.com/s/mcQz The "prepared" plan was created using psql, not through JDBC: PREPARE p1(text,text) AS ... EXPLAIN (analyze, buffers, timing, verbose) EXECUTE p1 ('{...}', '{....}') But the runtime is pretty much what I see when doing this through Java. My question is: why is processing the query through a prepared statement s= o much slower? This happens on a test system running Postgres 13.2 on CentOS, and another= test system running 13.5 on Ubuntu. For the time being, we can switch off the use of a PreparedStatement, but = I'm also interesting to know the underlying root cause. Any ideas?