99 *
1010 *
1111 * IDENTIFICATION
12- * $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.202 2006/03/05 15:58:28 momjian Exp $
12+ * $PostgreSQL: pgsql/src/backend/optimizer/path/indxpath.c,v 1.203 2006/04/08 21:32:17 tgl Exp $
1313 *
1414 *-------------------------------------------------------------------------
1515 */
@@ -253,6 +253,10 @@ find_usable_indexes(PlannerInfo *root, RelOptInfo *rel,
253253List * all_clauses = NIL ;/* not computed till needed */
254254ListCell * ilist ;
255255
256+ /* quick exit if no available clauses */
257+ if (clauses == NIL )
258+ return NIL ;
259+
256260foreach (ilist ,rel -> indexlist )
257261{
258262IndexOptInfo * index = (IndexOptInfo * )lfirst (ilist );
@@ -1370,16 +1374,20 @@ best_inner_indexscan(PlannerInfo *root, RelOptInfo *rel,
13701374}
13711375
13721376/*
1373- * Find all the relevantrestriction and join clauses.
1377+ * Find all the relevant join clauses.
13741378 */
13751379clause_list = find_clauses_for_join (root ,rel ,outer_relids ,isouterjoin );
13761380
13771381/*
13781382 * Find all the index paths that are usable for this join, except for
1379- * stuff involving OR and ScalarArrayOpExpr clauses.
1383+ * stuff involving OR and ScalarArrayOpExpr clauses. We can use both
1384+ * join and restriction clauses as indexquals, but we insist the path
1385+ * use at least one join clause (else it'd not be an "inner indexscan"
1386+ * but a plain indexscan, and those have already been considered).
13801387 */
13811388indexpaths = find_usable_indexes (root ,rel ,
1382- clause_list ,NIL ,
1389+ clause_list ,
1390+ rel -> baserestrictinfo ,
13831391 false, true,
13841392outer_relids ,
13851393SAOP_FORBID );
@@ -1389,7 +1397,8 @@ best_inner_indexscan(PlannerInfo *root, RelOptInfo *rel,
13891397 * clauses present in the clause list.
13901398 */
13911399bitindexpaths = generate_bitmap_or_paths (root ,rel ,
1392- clause_list ,NIL ,
1400+ clause_list ,
1401+ rel -> baserestrictinfo ,
13931402 true,
13941403outer_relids );
13951404
@@ -1439,13 +1448,12 @@ best_inner_indexscan(PlannerInfo *root, RelOptInfo *rel,
14391448
14401449/*
14411450 * find_clauses_for_join
1442- * Generate a list of clauses that are potentially useful for
1451+ * Generate a list ofjoin clauses that are potentially useful for
14431452 * scanning rel as the inner side of a nestloop join.
14441453 *
1445- * We consider both join and restriction clauses. Any joinclause that uses
1446- * only otherrels in the specified outer_relids is fair game. But there must
1447- * be at least one such joinclause in the final list, otherwise we return NIL
1448- * indicating that there isn't any potential win here.
1454+ * Any joinclause that uses only otherrels in the specified outer_relids is
1455+ * fair game. Note that restriction clauses on rel can also be used in
1456+ * forming index conditions, but we do not include those here.
14491457 */
14501458static List *
14511459find_clauses_for_join (PlannerInfo * root ,RelOptInfo * rel ,
@@ -1473,28 +1481,28 @@ find_clauses_for_join(PlannerInfo *root, RelOptInfo *rel,
14731481
14741482bms_free (join_relids );
14751483
1476- /* if no join clause was matched then forget it, per comments above */
1484+ /*quick exit if no join clause was matched */
14771485if (clause_list == NIL )
14781486return NIL ;
14791487
1480- /*
1481- * We can also use any plain restriction clauses for the rel. We put
1482- * these at the front of the clause list for the convenience of
1483- * remove_redundant_join_clauses, which can never remove non-join clauses
1484- * and hence won't be able to get rid of a non-join clause if it appears
1485- * after a join clause it is redundant with.
1486- */
1487- clause_list = list_concat (list_copy (rel -> baserestrictinfo ),clause_list );
1488-
14891488/*
14901489 * We may now have clauses that are known redundant. Get rid of 'em.
14911490 */
14921491if (list_length (clause_list )> 1 )
1493- {
14941492clause_list = remove_redundant_join_clauses (root ,
14951493clause_list ,
14961494isouterjoin );
1497- }
1495+
1496+ /*
1497+ * We might have found join clauses that are known redundant with
1498+ * restriction clauses on rel (due to conclusions drawn by implied
1499+ * equality deduction; without that, this would obviously never happen).
1500+ * Get rid of them too.
1501+ */
1502+ if (rel -> baserestrictinfo != NIL )
1503+ clause_list = select_nonredundant_join_clauses (root ,clause_list ,
1504+ rel -> baserestrictinfo ,
1505+ isouterjoin );
14981506
14991507return clause_list ;
15001508}