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 1uEngf-00DDKh-Jm for pgsql-performance@arkaria.postgresql.org; Tue, 13 May 2025 11:20:41 +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 1uEngd-004sL5-De for pgsql-performance@arkaria.postgresql.org; Tue, 13 May 2025 11:20:39 +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 1uEngd-004sKx-3E for pgsql-performance@lists.postgresql.org; Tue, 13 May 2025 11:20:39 +0000 Received: from mail-lf1-x12a.google.com ([2a00:1450:4864:20::12a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uEnga-001fH2-04 for pgsql-performance@lists.postgresql.org; Tue, 13 May 2025 11:20:38 +0000 Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-549b116321aso6352798e87.3 for ; Tue, 13 May 2025 04:20:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747135235; x=1747740035; 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=QIs6GKUh7yT9nPdIGSq8ntg+enG9D1dAGxUDuxKktKk=; b=DtM9N/gUXx3ANjSghvMA8ldxFQztHQFb9//73w5fo1rNY+uziyv0KtBgwIw7uriu9G Oq/WgscWzPEiP1v+O0yA5FJq/9pwZnzc0yqp5qZY2YVmPHhKto/bVsHhE5DvjGZBJA0S FAC7n32rTumPFhfUZtHplLcTCqanW1Hi3ke0QcqSlbPu7/fmjRVa5NB4Y3dqRNr6/pbU bZZIMdP/aQJvP0sczipskKAsE5c66q6SykIu6r92BG7MhFPR0ar7Z+2zv/aycn7hH3+6 q0BeBOnJfLNAL9xnq5HvuM0MGYwCxcjpOtozIx/TNAWFlSqGAxoAU0IdFil/EVXfzf8Z NLSw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747135235; x=1747740035; h=content-transfer-encoding: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=QIs6GKUh7yT9nPdIGSq8ntg+enG9D1dAGxUDuxKktKk=; b=bL9WbOP+g5zGXtLyj0UN5wqxQbwngqzJ/pA/6JskN5tCgxXlo3oTNrQbZ34O202AkK tGw/Uz7iBXpHCxVWOzo3x9g9FlV8W2qjMNe0pJ05dG3h3x6cGtLxMd1nYJGGQur3O4b4 wQODEEP2x0zCUTX4ccfXHdZMq0l6E1i8/TV6Gfgvq6kBSok2wFsGHMqIqr8PdgXaMSIY /egn/fKlH2kTX6wQJ08Ngx4Sa50KnBpCC6FHwJwcOrNo5TF+wmVL2bl4o551HxuwcSQk tvChqVUbk2UDGCw4KT3BVaAxSvr+rKxqsdy6UheAWKjZyM/s4H7W5vtMelbSHjxgsg7q KCjw== X-Forwarded-Encrypted: i=1; AJvYcCWG7NkbUBcNVDPx+qQUibb/kp6Y9rK+/gyPs9jELMivjExyR52OeQnuAIOsh2CHYvWedsMOzO/u15MId3QKuWXb3w==@lists.postgresql.org X-Gm-Message-State: AOJu0YznYFWyi0iRG2UjSp7Isi6BbMawpVin11dRo4qzLPdBd2yV3Qx+ +fsjbx9PvFfYol0ZBj1NmqIulFVkQKemHNHgqpsUHYWYwns8oeU0knshjj9N7Psk5AdRIEnwlY+ EnMxdrcSnRfIysd5RhvsfmsIwosI= X-Gm-Gg: ASbGnctG4oZPgcq/ewfJ/tPlFCkoLJB6KSOq9Hb1gxiKhn7FyGiW04yy/N0k57gFxZD GQA3VKUyUsCGAN1Ra14PCh1pjVKigiUK+mtIdk43UAzMaz2FBtemkwO8X3KvLwBdW0HhnJZatj7 kGYHhu896lwAjFVGdeCDUEcUmi/oJPWXRaYg== X-Google-Smtp-Source: AGHT+IFfQ5qs1lIKrzTrMX2g8vhassKBXVXW1kCpybVXwZp6TYNBQnbzkfLxTkc4TcxPUjJIFMWWQ74kqKuHS8kQKrA= X-Received: by 2002:a05:651c:19a8:b0:30b:9813:b00c with SMTP id 38308e7fff4ca-326c46a10f7mr63607951fa.27.1747135234916; Tue, 13 May 2025 04:20:34 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Tue, 13 May 2025 07:20:21 -0400 X-Gm-Features: AX0GCFshO4ySsW6QunHuLSn8KISJE9g4JdxXvnwq6-ZqmhygHqtBlOU_IUaW2JA Message-ID: Subject: Re: inefficient/wrong plan cache mode selection for queries with partitioned tables (postgresql 17) To: Maxim Boguk Cc: Andrei Lepikhov , Pgsql Performance 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 Tue, 13 May 2025 at 03:19, Maxim Boguk wrote: > On Mon, May 12, 2025 at 6:01=E2=80=AFPM David Rowley wrote: >> This is just an artifact of the fact that runtime pruning is not factore= d 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 (be= cause in custom plan case - 95-98% time spent in planning not in execution)= . You misunderstood. The choice the planner (or choose_custom_plan) made to use the custom plan can be overridden with SET plan_cache_mode =3D force_generic_plan;, which seems to be what performs better for you, per your example EXPLAIN ANALYZE outputs. > And the problem is that the cost of a custom plan ignores the cost of pla= nning itself (which is like 2x orders of magnitude worse than the cost of r= eal time partition pruning of a generic plan). I started thinking of someth= ing 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. Certainly the cost estimate for planning there is quite crude. I doubt you'll find anyone arguing that it's not. It is however designed to be low-overhead. The estimated planning cost isn't the issue here. It's (as I mentioned) related to no cost consideration being given to run-time pruning. We could certainly adjust things so that is accounted for, and we (I think Robert and I) have talked about it in the past. The problem is that doing that is a wild stab in the dark, especially so for your range partitioned case where the amount of actual partitions pruned during executor startup could range from 0 to all of them. Unfortunately when we tag those costs onto the plan, we've no idea what the parameter values are going to be when the plan is executed. I think Robert suggested multiplying the Append cost by DEFAULT_INEQ_SEL for this bounded range type pruning. Whether that will help you or not depends on how many partitions you have and how evenly populated they are. In order words, it's a tricky problem with no one-size-fits-all solution. David