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 1oebUh-0004Mh-QP for pgsql-general@arkaria.postgresql.org; Sat, 01 Oct 2022 12:21:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oebUg-0005Cd-Fe for pgsql-general@arkaria.postgresql.org; Sat, 01 Oct 2022 12:21:22 +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 1oebUg-0005Aj-4T for pgsql-general@lists.postgresql.org; Sat, 01 Oct 2022 12:21:22 +0000 Received: from smtp.burggraben.net ([2a01:4f8:140:510a::3]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oebUb-0002Ef-US for pgsql-general@lists.postgresql.org; Sat, 01 Oct 2022 12:21:21 +0000 Received: from elch.exwg.net (elch.exwg.net [IPv6:2001:470:7120:1:127b:44ff:fe4f:148d]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "elch.exwg.net", Issuer "R3" (not verified)) by smtp.burggraben.net (Postfix) with ESMTPS id 626B6C00318; Sat, 1 Oct 2022 14:21:16 +0200 (CEST) Received: by elch.exwg.net (Postfix, from userid 1000) id 8A6713AB08; Sat, 1 Oct 2022 14:21:15 +0200 (CEST) Date: Sat, 1 Oct 2022 14:21:15 +0200 From: Christoph Moench-Tegeder To: Ron Cc: pgsql-general list Subject: Re: Example code Re: Singleton SELECT inside cursor loop Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/2.2.7 (2022-08-07) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ## Ron (ronljohnsonjr@gmail.com): > Note how quickly it runs the first five times, but takes 780x longer the > sixth time I run it.  Exiting psql and entering again causes the same > slowness the sixth time it's run. Tanks at the sixth time? That rings a bell: "The current rule for this is that the first five executions are done with custom plans[...]" from https://www.postgresql.org/docs/12/sql-prepare.html And your functions create prepared statements under the hood: https://www.postgresql.org/docs/12/plpgsql-implementation.html#PLPGSQL-PLAN-CACHING I guess you should visit https://www.postgresql.org/docs/12/runtime-config-query.html#GUC-PLAN-CACHE_MODE and try if plan_cache_mode = force_custom_plan helps here. Regards, Christoph -- Spare Space