Subject: [Patch]Add Graph* node support to expression_tree_mutator

From: SATYANARAYANA NARLAPURAM <[email-masked]>
Date: Tue, 21 Apr 2026 08:56:44 -0700

Hi hackers,

expression_tree_mutator_impl() was missing case handlers for
T_GraphPattern, T_GraphElementPattern, and T_GraphPropertyRef.
The corresponding expression_tree_walker_impl() already handled
all three node types, but the mutator did not, causing an
"unrecognized node type: 106" error whenever a GRAPH_TABLE
subquery appeared in a HAVING clause.

SELECT 1 FROM hv GROUP BY id
HAVING (SELECT COUNT(*) FROM GRAPH_TABLE(gh MATCH (a) COLUMNS (a.id AS x)))
> 0;
SET
ERROR:  unrecognized node type: 106

Attached a patch to address this.

Thanks,
Satya

---

From: Ashutosh Bapat <[email-masked]>
Date: Thu, 23 Apr 2026 12:18:55 +0530

On Tue, Apr 21, 2026 at 9:26 PM SATYANARAYANA NARLAPURAM
<[email-masked]> wrote:
> HAVING (SELECT COUNT(*) FROM GRAPH_TABLE(gh MATCH (a) COLUMNS (a.id AS x))) > 0;
> SET

Thanks for the report and the patch.

I first thought that the nodes needn't be part of the mutator since
they will be rewritten into some other nodes. But in this case the
mutator is being called from the transformation phase which does not
rewrite these nodes. I also found that range_table_mutator_impl()
invokes MUTATE on RangeTblEntry::graph_table and
RangeTblEntry::graph_table_columns which contain these three nodes in
their trees. So it seems like we forgot to handle these three nodes in
expression_tree_mutator(). From the name of the function, I thought it
only handles expressions (nodes which have Expr as their first
member). In that case adding GraphElementPattern and GraphPattern
should be handled somewhere else. But it seems the function also
handles Node types as well (e.g. List, TableFunc).

GraphPropertyRef has char *elvarname. Only the char * is copied by
FLATCOPY but not the character string it's pointing to. That made me a
bit uncomfortable because the mutated GraphPropertyRef would point to
the old name. But it looks like that's how other node types are
handled in that function. E.g. XmlExpr has member name which is also
FLATCOPY'ed. So we are good.

Why is MUTATE not called on GraphElementPattern::labelexpr and
GraphElementPattern::quantifier?

The test query is placed at the appropriate section in the file. I
would try to fit the query in lesser lines by placing SELECT, FROM,
GROUP BY on the same line. But that's more of a personal style.

-- 
Best Wishes,
Ashutosh Bapat

---

From: Ashutosh Bapat <[email-masked]>
Date: Tue, 28 Apr 2026 20:35:11 +0530

> GraphElementPattern::quantifier?

The walker didn't WALK labelexpr and quantifier either. Given that
labelexpr is a boolean expression it needs to be WALKed. quantifier is
IntList which is ignored by other nodes as well when WALKing. But we
need to copy quantifier in mutator otherwise the mutated expression
will point to the same IntList as the original node. Other nodes also
copy the OidLists and IntLists when mutating them. I have modified the
test query to cover the label expression mutator.

raw_expression_walker didn't cover labelexpr either. Added it there.
The label expression at that stage contains ColumnRef and BoolExpr
which are already covered by raw expression tree walker.

-- 
Best Wishes,
Ashutosh Bapat
From f0c7384ee37cf0b64d54efb53b2d97600ebdb928 Mon Sep 17 00:00:00 2001
From: satyanarayana narlapuram <[email-masked]>
Date: Tue, 21 Apr 2026 14:33:33 +0000
Subject: [PATCH v20260428 3/3] Handle nodes that may appear in GraphPattern
 expression trees

expression_tree_mutator_impl() was missing case handlers for T_GraphPattern,
T_GraphElementPattern, and T_GraphPropertyRef.  The corresponding
expression_tree_walker_impl() already handled all three node types, but the
mutator did not, causing an "unrecognized node type " error whenever a
GRAPH_TABLE appeared in an expression tree.

While at it also update raw_expression_tree_walker() and
expression_tree_walker() to handle missing nodes that may appear in GraphPattern
expression trees. When raw_expression_tree_walker() is called,
GraphElementPattern::labelexpr does contains ColumnRefs instead of
GraphLabelRefs. Hence those are not handled in raw_expression_tree_walker().

Reported by: Satyanarayana Narlapuram <[email-masked]>
Author: Satyanarayana Narlapuram <[email-masked]>
Author: Ashutosh Bapat <[email-masked]>
Reviewed by: Ashutosh Bapat <[email-masked]>
Discussion: https://www.postgresql.org/message-id/CAHg+QDc97WFTSkXg=[email-masked]
---
 src/backend/nodes/nodeFuncs.c             | 45 +++++++++++++++++++++++
 src/test/regress/expected/graph_table.out | 15 ++++++++
 src/test/regress/sql/graph_table.sql      |  9 +++++
 3 files changed, 69 insertions(+)

diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c
index 7edbd5b7225..db2eff343d7 100644
--- a/src/backend/nodes/nodeFuncs.c
+++ b/src/backend/nodes/nodeFuncs.c
@@ -2135,6 +2135,7 @@ expression_tree_walker_impl(Node *node,
 		case T_RangeTblRef:
 		case T_SortGroupClause:
 		case T_CTESearchClause:
+		case T_GraphLabelRef:
 		case T_GraphPropertyRef:
 		case T_MergeSupportFunc:
 			/* primitive node types with no expression subnodes */
@@ -2698,6 +2699,8 @@ expression_tree_walker_impl(Node *node,
 			{
 				GraphElementPattern *gep = (GraphElementPattern *) node;
 
+				if (WALK(gep->labelexpr))
+					return true;
 				if (WALK(gep->subexpr))
 					return true;
 				if (WALK(gep->whereClause))
@@ -3814,6 +3817,46 @@ expression_tree_mutator_impl(Node *node,
 				return (Node *) newnode;
 			}
 			break;
+		case T_GraphLabelRef:
+			{
+				GraphLabelRef *newnode;
+
+				FLATCOPY(newnode, node, GraphLabelRef);
+				return (Node *) newnode;
+			}
+			break;
+		case T_GraphPropertyRef:
+			{
+				GraphPropertyRef *newnode;
+
+				FLATCOPY(newnode, node, GraphPropertyRef);
+				return (Node *) newnode;
+			}
+			break;
+		case T_GraphElementPattern:
+			{
+				GraphElementPattern *gep = (GraphElementPattern *) node;
+				GraphElementPattern *newnode;
+
+				FLATCOPY(newnode, gep, GraphElementPattern);
+				MUTATE(newnode->labelexpr, gep->labelexpr, Node *);
+				MUTATE(newnode->subexpr, gep->subexpr, List *);
+				MUTATE(newnode->whereClause, gep->whereClause, Node *);
+				newnode->quantifier = list_copy(gep->quantifier);
+				return (Node *) newnode;
+			}
+			break;
+		case T_GraphPattern:
+			{
+				GraphPattern *gp = (GraphPattern *) node;
+				GraphPattern *newnode;
+
+				FLATCOPY(newnode, gp, GraphPattern);
+				MUTATE(newnode->path_pattern_list, gp->path_pattern_list, List *);
+				MUTATE(newnode->whereClause, gp->whereClause, Node *);
+				return (Node *) newnode;
+			}
+			break;
 		default:
 			elog(ERROR, "unrecognized node type: %d",
 				 (int) nodeTag(node));
@@ -4796,6 +4839,8 @@ raw_expression_tree_walker_impl(Node *node,
 			{
 				GraphElementPattern *gep = (GraphElementPattern *) node;
 
+				if (WALK(gep->labelexpr))
+					return true;
 				if (WALK(gep->subexpr))
 					return true;
 				if (WALK(gep->whereClause))
diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out
index 12b8706b5f3..8038fcd39b7 100644
--- a/src/test/regress/expected/graph_table.out
+++ b/src/test/regress/expected/graph_table.out
@@ -1032,4 +1032,19 @@ SELECT sname, dname FROM GRAPH_TABLE (g1 MATCH (src)->(dest) WHERE src.vprop1 >
 ERROR:  subqueries within GRAPH_TABLE reference are not supported
 SELECT sname, dname FROM GRAPH_TABLE (g1 MATCH (src)->(dest) WHERE out_degree(src.vname) > (SELECT max(out_degree(nname)) FROM GRAPH_TABLE (g1 MATCH (node) COLUMNS (node.vname AS nname))) COLUMNS(src.vname AS sname, dest.vname AS dname));
 ERROR:  subqueries within GRAPH_TABLE reference are not supported
+-- GRAPH_TABLE subquery in HAVING clause
+SELECT src.vname, count(*)
+  FROM v1 AS src
+  GROUP BY src.vname
+  HAVING count(*) >= (SELECT count(*)
+                        FROM GRAPH_TABLE (g1 MATCH (a IS vl1 | vl2)
+                                          COLUMNS (a.vname AS n))
+                       WHERE n = src.vname);
+ vname | count 
+-------+-------
+ v13   |     1
+ v12   |     1
+ v11   |     1
+(3 rows)
+
 -- leave the objects behind for pg_upgrade/pg_dump tests
diff --git a/src/test/regress/sql/graph_table.sql b/src/test/regress/sql/graph_table.sql
index a5df4647b6a..a3681c6c0ef 100644
--- a/src/test/regress/sql/graph_table.sql
+++ b/src/test/regress/sql/graph_table.sql
@@ -590,4 +590,13 @@ SELECT * FROM customers co WHERE co.customer_id = (SELECT customer_id FROM GRAPH
 SELECT sname, dname FROM GRAPH_TABLE (g1 MATCH (src)->(dest) WHERE src.vprop1 > (SELECT max(v1.vprop1) FROM v1) COLUMNS(src.vname AS sname, dest.vname AS dname));
 SELECT sname, dname FROM GRAPH_TABLE (g1 MATCH (src)->(dest) WHERE out_degree(src.vname) > (SELECT max(out_degree(nname)) FROM GRAPH_TABLE (g1 MATCH (node) COLUMNS (node.vname AS nname))) COLUMNS(src.vname AS sname, dest.vname AS dname));
 
+-- GRAPH_TABLE subquery in HAVING clause
+SELECT src.vname, count(*)
+  FROM v1 AS src
+  GROUP BY src.vname
+  HAVING count(*) >= (SELECT count(*)
+                        FROM GRAPH_TABLE (g1 MATCH (a IS vl1 | vl2)
+                                          COLUMNS (a.vname AS n))
+                       WHERE n = src.vname);
+
 -- leave the objects behind for pg_upgrade/pg_dump tests
-- 
2.34.1

---

From: Robert Haas <[email-masked]>
Date: Wed, 29 Apr 2026 16:46:57 -0400

> which are already covered by raw expression tree walker.

Hi,

Thanks for working on this. I ran into it independently today, and
then discovered this thread. In expression_tree_mutator_impl,
T_GraphLabelRef can be added to the "Primitive node types with no
expression subnodes" section just as was done in
expression_tree_walker_impl.

Other than that, this looks good to me.

-- 
Robert Haas
EDB: http://www.enterprisedb.com

---

From: Ashutosh Bapat <[email-masked]>
Date: Thu, 30 Apr 2026 12:14:13 +0530

On Thu, Apr 30, 2026 at 2:17 AM Robert Haas <[email-masked]> wrote:

Thanks for reviewing the patch.

> expression_tree_walker_impl.

Right. Fixed in the attached patch. Both GraphPropertyLabel and
GraphLabelRef need to be placed in that section since both are
primitive nodes.

-- 
Best Wishes,
Ashutosh Bapat
From f2e847421c52ea3073a82cc30ad0072d5cd68c58 Mon Sep 17 00:00:00 2001
From: satyanarayana narlapuram <[email-masked]>
Date: Tue, 21 Apr 2026 14:33:33 +0000
Subject: [PATCH v20260430 3/5] Handle nodes that may appear in GraphPattern
 expression trees

expression_tree_mutator_impl() was does not handle T_GraphPattern,
T_GraphElementPattern, and T_GraphPropertyRef.  The corresponding
expression_tree_walker_impl() already handles all three node types. This
causes an "unrecognized node type " error whenever a GRAPH_TABLE
appeared in an expression tree.

While at it also update raw_expression_tree_walker() and
expression_tree_walker() to handle missing nodes that may appear in
GraphPattern expression trees. When raw_expression_tree_walker() is
called, GraphElementPattern::labelexpr does contains ColumnRefs instead
of GraphLabelRefs. Hence those are not handled in
raw_expression_tree_walker().

Reported by: Satyanarayana Narlapuram <[email-masked]>
Author: Satyanarayana Narlapuram <[email-masked]>
Author: Ashutosh Bapat <[email-masked]>
Reviewed by: Ashutosh Bapat <[email-masked]>
Reviewed by: Robert Haas <[email-masked]>
Discussion: https://www.postgresql.org/message-id/CAHg+QDc97WFTSkXg=[email-masked]
---
 src/backend/nodes/nodeFuncs.c             | 31 +++++++++++++++++++++++
 src/test/regress/expected/graph_table.out | 15 +++++++++++
 src/test/regress/sql/graph_table.sql      |  9 +++++++
 3 files changed, 55 insertions(+)

index 7edbd5b7225..a712b76eeb1 100644
 
@@ -3062,6 +3065,8 @@ expression_tree_mutator_impl(Node *node,
 			return copyObject(node);
 		case T_WithCheckOption:
@@ -3814,6 +3819,30 @@ expression_tree_mutator_impl(Node *node,
@@ -4796,6 +4825,8 @@ raw_expression_tree_walker_impl(Node *node,

---

From: Peter Eisentraut <[email-masked]>
Date: Mon, 4 May 2026 17:39:23 +0200

> GraphLabelRef need to be placed in that section since both are

Committed.  (I reformatted the test query a little bit as you had 
suggested upstream.  Also, the ordering of the switch cases was slightly 
different between the walker and the mutator, which I fixed.)

---

From: Ashutosh Bapat <[email-masked]>
Date: Mon, 11 May 2026 11:45:59 +0530

> different between the walker and the mutator, which I fixed.)

Thanks. The changes look good to me.

-- 
Best Wishes,
Ashutosh Bapat