Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vhdlB-003DeP-3A for pgsql-hackers@arkaria.postgresql.org; Mon, 19 Jan 2026 01:08:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vhdkB-00B8MJ-38 for pgsql-hackers@arkaria.postgresql.org; Mon, 19 Jan 2026 01:07:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vhdkB-00B8MB-1z for pgsql-hackers@lists.postgresql.org; Mon, 19 Jan 2026 01:07:48 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vhdk9-001Faf-30 for pgsql-hackers@lists.postgresql.org; Mon, 19 Jan 2026 01:07:47 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-2a07f8dd9cdso23419535ad.1 for ; Sun, 18 Jan 2026 17:07:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768784862; x=1769389662; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=nWq4dig90XF4erHzmseLmfWYa/3rZFbitIvxb3GBMxA=; b=dT1vc8JHAeGzAEdSNwBebc5uz3bWljCCwJPbBYdhAx/otXk1MJ+U/iSErFAHZ1E48N 3u78p7KzXjEuWnjvmQOypOj3oPGC1Weom5mb/pkiwIgT7Vx7isWtttuQNNcD80+DtXb7 tSDrzC9JdgRPQwCdkqPPoWRtmw0vLXfxHxgsg4Vrer4D0apqt5+WH1mTtB3c1MtdRoMp ac1dAmkxHwwto/A3Tlp4UemiEQ4sS7YPDh3qleULhPmwq4/QE68tVwvdvUzZ65xaIJbG 1DnXJngj3iiL0jfL2fG03BAQevXHw+fc6OPxfcA6yp9dCUwKrdNUbJwW2wHPmPN5gtco lA2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768784862; x=1769389662; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nWq4dig90XF4erHzmseLmfWYa/3rZFbitIvxb3GBMxA=; b=RJvNWCbqBwaR63+qaDuslMPUKflzXICOL/UI5i4XKbjm4bYa2e6o2qFzqDMXUH8fpi oWAwi+65VorbT8b3Bh9m2N+LtcUmp/8aTozGNZQkF1z0W2AfFCiLcrnORdgWo0pu2Pcz 9V6fzhb2l0Q1s7Yl7ORDjh9NZBy01d0kGLDmSnYVRDB8qF6FQ4vrDf3thGGQ1fzfLJcL A99HRBRog4TNUCqukyt8qKoj6wsJUcbmQ4vb6h7CrfxPzrwF/HhBYXuWfK2ASVSyxf1G x2JPssnPmi5wu9eE1N/m/GlXXjrtj0ZJmVlrwu4jc1mciVtFxoTDye9miBX9i9QkZFwu /CrA== X-Forwarded-Encrypted: i=1; AJvYcCUR4SxoIw1JNEHqBCk5Bz5JaxXcxdJlcVIoNavmiWGTR3d7o7aA/yelt9j6jqcplp1iK/N1zOHrV+CqtmLz@lists.postgresql.org X-Gm-Message-State: AOJu0YxceNQKtkEhaly395m3zDvu9HpxBKgNsFT5x++OyrakVADKMezU 8+NAKXCWYgpd4SW6iFJHAfED8LBVNs6o0IvDoD+QMMEgKVLKdbPkWoLV X-Gm-Gg: AY/fxX6lzZKwBNh++dFxNfAFoYti5/MEDl58Li8twVGgAxJI6/H34Zv7NbjCBvJm0v7 d98cbHXF7hdCllHI1DMTjfsyh36CQA8ZNrs4Lp8ZJZWrSkmijsMax+EZmV+2ruijrASTJ67rMvY R3mevFVIpmY5NVMAj5k2TTTryi9MK4ty2/wGkhrkKJ8vwXQed01dbap3mYNglxqRw9rvqw1Dd3o W+FD42qFV+n4Vv1iF7grkY7CIKbCVnmHhs+s14VnOVAH85pyLXA5UpQbDsw6NDtjTpVBmvcwNXL gdp8+bFJ/TzuMt2SxcrxuD1bKA/BVtYU9wnIl/Nz0ioyNceBg8Yy5IGYW10OxfGoazMeiNTr/y+ LG1kP2Xhh4BbR790fbBAkON3YUdAt5VdE5z4UAGnESBG20u+mbTyuKq26uYlebNgZQhi1aH2Va+ ilw+4= X-Received: by 2002:a17:903:1cd:b0:2a0:be68:9457 with SMTP id d9443c01a7336-2a7177d0da0mr102925735ad.44.1768784861980; Sun, 18 Jan 2026 17:07:41 -0800 (PST) Received: from jrouhaud ([115.43.41.38]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2a7193fc503sm75966425ad.75.2026.01.18.17.07.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Jan 2026 17:07:41 -0800 (PST) Date: Mon, 19 Jan 2026 09:07:36 +0800 From: Julien Rouhaud To: Sami Imseih Cc: Tom Lane , pgsql-hackers@lists.postgresql.org Subject: Re: Cleaning up PREPARE query strings? Message-ID: References: <928501.1766593260@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Sun, Jan 18, 2026 at 10:16:16AM -0600, Sami Imseih wrote: > > > > > ISTM that your proposal will actually use more memory because > > > pstate->p_sourcetext does not get free'd, but we must now allocate > > > space for a new "cleaned" query. > > > > I'm not sure that I understand. Sure we allocate a new temporary buffer > > for > > the cleaned up query string but this will be freed soon. The query string > > stored in the prepared statement will stay in memory as long as the prepare > > statement exist so this any cleanup can actually save a lot of memory. > > > > My point is pstate->p_sourcetext doesn't get freed just because we're not > referencing it from CachedPlanSource in prepared_queries. Instead, with > multi-statement strings, prepared_queries now use a newly allocated string, > new_query, which will be around until DEALLOCATE. > > ``` > + tmp = palloc(rawstmt->stmt_len + 1); > + strlcpy(tmp, cleaned, rawstmt->stmt_len + 1); > + > + new_query = tmp; > ``` > > So this patch always increases memory usage for multi-statement strings; > we have both the original multi-statement string and the cleaned up copy > around. I think that you have a misunderstanding of how memory context works in postgres. Both pstate->p_sourcetext and new_query will be freed when their underlying memory context will be reset whether or not they're individually freed, so they won't survive until the next EXECUTE or DEALLOCATE. It's actually usually worse to issue individual pfree() as it's just unneeded overhead. CreateCachedPlan() creates a dedicated memory context to save all its information, including a new copy of the passed query string, and that context will eventually be linked to a permanent one so that it can survive the original query execution lifetime. So with this patch we have an opportunity to allocate less data in the permanent memory context, which as I said requires a transient extra allocation of the current portion of the current query string.