Re: Row pattern recognition
Henson Choi <assam258@gmail.com>
From: Henson Choi <assam258@gmail.com>
To: Tatsuo Ishii <ishii@postgresql.org>
Cc: david.g.johnston@gmail.com, vik@postgresfriends.org, jacob.champion@enterprisedb.com, er@xs4all.nl, peter@eisentraut.org, pgsql-hackers@postgresql.org
Date: 2026-01-12T06:44:18Z
Lists: pgsql-hackers
Attachments
- 0001-grammar-and-AST-structure.txt (text/plain)
Subject: Re: Row pattern recognition
Hi Ishii-san,
I've been working on the pattern grammar and AST for row pattern
recognition.
The attached patch extends the PATTERN clause to support more regex-like
syntax.
The main additions are:
- Alternation: (A | B)
- Grouping with quantifiers: (A B)+, (A | B)*
- Full quantifier support: +, *, ?, {n}, {n,}, {n,m}
- Reluctant quantifiers: +?, *?, ?? (parsed but not executed yet)
I've introduced RPRPatternNode to represent the pattern as a Thompson-style
AST with four node types: VAR, SEQ, ALT, and GROUP. Each node stores min/max
bounds for quantifiers and a reluctant flag. This should make the eventual
NFA implementation more straightforward.
The parser also does some basic pattern optimizations. For example:
- (A (B C)) gets flattened to (A B C)
- ((A)) unwraps to just A
- (A{2}){3} becomes A{6}
- (A | B | A) simplifies to (A | B)
- A A A merges into A{3,3}
I also updated ruleutils.c to deparse patterns properly.
Current state:
The executor still uses regex matching, which works but has some
limitations.
Complex patterns with lots of alternations can be slow.
The 26-variable limit is still there since we're mapping to [a-z] for the
regex.
Reluctant quantifiers (+?, *?, ??) are parsed but will raise an error if you
try to use them. They'll need proper NFA support to work correctly.
Some optimization code was removed in the process, but it should work better
once NFA matching is in place.
Next steps:
The plan is to replace regex matching with NFA-based execution. The AST
structure is already set up for that.
The first step is converting the Thompson AST into a flat representation -
flattening pattern elements and building actual NFA states with transitions,
then replacing the regex engine with direct NFA matching. This should
handle all
the pattern syntax properly, including reluctant quantifiers.
Once that's working, the next step is handling multiple contexts during
matching -
tracking which states are active and absorbing context as we go through
rows.
This is essential for proper pattern matching behavior.
After that, I'll implement PATH and CLASSIFIER functions, which depend on
having
the match context available.
Then there's room for various optimizations - PATH space optimization,
incremental aggregate computation over matched rows, better state merging,
etc.
Longer-term, the goal is to get to MATCH_RECOGNIZE in the FROM clause for
full SQL standard compliance.
Let me know if you have any concerns or suggestions about the approach.
Best regards,
Henson
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Adjust cross-version upgrade tests for seg_out() fix
- 3e3d7875e956 19 (unreleased) cited
-
Rationalize error comments in partition split/merge tests
- ecb2508aaf9b 19 (unreleased) cited
-
Add fast path for foreign key constraint checks
- 2da86c1ef9b5 19 (unreleased) cited
-
Fix assorted pretty-trivial memory leaks in the backend.
- e78d1d6d47dc 19 (unreleased) cited
-
Add temporal FOREIGN KEY contraints
- 89f908a6d0ac 18.0 cited
-
Add trailing commas to enum definitions
- 611806cd726f 17.0 cited
-
Remove obsolete executor cleanup code
- d060e921ea5a 17.0 cited