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.94.2) (envelope-from ) id 1uchQr-00FvW3-6a for pgsql-hackers@arkaria.postgresql.org; Fri, 18 Jul 2025 09:31:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uchQp-009tSi-65 for pgsql-hackers@arkaria.postgresql.org; Fri, 18 Jul 2025 09:31:07 +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.94.2) (envelope-from ) id 1uchQo-009tSa-SL for pgsql-hackers@lists.postgresql.org; Fri, 18 Jul 2025 09:31:07 +0000 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uchQn-008Pfq-1K for pgsql-hackers@postgresql.org; Fri, 18 Jul 2025 09:31:07 +0000 Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-45600581226so19570735e9.1 for ; Fri, 18 Jul 2025 02:31:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1752831063; x=1753435863; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=2m2AUwhuIrYOjfkEG0/fnwwEvu6MmuFMBDPRe1bGSGY=; b=iGHjjx9WxpCWKwo7JI+RXpfiieEjl6DFqnLp7fsCUdTKr573gvLT8PgQ0zz1/U3Y43 F5ywF6lTLpqQ0Ighfq96zGweo1cHt0PEysocsUcsYtksnvD5Q2fbY5T4Wi0zdveax/wW wDZBzUODqux8J7xb+3Fy9guuAd5U0UUIz+bcRf/9JmaeTSAmOxzvpi/mMBWScw5Snfnk JbYB3Ij7u9vOtGFBXGL0ukj6NGMElNZO/H0+wk74j21dWHJlcK5kc8kifQMh9F4YoMim 3yzWESe3q7LwP7qdgE/yuh4aXsoosiOARb5BiksVHFCsxLBDaTvufDk1oHHbhe5gwUPT kwKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752831063; x=1753435863; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2m2AUwhuIrYOjfkEG0/fnwwEvu6MmuFMBDPRe1bGSGY=; b=Q675nmKWEz65j9NspXCTND9kKp8EDmb4C1CT27Xxb2ELoQAdg/iP4dSNlK/gen00YQ rODwJQ/RMlBxvQr6fwUR14Mxi+S1WYIb2QukbBnGBTYdrzHaSTngu6SPNz7+3HRUOcNJ AQ+MexUSLQgvIzu7TLGkI36fSkQnUsOH3HcglFv5PSGzwIWjkVWeQkan5WUEsNx8IPhS Xi7jDWfJqFXold6hjUs6GPUkHzVPhvUC3ZFoURopqSVihqOJO28M0vlp2C+pvHtrr+L6 XX0TdQgLDUvlw2ZcHCvJeHRCGsAd9tepY4zw4BqKhxwzw+EL3bEt3/Gqdxe9UQgK5t46 vGbQ== X-Forwarded-Encrypted: i=1; AJvYcCXizCMxs2ORoYsjszYbzu+N+tvLKvvcDZAEHtgf9ZzH3OZKHOgWBLu+7KVyg/7L3vfdYVjiubvqA0vfqOSz@postgresql.org X-Gm-Message-State: AOJu0YwfsbmYLzcRQXu3OEKSpJcN08+y1UODITnirNpp79jNiyeohXW6 XoyRniZp556lS+6fSffWCTX2YyxCsvKcGGjn3kxYDOajp5GvaxBjsP8s X-Gm-Gg: ASbGncugs+1SwXEPK8lTZC+oWVIvJUuhV61KnOX9a5g2tmnEfFXL6/8WxW49q3k6gEa 3DDL3z6huc2NRmISS5VyWPGOcsCcYL5KCBAPmaPvfwtY0uqdRhVWsRBzUEbjMFiBjOfW43Xg85U w03eV9XcClMlj5CM3Jl1v0fhZu66kYmJy9miFh+FAnEqH00eQVClhVJLHvyZnqtAx0RgwhsKiue KrcKiWPIHT2VHd63d/VYnNeVgdBiuDIKhdwR7P0o74d9MdGqxKgskMdmuzC2CaGo8dzUxW7GJ2/ KqLtATeYLGMuAa53E5uwzEfZoIzeJEFgWvyMM+GjCsd4vJFTSfjl9/IAIZoa1+gTL+hzN485zZc IHbPZdAh7kiCyRKrSurXvrdAZ1ISZ9+5SRw== X-Google-Smtp-Source: AGHT+IFyJgWZCZg9nV+scXohHjTtzovDOtM4gAgCZLNrZDpV1+kcvg2sSC2cHmUiFjLnnEHMy6hI9w== X-Received: by 2002:a05:600c:3b86:b0:456:fd4:5322 with SMTP id 5b1f17b1804b1-4562e04538bmr101664275e9.11.1752831063170; Fri, 18 Jul 2025 02:31:03 -0700 (PDT) Received: from [192.168.15.217] ([80.251.191.198]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4562e81ccb4sm74173385e9.17.2025.07.18.02.31.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 18 Jul 2025 02:31:02 -0700 (PDT) Message-ID: <2f2bde53-972d-402c-9da3-c5467cca00bf@gmail.com> Date: Fri, 18 Jul 2025 11:31:01 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: track generic and custom plans in pg_stat_statements To: Sami Imseih Cc: Greg Sabino Mullane , pgsql-hackers , Nikolay Samokhvalov , Ilia Evdokimov References: <1d667f28-c739-417b-b024-7f5c1004a008@tantorlabs.com> <60f09467-bfe6-49bc-a47a-fe887050a23a@tantorlabs.com> <5f775987-8a93-452d-bb3f-a1de53836406@gmail.com> <56d31e81-79d8-40bd-b957-0d6dc7c46589@gmail.com> Content-Language: en-US From: Andrei Lepikhov In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 17/7/2025 20:19, Sami Imseih wrote: > Thanks for the clarification. I see what you're getting at now. Thanks for reading this! > > You're suggesting adding CachedPlanSource to QueryDesc instead of > CachedPlan. This would allow extensions to access the statistics and cost > information from the CachedPlanSource, which would help tools like > pg_stat_statements track planning data, as we are trying to do with this > patch. It could also support other use cases, such as allowing extensions to > modify the costs in order to force a generic or custom plan. I had not > considered that second use case, but if there is a good case for it, I am not > opposed. Hmm, I don't propose modifying costs. The focus is on resetting the plan cache decision that PostgreSQL has made in automatic mode. During the DBMS operation, various factors may cause a generic plan to be suboptimal or make it more desirable as well. Discussions from 2010 to 2013 indicate that the community recognised the problem and discovered an approach based on execution time and real efforts rather than a cost-based method. While I doubt it could be ideal as a core solution, an extension may potentially do it for the sake of TPS maximisation. What we need is a way to access the plan cache entry. > > Adding CachedPlanSource to QueryDesc seems doable. However, Michael > previously objected to adding CachedPlan to QueryDesc. Is there any > similar hesitation about including CachedPlanSource? I agree that we should investigate further to find the most optimal solution. Personally, I'm open to including an internal reference to a plan cache entry within the QueryDesc, as long as the plan has its roots there. > Andrei, do we actually need access to CachedPlanSource::cplan? For > tracking the plan cache mode in pg_stat_statements, it be sufficient > to add a new boolean field such as is_last_plan_generic to > CachedPlanSource. Do you have another use case you have in mind > that would require a cplan field that references either the generic or > custom plan? I'm not entirely sure. I followed your idea of referencing the entire list of planned statements during the execution of a single statement. The is_last_plan_generic field may be sufficient at first glance. -- regards, Andrei Lepikhov