Postgres doesn't remove useless join when using partial unique index - Mailing list pgsql-hackers

From Kim Rose Carlsen
Subject Postgres doesn't remove useless join when using partial unique index
Date
Msg-id AM6PR05MB549116AC38C800FB4AD29DBFC79E0@AM6PR05MB5491.eurprd05.prod.outlook.com
Whole thread Raw
Responses Re: Postgres doesn't remove useless join when using partial unique index  (David Rowley <david.rowley@2ndquadrant.com>)
List pgsql-hackers
Hi

remove_useless_join does not prove uniqueness if the unique index is partial, and therefore wont remove the join if no columns are referenced (see example in bottom).

I have been trying to look around the source code and from what I have identified the problem seems to be that "check_index_predicates(..)" happens after "remove_useless_join(..)", and therefore cannot see that the unique index is actually covered by the join condition.

From analyzejoins.c:612, rel_supports_distinctness(..)
  if (ind->unique && ind->immediate &&
   (ind->indpred == NIL || ind->predOK))
   return true;

But the problem is ind->predOK is calculated in check_index_predicates(..) but this happens later so ind->predOK is always false when checked here.

I have tried to add check_index_predicates(..) to rel_supports_distinctness(..) and this produces the expected plan, but I have no idea of the implication of doing check_index_predicates(..) earlier.

This is my first time looking at the postgres source code, so I know attached "patch" is not the solution, but any pointers on where to go from here would be appreciated.


Example:
CREATE TABLE a (
  id         INTEGER PRIMARY KEY,
  sub_id     INTEGER NOT NULL,
  deleted_at TIMESTAMP
);
CREATE UNIQUE INDEX ON a (sub_id) WHERE (deleted_at IS NULL);

ANALYZE a;

EXPLAIN SELECT 1 FROM a AS a LEFT JOIN a AS b ON a.id = b.sub_id AND b.deleted_at IS NULL;

Expected plan:
                     QUERY PLAN                      
-----------------------------------------------------
 Seq Scan on a  (cost=0.00..28.50 rows=1850 width=4)

Actual plan:
                                      QUERY PLAN                                      
---------------------------------------------------------------------------------------
 Hash Left Join  (cost=14.76..48.13 rows=1850 width=4)
   Hash Cond: (a.id = b.sub_id)
   ->  Seq Scan on a  (cost=0.00..28.50 rows=1850 width=4)
   ->  Hash  (cost=14.65..14.65 rows=9 width=4)
         ->  Bitmap Heap Scan on a b  (cost=4.13..14.65 rows=9 width=4)
               Recheck Cond: (deleted_at IS NULL)
               ->  Bitmap Index Scan on a_sub_id_idx  (cost=0.00..4.13 rows=9 width=0)
(7 rows)


Attachment

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: PSA: we lack TAP test coverage on NetBSD and OpenBSD
Next
From: David Rowley
Date:
Subject: Re: Delay locking partitions during INSERT and UPDATE