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 1vxO92-00BYfE-01 for pgsql-hackers@arkaria.postgresql.org; Tue, 03 Mar 2026 11:42:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vxO90-006k54-0t for pgsql-hackers@arkaria.postgresql.org; Tue, 03 Mar 2026 11:42:30 +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 1vxO8z-006k4w-2g for pgsql-hackers@lists.postgresql.org; Tue, 03 Mar 2026 11:42:30 +0000 Received: from mail-lj1-x22d.google.com ([2a00:1450:4864:20::22d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vxO8w-00000000Aho-3nJD for pgsql-hackers@lists.postgresql.org; Tue, 03 Mar 2026 11:42:28 +0000 Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-389f200c26eso81135031fa.0 for ; Tue, 03 Mar 2026 03:42:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1772538145; cv=none; d=google.com; s=arc-20240605; b=ND0D7gQoTAGX2N06XUgkzaKfZnbq+PNlntzuOojOg4q2NDH0ZNlAQf7RExmfCDYWRR 7yyWk4YIKyzN6Vicqe96su4WEb/pMIT2llpr3ZtGolStWOIB1eLOmIdi6Nul+z/nfchy EkZtli6OjlNSH58+pTIs6AOXxNesbaNo3nE/KqKbWcz7WtHMmub85YwFefSYX+gxzxHe 0ync9gBNZM3JZ94VGqsz2yMYO7/NgPDRUWUTNANwKhdT00cyWwyefBpRHwH8fAAPAKtp 4PX41DKDBTB3wVaqfHJ+vRUD+mcrfojiYIus1yw1GDsoOPBfgcPp0smXtqudl1S/q2uA D/VQ== 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=WN3T3WY+O040EYje6tnhQ1saK+CPOIQMmfjDu/DS21A=; fh=amNylPCyxuCQcyuT4zZRLy0rWFrsPAYTjxKPjkWaA18=; b=T8Qvl1O8Yt+eowHMRnw/xBtyJs3FIQgla6/Q9/70ZlIl4+A4tTq91krl/nsnNLd368 WPvG+vblo8x8twlNq/Bh4pSx8rlHB3G37QapTrTEMgEn0fKv2X1Yl3Ts9hnm8lltd0sm FhHi8swkTdBSX9xoaLlZlcBFMqfKFDGmUVStm91CaO8KwbzwSUBhS8gDujenmEEV2Hmj 1YlG6TGbSduVJx58l2QP2C7KHSxTJa2FX2A+XCOAaztPAWJii7SNaao0kaKkodhjn6le 3aUS5A400xMQRzDt5y0p/1prWNB6vEI1cKRc2GtIs51JHxVbIRqsgmJQyNrD/+V05WjQ 4acQ==; 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=enterprisedb.com; s=google; t=1772538145; x=1773142945; 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=WN3T3WY+O040EYje6tnhQ1saK+CPOIQMmfjDu/DS21A=; b=hpkMUgFWD6SXA9n/OV7cl7xdAnBg20Un4+zkQJ+cq1ZihkhA40+cwDcu+kz2SxGykP 06OLOQjv2w+QMplBPO5OatUevThiQziURWACN8hSUfQEtwPcM4HvdIt8KyWYwoS2ZbpA clSyny8yarOCY+FEek36wuv3SIrw9yzeIUQT/hzty2JBuZF2lvFY6YElOqZYVLuZcrjg Kp+99Ob3U3SASRNgOgQdGQ5H1qevXDA/42Kk8EYey/R5ltOgc4soDVIaciNambvyeKyi Ljk6C0JFz6OS8buADngwgNNmxEHK/12+YbXWUyd6v928odugQqVPSViLSUnTUxbFNmlf j8Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772538145; x=1773142945; 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=WN3T3WY+O040EYje6tnhQ1saK+CPOIQMmfjDu/DS21A=; b=J6vb13aLeJ0smdDY7modcd4wBjtwFUnDvWYAwRgQ+GVwEXCig8ljfdAtMLvHE/y2Mv UVkUipKL5esi33FAuoiqNt2a4s8H664IxRjU8L5xjscb3A1VnwDYCQoLYaHTChtVpG6Y kW/YbBkDIkDiv0TRFpLC/obUimCY6QR29azYuijMtrX6JTrAmsoMFzv2dkPpo1dfEjcT RlWmO360QKMpvL1jaTvfi1hJ+Hqwho+Kke0itf0zcFmMwBZbSX606hNRXKc9vZ1/MuQm xFTkwXBLEBgbglyqFz3PA6Nw1bmLIz4z0McBDur08iThAqyoo3gzsZFWg+twGmHr9WJN oFkQ== X-Forwarded-Encrypted: i=1; AJvYcCVtT0063xhR1QC6s4wKDvCjys3GPC5eU3vBVNigXGJbmmQ/68eThzmUR5BQu9s4q65Ds6SQus4fsWY10GLy@lists.postgresql.org X-Gm-Message-State: AOJu0Yxu8CltyuC7bc2etENJAkq2pKTpySS0pQus/alsl+z9PQgHghpl CCZliPSDlJqpCBqLcihpPM+1wlM+Z7KyeL1ncppWvN7j97L2ie/JrtAg8FybU20cUAx+c1vHpvs eOoqHmxkiaNpxZFOPP70gsKzry3VhKYrktbdLB5eW X-Gm-Gg: ATEYQzw3iZjlm5Ws+ETis7gvpUN3oKJAh6ZTLr9LQv1plBMtPiBNA+AqnTVh3jNCZNV 7pEl54fRb/PqQtK+UhF9c7UutiAUNG7pSaWhCaMOXIM79mhPvS3tL3ef8MeoLGhKjo2aEuShVCQ RVdbuj8nJVv2DOf6L4ocCI/UiGOCYbs1Ek6xmBrPnTTx+Vdwf8OrGmNkHq4IJvG5B6/+XqxO2mC BRExlivMPzcPObVur5UlYbYglkv0TprT9G41LrmxXq47p0/WDcfGdHscAMbe7T/jJlFkauCtzvB jR7/yG3jsKgZbzcMfg8R0I+GvsKaxHpLi6rfVccKItrpT91oyaRlx8XN6cnp1IZWaW4= X-Received: by 2002:a05:651c:2116:b0:387:760:4c75 with SMTP id 38308e7fff4ca-38a08ba39a4mr76266181fa.39.1772538144982; Tue, 03 Mar 2026 03:42:24 -0800 (PST) MIME-Version: 1.0 References: <1136161.1769654478@sss.pgh.pa.us> In-Reply-To: From: Jakub Wartak Date: Tue, 3 Mar 2026 12:42:13 +0100 X-Gm-Features: AaiRm52JlMrrLo7OdXBytBUhhqZTWMP4ps9mF1UkL3J-f3EIkPdbpXCU4dW7hRk Message-ID: Subject: Re: pg_plan_advice (now with transparent SQL plan performance overrides - pg_stash_advice) To: Robert Haas 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 Fri, Feb 27, 2026 at 11:46=E2=80=AFPM Robert Haas wrote: > > On Thu, Feb 26, 2026 at 8:55=E2=80=AFAM Robert Haas wrote: > Here's v18. [..] [..] > Second, I also added a third contrib module called pg_stash_advice. > This uses the same hook that I previously added for test_plan_advice, > but unlike that module, this one's not just a test. It lets you set up > an "advice stash" which is basically a query_id->advice_string hash > table. If you then set pg_stash_advice.stash_name to the name of your > advice stash, it will do a lookup into that hash table every time a > query is planned and, if the query ID is found, it will do the > planning with the corresponding advice string. What I think is > particularly cool about this is that it shows that you can really use > that hook to apply advice on the fly in any way you want. I suspect > that query ID matching will be suitable for a lot of use cases, but > you could have a similar module that matches on query text or does > anything else that you want as long as an advice string pops out at > the end. It shows that the core pg_plan_advice infrastructure is > pluggable. So this is both something that I think a lot of people will > find useful all on its own, and also a design pattern that people can > copy and adapt. Hi Robert, I'm glad that you posted this. I've thought this is so important= that I've sligthly added more descriptive subject as this is NEW and easily to m= iss out as it was burried in the second paragraph and most folks probably would= miss that this $thread is now includes code for transparent SQL plan overrides (most known as baselines or SQL plan management) - it has many names, but I havent come across "stash" as one of them, so people could miss it too easily. 1. First thought is that I found it quite surprising, we now have 3 modules and it's might cause confusion and lack of consistency: - 3 can to be in shared_preload_libraries (pg_stash_advice, pg_plan_advice, pg_collecti_advice) - 2 others can use CREATE EXTENSION (pg_stash_advice, pg_collect_advice), b= ut "create extension pg_plan_advice;" fails so maybe they all should behave the same as people (including me) won't rea= d the docs and just blindly add it here and there and issue CREATE EXTENSION, but it's going to be hard to remember for which ones (?) So we need more consistency? 2. Should pgca always duplicate entries like that? (each call gets new entr= y?) Shouldn't it just update collection_time for identical calls of userid/dbid/queryid? postgres=3D# select * from pg_get_collected_shared_advice(); id | userid | dbid | queryid | collection_time | query | advice ----+--------+------+---------------------+-------------------------------+= -------------------------------------------------+-------------------------= ------------------ 0 | 10 | 5 | 1069089066624131207 | 2026-03-03 09:47:40.322294+01 | select * from pg_get_collected_shared_advice(); | NO_GATHER(pg_get_collected_shared_advice) (1 row) postgres=3D# select * from pg_get_collected_shared_advice(); id | userid | dbid | queryid | collection_time | query | advice ----+--------+------+---------------------+-------------------------------+= -------------------------------------------------+-------------------------= ------------------ 0 | 10 | 5 | 1069089066624131207 | 2026-03-03 09:47:40.322294+01 | select * from pg_get_collected_shared_advice(); | NO_GATHER(pg_get_collected_shared_advice) 1 | 10 | 5 | 1069089066624131207 | 2026-03-03 09:47:41.983973+01 | select * from pg_get_collected_shared_advice(); | NO_GATHER(pg_get_collected_shared_advice) (2 rows) It floods like that for everything, most visible with couple of independent starts of pgbench -M prepared. Maybe I'm wrong , but I don't think it worke= d like that earlier before refactor? 3. The good news is that I have managed to finally overrride SQL plans completley transparently using set of those modules for online pgbench runs= . However I found couple of issues. 3a. Because query_id each time will be different for every query even in standard pgbench mode, I've managed to achieve it only using prepared statements. Realistically we'll need some way of modular matching not just on query_id = OR query_id should be changed how it being calculcated or maybe by some other = means (argument is: not all users/apps use prepared statements): - at least some minimal query jumbling, part of me belives that e.g. code f= rom 62d712ecfd940 / pgss's generate_normalized_query() should be more reusabl= e across other extensions, and then why not just use it here? - maybe it should: ignore uppercase vs lowercase, removal of extra whitespa= ces - maybe it should: ignore schema names (in multi-tenant shops) - maybe it should: remove SQL comments (/*+ xxx */) or newlines (as there = are client-side libraries that annotate SQL queries by putting comments to lo= cate e.g. app source code location/origin) - maybe we even should have some regexp I'm not buying argument that it will make something slower, because : a) this is on-demand loaded module for those who want it b) it exists purely to avoid way bigger performance problems in the first p= lace 3b. When performing manual testing using PREPARE s1 / EXECUTE s1, the SQL p= lans are effective out of the box. Below I'm intentionally downgrading runtime performance and that works: postgres=3D# PREPARE p1 (int) AS UPDATE pgbench_accounts SET abalance =3D abalance + $1 WHERE aid =3D $2; PREPARE postgres=3D# \timing on Timing is on. postgres=3D# EXECUTE p1(42, 42); UPDATE 1 Time: 8.241 ms -- making it slow, queryid from explain (verbose), same session: postgres=3D# select pg_set_stashed_advice('abc456', -9041336337128051785, 'SEQ_SCAN(pgbench_accounts)'); pg_set_stashed_advice ----------------------- (1 row) Time: 0.995 ms postgres=3D# show pg_stash_advice.stash_name ; pg_stash_advice.stash_name ---------------------------- abc456 (1 row) Time: 0.366 ms -- OK, that's effective (up from 8ms) postgres=3D# EXECUTE p1(42, 42); UPDATE 1 Time: 448.415 ms 3c. However for pgbench -M prepared, such online plan alterations are strangley not effective. Even creating new stash under different name, sett= ing GUC, and pg_reloading_conf is not effective too for hundreths of seconds of pgbench. 3d. ... however restarting restarting pgbench helps and the session changes plan (and thus performance characteristics). When using manual way of reproducing this more like: sess1=3D# PREPARE p1 (int) AS UPDATE pgbench_accounts SET abalance =3D abalance + $1 WHERE aid =3D $2; PREPARE sess1=3D# EXECUTE p1(42, 42); UPDATE 1 sess1=3D# \timing on Timing is on. sess1=3D# EXECUTE p1(42, 42); UPDATE 1 Time: 8.823 ms -- now from 2nd session sess2=3D# select pg_set_stashed_advice('abc456', -9041336337128051785, 'SEQ_SCAN(pgbench_accounts)'); -- .. it was NOT effective, and still fast, but it should be slow: sess1=3D# EXECUTE p1(42, 42); UPDATE 1 Time: 8.781 ms postgres=3D# However doing it from sess2 sometimes, makes it often faster effective: postgres=3D# select pg_create_advice_stash('xyz123'); postgres=3D# select pg_set_stashed_advice('xyz123', -9041336337128051785, 'SEQ_SCAN(pgbench_accounts)'); postgres=3D# alter system set pg_stash_advice.stash_name TO 'xyz123'; postgres=3D# select pg_reload_conf(); but apparently that was still not solving the pgbench case problem. So it doesn't seem to be deterministic when the new plan is applied (?) 3e. As this was pretty concerning, I've repeated the pgbench -M prepared excercise and I've figured out that I could achieve effect that I wanted ( boucing between fast <-> slow immediatley) by injecting call via gdb on tha= t backend to InvalidateSystemCaches(). Kind of brute-force, but only then it worked instantly the pgbench backend started using new "stash" immediatley). Quest= ion should or should not it immediatley effective and have you got any idea why there is such difference in behaviour between pgbench and psql? Should we investigate it further? I think, we should? 4. The familiy of pg_*stash*() functions could return some ::bool result in= stead of void. Like true? (it's usage "feeling" that leaves one wondering if the command was effective or not, e.g. to get consistency with let's say pg_reload_conf= ()) 5. QQ: will pg_stash_advice persist the stashes one day? 6. Any idea for better name than 'stash' ? :) It's some new term that is fo= r sure not wildly recognized. Some other used name across industry: plan stability, advice freeze, plan freeze, plan force, baselines, plan management, force p= lan, force paths, SQL plan optimization profiles, query store (MSSQL). 7. I saw you have written that in docs to be careful about memory use, but wouldn't be it safer if that maximum memory for pgca (when collecting in sh= ared with almost infinite limite) would be still subject to like let's say 5% of= s_b (or any other number here)? 8. I'm wondering if we should apply standard PostgreSQL case insensitivity rule (e.g. like for relations) for those stashes? On one front we ignore ca= se sensitivty for objects, one another this is GUC so perhaps it is OK (I feel it is ok, but I wanted to ask). 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 NU= LL? > [..altered sequence], [..] > First I realized that it might be confusing to have the collector > interface as part of pg_plan_advice, because for most of what > pg_plan_advice does, you didn't need the extension, but for that part, > you did. So, I broke that part out into its own extension, now called > pg_collect_advice, [..] 10. I'm was here mainly for v18-0007 (pg_stash_advice), but this still look= s like some small bug to me (minmax matching in v18-0003): create table t1 as select * from generate_series(1, 100000) as id; create unique index t1_pk on t1 (id); analyze t1; -- OK "matched" postgres=3D# set pg_plan_advice.advice to 'INDEX_ONLY_SCAN(t1@minmax_1 public.t1_pk)'; SET postgres=3D# explain (plan_advice, costs off) select max(id) from t1; QUERY PLAN ----------------------------------------------------------- Result Replaces: MinMaxAggregate InitPlan minmax_1 -> Limit -> Index Only Scan Backward using t1_pk on t1 Index Cond: (id IS NOT NULL) Supplied Plan Advice: INDEX_ONLY_SCAN(t1@minmax_1 public.t1_pk) /* matched */ Generated Plan Advice: INDEX_ONLY_SCAN(t1@minmax_1 public.t1_pk) NO_GATHER(t1@minmax_1) (11 rows) Manual SET, wont work and it is failed (which is OK) postgres=3D# set pg_plan_advice.advice to 'SEQ_SCAN(t1)'; SET postgres=3D# explain (plan_advice, costs off) select max(id) from t1; QUERY PLAN ---------------------------------------------------------- Result Replaces: MinMaxAggregate InitPlan minmax_1 -> Limit -> Index Only Scan Backward using t1_pk on t1 Index Cond: (id IS NOT NULL) Supplied Plan Advice: SEQ_SCAN(t1) /* matched, failed */ Generated Plan Advice: INDEX_ONLY_SCAN(t1@minmax_1 public.t1_pk) NO_GATHER(t1@minmax_1) However with below SEQ_SCAN is applied/matched, but marked as failed (so bug?): postgres=3D# set pg_plan_advice.advice to 'SEQ_SCAN(t1@minmax_1)'; SET postgres=3D# explain (plan_advice, costs off) select max(id) from t1; QUERY PLAN ----------------------------------------------- Aggregate -> Seq Scan on t1 Supplied Plan Advice: SEQ_SCAN(t1@minmax_1) /* matched, failed */ Generated Plan Advice: SEQ_SCAN(t1) NO_GATHER(t1) -J.