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 1vgFAw-009qPG-2G for pgsql-hackers@arkaria.postgresql.org; Thu, 15 Jan 2026 04:41:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vgFAv-00Eg9K-2u for pgsql-hackers@arkaria.postgresql.org; Thu, 15 Jan 2026 04:41:38 +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 1vgFAv-00Eg9C-20 for pgsql-hackers@lists.postgresql.org; Thu, 15 Jan 2026 04:41:37 +0000 Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vgFAt-000YSt-30 for pgsql-hackers@lists.postgresql.org; Thu, 15 Jan 2026 04:41:37 +0000 Received: by mail-pj1-x102f.google.com with SMTP id 98e67ed59e1d1-34c9edf63a7so328938a91.1 for ; Wed, 14 Jan 2026 20:41:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768452093; x=1769056893; 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=6/xOJB1Lk0FEymoYmHmJCX/K4Q9gDxwb1eVAf6VXtFk=; b=BvdMcPLu6r4oXVAbejhkNqWkFBqOBJuJuXsPsHERSmibVml4ioUJRqLCS7ACpysLvb R3wT33JBZ3oFZE2cXgphWQ1jwZVPwySi25ib9brYi+ABhVvHoqR6rTMmovLxpPF8YquT 1eXLNREKSawD/YeVu/SiKMZvvpJk/SJrmQwzb701FXZpBvsJITSgVS5i3ZXTgRynj75o smvp11H8r69hStEk+LaniopK2ZKJ8rXIZsUfnafOQ7AleGxObx3lADidA9kzxMHta/og +q6KiDAUY8AgmZL4VY9/Egm1ndD3+Esw/YjiEFREUnCcSBvIS6fRZFrU9VfTvgUJwv9s kK4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768452093; x=1769056893; 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=6/xOJB1Lk0FEymoYmHmJCX/K4Q9gDxwb1eVAf6VXtFk=; b=QCmm5LAlJRmETZjQdGK+wH2b9VgCQ81UnZRMhWrr3eXBPYChD1epErWiIY8sjv5RgG 7fYaxYimzbgEotVhphdq48EXc/5JyCpHMQc5WacHZygozBouDRTYPdqOdh2cMAVJlAtU Xv+uB9nf7D1wm2bhMgand2yRMpS9Xp35JykUBEOt3oGKTP+7C9qH7m5TeLePBYs3xkOw e0nZQgT+vYl9t33Q2XfSAjjEoAgNXZgt1vELRLX13DOhVAHrsm3kQmnQYt+uvbOImc1/ 32ODMbRDe9iXROuPjj8XX7jbjFK754crlg3MEer2SVBALHcjJPVB3pZk7TJA9DGMbtp7 26PA== X-Forwarded-Encrypted: i=1; AJvYcCUFK6yfxhB/QiR1bI845J9C2UKYBpOwYb4iZUk3V2rBhDm0hskTchIHKxG+kFXmwHwbeYOvpSJ8lq/kWz6h@lists.postgresql.org X-Gm-Message-State: AOJu0YyarGFBYXMxf6fcWE3wekWmATUGN9ocvfqJfu0wTdwaeQ0FvAJv +apTCXkxUSHznAChsUpebskr4XXpjl82qsTnGFUx9crlJCAhVsLK+3ln X-Gm-Gg: AY/fxX62986gvD6gAh9ZENil+whr9nsb3HnX5zEJVYvogro8lKAyHGn0Hg35qjZUAJc gXXWQy/e6aJ2Wkh/n8xDcp5CebSW9t/AngZPnLUUOl3C1+DDK56HzdQM7EyzNL9x6c3zokTd8VJ kjdQqA1oZmDBarvLMnsFgxX6pH7G0HQLsL64GHVARdTMScV6NEdToqk6EsqYctQXibiAblBC0cH DGnybNqGcG0U6EDzF/UpGK2HmrzUvrZChLXIkq+Mus6w/os2XrgMIuUVXESDapUFXahhCVInlJS OAVGXl0KWOCaKLQlVyfWCZdb9Nx4SXAe9IH1Ume3i9BVZ8wtsTAkJ6UpmGRo+RDzhD6oa4YY9VC q/yjuWN4/KoLAc+1xJuND64UYtQsSpeZI9mCNJY2wcfEbma1c0Y8oaKb31D4S63vPvKgm5l8EoK xx9UQ= X-Received: by 2002:a17:90b:2709:b0:340:ad5e:ca with SMTP id 98e67ed59e1d1-351090b4b91mr5325093a91.12.1768452093300; Wed, 14 Jan 2026 20:41:33 -0800 (PST) Received: from jrouhaud ([115.43.41.38]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-352677e1581sm826843a91.5.2026.01.14.20.41.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Jan 2026 20:41:32 -0800 (PST) Date: Thu, 15 Jan 2026 12:41:29 +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 Wed, Jan 14, 2026 at 09:56:21AM -0600, Sami Imseih wrote: > > That's very strange. I'm pretty sure that I tried since the earlier approach I > > mentioned using doing it in CreateCachedPlan() had that exact problem. > > > > Also, src/test/recovery/t/027_stream_regress.pl does run the full regression > > tests with pg_stat_statements enabled, and it doesn't fail. > > > > I'm not sure what is different in your case. > > the regression tests succeed because by the time the multi-statement command > is executed, the queries involved have already been tracked by > pg_stat_statements, > so they don't need to get stored again, and no reason to go through > generate_normalize_query. In this case, SELECT $1 is already tracked. I see. I'm not sure why my first prototype used to crash with that exact same problem in 027_stream_regress but not this one, but anyway. I only had a quick look at the code but unless I'm missing something it's mostly an oversight as I pass "new_query" to CreateCachedPlan() but still pass the original query string to pg_analyze_and_rewrite_varparams(). Using "new_query" there too should fix the problem? In an earlier message you wrote > I am thinking that storing the statement length and location in the entry > of prepared_queries may be the better option. Having that info, the > cleaned up query text can be generated on the fly whenever > pg_prepared_statement is called. I don't like this approach as it has more execution time overhead, but also doesn't fix at all the memory waste in the prepared statements cache. > To repro the crash, just reset pg_stat_statements prior. > > ``` > diff --git a/src/test/regress/sql/prepare.sql b/src/test/regress/sql/prepare.sql > index 0e7fe44725e..51421357f26 100644 > --- a/src/test/regress/sql/prepare.sql > +++ b/src/test/regress/sql/prepare.sql > @@ -4,6 +4,9 @@ > > SELECT name, statement, parameter_types, result_types FROM > pg_prepared_statements; > > +CREATE EXTENSION pg_stat_statements; > +SELECT pg_stat_statements_reset(); > + > SELECT 'bingo'\; PREPARE q1 AS SELECT 1 AS a \; SELECT 42; > EXECUTE q1; > ``` > > For this test, we should either reset statements before or construct a > statement for the test that has not been tracked. We can maybe > do that with a query against a newly created table in prepare.sql. > I like the latter more. FWIW I think that this should belong to pg_stat_statements testing, no the main regression tests. This would also ensure that we see consistent results in some other scenarios.