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 1vijY7-00E8BD-0U for pgsql-bugs@arkaria.postgresql.org; Thu, 22 Jan 2026 01:31:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vijY6-00AASi-0m for pgsql-bugs@arkaria.postgresql.org; Thu, 22 Jan 2026 01:31:50 +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 1vijY5-00AASa-38 for pgsql-bugs@lists.postgresql.org; Thu, 22 Jan 2026 01:31:50 +0000 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vijY3-001gat-2b for pgsql-bugs@lists.postgresql.org; Thu, 22 Jan 2026 01:31:49 +0000 Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-47ee4539adfso4253555e9.3 for ; Wed, 21 Jan 2026 17:31:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1769045507; cv=none; d=google.com; s=arc-20240605; b=IAlEX0Q8VAuY6iezVxcXWW+SKj446TFUlVzbyXpBlMJ5Fi2TwgnB+o6r9JCRDLwBrA vs09JZ/HZgxir6PPCD3Kca6Hp89ryT/s2Fqf0HUvNow6WjHtVi/D1Fg4Ipp2Oh0euqA/ HNwBwJWKY54miCEdg4Cs83ycCREajBYyS0nnY0DqnGhHO/887saSCwirt3jaYzA1nOyV AwlB3sXabq2ei6JvzmbeYKPXIRDXrHvhqNm5Ks8Y21tWCkfpGQPiMMqqR5zTjcNcmUnU Uv3LPt+Jm77s+6+C2MjR8a1wE1GhH0SZIvPY41qK/bsPC3L01cVFy4NEjTtkG3TzMBtS 2ejQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=GoBJEaAdIxaDGR02CxGgzS67o+1jbZfiFKOfwG8EoaU=; fh=0R5WW1kunRhR/ZYtrSkbmDuMk6xamWG0cvG8k6E+AWI=; b=M2KxeF0e6Le3A2a5/0oQKwbF5SS5lLjAGODkpZf/lPP1LLRJVixFAeCBkr9k9ewFXT Cebv2xBETZ3tkayyHa7iWkNERW123X1ThpUDGfpWDUuKYphUxrSjrrp6d/t2ei2UOeC7 BFJ5I6abuP7ZnAVAL52k2uzmx4XPSRvhAUgyxt/+gq8JW0aEHGk/MvFZqgYkOO6lSNtC aAm8KgCLRMTvhODSRRrMOmphDKGqWkEzcz0sfnlr26uxdpapEwo2SKS0HARqze+5G83v JGcSXwYGg/czZUY1VQZBMPlaV9td9uk+lN7Lt9Jn7cRJXMC9BA8pGAKxob3+FoQAOcUt KExg==; 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=1769045507; x=1769650307; darn=lists.postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=GoBJEaAdIxaDGR02CxGgzS67o+1jbZfiFKOfwG8EoaU=; b=Cqp0Z7/x8TtVLc5rT0sG6MkkcsCnw/VSOnpcTlstMkzMEfn1R4TnDj/BKFlcWzemcr JSqmls1ElJdDls9pkjNM3UrqxZ+yBhHRPHOtNrlY9j4sBsdC8UyjNNvTkGbJObNtxNli oh3UPLMKnZm04GpbdQZS2TUn7kIjvtB27XtSb2P5x8YCt51t1W90qdS0YeYgbLppkRES vT6v01Odw1ik/HIzL7NvfOd4b4tqpcCqA+eRcfbnDS/cuflnkVYu+JO9kcT8qAyamVAJ t2q4h9IDCg5BIiF8miQWoiFffa6JXzzK/RZFa9XTUBjBZgpRIwEE5oicpIur3n1AxJVy HJTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769045507; x=1769650307; h=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=GoBJEaAdIxaDGR02CxGgzS67o+1jbZfiFKOfwG8EoaU=; b=TQytNi6RuE3s8bqthDXGgYJsEwiBpbDei9Cycw7vu2U0vIEIAiZQKZhlhXqMGjIcGK Gu4IJIteLGjleMD5Cd6Uyqx0n7YwOn6erqL4vHhDG+FDsBxkaQK7iqhQM9QpnWTlRYSm /r45Rmq1aMJwgYF6taho8v5E7CNq4gGkDvMUHxKoDQimHmpvH3WsU2R55oQdsbOwLqqP QEtQeRbDxpQSzxGfJHF5G6bcepxwYgDcfVu5uxVYHmnsTZVhA84vq3i/KBHm+cgGXYv+ AQm3WIfuLJD3zYdajQJ+mj1EwRMA1s8nIDQdkZ5NSHAbEk36ebQoT+H+WlWaTO1oY8Fb FBVw== X-Forwarded-Encrypted: i=1; AJvYcCWCH0M0cP/R1TtrYC0Sf9kGb7QqaaVH41g4a2UhlQEdO6gOMcD1gI4zer7oEp4n2ayIsmD9JAfd8QZj@lists.postgresql.org X-Gm-Message-State: AOJu0YxbLRT/386qOO3INC3Ytsscyy3VbKMx+ZtSk39wwbV+15OUjndY muuTwXaYB85t9iXpa0GLi1A+fOeSM9/6CSMZl86jxUkdtrjuPZzxgS29do+nIjn30MtSuf3vk2Y +AgiEfQD0mbMf+jAg9yeWNBv+KHUH7IxEZScA X-Gm-Gg: AZuq6aLI7fKBCQqUy3Y4t6xONE828EapNLqQtiteIvY0D2HVFmgMKmL1uKI76bwae0d 61ronBd2m8L3U1BZe0vduVuH1379KQjOO7W1xkM/LtXXo9w5LFzogIFg6tmefPchbjxfxthbGar mDNPIrh7/AXOh9Oft/Q/eNCM7KBEskfQ5zQMYz45o1fYJ1Nby38Ho+N+EmeHfLvsLoN30c8fNs+ Zv+2fkNFBquXe8LwW59m3k2b//wyCyVUGp1AcXiVpzVSQi3FFIi/vKx70PHpA8NdDChTr/TVtpv bbb3AqJyXV99i3nFYZnkmmtpCFyoxzF0yO4fGv5XOQ4CvGUozGeMdooAKUr+sg== X-Received: by 2002:a05:6000:186f:b0:42b:38b1:e32e with SMTP id ffacd0b85a97d-4358ff6265dmr12411172f8f.46.1769045506852; Wed, 21 Jan 2026 17:31:46 -0800 (PST) MIME-Version: 1.0 References: <19385-b384f1bd063ec84a@postgresql.org> In-Reply-To: <19385-b384f1bd063ec84a@postgresql.org> From: David Rowley Date: Thu, 22 Jan 2026 14:31:35 +1300 X-Gm-Features: AZwV_QjbXwoFCh9CS9hyFD0yUSvp5yRbxQ1KZ4A0UmXtYxb_j6XawH_ENO2gje4 Message-ID: Subject: Re: BUG #19385: Normal SELECT generates an ineffecifient query plan compare to the prepared SELECT. To: 798604270@qq.com, pgsql-bugs@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 21 Jan 2026 at 23:44, PG Bug reporting form wrote: > In the following test case, there are two equivalent simple SELECTs with > DISTINCT, however, the normal SELECT is slower than the prepared SELECT. > Given that prepared SELECT statements typically generate suboptimal query > plans due to the presence of unknown literals, one would expect prepared > SELECT to be slower than normal SELECT. However, in this example, the > prepared SELECT executes faster, suggesting that there may still be room for > optimization in the query plan generation for normal SELECT. Ultimately, the SeqScan -> NestLoop -> Hash Agg plan is only winning over the Index Only Scan -> NestLoop -> Unique due to the planner's estimated costs for the Nested Loop's inner scan. If you find the cost balance between Seq Scan vs Index [Only] Scan isn't accurate for your hardware, then adjust random_page_cost. The planner not choosing the fastest to execute plan all the time does not constitute a bug. You may want to consult the documentation in [1]. [2] may also be useful to you. David [1] https://www.postgresql.org/docs/18/runtime-config-query.html#GUC-RANDOM-PAGE-COST [2] https://www.postgresql.org/docs/18/runtime-config-query.html#GUC-EFFECTIVE-CACHE-SIZE