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.94.2) (envelope-from ) id 1r87BW-008ICn-1l for pgsql-general@arkaria.postgresql.org; Tue, 28 Nov 2023 23:08:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1r87BT-007noR-Ey for pgsql-general@arkaria.postgresql.org; Tue, 28 Nov 2023 23:08:03 +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.94.2) (envelope-from ) id 1r87BT-007noJ-4F for pgsql-general@lists.postgresql.org; Tue, 28 Nov 2023 23:08:03 +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.94.2) (envelope-from ) id 1r87BQ-007qBT-2v for pgsql-general@lists.postgresql.org; Tue, 28 Nov 2023 23:08:01 +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 3ASN7vXY186559; Tue, 28 Nov 2023 18:07:57 -0500 From: Tom Lane To: Owen Nelson cc: pgsql-general@lists.postgresql.org Subject: Re: Understanding partial index selection In-reply-to: References: Comments: In-reply-to Owen Nelson message dated "Tue, 28 Nov 2023 14:23:17 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <186557.1701212877.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 28 Nov 2023 18:07:57 -0500 Message-ID: <186558.1701212877@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Owen Nelson writes: > The hope is the sub-select would leverage the index > "message_payload_not_null_pidx" but when I `EXPLAIN ANALYZE` the query, = I > see a seq scan instead. I think your problem is the horrid rowcount misestimation here: > -> Seq Scan on message message_1 (cost=3D= 0.00..2050554.24 rows=3D6175068 width=3D38) (actual time=3D8424.977..20880= .945 rows=3D65 loops=3D1) > Filter: ((payload IS NOT NULL) AND= (expiration <=3D now())) > Rows Removed by Filter: 37772897 The planner seems to think that about a sixth of the table satisfies the filter condition, which is way more than enough to discourage it from using an indexscan. If it had gotten an estimate within even one or two orders of magnitude of the reality of 65 rows, it'd have gone for an indexscan, I'm pretty sure. Are your ANALYZE stats up to date on this table? If so, there must be some strong correlation between the payload and expiration conditions that the planner doesn't know about. Perhaps creating extended statistics on those two columns would help. regards, tom lane