GRAPH_TABLE: aggregates/window/set-returning functions in COLUMNS crash the backend

From: Ewan Young <kdbase(dot)hack(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: Peter Eisentraut <peter(at)eisentraut(dot)org>, Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
Subject: GRAPH_TABLE: aggregates/window/set-returning functions in COLUMNS crash the backend
Date: 2026-06-16 09:10:51
Message-ID: CAON2xHOYAmYLkB2jGi6g77d6Fqv8YgOrfV-riQVf0K_7AdxD3w@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

While testing SQL/PGQ I found that putting an aggregate, window
function, or set-returning function in the COLUMNS list of a
GRAPH_TABLE query crashes the backend.

Minimal reproducer:

CREATE TABLE v (id int PRIMARY KEY);
INSERT INTO v VALUES (1);
CREATE PROPERTY GRAPH g VERTEX TABLES (v);

SELECT max(c) FROM GRAPH_TABLE (g MATCH (x IS v) COLUMNS (count(*) AS c));
On an assert-enabled build this trips

TRAP: failed Assert("econtext->ecxt_aggvalues != NULL"),
File: "execExprInterp.c", Line: 1969
and on a non-assert build it fails at execution with

ERROR: Aggref found in non-Agg plan node
A window function in COLUMNS behaves the same way; a set-returning
function is silently accepted and produces nonsensical results.

Root cause: transformRangeGraphTable() (parser/parse_clause.c)
transforms the COLUMNS expressions with EXPR_KIND_SELECT_TARGET, which
permits aggregates, window functions and SRFs. GRAPH_TABLE has no
machinery to evaluate them, though: rewriteGraphTable.c copies the
COLUMNS target list verbatim into a freshly built subquery whose
hasAggs/hasWindowFuncs flags are never set, so the planner builds no
Agg/WindowAgg node and the Aggref/WindowFunc reaches the executor.
(As a side effect p_hasAggs also leaks into the enclosing query,
yielding spurious "must appear in the GROUP BY clause" errors for some
other COLUMNS shapes.)

These constructs are not meaningful in a GRAPH_TABLE COLUMNS list, so
the attached patch rejects them at parse-analysis time, the same way
every other non-aggregating context does. It adds a dedicated
EXPR_KIND_GRAPH_TABLE_COLUMNS and wires it into the aggregate, window
and set-returning-function checks, producing errors such as

ERROR: aggregate functions are not allowed in GRAPH_TABLE COLUMNS
Plain column references and subqueries are unaffected (subqueries
continue to be rejected as before). A regression test is added to
graph_table.sql, and "make check" passes.

This is present on master and in the PG19 beta.

Thanks,
Ewan Young

Attachment Content-Type Size
v1-0001-Reject-aggregates-window-functions-and-SRFs-in-GR.patch application/octet-stream 8.9 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Ajin Cherian 2026-06-16 09:20:01 Re: [PATCH] Preserve replication origin OIDs in pg_upgrade
Previous Message Ashutosh Bapat 2026-06-16 09:05:47 Re: Report bytes and transactions actually sent downtream