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 1vWD6e-00FE5u-0H for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Dec 2025 12:27:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vWD6d-0029MW-0I for pgsql-hackers@arkaria.postgresql.org; Thu, 18 Dec 2025 12:27:43 +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 1vWD6c-0029MK-2M for pgsql-hackers@lists.postgresql.org; Thu, 18 Dec 2025 12:27:43 +0000 Received: from mail-lj1-x234.google.com ([2a00:1450:4864:20::234]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vWD6a-001PLc-1X for pgsql-hackers@lists.postgresql.org; Thu, 18 Dec 2025 12:27:42 +0000 Received: by mail-lj1-x234.google.com with SMTP id 38308e7fff4ca-37b9d8122fdso4212431fa.3 for ; Thu, 18 Dec 2025 04:27:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1766060859; x=1766665659; 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=L4W4e71vXIrtZaxmNv5WTUyFwFLGNv+x9nIFgcUKHg4=; b=GRjdEyEiOCF9Gh5kvTHvGt00iv6AZAIFxbuTb7KjANnzavBhmOm89uu2hz+tw8oylf gV621iSBL03YHbjh0RFPdASZOVxuZdJ9vuKZ06TY6ZraXMSDJHbW8CYOaGqi1hREmWIA nCR9y7AOhUxoi0TL0tTYbof93nOFOBr2377yqM1/2S/h1ovQZkL/V4e1MQUdIXkFn8sh FPO1JxMY4MSYUDAZOwI6IeBwdJsqKepPpPnY1HX/iIuDYf3+vU7kN/rHaq1ZdWx5xr9R Km6z20559RyrwmoCaBbiuBAOna9tzuqRf3PYAF7HF5wEY4Qq+3kdFXMptTSKAn86V6ac ONJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766060859; x=1766665659; 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=L4W4e71vXIrtZaxmNv5WTUyFwFLGNv+x9nIFgcUKHg4=; b=hT8vDagCiHh7Hp7Vik6yQkvxKgqm8/l0pMPuAo5XPCkEN1X5WVWZy6+2IwGHwHuZHX E35brXOX0OMddabSDL+xGbJs+A5A7edaiI6onp9X7UamyQwxwCR2J8WUNd6nc1Mw/6OU fQINh635+ip4rnRD4RCon2PhObuySZcl6JknN9k+pe4EqgmMeLTjipdrNbK4jI2G60Mk w1XKGD9M4gR5trt3gf3kbwQINoX3mtvxQOcXFum9hLZon3jQGkqdleP/ALIIE7ipocvM EJ06CuNge9mvHZL6dr/8+kgV4Lc9ZW3Tc1zA2mV/GfmmzJkbUSek5GXm4AVERmU4xIVe TqOA== X-Forwarded-Encrypted: i=1; AJvYcCWeG3BgDQE+lbl/kbnF1lpiKYj/QZC8tRh1nT9tCQAsqo4TGw2Ihlo9oPE05NwSRvqLBAmvd4S8N4WZa1aC@lists.postgresql.org X-Gm-Message-State: AOJu0YyvdPRCFap5ikUKyWqiU6A1BNaGOEkg0Kz6h0HhvRcd3dl6NWI1 exJmtK1HJr3JWpZXi+1bPyQzKpwPxzH++5Nr/rzvyjwcCx2J5J6grcjngK3wq7NqTQYF7G5pfk4 klp9hriDsWiwnUK9BiQ3K6M/I8ra5fmyuynojUDZc X-Gm-Gg: AY/fxX6RjVwxneqa0LDRcfTL51VoLXfCwQYiGVCUrrCBD3Fg+V8WFJHd7sGh0/ycuJH IFn8JD2IYxdklFWdOsOZoGVZ6pgSkyln2/qH3F4bAx0fzUla7RFBFRruNBil8HoR2Z8jiLl+rMS lXPj4xO6LThHHgKVK7hnmjL9503WgnVx/+AmB8MYArs6EiJYYb9VYSucxDAv/D4nxUvCke7TqEX lAahN73XfMbpSM04XaWqixrNmGb6Nj6acc5LsLLno8sOcL6u8uMdOpWqT5DP71bRGFQfcMpqIUZ jvsvLQ== X-Google-Smtp-Source: AGHT+IHqAJE988vRqQr8PMuBCywb8LPydk+FzsVKi/Ag3gHzO0/1mb4FaFwDetB+yZBTT6bNz6clsrTUEwiQC+DJfWo= X-Received: by 2002:a05:651c:154a:b0:37f:bca3:bd5d with SMTP id 38308e7fff4ca-37fd1e54028mr65488921fa.1.1766060858553; Thu, 18 Dec 2025 04:27:38 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Jakub Wartak Date: Thu, 18 Dec 2025 13:27:27 +0100 X-Gm-Features: AQt7F2pWAK7NV4FZaDQtYksc1gNOyDoqfFMPwRPYAF4xzVLvuHEvEu69YFIy58k Message-ID: Subject: Re: pg_plan_advice To: Robert Haas Cc: 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, Dec 17, 2025 at 2:44=E2=80=AFPM Jakub Wartak wrote: > > On Wed, Dec 17, 2025 at 11:12=E2=80=AFAM Jakub Wartak > wrote: > > > > On Mon, Dec 15, 2025 at 9:06=E2=80=AFPM Robert Haas wrote: > > > > > > Here's v7. > > [..] > >[..q20..] > > OK, now for the q10: Hi, this is a follow-up just to the q10. > So to me it looks like in Generated Plan Advice we: > - have proper HASH_JOIN(customer nation) > - but it somehow forgot to include "HASH_JOIN(orders)" to cover for > that Parallel Hash Join on (orders.o_custkey =3D customer.c_custkey) > with input from NL. After adding that manually, it achieves the same > input plan properly. [..] Well, it's quite a ride with the Q10 and I partially wrong with above: 0. The reported earlier wrong missing "HASH_JOIN(orders customer)" - that part was okay 1. The Incremental Sort is being used in the original plan, but is still IS not reflected in the generated advice. 2a. I've noticed Memoize/Index Scan was not being respected for "nation" 2b. Seq scan for nation was being done for "nation" So total modification list, I've ended up doing (+ for adding , - for remov= ing): + HASH_JOIN(orders customer) -- from earlier reply + NESTED_LOOP_MEMOIZE(nation) + INDEX_SCAN(nation public.pk_nation) - HASH_JOIN(customer nation) -- as it was we were having NL() in org plan SEQ_SCAN(orders customer nation) =3D=3D> SEQ_SCAN(orders customer) In full the best shape seems to be Q10 with pg_plan_advice.advice =3D 'HASH_JOIN(orders customer) JOIN_ORDER(orders lineitem customer nation) NESTED_LOOP_PLAIN(lineitem) SEQ_SCAN(orders customer) INDEX_SCAN(lineitem public.lineitem_l_orderkey_idx_l_returnflag) GATHER_MERGE((customer orders lineitem nation)) NESTED_LOOP_MEMOIZE(nation)'; which yields: Sort Sort Key: (sum((lineitem.l_extendedprice * ('1'::numeric - lineitem.l_discount)))) DESC -> GroupAggregate Group Key: customer.c_custkey, nation.n_name -> Gather Merge Workers Planned: 2 -> Sort Sort Key: customer.c_custkey, nation.n_name -> Nested Loop -> Parallel Hash Join Hash Cond: (orders.o_custkey =3D customer.c_custkey) -> Nested Loop -> Parallel Seq Scan on orders Filter: ((o_orderdate >=3D '1993-10-01'::date) AND (o_orderdate < '1994-01-01 00:00:00'::timestamp without time zone)) -> Index Scan using lineitem_l_orderkey_idx_l_returnflag on lineitem Index Cond: (l_orderkey =3D orders.o_orderkey) -> Parallel Hash -> Parallel Seq Scan on customer -> Memoize Cache Key: customer.c_nationkey Cache Mode: logical -> Index Scan using pk_nation on nation Index Cond: (n_nationkey =3D customer.c_nationkey) but that Incremental Sort *is* still missing. In original plan we are doing Incremental Sort (Sort Key: customer.c_custkey, nation.n_name, Presorted Key: customer.c_custkey) <-- .... Sort(Sort Key: customer.c_custkey) However, even with my overrides I haven't found an immediately obvious way to force it to use Incremental Sort on a specific field, so it just sorts on two at once. Maybe it's something that should be expressed through GATHER_MERGE()?, but that's not obvious how and where. In terms of raw performance , it seems to be very similiar (98ms +/- 8ms even between those two). -J.