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 1vxn2l-00H8Sb-0L for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Mar 2026 14:17:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vxn2j-00D7lD-1b for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Mar 2026 14:17:41 +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.96) (envelope-from ) id 1vxn2j-00D7l3-0K for pgsql-hackers@lists.postgresql.org; Wed, 04 Mar 2026 14:17:41 +0000 Received: from mail-ej1-x636.google.com ([2a00:1450:4864:20::636]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vxn2h-00000000NsY-0vCA for pgsql-hackers@lists.postgresql.org; Wed, 04 Mar 2026 14:17:40 +0000 Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-b8fd976e90cso871473666b.0 for ; Wed, 04 Mar 2026 06:17:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1772633858; cv=none; d=google.com; s=arc-20240605; b=EGnG9WH6X5BoI9R+BOe0xkK0Xql+oYUVh7V8Knr6aijQZml8GANBV6mnIqgYPvY/IX vyUBl+tQtNHTxi9KqNFk1pTlyoS7kHiChXKITsDsj/Fd2ON7ACzJbXUHuUXSB9WdZ04t nD366QHOIdZXB8DTWxrf2X2y7eKguRZosg5RWSh5ESYFpEQH/Wm1ivyka+M4aqdfigyE Gi82b6/UvfX8QUB+KKcmAsnvRhtxfAhfPzJBI02LJDjVVL5V70A7xojJLh3Vby4OV2yw tk7YPVskqahheECDebl9zxNIKbbm63aLVAGOSfcj0skO7NAb12+Ukngpnya4/pzD9/s/ /COw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=fdQzlc5hBXLYnwkD3g12SXP8Otx31BP94qI9HrTBNt8=; fh=b3bEQOS2BWX9cCIQl+VPQtJmq5oE3FuQjnkY95WNb/Y=; b=EVjgZ8nOODVRFMqG5HbRIHDai4AFUm+eQ7Ijc5EPf11UrUTrpY2PQZ5dMRX5f2go9h F0jFHXrpnTHv73U7hHRCj9TWRDcuGmUQMOMHJTt3AaFTYYnHjdQMNzza8a7O6qC8yeTU oM2gvwqIL5WWsfV+oSZjpCe89b8fS/BAwV1rOycBAyPUQ686ZPng8Oi6YlNxy8Ap/b/1 wQfmRNrpuVCwEM5SLqA8sNglYjrotyQGnsfGg0o6CZcH1j5gAI6juC22y30PAuRWDkYY a5tClPf/GpftQi/X7PGVsaQroipeCTIX34I4zp0ps+cClrEniFh7TXcbPadSxBqHJIoo lExA==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772633858; x=1773238658; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=fdQzlc5hBXLYnwkD3g12SXP8Otx31BP94qI9HrTBNt8=; b=WnLKu260B6Mjdhfj15vTxVQ31oWX/bVJP+3atkzUeWUvMvpvWaGYFUQCInkrRCJWAo Z4/SDXAeD4HLknsZwTlH1dDOgwRRU+cgB6X35hggGxgZ1iHy6kDqxKPrxqQlut2US2Eg 05Gt3bHdZ/Lng8HmwFuniejn7zfM5ScO1fjoZCUFLaf6Xtgad+o6qd/yRvhj+wSE3hgr mXWdgNex1EjQ+eqD+vh7AuoGRYti74j97ow3nj0NyoEnPEE0Y3GMU+4uUxqO5KimCRUD +t4PxqMFQ5uPwcrIrqKDPqFHOU84DgKyPg0oMlb41RXfVEW5MqUGOPrFyrAbkJpzjt/n CypA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772633858; x=1773238658; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=fdQzlc5hBXLYnwkD3g12SXP8Otx31BP94qI9HrTBNt8=; b=k0khGNJxc6r5Y3ildK56Jhea3n7twZ5Y/1l0fZwwWSS5QiweMezF6CUSi8KJt9te9a d447hbfm5vHiDtT8DS0b3mRuz+qRGT7BAvmjRviBhSXCR6PVwmQOCcPG4s57y5cuHWq0 dWLRfEieeI46khRnxcmMfWngo2EE6YHAH/18hvnbQTRz9t0pndq+sOMoGaMqa/+WR8nn lCVDd4wUizu157quKHpumTrFGRydy7ZCQhLkGqdLIJZYToJM2qRpEB8Vs8dOaPj2qd84 3+y8zgrLtSHbCSkpmqQV+h/v2eGwVtOYgnFrTd446egeu8Cz+WRpkg1cOxljeB3R0G4G baQQ== X-Forwarded-Encrypted: i=1; AJvYcCUCeG1UOJpkdztuckc4iOhyD4DcW5mzrEwvfX1MOJlQ4iHUNwd9vibZayF5AWJZhSIokM0f+2iIfJTjk26o@lists.postgresql.org X-Gm-Message-State: AOJu0YzSp96Rlxhi3w+Jm+lHcvh07yueCS2r240P17WGxzrtYaYKg+b9 yiOSzRz1y0FGjTrBW9bDl/LfaaZstEq+CvgfhgEaqMXnK/vj6uhd/lVq8Jc3Kydl353QW5vXyP2 Jfobet4rR9rrWYQjXF50jTvhI/P6oNLU= X-Gm-Gg: ATEYQzx27ZQqJoYaA3Hm3Vo91ZZYyCAtdi+729DSlSzPsfMw9TiFa2Voic3cVLwLG1a Jrc0+chABfGzhf41u1C12jRM+55nzOq0Ky2ISab0g3HiSQKJYorYdXVcUzeWv+XOa6AnZbyJ3+E 5JsuLEsdY6pYSNKyd4Gu41sIHEhRXyxhwNZjznyulghrTgWx48OvmBwZQwryTloqD8jhrzCbVhB 0+ZMo7WkboMMna1Aq1MYejRHHmOQN4SWUbT2mWrKM9dyMRTI6l7p8puEKcPjcl6OeLDaxQhbk+y 5pphrGD8ahf/ymH1vE1SgiHzOI1M/jTBnIX0Ldk= X-Received: by 2002:a17:907:9719:b0:b93:6bfe:4f2 with SMTP id a640c23a62f3a-b93f11c15b0mr144556866b.17.1772633857324; Wed, 04 Mar 2026 06:17:37 -0800 (PST) MIME-Version: 1.0 References: <1136161.1769654478@sss.pgh.pa.us> In-Reply-To: From: Robert Haas Date: Wed, 4 Mar 2026 09:17:25 -0500 X-Gm-Features: AaiRm50YW8fuqGti2DKl99N2aAOg_ErdGeDG1ItSegIsuZ-AKgNNTDmJ_kBG9Oo Message-ID: Subject: Re: pg_plan_advice (now with transparent SQL plan performance overrides - pg_stash_advice) To: Jakub Wartak Cc: Alexandra Wang , Richard Guo , Lukas Fittl , Tom Lane , Jacob Champion , Dian Fay , Matheus Alcantara , PostgreSQL Hackers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Mar 4, 2026 at 4:50=E2=80=AFAM Jakub Wartak wrote: > This is micro-thing, feel free to ignore, but well I was after something = much > more easy: `CREATE EXTENSION pg_plan_advice`to be no-op without any failu= re > even if it doesnt provide any views or fucntions right now (so empty > share/extension/pg_plan_advice.control and -1.0.sql) or at least some dum= my > function, just so that CREATE EXTENSION pg_plan_advice wouldn't fail. > This is nothing technical, it's just sharp rough edge for user (but > technically sound), that 2 are deployed with CREATE EXTENSION but 3rd one= is > not. I just think that if we put 3 into shared_preload_libraries then I w= on't > have to think for which ones to exec CREATE EXTENSION (I would just blind= ly do > it for all and the error wouldn't make somebody unhappy that something is > potentially not working because CREATE EXTENSION is not for it) - it's pu= re > user-focused usability feedback. Absolutely not. We don't do that for other contrib modules that don't require SQL definitions (e.g. auth_delay, auto_explain, basebackup_to_shell, passwordcheck) and I don't think we should start now. I agree that it can be confusing for users that there is a distinction between extensions and loadable modules, but the right solution to that problem is to educate the users. I think people who do know how things are supposed to work would find it quite irritating to be handed an extension that doesn't actually install anything. Also, I have some hope that at some point in the future, we might decide to ingest pg_plan_advice into core while keeping the other things as contrib modules, and if that ever happens, it will be a lot easier if it's only a loadable module and has no extension associated with it. > Yes with -M extended it is instant. I have found also that with > -M prepared I can do simple one-time `analyze pgbench_accounts` (when cha= nging > SEQ_SCAN <-> INDEX_SCAN for that table) and that is also enough for > the backend to immediatley see (and react to) to what's in the active > configured stash even for future changes without further ANALYZEs. > Not sure if pg_stash_advice needs a function to flush-force all backends, > so the plans are 100% effecitve, as apparently we seem to have ANALYZE > already, but it is not that obvious that one might want to use it. > > If there would be such function to gurantee, we probably wouldn't see > complaints like 'I have done this and session still is using old plan'. True, but causing a system-wide cache invalidation can also create quite a performance hit. I'm not quite sure what the best thing to do here is. I can see an argument that changing the advice for a certain query ought to invalidated stored plans for that query, but I don't think our invalidation infrastructure is capable of doing anything that targeted. Just invalidating everything seems pretty heavy-handed, but maybe it will turn out to be the right answer. I think we should wait for more people to play with this before deciding anything. > > > 5. QQ: will pg_stash_advice persist the stashes one day? > > > > I have no current plan to implement that, but who knows? > > OK, so perhaps docs for pg_create_advice_stash() and pg_set_stashed_advic= e() > should mention those 'stashes' are not persistent across restarts. Withou= t > this I can already hear scream of some users from the future that they > applied advice, it fixed problem and after some time it disappeared (In > other RDBMS it is persistent, so users coming from there might have such > expectations). I mean, there is already text about this in the very first paragraph of the pg_stash_advice documentation. Perhaps you're saying you think that information needs to be mentioned in multiple places in that documentation, or in a different place than where it's currently mentioned, but I'm slightly suspicious that you might not have actually read what I already wrote. > Well IMHO all the rest naming in pretty great shape and I think that > pg_[collect|plan]_advice are great names too. It's just that `stash` > keyword doesn't ring a bell to me at all that `pg_stash_advice` is > related in any way to online/transparent/runtime plan modification and > can be used to alter plans for other backends. Something like > `pg_deploy_advice` or `pg_apply_advice` would be more in line with the > other two, but perhaps it's just me.. I don't think it's just you. I originally named this pg_auto_advice. However, the naming problem that then ran into was: what do you call the containers that actually store the advice? I ended up calling them "advice stores". But I didn't like that very much, because now the name of the module (pg_auto_advice) and the name of the objects that it creates (advice stores) are not obviously related. Moreover, I had an unpleasant hunch that people weren't going to like the idea of using "store" as a noun. So I tried to think of a word that I could use for both the name of the extension and the name of the objects, and stash is what I came up with. Your suggestions here are in much the same vein as my original idea, but I would argue that they also have the same problem: we're not going to call the named advice containers "advice deploys" or "advice applies". Now, perhaps there's an argument that those names don't have to match, but I think it makes it a lot less confusing that they do. > How about if we would just measure (estimate) with some small table numbe= r of > entries vs memory used and put that into the docs?, so users are wary tha= t > they shouldn't just blidnly set it to high value? E.g. with > 1000 local limit I get ~280kB for pg_collect_advice context and with 1000= 000 > local limit I've got it to ~50MB and stopped looking further (it was stil= l > growing). Itself that's not terrible but higher values with lots of backe= nds > might cause huge memory pressure (OOMs). It depends a lot on the length of the query strings and the advice strings. I wondered whether there was some point in having a setting to collect only the advice string and not the query string, but in the end I feel like pg_collect_advice is very much 1.0 software. It works, but it's fairly primitive. It definitely shouldn't be confused with industrial-strength, battle-grade, Teflon-hardened code. The problem from my point of view is that not only are we short on time for this release, but I do not feel like I know what the requirements for something better really are. I think your suggestions so far -- deduplication, better memory control -- are very reasonable ideas, but I think it's hard to know what is really important until more people get their hands on this, try it out, and then (probably) complain about it. I think between the people on this thread, or even between you and I, we could easily come up with a list of 30, maybe even 50, possible improvements to pg_collect_advice, but I am not at all confident we'd correctly guess which 3-5 of those were going to be most important to users. I think we just need to wait for more data before deciding how to evolve this. > Or maybe other idea: is there is possibility of making GUCs like > local_collection_limits/local_collector settable only using > SET/SET LOCAL, but not global? I mean what's the point of having being > able to collect locally system-wide when realistically I cannot > pull it back from backend-local memory? (and this removes the danger > of multple backends goind wild with memory together). The GUC infrastructure doesn't support this. > resetting it back to 0 or disabling local collector and reloading won't > fix it, backend needs to re-establish connection. Even with just 10000 > local collection limit it just gets down from ~1500 tps to 900 tps. > It's seems the impact on CPU coming to be from exec_simple_query() > -> finish_xact_command() -> MemoryContextCheck() -> AllocSetCheck(), > so memory context validation that have literally nothing to do with > with this patch (other than it using a lot of memory in those scenarios) Sounds like you are running a debug build with asserts enabled, which you probably don't want to do if you're trying to benchmark. > > > 9. If IsQueryIdEnabled() is false (even after trying out to use 'auto= ') > > > shouldn't this module raise warning when pg_stash_advice.stash_name != =3D NULL? > > > > I think the bar for printing a warning on every single query is > > extremely high. Doing that just because of a questionable > > configuration setting doesn't seem like a good idea. > > Why on every single query? I'm thinking that this should bail out in pgca= &pgsa > during initialization of those modules. What's the point of those modules > if queryid is always 0? (I'm assuming somebody has compue_query_id=3Doff = and > still loaded those modules). That seems like a tremendous overreaction, given that (1) it might cause the server to fail to start and (2) compute_query_id can be changed at any time. > BTW: the good news is that I haven't seen a single crash when throwing > wild stuff on it or having strange ideas at pg_stash_advice usage. I don't think there are too many crashes left (famous last words). Here's a list of things I'm currently most worried about, approximately in order starting from the most worrying: * Maybe the advice-generation stuff doesn't correctly analyze all possible plan trees, esp. cases not covered by our core regression tests. * Maybe the stuff that uses DSM isn't careful enough and can therefore cause server-lifespan memory leaks in some scenario. * Maybe I haven't got the security model right and some aspect of what I've done there is CVE-worthy. * Maybe advice application is broken in some cases in a way that can't be fixed without additional core changes. I'd be very grateful for review targeting any of these areas. Thanks, --=20 Robert Haas EDB: http://www.enterprisedb.com