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 1q4cC2-0002gl-4R for pgsql-performance@arkaria.postgresql.org; Thu, 01 Jun 2023 06:53:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q4cBz-00021U-Re for pgsql-performance@arkaria.postgresql.org; Thu, 01 Jun 2023 06:53:51 +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 1q4cBz-00021G-Dm for pgsql-performance@lists.postgresql.org; Thu, 01 Jun 2023 06:53:51 +0000 Received: from mail-ej1-x631.google.com ([2a00:1450:4864:20::631]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q4cBr-003RE3-6C for pgsql-performance@lists.postgresql.org; Thu, 01 Jun 2023 06:53:50 +0000 Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-97392066d04so54538466b.3 for ; Wed, 31 May 2023 23:53:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20221208.gappssmtp.com; s=20221208; t=1685602423; x=1688194423; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=ryf93DwrF4AwtHmvmlh+Nffpi68IK39rMZ5I/1Nrq8s=; b=hgiteYZYXdDtAtlX6QQrybDL5gDF7z4fy4sHGSgd68hYG5YMhQNbyQO4yVjn15UeLK ireFbd5dnn+UtRkW1E3spZ0SC5/UnQ0oEh/VTrlOC8VPeABH3vItWAPB7Eoobj0Jt6/e 0EhxZH8VXMATA1SDtxjIQ78fcoyoNGhZVEzisuFMVnFxuBMfHKJZJ8yrNFSun8axcPD2 x190jcokw2/aLzQRzFMn0JdtsQbBrM7MgdGL0yUF26cwSp0FOWV9Y0sRDaGvxZNqT5m2 w+iHgbRZlcZmyTrNmHErez0EAkIPWNvWLprcQ5sC+uiNBNA3gEltmg+AqwD31LxbidGl ad5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685602423; x=1688194423; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=ryf93DwrF4AwtHmvmlh+Nffpi68IK39rMZ5I/1Nrq8s=; b=EiyICSrc+oWGdNTNS0jVZxLcgqTxdpP1wGrvgznpobZrQ2bYXrtDZuZBFo6dIuTN/Y iS6BIDg9wTTx102MOYEw2p/TGXPVRiBDVxEV+S8DocEFZUVxTGY6R61AkRpma9PbtMQR AgP/mLLGxgTY1ntZLZfyDUEKHCcTdeP8F/GOfKx8fEmiz7VOsgSDTm02dubEDp+jER+z MIbpYy4x6nzKGrxa0wzISFr0Jz6hGKjuJP/YXbb4RhECFvc2zWVNalBZK4Uq2LZYhsjh nXkOXc8BtrNAJDVs0UlkeswYq66w3/zT0lANzoF+2WZyHQZzb2A3oG+TijN3I67B5T+v ZVfw== X-Gm-Message-State: AC+VfDx0xP81leatv4rDe8LeyfUoa8Ef6dSWGp9fRrJk/hifn0oczhMm MkDHp+5FqA/eiwO82xtFH0rsIQ== X-Google-Smtp-Source: ACHHUZ6+woGOxS4B39RQ9UCgGyNC3Vig80AlaxzqYcxmAlYFqoI3NFao/INLnSbuRm6gZre7x1QeHQ== X-Received: by 2002:a17:906:4fd0:b0:965:d17b:26d with SMTP id i16-20020a1709064fd000b00965d17b026dmr8979877ejw.35.1685602422716; Wed, 31 May 2023 23:53:42 -0700 (PDT) Received: from localhost.localdomain ([2001:871:5e:e788:ec0f:bb76:46ee:40fc]) by smtp.gmail.com with ESMTPSA id q15-20020a1709066b0f00b0096b4c3489e6sm10240452ejr.177.2023.05.31.23.53.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 May 2023 23:53:42 -0700 (PDT) Message-ID: <9513916420a6c0a00eb5ecacfba984e2e5a97e0d.camel@cybertec.at> Subject: Re: thousands of CachedPlan entry per backend From: Laurenz Albe To: "James Pang (chaolpan)" , "pgsql-performance@lists.postgresql.org" Date: Thu, 01 Jun 2023 08:53:41 +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 Thu, 2023-06-01 at 03:36 +0000, James Pang (chaolpan) wrote: > PG V14.8-1 , client using Postgresql JDBC driver we found 40MB process me= mory per > backend, from Operating system and memorycontext dump =E2=80=9CGrand tota= l:=E2=80=9D, both mached. > But from details, we found almost of entry belong to =C2=A0=E2=80=9CCache= MemoryContext=E2=80=9D, > from this line =C2=A0CacheMemoryContext: 8737352 total in 42 blocks; 1021= 944 free (215 chunks); 7715408 used, > but there are thousands of lines of it=E2=80=99s child, the sum of blocks= much more than =E2=80=9C8737352=E2=80=9D total in 42 blocks >=20 > Our application use Postgresql JDBC driver with default parameters(maxpre= pared statement 256), > there are many triggers, functions in this database, and a few functions = run sql by an extension > pg_background. =C2=A0We have thousands of connections and have big concer= n why have thousands of entrys > of cached SQL ? =C2=A0that will consume huge memory ,=C2=A0 anyway to lim= it the cached plan entry to save memory > consumption?=C2=A0 Or it looks like an abnormal behavior or bug to see so= many cached plan lines. If you have thousands of connections, that's your problem. You need effect= ive connection pooling. Then 40MB per backend won't be a problem at all. Having thousands of conne= ctions will cause other, worse, problems for you. See for example=20 https://www.cybertec-postgresql.com/en/tuning-max_connections-in-postgresql= / If you want to use functions, but don't want to benefit from plan caching, = you can set the configuration parameter "plan_cache_mode" to "force_custom_plan". Yours, Laurenz Albe