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 1oebTU-0004Is-9S for pgsql-general@arkaria.postgresql.org; Sat, 01 Oct 2022 12:20:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oebTS-0008UM-JD for pgsql-general@arkaria.postgresql.org; Sat, 01 Oct 2022 12:20:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oebTS-0008U3-8I for pgsql-general@lists.postgresql.org; Sat, 01 Oct 2022 12:20:06 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oebTO-0007DW-As for pgsql-general@lists.postgresql.org; Sat, 01 Oct 2022 12:20:05 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id ADC507D36; Sat, 1 Oct 2022 14:20:00 +0200 (CEST) Date: Sat, 1 Oct 2022 14:20:00 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Example code Re: Singleton SELECT inside cursor loop Message-ID: <20221001122000.jq7f5wnmzbewimpn@hjp.at> Mail-Followup-To: pgsql-general@lists.postgresql.org References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="ghytsfuuzummzdnm" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --ghytsfuuzummzdnm Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2022-10-01 02:22:22 -0500, Ron wrote: > I extracted the singleton into a function, and that helped me isolate the > problem. I don't know what a singleton select is (duckduckgo tells me it has something to do with DB2 and/or COBOL, or maybe it's a whisky), but: > Note how quickly it runs the first five times, but takes 780x longer the > sixth time I run it.=A0 Exiting psql and entering again causes the same > slowness the sixth time it's run. PostgresSQL evaluates the exact plan the first 5 times it encounters the same prepared query. After that it switches to a generic plan if it thinks that the generic plan isn't worse than the specialized plans. If the plan suddenly gets worse after 5 executions, you've probably run into a case where the generic plan is worse although the cost computed by the planner isn't. hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --ghytsfuuzummzdnm Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmM4MGwACgkQ8g5IURL+ KF25IQ//dXhuPJ3t4B9hQo61Y0/+NfOTiO1u78g/P205ddh8GBzhG+IonemKSxLg yZ2qr84VS+kQdfLm3upexSVj6RlXcFWdcgUQwINoyTLZTYgoOIb6yY0SGgSuNLHv KjlQ/+yrO8XaCj9S/P4FKLHkNQxtirr/gC61L75aPvI/+Rk8KvcxPSESSBihNZ24 QwoXts8UaAoft4FmifTijyeGTxPJjtS2NaXysM9dNKbeg7N50//qy31yo/HHhIcN YCQdLnnxVycc7Jp7mpjbwfuFpKB+ZMEVNzkuXxI2TqVI5/6POSzaVkFOqVTeXYPh GpeyTVBzVNTRoQp2ypTlGzdFu67GRbiglsnt4YpaJ7mBTqpqmeiokLwVJGsUO0O1 hPcgwN5hn5Wl/3ZaMwasX8c9ZmHdpzrJtAYk9Snwa94VjuDrqLxzaNwznvf4Lnw9 rwJvx4SujpgIfbh0ZHzQ8gdpK7/AjjcwtFzmZnneICpc4wIsMBSP4MzbEZ8zh8D2 gScrls43EeXXlrsGFxTlBYY8E4gT8AeYzChShcSF9EUW3b+3jQSjq5sipWWdyoMs IgIq25yn4dtInL7qX5nbPJGffu6v0S/JjyYWl2Z4n0nirwpvdccj3LQdUQP2dGxZ kzBmG4Y3vQbUdp9uR/HTViB0PxR0zpS5g6wtd41if5U1bVPKOd4= =NQrc -----END PGP SIGNATURE----- --ghytsfuuzummzdnm--