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 1uEUw9-008z3h-AR for pgsql-performance@arkaria.postgresql.org; Mon, 12 May 2025 15:19:25 +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 1uEUw8-00GtDB-Cy for pgsql-performance@arkaria.postgresql.org; Mon, 12 May 2025 15:19:24 +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 1uEUw8-00GtD3-1b for pgsql-performance@lists.postgresql.org; Mon, 12 May 2025 15:19:24 +0000 Received: from mail-vs1-xe2b.google.com ([2607:f8b0:4864:20::e2b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uEUw5-001Wqo-18 for pgsql-performance@lists.postgresql.org; Mon, 12 May 2025 15:19:23 +0000 Received: by mail-vs1-xe2b.google.com with SMTP id ada2fe7eead31-4ddb0fc22a8so1725950137.1 for ; Mon, 12 May 2025 08:19:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747063159; x=1747667959; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=msapX23IX7v+PyDJXioMFTuvTrIql+saCmofmsah+7w=; b=ZeSgJ3uZCt1hTjFkeINXEEEdPiVmSe/al0AwtO8F8QctGNmYaJTveEa9vhyGSv9ztF F4cYsN25rIvTxFOaZL2H5/R52Tai19I38lCaek2V8L2xdA5Lj7tyZumsnjS3UE+7eZYj TPbHPPaWb6CWtviEOm923RmGLtYl2Ve099898jrMssEsuPQCUIUB9TCM1sDY2nvxhdZP ab2Sb8F8tetbYA3DVf4M/tm8WowPFu5OGTxZFfmcdUllblQZIuzddGDzbz9pgTaW2YP2 Tl5xmessPQ8anrc2YB1C2VQEM08du06phHIj8wXQnqaadUCYk4e1slWIbLJkdBUwmAYH 809w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747063159; x=1747667959; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=msapX23IX7v+PyDJXioMFTuvTrIql+saCmofmsah+7w=; b=AW2Dqw97+5oUOBjbjjgbO7d0CRQDleVaHwTNBwfWweq1G1YRsMpWdla3nlo0bbvScj eP+ZedvdeYV9EKwdcZ7iHc7FZpf2SBJUOgmK8B/sCxnhpQhmnhRE5Rtadb2w4srXlq5h 6gdc1Sc3G2xT4GoVT6fMhEI1GLj+pAsIvtslwUYE181Um5KAwoVcmyoyNVWSIWN5HNU8 8qmxOjmylai+1h9T3v0FI8lk2ic/wMMRqbW8OW2nX7+Gl7nsIllHkunqMdWprM03j0Bf UqDhu1v1rjIexN2ddiKpSGgXMf2rkUeqKxzD1fQrbUDNNwXrC4iiWq812UJKtM2sFMwu e0AA== X-Forwarded-Encrypted: i=1; AJvYcCV4XZMeuN9EeMQe8UMOfS3imdBtBtjm7LJih7PV2i7LzdWQqS3aQTTgz8qjfJ2DptPVXh7vBkgRBGnnVQnV5xgOAQ==@lists.postgresql.org X-Gm-Message-State: AOJu0Yy4SigLGJtYKmiPS+8Wk+waL7EczAPPvSuuF7OK13jCsWI5aRjG u2b/nZ55By9WalRuCmd0ZloiiGSzVWX7Dk0nmgd86sAvMcgcNWTi2EUR4v01KDFa4BvzMmHVeH8 fSTAqOWd39HqspyZNhBDd2tgL/BLar0gn X-Gm-Gg: ASbGnctGt/l0K64r1nxyxNfH07CuhwF5Xi2pwv8GHBawyBmDVFdUs7xy3yZPM9mvaVr RjcptG+B2BCpuSex4rdTpkRTn00ZoSeLcJd8puvNoBIALEntDL8ky13TPJnnKohVcBMAbjRqhKW 86OJZM47uddXcAbZDnubN+vdh8ywwiEu/Ztf3J4nB/TqDt5yqll+Om463pnj4= X-Google-Smtp-Source: AGHT+IENWBftjxjCuTwVs8dAze9ZgbDjrGUt6sNYOu5r4FIY2Ogp93ckOAkNcwDQ0siwcS3MQJbZqAL7nin3qYv7nrU= X-Received: by 2002:a05:6122:20a0:b0:516:18cd:c1fc with SMTP id 71dfb90a1353d-52c53d477eemr9455552e0c.8.1747063159170; Mon, 12 May 2025 08:19:19 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Maxim Boguk Date: Mon, 12 May 2025 18:18:43 +0300 X-Gm-Features: AX0GCFuaTm1ckKn00lg3kZxIDmgemJ4UN9HTtR3I9Q1UG0aElnBWwUPMmy2CWS8 Message-ID: Subject: Re: inefficient/wrong plan cache mode selection for queries with partitioned tables (postgresql 17) To: David Rowley Cc: Andrei Lepikhov , Pgsql Performance Content-Type: multipart/alternative; boundary="00000000000000af0a0634f1d7dc" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000000af0a0634f1d7dc Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, May 12, 2025 at 6:01=E2=80=AFPM David Rowley = wrote: > On Mon, 12 May 2025, 05:08 Andrei Lepikhov, wrote: > >> Thanks for this puzzle! >> I suppose, in case generic planning is much faster than custom one, >> there are two candidates exist: >> 1. Touching the index during planning causes too much overhead - see >> get_actual_variable_range >> 2. You have a massive default_statistics_target for a table involved. >> > > This is just an artifact of the fact that runtime pruning is not factored > into the costs. Note the cost of the generic plan. The plan_cache_mode GU= C > is about the only way to overrule the choice to use the custom plan. > Situation quite the opposite - I need to force a generic plan because it has the same execution time as a custom plan but performs 20-50x faster (because in custom plan case - 95-98% time spent in planning not in execution). And the problem is that the cost of a custom plan ignores the cost of planning itself (which is like 2x orders of magnitude worse than the cost of real time partition pruning of a generic plan). I started thinking of something like cost_planner GUC to help with similar issues (where planning cost calculated as cost_planned*(some heuristic function with amount involved in query tables). In my case the high cost of planning itself should force the database to use generic plan. --=20 Maxim Boguk Senior Postgresql DBA Phone UA: +380 99 143 0000 Phone AU: +61 45 218 5678 --00000000000000af0a0634f1d7dc Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On= Mon, May 12, 2025 at 6:01=E2=80=AFPM David Rowley <dgrowleyml@gmail.com> wrote:
On M= on, 12 May 2025, 05:08 Andrei Lepikhov, <lepihov@gmail.com> wrote:
Thanks for this puzzle!
I suppose, in case generic planning is much faster than custom one,
there are two candidates exist:
1. Touching the index during planning causes too much overhead - see
get_actual_variable_range
2. You have a massive default_statistics_target for a table involved.

This i= s just an artifact of the fact that runtime pruning is not factored into th= e costs. Note the cost of the generic plan. The plan_cache_mode GUC is abou= t the only way to overrule the choice to use the custom plan.

Situation quite the opposite - I need= to force a generic plan because it has the same execution time as a custom= plan but performs 20-50x faster (because in custom plan case - 95-98% time= spent in planning not in execution).

And the problem is that the cost of a custom plan ignores the cost o= f planning itself (which is like 2x orders of magnitude worse than the cost= of real time partition pruning of a generic plan). I started thinking of s= omething like cost_planner GUC to help with similar issues (where planning = cost calculated as cost_planned*(some heuristic function with amount involv= ed in query tables).

In my case the = high cost of planning itself should force the database to use generic plan.=

-= -
Maxim Boguk
Senior Postgresql DBA

Pho= ne UA: +380 99 143 0000
Phone AU: +61=C2=A0 45 218 5678

--00000000000000af0a0634f1d7dc--