v45-0002-Row-pattern-recognition-patch-parse-analysis.patch

application/octet-stream

Filename: v45-0002-Row-pattern-recognition-patch-parse-analysis.patch
Type: application/octet-stream
Part: 1
Message: Re: Row pattern recognition

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v45-0002
Subject: Row pattern recognition patch (parse/analysis).
File+
src/backend/nodes/copyfuncs.c 27 0
src/backend/nodes/equalfuncs.c 35 0
src/backend/nodes/outfuncs.c 51 0
src/backend/nodes/readfuncs.c 85 0
src/backend/parser/Makefile 1 0
src/backend/parser/meson.build 1 0
src/backend/parser/parse_agg.c 8 1
src/backend/parser/parse_clause.c 6 4
src/backend/parser/parse_expr.c 42 0
src/backend/parser/parse_func.c 14 0
src/backend/parser/parse_rpr.c 404 0
src/backend/parser/README 1 0
src/include/parser/parse_clause.h 3 0
src/include/parser/parse_rpr.h 22 0
From ef9eb979977560ccc11a5c9164485a754856dcbe Mon Sep 17 00:00:00 2001
From: Tatsuo Ishii <ishii@postgresql.org>
Date: Wed, 18 Mar 2026 20:16:17 +0900
Subject: [PATCH v45 2/8] Row pattern recognition patch (parse/analysis).

---
 src/backend/nodes/copyfuncs.c     |  27 ++
 src/backend/nodes/equalfuncs.c    |  35 +++
 src/backend/nodes/outfuncs.c      |  51 ++++
 src/backend/nodes/readfuncs.c     |  85 +++++++
 src/backend/parser/Makefile       |   1 +
 src/backend/parser/README         |   1 +
 src/backend/parser/meson.build    |   1 +
 src/backend/parser/parse_agg.c    |   9 +-
 src/backend/parser/parse_clause.c |  10 +-
 src/backend/parser/parse_expr.c   |  42 ++++
 src/backend/parser/parse_func.c   |  14 ++
 src/backend/parser/parse_rpr.c    | 404 ++++++++++++++++++++++++++++++
 src/include/parser/parse_clause.h |   3 +
 src/include/parser/parse_rpr.h    |  22 ++
 14 files changed, 700 insertions(+), 5 deletions(-)
 create mode 100644 src/backend/parser/parse_rpr.c
 create mode 100644 src/include/parser/parse_rpr.h

diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c
index ff22a04abe5..e67ad39bdb8 100644
--- a/src/backend/nodes/copyfuncs.c
+++ b/src/backend/nodes/copyfuncs.c
@@ -16,6 +16,7 @@
 #include "postgres.h"
 
 #include "miscadmin.h"
+#include "nodes/plannodes.h"
 #include "utils/datum.h"
 
 
@@ -166,6 +167,32 @@ _copyBitmapset(const Bitmapset *from)
 	return bms_copy(from);
 }
 
+static RPRPattern *
+_copyRPRPattern(const RPRPattern *from)
+{
+	RPRPattern *newnode = makeNode(RPRPattern);
+
+	COPY_SCALAR_FIELD(numVars);
+	COPY_SCALAR_FIELD(maxDepth);
+	COPY_SCALAR_FIELD(numElements);
+
+	/* Deep copy the varNames array (DEFINE clause is required) */
+	Assert(from->numVars > 0);
+	newnode->varNames = palloc0(from->numVars * sizeof(char *));
+	for (int i = 0; i < from->numVars; i++)
+		newnode->varNames[i] = pstrdup(from->varNames[i]);
+
+	/* Deep copy the elements array (always has at least one element + FIN) */
+	Assert(from->numElements >= 2);
+	newnode->elements = palloc(from->numElements * sizeof(RPRPatternElement));
+	memcpy(newnode->elements, from->elements,
+		   from->numElements * sizeof(RPRPatternElement));
+
+	COPY_SCALAR_FIELD(isAbsorbable);
+
+	return newnode;
+}
+
 
 /*
  * copyObjectImpl -- implementation of copyObject(); see nodes/nodes.h
diff --git a/src/backend/nodes/equalfuncs.c b/src/backend/nodes/equalfuncs.c
index 3d1a1adf86e..328199918b8 100644
--- a/src/backend/nodes/equalfuncs.c
+++ b/src/backend/nodes/equalfuncs.c
@@ -20,6 +20,7 @@
 #include "postgres.h"
 
 #include "miscadmin.h"
+#include "nodes/plannodes.h"
 #include "utils/datum.h"
 
 
@@ -149,6 +150,40 @@ _equalBitmapset(const Bitmapset *a, const Bitmapset *b)
 	return bms_equal(a, b);
 }
 
+static bool
+_equalRPRPattern(const RPRPattern *a, const RPRPattern *b)
+{
+	COMPARE_SCALAR_FIELD(numVars);
+	COMPARE_SCALAR_FIELD(maxDepth);
+	COMPARE_SCALAR_FIELD(numElements);
+
+	/* Compare varNames array */
+	if (a->numVars > 0)
+	{
+		if (a->varNames == NULL || b->varNames == NULL)
+			return false;
+		for (int i = 0; i < a->numVars; i++)
+		{
+			if (strcmp(a->varNames[i], b->varNames[i]) != 0)
+				return false;
+		}
+	}
+
+	/* Compare elements array */
+	if (a->numElements > 0)
+	{
+		if (a->elements == NULL || b->elements == NULL)
+			return false;
+		if (memcmp(a->elements, b->elements,
+				   a->numElements * sizeof(RPRPatternElement)) != 0)
+			return false;
+	}
+
+	COMPARE_SCALAR_FIELD(isAbsorbable);
+
+	return true;
+}
+
 /*
  * Lists are handled specially
  */
diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
index 953c5797c5d..e6ea9ce22d9 100644
--- a/src/backend/nodes/outfuncs.c
+++ b/src/backend/nodes/outfuncs.c
@@ -23,6 +23,7 @@
 #include "nodes/bitmapset.h"
 #include "nodes/nodes.h"
 #include "nodes/pg_list.h"
+#include "nodes/plannodes.h"
 #include "utils/datum.h"
 
 /* State flag that determines how nodeToStringInternal() should treat location fields */
@@ -727,6 +728,56 @@ _outA_Const(StringInfo str, const A_Const *node)
 	WRITE_LOCATION_FIELD(location);
 }
 
+static void
+_outRPRPattern(StringInfo str, const RPRPattern *node)
+{
+	WRITE_NODE_TYPE("RPRPATTERN");
+
+	WRITE_INT_FIELD(numVars);
+	WRITE_INT_FIELD(maxDepth);
+	WRITE_INT_FIELD(numElements);
+
+	/* Write varNames array as list of strings */
+	appendStringInfoString(str, " :varNames");
+	if (node->numVars > 0 && node->varNames != NULL)
+	{
+		appendStringInfoString(str, " (");
+		for (int i = 0; i < node->numVars; i++)
+		{
+			if (i > 0)
+				appendStringInfoChar(str, ' ');
+			outToken(str, node->varNames[i]);
+		}
+		appendStringInfoChar(str, ')');
+	}
+	else
+		appendStringInfoString(str, " <>");
+
+	/* Write elements array */
+	appendStringInfoString(str, " :elements");
+	if (node->numElements > 0 && node->elements != NULL)
+	{
+		appendStringInfoChar(str, ' ');
+		for (int i = 0; i < node->numElements; i++)
+		{
+			const RPRPatternElement *elem = &node->elements[i];
+
+			appendStringInfo(str, "(%d %d %u %d %d %d %d)",
+							 (int) elem->varId,
+							 (int) elem->depth,
+							 (unsigned) elem->flags,
+							 (int) elem->min,
+							 (int) elem->max,
+							 (int) elem->next,
+							 (int) elem->jump);
+		}
+	}
+	else
+		appendStringInfoString(str, " <>");
+
+	WRITE_BOOL_FIELD(isAbsorbable);
+}
+
 
 /*
  * outNode -
diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c
index b6b2ce6c792..5bbde5bcad2 100644
--- a/src/backend/nodes/readfuncs.c
+++ b/src/backend/nodes/readfuncs.c
@@ -28,6 +28,7 @@
 
 #include "miscadmin.h"
 #include "nodes/bitmapset.h"
+#include "nodes/plannodes.h"
 #include "nodes/readfuncs.h"
 
 
@@ -567,6 +568,90 @@ _readExtensibleNode(void)
 	READ_DONE();
 }
 
+static RPRPattern *
+_readRPRPattern(void)
+{
+	READ_LOCALS(RPRPattern);
+
+	READ_INT_FIELD(numVars);
+	READ_INT_FIELD(maxDepth);
+	READ_INT_FIELD(numElements);
+
+	/* Read varNames array */
+	token = pg_strtok(&length); /* skip :varNames */
+	token = pg_strtok(&length); /* get '(' or '<>' */
+	if (local_node->numVars > 0 && token[0] == '(')
+	{
+		local_node->varNames = palloc(local_node->numVars * sizeof(char *));
+		for (int i = 0; i < local_node->numVars; i++)
+		{
+			token = pg_strtok(&length);
+			local_node->varNames[i] = debackslash(token, length);
+		}
+		token = pg_strtok(&length); /* skip ')' */
+	}
+	else
+	{
+		local_node->varNames = NULL;
+	}
+
+	/* Read elements array */
+	token = pg_strtok(&length); /* skip :elements */
+	token = pg_strtok(&length); /* get '(' or '<>' */
+	if (local_node->numElements > 0 && token[0] == '(')
+	{
+		local_node->elements = palloc0(local_node->numElements * sizeof(RPRPatternElement));
+		for (int i = 0; i < local_node->numElements; i++)
+		{
+			RPRPatternElement *elem = &local_node->elements[i];
+			int			varId,
+						flags,
+						depth,
+						min,
+						max,
+						next,
+						jump;
+
+			/* Parse "(varId depth flags min max next jump)" */
+			token = pg_strtok(&length);
+			varId = atoi(token);
+			token = pg_strtok(&length);
+			depth = atoi(token);
+			token = pg_strtok(&length);
+			flags = atoi(token);
+			token = pg_strtok(&length);
+			min = atoi(token);
+			token = pg_strtok(&length);
+			max = atoi(token);
+			token = pg_strtok(&length);
+			next = atoi(token);
+			token = pg_strtok(&length);
+			jump = atoi(token);
+			token = pg_strtok(&length); /* skip ')' */
+
+			elem->varId = (RPRVarId) varId;
+			elem->flags = (RPRElemFlags) flags;
+			elem->depth = (RPRDepth) depth;
+			elem->min = (RPRQuantity) min;
+			elem->max = (RPRQuantity) max;
+			elem->next = (RPRElemIdx) next;
+			elem->jump = (RPRElemIdx) jump;
+
+			/* Read next element's '(' or end */
+			if (i < local_node->numElements - 1)
+				token = pg_strtok(&length); /* get '(' */
+		}
+	}
+	else
+	{
+		local_node->elements = NULL;
+	}
+
+	READ_BOOL_FIELD(isAbsorbable);
+
+	READ_DONE();
+}
+
 
 /*
  * parseNodeString
diff --git a/src/backend/parser/Makefile b/src/backend/parser/Makefile
index 8b5a4af6bf2..51e6b1adfb8 100644
--- a/src/backend/parser/Makefile
+++ b/src/backend/parser/Makefile
@@ -30,6 +30,7 @@ OBJS = \
 	parse_oper.o \
 	parse_param.o \
 	parse_relation.o \
+	parse_rpr.o \
 	parse_target.o \
 	parse_type.o \
 	parse_utilcmd.o \
diff --git a/src/backend/parser/README b/src/backend/parser/README
index e26eb437a9f..2baffa9517e 100644
--- a/src/backend/parser/README
+++ b/src/backend/parser/README
@@ -26,6 +26,7 @@ parse_node.c	create nodes for various structures
 parse_oper.c	handle operators in expressions
 parse_param.c	handle Params (for the cases used in the core backend)
 parse_relation.c support routines for tables and column handling
+parse_rpr.c	    handle Row Pattern Recognition
 parse_target.c	handle the result list of the query
 parse_type.c	support routines for data type handling
 parse_utilcmd.c	parse analysis for utility commands (done at execution time)
diff --git a/src/backend/parser/meson.build b/src/backend/parser/meson.build
index 86c09b29ec2..82fe86e10db 100644
--- a/src/backend/parser/meson.build
+++ b/src/backend/parser/meson.build
@@ -17,6 +17,7 @@ backend_sources += files(
   'parse_oper.c',
   'parse_param.c',
   'parse_relation.c',
+  'parse_rpr.c',
   'parse_target.c',
   'parse_type.c',
   'parse_utilcmd.c',
diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c
index 6076e9373c1..fb5e7fb927f 100644
--- a/src/backend/parser/parse_agg.c
+++ b/src/backend/parser/parse_agg.c
@@ -590,7 +590,10 @@ check_agglevels_and_constraints(ParseState *pstate, Node *expr)
 				err = _("aggregate functions are not allowed in property definition expressions");
 			else
 				err = _("grouping operations are not allowed in property definition expressions");
+			break;
 
+		case EXPR_KIND_RPR_DEFINE:
+			errkind = true;
 			break;
 
 			/*
@@ -1035,6 +1038,9 @@ transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc,
 		case EXPR_KIND_PROPGRAPH_PROPERTY:
 			err = _("window functions are not allowed in property definition expressions");
 			break;
+		case EXPR_KIND_RPR_DEFINE:
+			errkind = true;
+			break;
 
 			/*
 			 * There is intentionally no default: case here, so that the
@@ -1115,7 +1121,8 @@ transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc,
 				equal(refwin->orderClause, windef->orderClause) &&
 				refwin->frameOptions == windef->frameOptions &&
 				equal(refwin->startOffset, windef->startOffset) &&
-				equal(refwin->endOffset, windef->endOffset))
+				equal(refwin->endOffset, windef->endOffset) &&
+				equal(refwin->rpCommonSyntax, windef->rpCommonSyntax))
 			{
 				/* found a duplicate window specification */
 				wfunc->winref = winref;
diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c
index 967eea44f1c..6d7408c7d03 100644
--- a/src/backend/parser/parse_clause.c
+++ b/src/backend/parser/parse_clause.c
@@ -39,6 +39,7 @@
 #include "parser/parse_graphtable.h"
 #include "parser/parse_oper.h"
 #include "parser/parse_relation.h"
+#include "parser/parse_rpr.h"
 #include "parser/parse_target.h"
 #include "parser/parse_type.h"
 #include "parser/parser.h"
@@ -88,8 +89,6 @@ static void checkExprIsVarFree(ParseState *pstate, Node *n,
 							   const char *constructName);
 static TargetEntry *findTargetlistEntrySQL92(ParseState *pstate, Node *node,
 											 List **tlist, ParseExprKind exprKind);
-static TargetEntry *findTargetlistEntrySQL99(ParseState *pstate, Node *node,
-											 List **tlist, ParseExprKind exprKind);
 static int	get_matching_location(int sortgroupref,
 								  List *sortgrouprefs, List *exprs);
 static List *resolve_unique_index_expr(ParseState *pstate, InferClause *infer,
@@ -101,7 +100,6 @@ static Node *transformFrameOffset(ParseState *pstate, int frameOptions,
 								  Oid rangeopfamily, Oid rangeopcintype, Oid *inRangeFunc,
 								  Node *clause);
 
-
 /*
  * transformFromClause -
  *	  Process the FROM clause and add items to the query's range table,
@@ -2304,7 +2302,7 @@ findTargetlistEntrySQL92(ParseState *pstate, Node *node, List **tlist,
  * tlist	the target list (passed by reference so we can append to it)
  * exprKind identifies clause type being processed
  */
-static TargetEntry *
+TargetEntry *
 findTargetlistEntrySQL99(ParseState *pstate, Node *node, List **tlist,
 						 ParseExprKind exprKind)
 {
@@ -3155,6 +3153,10 @@ transformWindowDefinitions(ParseState *pstate,
 											 rangeopfamily, rangeopcintype,
 											 &wc->endInRangeFunc,
 											 windef->endOffset);
+
+		/* Process Row Pattern Recognition related clauses */
+		transformRPR(pstate, wc, windef, targetlist);
+
 		wc->winref = winref;
 
 		result = lappend(result, wc);
diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c
index 312dfdc182a..e4128576613 100644
--- a/src/backend/parser/parse_expr.c
+++ b/src/backend/parser/parse_expr.c
@@ -579,6 +579,7 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref)
 		case EXPR_KIND_GENERATED_COLUMN:
 		case EXPR_KIND_CYCLE_MARK:
 		case EXPR_KIND_PROPGRAPH_PROPERTY:
+		case EXPR_KIND_RPR_DEFINE:
 			/* okay */
 			break;
 
@@ -614,6 +615,42 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref)
 			return node;
 	}
 
+	/*
+	 * Qualified column references in DEFINE are not supported.  This covers
+	 * both FROM-clause range variables (prohibited by §6.5) and pattern
+	 * variable qualified names (e.g. UP.price), which are valid per §4.16
+	 * but not yet implemented.
+	 */
+	if (pstate->p_expr_kind == EXPR_KIND_RPR_DEFINE &&
+		list_length(cref->fields) != 1)
+	{
+		char	   *qualifier = strVal(linitial(cref->fields));
+		ListCell   *lc;
+		bool		is_pattern_var = false;
+
+		foreach(lc, pstate->p_rpr_pattern_vars)
+		{
+			if (strcmp(strVal(lfirst(lc)), qualifier) == 0)
+			{
+				is_pattern_var = true;
+				break;
+			}
+		}
+
+		if (is_pattern_var)
+			ereport(ERROR,
+					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+					 errmsg("pattern variable qualified column reference \"%s\" is not supported in DEFINE clause",
+							NameListToString(cref->fields)),
+					 parser_errposition(pstate, cref->location)));
+		else
+			ereport(ERROR,
+					(errcode(ERRCODE_SYNTAX_ERROR),
+					 errmsg("range variable qualified column reference \"%s\" is not allowed in DEFINE clause",
+							NameListToString(cref->fields)),
+					 parser_errposition(pstate, cref->location)));
+	}
+
 	/*----------
 	 * The allowed syntaxes are:
 	 *
@@ -1880,6 +1917,9 @@ transformSubLink(ParseState *pstate, SubLink *sublink)
 		case EXPR_KIND_PROPGRAPH_PROPERTY:
 			err = _("cannot use subquery in property definition expression");
 			break;
+		case EXPR_KIND_RPR_DEFINE:
+			err = _("cannot use subquery in DEFINE expression");
+			break;
 
 			/*
 			 * There is intentionally no default: case here, so that the
@@ -3241,6 +3281,8 @@ ParseExprKindName(ParseExprKind exprKind)
 			return "CYCLE";
 		case EXPR_KIND_PROPGRAPH_PROPERTY:
 			return "property definition expression";
+		case EXPR_KIND_RPR_DEFINE:
+			return "DEFINE";
 
 			/*
 			 * There is intentionally no default: case here, so that the
diff --git a/src/backend/parser/parse_func.c b/src/backend/parser/parse_func.c
index 8dbd41a3548..23b02fb3bc0 100644
--- a/src/backend/parser/parse_func.c
+++ b/src/backend/parser/parse_func.c
@@ -31,6 +31,7 @@
 #include "parser/parse_target.h"
 #include "parser/parse_type.h"
 #include "utils/builtins.h"
+#include "utils/fmgroids.h"
 #include "utils/lsyscache.h"
 #include "utils/syscache.h"
 
@@ -756,6 +757,16 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
 	if (retset)
 		check_srf_call_placement(pstate, last_srf, location);
 
+	/* next() and prev() are only allowed in a WINDOW DEFINE clause */
+	if (fdresult == FUNCDETAIL_NORMAL &&
+		pstate->p_expr_kind != EXPR_KIND_RPR_DEFINE &&
+		(funcid == F_PREV || funcid == F_NEXT))
+		ereport(ERROR,
+				(errcode(ERRCODE_SYNTAX_ERROR),
+				 errmsg("%s can only be used in a DEFINE clause",
+						NameListToString(funcname)),
+				 parser_errposition(pstate, location)));
+
 	/* build the appropriate output structure */
 	if (fdresult == FUNCDETAIL_NORMAL || fdresult == FUNCDETAIL_PROCEDURE)
 	{
@@ -2786,6 +2797,9 @@ check_srf_call_placement(ParseState *pstate, Node *last_srf, int location)
 		case EXPR_KIND_PROPGRAPH_PROPERTY:
 			err = _("set-returning functions are not allowed in property definition expressions");
 			break;
+		case EXPR_KIND_RPR_DEFINE:
+			errkind = true;
+			break;
 
 			/*
 			 * There is intentionally no default: case here, so that the
diff --git a/src/backend/parser/parse_rpr.c b/src/backend/parser/parse_rpr.c
new file mode 100644
index 00000000000..e574b69d9b5
--- /dev/null
+++ b/src/backend/parser/parse_rpr.c
@@ -0,0 +1,404 @@
+/*-------------------------------------------------------------------------
+ *
+ * parse_rpr.c
+ *	  Handle Row Pattern Recognition clauses in parser.
+ *
+ * This file transforms RPR-related clauses from raw parse tree to planner
+ * structures during query analysis:
+ *   - Validates frame options (must start at CURRENT ROW, no EXCLUDE)
+ *   - Validates PATTERN variable count (max RPR_VARID_MAX)
+ *   - Transforms DEFINE clause into TargetEntry list
+ *   - Stores PATTERN/SKIP TO/INITIAL clauses for planner
+ *
+ * Pattern optimization and compilation to NFA bytecode happens later
+ * in the planner (see optimizer/plan/rpr.c).
+ *
+ * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ *	  src/backend/parser/parse_rpr.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include "postgres.h"
+
+#include "nodes/makefuncs.h"
+#include "nodes/nodeFuncs.h"
+#include "optimizer/rpr.h"
+#include "parser/parse_clause.h"
+#include "parser/parse_collate.h"
+#include "parser/parse_coerce.h"
+#include "parser/parse_expr.h"
+#include "parser/parse_rpr.h"
+#include "parser/parse_target.h"
+
+/* Forward declarations */
+static void validateRPRPatternVarCount(ParseState *pstate, RPRPatternNode *node,
+									   List *rpDefs, List **varNames);
+static List *transformDefineClause(ParseState *pstate, WindowClause *wc,
+								   WindowDef *windef, List **targetlist);
+
+/*
+ * transformRPR
+ *		Process Row Pattern Recognition related clauses.
+ *
+ * Validates and transforms RPR clauses from parse tree to planner structures:
+ *   - Validates frame options (must start at CURRENT ROW, no EXCLUDE)
+ *   - Stores AFTER MATCH SKIP TO clause
+ *   - Stores SEEK/INITIAL clause
+ *   - Transforms DEFINE clause into TargetEntry list
+ *   - Stores PATTERN AST for deparsing (optimization happens in planner)
+ *
+ * Returns early if windef has no rpCommonSyntax (non-RPR window).
+ */
+void
+transformRPR(ParseState *pstate, WindowClause *wc, WindowDef *windef,
+			 List **targetlist)
+{
+	/* Window definition must exist when called */
+	Assert(windef != NULL);
+
+	/*
+	 * Row Pattern Common Syntax clause exists?
+	 */
+	if (windef->rpCommonSyntax == NULL)
+		return;
+
+	/* Check Frame options */
+
+	/* Frame type must be "ROW" */
+	if (wc->frameOptions & FRAMEOPTION_GROUPS)
+		ereport(ERROR,
+				(errcode(ERRCODE_SYNTAX_ERROR),
+				 errmsg("FRAME option GROUP is not permitted when row pattern recognition is used"),
+				 errhint("Use: ROWS instead"),
+				 parser_errposition(pstate,
+									windef->frameLocation >= 0 ?
+									windef->frameLocation : windef->location)));
+	if (wc->frameOptions & FRAMEOPTION_RANGE)
+		ereport(ERROR,
+				(errcode(ERRCODE_SYNTAX_ERROR),
+				 errmsg("FRAME option RANGE is not permitted when row pattern recognition is used"),
+				 errhint("Use: ROWS instead"),
+				 parser_errposition(pstate,
+									windef->frameLocation >= 0 ?
+									windef->frameLocation : windef->location)));
+
+	/* Frame must start at current row */
+	if ((wc->frameOptions & FRAMEOPTION_START_CURRENT_ROW) == 0)
+	{
+		const char *frameType = "ROWS";
+		const char *startBound = "unknown";
+
+		/* Determine current start bound */
+		if (wc->frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING)
+			startBound = "UNBOUNDED PRECEDING";
+		else if (wc->frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING)
+			startBound = "offset PRECEDING";
+		else if (wc->frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING)
+			startBound = "offset FOLLOWING";
+
+		/* At least one valid frame start option should be set */
+		Assert((wc->frameOptions & FRAMEOPTION_START_UNBOUNDED_PRECEDING) ||
+			   (wc->frameOptions & FRAMEOPTION_START_OFFSET_PRECEDING) ||
+			   (wc->frameOptions & FRAMEOPTION_START_OFFSET_FOLLOWING));
+
+		ereport(ERROR,
+				(errcode(ERRCODE_SYNTAX_ERROR),
+				 errmsg("FRAME must start at CURRENT ROW when row pattern recognition is used"),
+				 errdetail("Current frame starts with %s.", startBound),
+				 errhint("Use: %s BETWEEN CURRENT ROW AND ...", frameType),
+				 parser_errposition(pstate, windef->frameLocation >= 0 ? windef->frameLocation : windef->location)));
+	}
+
+	/* EXCLUDE options are not permitted */
+	if ((wc->frameOptions & FRAMEOPTION_EXCLUSION) != 0)
+	{
+		const char *excludeType = "EXCLUDE";
+
+		/* Determine which EXCLUDE option was used */
+		if (wc->frameOptions & FRAMEOPTION_EXCLUDE_CURRENT_ROW)
+			excludeType = "EXCLUDE CURRENT ROW";
+		else if (wc->frameOptions & FRAMEOPTION_EXCLUDE_GROUP)
+			excludeType = "EXCLUDE GROUP";
+		else if (wc->frameOptions & FRAMEOPTION_EXCLUDE_TIES)
+			excludeType = "EXCLUDE TIES";
+
+		/* At least one valid exclude option should be set */
+		Assert((wc->frameOptions & FRAMEOPTION_EXCLUDE_CURRENT_ROW) ||
+			   (wc->frameOptions & FRAMEOPTION_EXCLUDE_GROUP) ||
+			   (wc->frameOptions & FRAMEOPTION_EXCLUDE_TIES));
+
+		ereport(ERROR,
+				(errcode(ERRCODE_SYNTAX_ERROR),
+				 errmsg("EXCLUDE options are not permitted when row pattern recognition is used"),
+				 errdetail("Frame definition includes %s.", excludeType),
+				 errhint("Remove the EXCLUDE clause from the window definition."),
+				 parser_errposition(pstate, windef->excludeLocation >= 0 ? windef->excludeLocation : windef->location)));
+	}
+
+	/* Transform AFTER MATCH SKIP TO clause */
+	wc->rpSkipTo = windef->rpCommonSyntax->rpSkipTo;
+
+	/* Transform SEEK or INITIAL clause */
+	wc->initial = windef->rpCommonSyntax->initial;
+
+	/* Transform DEFINE clause into list of TargetEntry's */
+	wc->defineClause = transformDefineClause(pstate, wc, windef, targetlist);
+
+	/* Store PATTERN AST for deparsing */
+	wc->rpPattern = windef->rpCommonSyntax->rpPattern;
+}
+
+/*
+ * validateRPRPatternVarCount
+ *		Validate that PATTERN variables don't exceed RPR_VARID_MAX.
+ *
+ * Recursively traverses the pattern tree, collecting unique variable names.
+ * Throws an error if the number of unique variables exceeds RPR_VARID_MAX.
+ *
+ * If rpDefs is non-NULL, DEFINE variable names are also collected into
+ * varNames so that transformColumnRef can distinguish pattern variable
+ * qualifiers from FROM-clause range variables.
+ *
+ * varNames is both input and output: existing names are preserved, new ones added.
+ */
+static void
+validateRPRPatternVarCount(ParseState *pstate, RPRPatternNode *node,
+						   List *rpDefs, List **varNames)
+{
+	ListCell   *lc;
+
+	/* Pattern node must exist - parser always provides non-NULL root */
+	Assert(node != NULL);
+
+	switch (node->nodeType)
+	{
+		case RPR_PATTERN_VAR:
+			/* Add variable name if not already in list */
+			{
+				bool		found = false;
+
+				foreach(lc, *varNames)
+				{
+					if (strcmp(strVal(lfirst(lc)), node->varName) == 0)
+					{
+						found = true;
+						break;
+					}
+				}
+				if (!found)
+				{
+					/* Check against RPR_VARID_MAX before adding */
+					if (list_length(*varNames) >= RPR_VARID_MAX)
+						ereport(ERROR,
+								(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+								 errmsg("too many pattern variables"),
+								 errdetail("Maximum is %d.", RPR_VARID_MAX),
+								 parser_errposition(pstate,
+													exprLocation((Node *) node))));
+
+					*varNames = lappend(*varNames, makeString(pstrdup(node->varName)));
+				}
+			}
+			break;
+
+		case RPR_PATTERN_SEQ:
+		case RPR_PATTERN_ALT:
+		case RPR_PATTERN_GROUP:
+			/* Recurse into children */
+			foreach(lc, node->children)
+			{
+				validateRPRPatternVarCount(pstate, (RPRPatternNode *) lfirst(lc),
+										   NULL, varNames);
+			}
+			break;
+	}
+
+	/*
+	 * After the top-level call, also collect DEFINE variable names that are
+	 * not already in the list.  This is only done once at the outermost
+	 * recursion level, detected by rpDefs being non-NULL (recursive calls
+	 * pass NULL).
+	 */
+	if (rpDefs)
+	{
+		foreach(lc, rpDefs)
+		{
+			ResTarget  *rt = (ResTarget *) lfirst(lc);
+			ListCell   *lc2;
+			bool		found = false;
+
+			foreach(lc2, *varNames)
+			{
+				if (strcmp(strVal(lfirst(lc2)), rt->name) == 0)
+				{
+					found = true;
+					break;
+				}
+			}
+			if (!found)
+				*varNames = lappend(*varNames,
+									makeString(pstrdup(rt->name)));
+		}
+	}
+}
+
+/*
+ * transformDefineClause
+ *		Process DEFINE clause and transform ResTarget into list of TargetEntry.
+ *
+ * First:
+ *   1. Validates PATTERN variable count and collects RPR variable names
+ *
+ * Then for each DEFINE variable:
+ *   2. Checks for duplicate variable names in DEFINE clause
+ *   3. Transforms expressions and adds to targetlist via findTargetlistEntrySQL99
+ *   4. Creates defineClause entry with proper resname (pattern variable name)
+ *   5. Coerces expressions to boolean type
+ *   6. Marks column origins and assigns collation information
+ *
+ * Note: Variables not in DEFINE are evaluated as TRUE by the executor.
+ * Variables in DEFINE but not in PATTERN are filtered out by the planner.
+ *
+ * XXX Pattern variable qualified column references in DEFINE (e.g.
+ * "A.price") are not yet supported.  Currently rejected by
+ * transformColumnRef in parse_expr.c via the p_rpr_pattern_vars check.
+ */
+static List *
+transformDefineClause(ParseState *pstate, WindowClause *wc, WindowDef *windef,
+					  List **targetlist)
+{
+	ListCell   *lc,
+			   *l;
+	ResTarget  *restarget,
+			   *r;
+	List	   *restargets;
+	List	   *defineClause = NIL;
+	char	   *name;
+	List	   *patternVarNames = NIL;
+
+	/*
+	 * If Row Definition Common Syntax exists, DEFINE clause must exist. (the
+	 * raw parser should have already checked it.)
+	 */
+	Assert(windef->rpCommonSyntax->rpDefs != NULL);
+
+	/*
+	 * Validate PATTERN variable count and collect all RPR variable names
+	 * (PATTERN + DEFINE) for use in transformColumnRef.
+	 */
+	validateRPRPatternVarCount(pstate, windef->rpCommonSyntax->rpPattern,
+							   windef->rpCommonSyntax->rpDefs,
+							   &patternVarNames);
+	pstate->p_rpr_pattern_vars = patternVarNames;
+
+	/*
+	 * Check for duplicate row pattern definition variables.  The standard
+	 * requires that no two row pattern definition variable names shall be
+	 * equivalent.
+	 */
+	restargets = NIL;
+	foreach(lc, windef->rpCommonSyntax->rpDefs)
+	{
+		TargetEntry *te,
+				   *teDefine;
+		int			defineExprLocation;
+
+		restarget = (ResTarget *) lfirst(lc);
+		name = restarget->name;
+
+		foreach(l, restargets)
+		{
+			char	   *n;
+
+			r = (ResTarget *) lfirst(l);
+			n = r->name;
+
+			if (!strcmp(n, name))
+				ereport(ERROR,
+						(errcode(ERRCODE_SYNTAX_ERROR),
+						 errmsg("row pattern definition variable name \"%s\" appears more than once in DEFINE clause",
+								name),
+						 parser_errposition(pstate, exprLocation((Node *) r))));
+		}
+
+		restargets = lappend(restargets, restarget);
+
+		/*
+		 * Transform the DEFINE expression (restarget->val) and add it to the
+		 * targetlist as a TargetEntry if not already present, so the planner
+		 * can propagate the referenced columns to the outer plan's
+		 * targetlist.
+		 *
+		 * Note: findTargetlistEntrySQL99 transforms and clobbers
+		 * restarget->val.
+		 */
+
+		/*
+		 * Save the original expression location before transformation.
+		 * findTargetlistEntrySQL99 may return an existing TargetEntry whose
+		 * location points to where it was originally created (e.g., ORDER
+		 * BY), not the DEFINE clause. We need to preserve the DEFINE location
+		 * for accurate error reporting.
+		 */
+		defineExprLocation = exprLocation(restarget->val);
+
+		te = findTargetlistEntrySQL99(pstate, restarget->val,
+									  targetlist, EXPR_KIND_RPR_DEFINE);
+
+		/* -------------------
+		 * Copy the TargetEntry for defineClause and always set the pattern
+		 * variable name. We use copyObject so the original targetlist entry
+		 * is not modified.
+		 *
+		 * Note: We must always set resname to the pattern variable name.
+		 * findTargetlistEntrySQL99 creates new TEs with resname = NULL
+		 * (resjunk entries), but returns existing TEs unchanged when the
+		 * expression already exists in targetlist.
+		 *
+		 * Example: "SELECT id, flag, ... WINDOW w AS (... DEFINE T AS flag)"
+		 *
+		 * 1. SELECT list processing creates: TE{resname="flag", expr=flag}
+		 * 2. DEFINE T AS flag: findTargetlistEntrySQL99 finds existing TE
+		 * 3. te->resname is "flag" (from SELECT), not NULL
+		 * 4. Without unconditionally setting resname, teDefine->resname
+		 *    would remain "flag" instead of pattern variable name "T"
+		 * 5. buildRPRPattern builds defineVariableList from resname, so
+		 *    it would contain ["flag"] instead of ["T"]
+		 * 6. Pattern variable "T" not found -> Assert failure crash
+		 */
+		teDefine = copyObject(te);
+		teDefine->resname = pstrdup(name);
+
+		/*
+		 * Update the expression location to point to the DEFINE clause. This
+		 * ensures error messages reference the correct source location.
+		 */
+		if (defineExprLocation >= 0 && IsA(teDefine->expr, Var))
+			((Var *) teDefine->expr)->location = defineExprLocation;
+
+		/* build transformed DEFINE clause (list of TargetEntry) */
+		defineClause = lappend(defineClause, teDefine);
+	}
+	list_free(restargets);
+	pstate->p_rpr_pattern_vars = NIL;
+
+	/*
+	 * Make sure that the row pattern definition search condition is a boolean
+	 * expression.
+	 */
+	foreach_ptr(TargetEntry, te, defineClause)
+		te->expr = (Expr *) coerce_to_boolean(pstate, (Node *) te->expr, "DEFINE");
+
+	/* mark column origins */
+	markTargetListOrigins(pstate, defineClause);
+
+	/* mark all nodes in the DEFINE clause tree with collation information */
+	assign_expr_collations(pstate, (Node *) defineClause);
+
+	return defineClause;
+}
diff --git a/src/include/parser/parse_clause.h b/src/include/parser/parse_clause.h
index fe234611007..8aaac881f2b 100644
--- a/src/include/parser/parse_clause.h
+++ b/src/include/parser/parse_clause.h
@@ -52,6 +52,9 @@ extern List *addTargetToSortList(ParseState *pstate, TargetEntry *tle,
 extern Index assignSortGroupRef(TargetEntry *tle, List *tlist);
 extern bool targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList);
 
+extern TargetEntry *findTargetlistEntrySQL99(ParseState *pstate, Node *node,
+											 List **tlist, ParseExprKind exprKind);
+
 /* functions in parse_jsontable.c */
 extern ParseNamespaceItem *transformJsonTable(ParseState *pstate, JsonTable *jt);
 
diff --git a/src/include/parser/parse_rpr.h b/src/include/parser/parse_rpr.h
new file mode 100644
index 00000000000..7fab6f292aa
--- /dev/null
+++ b/src/include/parser/parse_rpr.h
@@ -0,0 +1,22 @@
+/*-------------------------------------------------------------------------
+ *
+ * parse_rpr.h
+ *	  handle Row Pattern Recognition in parser
+ *
+ *
+ * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/parser/parse_rpr.h
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef PARSE_RPR_H
+#define PARSE_RPR_H
+
+#include "parser/parse_node.h"
+
+extern void transformRPR(ParseState *pstate, WindowClause *wc,
+						 WindowDef *windef, List **targetlist);
+
+#endif							/* PARSE_RPR_H */
-- 
2.43.0