Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wMgDd-000GWO-10 for pgsql-hackers@arkaria.postgresql.org; Tue, 12 May 2026 06:03:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wMgDb-003H0F-2Y for pgsql-hackers@arkaria.postgresql.org; Tue, 12 May 2026 06:03:47 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wMgDb-003H06-1Z for pgsql-hackers@lists.postgresql.org; Tue, 12 May 2026 06:03:47 +0000 Received: from meldrar.postgresql.org ([2a02:c0:301:0:ffff::31]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wMgDZ-00000000AYx-0hxU for pgsql-hackers@postgresql.org; Tue, 12 May 2026 06:03:47 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Content-Transfer-Encoding:Content-Type: Mime-Version:References:In-Reply-To:From:Subject:Cc:To:Message-Id:Date:Sender :Reply-To:Content-ID:Content-Description; bh=WKiVNndOOz/bqCAW0BEY3x4pB36GrRuO8Seww0jfD+8=; b=JxTRyHkxubNO1haWDU5H1EwvcP wFSAat6r9BlTZzioTie6o64m/z8EkN/jZqJRSR7FwjEDFF6ItPzAJ2au2BcspwqRGrPRi3H0jlgEo DhYEcUGMMq9YbRtk291lmHU+GQQ2r5oSQILR3IaDL/XZ2TLagbWIHRSP3rb3licAR+MK/yjEsoTjV TOoAAN3Goz1u4mzJDnq9WyifMwRGqHYYYmKtkXNVHG5WucaAVou4EcL6fQE5lmUV4I0vW7mjNhjzT rciENcVSmpe8HwtucSxOf7Ifmn95RvUSerCY43kVKqJ0TNX76qxL9VOgvY8Wo+6zSVZ5S80ungsQq rDd5kr8Q==; Received: from [2409:11:4120:300:3e3d:2b96:316a:3c1c] (helo=localhost) by meldrar.postgresql.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wMgDQ-000M2o-2H; Tue, 12 May 2026 06:03:39 +0000 Date: Tue, 12 May 2026 15:03:28 +0900 (JST) Message-Id: <20260512.150328.1502361632049642572.ishii@postgresql.org> To: assam258@gmail.com Cc: zsolt.parragi@percona.com, sjjang112233@gmail.com, vik@postgresfriends.org, er@xs4all.nl, jacob.champion@enterprisedb.com, david.g.johnston@gmail.com, peter@eisentraut.org, li.evan.chao@gmail.com, pgsql-hackers@postgresql.org Subject: Re: Row pattern recognition From: Tatsuo Ishii In-Reply-To: <20260502.140304.670813149418899420.ishii@postgresql.org> References: <20260427.174220.1939160662649810289.ishii@postgresql.org> <20260502.140304.670813149418899420.ishii@postgresql.org> X-Mailer: Mew version 6.8 on Emacs 29.3 Mime-Version: 1.0 Content-Type: Multipart/Mixed; boundary="--Next_Part(Tue_May_12_15_03_28_2026_397)--" Content-Transfer-Encoding: 7bit X-Host-Lookup-Failed: Reverse DNS lookup failed for 2409:11:4120:300:3e3d:2b96:316a:3c1c (failed) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ----Next_Part(Tue_May_12_15_03_28_2026_397)-- Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Hi Henson, > Attached is the v47 patches for Row pattern recognition (SQL/RPR). > - Add src/backend/executor/README.rpr (previously was in ExecRPR.c) README.rpr is extremely useful for those who want to review the RPR patches. I found a room to enhance the document. Attached is a small patch tries to enhance README.rpr, on top of v47. - Make "target audience" and "scope" of the README more descriptive. - Add References (currently the SQL standards only) - Add explanation of some abbreviations (NFA, AST) - Add reference sections for absorption. Readers might not be familiar with "absorption" - Add more fields to WindowAggState - Add window framing rules with RPR Regards, -- Tatsuo Ishii SRA OSS K.K. English: http://www.sraoss.co.jp/index_en/ Japanese:http://www.sraoss.co.jp ----Next_Part(Tue_May_12_15_03_28_2026_397)-- Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="README.rpr.txt" diff --git a/src/backend/executor/README.rpr b/src/backend/executor/README.rpr index 52bcd77390c..dd98ce97adf 100644 --- a/src/backend/executor/README.rpr +++ b/src/backend/executor/README.rpr @@ -2,11 +2,15 @@ PostgreSQL Row Pattern Recognition: Flat-Array Stream NFA Guide ============================================================================ - Target audience: Developers with a basic understanding of the PostgreSQL - executor and planner architecture + This README's target audience is developers with a basic + understanding of the PostgreSQL executor and planner architecture. + Also it would be better for them to understand the specification of + the row pattern recognition in the SQL standard [1][2]. If you do + not have access to the SQL standard, Oracle's manual or Trino's + manual can be alternatives for them. - Scope: The entire process from PATTERN/DEFINE clause parsing to NFA - runtime execution + This README's scope is the entire process from PATTERN/DEFINE clause + parsing to NFA runtime execution. Related code: - src/backend/parser/parse_rpr.c (parser phase) @@ -23,10 +27,11 @@ What is a Flat-Array Stream NFA? - The NFA in this implementation is not a traditional state-transition graph - but a flat array of fixed-size 16-byte elements. At runtime, it processes - the row stream in a forward-only manner, expanding epsilon transitions - eagerly without backtracking. + The NFA (Nondeterministic Finite Automaton) in this implementation + is not a traditional state-transition graph but a flat array of + fixed-size 16-byte elements. At runtime, it processes the row stream + in a forward-only manner, expanding epsilon transitions eagerly + without backtracking. - Flat-Array: Pattern compiled into a flat array, not a graph (Chapter IV) @@ -126,14 +131,14 @@ following: (3) DEFINE clause transformation (transformDefineClause) -III-2. PATTERN AST +III-2. PATTERN AST (Abstract Syntax Tree) The parser transforms the PATTERN clause into an RPRPatternNode tree. Each node has one of the following four types: RPR_PATTERN_VAR Variable reference. Name stored in varName field. RPR_PATTERN_SEQ Concatenation. Children node list in children. - RPR_PATTERN_ALT Alternation. Branch node list in children. + RPR_PATTERN_ALT Alternation (or). Branch node list in children. RPR_PATTERN_GROUP Group (parentheses). Body node list in children. All nodes have min/max fields to express quantifiers: @@ -264,9 +269,11 @@ Element flags (1 byte, bitmask): matches. (IV-4b) 0x04 RPR_ELEM_ABSORBABLE_BRANCH (VAR, BEGIN, END, ALT) - Element lies within an absorbable region. Used at runtime - to track whether the current NFA state is in an absorbable - context. + Element lies within an absorbable region. Used at runtime to + track whether the current NFA state is in an absorbable + context. See "IV-5. Absorbability Analysis" and + "VIII-2. Solution: Context Absorption" for more details about + absorption. 0x08 RPR_ELEM_ABSORBABLE (VAR, END) Absorption judgment point. Where to compare consecutive @@ -508,7 +515,10 @@ V-3. RPR Fields of WindowAggState nfaStateFree Reuse pool for states nfaVarMatched Per-row cache: varMatched[varId] nfaVisitedElems Bitmap for cycle detection + nfaVisitedNWords Number of bitmapwords in nfaVisitedElems nfaStateSize Precomputed size of RPRNFAState + defineMatchStartDependent DEFINE vars needing per-context evaluation (match_start-dependent) + nfaLastProcessedRow Last row processed by NFA (-1 = none) Memory management: @@ -1047,6 +1057,10 @@ X-3. INITIAL vs SEEK X-4. Bounded Frame Handling + With RPR, the frame mode is always ROWS and the frame start must be + CURRENT ROW. The frame end can be either UNBOUNDED FOLLOWING or n + FOLLOWING. + When the frame is bounded (e.g., ROWS BETWEEN CURRENT ROW AND 5 FOLLOWING), ExecRPRProcessRow receives hasLimitedFrame=true and frameOffset indicating the upper bound. Before the match phase, @@ -1573,6 +1587,15 @@ C-7. PATTERN ((A+ B | C*)+ D) -- Per-branch absorption in ALT nullable. BEGIN and ALT get ABSORBABLE_BRANCH (on the path to absorbable elements). + +References: + +[1] ISO/IEC 19075-5 Information technology - Guidance for the use of + database language SQL - Part 5: Row pattern recognition + +[2] ISO/IEC 9075-2 Information technology - Database languages - SQL - + Part 2: Foundation (SQL/Foundation) + ============================================================================ End of document ============================================================================ ----Next_Part(Tue_May_12_15_03_28_2026_397)----