Re: Row pattern recognition

From: Henson Choi <assam258(at)gmail(dot)com>
To: Tatsuo Ishii <ishii(at)postgresql(dot)org>
Cc: jian(dot)universality(at)gmail(dot)com, 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-11-25 18:54:40
Message-ID: CAAAe_zAa58w3VrKbu_teaac50eqAncTRJLYZrMB9gwu_2TYoKg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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.

A navigation argument that pull-up, inlining or a not-null proof
turns into a constant is much the same case, but the situation is a
little different: the argument is skipped because the navigation has
no row to land on, not because of a CASE branch, and the constant may
come from elsewhere rather than being written in the argument itself.
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.

I will send this fix together with fixes for the other correctness
defects the audit found, in my next mail, once I have reviewed them
all again. They are:

- Wrong results: pattern optimization changes the matches when a
group follows a copy of its own body
- Backend crashes: parsing a very long alternation written without
spaces, and the wrapping of a join alias expanded below its join
level
- Internal errors during planning: columns read only by DEFINE do
not get through grouping and eager aggregation
- Dump and restore: named routine parameters inside DEFINE, and
pattern variable names that the lexer turns into lookahead tokens

Along with them I will send two changes to the pattern limits, which
are not defects: the nesting depth is checked as written at parse
time rather than at plan time, and an alternation no longer counts as
a nesting level.

[1]
https://www.postgresql.org/docs/devel/sql-expressions.html#SYNTAX-EXPRESS-EVAL

Best regards,
Henson

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Masahiko Sawada 2026-11-25 18:57:59 Re: Why is the LSN reported for pg_logical_emit_message() different from other decoded operations?
Previous Message Melanie Plageman 2026-11-25 18:34:10 Re: Why clearing the VM doesn't require registering vm buffer in wal record