Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kqRaR-0008Gd-8C for pgsql-performance@arkaria.postgresql.org; Sat, 19 Dec 2020 02:03:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kqRaQ-0006P1-5C for pgsql-performance@arkaria.postgresql.org; Sat, 19 Dec 2020 02:03:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kqRaP-0006Ou-Uj for pgsql-performance@lists.postgresql.org; Sat, 19 Dec 2020 02:03:09 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kqRaJ-0000BG-4Z for pgsql-performance@postgresql.org; Sat, 19 Dec 2020 02:03:09 +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 0BJ22xuc226300; Fri, 18 Dec 2020 21:02:59 -0500 From: Tom Lane To: Ken Tanzer cc: pgsql-performance Subject: Re: Reversing NULLS in ORDER causes index not to be used? In-reply-to: References: Comments: In-reply-to Ken Tanzer message dated "Fri, 18 Dec 2020 17:53:03 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <226298.1608343379.1@sss.pgh.pa.us> Date: Fri, 18 Dec 2020 21:02:59 -0500 Message-ID: <226299.1608343379@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Ken Tanzer writes: > Hi. I'm wondering if this is normal or at least known behavior? > Basically, if I'm specifying a LIMIT and also NULLS FIRST (or NULLS LAST > with a descending sort), I get a sequence scan and a couple of orders of > magnitude slower query. Perhaps not relevantly, but definitely ironically, > the sort field in question is defined to be NOT NULL. The index won't get credit for matching the requested ordering if it's got the wrong null-ordering polarity. There's not an exception for NOT NULL columns. If you know the column hasn't got nulls, why are you bothering with a nondefault null-ordering request? regards, tom lane