Re: [PATCH] rewriteGraphTable: Fix missing RTEs in FROM clause by setting inFromCl=true

From: Henson Choi <assam258(at)gmail(dot)com>
To: zengman <zengman(at)halodbtech(dot)com>, Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Peter Eisentraut <peter(at)eisentraut(dot)org>, Junwang Zhao <zhjwpku(at)gmail(dot)com>
Subject: Re: [PATCH] rewriteGraphTable: Fix missing RTEs in FROM clause by setting inFromCl=true
Date: 2026-03-19 15:03:14
Message-ID: CAAAe_zBp8_SNdkbG=Gc_XkmoVUwv9WJGefOBKLar7UdNo8256A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Man,

I made a super simple extension `https://github.com/Z-Xiao-M/pg_pgq2sql`
> <https://github.com/Z-Xiao-M/pg_pgq2sql> to get the equivalent SQL of PGQ
> queries – it simply calls `pg_get_querydef` after `QueryRewrite` to fetch
> the SQL text.
>
> However, I noticed that some FROM clauses were missing in the generated
> SQL statements.
>

Good catch. The patch is correct — these RTEs are real FROM clause
entries (they're appended to fromlist), so inFromCl should be true.

I looked at whether this has any impact beyond pg_pgq2sql:

- pg_get_viewdef() deparses the pre-rewrite query, so it outputs the
original GRAPH_TABLE(...) syntax — not affected.
- pg_dump uses pg_get_viewdef(), so also not affected.

So in practice, this only affects code that deparses the post-rewrite
query (like your extension). No core code path currently does this for
graph table queries.

> pni = addRangeTableEntryForRelation(make_parsestate(NULL),
> rel, AccessShareLock,
> -
> NULL, true, false);
> +
> NULL, true, true);
>

That said, the fix is clearly correct. As documented in parsenodes.h,
inFromCl is false for "RTEs that are added to a query behind the
scenes, such as the NEW and OLD variables for a rule, or the subqueries
of a UNION." The element table RTEs here are neither — they are
genuinely in the FROM clause.

Ashutosh, what do you think?

Best regards,
Henson

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Junwang Zhao 2026-03-19 15:04:58 Re: [PATCH] rewriteGraphTable: Fix missing RTEs in FROM clause by setting inFromCl=true
Previous Message Andrew Dunstan 2026-03-19 15:02:21 Re: Emitting JSON to file using COPY TO