| From: | jian he <jian(dot)universality(at)gmail(dot)com> |
|---|---|
| To: | assam258(at)gmail(dot)com |
| Cc: | Tatsuo Ishii <ishii(at)postgresql(dot)org>, zsolt(dot)parragi(at)percona(dot)com, sjjang112233(at)gmail(dot)com, vik(at)postgresfriends(dot)org, er(at)xs4all(dot)nl, jacob(dot)champion(at)enterprisedb(dot)com, david(dot)g(dot)johnston(at)gmail(dot)com, peter(at)eisentraut(dot)org, li(dot)evan(dot)chao(at)gmail(dot)com, pgsql-hackers(at)postgresql(dot)org |
| Subject: | Re: Row pattern recognition |
| Date: | 2026-12-01 01:37:30 |
| Message-ID: | CACJufxF=XpULRV2ps82uzKyGzLZ7tYxnCaQ2X3pB8xXbF=Z_3g@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Thu, Nov 26, 2026 at 2:54 AM Henson Choi <assam258(at)gmail(dot)com> wrote:
>
> Hi Tatsuo,
>
> > Ok, so the case is the division by zero in the constant folding at the
> > planning time. If that is the only case, I would consider working
> > hard for #2 not worth the trouble. I prefer #1 and we should write it
> > as a limitation of the patch so that we don't forget about it.
>
> Agreed, I will go with #1. Division by zero is only the example;
> any other error that constant folding can raise behaves the same.
>
> Core already documents the same behavior in "Expression Evaluation
> Rules" [1]:
>
> SELECT CASE WHEN x > 0 THEN x ELSE 1/0 END FROM tab;
>
> can fail with division by zero at plan time even if every row has
> x > 0, and the same can happen in queries within functions, whose
> arguments and local variables can be inserted as constants.
>
> So I think it would be good to describe this case in that section as
> well. My next increment adds paragraphs there on how a column can
> become a constant while the query is planned, with examples outside
> row pattern recognition, and one on navigation arguments. It points
> to them from the navigation functions and notes the limitation in
> README.rpr, with tests that show the plan-time error.
>
Hi.
WITH t(id, v) AS (VALUES (1, 10))
SELECT count(*) OVER w AS cnt
FROM t
WINDOW w AS (
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
PATTERN (A) DEFINE A AS PREV(v / 0) > 0);
return `` division by zero``
is the direction to move forward, i think.
Constant folding and function inlining happen at plan time; the
expression (v / 0) can be
evaluated before the executor ever decides whether PREV() has a row to read.
Exempting one subtree of an expression from constant folding (or function
inlining) is hacky, and it tends to cause subtle bugs. I made the same
mistake with CAST(... DEFAULT ... ON CONVERSION ERROR).
Overall, i think in eval_const_expressions_mutator, we do below should
be enough.
+ case T_RPRNavExpr:
+ {
+ RPRNavExpr *nav = (RPRNavExpr *) node;
+ RPRNavExpr *newnav;
+
+ /*
+ * The argument and the offsets are
simplified like any other
+ * expression, SQL function inlining included.
+ */
+ newnav = makeNode(RPRNavExpr);
+ memcpy(newnav, nav, sizeof(RPRNavExpr));
+ newnav->arg = (Expr *)
+
eval_const_expressions_mutator((Node *) nav->arg, context);
+ newnav->offset_arg = (Expr *)
+
eval_const_expressions_mutator((Node *) nav->offset_arg,
+
context);
+ newnav->compound_offset_arg = (Expr *)
+
eval_const_expressions_mutator((Node *) nav->compound_offset_arg,
+
context);
+ return (Node *) newnav;
+ }
It is OK not to document anything, see
https://www.postgresql.org/message-id/258f84d5-57c6-454a-a22b-6242871216ec%40illuminatedcomputing.com
| From | Date | Subject | |
|---|---|---|---|
| Next Message | shihao zhong | 2026-12-01 01:53:48 | Re: pg_upgrade's get_rel_infos_query() query can sometimes be slow |
| Previous Message | shihao zhong | 2026-12-01 01:32:25 | Re: Don't use the deprecated and insecure PQcancel in our frontend tools anymore |