Re: [PATCH] rewriteGraphTable: Fix missing RTEs in FROM clause by setting inFromCl=true - Mailing list pgsql-hackers

From Henson Choi
Subject Re: [PATCH] rewriteGraphTable: Fix missing RTEs in FROM clause by setting inFromCl=true
Date
Msg-id CAAAe_zBp8_SNdkbG=Gc_XkmoVUwv9WJGefOBKLar7UdNo8256A@mail.gmail.com
Whole thread Raw
In response to [PATCH] rewriteGraphTable: Fix missing RTEs in FROM clause by setting inFromCl=true  ("zengman" <zengman@halodbtech.com>)
List pgsql-hackers
Hi Man,

I made a super simple extension `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

pgsql-hackers by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: Emitting JSON to file using COPY TO
Next
From: Junwang Zhao
Date:
Subject: Re: [PATCH] rewriteGraphTable: Fix missing RTEs in FROM clause by setting inFromCl=true