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 1viaAg-00AMky-2p for pgsql-bugs@arkaria.postgresql.org; Wed, 21 Jan 2026 15:31:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1viaAf-007p8X-37 for pgsql-bugs@arkaria.postgresql.org; Wed, 21 Jan 2026 15:31:02 +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 1viaAf-007p8O-2J for pgsql-bugs@lists.postgresql.org; Wed, 21 Jan 2026 15:31:02 +0000 Received: from mail-wm1-x32c.google.com ([2a00:1450:4864:20::32c]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1viaAd-001c5f-2N for pgsql-bugs@lists.postgresql.org; Wed, 21 Jan 2026 15:31:01 +0000 Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-47ee0291921so48520385e9.3 for ; Wed, 21 Jan 2026 07:31:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769009459; x=1769614259; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:content-language:references :to:from:subject:user-agent:mime-version:date:message-id:from:to:cc :subject:date:message-id:reply-to; bh=Ulqt/Eb7F5JpROp6yAE7+o6WUIgfci6ecJYV9WK6RG0=; b=Tdm5hOnpTaajIhjmpMALFbqoV1t/a4rFnqtAesr1i6DpxYMTXZ+FeOTR4KD1s2bmzU RGqa5zTQy790syo3n3eluHmMMywc022m9+A2+yn2alvVtFXopum6sh4VvB5xU8SXxyzV wQW727ths+Y8yyhSLqL7YrCJhQ+lQnIQ9yeeLNI/sCUUp2HjX9XgV+ayh79AXV6HdN9E l/aZME9o6t+OjftBqn/7SCPPAAsrmw8BX3vGFj0YKelsVZeUBKDnZQ+jHN8kOBrQi/po HNZQo/1wm1ZYu6s7sN24mGKlqboQcZJFDkQ2TMi5vfApXRUoasTkBcuuMlMTLY3x8sIu fveg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769009459; x=1769614259; h=content-transfer-encoding:in-reply-to:content-language:references :to:from:subject:user-agent:mime-version:date:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Ulqt/Eb7F5JpROp6yAE7+o6WUIgfci6ecJYV9WK6RG0=; b=iUC8mpFmuEN4v2ZnHkAzW+mbvrrwUQsQXludjSR01elIGd3obGLw2EoxwAYWAgWh5r WE/8ZuwWjHIk9eBZEjyAOeNz8paEGcydBhd/0/K06S0Fe5OaAwAjoTXPHduQWEDxiT3V Rf6aav0qxDSWxKvmP9aqwlBOot9w7q3YH8zyD6HGiUDjq1ZSfKEiXxcRz9/IfxPwQ43R 73dyqjyatAhHne845lMw0meTv+wAvsWS8yWzDFqYM2ryukhMMqxrII39aHlAquFj/Nkm 5yvEv56TYSKZFu46ybmbLLg9RclU0XGV2NjMJiMDPEzl0CAzcOm6uYbLu/V6Rskc+HUP g9MA== X-Forwarded-Encrypted: i=1; AJvYcCWSeiV7HtM53Y8DOznwzaoDH2wrbVOwln8oqcYOUzH0R1xvHVj7H06/bSA3MRNbharaRCKK7w0kaFbO@lists.postgresql.org X-Gm-Message-State: AOJu0YzNotKOXGLwtzLjj69z3XdubXpRnQA0neJoM2n3meE2HtG97uod D6KOooYC8JoXVp+j6/y8OABL7p9jkppiV7SZlnUSSSGBhjzQr3Z+OTDf X-Gm-Gg: AZuq6aJkySh0BYlbO7/NWfKviFkl01oMLxg+1EV3quWDYG9eEekrBBClRBxlw/xrH31 gv/w/EDOfSg4pKbZmJzXqkj9Db8EaVZR2kPwfUXYNMWEGpQfFNFaMIPHDmckSMP+XoYai0l+f11 BgciBoqkwxPuz+OaUp4U4KGASzRbrWxnmB57/0tw/Th4xVIitskGSP1D/8Kz4/tHYbc1PExHSk+ lAZpzdjdDePhN8x2J1XO3gtq6NwvTd/R3O54Zn2bTuzAuSwVjJOxrscbqVHF7IEzX39YDkVbJyQ zkg1DZaxowYLSTehGiaqxGdRtEAgCgYEqyFfiX/luoFtsf1RVq19Tz8oyubszhnG4m2Um7w45G8 eQg7IaZH8IpeeCtSwJYXaVP684Ae4aDfJmk9e9HxnpxCvQZdP0PfvvMX7eNEMER7e6KHwx96jVG rPSKKqHS8SbR6LpBS5RO+ge7kg94e/wU79vR0PR7l7AbCdb2ZHxTd1tyaRvc6gJeZYsuDZt2FJ X-Received: by 2002:a05:600c:a009:b0:475:dd9a:f791 with SMTP id 5b1f17b1804b1-4801eb0ef0bmr208893855e9.28.1769009458904; Wed, 21 Jan 2026 07:30:58 -0800 (PST) Received: from [192.168.0.86] (84.123.225.106.dyn.user.ono.com. [84.123.225.106]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47f3b7a5f94sm399073355e9.0.2026.01.21.07.30.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 21 Jan 2026 07:30:58 -0800 (PST) Message-ID: <71c18447-5545-401d-b1aa-4f73ae8cb5aa@gmail.com> Date: Wed, 21 Jan 2026 16:30:57 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: BUG #19385: Normal SELECT generates an ineffecifient query plan compare to the prepared SELECT. From: Andrei Lepikhov To: 798604270@qq.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form References: <19385-b384f1bd063ec84a@postgresql.org> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 21/1/26 12:57, Andrei Lepikhov wrote: > On 21/1/26 07:53, PG Bug reporting form wrote: >> The following bug has been logged on the website: >> >> Bug reference:      19385 >> Logged by:          Chi Zhang >> Email address:      798604270@qq.com >> PostgreSQL version: 18.1 >> Operating system:   ubuntu 24.04 with docker >> Description: >> >> Hi, >> >> 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. > There is no equivalence. > > In the 'simple query' case with such a big LIMIT, you force the planner > to choose a full-scan-optimal path. In the generic case, the planner > uses 'magic constants' to provide some glue during planning, and LIMIT > 10 is one of them, as we see from your example. > > I once proposed an alternative solution: use a 'reference' value to > create a generic plan, as SQL Server does. If you're really interested > in such optimisation, support it! > That is [1] the reference to the patch that allows you to choose specific constants when building a generic plan. [1] https://www.postgresql.org/message-id/flat/19919494-92a8-4905-a250-6cf17b89f7c3@gmail.com -- regards, Andrei Lepikhov, pgEdge