[PostGIS] #5984: PostGIS selectivity is screwing up queries and forcing it to choose a spatial index when it's inappropriate

PostGIS trac at osgeo.org
Fri Sep 12 13:32:11 PDT 2025


#5984: PostGIS selectivity is screwing up queries and forcing it to choose a
spatial index when it's inappropriate
-----------------------+---------------------------
  Reporter:  robe      |      Owner:  pramsey
      Type:  defect    |     Status:  new
  Priority:  critical  |  Milestone:  PostGIS 3.4.5
 Component:  postgis   |    Version:  3.5.x
Resolution:            |   Keywords:
-----------------------+---------------------------
Comment (by robe):

 For completeness I installed PostGIS 3.3.9dev on same server I have
 PostGIS 3.6.0 and my results match what Bas provided for the 3.3 case.

 Namely


 {{{
  SELECT _postgis_selectivity ('nodes', 'geom',
 ST_GeomFromEWKT('SRID=4326;POLYGON((6.11982 52.6121955,6.11982
 53.2038323,7.0927397 53.2038323,7.0927397 52.6121955,6.11982
 52.6121955))'), '2');

 }}}

 Yields - 0.07927752284407437

 and the explain plan for both ST_Contains and ST_Intersects are decent,
 don't use the spatial index


 {{{
 EXPLAIN ANALYZE SELECT wn.way_id FROM way_nodes AS wn INNER JOIN nodes AS
 n ON wn.node_id = n.id
         WHERE wn.node_id = 8646506472  AND
 ST_Intersects(ST_GeomFromEWKT('SRID=4326;POLYGON((6.1198199
 52.612195500000006,6.1198199 53.2038323,7.0927397 53.2038323,7.0927397
 52.612195500000006,6.1198199 52.612195500000006))'), n.geom);

 }}}



 {{{
  Nested Loop  (cost=1.14..42.18 rows=1 width=8) (actual time=0.055..0.056
 rows=0 loops=1)
    ->  Index Scan using idx_way_nodes_node_id on way_nodes wn
 (cost=0.57..8.59 rows=1 width=16) (actual time=0.028..0.028 rows=1
 loops=1)
          Index Cond: (node_id = '8646506472'::bigint)
    ->  Index Scan using pk_nodes on nodes n  (cost=0.57..33.59 rows=1
 width=8) (actual time=0.024..0.024 rows=0 loops=1)
          Index Cond: (id = '8646506472'::bigint)
          Filter:
 st_intersects('0103000020E61000000100000005000000D5A35F11B27A184013A1116C5C4E4A40D5A35F11B27A18402A2F432D179A4A40D529EA27F75E1C402A2F432D179A4A40D529EA27F75E1C4013A1116C5C4E4A40
 D5A35F11B27A184013A1116C5C4E4A40'::geometry, geom)
          Rows Removed by Filter: 1
  Planning Time: 2.835 ms
  Execution Time: 0.171 ms
 (9 rows)
 }}}

 Contains is same plan as what he had.
-- 
Ticket URL: <https://trac.osgeo.org/postgis/ticket/5984#comment:7>
PostGIS <http://trac.osgeo.org/postgis/>
The PostGIS Trac is used for bug, enhancement & task tracking, a user and developer wiki, and a view into the subversion code repository of PostGIS project.


More information about the postgis-tickets mailing list