pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE
14+ messages / 5 participants
[nested] [flat]

* Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE
@ 2026-08-12 15:34  Sami Imseih <samimseih@gmail.com>
  0 siblings, 1 reply; 14+ messages in thread

From: Sami Imseih @ 2026-08-12 15:34 UTC (permalink / raw)
  To: pgsql-hackers; +Cc: Ewan Young <kdbase.hack@gmail.com>; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>

Hi,

Commit f585671055d1 [1] disallowed aggregates, window functions, and SRFs in a
GRAPH_TABLE COLUMNS list. For the aggregate case it tests pstate->p_hasAggs
after transforming the columns. That is not enough. An aggregate that
references an outer query is attributed to a parent query level, so
check_agglevels_and_constraints sets p_hasAggs on that parent ParseState and
not on the GRAPH_TABLE's own, so the aggregate is not caught.

There are two places this shows up.

1. An outer-referencing aggregate in the COLUMNS list.

```
  postgres=# CREATE TABLE customers (customer_id int PRIMARY KEY, name text);
  CREATE TABLE
  postgres=# CREATE PROPERTY GRAPH myshop VERTEX TABLES (customers);
  CREATE PROPERTY GRAPH
  postgres=# SELECT (SELECT num
  postgres(#         FROM GRAPH_TABLE (myshop MATCH (c IS customers)
  postgres(#                           COLUMNS (count(o.customer_id) AS num)) t)
  postgres-# FROM customers o;
  ERROR:  Aggref found in non-Agg plan node
```

2. The graph pattern WHERE clause was not checked at all in f585671055d1. A
same-level aggregate there is already rejected with "aggregate functions are
not allowed in WHERE", but an outer-referencing one is attributed to a parent
level is not caught, failing instead with "Aggref found in non-Agg plan node".

```
  postgres=# -- same-level aggregate
  postgres=# SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers
  postgres(#                            WHERE count(c.customer_id) > 0)
  postgres(#                            COLUMNS (c.name AS nm));
  ERROR:  aggregate functions are not allowed in WHERE

  postgres=# -- outer-referencing aggregate
  postgres=# SELECT (SELECT nm
  postgres(#         FROM GRAPH_TABLE (myshop MATCH (c IS customers
  postgres(#                           WHERE count(o.customer_id) > 0)
  postgres(#                           COLUMNS (c.name AS nm)) t)
  postgres-# FROM customers o;
  ERROR:  Aggref found in non-Agg plan node
```

Unlike the repro in f585671055d1, neither case results in an assertion
failure. But surfacing an internal planner error from user SQL is wrong on its
own.

The attached patch closes both gaps by walking the transformed COLUMNS list and
the graph pattern for Aggref and GroupingFunc nodes. Unlike p_hasAggs, the walk
detects an aggregate by its presence in those the trees. Window functions
and SRFs only mark the local ParseState, so their checks introduced in
f585671055d1 remain in place.

Thoughts?

[1] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=f585671055d1c56d6fba0bc5835d28e68248c...

--
Sami Imseih
Amazon Web Services (AWS)

Attachments:

v1-0001-Disallow-outer-reference-and-WHERE-clause-aggrega.patch (application/octet-stream, 7.2K) download From 7d6b309246a188f1cefb84c11d85865a47729100 Mon Sep 17 00:00:00 2001 From: Sami Imseih <samimseih@gmail.com> Date: Wed, 12 Aug 2026 14:19:42 +0000 Subject: [PATCH v1 1/1] Disallow outer-reference and WHERE-clause aggregates in GRAPH_TABLE Commit f585671055d1 disallowed aggregates, window functions, and SRFs in a GRAPH_TABLE COLUMNS list. Aggregates were detected with pstate->p_hasAggs after transforming the columns, while window functions and SRFs used the separate p_hasWindowFuncs and p_hasTargetSRFs flags. The aggregate check has two gaps. 1. An aggregate that references an outer query belongs to a parent query level, so check_agglevels_and_constraints sets p_hasAggs on the parent ParseState, not the GRAPH_TABLE's own. The local flag stays false and the aggregate is not identified. 2. The graph pattern WHERE clause was not checked at all. A local aggregate there is already rejected with "aggregate functions are not allowed in WHERE", but an outer-referencing one is attributed to a parent level and escapes that check as well. In either case the query reaches the executor and fails with the internal error "Aggref found in non-Agg plan node". These shapes do not trip the assertion seen in f585671055d1, but reaching an internal "planner messed up" error from user SQL is itself the bug. An unsupported feature should be rejected cleanly at parse time. Close both gaps by walking the transformed COLUMNS list and the graph pattern for Aggref and GroupingFunc nodes. Unlike p_hasAggs, the walk detects an aggregate by its presence in those trees rather than by the query level it belongs to. Window functions and SRFs always mark the local ParseState, so their existing flag checks are unchanged. Add tests covering these cases. --- src/backend/parser/parse_clause.c | 34 +++++++++++++++++++---- src/test/regress/expected/graph_table.out | 8 ++++++ src/test/regress/sql/graph_table.sql | 6 ++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c index 68b525ffdcc..33977767280 100644 --- a/src/backend/parser/parse_clause.c +++ b/src/backend/parser/parse_clause.c @@ -931,6 +931,24 @@ parserOpenPropGraph(ParseState *pstate, const RangeVar *relation, LOCKMODE lockm return rel; } +/* + * Walk a GRAPH_TABLE expression tree looking for an Aggref or GroupingFunc. + * We can't test pstate->p_hasAggs, since an outer-referencing aggregate sets + * that flag on a parent ParseState, not ours. + */ +static bool +graph_table_has_aggs_walker(Node *node, void *context) +{ + if (node == NULL) + return false; + + if (IsA(node, Aggref) || IsA(node, GroupingFunc)) + return true; + + return expression_tree_walker(node, graph_table_has_aggs_walker, + context); +} + /* * transformRangeGraphTable -- transform a GRAPH_TABLE clause */ @@ -946,7 +964,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) ListCell *lc; int resno = 0; bool saved_hasSublinks; - bool saved_hasAggs; bool saved_hasWindowFuncs; bool saved_hasTargetSRFs; @@ -970,8 +987,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) saved_hasSublinks = pstate->p_hasSubLinks; pstate->p_hasSubLinks = false; - saved_hasAggs = pstate->p_hasAggs; - pstate->p_hasAggs = false; saved_hasWindowFuncs = pstate->p_hasWindowFuncs; pstate->p_hasWindowFuncs = false; saved_hasTargetSRFs = pstate->p_hasTargetSRFs; @@ -1043,12 +1058,20 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) /* * GRAPH_TABLE cannot yet evaluate aggregate, window, or set-returning - * functions in its COLUMNS list, so prohibit them for now. + * functions, so prohibit them for now. Aggregates are found by walking + * the COLUMNS list and the graph pattern, because an outer-referencing + * aggregate does not set pstate->p_hasAggs on our ParseState. Window + * functions and SRFs always set their flag on our own ParseState, so a + * flag test finds them. */ - if (pstate->p_hasAggs) + if (graph_table_has_aggs_walker((Node *) columns, NULL)) ereport(ERROR, errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("aggregate functions in GRAPH_TABLE COLUMNS are not supported")); + if (graph_table_has_aggs_walker((Node *) gp, NULL)) + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("aggregate functions in GRAPH_TABLE WHERE clause are not supported")); if (pstate->p_hasWindowFuncs) ereport(ERROR, errcode(ERRCODE_FEATURE_NOT_SUPPORTED), @@ -1057,7 +1080,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) ereport(ERROR, errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("set-returning functions in GRAPH_TABLE COLUMNS are not supported")); - pstate->p_hasAggs = saved_hasAggs; pstate->p_hasWindowFuncs = saved_hasWindowFuncs; pstate->p_hasTargetSRFs = saved_hasTargetSRFs; diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out index cde3114ebf4..0b9a0856d65 100644 --- a/src/test/regress/expected/graph_table.out +++ b/src/test/regress/expected/graph_table.out @@ -478,6 +478,14 @@ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number() O ERROR: window functions in GRAPH_TABLE COLUMNS are not supported SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_series(1, 2) AS gs)); ERROR: set-returning functions in GRAPH_TABLE COLUMNS are not supported +-- also rejected when the aggregate references an outer query +SELECT (SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.customer_id) AS num)) t) +FROM customers o; +ERROR: aggregate functions in GRAPH_TABLE COLUMNS are not supported +-- likewise for an outer-referencing aggregate in a pattern WHERE clause +SELECT (SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.customer_id) > 0) COLUMNS (c.name AS nm)) t) +FROM customers o; +ERROR: aggregate functions in GRAPH_TABLE WHERE clause are not supported -- consecutive element patterns with same kind SELECT * FROM GRAPH_TABLE (g1 MATCH ()() COLUMNS (1 as one)); ERROR: adjacent vertex patterns are not supported diff --git a/src/test/regress/sql/graph_table.sql b/src/test/regress/sql/graph_table.sql index 7a4189833d8..4369921fafb 100644 --- a/src/test/regress/sql/graph_table.sql +++ b/src/test/regress/sql/graph_table.sql @@ -310,6 +310,12 @@ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT NULL)-[ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) AS num)); SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number() OVER () AS rn)); SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_series(1, 2) AS gs)); +-- also rejected when the aggregate references an outer query +SELECT (SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.customer_id) AS num)) t) +FROM customers o; +-- likewise for an outer-referencing aggregate in a pattern WHERE clause +SELECT (SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.customer_id) > 0) COLUMNS (c.name AS nm)) t) +FROM customers o; -- consecutive element patterns with same kind SELECT * FROM GRAPH_TABLE (g1 MATCH ()() COLUMNS (1 as one)); SELECT * FROM GRAPH_TABLE (g1 MATCH -> COLUMNS (1 AS one)); -- 2.47.3
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE
@ 2026-08-12 16:16  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Sami Imseih <samimseih@gmail.com>
  0 siblings, 1 reply; 14+ messages in thread

From: Tom Lane @ 2026-08-12 16:16 UTC (permalink / raw)
  To: Sami Imseih <samimseih@gmail.com>; +Cc: pgsql-hackers; Ewan Young <kdbase.hack@gmail.com>; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>

Sami Imseih <samimseih@gmail.com> writes:
> Commit f585671055d1 [1] disallowed aggregates, window functions, and SRFs in a
> GRAPH_TABLE COLUMNS list. For the aggregate case it tests pstate->p_hasAggs
> after transforming the columns. That is not enough. An aggregate that
> references an outer query is attributed to a parent query level, so
> check_agglevels_and_constraints sets p_hasAggs on that parent ParseState and
> not on the GRAPH_TABLE's own, so the aggregate is not caught.

It kind of seems like that should work, since an outer-level Agg is
effectively a constant within any execution of the lower query.
However, I'm content to say that making it work is out of scope for
v19.

> The attached patch closes both gaps by walking the transformed COLUMNS list and
> the graph pattern for Aggref and GroupingFunc nodes. Unlike p_hasAggs, the walk
> detects an aggregate by its presence in those the trees. Window functions
> and SRFs only mark the local ParseState, so their checks introduced in
> f585671055d1 remain in place.

I do not like this fix approach, and for that matter I don't like
f58567105.  This is expensive thanks to the extra tree traversal,
f58567105 is user-unfriendly because it fails to say exactly what
or where is the construct it's rejecting, and neither patch is
following the perfectly good structure that the parser already
has for this kind of check.  IMO the correct way to handle this
restriction is to check it in check_agglevels_and_constraints() based
on the ParseExprKind of the surrounding expression.  It looks like the
graph transformation steps in the parser think they can get away with
using EXPR_KIND_WHERE and EXPR_KIND_SELECT_TARGET, but that's just
wrong if the constraints on these subexpressions are any different
from what they are for regular WHERE and SELECT targets.  The right
way to go about this is to back-fill new ParseExprKind(s) as needed
and then make the necessary checks in a way similar to existing code.
Doing that might help you find other comparable oversights, too.

			regards, tom lane






^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE
@ 2026-08-12 22:53  Sami Imseih <samimseih@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 14+ messages in thread

From: Sami Imseih @ 2026-08-12 22:53 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-hackers; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>; Ewan Young <kdbase.hack@gmail.com>

Thanks for the comments!

> I do not like this fix approach, and for that matter I don't like
> f58567105.  This is expensive thanks to the extra tree traversal,

I was not feeling totally convinced about this either, but was not
sure if the tree traversal is that big of a problem. We should
definitely avoid doing this, if we can.

> f58567105 is user-unfriendly because it fails to say exactly what
> or where is the construct it's rejecting, and neither patch is
> following the perfectly good structure that the parser already
> has for this kind of check.  IMO the correct way to handle this
> restriction is to check it in check_agglevels_and_constraints() based
> on the ParseExprKind of the surrounding expression.

You are right. This makes sense to me now.  We can introduce
EXPR_KIND_GRAPH_TABLE_COLUMNS and
EXPR_KIND_GRAPH_TABLE_WHERE ParseExprKind's.

Inside check_agglevels_and_constraints(), we can check the restriction
before we walk up to the query level the aggregate belongs to.

Also, transformWindowFuncCall() and check_srf_call_placement() should
do the same thing, meaning the work done in f58567105is effectively
reverted.

The attached patch does this. Is this what you have in mind?

--
Sami

Attachments:

v2-0001-Rework-GRAPH_TABLE-aggregate-window-SRF-rejection.patch (application/octet-stream, 14.4K) download From ad75252b429d056bb81efd4dec98471436ba6275 Mon Sep 17 00:00:00 2001 From: Sami Imseih <samimseih@gmail.com> Date: Wed, 12 Aug 2026 19:24:07 +0000 Subject: [PATCH v2 1/1] Rework GRAPH_TABLE aggregate/window/SRF rejection using ParseExprKind Commit f585671055d1 disallowed aggregates, window functions, and set-returning functions in a GRAPH_TABLE COLUMNS list by inspecting the parse state after transforming the list. It reports no location, misses the graph pattern WHERE clause, and lets through an aggregate that references an outer query. Such an aggregate belongs to the outer query level, so the check on the GRAPH_TABLE's own level does not catch it and it reaches the executor as "Aggref found in non-Agg plan node". Instead give the COLUMNS list and the graph pattern WHERE clause their own ParseExprKind values and enforce the restriction through the parser's transformation functions check_agglevels_and_constraints(), transformWindowFuncCall(), and check_srf_call_placement(). It effectively reverts f585671055d1. Tests added for the outer-reference and WHERE clause. --- src/backend/parser/parse_agg.c | 26 ++++++++++++++++++ src/backend/parser/parse_clause.c | 32 +---------------------- src/backend/parser/parse_expr.c | 8 ++++++ src/backend/parser/parse_func.c | 4 +++ src/backend/parser/parse_graphtable.c | 6 ++--- src/include/parser/parse_node.h | 2 ++ src/test/regress/expected/graph_table.out | 28 +++++++++++++++++--- src/test/regress/sql/graph_table.sql | 7 ++++- 8 files changed, 74 insertions(+), 39 deletions(-) diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c index 754a20507d0..213f517563b 100644 --- a/src/backend/parser/parse_agg.c +++ b/src/backend/parser/parse_agg.c @@ -336,6 +336,23 @@ check_agglevels_and_constraints(ParseState *pstate, Node *expr) p_levelsup = &grp->agglevelsup; } + /* + * Reject aggregates and grouping operations written in a GRAPH_TABLE + * clause. pstate has not been walked up yet, so p_expr_kind here still + * reflects the clause the aggregate was written in. We must check now, + * before we walk up to the query level the aggregate belongs to, since + * for an aggregate referencing an outer query that is a different level + * and p_expr_kind would no longer reflect the GRAPH_TABLE clause. + */ + if (pstate->p_expr_kind == EXPR_KIND_GRAPH_TABLE_COLUMNS || + pstate->p_expr_kind == EXPR_KIND_GRAPH_TABLE_WHERE) + ereport(ERROR, + (errcode(ERRCODE_GROUPING_ERROR), + isAgg ? + errmsg("aggregate functions are not allowed in GRAPH_TABLE") : + errmsg("grouping operations are not allowed in GRAPH_TABLE"), + parser_errposition(pstate, location))); + /* * Check the arguments to compute the aggregate's level and detect * improper nesting. @@ -600,6 +617,11 @@ check_agglevels_and_constraints(ParseState *pstate, Node *expr) break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + /* rejected above */ + break; + /* * There is intentionally no default: case here, so that the * compiler will warn if we add a new ParseExprKind without @@ -1045,6 +1067,10 @@ transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, case EXPR_KIND_FOR_PORTION: err = _("window functions are not allowed in FOR PORTION OF expressions"); break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + err = _("window functions are not allowed in GRAPH_TABLE"); + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c index 68b525ffdcc..b1aa13eeb1d 100644 --- a/src/backend/parser/parse_clause.c +++ b/src/backend/parser/parse_clause.c @@ -946,9 +946,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) ListCell *lc; int resno = 0; bool saved_hasSublinks; - bool saved_hasAggs; - bool saved_hasWindowFuncs; - bool saved_hasTargetSRFs; rel = parserOpenPropGraph(pstate, rgt->graph_name, AccessShareLock); @@ -970,13 +967,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) saved_hasSublinks = pstate->p_hasSubLinks; pstate->p_hasSubLinks = false; - saved_hasAggs = pstate->p_hasAggs; - pstate->p_hasAggs = false; - saved_hasWindowFuncs = pstate->p_hasWindowFuncs; - pstate->p_hasWindowFuncs = false; - saved_hasTargetSRFs = pstate->p_hasTargetSRFs; - pstate->p_hasTargetSRFs = false; - gp = transformGraphPattern(pstate, rgt->graph_pattern); /* @@ -991,7 +981,7 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) TargetEntry *te; char *colname; - colexpr = transformExpr(pstate, rt->val, EXPR_KIND_SELECT_TARGET); + colexpr = transformExpr(pstate, rt->val, EXPR_KIND_GRAPH_TABLE_COLUMNS); if (rt->name) colname = rt->name; @@ -1041,26 +1031,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) errmsg("subqueries within GRAPH_TABLE reference are not supported"))); pstate->p_hasSubLinks = saved_hasSublinks; - /* - * GRAPH_TABLE cannot yet evaluate aggregate, window, or set-returning - * functions in its COLUMNS list, so prohibit them for now. - */ - if (pstate->p_hasAggs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("aggregate functions in GRAPH_TABLE COLUMNS are not supported")); - if (pstate->p_hasWindowFuncs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("window functions in GRAPH_TABLE COLUMNS are not supported")); - if (pstate->p_hasTargetSRFs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("set-returning functions in GRAPH_TABLE COLUMNS are not supported")); - pstate->p_hasAggs = saved_hasAggs; - pstate->p_hasWindowFuncs = saved_hasWindowFuncs; - pstate->p_hasTargetSRFs = saved_hasTargetSRFs; - return addRangeTableEntryForGraphTable(pstate, graphid, castNode(GraphPattern, gp), columns, colnames, rgt->alias, false, true); } diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 30c889f505f..f1c6aa236cf 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -579,6 +579,8 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref) case EXPR_KIND_GENERATED_COLUMN: case EXPR_KIND_CYCLE_MARK: case EXPR_KIND_PROPGRAPH_PROPERTY: + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: /* okay */ break; @@ -1843,6 +1845,8 @@ transformSubLink(ParseState *pstate, SubLink *sublink) case EXPR_KIND_VALUES: case EXPR_KIND_VALUES_SINGLE: case EXPR_KIND_CYCLE_MARK: + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: /* okay */ break; case EXPR_KIND_CHECK_CONSTRAINT: @@ -3255,6 +3259,10 @@ ParseExprKindName(ParseExprKind exprKind) return "property definition expression"; case EXPR_KIND_FOR_PORTION: return "FOR PORTION OF"; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + return "GRAPH_TABLE COLUMNS"; + case EXPR_KIND_GRAPH_TABLE_WHERE: + return "GRAPH_TABLE WHERE"; /* * 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 c87804f5d41..0bb3398ce25 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -2848,6 +2848,10 @@ check_srf_call_placement(ParseState *pstate, Node *last_srf, int location) case EXPR_KIND_FOR_PORTION: err = _("set-returning functions are not allowed in FOR PORTION OF expressions"); break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + err = _("set-returning functions are not allowed in GRAPH_TABLE"); + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_graphtable.c b/src/backend/parser/parse_graphtable.c index 73fbfb541f7..e323376f0ea 100644 --- a/src/backend/parser/parse_graphtable.c +++ b/src/backend/parser/parse_graphtable.c @@ -92,7 +92,7 @@ transformGraphTablePropertyRef(ParseState *pstate, ColumnRef *cref) if (IsA(field1, A_Star) || IsA(field2, A_Star)) { - if (pstate->p_expr_kind == EXPR_KIND_SELECT_TARGET) + if (pstate->p_expr_kind == EXPR_KIND_GRAPH_TABLE_COLUMNS) ereport(ERROR, errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("\"*\" is not supported here"), @@ -251,7 +251,7 @@ transformGraphElementPattern(ParseState *pstate, GraphElementPattern *gep) gep->labelexpr = transformLabelExpr(gpstate, gep->labelexpr); - gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_WHERE); + gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_GRAPH_TABLE_WHERE); /* * Assign collations here for the reason mentioned in the prologue of @@ -387,7 +387,7 @@ transformGraphPattern(ParseState *pstate, GraphPattern *graph_pattern) transformPathPatternList(pstate, graph_pattern->path_pattern_list)); graph_pattern->path_pattern_list = path_pattern_list; - graph_pattern->whereClause = transformExpr(pstate, graph_pattern->whereClause, EXPR_KIND_WHERE); + graph_pattern->whereClause = transformExpr(pstate, graph_pattern->whereClause, EXPR_KIND_GRAPH_TABLE_WHERE); assign_expr_collations(pstate, graph_pattern->whereClause); return (Node *) graph_pattern; diff --git a/src/include/parser/parse_node.h b/src/include/parser/parse_node.h index f7f4ba6c2a8..2b8ac813554 100644 --- a/src/include/parser/parse_node.h +++ b/src/include/parser/parse_node.h @@ -84,6 +84,8 @@ typedef enum ParseExprKind EXPR_KIND_GENERATED_COLUMN, /* generation expression for a column */ EXPR_KIND_CYCLE_MARK, /* cycle mark value */ EXPR_KIND_PROPGRAPH_PROPERTY, /* derived property expression */ + EXPR_KIND_GRAPH_TABLE_COLUMNS, /* GRAPH_TABLE COLUMNS list item */ + EXPR_KIND_GRAPH_TABLE_WHERE, /* WHERE in a GRAPH_TABLE pattern */ } ParseExprKind; diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out index cde3114ebf4..b9bb314f191 100644 --- a/src/test/regress/expected/graph_table.out +++ b/src/test/regress/expected/graph_table.out @@ -471,13 +471,33 @@ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT NULL)-[ ERROR: "*" not allowed here LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT... ^ --- aggregate, window, and set-returning functions are not supported in COLUMNS +-- aggregate, window, and set-returning functions are not allowed in the +-- COLUMNS list or the graph pattern WHERE, including an aggregate that +-- references an outer query SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) AS num)); -ERROR: aggregate functions in GRAPH_TABLE COLUMNS are not supported +ERROR: aggregate functions are not allowed in GRAPH_TABLE +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) A... + ^ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number() OVER () AS rn)); -ERROR: window functions in GRAPH_TABLE COLUMNS are not supported +ERROR: window functions are not allowed in GRAPH_TABLE +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number... + ^ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_series(1, 2) AS gs)); -ERROR: set-returning functions in GRAPH_TABLE COLUMNS are not supported +ERROR: set-returning functions are not allowed in GRAPH_TABLE +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_s... + ^ +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id) > 0) COLUMNS (c.name AS nm)); +ERROR: aggregate functions are not allowed in GRAPH_TABLE +LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.cu... + ^ +SELECT (SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.customer_id) AS num)) t) FROM customers o; +ERROR: aggregate functions are not allowed in GRAPH_TABLE +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.cu... + ^ +SELECT (SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; +ERROR: aggregate functions are not allowed in GRAPH_TABLE +LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.cu... + ^ -- consecutive element patterns with same kind SELECT * FROM GRAPH_TABLE (g1 MATCH ()() COLUMNS (1 as one)); ERROR: adjacent vertex patterns are not supported diff --git a/src/test/regress/sql/graph_table.sql b/src/test/regress/sql/graph_table.sql index 7a4189833d8..c2ca688374c 100644 --- a/src/test/regress/sql/graph_table.sql +++ b/src/test/regress/sql/graph_table.sql @@ -306,10 +306,15 @@ SELECT * FROM GRAPH_TABLE (g1 MATCH (src IS el1 | vl1)-[conn]->(dest) COLUMNS (c SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.address = 'US')-[IS customer_orders]->(o IS orders) COLUMNS (c.*)); -- star anywhere else is not allowed as a property reference SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT NULL)-[IS customer_orders]->(o IS orders) COLUMNS (c.name)); --- aggregate, window, and set-returning functions are not supported in COLUMNS +-- aggregate, window, and set-returning functions are not allowed in the +-- COLUMNS list or the graph pattern WHERE, including an aggregate that +-- references an outer query SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) AS num)); SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number() OVER () AS rn)); SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_series(1, 2) AS gs)); +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id) > 0) COLUMNS (c.name AS nm)); +SELECT (SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.customer_id) AS num)) t) FROM customers o; +SELECT (SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; -- consecutive element patterns with same kind SELECT * FROM GRAPH_TABLE (g1 MATCH ()() COLUMNS (1 as one)); SELECT * FROM GRAPH_TABLE (g1 MATCH -> COLUMNS (1 AS one)); -- 2.47.3
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE
@ 2026-08-13 15:35  Sami Imseih <samimseih@gmail.com>
  parent: Sami Imseih <samimseih@gmail.com>
  0 siblings, 1 reply; 14+ messages in thread

From: Sami Imseih @ 2026-08-13 15:35 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-hackers; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>; Ewan Young <kdbase.hack@gmail.com>

v3 attached. No .c changes from v2.

Added tests for GROUPING and for an aggregate in the graph-pattern WHERE.
Also dropped the inner-reference element-pattern WHERE test, since it would
have passed under f585671055d1 and proved nothing. The outer-reference
element-pattern test was already present.

Also, I created a CF entry https://commitfest.postgresql.org/patch/7144/

--
Sami

Attachments:

v3-0001-Rework-GRAPH_TABLE-aggregate-window-SRF-rejection.patch (application/octet-stream, 15.3K) download From bfc5d794a12996db1d0ebe0b67503635fb11f6ad Mon Sep 17 00:00:00 2001 From: Sami Imseih <samimseih@gmail.com> Date: Wed, 12 Aug 2026 19:24:07 +0000 Subject: [PATCH v3 1/1] Rework GRAPH_TABLE aggregate/window/SRF rejection using ParseExprKind Commit f585671055d1 disallowed aggregates, window functions, and set-returning functions in a GRAPH_TABLE COLUMNS list by inspecting the parse state after transforming the list. It reports no location, misses the graph pattern WHERE clause, and lets through an aggregate that references an outer query. Such an aggregate belongs to the outer query level, so the check on the GRAPH_TABLE's own level does not catch it and it reaches the executor as "Aggref found in non-Agg plan node". Instead give the COLUMNS list and the graph pattern WHERE clause their own ParseExprKind values and enforce the restriction through the parser's transformation functions check_agglevels_and_constraints(), transformWindowFuncCall(), and check_srf_call_placement(). It effectively reverts f585671055d1. Because the checks now happen in those transformation functions, the errors carry the location of the aggregate, window function, or SRF via parser_errposition() instead of having no location at all. Note that this changes the SQLSTATE for a rejected aggregate from ERRCODE_FEATURE_NOT_SUPPORTED to ERRCODE_GROUPING_ERROR, consistent with how aggregates are rejected in other clauses. Tests added for the outer-reference and WHERE clause. --- src/backend/parser/parse_agg.c | 26 ++++++++++++++++++ src/backend/parser/parse_clause.c | 32 +---------------------- src/backend/parser/parse_expr.c | 8 ++++++ src/backend/parser/parse_func.c | 4 +++ src/backend/parser/parse_graphtable.c | 6 ++--- src/include/parser/parse_node.h | 2 ++ src/test/regress/expected/graph_table.out | 32 ++++++++++++++++++++--- src/test/regress/sql/graph_table.sql | 8 +++++- 8 files changed, 79 insertions(+), 39 deletions(-) diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c index 754a20507d0..213f517563b 100644 --- a/src/backend/parser/parse_agg.c +++ b/src/backend/parser/parse_agg.c @@ -336,6 +336,23 @@ check_agglevels_and_constraints(ParseState *pstate, Node *expr) p_levelsup = &grp->agglevelsup; } + /* + * Reject aggregates and grouping operations written in a GRAPH_TABLE + * clause. pstate has not been walked up yet, so p_expr_kind here still + * reflects the clause the aggregate was written in. We must check now, + * before we walk up to the query level the aggregate belongs to, since + * for an aggregate referencing an outer query that is a different level + * and p_expr_kind would no longer reflect the GRAPH_TABLE clause. + */ + if (pstate->p_expr_kind == EXPR_KIND_GRAPH_TABLE_COLUMNS || + pstate->p_expr_kind == EXPR_KIND_GRAPH_TABLE_WHERE) + ereport(ERROR, + (errcode(ERRCODE_GROUPING_ERROR), + isAgg ? + errmsg("aggregate functions are not allowed in GRAPH_TABLE") : + errmsg("grouping operations are not allowed in GRAPH_TABLE"), + parser_errposition(pstate, location))); + /* * Check the arguments to compute the aggregate's level and detect * improper nesting. @@ -600,6 +617,11 @@ check_agglevels_and_constraints(ParseState *pstate, Node *expr) break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + /* rejected above */ + break; + /* * There is intentionally no default: case here, so that the * compiler will warn if we add a new ParseExprKind without @@ -1045,6 +1067,10 @@ transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, case EXPR_KIND_FOR_PORTION: err = _("window functions are not allowed in FOR PORTION OF expressions"); break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + err = _("window functions are not allowed in GRAPH_TABLE"); + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c index 68b525ffdcc..b1aa13eeb1d 100644 --- a/src/backend/parser/parse_clause.c +++ b/src/backend/parser/parse_clause.c @@ -946,9 +946,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) ListCell *lc; int resno = 0; bool saved_hasSublinks; - bool saved_hasAggs; - bool saved_hasWindowFuncs; - bool saved_hasTargetSRFs; rel = parserOpenPropGraph(pstate, rgt->graph_name, AccessShareLock); @@ -970,13 +967,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) saved_hasSublinks = pstate->p_hasSubLinks; pstate->p_hasSubLinks = false; - saved_hasAggs = pstate->p_hasAggs; - pstate->p_hasAggs = false; - saved_hasWindowFuncs = pstate->p_hasWindowFuncs; - pstate->p_hasWindowFuncs = false; - saved_hasTargetSRFs = pstate->p_hasTargetSRFs; - pstate->p_hasTargetSRFs = false; - gp = transformGraphPattern(pstate, rgt->graph_pattern); /* @@ -991,7 +981,7 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) TargetEntry *te; char *colname; - colexpr = transformExpr(pstate, rt->val, EXPR_KIND_SELECT_TARGET); + colexpr = transformExpr(pstate, rt->val, EXPR_KIND_GRAPH_TABLE_COLUMNS); if (rt->name) colname = rt->name; @@ -1041,26 +1031,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) errmsg("subqueries within GRAPH_TABLE reference are not supported"))); pstate->p_hasSubLinks = saved_hasSublinks; - /* - * GRAPH_TABLE cannot yet evaluate aggregate, window, or set-returning - * functions in its COLUMNS list, so prohibit them for now. - */ - if (pstate->p_hasAggs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("aggregate functions in GRAPH_TABLE COLUMNS are not supported")); - if (pstate->p_hasWindowFuncs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("window functions in GRAPH_TABLE COLUMNS are not supported")); - if (pstate->p_hasTargetSRFs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("set-returning functions in GRAPH_TABLE COLUMNS are not supported")); - pstate->p_hasAggs = saved_hasAggs; - pstate->p_hasWindowFuncs = saved_hasWindowFuncs; - pstate->p_hasTargetSRFs = saved_hasTargetSRFs; - return addRangeTableEntryForGraphTable(pstate, graphid, castNode(GraphPattern, gp), columns, colnames, rgt->alias, false, true); } diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 30c889f505f..f1c6aa236cf 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -579,6 +579,8 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref) case EXPR_KIND_GENERATED_COLUMN: case EXPR_KIND_CYCLE_MARK: case EXPR_KIND_PROPGRAPH_PROPERTY: + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: /* okay */ break; @@ -1843,6 +1845,8 @@ transformSubLink(ParseState *pstate, SubLink *sublink) case EXPR_KIND_VALUES: case EXPR_KIND_VALUES_SINGLE: case EXPR_KIND_CYCLE_MARK: + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: /* okay */ break; case EXPR_KIND_CHECK_CONSTRAINT: @@ -3255,6 +3259,10 @@ ParseExprKindName(ParseExprKind exprKind) return "property definition expression"; case EXPR_KIND_FOR_PORTION: return "FOR PORTION OF"; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + return "GRAPH_TABLE COLUMNS"; + case EXPR_KIND_GRAPH_TABLE_WHERE: + return "GRAPH_TABLE WHERE"; /* * 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 c87804f5d41..0bb3398ce25 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -2848,6 +2848,10 @@ check_srf_call_placement(ParseState *pstate, Node *last_srf, int location) case EXPR_KIND_FOR_PORTION: err = _("set-returning functions are not allowed in FOR PORTION OF expressions"); break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + err = _("set-returning functions are not allowed in GRAPH_TABLE"); + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_graphtable.c b/src/backend/parser/parse_graphtable.c index 73fbfb541f7..e323376f0ea 100644 --- a/src/backend/parser/parse_graphtable.c +++ b/src/backend/parser/parse_graphtable.c @@ -92,7 +92,7 @@ transformGraphTablePropertyRef(ParseState *pstate, ColumnRef *cref) if (IsA(field1, A_Star) || IsA(field2, A_Star)) { - if (pstate->p_expr_kind == EXPR_KIND_SELECT_TARGET) + if (pstate->p_expr_kind == EXPR_KIND_GRAPH_TABLE_COLUMNS) ereport(ERROR, errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("\"*\" is not supported here"), @@ -251,7 +251,7 @@ transformGraphElementPattern(ParseState *pstate, GraphElementPattern *gep) gep->labelexpr = transformLabelExpr(gpstate, gep->labelexpr); - gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_WHERE); + gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_GRAPH_TABLE_WHERE); /* * Assign collations here for the reason mentioned in the prologue of @@ -387,7 +387,7 @@ transformGraphPattern(ParseState *pstate, GraphPattern *graph_pattern) transformPathPatternList(pstate, graph_pattern->path_pattern_list)); graph_pattern->path_pattern_list = path_pattern_list; - graph_pattern->whereClause = transformExpr(pstate, graph_pattern->whereClause, EXPR_KIND_WHERE); + graph_pattern->whereClause = transformExpr(pstate, graph_pattern->whereClause, EXPR_KIND_GRAPH_TABLE_WHERE); assign_expr_collations(pstate, graph_pattern->whereClause); return (Node *) graph_pattern; diff --git a/src/include/parser/parse_node.h b/src/include/parser/parse_node.h index f7f4ba6c2a8..2b8ac813554 100644 --- a/src/include/parser/parse_node.h +++ b/src/include/parser/parse_node.h @@ -84,6 +84,8 @@ typedef enum ParseExprKind EXPR_KIND_GENERATED_COLUMN, /* generation expression for a column */ EXPR_KIND_CYCLE_MARK, /* cycle mark value */ EXPR_KIND_PROPGRAPH_PROPERTY, /* derived property expression */ + EXPR_KIND_GRAPH_TABLE_COLUMNS, /* GRAPH_TABLE COLUMNS list item */ + EXPR_KIND_GRAPH_TABLE_WHERE, /* WHERE in a GRAPH_TABLE pattern */ } ParseExprKind; diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out index cde3114ebf4..3894f0f8aed 100644 --- a/src/test/regress/expected/graph_table.out +++ b/src/test/regress/expected/graph_table.out @@ -471,13 +471,37 @@ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT NULL)-[ ERROR: "*" not allowed here LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT... ^ --- aggregate, window, and set-returning functions are not supported in COLUMNS +-- aggregate, grouping, window, and set-returning functions are not allowed +-- in the COLUMNS list or the graph pattern WHERE, including an aggregate that +-- references an outer query SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) AS num)); -ERROR: aggregate functions in GRAPH_TABLE COLUMNS are not supported +ERROR: aggregate functions are not allowed in GRAPH_TABLE +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) A... + ^ +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c.customer_id) AS g)); +ERROR: grouping operations are not allowed in GRAPH_TABLE +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c... + ^ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number() OVER () AS rn)); -ERROR: window functions in GRAPH_TABLE COLUMNS are not supported +ERROR: window functions are not allowed in GRAPH_TABLE +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number... + ^ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_series(1, 2) AS gs)); -ERROR: set-returning functions in GRAPH_TABLE COLUMNS are not supported +ERROR: set-returning functions are not allowed in GRAPH_TABLE +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_s... + ^ +SELECT (SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.customer_id) AS num)) t) FROM customers o; +ERROR: aggregate functions are not allowed in GRAPH_TABLE +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.cu... + ^ +SELECT (SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers) WHERE count(o.customer_id) > 0 COLUMNS (c.name AS nm)) t) FROM customers o; +ERROR: aggregate functions are not allowed in GRAPH_TABLE +LINE 1: ... GRAPH_TABLE (myshop MATCH (c IS customers) WHERE count(o.cu... + ^ +SELECT (SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; +ERROR: aggregate functions are not allowed in GRAPH_TABLE +LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.cu... + ^ -- consecutive element patterns with same kind SELECT * FROM GRAPH_TABLE (g1 MATCH ()() COLUMNS (1 as one)); ERROR: adjacent vertex patterns are not supported diff --git a/src/test/regress/sql/graph_table.sql b/src/test/regress/sql/graph_table.sql index 7a4189833d8..05ea2b5f02f 100644 --- a/src/test/regress/sql/graph_table.sql +++ b/src/test/regress/sql/graph_table.sql @@ -306,10 +306,16 @@ SELECT * FROM GRAPH_TABLE (g1 MATCH (src IS el1 | vl1)-[conn]->(dest) COLUMNS (c SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.address = 'US')-[IS customer_orders]->(o IS orders) COLUMNS (c.*)); -- star anywhere else is not allowed as a property reference SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT NULL)-[IS customer_orders]->(o IS orders) COLUMNS (c.name)); --- aggregate, window, and set-returning functions are not supported in COLUMNS +-- aggregate, grouping, window, and set-returning functions are not allowed +-- in the COLUMNS list or the graph pattern WHERE, including an aggregate that +-- references an outer query SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) AS num)); +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c.customer_id) AS g)); SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number() OVER () AS rn)); SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_series(1, 2) AS gs)); +SELECT (SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.customer_id) AS num)) t) FROM customers o; +SELECT (SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers) WHERE count(o.customer_id) > 0 COLUMNS (c.name AS nm)) t) FROM customers o; +SELECT (SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; -- consecutive element patterns with same kind SELECT * FROM GRAPH_TABLE (g1 MATCH ()() COLUMNS (1 as one)); SELECT * FROM GRAPH_TABLE (g1 MATCH -> COLUMNS (1 AS one)); -- 2.47.3
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE
@ 2026-08-15 19:21  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Sami Imseih <samimseih@gmail.com>
  0 siblings, 1 reply; 14+ messages in thread

From: Tom Lane @ 2026-08-15 19:21 UTC (permalink / raw)
  To: Sami Imseih <samimseih@gmail.com>; +Cc: pgsql-hackers; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>; Ewan Young <kdbase.hack@gmail.com>

Sami Imseih <samimseih@gmail.com> writes:
> v3 attached. No .c changes from v2.

I looked this over.  The general pattern of adding more EXPR_KIND
values looks fine, but I really didn't like the nonstandard way in
which you tested for GRAPH_TABLE in check_agglevels_and_constraints.
Testing before running up to the aggregate's semantic level is just
wrong: it would fail to reject an aggregate within a subquery within
one of these GRAPH_TABLE clauses.  And if we're testing at some other
level than the semantic level, should we also reject GRAPH_TABLE
context at intermediate parse levels?

This was ugly enough that it motivated me to go look at exactly why
an outer aggregate doesn't work here, in hopes of removing the
inconsistent restriction.  I found it: replace_property_refs_mutator,
which increments Vars' varlevelsup to account for the fact that
they're being pushed into a subquery, failed to do the equivalent
thing for Aggrefs and GroupingFuncs.  (Compare, for instance,
IncrementVarSublevelsUp.)  The case seems to Just Work after fixing
that, so I changed check_agglevels_and_constraints to enforce the
restriction in just the same way as it does for other clauses where
we disallow aggs.

As a minor improvement, you can just use the "errkind = true" option
in these error-reporting functions; less code, fewer strings for the
translators to deal with, same or better message wording.

v4 attached.  I think this is committable if you don't see anything
else to change.

			regards, tom lane

Attachments:

v4-0001-Rework-GRAPH_TABLE-aggregate-window-SRF-rejection.patch (text/x-diff, 16.3K) download From 4ba70c021a02f864f660c09bd89591c4292db691 Mon Sep 17 00:00:00 2001 From: Tom Lane <tgl@sss.pgh.pa.us> Date: Sat, 15 Aug 2026 15:08:13 -0400 Subject: [PATCH v4] Rework GRAPH_TABLE aggregate/window/SRF rejection using ParseExprKind. Commit f58567105 disallowed aggregates, window functions, and set-returning functions in a GRAPH_TABLE COLUMNS list by inspecting the parse state after transforming the list. This way is not capable of reporting a parse location for the misplaced construct; worse, the patch missed checking for aggregates etc. in GRAPH_TABLE WHERE. Instead give the COLUMNS list and the graph pattern WHERE clause their own ParseExprKind values and enforce the restriction within the parser's transformation functions check_agglevels_and_constraints(), transformWindowFuncCall(), and check_srf_call_placement(). (This reverts the code changes of f58567105, though we keep the test cases and add some more.) This is more consistent with how the parser implements other misplaced-construct checks, and it allows delivery of better error messages. The parsing check rejects only aggregates having level zero. This is intentional: an outer-level aggregate is effectively a constant within the subquery containing GRAPH_TABLE, so there's no reason not to allow it. The case did not work before, but that seems to be only because replace_property_refs_mutator neglected its duty to mutate agglevelsup in the same way as varlevelsup. Note that this changes the SQLSTATE for a rejected aggregate from ERRCODE_FEATURE_NOT_SUPPORTED to ERRCODE_GROUPING_ERROR, consistent with how misplaced aggregates are rejected in other cases. Author: Sami Imseih <samimseih@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/CAA5RZ0tvdYODLQvYwVzAxUPe5=E3vSe8Zy7TvrQq+syvGKpHSQ@mail.gmail.com Backpatch-through: 19 --- src/backend/parser/parse_agg.c | 10 +++++ src/backend/parser/parse_clause.c | 32 +-------------- src/backend/parser/parse_expr.c | 8 ++++ src/backend/parser/parse_func.c | 4 ++ src/backend/parser/parse_graphtable.c | 6 +-- src/backend/rewrite/rewriteGraphTable.c | 34 ++++++++++++++++ src/include/parser/parse_node.h | 2 + src/test/regress/expected/graph_table.out | 49 +++++++++++++++++++++-- src/test/regress/sql/graph_table.sql | 9 ++++- 9 files changed, 115 insertions(+), 39 deletions(-) diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c index 754a20507d0..a80d11215e2 100644 --- a/src/backend/parser/parse_agg.c +++ b/src/backend/parser/parse_agg.c @@ -600,6 +600,12 @@ check_agglevels_and_constraints(ParseState *pstate, Node *expr) break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + errkind = true; + + break; + /* * There is intentionally no default: case here, so that the * compiler will warn if we add a new ParseExprKind without @@ -1045,6 +1051,10 @@ transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, case EXPR_KIND_FOR_PORTION: err = _("window functions are not allowed in FOR PORTION OF expressions"); break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + errkind = true; + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c index 68b525ffdcc..b1aa13eeb1d 100644 --- a/src/backend/parser/parse_clause.c +++ b/src/backend/parser/parse_clause.c @@ -946,9 +946,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) ListCell *lc; int resno = 0; bool saved_hasSublinks; - bool saved_hasAggs; - bool saved_hasWindowFuncs; - bool saved_hasTargetSRFs; rel = parserOpenPropGraph(pstate, rgt->graph_name, AccessShareLock); @@ -970,13 +967,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) saved_hasSublinks = pstate->p_hasSubLinks; pstate->p_hasSubLinks = false; - saved_hasAggs = pstate->p_hasAggs; - pstate->p_hasAggs = false; - saved_hasWindowFuncs = pstate->p_hasWindowFuncs; - pstate->p_hasWindowFuncs = false; - saved_hasTargetSRFs = pstate->p_hasTargetSRFs; - pstate->p_hasTargetSRFs = false; - gp = transformGraphPattern(pstate, rgt->graph_pattern); /* @@ -991,7 +981,7 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) TargetEntry *te; char *colname; - colexpr = transformExpr(pstate, rt->val, EXPR_KIND_SELECT_TARGET); + colexpr = transformExpr(pstate, rt->val, EXPR_KIND_GRAPH_TABLE_COLUMNS); if (rt->name) colname = rt->name; @@ -1041,26 +1031,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) errmsg("subqueries within GRAPH_TABLE reference are not supported"))); pstate->p_hasSubLinks = saved_hasSublinks; - /* - * GRAPH_TABLE cannot yet evaluate aggregate, window, or set-returning - * functions in its COLUMNS list, so prohibit them for now. - */ - if (pstate->p_hasAggs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("aggregate functions in GRAPH_TABLE COLUMNS are not supported")); - if (pstate->p_hasWindowFuncs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("window functions in GRAPH_TABLE COLUMNS are not supported")); - if (pstate->p_hasTargetSRFs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("set-returning functions in GRAPH_TABLE COLUMNS are not supported")); - pstate->p_hasAggs = saved_hasAggs; - pstate->p_hasWindowFuncs = saved_hasWindowFuncs; - pstate->p_hasTargetSRFs = saved_hasTargetSRFs; - return addRangeTableEntryForGraphTable(pstate, graphid, castNode(GraphPattern, gp), columns, colnames, rgt->alias, false, true); } diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 30c889f505f..f1c6aa236cf 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -579,6 +579,8 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref) case EXPR_KIND_GENERATED_COLUMN: case EXPR_KIND_CYCLE_MARK: case EXPR_KIND_PROPGRAPH_PROPERTY: + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: /* okay */ break; @@ -1843,6 +1845,8 @@ transformSubLink(ParseState *pstate, SubLink *sublink) case EXPR_KIND_VALUES: case EXPR_KIND_VALUES_SINGLE: case EXPR_KIND_CYCLE_MARK: + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: /* okay */ break; case EXPR_KIND_CHECK_CONSTRAINT: @@ -3255,6 +3259,10 @@ ParseExprKindName(ParseExprKind exprKind) return "property definition expression"; case EXPR_KIND_FOR_PORTION: return "FOR PORTION OF"; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + return "GRAPH_TABLE COLUMNS"; + case EXPR_KIND_GRAPH_TABLE_WHERE: + return "GRAPH_TABLE WHERE"; /* * 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 c87804f5d41..0c72b95e4a5 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -2848,6 +2848,10 @@ check_srf_call_placement(ParseState *pstate, Node *last_srf, int location) case EXPR_KIND_FOR_PORTION: err = _("set-returning functions are not allowed in FOR PORTION OF expressions"); break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + errkind = true; + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_graphtable.c b/src/backend/parser/parse_graphtable.c index 73fbfb541f7..e323376f0ea 100644 --- a/src/backend/parser/parse_graphtable.c +++ b/src/backend/parser/parse_graphtable.c @@ -92,7 +92,7 @@ transformGraphTablePropertyRef(ParseState *pstate, ColumnRef *cref) if (IsA(field1, A_Star) || IsA(field2, A_Star)) { - if (pstate->p_expr_kind == EXPR_KIND_SELECT_TARGET) + if (pstate->p_expr_kind == EXPR_KIND_GRAPH_TABLE_COLUMNS) ereport(ERROR, errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("\"*\" is not supported here"), @@ -251,7 +251,7 @@ transformGraphElementPattern(ParseState *pstate, GraphElementPattern *gep) gep->labelexpr = transformLabelExpr(gpstate, gep->labelexpr); - gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_WHERE); + gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_GRAPH_TABLE_WHERE); /* * Assign collations here for the reason mentioned in the prologue of @@ -387,7 +387,7 @@ transformGraphPattern(ParseState *pstate, GraphPattern *graph_pattern) transformPathPatternList(pstate, graph_pattern->path_pattern_list)); graph_pattern->path_pattern_list = path_pattern_list; - graph_pattern->whereClause = transformExpr(pstate, graph_pattern->whereClause, EXPR_KIND_WHERE); + graph_pattern->whereClause = transformExpr(pstate, graph_pattern->whereClause, EXPR_KIND_GRAPH_TABLE_WHERE); assign_expr_collations(pstate, graph_pattern->whereClause); return (Node *) graph_pattern; diff --git a/src/backend/rewrite/rewriteGraphTable.c b/src/backend/rewrite/rewriteGraphTable.c index 0eaf28b3de5..9b5495ffaa2 100644 --- a/src/backend/rewrite/rewriteGraphTable.c +++ b/src/backend/rewrite/rewriteGraphTable.c @@ -1039,6 +1039,40 @@ replace_property_refs_mutator(Node *node, struct replace_property_refs_context * return (Node *) newvar; } + else if (IsA(node, Aggref)) + { + Aggref *aggref; + + /* Copy the Aggref node and mutate its sub-structure */ + aggref = (Aggref *) expression_tree_mutator(node, + replace_property_refs_mutator, + context); + + /* + * An aggregate is allowed in a graph table expression, but only if + * it's an outer aggregate. Since it will be in a subquery after the + * rewrite, we have to increase the level by one. + */ + Assert(aggref->agglevelsup > 0); + aggref->agglevelsup++; + + return (Node *) aggref; + } + else if (IsA(node, GroupingFunc)) + { + GroupingFunc *grp; + + /* Copy the GroupingFunc node and mutate its sub-structure */ + grp = (GroupingFunc *) expression_tree_mutator(node, + replace_property_refs_mutator, + context); + + /* Like Aggref, this should be an outer-level reference */ + Assert(grp->agglevelsup > 0); + grp->agglevelsup++; + + return (Node *) grp; + } else if (IsA(node, GraphPropertyRef)) { GraphPropertyRef *gpr = (GraphPropertyRef *) node; diff --git a/src/include/parser/parse_node.h b/src/include/parser/parse_node.h index f7f4ba6c2a8..2b8ac813554 100644 --- a/src/include/parser/parse_node.h +++ b/src/include/parser/parse_node.h @@ -84,6 +84,8 @@ typedef enum ParseExprKind EXPR_KIND_GENERATED_COLUMN, /* generation expression for a column */ EXPR_KIND_CYCLE_MARK, /* cycle mark value */ EXPR_KIND_PROPGRAPH_PROPERTY, /* derived property expression */ + EXPR_KIND_GRAPH_TABLE_COLUMNS, /* GRAPH_TABLE COLUMNS list item */ + EXPR_KIND_GRAPH_TABLE_WHERE, /* WHERE in a GRAPH_TABLE pattern */ } ParseExprKind; diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out index cde3114ebf4..04b58a625ad 100644 --- a/src/test/regress/expected/graph_table.out +++ b/src/test/regress/expected/graph_table.out @@ -471,13 +471,54 @@ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT NULL)-[ ERROR: "*" not allowed here LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT... ^ --- aggregate, window, and set-returning functions are not supported in COLUMNS +-- aggregate, grouping, window, and set-returning functions are not allowed +-- in the COLUMNS list or the graph pattern WHERE, except for outer-level aggs SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) AS num)); -ERROR: aggregate functions in GRAPH_TABLE COLUMNS are not supported +ERROR: aggregate functions are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) A... + ^ +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c.customer_id) AS g)); +ERROR: grouping operations are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c... + ^ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number() OVER () AS rn)); -ERROR: window functions in GRAPH_TABLE COLUMNS are not supported +ERROR: window functions are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number... + ^ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_series(1, 2) AS gs)); -ERROR: set-returning functions in GRAPH_TABLE COLUMNS are not supported +ERROR: set-returning functions are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_s... + ^ +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id) > 0) COLUMNS (c.name AS nm)); +ERROR: aggregate functions are not allowed in GRAPH_TABLE WHERE +LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.cu... + ^ +SELECT EXISTS(SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.customer_id) AS num)) t) FROM customers o; + exists +-------- + t +(1 row) + +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers) WHERE count(o.customer_id) > 0 COLUMNS (c.name AS nm)) t) FROM customers o; + exists +-------- + t +(1 row) + +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; + exists +-------- + t +(1 row) + +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE GROUPING(o.customer_id) = 1) COLUMNS (c.name AS nm)) t) FROM customers o GROUP BY customer_id; + exists +-------- + f + f + f +(3 rows) + -- consecutive element patterns with same kind SELECT * FROM GRAPH_TABLE (g1 MATCH ()() COLUMNS (1 as one)); ERROR: adjacent vertex patterns are not supported diff --git a/src/test/regress/sql/graph_table.sql b/src/test/regress/sql/graph_table.sql index 7a4189833d8..8a1173aba92 100644 --- a/src/test/regress/sql/graph_table.sql +++ b/src/test/regress/sql/graph_table.sql @@ -306,10 +306,17 @@ SELECT * FROM GRAPH_TABLE (g1 MATCH (src IS el1 | vl1)-[conn]->(dest) COLUMNS (c SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.address = 'US')-[IS customer_orders]->(o IS orders) COLUMNS (c.*)); -- star anywhere else is not allowed as a property reference SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT NULL)-[IS customer_orders]->(o IS orders) COLUMNS (c.name)); --- aggregate, window, and set-returning functions are not supported in COLUMNS +-- aggregate, grouping, window, and set-returning functions are not allowed +-- in the COLUMNS list or the graph pattern WHERE, except for outer-level aggs SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) AS num)); +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c.customer_id) AS g)); SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number() OVER () AS rn)); SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_series(1, 2) AS gs)); +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id) > 0) COLUMNS (c.name AS nm)); +SELECT EXISTS(SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.customer_id) AS num)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers) WHERE count(o.customer_id) > 0 COLUMNS (c.name AS nm)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE GROUPING(o.customer_id) = 1) COLUMNS (c.name AS nm)) t) FROM customers o GROUP BY customer_id; -- consecutive element patterns with same kind SELECT * FROM GRAPH_TABLE (g1 MATCH ()() COLUMNS (1 as one)); SELECT * FROM GRAPH_TABLE (g1 MATCH -> COLUMNS (1 AS one)); -- 2.52.0
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE
@ 2026-08-16 00:23  Sami Imseih <samimseih@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 14+ messages in thread

From: Sami Imseih @ 2026-08-16 00:23 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-hackers; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>; Ewan Young <kdbase.hack@gmail.com>

Thanks for v4, Tom!

> This was ugly enough that it motivated me to go look at exactly why
> an outer aggregate doesn't work here, in hopes of removing the
> inconsistent restriction.  I found it: replace_property_refs_mutator,
> which increments Vars' varlevelsup to account for the fact that
> they're being pushed into a subquery,

You are right. This is not something I considered. No reason
why an outer aggregate can't be used inside the GRAPH_TABLE.
The restriction only applies to the same-level aggregate,
which there's no machinery for.

I tested v4 with a mixed same-level and outer-level aggregate, and
this combination is not rejected as it should be.

Using the same EXISTS test from v4, none of these produce the clean
parse-time rejection we expect. The COLUMNS case is worse, it returns
a row instead of erroring. Under EXISTS the aggregate's value is never
required, so the incorrect same-level aggregate is not evaluated and
the bad query is accepted.

```
postgres=# SELECT EXISTS(SELECT num FROM GRAPH_TABLE (myshop MATCH (c
IS customers) COLUMNS (count(c.customer_id + o.customer_id) AS num))
t) FROM customers o;
 exists
--------
 t
(1 row)

postgres=# SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c
IS customers WHERE count(c.customer_id + o.customer_id) > 0) COLUMNS
(c.name AS nm)) t) FROM customers o;
ERROR:  Upper-level Var found where not expected
postgres=# SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c
IS customers WHERE GROUPING(c.customer_id, o.customer_id) = 1) COLUMNS
(c.name AS nm)) t) FROM customers o GROUP BY customer_id;
ERROR:  arguments to GROUPING must be grouping expressions of the
associated query level
```

So, based on your explanation earlier, this led me to find that the
leveling is still wrong. Specifically, min_varlevel > 0, since the
level-zero GraphPropertyRef is not accounted for in
check_agg_arguments_walker. In this case min_varlevel should be 0.

The fix is to teach check_agg_arguments_walker that a GraphPropertyRef
carries a level, the GRAPH_TABLE's own level. This is safe because a
GraphPropertyRef can only ever refer to its own GRAPH_TABLE's level
and can never appear inside a sub-select, so treating it as a level-zero
reference is always correct. Such an aggregate then resolves to the
GRAPH_TABLE's own level and the existing same-level rejection fires.
With that in place all of the cases above are rejected cleanly at
parse time.

```
postgres=# SELECT EXISTS(SELECT num FROM GRAPH_TABLE (myshop MATCH (c
IS customers) COLUMNS (count(c.customer_id + o.customer_id) AS num))
t) FROM customers o;
ERROR:  aggregate functions are not allowed in GRAPH_TABLE COLUMNS
postgres=# SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c
IS customers WHERE count(c.customer_id + o.customer_id) > 0) COLUMNS
(c.name AS nm)) t) FROM customers o;
ERROR:  aggregate functions are not allowed in GRAPH_TABLE WHERE
postgres=# SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c
IS customers WHERE GROUPING(c.customer_id, o.customer_id) = 1) COLUMNS
(c.name AS nm)) t) FROM customers o GROUP BY customer_id;
ERROR:  grouping operations are not allowed in GRAPH_TABLE WHERE
```

v5 attached fixes the leveling problem, folded into your v4, with a
mixed-level test cases added.

What do you think?


--
Sami Imseih
Amazon Web Services (AWS)

Attachments:

v5-0001-Rework-GRAPH_TABLE-aggregate-window-SRF-rejection.patch (application/octet-stream, 18.7K) download From 4c8e32fccdbd4f3f2000150f6ed2c109cf8b43e8 Mon Sep 17 00:00:00 2001 From: Tom Lane <tgl@sss.pgh.pa.us> Date: Sat, 15 Aug 2026 15:08:13 -0400 Subject: [PATCH v5 1/1] Rework GRAPH_TABLE aggregate/window/SRF rejection using ParseExprKind. Commit f58567105 disallowed aggregates, window functions, and set-returning functions in a GRAPH_TABLE COLUMNS list by inspecting the parse state after transforming the list. This way is not capable of reporting a parse location for the misplaced construct; worse, the patch missed checking for aggregates etc. in GRAPH_TABLE WHERE. Instead give the COLUMNS list and the graph pattern WHERE clause their own ParseExprKind values and enforce the restriction within the parser's transformation functions check_agglevels_and_constraints(), transformWindowFuncCall(), and check_srf_call_placement(). (This reverts the code changes of f58567105, though we keep the test cases and add some more.) This is more consistent with how the parser implements other misplaced-construct checks, and it allows delivery of better error messages. The parsing check rejects only aggregates having level zero. This is intentional: an outer-level aggregate is effectively a constant within the subquery containing GRAPH_TABLE, so there's no reason not to allow it. The case did not work before, but that seems to be only because replace_property_refs_mutator neglected its duty to mutate agglevelsup in the same way as varlevelsup. Enforcing the restriction correctly also required teaching check_agg_arguments_walker to treat GraphPropertyRef as a node that carries a level. Without that, a same-level aggregate that also references an outer query level would be considered an outer-level aggregate and escape the check. Note that this changes the SQLSTATE for a rejected aggregate from ERRCODE_FEATURE_NOT_SUPPORTED to ERRCODE_GROUPING_ERROR, consistent with how misplaced aggregates are rejected in other cases. Author: Sami Imseih <samimseih@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/CAA5RZ0tvdYODLQvYwVzAxUPe5=E3vSe8Zy7TvrQq+syvGKpHSQ@mail.gmail.com Backpatch-through: 19 --- src/backend/parser/parse_agg.c | 22 ++++++++ src/backend/parser/parse_clause.c | 32 +----------- src/backend/parser/parse_expr.c | 8 +++ src/backend/parser/parse_func.c | 4 ++ src/backend/parser/parse_graphtable.c | 6 +-- src/backend/rewrite/rewriteGraphTable.c | 34 +++++++++++++ src/include/parser/parse_node.h | 2 + src/test/regress/expected/graph_table.out | 61 +++++++++++++++++++++-- src/test/regress/sql/graph_table.sql | 12 ++++- 9 files changed, 142 insertions(+), 39 deletions(-) diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c index 754a20507d0..01f9b527ce3 100644 --- a/src/backend/parser/parse_agg.c +++ b/src/backend/parser/parse_agg.c @@ -600,6 +600,12 @@ check_agglevels_and_constraints(ParseState *pstate, Node *expr) break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + errkind = true; + + break; + /* * There is intentionally no default: case here, so that the * compiler will warn if we add a new ParseExprKind without @@ -769,6 +775,18 @@ check_agg_arguments_walker(Node *node, { if (node == NULL) return false; + if (IsA(node, GraphPropertyRef)) + { + /* + * A GraphPropertyRef refers to its own GRAPH_TABLE's level, so it + * contributes min_varlevel 0. It can't occur in a sub-select (the + * child parse state doesn't copy the GRAPH_TABLE namespace), hence + * the Assert. + */ + Assert(context->sublevels_up == 0); + context->min_varlevel = 0; + return false; + } if (IsA(node, Var)) { int varlevelsup = ((Var *) node)->varlevelsup; @@ -1045,6 +1063,10 @@ transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, case EXPR_KIND_FOR_PORTION: err = _("window functions are not allowed in FOR PORTION OF expressions"); break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + errkind = true; + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c index 68b525ffdcc..b1aa13eeb1d 100644 --- a/src/backend/parser/parse_clause.c +++ b/src/backend/parser/parse_clause.c @@ -946,9 +946,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) ListCell *lc; int resno = 0; bool saved_hasSublinks; - bool saved_hasAggs; - bool saved_hasWindowFuncs; - bool saved_hasTargetSRFs; rel = parserOpenPropGraph(pstate, rgt->graph_name, AccessShareLock); @@ -970,13 +967,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) saved_hasSublinks = pstate->p_hasSubLinks; pstate->p_hasSubLinks = false; - saved_hasAggs = pstate->p_hasAggs; - pstate->p_hasAggs = false; - saved_hasWindowFuncs = pstate->p_hasWindowFuncs; - pstate->p_hasWindowFuncs = false; - saved_hasTargetSRFs = pstate->p_hasTargetSRFs; - pstate->p_hasTargetSRFs = false; - gp = transformGraphPattern(pstate, rgt->graph_pattern); /* @@ -991,7 +981,7 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) TargetEntry *te; char *colname; - colexpr = transformExpr(pstate, rt->val, EXPR_KIND_SELECT_TARGET); + colexpr = transformExpr(pstate, rt->val, EXPR_KIND_GRAPH_TABLE_COLUMNS); if (rt->name) colname = rt->name; @@ -1041,26 +1031,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) errmsg("subqueries within GRAPH_TABLE reference are not supported"))); pstate->p_hasSubLinks = saved_hasSublinks; - /* - * GRAPH_TABLE cannot yet evaluate aggregate, window, or set-returning - * functions in its COLUMNS list, so prohibit them for now. - */ - if (pstate->p_hasAggs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("aggregate functions in GRAPH_TABLE COLUMNS are not supported")); - if (pstate->p_hasWindowFuncs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("window functions in GRAPH_TABLE COLUMNS are not supported")); - if (pstate->p_hasTargetSRFs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("set-returning functions in GRAPH_TABLE COLUMNS are not supported")); - pstate->p_hasAggs = saved_hasAggs; - pstate->p_hasWindowFuncs = saved_hasWindowFuncs; - pstate->p_hasTargetSRFs = saved_hasTargetSRFs; - return addRangeTableEntryForGraphTable(pstate, graphid, castNode(GraphPattern, gp), columns, colnames, rgt->alias, false, true); } diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 30c889f505f..f1c6aa236cf 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -579,6 +579,8 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref) case EXPR_KIND_GENERATED_COLUMN: case EXPR_KIND_CYCLE_MARK: case EXPR_KIND_PROPGRAPH_PROPERTY: + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: /* okay */ break; @@ -1843,6 +1845,8 @@ transformSubLink(ParseState *pstate, SubLink *sublink) case EXPR_KIND_VALUES: case EXPR_KIND_VALUES_SINGLE: case EXPR_KIND_CYCLE_MARK: + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: /* okay */ break; case EXPR_KIND_CHECK_CONSTRAINT: @@ -3255,6 +3259,10 @@ ParseExprKindName(ParseExprKind exprKind) return "property definition expression"; case EXPR_KIND_FOR_PORTION: return "FOR PORTION OF"; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + return "GRAPH_TABLE COLUMNS"; + case EXPR_KIND_GRAPH_TABLE_WHERE: + return "GRAPH_TABLE WHERE"; /* * 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 c87804f5d41..0c72b95e4a5 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -2848,6 +2848,10 @@ check_srf_call_placement(ParseState *pstate, Node *last_srf, int location) case EXPR_KIND_FOR_PORTION: err = _("set-returning functions are not allowed in FOR PORTION OF expressions"); break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + errkind = true; + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_graphtable.c b/src/backend/parser/parse_graphtable.c index 73fbfb541f7..e323376f0ea 100644 --- a/src/backend/parser/parse_graphtable.c +++ b/src/backend/parser/parse_graphtable.c @@ -92,7 +92,7 @@ transformGraphTablePropertyRef(ParseState *pstate, ColumnRef *cref) if (IsA(field1, A_Star) || IsA(field2, A_Star)) { - if (pstate->p_expr_kind == EXPR_KIND_SELECT_TARGET) + if (pstate->p_expr_kind == EXPR_KIND_GRAPH_TABLE_COLUMNS) ereport(ERROR, errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("\"*\" is not supported here"), @@ -251,7 +251,7 @@ transformGraphElementPattern(ParseState *pstate, GraphElementPattern *gep) gep->labelexpr = transformLabelExpr(gpstate, gep->labelexpr); - gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_WHERE); + gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_GRAPH_TABLE_WHERE); /* * Assign collations here for the reason mentioned in the prologue of @@ -387,7 +387,7 @@ transformGraphPattern(ParseState *pstate, GraphPattern *graph_pattern) transformPathPatternList(pstate, graph_pattern->path_pattern_list)); graph_pattern->path_pattern_list = path_pattern_list; - graph_pattern->whereClause = transformExpr(pstate, graph_pattern->whereClause, EXPR_KIND_WHERE); + graph_pattern->whereClause = transformExpr(pstate, graph_pattern->whereClause, EXPR_KIND_GRAPH_TABLE_WHERE); assign_expr_collations(pstate, graph_pattern->whereClause); return (Node *) graph_pattern; diff --git a/src/backend/rewrite/rewriteGraphTable.c b/src/backend/rewrite/rewriteGraphTable.c index 0eaf28b3de5..9b5495ffaa2 100644 --- a/src/backend/rewrite/rewriteGraphTable.c +++ b/src/backend/rewrite/rewriteGraphTable.c @@ -1039,6 +1039,40 @@ replace_property_refs_mutator(Node *node, struct replace_property_refs_context * return (Node *) newvar; } + else if (IsA(node, Aggref)) + { + Aggref *aggref; + + /* Copy the Aggref node and mutate its sub-structure */ + aggref = (Aggref *) expression_tree_mutator(node, + replace_property_refs_mutator, + context); + + /* + * An aggregate is allowed in a graph table expression, but only if + * it's an outer aggregate. Since it will be in a subquery after the + * rewrite, we have to increase the level by one. + */ + Assert(aggref->agglevelsup > 0); + aggref->agglevelsup++; + + return (Node *) aggref; + } + else if (IsA(node, GroupingFunc)) + { + GroupingFunc *grp; + + /* Copy the GroupingFunc node and mutate its sub-structure */ + grp = (GroupingFunc *) expression_tree_mutator(node, + replace_property_refs_mutator, + context); + + /* Like Aggref, this should be an outer-level reference */ + Assert(grp->agglevelsup > 0); + grp->agglevelsup++; + + return (Node *) grp; + } else if (IsA(node, GraphPropertyRef)) { GraphPropertyRef *gpr = (GraphPropertyRef *) node; diff --git a/src/include/parser/parse_node.h b/src/include/parser/parse_node.h index f7f4ba6c2a8..2b8ac813554 100644 --- a/src/include/parser/parse_node.h +++ b/src/include/parser/parse_node.h @@ -84,6 +84,8 @@ typedef enum ParseExprKind EXPR_KIND_GENERATED_COLUMN, /* generation expression for a column */ EXPR_KIND_CYCLE_MARK, /* cycle mark value */ EXPR_KIND_PROPGRAPH_PROPERTY, /* derived property expression */ + EXPR_KIND_GRAPH_TABLE_COLUMNS, /* GRAPH_TABLE COLUMNS list item */ + EXPR_KIND_GRAPH_TABLE_WHERE, /* WHERE in a GRAPH_TABLE pattern */ } ParseExprKind; diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out index cde3114ebf4..c6ea4878b43 100644 --- a/src/test/regress/expected/graph_table.out +++ b/src/test/regress/expected/graph_table.out @@ -471,13 +471,66 @@ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT NULL)-[ ERROR: "*" not allowed here LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT... ^ --- aggregate, window, and set-returning functions are not supported in COLUMNS +-- aggregate, grouping, window, and set-returning functions are not allowed +-- in the COLUMNS list or the graph pattern WHERE, except for outer-level aggs SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) AS num)); -ERROR: aggregate functions in GRAPH_TABLE COLUMNS are not supported +ERROR: aggregate functions are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) A... + ^ +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c.customer_id) AS g)); +ERROR: grouping operations are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c... + ^ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number() OVER () AS rn)); -ERROR: window functions in GRAPH_TABLE COLUMNS are not supported +ERROR: window functions are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number... + ^ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_series(1, 2) AS gs)); -ERROR: set-returning functions in GRAPH_TABLE COLUMNS are not supported +ERROR: set-returning functions are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_s... + ^ +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id) > 0) COLUMNS (c.name AS nm)); +ERROR: aggregate functions are not allowed in GRAPH_TABLE WHERE +LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.cu... + ^ +SELECT EXISTS(SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.customer_id) AS num)) t) FROM customers o; + exists +-------- + t +(1 row) + +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers) WHERE count(o.customer_id) > 0 COLUMNS (c.name AS nm)) t) FROM customers o; + exists +-------- + t +(1 row) + +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; + exists +-------- + t +(1 row) + +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE GROUPING(o.customer_id) = 1) COLUMNS (c.name AS nm)) t) FROM customers o GROUP BY customer_id; + exists +-------- + f + f + f +(3 rows) + +SELECT EXISTS(SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(c.customer_id + o.customer_id) AS num)) t) FROM customers o; +ERROR: aggregate functions are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(c.cu... + ^ +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id + o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; +ERROR: aggregate functions are not allowed in GRAPH_TABLE WHERE +LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.cu... + ^ +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE GROUPING(c.customer_id, o.customer_id) = 1) COLUMNS (c.name AS nm)) t) FROM customers o GROUP BY customer_id; +ERROR: grouping operations are not allowed in GRAPH_TABLE WHERE +LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE GROUPING(c... + ^ -- consecutive element patterns with same kind SELECT * FROM GRAPH_TABLE (g1 MATCH ()() COLUMNS (1 as one)); ERROR: adjacent vertex patterns are not supported diff --git a/src/test/regress/sql/graph_table.sql b/src/test/regress/sql/graph_table.sql index 7a4189833d8..90a807f6e68 100644 --- a/src/test/regress/sql/graph_table.sql +++ b/src/test/regress/sql/graph_table.sql @@ -306,10 +306,20 @@ SELECT * FROM GRAPH_TABLE (g1 MATCH (src IS el1 | vl1)-[conn]->(dest) COLUMNS (c SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.address = 'US')-[IS customer_orders]->(o IS orders) COLUMNS (c.*)); -- star anywhere else is not allowed as a property reference SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT NULL)-[IS customer_orders]->(o IS orders) COLUMNS (c.name)); --- aggregate, window, and set-returning functions are not supported in COLUMNS +-- aggregate, grouping, window, and set-returning functions are not allowed +-- in the COLUMNS list or the graph pattern WHERE, except for outer-level aggs SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) AS num)); +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c.customer_id) AS g)); SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number() OVER () AS rn)); SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_series(1, 2) AS gs)); +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id) > 0) COLUMNS (c.name AS nm)); +SELECT EXISTS(SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.customer_id) AS num)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers) WHERE count(o.customer_id) > 0 COLUMNS (c.name AS nm)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE GROUPING(o.customer_id) = 1) COLUMNS (c.name AS nm)) t) FROM customers o GROUP BY customer_id; +SELECT EXISTS(SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(c.customer_id + o.customer_id) AS num)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id + o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE GROUPING(c.customer_id, o.customer_id) = 1) COLUMNS (c.name AS nm)) t) FROM customers o GROUP BY customer_id; -- consecutive element patterns with same kind SELECT * FROM GRAPH_TABLE (g1 MATCH ()() COLUMNS (1 as one)); SELECT * FROM GRAPH_TABLE (g1 MATCH -> COLUMNS (1 AS one)); -- 2.47.3
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE
@ 2026-08-16 01:18  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Sami Imseih <samimseih@gmail.com>
  0 siblings, 1 reply; 14+ messages in thread

From: Tom Lane @ 2026-08-16 01:18 UTC (permalink / raw)
  To: Sami Imseih <samimseih@gmail.com>; +Cc: pgsql-hackers; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>; Ewan Young <kdbase.hack@gmail.com>

Sami Imseih <samimseih@gmail.com> writes:
> postgres=# SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c
> IS customers WHERE count(c.customer_id + o.customer_id) > 0) COLUMNS
> (c.name AS nm)) t) FROM customers o;
> ERROR:  Upper-level Var found where not expected

Ouch.

> So, based on your explanation earlier, this led me to find that the
> leveling is still wrong. Specifically, min_varlevel > 0, since the
> level-zero GraphPropertyRef is not accounted for in
> check_agg_arguments_walker. In this case min_varlevel should be 0.

Right, I came to the same conclusion.

> The fix is to teach check_agg_arguments_walker that a GraphPropertyRef
> carries a level, the GRAPH_TABLE's own level. This is safe because a
> GraphPropertyRef can only ever refer to its own GRAPH_TABLE's level
> and can never appear inside a sub-select, so treating it as a level-zero
> reference is always correct.

I find that argument pretty shaky.  If I can write
	MATCH WHERE c.customer_id > 0
why can't I write
	MATCH WHERE (SELECT c.customer_id > 0)
?  I see that that in fact doesn't work, but that seems like a bug
in itself.

More generally, what this suggests to me is that we have a ton of
other bugs-of-omission in places that process Vars and don't know that
a GraphPropertyRef acts like a Var.  So what I'm thinking right now is
that this is a fundamental design error, and that we should nuke
GraphPropertyRef altogether in favor of using a Var that references
the appropriate column of the RTE_GRAPH_TABLE relation.  We have
learned the hard way in the past that you don't want to invent things
that act like Vars but aren't Vars; if you must, you are going to be
doing a heck of a lot of work to patch up everyplace that will need to
know about them.  The hazard here is limited somewhat by the fact that
the planner and executor will never see GraphPropertyRefs, but I'm
afraid that still leaves plenty of scope for bugs (in the rewriter, in
particular).

			regards, tom lane






^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE
@ 2026-08-17 23:29  Sami Imseih <samimseih@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 14+ messages in thread

From: Sami Imseih @ 2026-08-17 23:29 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-hackers; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>; Ewan Young <kdbase.hack@gmail.com>

> I find that argument pretty shaky.

You are right. I was wrong to assume a GraphPropertyRef can only ever refer
to its own GRAPH_TABLE's level. Written inside a sub-select it does not.

> If I can write
>     MATCH WHERE c.customer_id > 0
> why can't I write
>     MATCH WHERE (SELECT c.customer_id > 0)
> ?  I see that that in fact doesn't work, but that seems like a bug
> in itself.

Disallowing subqueries in GRAPH_TABLE is the current intent of the
feature. The code
in upstream today only checks p_hasSubLinks at the end of
transformRangeGraphTable, after the WHERE and COLUMNS list is transformed, so
it only catches subqueries that transform cleanly, i.e. "(SELECT 1)" gets the
intended error, but "(SELECT c.customer_id > 0)" fails with "missing FROM-clause
entry"

The attached v6 does two things. First, it rejects a SubLink under the
GRAPH_TABLE
expr kinds in transformSubLink, the moment it is seen and before the
sub-select body
is analyzed. So MATCH WHERE (SELECT c.customer_id > 0) now reports

```
ERROR:  subqueries within GRAPH_TABLE reference are not supported
```

instead of

```
ERROR:  missing FROM-clause entry for table "c"
```

Also, because GRAPH_TABLE now rejects subqueries, a property reference
can
never sit inside one, so it is always at its own GRAPH_TABLE's level. That lets
check_agg_arguments_walker treat a GraphPropertyRef as a Var with
varlevelsup 0,
which it otherwise cannot see since a GraphPropertyRef is not a Var.
The walker still
adjusts for the level it is found at, the same as for a Var.

This corrects the aggregate case I showed earlier. The patch includes
regression tests
for it.

> More generally, what this suggests to me is that we have a ton of
> other bugs-of-omission in places that process Vars and don't know that
> a GraphPropertyRef acts like a Var.

Agreed, and v6 is an example of this. It teaches check_agg_arguments_walker
that a GraphPropertyRef behaves like a Var.

Besides other potential bugs, It also limits the feature in the future.
Keeping subqueries out of GRAPH_TABLE is part of what makes some
of the bugs discovered fixable with the current GraphPropertyRef, but
I suspect it will be a real limitation if we try to relax these restrictions.

> So what I'm thinking right now is
> that this is a fundamental design error, and that we should nuke
> GraphPropertyRef altogether in favor of using a Var that references
> the appropriate column of the RTE_GRAPH_TABLE relation.

I spent time today on this and I think the direction is right. In what
I have running
locally, a property reference is emitted as a plain Var over the
RTE_GRAPH_TABLE relation,
so the Var handles leveling and the rest for free. GraphPropertyRef
does not go away
entirely, though. It moves onto a side list on the RTE that the
rewriter uses to resolve
each property Var back to its graph property.

The design has some open questions, but I will share if this is the direction
to go.

--
Sami Imseih
Amazon Web Services (AWS)

Attachments:

v6-0001-Rework-GRAPH_TABLE-aggregate-window-SRF-rejection.patch (application/octet-stream, 22.4K) download From b3aa5fa70772ff2c442563176a349ba93a482d4d Mon Sep 17 00:00:00 2001 From: Tom Lane <tgl@sss.pgh.pa.us> Date: Sat, 15 Aug 2026 15:08:13 -0400 Subject: [PATCH v6 1/1] Rework GRAPH_TABLE aggregate/window/SRF rejection using ParseExprKind. Commit f58567105 disallowed aggregates, window functions, and set-returning functions in a GRAPH_TABLE COLUMNS list by inspecting the parse state after transforming the list. This way is not capable of reporting a parse location for the misplaced construct; worse, the patch missed checking for aggregates etc. in GRAPH_TABLE WHERE. Instead give the COLUMNS list and the graph pattern WHERE clause their own ParseExprKind values and enforce the restriction within the parser's transformation functions check_agglevels_and_constraints(), transformWindowFuncCall(), and check_srf_call_placement(). (This reverts the code changes of f58567105, though we keep the test cases and add some more.) This is more consistent with how the parser implements other misplaced-construct checks, and it allows delivery of better error messages. Subqueries are likewise disallowed within a GRAPH_TABLE COLUMNS list or graph pattern WHERE clause; transformSubLink now rejects a SubLink under these ParseExprKinds. The parsing check rejects only aggregates having level zero. This is intentional: an outer-level aggregate is effectively a constant within the subquery containing GRAPH_TABLE, so there's no reason not to allow it. The case did not work before, but that seems to be only because replace_property_refs_mutator neglected its duty to mutate agglevelsup in the same way as varlevelsup. Rejecting only level-zero aggregates also requires check_agg_arguments_walker to account for GraphPropertyRef nodes. A GraphPropertyRef is not a Var, so the walker would not otherwise see it, but since subqueries within GRAPH_TABLE are disallowed a property reference is always at its own GRAPH_TABLE's level and is treated as a Var with varlevelsup 0. Without this, an aggregate mixing a property with an outer-level Var would take its level from the outer Var alone and be mistaken for an outer-level aggregate. Note that this changes the SQLSTATE for a rejected aggregate from ERRCODE_FEATURE_NOT_SUPPORTED to ERRCODE_GROUPING_ERROR, consistent with how misplaced aggregates are rejected in other cases. Author: Sami Imseih <samimseih@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/CAA5RZ0tvdYODLQvYwVzAxUPe5=E3vSe8Zy7TvrQq+syvGKpHSQ@mail.gmail.com Backpatch-through: 19 --- src/backend/parser/parse_agg.c | 22 ++++++- src/backend/parser/parse_clause.c | 53 ++-------------- src/backend/parser/parse_expr.c | 10 ++++ src/backend/parser/parse_func.c | 4 ++ src/backend/parser/parse_graphtable.c | 6 +- src/backend/rewrite/rewriteGraphTable.c | 34 +++++++++++ src/include/parser/parse_node.h | 2 + src/test/regress/expected/graph_table.out | 73 +++++++++++++++++++++-- src/test/regress/sql/graph_table.sql | 15 ++++- 9 files changed, 160 insertions(+), 59 deletions(-) diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c index 754a20507d0..2e396c2188b 100644 --- a/src/backend/parser/parse_agg.c +++ b/src/backend/parser/parse_agg.c @@ -600,6 +600,12 @@ check_agglevels_and_constraints(ParseState *pstate, Node *expr) break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + errkind = true; + + break; + /* * There is intentionally no default: case here, so that the * compiler will warn if we add a new ParseExprKind without @@ -769,9 +775,17 @@ check_agg_arguments_walker(Node *node, { if (node == NULL) return false; - if (IsA(node, Var)) + if (IsA(node, Var) || IsA(node, GraphPropertyRef)) { - int varlevelsup = ((Var *) node)->varlevelsup; + /* + * A GraphPropertyRef always refers to a property of its own + * GRAPH_TABLE. Because subqueries within GRAPH_TABLE are not + * allowed, it can never reference an outer query level, so it behaves + * like a Var with varlevelsup 0. Counting it as level 0 keeps an + * aggregate that also references an outer Var from being classified + * at that outer level instead of at the GRAPH_TABLE's own level. + */ + int varlevelsup = IsA(node, Var) ? ((Var *) node)->varlevelsup : 0; /* convert levelsup to frame of reference of original query */ varlevelsup -= context->sublevels_up; @@ -1045,6 +1059,10 @@ transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, case EXPR_KIND_FOR_PORTION: err = _("window functions are not allowed in FOR PORTION OF expressions"); break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + errkind = true; + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c index 67de2733d8a..f14fe9b2447 100644 --- a/src/backend/parser/parse_clause.c +++ b/src/backend/parser/parse_clause.c @@ -945,10 +945,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) List *colnames = NIL; ListCell *lc; int resno = 0; - bool saved_hasSublinks; - bool saved_hasAggs; - bool saved_hasWindowFuncs; - bool saved_hasTargetSRFs; rel = parserOpenPropGraph(pstate, rgt->graph_name, AccessShareLock); @@ -957,9 +953,9 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) gpstate->graphid = graphid; /* - * The syntax does not allow nested GRAPH_TABLE and this function - * prohibits subquery within GRAPH_TABLE. There should be only one - * GRAPH_TABLE being transformed at a time. + * The syntax does not allow nested GRAPH_TABLE, and subqueries within a + * GRAPH_TABLE are prohibited (see transformSubLink), so there should be + * only one GRAPH_TABLE being transformed at a time. */ Assert(!pstate->p_graph_table_pstate); pstate->p_graph_table_pstate = gpstate; @@ -967,16 +963,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) Assert(!pstate->p_lateral_active); pstate->p_lateral_active = true; - saved_hasSublinks = pstate->p_hasSubLinks; - pstate->p_hasSubLinks = false; - - saved_hasAggs = pstate->p_hasAggs; - pstate->p_hasAggs = false; - saved_hasWindowFuncs = pstate->p_hasWindowFuncs; - pstate->p_hasWindowFuncs = false; - saved_hasTargetSRFs = pstate->p_hasTargetSRFs; - pstate->p_hasTargetSRFs = false; - gp = transformGraphPattern(pstate, rgt->graph_pattern); /* @@ -991,7 +977,7 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) TargetEntry *te; char *colname; - colexpr = transformExpr(pstate, rt->val, EXPR_KIND_SELECT_TARGET); + colexpr = transformExpr(pstate, rt->val, EXPR_KIND_GRAPH_TABLE_COLUMNS); if (rt->name) colname = rt->name; @@ -1030,37 +1016,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) pstate->p_graph_table_pstate = NULL; pstate->p_lateral_active = false; - /* - * If we support subqueries within GRAPH_TABLE, those need to be - * propagated to the queries resulting from rewriting graph table RTE. We - * don't do that right now, hence prohibit it for now. - */ - if (pstate->p_hasSubLinks) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("subqueries within GRAPH_TABLE reference are not supported"))); - pstate->p_hasSubLinks = saved_hasSublinks; - - /* - * GRAPH_TABLE cannot yet evaluate aggregate, window, or set-returning - * functions in its COLUMNS list, so prohibit them for now. - */ - if (pstate->p_hasAggs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("aggregate functions in GRAPH_TABLE COLUMNS are not supported")); - if (pstate->p_hasWindowFuncs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("window functions in GRAPH_TABLE COLUMNS are not supported")); - if (pstate->p_hasTargetSRFs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("set-returning functions in GRAPH_TABLE COLUMNS are not supported")); - pstate->p_hasAggs = saved_hasAggs; - pstate->p_hasWindowFuncs = saved_hasWindowFuncs; - pstate->p_hasTargetSRFs = saved_hasTargetSRFs; - return addRangeTableEntryForGraphTable(pstate, graphid, castNode(GraphPattern, gp), columns, colnames, rgt->alias, false, true); } diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 30c889f505f..7fc7c731dcb 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -579,6 +579,8 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref) case EXPR_KIND_GENERATED_COLUMN: case EXPR_KIND_CYCLE_MARK: case EXPR_KIND_PROPGRAPH_PROPERTY: + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: /* okay */ break; @@ -1845,6 +1847,10 @@ transformSubLink(ParseState *pstate, SubLink *sublink) case EXPR_KIND_CYCLE_MARK: /* okay */ break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + err = _("subqueries within GRAPH_TABLE reference are not supported"); + break; case EXPR_KIND_CHECK_CONSTRAINT: case EXPR_KIND_DOMAIN_CHECK: err = _("cannot use subquery in check constraint"); @@ -3255,6 +3261,10 @@ ParseExprKindName(ParseExprKind exprKind) return "property definition expression"; case EXPR_KIND_FOR_PORTION: return "FOR PORTION OF"; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + return "GRAPH_TABLE COLUMNS"; + case EXPR_KIND_GRAPH_TABLE_WHERE: + return "GRAPH_TABLE WHERE"; /* * 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 c87804f5d41..0c72b95e4a5 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -2848,6 +2848,10 @@ check_srf_call_placement(ParseState *pstate, Node *last_srf, int location) case EXPR_KIND_FOR_PORTION: err = _("set-returning functions are not allowed in FOR PORTION OF expressions"); break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + errkind = true; + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_graphtable.c b/src/backend/parser/parse_graphtable.c index 73fbfb541f7..e323376f0ea 100644 --- a/src/backend/parser/parse_graphtable.c +++ b/src/backend/parser/parse_graphtable.c @@ -92,7 +92,7 @@ transformGraphTablePropertyRef(ParseState *pstate, ColumnRef *cref) if (IsA(field1, A_Star) || IsA(field2, A_Star)) { - if (pstate->p_expr_kind == EXPR_KIND_SELECT_TARGET) + if (pstate->p_expr_kind == EXPR_KIND_GRAPH_TABLE_COLUMNS) ereport(ERROR, errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("\"*\" is not supported here"), @@ -251,7 +251,7 @@ transformGraphElementPattern(ParseState *pstate, GraphElementPattern *gep) gep->labelexpr = transformLabelExpr(gpstate, gep->labelexpr); - gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_WHERE); + gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_GRAPH_TABLE_WHERE); /* * Assign collations here for the reason mentioned in the prologue of @@ -387,7 +387,7 @@ transformGraphPattern(ParseState *pstate, GraphPattern *graph_pattern) transformPathPatternList(pstate, graph_pattern->path_pattern_list)); graph_pattern->path_pattern_list = path_pattern_list; - graph_pattern->whereClause = transformExpr(pstate, graph_pattern->whereClause, EXPR_KIND_WHERE); + graph_pattern->whereClause = transformExpr(pstate, graph_pattern->whereClause, EXPR_KIND_GRAPH_TABLE_WHERE); assign_expr_collations(pstate, graph_pattern->whereClause); return (Node *) graph_pattern; diff --git a/src/backend/rewrite/rewriteGraphTable.c b/src/backend/rewrite/rewriteGraphTable.c index 0eaf28b3de5..9b5495ffaa2 100644 --- a/src/backend/rewrite/rewriteGraphTable.c +++ b/src/backend/rewrite/rewriteGraphTable.c @@ -1039,6 +1039,40 @@ replace_property_refs_mutator(Node *node, struct replace_property_refs_context * return (Node *) newvar; } + else if (IsA(node, Aggref)) + { + Aggref *aggref; + + /* Copy the Aggref node and mutate its sub-structure */ + aggref = (Aggref *) expression_tree_mutator(node, + replace_property_refs_mutator, + context); + + /* + * An aggregate is allowed in a graph table expression, but only if + * it's an outer aggregate. Since it will be in a subquery after the + * rewrite, we have to increase the level by one. + */ + Assert(aggref->agglevelsup > 0); + aggref->agglevelsup++; + + return (Node *) aggref; + } + else if (IsA(node, GroupingFunc)) + { + GroupingFunc *grp; + + /* Copy the GroupingFunc node and mutate its sub-structure */ + grp = (GroupingFunc *) expression_tree_mutator(node, + replace_property_refs_mutator, + context); + + /* Like Aggref, this should be an outer-level reference */ + Assert(grp->agglevelsup > 0); + grp->agglevelsup++; + + return (Node *) grp; + } else if (IsA(node, GraphPropertyRef)) { GraphPropertyRef *gpr = (GraphPropertyRef *) node; diff --git a/src/include/parser/parse_node.h b/src/include/parser/parse_node.h index f7f4ba6c2a8..2b8ac813554 100644 --- a/src/include/parser/parse_node.h +++ b/src/include/parser/parse_node.h @@ -84,6 +84,8 @@ typedef enum ParseExprKind EXPR_KIND_GENERATED_COLUMN, /* generation expression for a column */ EXPR_KIND_CYCLE_MARK, /* cycle mark value */ EXPR_KIND_PROPGRAPH_PROPERTY, /* derived property expression */ + EXPR_KIND_GRAPH_TABLE_COLUMNS, /* GRAPH_TABLE COLUMNS list item */ + EXPR_KIND_GRAPH_TABLE_WHERE, /* WHERE in a GRAPH_TABLE pattern */ } ParseExprKind; diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out index cde3114ebf4..33909f0feb1 100644 --- a/src/test/regress/expected/graph_table.out +++ b/src/test/regress/expected/graph_table.out @@ -471,13 +471,66 @@ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT NULL)-[ ERROR: "*" not allowed here LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT... ^ --- aggregate, window, and set-returning functions are not supported in COLUMNS +-- aggregate, grouping, window, and set-returning functions are not allowed +-- in the COLUMNS list or the graph pattern WHERE, except for outer-level aggs SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) AS num)); -ERROR: aggregate functions in GRAPH_TABLE COLUMNS are not supported +ERROR: aggregate functions are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) A... + ^ +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c.customer_id) AS g)); +ERROR: grouping operations are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c... + ^ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number() OVER () AS rn)); -ERROR: window functions in GRAPH_TABLE COLUMNS are not supported +ERROR: window functions are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number... + ^ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_series(1, 2) AS gs)); -ERROR: set-returning functions in GRAPH_TABLE COLUMNS are not supported +ERROR: set-returning functions are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_s... + ^ +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id) > 0) COLUMNS (c.name AS nm)); +ERROR: aggregate functions are not allowed in GRAPH_TABLE WHERE +LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.cu... + ^ +SELECT EXISTS(SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.customer_id) AS num)) t) FROM customers o; + exists +-------- + t +(1 row) + +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers) WHERE count(o.customer_id) > 0 COLUMNS (c.name AS nm)) t) FROM customers o; + exists +-------- + t +(1 row) + +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; + exists +-------- + t +(1 row) + +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id + o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; +ERROR: aggregate functions are not allowed in GRAPH_TABLE WHERE +LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.cu... + ^ +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE GROUPING(o.customer_id) = 1) COLUMNS (c.name AS nm)) t) FROM customers o GROUP BY customer_id; + exists +-------- + f + f + f +(3 rows) + +-- a property in a GRAPH_TABLE subquery below the aggregate is not local to it, +-- so the aggregate is allowed +SELECT count(o.customer_id + (SELECT count(n) FROM GRAPH_TABLE (g1 MATCH (src) COLUMNS (src.vprop1 AS n)) x)) FROM customers o; + count +------- + 3 +(1 row) + -- consecutive element patterns with same kind SELECT * FROM GRAPH_TABLE (g1 MATCH ()() COLUMNS (1 as one)); ERROR: adjacent vertex patterns are not supported @@ -1100,8 +1153,20 @@ SELECT * FROM customers co WHERE co.customer_id = (SELECT customer_id FROM GRAPH -- query within graph table 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)); ERROR: subqueries within GRAPH_TABLE reference are not supported +LINE 1: ..._TABLE (g1 MATCH (src)->(dest) WHERE src.vprop1 > (SELECT ma... + ^ 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 +LINE 1: ...MATCH (src)->(dest) WHERE out_degree(src.vname) > (SELECT ma... + ^ +SELECT sname FROM GRAPH_TABLE (g1 MATCH (src WHERE (SELECT src.vprop1 > 0)) COLUMNS (src.vname AS sname)); +ERROR: subqueries within GRAPH_TABLE reference are not supported +LINE 1: ...ELECT sname FROM GRAPH_TABLE (g1 MATCH (src WHERE (SELECT sr... + ^ +SELECT x FROM GRAPH_TABLE (g1 MATCH (src) COLUMNS ((SELECT 1) AS x)); +ERROR: subqueries within GRAPH_TABLE reference are not supported +LINE 1: ...ELECT x FROM GRAPH_TABLE (g1 MATCH (src) COLUMNS ((SELECT 1)... + ^ -- GRAPH_TABLE subquery in HAVING clause (tests expression mutator) SELECT src.vname, count(*) FROM v1 AS src GROUP BY src.vname diff --git a/src/test/regress/sql/graph_table.sql b/src/test/regress/sql/graph_table.sql index 7a4189833d8..49ae571ddad 100644 --- a/src/test/regress/sql/graph_table.sql +++ b/src/test/regress/sql/graph_table.sql @@ -306,10 +306,21 @@ SELECT * FROM GRAPH_TABLE (g1 MATCH (src IS el1 | vl1)-[conn]->(dest) COLUMNS (c SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.address = 'US')-[IS customer_orders]->(o IS orders) COLUMNS (c.*)); -- star anywhere else is not allowed as a property reference SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT NULL)-[IS customer_orders]->(o IS orders) COLUMNS (c.name)); --- aggregate, window, and set-returning functions are not supported in COLUMNS +-- aggregate, grouping, window, and set-returning functions are not allowed +-- in the COLUMNS list or the graph pattern WHERE, except for outer-level aggs SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) AS num)); +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c.customer_id) AS g)); SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number() OVER () AS rn)); SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_series(1, 2) AS gs)); +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id) > 0) COLUMNS (c.name AS nm)); +SELECT EXISTS(SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.customer_id) AS num)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers) WHERE count(o.customer_id) > 0 COLUMNS (c.name AS nm)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id + o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE GROUPING(o.customer_id) = 1) COLUMNS (c.name AS nm)) t) FROM customers o GROUP BY customer_id; +-- a property in a GRAPH_TABLE subquery below the aggregate is not local to it, +-- so the aggregate is allowed +SELECT count(o.customer_id + (SELECT count(n) FROM GRAPH_TABLE (g1 MATCH (src) COLUMNS (src.vprop1 AS n)) x)) FROM customers o; -- consecutive element patterns with same kind SELECT * FROM GRAPH_TABLE (g1 MATCH ()() COLUMNS (1 as one)); SELECT * FROM GRAPH_TABLE (g1 MATCH -> COLUMNS (1 AS one)); @@ -617,6 +628,8 @@ SELECT * FROM customers co WHERE co.customer_id = (SELECT customer_id FROM GRAPH -- query within graph table 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)); +SELECT sname FROM GRAPH_TABLE (g1 MATCH (src WHERE (SELECT src.vprop1 > 0)) COLUMNS (src.vname AS sname)); +SELECT x FROM GRAPH_TABLE (g1 MATCH (src) COLUMNS ((SELECT 1) AS x)); -- GRAPH_TABLE subquery in HAVING clause (tests expression mutator) SELECT src.vname, count(*) FROM v1 AS src -- 2.47.3
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE
@ 2026-08-25 15:57  Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
  parent: Sami Imseih <samimseih@gmail.com>
  0 siblings, 2 replies; 14+ messages in thread

From: Ashutosh Bapat @ 2026-08-25 15:57 UTC (permalink / raw)
  To: Sami Imseih <samimseih@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers; Ewan Young <kdbase.hack@gmail.com>

Hi Sami,

Thanks for working on this. I agree that the changes in
transformRangeGraphTable() to reject aggregates, window functions, and
set-returning functions in GRAPH_TABLE were not sufficient. This patch
fixes it properly.

The patch has two parts
1. Rework the parser to reject aggregates, window functions, and
set-returning functions in GRAPH_TABLE COLUMNS list and graph pattern
WHERE clause using ParseExprKind. It correctly reverts the changes in
transformRangeGraphTable() which tried to do the same but did not
cover all the cases. It adds new ParseExprKind values for GRAPH_TABLE
COLUMNS and GRAPH_TABLE WHERE. I see that they are added to all the
necessary places. We need two separate ParseExprKind values to report
appropriate "all properties reference" error when it appears in
GRAPH_TABLE COLUMNS list versus in the WHERE clause. Otherwise the two
values are used in the same way to reject aggregates, window
functions, and set-returning functions.

2. Treat GraphPropertyRef as a Var and handle all Var-like nodes in
replace_property_refs(). I will cover this below.

On Tue, Aug 18, 2026 at 4:59 AM Sami Imseih <samimseih@gmail.com> wrote:
>
> Besides other potential bugs, It also limits the feature in the future.
> Keeping subqueries out of GRAPH_TABLE is part of what makes some
> of the bugs discovered fixable with the current GraphPropertyRef, but
> I suspect it will be a real limitation if we try to relax these restrictions.
>

This is a temporary restriction. Sooner or later we will support
subqueries nested under GraphPropertyRef.

> > So what I'm thinking right now is
> > that this is a fundamental design error, and that we should nuke
> > GraphPropertyRef altogether in favor of using a Var that references
> > the appropriate column of the RTE_GRAPH_TABLE relation.
>
> I spent time today on this and I think the direction is right. In what
> I have running
> locally, a property reference is emitted as a plain Var over the
> RTE_GRAPH_TABLE relation,
> so the Var handles leveling and the rest for free. GraphPropertyRef
> does not go away
> entirely, though. It moves onto a side list on the RTE that the
> rewriter uses to resolve
> each property Var back to its graph property.
>

I share the concern raised by Tom about bugs due to missing
GraphPropertyRef handling. However, I don't think we can use Var
instead of GraphPropertyRef. It represents a reference to a property
of all the graph elements that are bound to an element pattern in a
GRAPH_TABLE. The element pattern is not represented by a range table
and Var node does not have a field to represent an element pattern
variable. Further the same GraphPropertyRef may be rewritten as
different expressions depending on the element table it gets
associated with when generating a subquery for a given path. Achieving
this with Var will be quite complex, if not impossible. The question
is whether we can find all the places where we need to treat
GraphPropertyRef as a Var. Given that the GraphPropertyRefs are
generated during transformation and vanish after rewrite, the places
where we need to handle them are limited, as noted by Tom already.
Many of those are already covered by the patch. Going through all the
places, rather painfully, where we use levelsup for Var-like nodes,
looking at var.c, I see that most of that code is applicable to
planning, optimization and execution. The additional places which I
think we need to handle are:
a. locate_var_of_level_walker() to handle GraphPropertyRef so that the
correct error location can be reported. We have separate
locate_aggref_of_level_walker(). So first I thought that it would be
appropriate to add a separate
locate_graphpropertyref_of_level_walker() function. But we want to
report an error from a place where the distinction between Var and
GraphPropertyRef is lost. So a separate function doesn't make sense.
b. contain_vars_of_level() should not see GraphPropertyRef since the
latter lives within GRAPH_TABLE only. But the function is used during
query transformation, so it will be good to add a GraphPropertyRef
case and Assert()/throw error if it is seen. We should add a comment
there to expect GraphPropertyRef there once we start supporting nested
subqueries in GRAPH_TABLE.
c. IncrementVarSublevelsUp() will need to handle GraphPropertyRef once
we support nested subqueries in GRAPH_TABLE. For now we may want to
add a GraphPropertyRef case and Assert()/throw error if it is seen. We
should add a comment there to expect GraphPropertyRef there once we
start supporting nested subqueries in GRAPH_TABLE. Please note, I
don't expect ChangeVarNodes to handle GraphPropertyRef since there is
no rtindex in GraphPropertyRef.

I didn't find any other place where we need to handle
GraphPropertyRef. But there are just many places where we use Vars and
varlevelsup. So there is a non-zero possibility that I may have missed
a few. I wish we could have differentiated between Var in the planner,
optimizer code and ColumnRef in the parser, transformer and rewriter
code. That would have made it easier to find the places where we need
to handle GraphPropertyRef. But the ship has sailed long back.

Here are some comments on v6 patches.

@@ -769,9 +775,17 @@ check_agg_arguments_walker(Node *node,
 {
  if (node == NULL)
  return false;
- if (IsA(node, Var))
+ if (IsA(node, Var) || IsA(node, GraphPropertyRef))
  {

I would rather add a separate GraphPropertyRef case instead of
combining it with Var. When we start supporting nested subqueries in
GRAPH_TABLE, we will need to add levelsup to GraphPropertyRef and
handle it appropriately. I am leaning towards doing that now and
setting it to 0. Then use here instead of hardcoded 0. Also pass it on
to the Vars in the expression that replaces the GraphPropertyRef by
calling ChangeVarNodes() with sublevelsup = GraphPropertyRef::levelsup
instead of 0. Once subquery support is added, we will set
GraphPropertyRef::levelsup to the appropriate value and the
ChangeVarNodes() will propagate it to the Vars in the replacement
expression. If we go about it this way, we should add an Assert in
replace_property_refs() to make sure that GraphPropertyRef::levelsup
== 0. But I am also fine with implicitly treating GraphPropertyRef as
level 0 with a comment that we should pass on the levelsup to the Vars
in the replacement expression once we support nested subqueries in
GRAPH_TABLE.

@@ -1845,6 +1847,10 @@ transformSubLink(ParseState *pstate, SubLink *sublink)
  case EXPR_KIND_CYCLE_MARK:
  /* okay */
  break;
+ case EXPR_KIND_GRAPH_TABLE_COLUMNS:
+ case EXPR_KIND_GRAPH_TABLE_WHERE:
+ err = _("subqueries within GRAPH_TABLE reference are not supported");

I would use "cannot use subquery in GRAPH_TABLE reference" to be
consistent with the other error messages in this function.

@@ -1039,6 +1039,40 @@ replace_property_refs_mutator(Node *node,
struct replace_property_refs_context *

  return (Node *) newvar;
  }
+ else if (IsA(node, Aggref))
+ {
+ Aggref   *aggref;
+
+ /* Copy the Aggref node and mutate its sub-structure */
+ aggref = (Aggref *) expression_tree_mutator(node,
+ replace_property_refs_mutator,
+ context);
+
+ /*
+ * An aggregate is allowed in a graph table expression, but only if
+ * it's an outer aggregate.  Since it will be in a subquery after the
+ * rewrite, we have to increase the level by one.
+ */
+ Assert(aggref->agglevelsup > 0);
+ aggref->agglevelsup++;
+
+ return (Node *) aggref;
+ }
+ else if (IsA(node, GroupingFunc))
+ {
+ GroupingFunc *grp;
+
+ /* Copy the GroupingFunc node and mutate its sub-structure */
+ grp = (GroupingFunc *) expression_tree_mutator(node,
+   replace_property_refs_mutator,
+   context);
+
+ /* Like Aggref, this should be an outer-level reference */
+ Assert(grp->agglevelsup > 0);
+ grp->agglevelsup++;
+
+ return (Node *) grp;
+ }

I think we could make this function lean by letting
IncrementVarSublevelsUp() do the levelsup adjustment before the
GraphPropertyRef is replaced. It will require traversing the
expression tree twice, but it will eliminate the possibility of
replace_property_refs_mutator() missing a node type that has levelsup.

@@ -84,6 +84,8 @@ typedef enum ParseExprKind
  EXPR_KIND_GENERATED_COLUMN, /* generation expression for a column */
  EXPR_KIND_CYCLE_MARK, /* cycle mark value */
  EXPR_KIND_PROPGRAPH_PROPERTY, /* derived property expression */
+ EXPR_KIND_GRAPH_TABLE_COLUMNS, /* GRAPH_TABLE COLUMNS list item */
+ EXPR_KIND_GRAPH_TABLE_WHERE, /* WHERE in a GRAPH_TABLE pattern */

Nit. I would just write WHERE in a GRAPH_TABLE since GRAPH_TABLE is
not a pattern, but a table function.

-- 
Best Wishes,
Ashutosh Bapat






^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE
@ 2026-09-01 00:14  Sami Imseih <samimseih.pg@gmail.com>
  parent: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
  1 sibling, 1 reply; 14+ messages in thread

From: Sami Imseih @ 2026-09-01 00:14 UTC (permalink / raw)
  To: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>; +Cc: Sami Imseih <samimseih@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers; Ewan Young <kdbase.hack@gmail.com>

Hi Ashutosh,

Thanks for the review.

> > > So what I'm thinking right now is
> > > that this is a fundamental design error, and that we should nuke
> > > GraphPropertyRef altogether in favor of using a Var that references
> > > the appropriate column of the RTE_GRAPH_TABLE relation.
> >
> > I spent time today on this and I think the direction is right. In what
> > I have running
> > locally, a property reference is emitted as a plain Var over the
> > RTE_GRAPH_TABLE relation,
> > so the Var handles leveling and the rest for free. GraphPropertyRef
> > does not go away
> > entirely, though. It moves onto a side list on the RTE that the
> > rewriter uses to resolve
> > each property Var back to its graph property.
> >
>
> I share the concern raised by Tom about bugs due to missing
> GraphPropertyRef handling. However, I don't think we can use Var
> instead of GraphPropertyRef. It represents a reference to a property
> of all the graph elements that are bound to an element pattern in a
> GRAPH_TABLE. The element pattern is not represented by a range table
> and Var node does not have a field to represent an element pattern
> variable. Further the same GraphPropertyRef may be rewritten as
> different expressions depending on the element table it gets
> associated with when generating a subquery for a given path.

You are right. A Var does not seem correct here after looking at it a
bit more. GraphPropertyRef is not a column of an existing RTE. Also,
the same reference can resolve differently in different generated path
subqueries depending on which element table that path uses. So keeping a
separate placeholder seems more natural.

> this with Var will be quite complex, if not impossible. The question
> is whether we can find all the places where we need to treat
> GraphPropertyRef as a Var. Given that the GraphPropertyRefs are
> generated during transformation and vanish after rewrite, the places
> where we need to handle them are limited, as noted by Tom already.
> Many of those are already covered by the patch. Going through all the
> places, rather painfully, where we use levelsup for Var-like nodes,
> looking at var.c, I see that most of that code is applicable to
> planning, optimization and execution. The additional places which I
> think we need to handle are:
> a. locate_var_of_level_walker() to handle GraphPropertyRef so that the
> correct error location can be reported. We have separate
> locate_aggref_of_level_walker(). So first I thought that it would be
> appropriate to add a separate
> locate_graphpropertyref_of_level_walker() function. But we want to
> report an error from a place where the distinction between Var and
> GraphPropertyRef is lost. So a separate function doesn't make sense.
> b. contain_vars_of_level() should not see GraphPropertyRef since the
> latter lives within GRAPH_TABLE only. But the function is used during
> query transformation, so it will be good to add a GraphPropertyRef
> case and Assert()/throw error if it is seen. We should add a comment
> there to expect GraphPropertyRef there once we start supporting nested
> subqueries in GRAPH_TABLE.
> c. IncrementVarSublevelsUp() will need to handle GraphPropertyRef once
> we support nested subqueries in GRAPH_TABLE. For now we may want to
> add a GraphPropertyRef case and Assert()/throw error if it is seen. We
> should add a comment there to expect GraphPropertyRef there once we
> start supporting nested subqueries in GRAPH_TABLE. Please note, I
> don't expect ChangeVarNodes to handle GraphPropertyRef since there is
> no rtindex in GraphPropertyRef.

Right. The places where GraphPropertyRef needs Var-like treatment seem
fairly limited. In v7, I handled the walkers that need query level
information, such as locate_var_of_level_walker() and
check_agg_arguments_walker(). I also added a GraphPropertyRef case to
contain_vars_of_level(), since that can be reached during query
transformation before rewrite removes GraphPropertyRef.

IncrementVarSublevelsUp() is different. There I added a
GraphPropertyRef case that leaves it alone and asserts
gprlevelsup == 0, since we do not yet support subqueries inside
GRAPH_TABLE. Once we do, we will need to adjust the levels there as
well.

> Here are some comments on v6 patches.

> @@ -769,9 +775,17 @@ check_agg_arguments_walker(Node *node,
>  {
>   if (node == NULL)
>   return false;
> - if (IsA(node, Var))
> + if (IsA(node, Var) || IsA(node, GraphPropertyRef))
>   {

> I would rather add a separate GraphPropertyRef case instead of
> combining it with Var. When we start supporting nested subqueries in
> GRAPH_TABLE, we will need to add levelsup to GraphPropertyRef and
> handle it appropriately. I am leaning towards doing that now and
> setting it to 0. Then use here instead of hardcoded 0.

Done in v7. I added gprlevelsup to carry the level.

> Also pass it on
> to the Vars in the expression that replaces the GraphPropertyRef by
> calling ChangeVarNodes() with sublevelsup = GraphPropertyRef::levelsup
> instead of 0. Once subquery support is added, we will set
> GraphPropertyRef::levelsup to the appropriate value and the
> ChangeVarNodes() will propagate it to the Vars in the replacement
> expression. If we go about it this way, we should add an Assert in
> replace_property_refs() to make sure that GraphPropertyRef::levelsup
> == 0. But I am also fine with implicitly treating GraphPropertyRef as
> level 0 with a comment that we should pass on the levelsup to the Vars
> in the replacement expression once we support nested subqueries in
> GRAPH_TABLE.

... replace_property_refs() now passes gprlevelsup through to
ChangeVarNodes() instead of hardcoding 0. I also added
gprlevelsup == 0 asserts in both IncrementVarSublevelsUp_walker() and
replace_property_refs_mutator(), since subqueries inside GRAPH_TABLE
are not supported yet.

> @@ -1845,6 +1847,10 @@ transformSubLink(ParseState *pstate, SubLink
*sublink)
>   case EXPR_KIND_CYCLE_MARK:
>   /* okay */
>   break;
> + case EXPR_KIND_GRAPH_TABLE_COLUMNS:
> + case EXPR_KIND_GRAPH_TABLE_WHERE:
> + err = _("subqueries within GRAPH_TABLE reference are not supported");
>
>
> I would use "cannot use subquery in GRAPH_TABLE reference" to be
> consistent with the other error messages in this function.

Done in v7.

> I think we could make this function lean by letting
> IncrementVarSublevelsUp() do the levelsup adjustment before the
> GraphPropertyRef is replaced. It will require traversing the
> expression tree twice, but it will eliminate the possibility of
> replace_property_refs_mutator() missing a node type that has levelsup.

Done in v7. I removed the special Var-levelsup handling from
replace_property_refs_mutator() and let IncrementVarSublevelsUp()
adjust the copied expression in generate_query_for_graph_path() before
GraphPropertyRef is replaced.

> @@ -84,6 +84,8 @@ typedef enum ParseExprKind
>   EXPR_KIND_GENERATED_COLUMN, /* generation expression for a column */
>   EXPR_KIND_CYCLE_MARK, /* cycle mark value */
>   EXPR_KIND_PROPGRAPH_PROPERTY, /* derived property expression */
> + EXPR_KIND_GRAPH_TABLE_COLUMNS, /* GRAPH_TABLE COLUMNS list item */
> + EXPR_KIND_GRAPH_TABLE_WHERE, /* WHERE in a GRAPH_TABLE pattern */
>
>
> Nit. I would just write WHERE in a GRAPH_TABLE since GRAPH_TABLE is
> not a pattern, but a table function.

Done in v7.

Also, with these changes, I did see what it would take to support
subqueries and
I have something that I can show in a different thread. I think the
aub-query support
will be a v20 item, while v7- should be targeted for v19.

--
Sami Imseih
Amazon Web Services (AWS)

Attachments:

v7-0001-Rework-GRAPH_TABLE-aggregate-window-SRF-rejection.patch (application/octet-stream, 28.6K) download From 5476fb8e5ce11f8d15d3de2deb1a17248624c196 Mon Sep 17 00:00:00 2001 From: Sami Imseih <samimseih.pg@gmail.com> Date: Sat, 15 Aug 2026 15:08:13 -0400 Subject: [PATCH v7 1/1] Rework GRAPH_TABLE aggregate/window/SRF rejection using ParseExprKind. Commit f58567105 disallowed aggregates, window functions, and set-returning functions in a GRAPH_TABLE COLUMNS list by inspecting the parse state after transforming the list. This way is not capable of reporting a parse location for the misplaced construct; worse, the patch missed checking for aggregates etc. in GRAPH_TABLE WHERE. Instead give the COLUMNS list and the graph pattern WHERE clause their own ParseExprKind values and enforce the restriction within the parser's transformation functions check_agglevels_and_constraints(), transformWindowFuncCall(), and check_srf_call_placement(). (This reverts the code changes of f58567105, though we keep the test cases and add some more.) This is more consistent with how the parser implements other misplaced-construct checks, and it allows delivery of better error messages. Subqueries are likewise disallowed within a GRAPH_TABLE COLUMNS list or graph pattern WHERE clause; transformSubLink now rejects a SubLink under these ParseExprKinds. The parsing check rejects only aggregates having level zero. This is intentional: an outer-level aggregate is effectively a constant within the subquery containing GRAPH_TABLE, so there's no reason not to allow it. The case did not work before because GraphPropertyRef did not carry query-level information explicitly and the rewrite path did not consistently adjust already-outer references before nesting the expression under a generated subquery. Rejecting only level-zero aggregates therefore requires GraphPropertyRef to carry an explicit levelsup field and for check_agg_arguments_walker to account for GraphPropertyRef nodes directly. Since GraphPropertyRef is not a Var, teach the relevant query-level walkers to treat it as a Var-like node where needed, and prepare GRAPH_TABLE expressions for the added query level before replace_property_refs() resolves property refs. Note that this changes the SQLSTATE for a rejected aggregate from ERRCODE_FEATURE_NOT_SUPPORTED to ERRCODE_GROUPING_ERROR, consistent with how misplaced aggregates are rejected in other cases. Author: Sami Imseih <samimseih@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/CAA5RZ0tvdYODLQvYwVzAxUPe5=E3vSe8Zy7TvrQq+syvGKpHSQ@mail.gmail.com Backpatch-through: 19 --- src/backend/optimizer/util/var.c | 22 ++++++- src/backend/parser/parse_agg.c | 25 ++++++++ src/backend/parser/parse_clause.c | 47 +------------- src/backend/parser/parse_expr.c | 10 +++ src/backend/parser/parse_func.c | 4 ++ src/backend/parser/parse_graphtable.c | 10 +-- src/backend/rewrite/rewriteGraphTable.c | 52 ++++++++-------- src/backend/rewrite/rewriteManip.c | 13 ++++ src/include/nodes/primnodes.h | 15 +++++ src/include/parser/parse_node.h | 2 + src/test/regress/expected/graph_table.out | 75 +++++++++++++++++++++-- src/test/regress/sql/graph_table.sql | 14 ++++- 12 files changed, 202 insertions(+), 87 deletions(-) diff --git a/src/backend/optimizer/util/var.c b/src/backend/optimizer/util/var.c index 907a255c36f..929cb740874 100644 --- a/src/backend/optimizer/util/var.c +++ b/src/backend/optimizer/util/var.c @@ -6,7 +6,9 @@ * Note: for most purposes, PlaceHolderVar is considered a Var too, * even if its contained expression is variable-free. Also, CurrentOfExpr * is treated as a Var for purposes of determining whether an expression - * contains variables. + * contains variables. GraphPropertyRef carries a levelsup and is likewise + * treated as a Var when determining which query level a reference belongs to + * (see primnodes.h). * * * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group @@ -462,6 +464,12 @@ contain_vars_of_level_walker(Node *node, int *sublevels_up) return true; /* abort tree traversal and return true */ return false; } + if (IsA(node, GraphPropertyRef)) + { + if (((GraphPropertyRef *) node)->gprlevelsup == *sublevels_up) + return true; /* abort tree traversal and return true */ + return false; + } if (IsA(node, CurrentOfExpr)) { if (*sublevels_up == 0) @@ -585,6 +593,18 @@ locate_var_of_level_walker(Node *node, } return false; } + if (IsA(node, GraphPropertyRef)) + { + GraphPropertyRef *gpr = (GraphPropertyRef *) node; + + if (gpr->gprlevelsup == context->sublevels_up && + gpr->location >= 0) + { + context->var_location = gpr->location; + return true; /* abort tree traversal and return true */ + } + return false; + } if (IsA(node, CurrentOfExpr)) { /* since CurrentOfExpr doesn't carry location, nothing we can do */ diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c index 754a20507d0..ebc6dff2d0e 100644 --- a/src/backend/parser/parse_agg.c +++ b/src/backend/parser/parse_agg.c @@ -600,6 +600,12 @@ check_agglevels_and_constraints(ParseState *pstate, Node *expr) break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + errkind = true; + + break; + /* * There is intentionally no default: case here, so that the * compiler will warn if we add a new ParseExprKind without @@ -784,6 +790,21 @@ check_agg_arguments_walker(Node *node, } return false; } + if (IsA(node, GraphPropertyRef)) + { + int gprlevelsup = ((GraphPropertyRef *) node)->gprlevelsup; + + /* convert levelsup to frame of reference of original query */ + gprlevelsup -= context->sublevels_up; + /* ignore local vars of subqueries */ + if (gprlevelsup >= 0) + { + if (context->min_varlevel < 0 || + context->min_varlevel > gprlevelsup) + context->min_varlevel = gprlevelsup; + } + return false; + } if (IsA(node, Aggref)) { int agglevelsup = ((Aggref *) node)->agglevelsup; @@ -1045,6 +1066,10 @@ transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, case EXPR_KIND_FOR_PORTION: err = _("window functions are not allowed in FOR PORTION OF expressions"); break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + errkind = true; + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c index 67de2733d8a..34a4fa0672b 100644 --- a/src/backend/parser/parse_clause.c +++ b/src/backend/parser/parse_clause.c @@ -945,10 +945,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) List *colnames = NIL; ListCell *lc; int resno = 0; - bool saved_hasSublinks; - bool saved_hasAggs; - bool saved_hasWindowFuncs; - bool saved_hasTargetSRFs; rel = parserOpenPropGraph(pstate, rgt->graph_name, AccessShareLock); @@ -967,16 +963,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) Assert(!pstate->p_lateral_active); pstate->p_lateral_active = true; - saved_hasSublinks = pstate->p_hasSubLinks; - pstate->p_hasSubLinks = false; - - saved_hasAggs = pstate->p_hasAggs; - pstate->p_hasAggs = false; - saved_hasWindowFuncs = pstate->p_hasWindowFuncs; - pstate->p_hasWindowFuncs = false; - saved_hasTargetSRFs = pstate->p_hasTargetSRFs; - pstate->p_hasTargetSRFs = false; - gp = transformGraphPattern(pstate, rgt->graph_pattern); /* @@ -991,7 +977,7 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) TargetEntry *te; char *colname; - colexpr = transformExpr(pstate, rt->val, EXPR_KIND_SELECT_TARGET); + colexpr = transformExpr(pstate, rt->val, EXPR_KIND_GRAPH_TABLE_COLUMNS); if (rt->name) colname = rt->name; @@ -1030,37 +1016,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) pstate->p_graph_table_pstate = NULL; pstate->p_lateral_active = false; - /* - * If we support subqueries within GRAPH_TABLE, those need to be - * propagated to the queries resulting from rewriting graph table RTE. We - * don't do that right now, hence prohibit it for now. - */ - if (pstate->p_hasSubLinks) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("subqueries within GRAPH_TABLE reference are not supported"))); - pstate->p_hasSubLinks = saved_hasSublinks; - - /* - * GRAPH_TABLE cannot yet evaluate aggregate, window, or set-returning - * functions in its COLUMNS list, so prohibit them for now. - */ - if (pstate->p_hasAggs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("aggregate functions in GRAPH_TABLE COLUMNS are not supported")); - if (pstate->p_hasWindowFuncs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("window functions in GRAPH_TABLE COLUMNS are not supported")); - if (pstate->p_hasTargetSRFs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("set-returning functions in GRAPH_TABLE COLUMNS are not supported")); - pstate->p_hasAggs = saved_hasAggs; - pstate->p_hasWindowFuncs = saved_hasWindowFuncs; - pstate->p_hasTargetSRFs = saved_hasTargetSRFs; - return addRangeTableEntryForGraphTable(pstate, graphid, castNode(GraphPattern, gp), columns, colnames, rgt->alias, false, true); } diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 30c889f505f..d69a0a79f48 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -579,6 +579,8 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref) case EXPR_KIND_GENERATED_COLUMN: case EXPR_KIND_CYCLE_MARK: case EXPR_KIND_PROPGRAPH_PROPERTY: + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: /* okay */ break; @@ -1845,6 +1847,10 @@ transformSubLink(ParseState *pstate, SubLink *sublink) case EXPR_KIND_CYCLE_MARK: /* okay */ break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + err = _("cannot use subquery in GRAPH_TABLE reference"); + break; case EXPR_KIND_CHECK_CONSTRAINT: case EXPR_KIND_DOMAIN_CHECK: err = _("cannot use subquery in check constraint"); @@ -3255,6 +3261,10 @@ ParseExprKindName(ParseExprKind exprKind) return "property definition expression"; case EXPR_KIND_FOR_PORTION: return "FOR PORTION OF"; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + return "GRAPH_TABLE COLUMNS"; + case EXPR_KIND_GRAPH_TABLE_WHERE: + return "GRAPH_TABLE WHERE"; /* * 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 c87804f5d41..0c72b95e4a5 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -2848,6 +2848,10 @@ check_srf_call_placement(ParseState *pstate, Node *last_srf, int location) case EXPR_KIND_FOR_PORTION: err = _("set-returning functions are not allowed in FOR PORTION OF expressions"); break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + errkind = true; + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_graphtable.c b/src/backend/parser/parse_graphtable.c index 73fbfb541f7..c8e230379a5 100644 --- a/src/backend/parser/parse_graphtable.c +++ b/src/backend/parser/parse_graphtable.c @@ -92,7 +92,7 @@ transformGraphTablePropertyRef(ParseState *pstate, ColumnRef *cref) if (IsA(field1, A_Star) || IsA(field2, A_Star)) { - if (pstate->p_expr_kind == EXPR_KIND_SELECT_TARGET) + if (pstate->p_expr_kind == EXPR_KIND_GRAPH_TABLE_COLUMNS) ereport(ERROR, errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("\"*\" is not supported here"), @@ -106,7 +106,6 @@ transformGraphTablePropertyRef(ParseState *pstate, ColumnRef *cref) elvarname = strVal(field1); propname = strVal(field2); - if (list_member(gpstate->variables, field1)) { GraphPropertyRef *gpr; @@ -137,6 +136,8 @@ transformGraphTablePropertyRef(ParseState *pstate, ColumnRef *cref) gpr->location = cref->location; gpr->elvarname = elvarname; + + gpr->gprlevelsup = 0; gpr->propid = pgpform->oid; gpr->typeId = pgpform->pgptypid; gpr->typmod = pgpform->pgptypmod; @@ -250,8 +251,7 @@ transformGraphElementPattern(ParseState *pstate, GraphElementPattern *gep) gpstate->cur_gep = gep; gep->labelexpr = transformLabelExpr(gpstate, gep->labelexpr); - - gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_WHERE); + gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_GRAPH_TABLE_WHERE); /* * Assign collations here for the reason mentioned in the prologue of @@ -387,7 +387,7 @@ transformGraphPattern(ParseState *pstate, GraphPattern *graph_pattern) transformPathPatternList(pstate, graph_pattern->path_pattern_list)); graph_pattern->path_pattern_list = path_pattern_list; - graph_pattern->whereClause = transformExpr(pstate, graph_pattern->whereClause, EXPR_KIND_WHERE); + graph_pattern->whereClause = transformExpr(pstate, graph_pattern->whereClause, EXPR_KIND_GRAPH_TABLE_WHERE); assign_expr_collations(pstate, graph_pattern->whereClause); return (Node *) graph_pattern; diff --git a/src/backend/rewrite/rewriteGraphTable.c b/src/backend/rewrite/rewriteGraphTable.c index 0eaf28b3de5..ce32a97b4f2 100644 --- a/src/backend/rewrite/rewriteGraphTable.c +++ b/src/backend/rewrite/rewriteGraphTable.c @@ -34,7 +34,6 @@ #include "parser/parse_oper.h" #include "parser/parse_relation.h" #include "parser/parsetree.h" -#include "parser/parse_graphtable.h" #include "rewrite/rewriteGraphTable.h" #include "rewrite/rewriteHandler.h" #include "rewrite/rewriteManip.h" @@ -100,7 +99,8 @@ static Query *generate_query_for_empty_path_pattern(RangeTblEntry *rte); static Query *generate_union_from_pathqueries(List **pathqueries); static List *get_path_elements_for_path_factor(Oid propgraphid, struct path_factor *pf); static bool is_property_associated_with_label(Oid labeloid, Oid propoid); -static Node *get_element_property_expr(Oid elemoid, Oid propoid, int rtindex); +static Node *get_element_property_expr(Oid elemoid, Oid propoid, int rtindex, + Index gprlevelsup); /* * Convert GRAPH_TABLE clause into a subquery using relational @@ -530,9 +530,10 @@ generate_query_for_graph_path(RangeTblEntry *rte, List *graph_path) if (pf->whereClause) { - Node *tr; + Node *tr = copyObject(pf->whereClause); - tr = replace_property_refs(rte->relid, pf->whereClause, list_make1(pe)); + IncrementVarSublevelsUp(tr, 1, 0); + tr = replace_property_refs(rte->relid, tr, list_make1(pe)); qual_exprs = lappend(qual_exprs, tr); } @@ -540,9 +541,10 @@ generate_query_for_graph_path(RangeTblEntry *rte, List *graph_path) if (rte->graph_pattern->whereClause) { - Node *path_quals = replace_property_refs(rte->relid, - (Node *) rte->graph_pattern->whereClause, - graph_path); + Node *path_quals = copyObject(rte->graph_pattern->whereClause); + + IncrementVarSublevelsUp(path_quals, 1, 0); + path_quals = replace_property_refs(rte->relid, path_quals, graph_path); qual_exprs = lappend(qual_exprs, path_quals); } @@ -551,9 +553,11 @@ generate_query_for_graph_path(RangeTblEntry *rte, List *graph_path) qual_exprs ? (Node *) makeBoolExpr(AND_EXPR, qual_exprs, -1) : NULL); /* Construct query targetlist from COLUMNS specification of GRAPH_TABLE. */ + path_query->targetList = copyObject(rte->graph_table_columns); + IncrementVarSublevelsUp((Node *) path_query->targetList, 1, 0); path_query->targetList = castNode(List, replace_property_refs(rte->relid, - (Node *) rte->graph_table_columns, + (Node *) path_query->targetList, graph_path)); /* @@ -1025,21 +1029,7 @@ replace_property_refs_mutator(Node *node, struct replace_property_refs_context * { if (node == NULL) return NULL; - if (IsA(node, Var)) - { - Var *var = (Var *) node; - Var *newvar = copyObject(var); - - /* - * If it's already a Var, then it was a lateral reference. Since we - * are in a subquery after the rewrite, we have to increase the level - * by one. - */ - newvar->varlevelsup++; - - return (Node *) newvar; - } - else if (IsA(node, GraphPropertyRef)) + if (IsA(node, GraphPropertyRef)) { GraphPropertyRef *gpr = (GraphPropertyRef *) node; Node *n = NULL; @@ -1063,6 +1053,8 @@ replace_property_refs_mutator(Node *node, struct replace_property_refs_context * */ Assert(found_mapping); + Assert(gpr->gprlevelsup == 0); + mapping_factor = found_mapping->path_factor; /* @@ -1093,7 +1085,8 @@ replace_property_refs_mutator(Node *node, struct replace_property_refs_context * n = stringToNode(TextDatumGetCString(SysCacheGetAttrNotNull(PROPGRAPHLABELPROP, tup, Anum_pg_propgraph_label_property_plpexpr))); - ChangeVarNodes(n, 1, mapping_factor->factorpos + 1, 0); + ChangeVarNodes(n, 1, mapping_factor->factorpos + 1, + gpr->gprlevelsup); ReleaseSysCache(tup); } @@ -1132,8 +1125,10 @@ replace_property_refs_mutator(Node *node, struct replace_property_refs_context * * SQL/PGQ standard section 6.5 Property Reference, General * Rule 2.b. */ - n = get_element_property_expr(found_mapping->elemoid, gpr->propid, - mapping_factor->factorpos + 1); + n = get_element_property_expr(found_mapping->elemoid, + gpr->propid, + mapping_factor->factorpos + 1, + gpr->gprlevelsup); if (!n) n = (Node *) makeNullConst(gpr->typeId, gpr->typmod, gpr->collation); @@ -1304,7 +1299,8 @@ is_property_associated_with_label(Oid labeloid, Oid propoid) * NULL. */ static Node * -get_element_property_expr(Oid elemoid, Oid propoid, int rtindex) +get_element_property_expr(Oid elemoid, Oid propoid, int rtindex, + Index gprlevelsup) { Relation rel; SysScanDesc scan; @@ -1331,7 +1327,7 @@ get_element_property_expr(Oid elemoid, Oid propoid, int rtindex) continue; n = stringToNode(TextDatumGetCString(SysCacheGetAttrNotNull(PROPGRAPHLABELPROP, proptup, Anum_pg_propgraph_label_property_plpexpr))); - ChangeVarNodes(n, 1, rtindex, 0); + ChangeVarNodes(n, 1, rtindex, gprlevelsup); ReleaseSysCache(proptup); break; diff --git a/src/backend/rewrite/rewriteManip.c b/src/backend/rewrite/rewriteManip.c index 3653f00d383..f147d64c0c3 100644 --- a/src/backend/rewrite/rewriteManip.c +++ b/src/backend/rewrite/rewriteManip.c @@ -815,6 +815,19 @@ IncrementVarSublevelsUp_walker(Node *node, var->varlevelsup += context->delta_sublevels_up; return false; /* done here */ } + if (IsA(node, GraphPropertyRef)) + { + /* + * Unlike a Var, we must not adjust a GraphPropertyRef's level here. + * This walker runs over a GRAPH_TABLE COLUMNS/WHERE expression before + * replace_property_refs() resolves each GraphPropertyRef to a Var + * local to the generated path query, setting its final level via + * ChangeVarNodes(). As we don't yet allow correlated subqueries + * inside a GRAPH_TABLE reference, gprlevelsup is always 0; assert it. + */ + Assert(((GraphPropertyRef *) node)->gprlevelsup == 0); + return false; /* done here */ + } if (IsA(node, CurrentOfExpr)) { /* this should not happen */ diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index 5a636d1f179..aa7c7841b2b 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -2196,11 +2196,26 @@ typedef struct GraphLabelRef /* * GraphPropertyRef - property reference inside GRAPH_TABLE clause + * + * A GraphPropertyRef is generated during transformation and resolved away by + * the rewriter, so only a few places need to know about it. It carries a + * levelsup and is treated like a Var by contain_vars_of_level() and + * locate_var_of_level(); IncrementVarSublevelsUp() leaves it alone, since + * replace_property_refs() passes its gprlevelsup through when resolving it to + * a Var. + * We don't yet allow correlated subqueries inside a GRAPH_TABLE reference, so + * gprlevelsup is always 0 today. */ typedef struct GraphPropertyRef { Expr xpr; const char *elvarname; + + /* + * would be > 0 for an outer-query reference, but see above: always 0 + * today + */ + Index gprlevelsup; Oid propid; Oid typeId; int32 typmod; diff --git a/src/include/parser/parse_node.h b/src/include/parser/parse_node.h index f7f4ba6c2a8..34f061df0fa 100644 --- a/src/include/parser/parse_node.h +++ b/src/include/parser/parse_node.h @@ -84,6 +84,8 @@ typedef enum ParseExprKind EXPR_KIND_GENERATED_COLUMN, /* generation expression for a column */ EXPR_KIND_CYCLE_MARK, /* cycle mark value */ EXPR_KIND_PROPGRAPH_PROPERTY, /* derived property expression */ + EXPR_KIND_GRAPH_TABLE_COLUMNS, /* GRAPH_TABLE COLUMNS list item */ + EXPR_KIND_GRAPH_TABLE_WHERE, /* WHERE in a GRAPH_TABLE */ } ParseExprKind; diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out index 2e862a82ba0..eb2bd1b0220 100644 --- a/src/test/regress/expected/graph_table.out +++ b/src/test/regress/expected/graph_table.out @@ -471,13 +471,72 @@ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT NULL)-[ ERROR: "*" not allowed here LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT... ^ --- aggregate, window, and set-returning functions are not supported in COLUMNS +-- aggregate, grouping, window, and set-returning functions are not allowed +-- in the COLUMNS list or the graph pattern WHERE, except for outer-level aggs SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) AS num)); -ERROR: aggregate functions in GRAPH_TABLE COLUMNS are not supported +ERROR: aggregate functions are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) A... + ^ +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c.customer_id) AS g)); +ERROR: grouping operations are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c... + ^ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number() OVER () AS rn)); -ERROR: window functions in GRAPH_TABLE COLUMNS are not supported +ERROR: window functions are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number... + ^ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_series(1, 2) AS gs)); -ERROR: set-returning functions in GRAPH_TABLE COLUMNS are not supported +ERROR: set-returning functions are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_s... + ^ +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id) > 0) COLUMNS (c.name AS nm)); +ERROR: aggregate functions are not allowed in GRAPH_TABLE WHERE +LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.cu... + ^ +SELECT count(*) FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE 1 IN (c.customer_id, 1000)) COLUMNS (c.name AS nm)) t; + count +------- + 1 +(1 row) + +SELECT EXISTS(SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.customer_id) AS num)) t) FROM customers o; + exists +-------- + t +(1 row) + +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers) WHERE count(o.customer_id) > 0 COLUMNS (c.name AS nm)) t) FROM customers o; + exists +-------- + t +(1 row) + +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; + exists +-------- + t +(1 row) + +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id + o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; +ERROR: aggregate functions are not allowed in GRAPH_TABLE WHERE +LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.cu... + ^ +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE GROUPING(o.customer_id) = 1) COLUMNS (c.name AS nm)) t) FROM customers o GROUP BY customer_id; + exists +-------- + f + f + f +(3 rows) + +-- a property in a GRAPH_TABLE subquery below the aggregate is not local to it, +-- so the aggregate is allowed +SELECT count(o.customer_id + (SELECT count(n) FROM GRAPH_TABLE (g1 MATCH (src) COLUMNS (src.vprop1 AS n)) x)) FROM customers o; + count +------- + 3 +(1 row) + -- consecutive element patterns with same kind SELECT * FROM GRAPH_TABLE (g1 MATCH ()() COLUMNS (1 as one)); ERROR: adjacent vertex patterns are not supported @@ -1118,9 +1177,13 @@ SELECT * FROM customers co WHERE co.customer_id = (SELECT customer_id FROM GRAPH -- query within graph table 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)); -ERROR: subqueries within GRAPH_TABLE reference are not supported +ERROR: cannot use subquery in GRAPH_TABLE reference +LINE 1: ..._TABLE (g1 MATCH (src)->(dest) WHERE src.vprop1 > (SELECT ma... + ^ 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 +ERROR: cannot use subquery in GRAPH_TABLE reference +LINE 1: ...MATCH (src)->(dest) WHERE out_degree(src.vname) > (SELECT ma... + ^ -- GRAPH_TABLE subquery in HAVING clause (tests expression mutator) SELECT src.vname, count(*) FROM v1 AS src GROUP BY src.vname diff --git a/src/test/regress/sql/graph_table.sql b/src/test/regress/sql/graph_table.sql index 21e70015f6e..240288ce962 100644 --- a/src/test/regress/sql/graph_table.sql +++ b/src/test/regress/sql/graph_table.sql @@ -306,10 +306,22 @@ SELECT * FROM GRAPH_TABLE (g1 MATCH (src IS el1 | vl1)-[conn]->(dest) COLUMNS (c SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.address = 'US')-[IS customer_orders]->(o IS orders) COLUMNS (c.*)); -- star anywhere else is not allowed as a property reference SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT NULL)-[IS customer_orders]->(o IS orders) COLUMNS (c.name)); --- aggregate, window, and set-returning functions are not supported in COLUMNS +-- aggregate, grouping, window, and set-returning functions are not allowed +-- in the COLUMNS list or the graph pattern WHERE, except for outer-level aggs SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) AS num)); +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c.customer_id) AS g)); SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number() OVER () AS rn)); SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_series(1, 2) AS gs)); +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id) > 0) COLUMNS (c.name AS nm)); +SELECT count(*) FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE 1 IN (c.customer_id, 1000)) COLUMNS (c.name AS nm)) t; +SELECT EXISTS(SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.customer_id) AS num)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers) WHERE count(o.customer_id) > 0 COLUMNS (c.name AS nm)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id + o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE GROUPING(o.customer_id) = 1) COLUMNS (c.name AS nm)) t) FROM customers o GROUP BY customer_id; +-- a property in a GRAPH_TABLE subquery below the aggregate is not local to it, +-- so the aggregate is allowed +SELECT count(o.customer_id + (SELECT count(n) FROM GRAPH_TABLE (g1 MATCH (src) COLUMNS (src.vprop1 AS n)) x)) FROM customers o; -- consecutive element patterns with same kind SELECT * FROM GRAPH_TABLE (g1 MATCH ()() COLUMNS (1 as one)); SELECT * FROM GRAPH_TABLE (g1 MATCH -> COLUMNS (1 AS one)); -- 2.47.3
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE
@ 2026-09-01 01:08  Sami Imseih <samimseih.pg@gmail.com>
  parent: Sami Imseih <samimseih.pg@gmail.com>
  0 siblings, 0 replies; 14+ messages in thread

From: Sami Imseih @ 2026-09-01 01:08 UTC (permalink / raw)
  To: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers; Ewan Young <kdbase.hack@gmail.com>

Sorry for the spam.

I made one small change here. I don't think gprlevelsup is a good
name for the new leveling field. It should be called varlevelsup,
because in this patch it is really just carrying query levelsup in
the same sense as Var.varlevelsup.

Using gprlevelsup here suggests GRAPH_TABLE leveling, which
is a different thing. If we later support nested subqueries inside
GRAPH_TABLE, we will need a separate level to correlate an inner
GRAPH_TABLE with an outer one.

--
Sami Imseih
Amazon Web Services (AWS)

Attachments:

v8-0001-Rework-GRAPH_TABLE-aggregate-window-SRF-rejection.patch (application/octet-stream, 28.6K) download From f283f0ad53a56ff9f389451ee698aa19e4622170 Mon Sep 17 00:00:00 2001 From: Sami Imseih <samimseih.pg@gmail.com> Date: Sat, 15 Aug 2026 15:08:13 -0400 Subject: [PATCH v8 1/1] Rework GRAPH_TABLE aggregate/window/SRF rejection using ParseExprKind. Commit f58567105 disallowed aggregates, window functions, and set-returning functions in a GRAPH_TABLE COLUMNS list by inspecting the parse state after transforming the list. This way is not capable of reporting a parse location for the misplaced construct; worse, the patch missed checking for aggregates etc. in GRAPH_TABLE WHERE. Instead give the COLUMNS list and the graph pattern WHERE clause their own ParseExprKind values and enforce the restriction within the parser's transformation functions check_agglevels_and_constraints(), transformWindowFuncCall(), and check_srf_call_placement(). (This reverts the code changes of f58567105, though we keep the test cases and add some more.) This is more consistent with how the parser implements other misplaced-construct checks, and it allows delivery of better error messages. Subqueries are likewise disallowed within a GRAPH_TABLE COLUMNS list or graph pattern WHERE clause; transformSubLink now rejects a SubLink under these ParseExprKinds. The parsing check rejects only aggregates having level zero. This is intentional: an outer-level aggregate is effectively a constant within the subquery containing GRAPH_TABLE, so there's no reason not to allow it. The case did not work before because GraphPropertyRef did not carry query-level information explicitly and the rewrite path did not consistently adjust already-outer references before nesting the expression under a generated subquery. Rejecting only level-zero aggregates therefore requires GraphPropertyRef to carry an explicit varlevelsup field and for check_agg_arguments_walker to account for GraphPropertyRef nodes directly. Since GraphPropertyRef is not a Var, teach the relevant query-level walkers to treat it as a Var-like node where needed, and prepare GRAPH_TABLE expressions for the added query level before replace_property_refs() resolves property refs. Note that this changes the SQLSTATE for a rejected aggregate from ERRCODE_FEATURE_NOT_SUPPORTED to ERRCODE_GROUPING_ERROR, consistent with how misplaced aggregates are rejected in other cases. Author: Sami Imseih <samimseih.pg@gmail.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/CAA5RZ0tvdYODLQvYwVzAxUPe5=E3vSe8Zy7TvrQq+syvGKpHSQ@mail.gmail.com Backpatch-through: 19 --- src/backend/optimizer/util/var.c | 22 ++++++- src/backend/parser/parse_agg.c | 25 ++++++++ src/backend/parser/parse_clause.c | 47 +------------- src/backend/parser/parse_expr.c | 10 +++ src/backend/parser/parse_func.c | 4 ++ src/backend/parser/parse_graphtable.c | 10 +-- src/backend/rewrite/rewriteGraphTable.c | 52 ++++++++-------- src/backend/rewrite/rewriteManip.c | 13 ++++ src/include/nodes/primnodes.h | 12 ++++ src/include/parser/parse_node.h | 2 + src/test/regress/expected/graph_table.out | 75 +++++++++++++++++++++-- src/test/regress/sql/graph_table.sql | 14 ++++- 12 files changed, 199 insertions(+), 87 deletions(-) diff --git a/src/backend/optimizer/util/var.c b/src/backend/optimizer/util/var.c index 907a255c36f..cb81f226baa 100644 --- a/src/backend/optimizer/util/var.c +++ b/src/backend/optimizer/util/var.c @@ -6,7 +6,9 @@ * Note: for most purposes, PlaceHolderVar is considered a Var too, * even if its contained expression is variable-free. Also, CurrentOfExpr * is treated as a Var for purposes of determining whether an expression - * contains variables. + * contains variables. GraphPropertyRef carries a varlevelsup and is likewise + * treated as a Var when determining which query level a reference belongs to + * (see primnodes.h). * * * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group @@ -462,6 +464,12 @@ contain_vars_of_level_walker(Node *node, int *sublevels_up) return true; /* abort tree traversal and return true */ return false; } + if (IsA(node, GraphPropertyRef)) + { + if (((GraphPropertyRef *) node)->varlevelsup == *sublevels_up) + return true; /* abort tree traversal and return true */ + return false; + } if (IsA(node, CurrentOfExpr)) { if (*sublevels_up == 0) @@ -585,6 +593,18 @@ locate_var_of_level_walker(Node *node, } return false; } + if (IsA(node, GraphPropertyRef)) + { + GraphPropertyRef *gpr = (GraphPropertyRef *) node; + + if (gpr->varlevelsup == context->sublevels_up && + gpr->location >= 0) + { + context->var_location = gpr->location; + return true; /* abort tree traversal and return true */ + } + return false; + } if (IsA(node, CurrentOfExpr)) { /* since CurrentOfExpr doesn't carry location, nothing we can do */ diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c index 754a20507d0..d3281ca9a1b 100644 --- a/src/backend/parser/parse_agg.c +++ b/src/backend/parser/parse_agg.c @@ -600,6 +600,12 @@ check_agglevels_and_constraints(ParseState *pstate, Node *expr) break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + errkind = true; + + break; + /* * There is intentionally no default: case here, so that the * compiler will warn if we add a new ParseExprKind without @@ -784,6 +790,21 @@ check_agg_arguments_walker(Node *node, } return false; } + if (IsA(node, GraphPropertyRef)) + { + int varlevelsup = ((GraphPropertyRef *) node)->varlevelsup; + + /* convert levelsup to frame of reference of original query */ + varlevelsup -= context->sublevels_up; + /* ignore local vars of subqueries */ + if (varlevelsup >= 0) + { + if (context->min_varlevel < 0 || + context->min_varlevel > varlevelsup) + context->min_varlevel = varlevelsup; + } + return false; + } if (IsA(node, Aggref)) { int agglevelsup = ((Aggref *) node)->agglevelsup; @@ -1045,6 +1066,10 @@ transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, case EXPR_KIND_FOR_PORTION: err = _("window functions are not allowed in FOR PORTION OF expressions"); break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + errkind = true; + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c index 67de2733d8a..34a4fa0672b 100644 --- a/src/backend/parser/parse_clause.c +++ b/src/backend/parser/parse_clause.c @@ -945,10 +945,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) List *colnames = NIL; ListCell *lc; int resno = 0; - bool saved_hasSublinks; - bool saved_hasAggs; - bool saved_hasWindowFuncs; - bool saved_hasTargetSRFs; rel = parserOpenPropGraph(pstate, rgt->graph_name, AccessShareLock); @@ -967,16 +963,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) Assert(!pstate->p_lateral_active); pstate->p_lateral_active = true; - saved_hasSublinks = pstate->p_hasSubLinks; - pstate->p_hasSubLinks = false; - - saved_hasAggs = pstate->p_hasAggs; - pstate->p_hasAggs = false; - saved_hasWindowFuncs = pstate->p_hasWindowFuncs; - pstate->p_hasWindowFuncs = false; - saved_hasTargetSRFs = pstate->p_hasTargetSRFs; - pstate->p_hasTargetSRFs = false; - gp = transformGraphPattern(pstate, rgt->graph_pattern); /* @@ -991,7 +977,7 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) TargetEntry *te; char *colname; - colexpr = transformExpr(pstate, rt->val, EXPR_KIND_SELECT_TARGET); + colexpr = transformExpr(pstate, rt->val, EXPR_KIND_GRAPH_TABLE_COLUMNS); if (rt->name) colname = rt->name; @@ -1030,37 +1016,6 @@ transformRangeGraphTable(ParseState *pstate, RangeGraphTable *rgt) pstate->p_graph_table_pstate = NULL; pstate->p_lateral_active = false; - /* - * If we support subqueries within GRAPH_TABLE, those need to be - * propagated to the queries resulting from rewriting graph table RTE. We - * don't do that right now, hence prohibit it for now. - */ - if (pstate->p_hasSubLinks) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("subqueries within GRAPH_TABLE reference are not supported"))); - pstate->p_hasSubLinks = saved_hasSublinks; - - /* - * GRAPH_TABLE cannot yet evaluate aggregate, window, or set-returning - * functions in its COLUMNS list, so prohibit them for now. - */ - if (pstate->p_hasAggs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("aggregate functions in GRAPH_TABLE COLUMNS are not supported")); - if (pstate->p_hasWindowFuncs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("window functions in GRAPH_TABLE COLUMNS are not supported")); - if (pstate->p_hasTargetSRFs) - ereport(ERROR, - errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("set-returning functions in GRAPH_TABLE COLUMNS are not supported")); - pstate->p_hasAggs = saved_hasAggs; - pstate->p_hasWindowFuncs = saved_hasWindowFuncs; - pstate->p_hasTargetSRFs = saved_hasTargetSRFs; - return addRangeTableEntryForGraphTable(pstate, graphid, castNode(GraphPattern, gp), columns, colnames, rgt->alias, false, true); } diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 30c889f505f..d69a0a79f48 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -579,6 +579,8 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref) case EXPR_KIND_GENERATED_COLUMN: case EXPR_KIND_CYCLE_MARK: case EXPR_KIND_PROPGRAPH_PROPERTY: + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: /* okay */ break; @@ -1845,6 +1847,10 @@ transformSubLink(ParseState *pstate, SubLink *sublink) case EXPR_KIND_CYCLE_MARK: /* okay */ break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + err = _("cannot use subquery in GRAPH_TABLE reference"); + break; case EXPR_KIND_CHECK_CONSTRAINT: case EXPR_KIND_DOMAIN_CHECK: err = _("cannot use subquery in check constraint"); @@ -3255,6 +3261,10 @@ ParseExprKindName(ParseExprKind exprKind) return "property definition expression"; case EXPR_KIND_FOR_PORTION: return "FOR PORTION OF"; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + return "GRAPH_TABLE COLUMNS"; + case EXPR_KIND_GRAPH_TABLE_WHERE: + return "GRAPH_TABLE WHERE"; /* * 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 c87804f5d41..0c72b95e4a5 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -2848,6 +2848,10 @@ check_srf_call_placement(ParseState *pstate, Node *last_srf, int location) case EXPR_KIND_FOR_PORTION: err = _("set-returning functions are not allowed in FOR PORTION OF expressions"); break; + case EXPR_KIND_GRAPH_TABLE_COLUMNS: + case EXPR_KIND_GRAPH_TABLE_WHERE: + errkind = true; + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_graphtable.c b/src/backend/parser/parse_graphtable.c index 73fbfb541f7..3245878fa9e 100644 --- a/src/backend/parser/parse_graphtable.c +++ b/src/backend/parser/parse_graphtable.c @@ -92,7 +92,7 @@ transformGraphTablePropertyRef(ParseState *pstate, ColumnRef *cref) if (IsA(field1, A_Star) || IsA(field2, A_Star)) { - if (pstate->p_expr_kind == EXPR_KIND_SELECT_TARGET) + if (pstate->p_expr_kind == EXPR_KIND_GRAPH_TABLE_COLUMNS) ereport(ERROR, errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("\"*\" is not supported here"), @@ -106,7 +106,6 @@ transformGraphTablePropertyRef(ParseState *pstate, ColumnRef *cref) elvarname = strVal(field1); propname = strVal(field2); - if (list_member(gpstate->variables, field1)) { GraphPropertyRef *gpr; @@ -137,6 +136,8 @@ transformGraphTablePropertyRef(ParseState *pstate, ColumnRef *cref) gpr->location = cref->location; gpr->elvarname = elvarname; + + gpr->varlevelsup = 0; gpr->propid = pgpform->oid; gpr->typeId = pgpform->pgptypid; gpr->typmod = pgpform->pgptypmod; @@ -250,8 +251,7 @@ transformGraphElementPattern(ParseState *pstate, GraphElementPattern *gep) gpstate->cur_gep = gep; gep->labelexpr = transformLabelExpr(gpstate, gep->labelexpr); - - gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_WHERE); + gep->whereClause = transformExpr(pstate, gep->whereClause, EXPR_KIND_GRAPH_TABLE_WHERE); /* * Assign collations here for the reason mentioned in the prologue of @@ -387,7 +387,7 @@ transformGraphPattern(ParseState *pstate, GraphPattern *graph_pattern) transformPathPatternList(pstate, graph_pattern->path_pattern_list)); graph_pattern->path_pattern_list = path_pattern_list; - graph_pattern->whereClause = transformExpr(pstate, graph_pattern->whereClause, EXPR_KIND_WHERE); + graph_pattern->whereClause = transformExpr(pstate, graph_pattern->whereClause, EXPR_KIND_GRAPH_TABLE_WHERE); assign_expr_collations(pstate, graph_pattern->whereClause); return (Node *) graph_pattern; diff --git a/src/backend/rewrite/rewriteGraphTable.c b/src/backend/rewrite/rewriteGraphTable.c index 0eaf28b3de5..2b85ee6602b 100644 --- a/src/backend/rewrite/rewriteGraphTable.c +++ b/src/backend/rewrite/rewriteGraphTable.c @@ -34,7 +34,6 @@ #include "parser/parse_oper.h" #include "parser/parse_relation.h" #include "parser/parsetree.h" -#include "parser/parse_graphtable.h" #include "rewrite/rewriteGraphTable.h" #include "rewrite/rewriteHandler.h" #include "rewrite/rewriteManip.h" @@ -100,7 +99,8 @@ static Query *generate_query_for_empty_path_pattern(RangeTblEntry *rte); static Query *generate_union_from_pathqueries(List **pathqueries); static List *get_path_elements_for_path_factor(Oid propgraphid, struct path_factor *pf); static bool is_property_associated_with_label(Oid labeloid, Oid propoid); -static Node *get_element_property_expr(Oid elemoid, Oid propoid, int rtindex); +static Node *get_element_property_expr(Oid elemoid, Oid propoid, int rtindex, + Index varlevelsup); /* * Convert GRAPH_TABLE clause into a subquery using relational @@ -530,9 +530,10 @@ generate_query_for_graph_path(RangeTblEntry *rte, List *graph_path) if (pf->whereClause) { - Node *tr; + Node *tr = copyObject(pf->whereClause); - tr = replace_property_refs(rte->relid, pf->whereClause, list_make1(pe)); + IncrementVarSublevelsUp(tr, 1, 0); + tr = replace_property_refs(rte->relid, tr, list_make1(pe)); qual_exprs = lappend(qual_exprs, tr); } @@ -540,9 +541,10 @@ generate_query_for_graph_path(RangeTblEntry *rte, List *graph_path) if (rte->graph_pattern->whereClause) { - Node *path_quals = replace_property_refs(rte->relid, - (Node *) rte->graph_pattern->whereClause, - graph_path); + Node *path_quals = copyObject(rte->graph_pattern->whereClause); + + IncrementVarSublevelsUp(path_quals, 1, 0); + path_quals = replace_property_refs(rte->relid, path_quals, graph_path); qual_exprs = lappend(qual_exprs, path_quals); } @@ -551,9 +553,11 @@ generate_query_for_graph_path(RangeTblEntry *rte, List *graph_path) qual_exprs ? (Node *) makeBoolExpr(AND_EXPR, qual_exprs, -1) : NULL); /* Construct query targetlist from COLUMNS specification of GRAPH_TABLE. */ + path_query->targetList = copyObject(rte->graph_table_columns); + IncrementVarSublevelsUp((Node *) path_query->targetList, 1, 0); path_query->targetList = castNode(List, replace_property_refs(rte->relid, - (Node *) rte->graph_table_columns, + (Node *) path_query->targetList, graph_path)); /* @@ -1025,21 +1029,7 @@ replace_property_refs_mutator(Node *node, struct replace_property_refs_context * { if (node == NULL) return NULL; - if (IsA(node, Var)) - { - Var *var = (Var *) node; - Var *newvar = copyObject(var); - - /* - * If it's already a Var, then it was a lateral reference. Since we - * are in a subquery after the rewrite, we have to increase the level - * by one. - */ - newvar->varlevelsup++; - - return (Node *) newvar; - } - else if (IsA(node, GraphPropertyRef)) + if (IsA(node, GraphPropertyRef)) { GraphPropertyRef *gpr = (GraphPropertyRef *) node; Node *n = NULL; @@ -1063,6 +1053,8 @@ replace_property_refs_mutator(Node *node, struct replace_property_refs_context * */ Assert(found_mapping); + Assert(gpr->varlevelsup == 0); + mapping_factor = found_mapping->path_factor; /* @@ -1093,7 +1085,8 @@ replace_property_refs_mutator(Node *node, struct replace_property_refs_context * n = stringToNode(TextDatumGetCString(SysCacheGetAttrNotNull(PROPGRAPHLABELPROP, tup, Anum_pg_propgraph_label_property_plpexpr))); - ChangeVarNodes(n, 1, mapping_factor->factorpos + 1, 0); + ChangeVarNodes(n, 1, mapping_factor->factorpos + 1, + gpr->varlevelsup); ReleaseSysCache(tup); } @@ -1132,8 +1125,10 @@ replace_property_refs_mutator(Node *node, struct replace_property_refs_context * * SQL/PGQ standard section 6.5 Property Reference, General * Rule 2.b. */ - n = get_element_property_expr(found_mapping->elemoid, gpr->propid, - mapping_factor->factorpos + 1); + n = get_element_property_expr(found_mapping->elemoid, + gpr->propid, + mapping_factor->factorpos + 1, + gpr->varlevelsup); if (!n) n = (Node *) makeNullConst(gpr->typeId, gpr->typmod, gpr->collation); @@ -1304,7 +1299,8 @@ is_property_associated_with_label(Oid labeloid, Oid propoid) * NULL. */ static Node * -get_element_property_expr(Oid elemoid, Oid propoid, int rtindex) +get_element_property_expr(Oid elemoid, Oid propoid, int rtindex, + Index varlevelsup) { Relation rel; SysScanDesc scan; @@ -1331,7 +1327,7 @@ get_element_property_expr(Oid elemoid, Oid propoid, int rtindex) continue; n = stringToNode(TextDatumGetCString(SysCacheGetAttrNotNull(PROPGRAPHLABELPROP, proptup, Anum_pg_propgraph_label_property_plpexpr))); - ChangeVarNodes(n, 1, rtindex, 0); + ChangeVarNodes(n, 1, rtindex, varlevelsup); ReleaseSysCache(proptup); break; diff --git a/src/backend/rewrite/rewriteManip.c b/src/backend/rewrite/rewriteManip.c index 3653f00d383..fb9293723e1 100644 --- a/src/backend/rewrite/rewriteManip.c +++ b/src/backend/rewrite/rewriteManip.c @@ -815,6 +815,19 @@ IncrementVarSublevelsUp_walker(Node *node, var->varlevelsup += context->delta_sublevels_up; return false; /* done here */ } + if (IsA(node, GraphPropertyRef)) + { + /* + * Unlike a Var, we must not adjust a GraphPropertyRef's level here. + * This walker runs over a GRAPH_TABLE COLUMNS/WHERE expression before + * replace_property_refs() resolves each GraphPropertyRef to a Var + * local to the generated path query, setting its final level via + * ChangeVarNodes(). As we don't yet allow correlated subqueries + * inside a GRAPH_TABLE reference, varlevelsup is always 0; assert it. + */ + Assert(((GraphPropertyRef *) node)->varlevelsup == 0); + return false; /* done here */ + } if (IsA(node, CurrentOfExpr)) { /* this should not happen */ diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index 5a636d1f179..8b1f261a536 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -2196,11 +2196,23 @@ typedef struct GraphLabelRef /* * GraphPropertyRef - property reference inside GRAPH_TABLE clause + * + * A GraphPropertyRef is generated during transformation and resolved away by + * the rewriter, so only a few places need to know about it. It carries a + * varlevelsup and is treated like a Var by contain_vars_of_level() and + * locate_var_of_level(); IncrementVarSublevelsUp() leaves it alone, since + * replace_property_refs() passes its varlevelsup through when resolving it to + * a Var. + * We don't yet allow correlated subqueries inside a GRAPH_TABLE reference, so + * varlevelsup is always 0 today. */ typedef struct GraphPropertyRef { Expr xpr; const char *elvarname; + + /* > 0 for an outer-query reference, but see above: always 0 today */ + Index varlevelsup; Oid propid; Oid typeId; int32 typmod; diff --git a/src/include/parser/parse_node.h b/src/include/parser/parse_node.h index f7f4ba6c2a8..34f061df0fa 100644 --- a/src/include/parser/parse_node.h +++ b/src/include/parser/parse_node.h @@ -84,6 +84,8 @@ typedef enum ParseExprKind EXPR_KIND_GENERATED_COLUMN, /* generation expression for a column */ EXPR_KIND_CYCLE_MARK, /* cycle mark value */ EXPR_KIND_PROPGRAPH_PROPERTY, /* derived property expression */ + EXPR_KIND_GRAPH_TABLE_COLUMNS, /* GRAPH_TABLE COLUMNS list item */ + EXPR_KIND_GRAPH_TABLE_WHERE, /* WHERE in a GRAPH_TABLE */ } ParseExprKind; diff --git a/src/test/regress/expected/graph_table.out b/src/test/regress/expected/graph_table.out index 2e862a82ba0..eb2bd1b0220 100644 --- a/src/test/regress/expected/graph_table.out +++ b/src/test/regress/expected/graph_table.out @@ -471,13 +471,72 @@ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT NULL)-[ ERROR: "*" not allowed here LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT... ^ --- aggregate, window, and set-returning functions are not supported in COLUMNS +-- aggregate, grouping, window, and set-returning functions are not allowed +-- in the COLUMNS list or the graph pattern WHERE, except for outer-level aggs SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) AS num)); -ERROR: aggregate functions in GRAPH_TABLE COLUMNS are not supported +ERROR: aggregate functions are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) A... + ^ +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c.customer_id) AS g)); +ERROR: grouping operations are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c... + ^ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number() OVER () AS rn)); -ERROR: window functions in GRAPH_TABLE COLUMNS are not supported +ERROR: window functions are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number... + ^ SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_series(1, 2) AS gs)); -ERROR: set-returning functions in GRAPH_TABLE COLUMNS are not supported +ERROR: set-returning functions are not allowed in GRAPH_TABLE COLUMNS +LINE 1: ...APH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_s... + ^ +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id) > 0) COLUMNS (c.name AS nm)); +ERROR: aggregate functions are not allowed in GRAPH_TABLE WHERE +LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.cu... + ^ +SELECT count(*) FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE 1 IN (c.customer_id, 1000)) COLUMNS (c.name AS nm)) t; + count +------- + 1 +(1 row) + +SELECT EXISTS(SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.customer_id) AS num)) t) FROM customers o; + exists +-------- + t +(1 row) + +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers) WHERE count(o.customer_id) > 0 COLUMNS (c.name AS nm)) t) FROM customers o; + exists +-------- + t +(1 row) + +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; + exists +-------- + t +(1 row) + +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id + o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; +ERROR: aggregate functions are not allowed in GRAPH_TABLE WHERE +LINE 1: ...M GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.cu... + ^ +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE GROUPING(o.customer_id) = 1) COLUMNS (c.name AS nm)) t) FROM customers o GROUP BY customer_id; + exists +-------- + f + f + f +(3 rows) + +-- a property in a GRAPH_TABLE subquery below the aggregate is not local to it, +-- so the aggregate is allowed +SELECT count(o.customer_id + (SELECT count(n) FROM GRAPH_TABLE (g1 MATCH (src) COLUMNS (src.vprop1 AS n)) x)) FROM customers o; + count +------- + 3 +(1 row) + -- consecutive element patterns with same kind SELECT * FROM GRAPH_TABLE (g1 MATCH ()() COLUMNS (1 as one)); ERROR: adjacent vertex patterns are not supported @@ -1118,9 +1177,13 @@ SELECT * FROM customers co WHERE co.customer_id = (SELECT customer_id FROM GRAPH -- query within graph table 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)); -ERROR: subqueries within GRAPH_TABLE reference are not supported +ERROR: cannot use subquery in GRAPH_TABLE reference +LINE 1: ..._TABLE (g1 MATCH (src)->(dest) WHERE src.vprop1 > (SELECT ma... + ^ 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 +ERROR: cannot use subquery in GRAPH_TABLE reference +LINE 1: ...MATCH (src)->(dest) WHERE out_degree(src.vname) > (SELECT ma... + ^ -- GRAPH_TABLE subquery in HAVING clause (tests expression mutator) SELECT src.vname, count(*) FROM v1 AS src GROUP BY src.vname diff --git a/src/test/regress/sql/graph_table.sql b/src/test/regress/sql/graph_table.sql index 21e70015f6e..240288ce962 100644 --- a/src/test/regress/sql/graph_table.sql +++ b/src/test/regress/sql/graph_table.sql @@ -306,10 +306,22 @@ SELECT * FROM GRAPH_TABLE (g1 MATCH (src IS el1 | vl1)-[conn]->(dest) COLUMNS (c SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.address = 'US')-[IS customer_orders]->(o IS orders) COLUMNS (c.*)); -- star anywhere else is not allowed as a property reference SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE c.* IS NOT NULL)-[IS customer_orders]->(o IS orders) COLUMNS (c.name)); --- aggregate, window, and set-returning functions are not supported in COLUMNS +-- aggregate, grouping, window, and set-returning functions are not allowed +-- in the COLUMNS list or the graph pattern WHERE, except for outer-level aggs SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(*) AS num)); +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (GROUPING(c.customer_id) AS g)); SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (row_number() OVER () AS rn)); SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (generate_series(1, 2) AS gs)); +SELECT * FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id) > 0) COLUMNS (c.name AS nm)); +SELECT count(*) FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE 1 IN (c.customer_id, 1000)) COLUMNS (c.name AS nm)) t; +SELECT EXISTS(SELECT num FROM GRAPH_TABLE (myshop MATCH (c IS customers) COLUMNS (count(o.customer_id) AS num)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers) WHERE count(o.customer_id) > 0 COLUMNS (c.name AS nm)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE count(c.customer_id + o.customer_id) > 0) COLUMNS (c.name AS nm)) t) FROM customers o; +SELECT EXISTS(SELECT nm FROM GRAPH_TABLE (myshop MATCH (c IS customers WHERE GROUPING(o.customer_id) = 1) COLUMNS (c.name AS nm)) t) FROM customers o GROUP BY customer_id; +-- a property in a GRAPH_TABLE subquery below the aggregate is not local to it, +-- so the aggregate is allowed +SELECT count(o.customer_id + (SELECT count(n) FROM GRAPH_TABLE (g1 MATCH (src) COLUMNS (src.vprop1 AS n)) x)) FROM customers o; -- consecutive element patterns with same kind SELECT * FROM GRAPH_TABLE (g1 MATCH ()() COLUMNS (1 as one)); SELECT * FROM GRAPH_TABLE (g1 MATCH -> COLUMNS (1 AS one)); -- 2.47.3
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE
@ 2026-09-02 18:06  Melanie Plageman <melanieplageman@gmail.com>
  parent: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
  1 sibling, 1 reply; 14+ messages in thread

From: Melanie Plageman @ 2026-09-02 18:06 UTC (permalink / raw)
  To: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>; +Cc: Sami Imseih <samimseih@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; pgsql-hackers; Ewan Young <kdbase.hack@gmail.com>

On Tue, Aug 25, 2026 at 11:57 AM Ashutosh Bapat
<ashutosh.bapat.oss@gmail.com> wrote:
>
> > > So what I'm thinking right now is
> > > that this is a fundamental design error, and that we should nuke
> > > GraphPropertyRef altogether in favor of using a Var that references
> > > the appropriate column of the RTE_GRAPH_TABLE relation.
> >
> > I spent time today on this and I think the direction is right. In what
> > I have running
> > locally, a property reference is emitted as a plain Var over the
> > RTE_GRAPH_TABLE relation,
> > so the Var handles leveling and the rest for free. GraphPropertyRef
> > does not go away
> > entirely, though. It moves onto a side list on the RTE that the
> > rewriter uses to resolve
> > each property Var back to its graph property.
> >
>
> I share the concern raised by Tom about bugs due to missing
> GraphPropertyRef handling. However, I don't think we can use Var
> instead of GraphPropertyRef. It represents a reference to a property
> of all the graph elements that are bound to an element pattern in a
> GRAPH_TABLE. The element pattern is not represented by a range table
> and Var node does not have a field to represent an element pattern
> variable. Further the same GraphPropertyRef may be rewritten as
> different expressions depending on the element table it gets
> associated with when generating a subquery for a given path. Achieving
> this with Var will be quite complex, if not impossible. The question
> is whether we can find all the places where we need to treat
> GraphPropertyRef as a Var. Given that the GraphPropertyRefs are
> generated during transformation and vanish after rewrite, the places
> where we need to handle them are limited, as noted by Tom already.
> Many of those are already covered by the patch. Going through all the
> places, rather painfully, where we use levelsup for Var-like nodes,
> looking at var.c, I see that most of that code is applicable to
> planning, optimization and execution. The additional places which I
> think we need to handle are:
> a. locate_var_of_level_walker() to handle GraphPropertyRef so that the
> correct error location can be reported. We have separate
> locate_aggref_of_level_walker(). So first I thought that it would be
> appropriate to add a separate
> locate_graphpropertyref_of_level_walker() function. But we want to
> report an error from a place where the distinction between Var and
> GraphPropertyRef is lost. So a separate function doesn't make sense.
> b. contain_vars_of_level() should not see GraphPropertyRef since the
> latter lives within GRAPH_TABLE only. But the function is used during
> query transformation, so it will be good to add a GraphPropertyRef
> case and Assert()/throw error if it is seen. We should add a comment
> there to expect GraphPropertyRef there once we start supporting nested
> subqueries in GRAPH_TABLE.
> c. IncrementVarSublevelsUp() will need to handle GraphPropertyRef once
> we support nested subqueries in GRAPH_TABLE. For now we may want to
> add a GraphPropertyRef case and Assert()/throw error if it is seen. We
> should add a comment there to expect GraphPropertyRef there once we
> start supporting nested subqueries in GRAPH_TABLE. Please note, I
> don't expect ChangeVarNodes to handle GraphPropertyRef since there is
> no rtindex in GraphPropertyRef.
>
> I didn't find any other place where we need to handle
> GraphPropertyRef. But there are just many places where we use Vars and
> varlevelsup. So there is a non-zero possibility that I may have missed
> a few. I wish we could have differentiated between Var in the planner,
> optimizer code and ColumnRef in the parser, transformer and rewriter
> code. That would have made it easier to find the places where we need
> to handle GraphPropertyRef. But the ship has sailed long back.

[RMT hat]

RMT is wondering if the ongoing work on this is an enhancement for 20
or proposed work for 19. Does Ashutosh's reply satisfy Tom and Sami's
concerns with the design. Is there work here that is still being
proposed for v19 and is there consensus on it?

- Melanie






^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE
@ 2026-09-02 18:42  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Melanie Plageman <melanieplageman@gmail.com>
  0 siblings, 1 reply; 14+ messages in thread

From: Tom Lane @ 2026-09-02 18:42 UTC (permalink / raw)
  To: Melanie Plageman <melanieplageman@gmail.com>; +Cc: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>; Sami Imseih <samimseih@gmail.com>; pgsql-hackers; Ewan Young <kdbase.hack@gmail.com>

Melanie Plageman <melanieplageman@gmail.com> writes:
> RMT is wondering if the ongoing work on this is an enhancement for 20
> or proposed work for 19. Does Ashutosh's reply satisfy Tom and Sami's
> concerns with the design. Is there work here that is still being
> proposed for v19 and is there consensus on it?

TBH, I stopped paying attention to this thread after I saw Andres'
concerns about PGQ [1].  I figured there was no chance of PGQ not
getting reverted for 19, so it wasn't urgent to fix this aspect
of it.  Hence, I don't have an immediate answer on whether this is
a good-enough fix.

I will say that I do not like GraphPropertyRef one bit and that the
best way forward if we're not building an emergency band-aid for 19
is to get rid of it.  "We have to deal with this everyplace we deal
with Vars" is not an appetizing prospect.  I did realize after the
initial discussion that we already have a binding for Vars pointing at
the RTE_GRAPH_TABLE RTE, so we can't commandeer that representation
for what GraphPropertyRef represents.  But there's more than one way
to skin that cat.  The next idea I'd propose is to create a second RTE
of a different RTEKind, and then replace GraphPropertyRefs with Vars
pointing at that one.  This would be kind of analogous to RTE_GROUP
Vars, or at least that's a handy precedent.

			regards, tom lane

[1] https://www.postgresql.org/message-id/flat/dqa5mstx5mna3i7s23pdwl4m6bek7gqsgfccef44wjpswizufi%403aa6...






^ permalink  raw  reply  [nested|flat] 14+ messages in thread

* Re: Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE
@ 2026-09-08 15:38  Sami Imseih <samimseih@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 14+ messages in thread

From: Sami Imseih @ 2026-09-08 15:38 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Melanie Plageman <melanieplageman@gmail.com>; Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>; pgsql-hackers; Ewan Young <kdbase.hack@gmail.com>

Hi,

Due to b1f106c80cbe, I withdrew this CF entry for this:
https://commitfest.postgresql.org/patch/7144/

--
Sami Imseih
Amazon Web Services (AWS)






^ permalink  raw  reply  [nested|flat] 14+ messages in thread


end of thread, other threads:[~2026-09-08 15:38 UTC | newest]

Thread overview: 14+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-08-12 15:34 Disallow outer-level and WHERE-clause aggregates in GRAPH_TABLE Sami Imseih <samimseih@gmail.com>
2026-08-12 16:16 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-08-12 22:53   ` Sami Imseih <samimseih@gmail.com>
2026-08-13 15:35     ` Sami Imseih <samimseih@gmail.com>
2026-08-15 19:21       ` Tom Lane <tgl@sss.pgh.pa.us>
2026-08-16 00:23         ` Sami Imseih <samimseih@gmail.com>
2026-08-16 01:18           ` Tom Lane <tgl@sss.pgh.pa.us>
2026-08-17 23:29             ` Sami Imseih <samimseih@gmail.com>
2026-08-25 15:57               ` Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
2026-09-01 00:14                 ` Sami Imseih <samimseih.pg@gmail.com>
2026-09-01 01:08                   ` Sami Imseih <samimseih.pg@gmail.com>
2026-09-02 18:06                 ` Melanie Plageman <melanieplageman@gmail.com>
2026-09-02 18:42                   ` Tom Lane <tgl@sss.pgh.pa.us>
2026-09-08 15:38                     ` Sami Imseih <samimseih@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox