mirror of
https://github.com/postgres/postgres.git
synced 2025-12-19 17:02:53 +03:00
Add better handling of redundant IS [NOT] NULL quals
Until now PostgreSQL has not been very smart about optimizing away IS NOT NULL base quals on columns defined as NOT NULL. The evaluation of these needless quals adds overhead. Ordinarily, anyone who came complaining about that would likely just have been told to not include the qual in their query if it's not required. However, a recent bug report indicates this might not always be possible. Bug 17540 highlighted that when we optimize Min/Max aggregates the IS NOT NULL qual that the planner adds to make the rewritten plan ignore NULLs can cause issues with poor index choice. That particular case demonstrated that other quals, especially ones where no statistics are available to allow the planner a chance at estimating an approximate selectivity for can result in poor index choice due to cheap startup paths being prefered with LIMIT 1. Here we take generic approach to fixing this by having the planner check for NOT NULL columns and just have the planner remove these quals (when they're not needed) for all queries, not just when optimizing Min/Max aggregates. Additionally, here we also detect IS NULL quals on a NOT NULL column and transform that into a gating qual so that we don't have to perform the scan at all. This also works for join relations when the Var is not nullable by any outer join. This also helps with the self-join removal work as it must replace strict join quals with IS NOT NULL quals to ensure equivalence with the original query. Author: David Rowley, Richard Guo, Andy Fan Reviewed-by: Richard Guo, David Rowley Discussion: https://postgr.es/m/CAApHDvqg6XZDhYRPz0zgOcevSMo0d3vxA9DvHrZtKfqO30WTnw@mail.gmail.com Discussion: https://postgr.es/m/17540-7aa1855ad5ec18b4%40postgresql.org
This commit is contained in:
@@ -913,6 +913,8 @@ typedef struct RelOptInfo
|
||||
Relids *attr_needed pg_node_attr(read_write_ignore);
|
||||
/* array indexed [min_attr .. max_attr] */
|
||||
int32 *attr_widths pg_node_attr(read_write_ignore);
|
||||
/* zero-based set containing attnums of NOT NULL columns */
|
||||
Bitmapset *notnullattnums;
|
||||
/* relids of outer joins that can null this baserel */
|
||||
Relids nulling_relids;
|
||||
/* LATERAL Vars and PHVs referenced by rel */
|
||||
@@ -2598,7 +2600,10 @@ typedef struct RestrictInfo
|
||||
* 2. If we manufacture a commuted version of a qual to use as an index
|
||||
* condition, it copies the original's rinfo_serial, since it is in
|
||||
* practice the same condition.
|
||||
* 3. RestrictInfos made for a child relation copy their parent's
|
||||
* 3. If we reduce a qual to constant-FALSE, the new constant-FALSE qual
|
||||
* copies the original's rinfo_serial, since it is in practice the same
|
||||
* condition.
|
||||
* 4. RestrictInfos made for a child relation copy their parent's
|
||||
* rinfo_serial. Likewise, when an EquivalenceClass makes a derived
|
||||
* equality clause for a child relation, it copies the rinfo_serial of
|
||||
* the matching equality clause for the parent. This allows detection
|
||||
|
||||
Reference in New Issue
Block a user