]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Optimize ROW(...) IS [NOT] NULL using non-nullable fields
authorRichard Guo <rguo@postgresql.org>
Wed, 24 Dec 2025 09:00:02 +0000 (18:00 +0900)
committerRichard Guo <rguo@postgresql.org>
Wed, 24 Dec 2025 09:00:02 +0000 (18:00 +0900)
We break ROW(...) IS [NOT] NULL into separate tests on its component
fields.  During this breakdown, we can improve efficiency by utilizing
expr_is_nonnullable() to detect fields that are provably non-nullable.

If a component field is proven non-nullable, it affects the outcome
based on the test type.  For an IS NULL test, a single non-nullable
field refutes the whole NullTest, reducing it to constant FALSE.  For
an IS NOT NULL test, the check for that specific field is guaranteed
to succeed, so we can discard it from the list of component tests.

This extends the existing optimization logic, which previously only
handled Const fields, to support any expression that can be proven
non-nullable.

In passing, update the existing constant folding of NullTests to use
expr_is_nonnullable() instead of var_is_nonnullable(), enabling it to
benefit from future improvements to that function.

Author: Richard Guo <guofenglinux@gmail.com>
Reviewed-by: Tender Wang <tndrwang@gmail.com>
Reviewed-by: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
Reviewed-by: David Rowley <dgrowleyml@gmail.com>
Reviewed-by: Matheus Alcantara <matheusssilv97@gmail.com>
Discussion: https://postgr.es/m/CAMbWs49UhPBjm+NRpxerjaeuFKyUZJ_AjM3NBcSYK2JgZ6VTEQ@mail.gmail.com

src/backend/optimizer/util/clauses.c

index ac21057ba51d222af2d4f3561f5064d3d12128c1..eaeadcbcc516d760cbe61c71a6805b1b7141a22d 100644 (file)
@@ -3528,6 +3528,20 @@ eval_const_expressions_mutator(Node *node,
                                                        continue;
                                                }
 
+                                               /*
+                                                * A proven non-nullable field refutes the whole
+                                                * NullTest if the test is IS NULL; else we can
+                                                * discard it.
+                                                */
+                                               if (relem &&
+                                                       expr_is_nonnullable(context->root, (Expr *) relem,
+                                                                                               false))
+                                               {
+                                                       if (ntest->nulltesttype == IS_NULL)
+                                                               return makeBoolConst(false, false);
+                                                       continue;
+                                               }
+
                                                /*
                                                 * Else, make a scalar (argisrow == false) NullTest
                                                 * for this field.  Scalar semantics are required
@@ -3572,30 +3586,27 @@ eval_const_expressions_mutator(Node *node,
 
                                        return makeBoolConst(result, false);
                                }
-                               if (!ntest->argisrow && arg && IsA(arg, Var) && context->root)
+                               if (!ntest->argisrow && arg &&
+                                       expr_is_nonnullable(context->root, (Expr *) arg, false))
                                {
-                                       Var                *varg = (Var *) arg;
                                        bool            result;
 
-                                       if (var_is_nonnullable(context->root, varg, false))
+                                       switch (ntest->nulltesttype)
                                        {
-                                               switch (ntest->nulltesttype)
-                                               {
-                                                       case IS_NULL:
-                                                               result = false;
-                                                               break;
-                                                       case IS_NOT_NULL:
-                                                               result = true;
-                                                               break;
-                                                       default:
-                                                               elog(ERROR, "unrecognized nulltesttype: %d",
-                                                                        (int) ntest->nulltesttype);
-                                                               result = false; /* keep compiler quiet */
-                                                               break;
-                                               }
-
-                                               return makeBoolConst(result, false);
+                                               case IS_NULL:
+                                                       result = false;
+                                                       break;
+                                               case IS_NOT_NULL:
+                                                       result = true;
+                                                       break;
+                                               default:
+                                                       elog(ERROR, "unrecognized nulltesttype: %d",
+                                                                (int) ntest->nulltesttype);
+                                                       result = false; /* keep compiler quiet */
+                                                       break;
                                        }
+
+                                       return makeBoolConst(result, false);
                                }
 
                                newntest = makeNode(NullTest);