From 79a1b00226354e2c7f1bf5c1db90661b1f5a4148 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 25 Apr 2005 02:14:48 +0000 Subject: [PATCH] Replace slightly klugy create_bitmap_restriction() function with a more efficient routine in restrictinfo.c (which can make use of make_restrictinfo_internal). --- src/backend/optimizer/path/orindxpath.c | 5 +- src/backend/optimizer/plan/createplan.c | 87 +--------------------- src/backend/optimizer/util/restrictinfo.c | 89 ++++++++++++++++++++++- src/include/optimizer/planmain.h | 3 +- src/include/optimizer/restrictinfo.h | 5 +- 5 files changed, 99 insertions(+), 90 deletions(-) diff --git a/src/backend/optimizer/path/orindxpath.c b/src/backend/optimizer/path/orindxpath.c index fb055400d4..5ea528ef26 100644 --- a/src/backend/optimizer/path/orindxpath.c +++ b/src/backend/optimizer/path/orindxpath.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/optimizer/path/orindxpath.c,v 1.69 2005/04/25 01:30:13 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/optimizer/path/orindxpath.c,v 1.70 2005/04/25 02:14:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -146,7 +146,8 @@ create_or_index_quals(Query *root, RelOptInfo *rel) * Convert the path's indexclauses structure to a RestrictInfo tree, * and add it to the rel's restriction list. */ - newrinfos = create_bitmap_restriction((Path *) bestpath); + newrinfos = make_restrictinfo_from_bitmapqual((Path *) bestpath, + true, true); Assert(list_length(newrinfos) == 1); or_rinfo = (RestrictInfo *) linitial(newrinfos); Assert(IsA(or_rinfo, RestrictInfo)); diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c index 0ce65a93d1..8c06278f8f 100644 --- a/src/backend/optimizer/plan/createplan.c +++ b/src/backend/optimizer/plan/createplan.c @@ -10,7 +10,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.185 2005/04/25 01:30:13 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.186 2005/04/25 02:14:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -54,7 +54,6 @@ static BitmapHeapScan *create_bitmap_scan_plan(Query *root, List *tlist, List *scan_clauses); static Plan *create_bitmap_subplan(Query *root, Path *bitmapqual, List **qual, List **indexqual); -static List *create_bitmap_qual(Path *bitmapqual); static TidScan *create_tidscan_plan(Query *root, TidPath *best_path, List *tlist, List *scan_clauses); static SubqueryScan *create_subqueryscan_plan(Query *root, Path *best_path, @@ -981,86 +980,6 @@ create_bitmap_subplan(Query *root, Path *bitmapqual, return plan; } -/* - * Given a bitmapqual tree, generate the equivalent ordinary expression tree - * (which we need for the bitmapqualorig field of the BitmapHeapScan plan). - * The result is expressed as an implicit-AND list. - */ -static List * -create_bitmap_qual(Path *bitmapqual) -{ - List *result; - List *sublist; - - if (IsA(bitmapqual, BitmapAndPath)) - { - BitmapAndPath *apath = (BitmapAndPath *) bitmapqual; - ListCell *l; - - result = NIL; - foreach(l, apath->bitmapquals) - { - sublist = create_bitmap_qual(lfirst(l)); - result = list_concat(result, sublist); - } - } - else if (IsA(bitmapqual, BitmapOrPath)) - { - BitmapOrPath *opath = (BitmapOrPath *) bitmapqual; - List *newlist = NIL; - ListCell *l; - - foreach(l, opath->bitmapquals) - { - sublist = create_bitmap_qual(lfirst(l)); - if (sublist == NIL) - { - /* constant TRUE input yields constant TRUE OR result */ - return NIL; - } - newlist = lappend(newlist, make_ands_explicit(sublist)); - } - result = list_make1(make_orclause(newlist)); - } - else if (IsA(bitmapqual, IndexPath)) - { - IndexPath *ipath = (IndexPath *) bitmapqual; - - result = get_actual_clauses(ipath->indexclauses); - } - else - { - elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual)); - result = NIL; /* keep compiler quiet */ - } - - return result; -} - -/* - * Given a bitmapqual tree, generate the equivalent RestrictInfo list. - */ -List * -create_bitmap_restriction(Path *bitmapqual) -{ - List *bitmapquals; - List *bitmapclauses; - ListCell *l; - - bitmapquals = create_bitmap_qual(bitmapqual); - - /* must convert qual list to restrictinfos ... painful ... */ - bitmapclauses = NIL; - foreach(l, bitmapquals) - { - bitmapclauses = lappend(bitmapclauses, - make_restrictinfo((Expr *) lfirst(l), - true, true)); - } - - return bitmapclauses; -} - /* * create_tidscan_plan * Returns a tidscan plan for the base relation scanned by 'best_path' @@ -1210,7 +1129,9 @@ create_nestloop_plan(Query *root, { List *bitmapclauses; - bitmapclauses = create_bitmap_restriction(innerpath->bitmapqual); + bitmapclauses = + make_restrictinfo_from_bitmapqual(innerpath->bitmapqual, + true, true); joinrestrictclauses = select_nonredundant_join_clauses(root, joinrestrictclauses, diff --git a/src/backend/optimizer/util/restrictinfo.c b/src/backend/optimizer/util/restrictinfo.c index edf268019a..e19a308cdf 100644 --- a/src/backend/optimizer/util/restrictinfo.c +++ b/src/backend/optimizer/util/restrictinfo.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/optimizer/util/restrictinfo.c,v 1.34 2005/04/25 01:30:13 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/optimizer/util/restrictinfo.c,v 1.35 2005/04/25 02:14:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -65,10 +65,95 @@ make_restrictinfo(Expr *clause, bool is_pushed_down, bool valid_everywhere) is_pushed_down, valid_everywhere); } +/* + * make_restrictinfo_from_bitmapqual + * + * Given the bitmapqual Path structure for a bitmap indexscan, generate + * RestrictInfo node(s) equivalent to the condition represented by the + * indexclauses of the Path structure. + * + * The result is a List since we might need to return multiple RestrictInfos. + * + * To do this through the normal make_restrictinfo() API, callers would have + * to strip off the RestrictInfo nodes present in the indexclauses lists, and + * then make_restrictinfo() would have to build new ones. It's better to have + * a specialized routine to allow sharing of RestrictInfos. + */ +List * +make_restrictinfo_from_bitmapqual(Path *bitmapqual, + bool is_pushed_down, + bool valid_everywhere) +{ + List *result; + + if (IsA(bitmapqual, BitmapAndPath)) + { + BitmapAndPath *apath = (BitmapAndPath *) bitmapqual; + ListCell *l; + + result = NIL; + foreach(l, apath->bitmapquals) + { + List *sublist; + + sublist = make_restrictinfo_from_bitmapqual((Path *) lfirst(l), + is_pushed_down, + valid_everywhere); + result = list_concat(result, sublist); + } + } + else if (IsA(bitmapqual, BitmapOrPath)) + { + BitmapOrPath *opath = (BitmapOrPath *) bitmapqual; + List *withris = NIL; + List *withoutris = NIL; + ListCell *l; + + foreach(l, opath->bitmapquals) + { + List *sublist; + + sublist = make_restrictinfo_from_bitmapqual((Path *) lfirst(l), + is_pushed_down, + valid_everywhere); + if (sublist == NIL) + { + /* constant TRUE input yields constant TRUE OR result */ + /* (though this probably cannot happen) */ + return NIL; + } + /* Create AND subclause with RestrictInfos */ + withris = lappend(withris, make_ands_explicit(sublist)); + /* And one without */ + sublist = get_actual_clauses(sublist); + withoutris = lappend(withoutris, make_ands_explicit(sublist)); + } + /* Here's the magic part not available to outside callers */ + result = + list_make1(make_restrictinfo_internal(make_orclause(withoutris), + make_orclause(withris), + is_pushed_down, + valid_everywhere)); + } + else if (IsA(bitmapqual, IndexPath)) + { + IndexPath *ipath = (IndexPath *) bitmapqual; + + result = list_copy(ipath->indexclauses); + } + else + { + elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual)); + result = NIL; /* keep compiler quiet */ + } + + return result; +} + /* * make_restrictinfo_internal * - * Common code for the main entry point and the recursive cases. + * Common code for the main entry points and the recursive cases. */ static RestrictInfo * make_restrictinfo_internal(Expr *clause, Expr *orclause, diff --git a/src/include/optimizer/planmain.h b/src/include/optimizer/planmain.h index c98838a2b2..129439ddec 100644 --- a/src/include/optimizer/planmain.h +++ b/src/include/optimizer/planmain.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/optimizer/planmain.h,v 1.83 2005/04/25 01:30:14 tgl Exp $ + * $PostgreSQL: pgsql/src/include/optimizer/planmain.h,v 1.84 2005/04/25 02:14:48 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -33,7 +33,6 @@ extern Plan *optimize_minmax_aggregates(Query *root, List *tlist, * prototypes for plan/createplan.c */ extern Plan *create_plan(Query *root, Path *best_path); -extern List *create_bitmap_restriction(Path *bitmapqual); extern SubqueryScan *make_subqueryscan(List *qptlist, List *qpqual, Index scanrelid, Plan *subplan); extern Append *make_append(List *appendplans, bool isTarget, List *tlist); diff --git a/src/include/optimizer/restrictinfo.h b/src/include/optimizer/restrictinfo.h index 5c47a0d00b..3760a65669 100644 --- a/src/include/optimizer/restrictinfo.h +++ b/src/include/optimizer/restrictinfo.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/optimizer/restrictinfo.h,v 1.28 2005/04/25 01:30:14 tgl Exp $ + * $PostgreSQL: pgsql/src/include/optimizer/restrictinfo.h,v 1.29 2005/04/25 02:14:48 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -18,6 +18,9 @@ extern RestrictInfo *make_restrictinfo(Expr *clause, bool is_pushed_down, bool valid_everywhere); +extern List *make_restrictinfo_from_bitmapqual(Path *bitmapqual, + bool is_pushed_down, + bool valid_everywhere); extern bool restriction_is_or_clause(RestrictInfo *restrictinfo); extern List *get_actual_clauses(List *restrictinfo_list); extern void get_actual_join_clauses(List *restrictinfo_list, -- 2.40.0