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 1vo1Z5-00EnaP-0A for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Feb 2026 15:46:43 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vo1Z4-0003uX-0U for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Feb 2026 15:46:41 +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 1vo1Z3-0003uP-2o for pgsql-hackers@lists.postgresql.org; Thu, 05 Feb 2026 15:46:41 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vo1Z1-00000000ha9-44s7 for pgsql-hackers@postgresql.org; Thu, 05 Feb 2026 15:46:40 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 615Fkbua1615619; Thu, 5 Feb 2026 10:46:37 -0500 From: Tom Lane To: Mayrom Rabinovich cc: pgsql-hackers@postgresql.org Subject: Re: Planing edge case for sorts with limit on non null column In-reply-to: References: Comments: In-reply-to Mayrom Rabinovich message dated "Thu, 05 Feb 2026 13:23:51 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1615617.1770306397.1@sss.pgh.pa.us> Date: Thu, 05 Feb 2026 10:46:37 -0500 Message-ID: <1615618.1770306397@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Mayrom Rabinovich writes: > -- same deal query by the reverse order of the index, but also specify the > wrong null order > -- from my understanding this should not matter because we don't have any > nulls on the table > -- due to the constraint. No, that is not taken into account. The planner's notion of a concrete sort order always includes a nulls first/last flag, and this index doesn't match what the query asks for. If you want this query to use an index you'll need to make an index that puts nulls at the other end (either ASC NULLS FIRST or DESC NULLS LAST will do). I'm not really excited about poking holes in the PathKey concept to make this work the way you want. I think the odds of introducing bugs would be high. Also, the question could be turned around: if you know that the table contains no nulls, why are you going out of your way to specify the "wrong" null order? regards, tom lane