Re: Row pattern recognition

From: Henson Choi <assam258(at)gmail(dot)com>
To: Tatsuo Ishii <ishii(at)postgresql(dot)org>
Cc: 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, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Row pattern recognition
Date: 2026-02-26 13:34:03
Message-ID: CAAAe_zBx8EV3EGb9UDoWkrQhNyGoWUmEas2br3zWFPu1L6YyfQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Tatsuo,

Here are ten incremental patches on top of v43.

nocfbot-0001 through nocfbot-0005 are the same as the previous round
(32-bit test fix, PREV/NEXT restriction, ALT lexical ordering,
reluctant quantifiers, cycle detection).

As discussed, the A{0} patch (old nocfbot-0006) is withdrawn since
Section 4.14.1 requires n > 0. Your bare-block refactoring patches
are renumbered accordingly:

nocfbot-0006: Refactor create_windowagg_plan to remove bare
variable-scoping block (your patch, renumbered)

nocfbot-0007: Remove bare variable-scoping blocks in RPR code

Three new patches:

nocfbot-0008: Fix empty-match iteration for nullable group bodies

This is the fix I mentioned for the Section 7.2.8 behavioral
difference. When a group body is nullable (e.g. A? in (A?){2,3}),
the visited bitmap blocks re-entry to the skipped variable, so the
END element never gets a chance to produce an exit state for
count < min.

The fix adds a compile-time canEmptyLoop flag on END elements.
The fillRPRPattern functions now return whether each sub-pattern
is nullable, and this information propagates up to set the flag
on the enclosing END element.

At runtime, when the flag is set and count < min,
nfa_advance_end() creates a fast-forward exit state that jumps
directly past the group, treating all remaining required
iterations as empty. This avoids the cycle detection while
still counting empty iterations toward min, matching the
standard and Perl behavior.

nocfbot-0009: Fix use-after-free in NFA alternation and
optional-VAR routing

Two use-after-free bugs in the NFA advance phase:

1) nfa_advance_alt() reused the original state for the first ALT
branch, then created copies for subsequent branches. If
nfa_advance_state() freed the original (e.g. via
nfa_add_state_unique() deduplication), subsequent branches
would copy from freed memory. Fix: create independent states
for all branches and free the original.

2) nfa_route_to_elem() created the skip state for optional VARs
after calling nfa_add_state_unique(), which may free the
source state. Fix: create the skip state before add_unique.

nocfbot-0010: Remove redundant conditions and fix comments in
NFA executor

Cleanup pass over NFA executor functions:

- nfa_states_equal(): removed always-true compareDepth > 0
guard (RPRDepth is uint8, so depth + 1 is always >= 1)
- nfa_start_context(): removed always-true NULL/count guards
and bare variable-scoping block
- nfa_advance_var(): removed always-true count > 0 checks in
three places (nfa_advance_var is only reached after match
phase where count++ already executed, and max=0 is forbidden)
- nfa_process_row(): fixed missing periods in phase comments

Best regards,
Henson

Attachment Content-Type Size
nocfbot-0001-32bit-test-fix.txt text/plain 4.2 KB
nocfbot-0002-prev-next-restrict.txt text/plain 4.2 KB
nocfbot-0003-alt-lexical-order.txt text/plain 23.8 KB
nocfbot-0004-reluctant-quantifiers.txt text/plain 65.6 KB
nocfbot-0005-cycle-detection.txt text/plain 6.9 KB
nocfbot-0006-bare-block-planner.txt text/plain 3.3 KB
nocfbot-0007-bare-blocks-rpr.txt text/plain 7.4 KB
nocfbot-0008-empty-match-iter.txt text/plain 42.7 KB
nocfbot-0009-use-after-free.txt text/plain 20.8 KB
nocfbot-0010-redundant-conditions.txt text/plain 6.2 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Jim Jones 2026-02-26 13:39:02 Re: COMMENTS are not being copied in CREATE TABLE LIKE
Previous Message Fujii Masao 2026-02-26 13:33:19 Re: Show comments in \dRp+, \dRs+, and \dX+ psql meta-commands