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 1viWpr-008tAB-08 for pgsql-bugs@arkaria.postgresql.org; Wed, 21 Jan 2026 11:57:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1viWpp-0071HH-39 for pgsql-bugs@arkaria.postgresql.org; Wed, 21 Jan 2026 11:57:18 +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 1viWpp-0071H9-2H for pgsql-bugs@lists.postgresql.org; Wed, 21 Jan 2026 11:57:18 +0000 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1viWpn-001aWI-0x for pgsql-bugs@lists.postgresql.org; Wed, 21 Jan 2026 11:57:16 +0000 Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-4801bbbdb4aso5950745e9.1 for ; Wed, 21 Jan 2026 03:57:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768996634; x=1769601434; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=MWvpxSLDFEKhccadHy04p+6pEq3FBDWaFCRSFwSPdbo=; b=XYg60uh5Wl6scf3L4v5KdecMEYree3dGwqPKowmtTuGPo4wKvJ4eWwgiZONbK819+k 18R1RxHDdgwMgwKm9k+wBuHbRdIfv5gl1io20mx/56kSAn5As87zP6UVzmgcvc2gkx8E uFoowTvf5VxDN1HfA5H4NfKw4AfVkK9tNAoDdZcR9GeGMdtvET2Y791mqVuBTy07mLeC UJQpIKWfKWMPW5MDTGCQ8oylY8NDVKYy2NPBtCgOrzyghXFTl3jw5WKyRdYARHyS0cP0 AaVYzKFdmKlPiYnPf6bNNAfFBBgurhQWx10SAHz3mc3Yq+VoZ6OTa8dHtIiiK0Mo+Wlq SlJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768996634; x=1769601434; h=content-transfer-encoding:in-reply-to:from:content-language :references:to: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=MWvpxSLDFEKhccadHy04p+6pEq3FBDWaFCRSFwSPdbo=; b=Zv25gb5z866K0UGw68yFUTFm/8VccH6iLt5gr/InMK5+jW2LiaHgSgP558qPEv8nWu w31m1QpEjocSkFkpB+TYvBK9+BkyNCRIdtZNFFPDPxEHA35np3jywXrJCLMEr06zWNGG AheCpruwC4SGvk/jd8D+3GMOuC5EIeeUPar8vr0k4KPyrWpoLgpEXaXD17TD/hW6KPw+ btTWO+drwYbNrBGNGtcMkWksbkCNPfHYAnxkaHyEqxW7t9ketdOzhgvGxpa0gCteXTAg KqgDz4PjrewEualKMQ6o7ES4aMeIBdXnkO42AZ+Dxcvkv/fv9DfKBZzmS0KqXKTqf0Ty hNAw== X-Forwarded-Encrypted: i=1; AJvYcCUXT+LB+V/mq3ELMCEfeluj3QXyU1RWfM35QF9Zlhnn3eyFEBWt3J2ctOsJhwgoDCAYomPQRNg5eB8o@lists.postgresql.org X-Gm-Message-State: AOJu0Yx6VPVhor2svyBxI9pLtD+cGF7Rj9UEDl6VxrvYbJrv4+EwO3QS mSzm+p8wOBiC5c1gkWIz17rPcZThYoZHac3MsBl9Zb607LU1EhXsK2yR+bTToXnb X-Gm-Gg: AZuq6aKFHHS23O+UEix/RRAEV2rK/D285i7tg8pGFqYnNouqUTsldkI81ZWSbo0a3JY 6kzcKB8stujWW3I4j6ZnH1B/PbBwLu/Qqjj/LUIRi6JObyClaObkFhNiRLwHk8lIXccgcPvG2qM Qi4Q3ah9KWcDeTyVeh6GvUfUjG2XqptXSsXRjaBKgxui83+r4znG5OzSCzcaG1fBTmZDjhNYmpi ehPoOoiNlEuCEmcsYlkNSwVZsvP3FI4aHu2gQ47K34KjVU+SpwX5gCAu71Agp+cYpcYd6tvpbWy WX7XXWDL1cbXc2F9R0q+1uTOeN8AMMER9+XHpLpUXvu39FcCYlmXYAJ0L2HHcOKgTB42kulC5N5 goofJeqcgw1owGsi0pqgsy/dCg4IB4aaehfus/EmBG8E8cFz0QzBlui/M5WDTQqyaHGvYbHQydi VeS0sBO2asvkvvNb7o7sFOCf9YbAJSpyGXmyUjF0bCS2dDgB+zpbtLzWFrqMF2qQ== X-Received: by 2002:a05:600c:3e08:b0:480:1c1c:47d6 with SMTP id 5b1f17b1804b1-4801e53c069mr220520675e9.6.1768996634532; Wed, 21 Jan 2026 03:57:14 -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-47f4b267661sm368913195e9.13.2026.01.21.03.57.13 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 21 Jan 2026 03:57:14 -0800 (PST) Message-ID: Date: Wed, 21 Jan 2026 12:57:13 +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. To: 798604270@qq.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form References: <19385-b384f1bd063ec84a@postgresql.org> Content-Language: en-US From: Andrei Lepikhov In-Reply-To: <19385-b384f1bd063ec84a@postgresql.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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! -- regards, Andrei Lepikhov, pgEdge