Skip to content

Commit e556fb1

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 6d30e3a commit e556fb1

File tree

8 files changed

+105
-26
lines changed

8 files changed

+105
-26
lines changed

src/backend/optimizer/plan/planner.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ preprocess_expression(PlannerInfo *root, Node *expr, int kind)
679679
*/
680680
if (kind == EXPRKIND_QUAL)
681681
{
682-
expr = (Node *) canonicalize_qual((Expr *) expr);
682+
expr = (Node *) canonicalize_qual_ext((Expr *) expr, false);
683683

684684
#ifdef OPTIMIZER_DEBUG
685685
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
@@ -1568,7 +1568,7 @@ convert_EXISTS_to_ANY(PlannerInfo *root, Query *subselect,
15681568
* subroot.
15691569
*/
15701570
whereClause = eval_const_expressions(root, whereClause);
1571-
whereClause = (Node *) canonicalize_qual((Expr *) whereClause);
1571+
whereClause = (Node *) canonicalize_qual_ext((Expr *) whereClause, false);
15721572
whereClause = (Node *) make_ands_implicit((Expr *) whereClause);
15731573

15741574
/*

src/backend/optimizer/prep/prepqual.c

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

4141
static List *pull_ands(List *andlist);
4242
static List *pull_ors(List *orlist);
43-
static Expr *find_duplicate_ors(Expr *qual);
43+
static Expr *find_duplicate_ors(Expr *qual, bool is_check);
4444
static Expr *process_duplicate_ors(List *orlist);
4545

4646

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

@@ -295,7 +313,7 @@ canonicalize_qual(Expr *qual)
295313
* within the top-level AND/OR structure; there's no point in looking
296314
* deeper. Also remove any NULL constants in the top-level structure.
297315
*/
298-
newqual = find_duplicate_ors(qual);
316+
newqual = find_duplicate_ors(qual, is_check);
299317

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

418-
arg = find_duplicate_ors(arg);
437+
arg = find_duplicate_ors(arg, is_check);
419438

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

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

432462
orlist = lappend(orlist, arg);
@@ -448,18 +478,29 @@ find_duplicate_ors(Expr *qual)
448478
{
449479
Expr *arg = (Expr *) lfirst(temp);
450480

451-
arg = find_duplicate_ors(arg);
481+
arg = find_duplicate_ors(arg, is_check);
452482

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

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

465506
andlist = lappend(andlist, arg);

src/backend/optimizer/util/plancat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ get_relation_constraints(PlannerInfo *root,
685685
*/
686686
cexpr = eval_const_expressions(root, cexpr);
687687

688-
cexpr = (Node *) canonicalize_qual((Expr *) cexpr);
688+
cexpr = (Node *) canonicalize_qual_ext((Expr *) cexpr, true);
689689

690690
/* Fix Vars to have the desired varno */
691691
if (varno != 1)

src/backend/utils/cache/relcache.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3954,7 +3954,8 @@ RelationGetIndexExpressions(Relation relation)
39543954
* Run the expressions through eval_const_expressions. This is not just an
39553955
* optimization, but is necessary, because the planner will be comparing
39563956
* them to similarly-processed qual clauses, and may fail to detect valid
3957-
* matches without this. We don't bother with canonicalize_qual, however.
3957+
* matches without this. We must not use canonicalize_qual, however,
3958+
* since these aren't qual expressions.
39583959
*/
39593960
result = (List *) eval_const_expressions(NULL, (Node *) result);
39603961

@@ -4022,7 +4023,7 @@ RelationGetIndexPredicate(Relation relation)
40224023
*/
40234024
result = (List *) eval_const_expressions(NULL, (Node *) result);
40244025

4025-
result = (List *) canonicalize_qual((Expr *) result);
4026+
result = (List *) canonicalize_qual_ext((Expr *) result, false);
40264027

40274028
/* Also convert to implicit-AND format */
40284029
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 prepsecurity.c

src/test/regress/expected/inherit.out

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,3 +1591,27 @@ FROM generate_series(1, 3) g(i);
15911591
reset enable_seqscan;
15921592
reset enable_indexscan;
15931593
reset enable_bitmapscan;
1594+
--
1595+
-- Check handling of a constant-null CHECK constraint
1596+
--
1597+
create table cnullparent (f1 int);
1598+
create table cnullchild (check (f1 = 1 or f1 = null)) inherits(cnullparent);
1599+
insert into cnullchild values(1);
1600+
insert into cnullchild values(2);
1601+
insert into cnullchild values(null);
1602+
select * from cnullparent;
1603+
f1
1604+
----
1605+
1
1606+
2
1607+
1608+
(3 rows)
1609+
1610+
select * from cnullparent where f1 = 2;
1611+
f1
1612+
----
1613+
2
1614+
(1 row)
1615+
1616+
drop table cnullparent cascade;
1617+
NOTICE: drop cascades to table cnullchild

src/test/regress/sql/inherit.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,15 @@ FROM generate_series(1, 3) g(i);
562562
reset enable_seqscan;
563563
reset enable_indexscan;
564564
reset enable_bitmapscan;
565+
566+
--
567+
-- Check handling of a constant-null CHECK constraint
568+
--
569+
create table cnullparent (f1 int);
570+
create table cnullchild (check (f1 = 1 or f1 = null)) inherits(cnullparent);
571+
insert into cnullchild values(1);
572+
insert into cnullchild values(2);
573+
insert into cnullchild values(null);
574+
select * from cnullparent;
575+
select * from cnullparent where f1 = 2;
576+
drop table cnullparent cascade;

0 commit comments

Comments
 (0)