Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e2ed3c4

Browse files
committed
Fix improper uses of canonicalize_qual().
One of the things canonicalize_qual() does is to remove constant-NULL subexpressions of top-level AND/OR clauses. It does that on the assumption that what it's given is a top-level WHERE clause, so that NULL can be treated like FALSE. Although this is documented down inside a subroutine of canonicalize_qual(), it wasn't mentioned in the documentation of that function itself, and some callers hadn't gotten that memo. Notably, commit d007a95 caused get_relation_constraints() to apply canonicalize_qual() to CHECK constraints. That allowed constraint exclusion to misoptimize situations in which a CHECK constraint had a provably-NULL subclause, as seen in the regression test case added here, in which a child table that should be scanned is not. (Although this thinko is ancient, the test case doesn't fail before 9.2, for reasons I've not bothered to track down in detail. There may be related cases that do fail before that.) More recently, commit f0e4475 added an independent bug by applying canonicalize_qual() to index expressions, which is even sillier since those might not even be boolean. If they are, though, I think this could lead to making incorrect index entries for affected index expressions in v10. I haven't attempted to prove that though. To fix, add an "is_check" parameter to canonicalize_qual() to specify whether it should assume WHERE or CHECK semantics, and make it perform NULL-elimination accordingly. Adjust the callers to apply the right semantics, or remove the call entirely in cases where it's not known that the expression has one or the other semantics. I also removed the call in some cases involving partition expressions, where it should be a no-op because such expressions should be canonical already ... and was a no-op, independently of whether it could in principle have done something, because it was being handed the qual in implicit-AND format which isn't what it expects. In HEAD, add an Assert to catch that type of mistake in future. This represents an API break for external callers of canonicalize_qual(). While that's intentional in HEAD to make such callers think about which case applies to them, it seems like something we probably wouldn't be thanked for in released branches. Hence, in released branches, the extra parameter is added to a new function canonicalize_qual_ext(), and canonicalize_qual() is a wrapper that retains its old behavior. Patch by me with suggestions from Dean Rasheed. Back-patch to all supported branches. Discussion: https://postgr.es/m/24475.1520635069@sss.pgh.pa.us
1 parent cccba8b commit e2ed3c4

File tree

9 files changed

+122
-36
lines changed

9 files changed

+122
-36
lines changed

src/backend/commands/tablecmds.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13636,9 +13636,17 @@ ATExecAttachPartition(List **wqueue, Relation rel, PartitionCmd *cmd)
1363613636
partConstraint = list_concat(get_qual_from_partbound(attachrel, rel,
1363713637
cmd->bound),
1363813638
RelationGetPartitionQual(rel));
13639+
13640+
/*
13641+
* Run the partition quals through const-simplification similar to check
13642+
* constraints. We skip canonicalize_qual, though, because partition
13643+
* quals should be in canonical form already; also, since the qual is in
13644+
* implicit-AND format, we'd have to explicitly convert it to explicit-AND
13645+
* format and back again.
13646+
*/
1363913647
partConstraint = (List *) eval_const_expressions(NULL,
1364013648
(Node *) partConstraint);
13641-
partConstraint = (List *) canonicalize_qual((Expr *) partConstraint);
13649+
1364213650
partConstraint = list_make1(make_ands_explicit(partConstraint));
1364313651

1364413652
/*
@@ -13720,14 +13728,12 @@ ATExecAttachPartition(List **wqueue, Relation rel, PartitionCmd *cmd)
1372013728
* to detect valid matches without this.
1372113729
*/
1372213730
cexpr = eval_const_expressions(NULL, cexpr);
13723-
cexpr = (Node *) canonicalize_qual((Expr *) cexpr);
13731+
cexpr = (Node *) canonicalize_qual_ext((Expr *) cexpr, true);
1372413732

1372513733
existConstraint = list_concat(existConstraint,
1372613734
make_ands_implicit((Expr *) cexpr));
1372713735
}
1372813736

13729-
existConstraint = list_make1(make_ands_explicit(existConstraint));
13730-
1373113737
/* And away we go ... */
1373213738
if (predicate_implied_by(partConstraint, existConstraint, true))
1373313739
skip_validate = true;

src/backend/optimizer/plan/planner.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ preprocess_expression(PlannerInfo *root, Node *expr, int kind)
941941
*/
942942
if (kind == EXPRKIND_QUAL)
943943
{
944-
expr = (Node *) canonicalize_qual((Expr *) expr);
944+
expr = (Node *) canonicalize_qual_ext((Expr *) expr, false);
945945

946946
#ifdef OPTIMIZER_DEBUG
947947
printf("After canonicalize_qual()\n");

src/backend/optimizer/plan/subselect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,7 @@ convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
17231723
* subroot.
17241724
*/
17251725
whereClause = eval_const_expressions(root, whereClause);
1726-
whereClause = (Node *) canonicalize_qual((Expr *) whereClause);
1726+
whereClause = (Node *) canonicalize_qual_ext((Expr *) whereClause, false);
17271727
whereClause = (Node *) make_ands_implicit((Expr *) whereClause);
17281728

17291729
/*

src/backend/optimizer/prep/prepqual.c

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
static List *pull_ands(List *andlist);
4141
static List *pull_ors(List *orlist);
42-
static Expr *find_duplicate_ors(Expr *qual);
42+
static Expr *find_duplicate_ors(Expr *qual, bool is_check);
4343
static Expr *process_duplicate_ors(List *orlist);
4444

4545

@@ -269,6 +269,24 @@ negate_clause(Node *node)
269269
* canonicalize_qual
270270
* Convert a qualification expression to the most useful form.
271271
*
272+
* Backwards-compatibility wrapper for use by external code that hasn't
273+
* been updated.
274+
*/
275+
Expr *
276+
canonicalize_qual(Expr *qual)
277+
{
278+
return canonicalize_qual_ext(qual, false);
279+
}
280+
281+
/*
282+
* canonicalize_qual_ext
283+
* Convert a qualification expression to the most useful form.
284+
*
285+
* This is primarily intended to be used on top-level WHERE (or JOIN/ON)
286+
* clauses. It can also be used on top-level CHECK constraints, for which
287+
* pass is_check = true. DO NOT call it on any expression that is not known
288+
* to be one or the other, as it might apply inappropriate simplifications.
289+
*
272290
* The name of this routine is a holdover from a time when it would try to
273291
* force the expression into canonical AND-of-ORs or OR-of-ANDs form.
274292
* Eventually, we recognized that that had more theoretical purity than
@@ -283,7 +301,7 @@ negate_clause(Node *node)
283301
* Returns the modified qualification.
284302
*/
285303
Expr *
286-
canonicalize_qual(Expr *qual)
304+
canonicalize_qual_ext(Expr *qual, bool is_check)
287305
{
288306
Expr *newqual;
289307

@@ -296,7 +314,7 @@ canonicalize_qual(Expr *qual)
296314
* within the top-level AND/OR structure; there's no point in looking
297315
* deeper. Also remove any NULL constants in the top-level structure.
298316
*/
299-
newqual = find_duplicate_ors(qual);
317+
newqual = find_duplicate_ors(qual, is_check);
300318

301319
return newqual;
302320
}
@@ -395,16 +413,17 @@ pull_ors(List *orlist)
395413
* Only the top-level AND/OR structure is searched.
396414
*
397415
* While at it, we remove any NULL constants within the top-level AND/OR
398-
* structure, eg "x OR NULL::boolean" is reduced to "x". In general that
399-
* would change the result, so eval_const_expressions can't do it; but at
400-
* top level of WHERE, we don't need to distinguish between FALSE and NULL
401-
* results, so it's valid to treat NULL::boolean the same as FALSE and then
402-
* simplify AND/OR accordingly.
416+
* structure, eg in a WHERE clause, "x OR NULL::boolean" is reduced to "x".
417+
* In general that would change the result, so eval_const_expressions can't
418+
* do it; but at top level of WHERE, we don't need to distinguish between
419+
* FALSE and NULL results, so it's valid to treat NULL::boolean the same
420+
* as FALSE and then simplify AND/OR accordingly. Conversely, in a top-level
421+
* CHECK constraint, we may treat a NULL the same as TRUE.
403422
*
404423
* Returns the modified qualification. AND/OR flatness is preserved.
405424
*/
406425
static Expr *
407-
find_duplicate_ors(Expr *qual)
426+
find_duplicate_ors(Expr *qual, bool is_check)
408427
{
409428
if (or_clause((Node *) qual))
410429
{
@@ -416,18 +435,29 @@ find_duplicate_ors(Expr *qual)
416435
{
417436
Expr *arg = (Expr *) lfirst(temp);
418437

419-
arg = find_duplicate_ors(arg);
438+
arg = find_duplicate_ors(arg, is_check);
420439

421440
/* Get rid of any constant inputs */
422441
if (arg && IsA(arg, Const))
423442
{
424443
Const *carg = (Const *) arg;
425444

426-
/* Drop constant FALSE or NULL */
427-
if (carg->constisnull || !DatumGetBool(carg->constvalue))
428-
continue;
429-
/* constant TRUE, so OR reduces to TRUE */
430-
return arg;
445+
if (is_check)
446+
{
447+
/* Within OR in CHECK, drop constant FALSE */
448+
if (!carg->constisnull && !DatumGetBool(carg->constvalue))
449+
continue;
450+
/* Constant TRUE or NULL, so OR reduces to TRUE */
451+
return (Expr *) makeBoolConst(true, false);
452+
}
453+
else
454+
{
455+
/* Within OR in WHERE, drop constant FALSE or NULL */
456+
if (carg->constisnull || !DatumGetBool(carg->constvalue))
457+
continue;
458+
/* Constant TRUE, so OR reduces to TRUE */
459+
return arg;
460+
}
431461
}
432462

433463
orlist = lappend(orlist, arg);
@@ -449,18 +479,29 @@ find_duplicate_ors(Expr *qual)
449479
{
450480
Expr *arg = (Expr *) lfirst(temp);
451481

452-
arg = find_duplicate_ors(arg);
482+
arg = find_duplicate_ors(arg, is_check);
453483

454484
/* Get rid of any constant inputs */
455485
if (arg && IsA(arg, Const))
456486
{
457487
Const *carg = (Const *) arg;
458488

459-
/* Drop constant TRUE */
460-
if (!carg->constisnull && DatumGetBool(carg->constvalue))
461-
continue;
462-
/* constant FALSE or NULL, so AND reduces to FALSE */
463-
return (Expr *) makeBoolConst(false, false);
489+
if (is_check)
490+
{
491+
/* Within AND in CHECK, drop constant TRUE or NULL */
492+
if (carg->constisnull || DatumGetBool(carg->constvalue))
493+
continue;
494+
/* Constant FALSE, so AND reduces to FALSE */
495+
return arg;
496+
}
497+
else
498+
{
499+
/* Within AND in WHERE, drop constant TRUE */
500+
if (!carg->constisnull && DatumGetBool(carg->constvalue))
501+
continue;
502+
/* Constant FALSE or NULL, so AND reduces to FALSE */
503+
return (Expr *) makeBoolConst(false, false);
504+
}
464505
}
465506

466507
andlist = lappend(andlist, arg);

src/backend/optimizer/util/plancat.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ get_relation_constraints(PlannerInfo *root,
11871187
*/
11881188
cexpr = eval_const_expressions(root, cexpr);
11891189

1190-
cexpr = (Node *) canonicalize_qual((Expr *) cexpr);
1190+
cexpr = (Node *) canonicalize_qual_ext((Expr *) cexpr, true);
11911191

11921192
/* Fix Vars to have the desired varno */
11931193
if (varno != 1)
@@ -1240,11 +1240,13 @@ get_relation_constraints(PlannerInfo *root,
12401240
if (pcqual)
12411241
{
12421242
/*
1243-
* Run each expression through const-simplification and
1244-
* canonicalization similar to check constraints.
1243+
* Run the partition quals through const-simplification similar to
1244+
* check constraints. We skip canonicalize_qual, though, because
1245+
* partition quals should be in canonical form already; also, since
1246+
* the qual is in implicit-AND format, we'd have to explicitly convert
1247+
* it to explicit-AND format and back again.
12451248
*/
12461249
pcqual = (List *) eval_const_expressions(root, (Node *) pcqual);
1247-
pcqual = (List *) canonicalize_qual((Expr *) pcqual);
12481250

12491251
/* Fix Vars to have the desired varno */
12501252
if (varno != 1)

src/backend/utils/cache/relcache.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,8 @@ RelationBuildPartitionKey(Relation relation)
893893
* will be comparing them to similarly-processed qual clause operands,
894894
* and may fail to detect valid matches without this step. We don't
895895
* need to bother with canonicalize_qual() though, because partition
896-
* expressions are not full-fledged qualification clauses.
896+
* expressions should be in canonical form already (ie, no need for
897+
* OR-merging or constant elimination).
897898
*/
898899
expr = eval_const_expressions(NULL, (Node *) expr);
899900

@@ -4742,12 +4743,11 @@ RelationGetIndexExpressions(Relation relation)
47424743
* Run the expressions through eval_const_expressions. This is not just an
47434744
* optimization, but is necessary, because the planner will be comparing
47444745
* them to similarly-processed qual clauses, and may fail to detect valid
4745-
* matches without this. We don't bother with canonicalize_qual, however.
4746+
* matches without this. We must not use canonicalize_qual, however,
4747+
* since these aren't qual expressions.
47464748
*/
47474749
result = (List *) eval_const_expressions(NULL, (Node *) result);
47484750

4749-
result = (List *) canonicalize_qual((Expr *) result);
4750-
47514751
/* May as well fix opfuncids too */
47524752
fix_opfuncids((Node *) result);
47534753

@@ -4812,7 +4812,7 @@ RelationGetIndexPredicate(Relation relation)
48124812
*/
48134813
result = (List *) eval_const_expressions(NULL, (Node *) result);
48144814

4815-
result = (List *) canonicalize_qual((Expr *) result);
4815+
result = (List *) canonicalize_qual_ext((Expr *) result, false);
48164816

48174817
/* Also convert to implicit-AND format */
48184818
result = make_ands_implicit((Expr *) result);

src/include/optimizer/prep.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extern Relids get_relids_for_join(PlannerInfo *root, int joinrelid);
3434
*/
3535
extern Node *negate_clause(Node *node);
3636
extern Expr *canonicalize_qual(Expr *qual);
37+
extern Expr *canonicalize_qual_ext(Expr *qual, bool is_check);
3738

3839
/*
3940
* prototypes for preptlist.c

src/test/regress/expected/inherit.out

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,30 @@ reset enable_seqscan;
16371637
reset enable_indexscan;
16381638
reset enable_bitmapscan;
16391639
--
1640+
-- Check handling of a constant-null CHECK constraint
1641+
--
1642+
create table cnullparent (f1 int);
1643+
create table cnullchild (check (f1 = 1 or f1 = null)) inherits(cnullparent);
1644+
insert into cnullchild values(1);
1645+
insert into cnullchild values(2);
1646+
insert into cnullchild values(null);
1647+
select * from cnullparent;
1648+
f1
1649+
----
1650+
1
1651+
2
1652+
1653+
(3 rows)
1654+
1655+
select * from cnullparent where f1 = 2;
1656+
f1
1657+
----
1658+
2
1659+
(1 row)
1660+
1661+
drop table cnullparent cascade;
1662+
NOTICE: drop cascades to table cnullchild
1663+
--
16401664
-- Check that constraint exclusion works correctly with partitions using
16411665
-- implicit constraints generated from the partition bound information.
16421666
--

src/test/regress/sql/inherit.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,18 @@ reset enable_seqscan;
592592
reset enable_indexscan;
593593
reset enable_bitmapscan;
594594

595+
--
596+
-- Check handling of a constant-null CHECK constraint
597+
--
598+
create table cnullparent (f1 int);
599+
create table cnullchild (check (f1 = 1 or f1 = null)) inherits(cnullparent);
600+
insert into cnullchild values(1);
601+
insert into cnullchild values(2);
602+
insert into cnullchild values(null);
603+
select * from cnullparent;
604+
select * from cnullparent where f1 = 2;
605+
drop table cnullparent cascade;
606+
595607
--
596608
-- Check that constraint exclusion works correctly with partitions using
597609
-- implicit constraints generated from the partition bound information.

0 commit comments

Comments
 (0)