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 1oegFX-0003iJ-Gd for pgsql-general@arkaria.postgresql.org; Sat, 01 Oct 2022 17:26:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oegFV-0000TJ-Of for pgsql-general@arkaria.postgresql.org; Sat, 01 Oct 2022 17:26:01 +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 1oegFV-0000P4-BJ for pgsql-general@lists.postgresql.org; Sat, 01 Oct 2022 17:26:01 +0000 Received: from mail-oi1-x232.google.com ([2607:f8b0:4864:20::232]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oegFO-0004i5-FU for pgsql-general@lists.postgresql.org; Sat, 01 Oct 2022 17:26:00 +0000 Received: by mail-oi1-x232.google.com with SMTP id m130so7751217oif.6 for ; Sat, 01 Oct 2022 10:25:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date; bh=HLGjkz19h5q45N+C27/NvQQRkhzzEixbjMkvzC6NGQk=; b=hYx4nM0z+kPsgH3ASLUhT/86xb6R7JTjTgMNgNr0OsBK+sq263NWLVWOV0USXuHK1m k4im5sKXr8ba9Mp95TBb/FD+fxSjYVkZrCEhMEuIrIeHxyVelL1nLs9ZGJYCV6qhNczw WYufuiPjruKnBbrp+KehAE+az5B8YpW6YHh1rrQOOtuuzInjdQHBQRdTBcIhTD8aYYKK jCtV1JDsa4sNJ5ibrlVSPMJc8uf5GhUkdHmhxxQ2I6NkVgtjXwyPmenQVRk1tbWXe34T t9ylZaQoDUHHB7Ki+5X23UNB8g4aj9K2S3dDmu0EuAWRwYiad3zYeCP4YjC+tPMtQGAB hQRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date; bh=HLGjkz19h5q45N+C27/NvQQRkhzzEixbjMkvzC6NGQk=; b=1uhbd0uIzgf5n36uIt0t3EN5W3uIA2piBypD77wEqbBkCnNJbFCrJFRHavy0nAYY3s R3oFBict946B+fpyxv7MOqW3BGaaBcoBa9VjbHpgmpZjp5L4KSTvMtXX7RgmMbuhygH8 4NS4EsTkisNei8Kr5qIqCsL/lUGRPRmA1M8t2EYPO+51baSVcPjIme/f3BQN3/nc8qPh bvfVVKSi0SAXmthL+sZ9HEr6kUxDUEmfRdorTS2tfIKI7ahYZlKsw29k+CuZuWFROgNo Xwh+nDNpgVNHIJ0zsw9tB/9Ulyq7KUttAC0k9bbHZioQkDYyrEZfnkCdFrjlZty7AvQl bqWA== X-Gm-Message-State: ACrzQf2W54XpwtYB75gIpzg7HNo8R8MN3MvUWXomXGbuyW9nwgc5JkS5 SUH6H54FD2gtTgCJkp3f7I6OSvlhU9s= X-Google-Smtp-Source: AMsMyM5t/ePwr9yD3h1ASKbP85pGQ7CDIC6QgCxMWRbeJM5GQC8AyHv/R/siuIU1KUZ+0jmOgieo6Q== X-Received: by 2002:a05:6808:151f:b0:34f:964d:dca6 with SMTP id u31-20020a056808151f00b0034f964ddca6mr1409558oiw.131.1664645152002; Sat, 01 Oct 2022 10:25:52 -0700 (PDT) Received: from [192.168.88.10] (ip68-11-68-85.no.no.cox.net. [68.11.68.85]) by smtp.googlemail.com with ESMTPSA id p84-20020acad857000000b00345cce8083dsm1300568oig.33.2022.10.01.10.25.51 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 01 Oct 2022 10:25:51 -0700 (PDT) Message-ID: Date: Sat, 1 Oct 2022 12:25:50 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.9.1 Subject: Re: Example code Re: Singleton SELECT inside cursor loop Content-Language: en-US To: pgsql-general list References: From: Ron In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 10/1/22 07:21, Christoph Moench-Tegeder wrote: > ## 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 The question then is "why am I just now seeing the problem?"  We've been using v12 for two years, and it just happened. The only recent change is that I upgraded it from RDS 12.10 to 12.11 a couple of weeks ago. > And your functions create prepared statements under the hood: > https://www.postgresql.org/docs/12/plpgsql-implementation.html#PLPGSQL-PLAN-CACHING The same thing happens when I put the SELECT in a prepared statement, so that seems the likely cause. > 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. That solved the problem.  Thank you! -- Angular momentum makes the world go 'round.