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 1viW7J-008YZW-2Q for pgsql-bugs@arkaria.postgresql.org; Wed, 21 Jan 2026 11:11:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1viW7H-006eX9-1s for pgsql-bugs@arkaria.postgresql.org; Wed, 21 Jan 2026 11:11:15 +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.96) (envelope-from ) id 1viW7H-006eX0-0n for pgsql-bugs@lists.postgresql.org; Wed, 21 Jan 2026 11:11:15 +0000 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1viW7F-001hmT-2I for pgsql-bugs@lists.postgresql.org; Wed, 21 Jan 2026 11:11:15 +0000 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-47ee9817a35so38480675e9.1 for ; Wed, 21 Jan 2026 03:11:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768993868; x=1769598668; 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=VuGTSXnZwvOsAsHN9vwmBOCUKyyVfgNmW+ldFn3pXl8=; b=V+TASQRcHWKDf8nuKY4cBmZezwioe7+cRDBjucglHEPPW+N9LKrbjWctD0kN9tEgKU ndCEcnT7LcxQIe9V5FWXAdNCF8EXtLb7ZMR6/v9Xfg8ZaYsV4JCds58WqOUOPsBUK6cy ISG35Pg2zDwibAsiFgj6lSkKU+F/dFi6c5ZQUcpjyxplDYMiQ66L5Be3FezORyIXGsZZ ByiLGENx9bHw9pcnbjkMJ37mJVHx/xomI2ta+KexlFACDgjgcws7Psxq5hqfzF1ZOjbK 8srpfxN6diw27dg+XbDrro11J2NQD+lg9IdWOsXqZf/ue781Wx4uuv+q82zRMDCelfEr fVCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768993868; x=1769598668; 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=VuGTSXnZwvOsAsHN9vwmBOCUKyyVfgNmW+ldFn3pXl8=; b=puvYH+HDd4HARLzu/slWxW+g+ybJfsTYLuunUVFfoFPuS+vIEMoiiNK3sLg55fCAEP 7NEJB5JaJOgI3Uw2BjwTCxiB4evP0kf9TuHif+5ndcPhDt3ROxYmf2JMHqIyHiJxthFN fjRaU+I1/ux9Im6NCM2lOfLvgie6VDXLP7t+PzIVtLFZMTqKAnhisH1OYXVR/dDte6gB /e3du5r1jqKs0+3DOLfpahHPHQ9RclAYZ0LtxE2S4QyWKyckVYlr7tkqScYYZ5ZJFUIP BbEyIKqMeFLee4N3zVJdM3szgsIWGkfaL84usfqrJKeh9je8Uu3l6olZi3dMv9+VT2og 3EWg== X-Forwarded-Encrypted: i=1; AJvYcCUSY7NmpILAB4OLgzVsuk3a9seVJyOgXH5Wy8oYAPOdTh4hRnBFugABrnb5lULBVoWKsUXBr6xt1kWA@lists.postgresql.org X-Gm-Message-State: AOJu0YyCr/Iea6zfPWB13NHHRsvssTfX9VT0QSVKVpP3ju0o3SjU1reG 2os93EOre/Whh7iKuzxK/3V70I5VJNxKqVriRPgnPs1x9pf1xuae2X5a X-Gm-Gg: AZuq6aKq9DaOZA9D2ycukuOQiJWt6obITL2A0kqAnFOiGYazwjXOmMKlId3ZFOHjEOy 6h8qFId/g1xQ53+Y1lKhm7ttlkbySS+6b5mXiPVnOSMyhyCU1Uo9aCttCrHR0gSOMga3t30afcy 2KDy29Gwl0YCFOtIUEwf1yaP8/dQUJ+jVBAzH4+rf0+51i4EKajktoADWd2GF9TVrjvCPMl9/d3 3gm9MQOEYRsjFgndMh1nogYkPh0MV0E+XhsYCqF1hZ//TTzF0j8Lx9IuF3e7b48crpzOUyJ04b1 zdpBuX5I98GorIkW9ZVi7YS7Qrgn4ee0w+SACarrMe5VUQH0pfvWTHHQwDSUwVGEy1+EP+cp04u FuaqN1Elp1YPX8Li7oKE9XAeeAMbbZbTG87jqRi38wVzD9UKGatOpxruNngmDt3eC7evi74oait EDU87sU8LMsl8BB1lxh4eNzM1j+WDc3jIxfs8357+dPZWrb11CbzmMCWDthxrRyA== X-Received: by 2002:a05:600c:4e05:b0:45d:d97c:236c with SMTP id 5b1f17b1804b1-480416867d8mr51322765e9.21.1768993867725; Wed, 21 Jan 2026 03:11:07 -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-4804249be6dsm20128075e9.2.2026.01.21.03.11.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 21 Jan 2026 03:11:07 -0800 (PST) Message-ID: <82e59270-d4a0-4dbb-9c2d-5cd2005c933f@gmail.com> Date: Wed, 21 Jan 2026 12:11:05 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: BUG #19386: Unnecessary Sort in query plan for SELECT literal with ORDER BY To: 798604270@qq.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form References: <19386-be594598921461b9@postgresql.org> Content-Language: en-US From: Andrei Lepikhov In-Reply-To: <19386-be594598921461b9@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 09:26, PG Bug reporting form wrote: > The following bug has been logged on the website: > > Bug reference: 19386 > 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 queries. One is a > normal SELECT, and the other is a prepared SELECT. In the query plan of the > normal SELECT, there is an unnecessary Sort, which causes it to be slower > than the prepared SELECT. In general, the prepared SELECT should be slower > than the normal SELECT, as its query plan is suboptimal. So there maybe > potential opportunities for further optimization in the query planning of > normal SELECT statements. These queries aren't equivalent for me. The generic case may produce errors if a parameter has an incompatible type. The 'simple query' case validates constants and may simplify the clause, being sure no logical errors happen during clause evaluation. Another question - should we do anything to optimise this quite narrow (at least it seems so for me) case and stop simplification of the clause? -- regards, Andrei Lepikhov, pgEdge