Ignore IntoClause.viewQuery in query jumbling
authorMichael Paquier <michael@paquier.xyz>
Wed, 8 Mar 2023 02:41:52 +0000 (11:41 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 8 Mar 2023 02:41:52 +0000 (11:41 +0900)
IntoClause.viewQuery is a copy of the parsed-but-not-rewritten SELECT
clause copied to IntoClause when transforming CreateTableAsStmt for a
materialized view.  Including a second copy of the SELECT Query into the
query jumbling was leading to an incorrect numbering of the Const node
locations, as these would be counted twice instead of once.

This becomes visible once the query normalization is applied to CREATE
MATERIALIZED VIEW in pg_stat_statements in the shape of a query string
using only odd numbers for the normalized constants, (regression tests
added in pg_stat_statements as of de2aca2 would show the difference).
Including the original Query from CreateTableAsStmt is enough for the
query jumbling.

Reviewed-by: Bertrand Drouvot
Discussion: https://postgr.es/m/Y+MRdEq9W9XVa2AB@paquier.xyz

src/include/nodes/primnodes.h

index b4292253ccf204daf7dbe84baea1152db9c09103..4220c63ab726aba7535f19433683f12ac335924e 100644 (file)
@@ -128,8 +128,10 @@ typedef struct TableFunc
  * CREATE MATERIALIZED VIEW
  *
  * For CREATE MATERIALIZED VIEW, viewQuery is the parsed-but-not-rewritten
- * SELECT Query for the view; otherwise it's NULL.  (Although it's actually
- * Query*, we declare it as Node* to avoid a forward reference.)
+ * SELECT Query for the view; otherwise it's NULL.  This is irrelevant in
+ * the query jumbling as CreateTableAsStmt already includes a reference to
+ * its own Query, so ignore it.  (Although it's actually Query*, we declare
+ * it as Node* to avoid a forward reference.)
  */
 typedef struct IntoClause
 {
@@ -141,7 +143,8 @@ typedef struct IntoClause
    List       *options;        /* options from WITH clause */
    OnCommitAction onCommit;    /* what do we do at COMMIT? */
    char       *tableSpaceName; /* table space to use, or NULL */
-   Node       *viewQuery;      /* materialized view's SELECT query */
+   /* materialized view's SELECT query */
+   Node       *viewQuery pg_node_attr(query_jumble_ignore);
    bool        skipData;       /* true for WITH NO DATA */
 } IntoClause;