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 1udrvl-001fBv-1u for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Jul 2025 14:55:53 +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 1udrvj-001WL2-Iv for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Jul 2025 14:55:51 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1udrvj-001WKt-9E for pgsql-hackers@lists.postgresql.org; Mon, 21 Jul 2025 14:55:51 +0000 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1udrvh-0002Qc-24 for pgsql-hackers@postgresql.org; Mon, 21 Jul 2025 14:55:50 +0000 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-456108bf94bso31218055e9.0 for ; Mon, 21 Jul 2025 07:55:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753109747; x=1753714547; 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=4j20hccyK+rI1oE7HIGiBq59UxSuHHZHbeDiT/8VBxc=; b=cDsu90UDaB0GjvjC0uj/D/uzdl8s3W+bwK5ZfmUavOnyWrwUI8UNyu6JKfyK2yB+hh NAo+7cMyPrhgAH2cfyvD4f26Ra6MvkWXec9lb7j1FeMlw5Kx+OuoCcIn8aWONt53WNPh CWjL4qm5NIknkgjMpbzQwPi5+uuOwQkSMdmpdR9aohGitjPb7uvd0vA0GsKlVXfFYZ1x hszRaDDtoNRhmUjtUbmy/xzWzOeq7wbDom3Ek9RBNcnjKqGuUUVkcV1jmG3zg8IXsr6e I/rPdnbET5DIZoImTAer1P+1qXqEaqTEnaMD9VnBBUMovkcamO1qOPg/S1H5lVVWfGZp mK8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753109747; x=1753714547; 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=4j20hccyK+rI1oE7HIGiBq59UxSuHHZHbeDiT/8VBxc=; b=QOA6aaRYr29kFh8YKjS1XqkaVQ0lFv63uFEI0VVW4/tw+xv+Ih1KVgWpuRQwhzG/T/ j0EI7iojIXegDPs5by0KHIWqD98gmbaHU+D0wpOgQC27HtyFPGSO5DzkfFTYb8kD2G7W w6sO+sGKVs2CDlQCPVK/mpdaK96VGXBlaP2AOgjtfldeYjJ6vwW1cncLUgu56mo/XQkt 7cAb16cYIWj/+BH377Kg2imHw5zsCZhV+F3/BlYKNYXQAPyLSEdOzb4fqTF+z2t59ZJL OrWJuOTbTCJ2ypdRJ+m4V+EgKxMNCof2h3rKnXS537BSTmC+uiaidaUZWAvnWhN3voI3 ZX0g== X-Forwarded-Encrypted: i=1; AJvYcCXkHGpRORM64SCR/trOjwAmoQqcEvgNz44xLZm9o1Bg60sYs2KAnFcR++WFkf3F9WZH5inSr2IR+SP7weVS@postgresql.org X-Gm-Message-State: AOJu0YwnY7at+4Bv+o0LaHHKNzeMD/oldc0+UTgSVPnMPVC9gzJoxR29 SM0HCB/d4lo+6VMBy5l0Yw7Zy1GwmJYHDhEvwIg39se34kYLypEifMl58CyhTA== X-Gm-Gg: ASbGncs4xJqrKnWq4mTCM6A/SWBToSj5kiZOZX0VrZ2i9lQo4ISquHswok4pHT9tRHF 402gOcyRH74HmYmnen8z976JcvTebInxcE3xU/cI5yvOzYGAkh/OR6OxWFue9MvSHdg267C12xM 1tSHnkVTkAKqYwPhxwh/EiVsme/BLS9ERQDuMdAMMun3ZkJfnB8Qc/d3cU1kiB5a+fgZmX+1+PC vp/GwWsmo3rQ+WYhbJdDsPLUMM6SlvZ08KNWrb5kB1ZId9Yh4BFebX0DWah55fHDfJH5LXcPeqx XQJ2hi9lgRP40LXg6H86F6Vz3mYpE+syokR2abdsHY6ot53FBTvrNEGYsTJ+DY/lp+7uG+iETI6 TLuWtBGZgVG8w3mmRCXcg+Knx+nxt858nRQ== X-Google-Smtp-Source: AGHT+IEJL9MjbyGQSaGPG9Oa7NL47SbgEiEUdJRA/YLkrs87zGRDE9sAEjMQ/5PaeGDHDu3L8QhVIA== X-Received: by 2002:a05:6000:188e:b0:3a4:eed9:755d with SMTP id ffacd0b85a97d-3b60e4d2312mr17188345f8f.3.1753109746397; Mon, 21 Jul 2025 07:55:46 -0700 (PDT) Received: from [192.168.15.242] ([80.251.191.198]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3b61ca48a23sm10628541f8f.54.2025.07.21.07.55.45 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 21 Jul 2025 07:55:45 -0700 (PDT) Message-ID: <064b9efd-92c9-4670-95f4-d911d4c7ddeb@gmail.com> Date: Mon, 21 Jul 2025 16:55:45 +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: <60f09467-bfe6-49bc-a47a-fe887050a23a@tantorlabs.com> <5f775987-8a93-452d-bb3f-a1de53836406@gmail.com> <56d31e81-79d8-40bd-b957-0d6dc7c46589@gmail.com> <2f2bde53-972d-402c-9da3-c5467cca00bf@gmail.com> Content-Language: en-US From: Andrei Lepikhov 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 18/7/2025 21:37, Sami Imseih wrote: > Thanks for clearing up my understanding. Essentially, override the > current cost-based > method of determining custom vs. generic by using something like execution time, > which is somehow tracked by the extension. That is how I understand this. > Now, I wonder if it may be a good idea to add some hooks in GetCachedPlan > to make this work? Yes, it may work. But inside the GetCachedPlan, we still don't have access to the executed statement yet. > >>> 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. > > For the sake of this feature, I suspect making CachedPlanSource available > in QueryDesc will be a non-starter, but I would like to hear other opinions. > To accomplish the goals of this patch, we definitely need the current > execution’s > "plan cache mode" to be accessible in ExecutorEnd somehow. I agree with this - adding references to CachedPlan into the QueryDesc looks kludge. The most boring aspect of pg_stat_statements for me is the multiple statements case: a single incoming query (the only case in the cache of a generic plan) may be rewritten as various statements with the same query ID. So, the execution time of the initial statement should be the sum of the executions of all rewritten parts. I wonder if the pg_stat_statements processes it correctly at the moment. Because in this case, we'd need a hook inside the Portal execution code. However, I don't recall any threads on the mailing list discussing that. If the multiple-statements case didn't exist for a cached statement, Michael's idea with an active portal would be much better, and it would make sense to add PlanCacheSource::cplan and replace Portal::CachedPlan with Portal::PlanCacheSource reference. -- regards, Andrei Lepikhov