diff --git a/createas.c b/createas.c index 4617d56..d17285a 100644 --- a/createas.c +++ b/createas.c @@ -262,7 +262,11 @@ ExecCreateImmv(ParseState *pstate, CreateTableAsStmt *stmt, /* Create an index on incremental maintainable materialized view, if possible */ CreateIndexOnIMMV(viewQuery, matviewRel, true); - /* Create triggers on incremental maintainable materialized view */ + /* + * Create triggers on incremental maintainable materialized view + * This argument should use 'query'. This needs to use a rewritten query, + * because a sublink in jointree is not supported by this function. + */ CreateIvmTriggersOnBaseTables(query, matviewOid, true); /* Create triggers to prevent IMMV from beeing changed */ diff --git a/matview.c b/matview.c index 4e90b00..f3782d3 100644 --- a/matview.c +++ b/matview.c @@ -1049,10 +1049,10 @@ IVM_immediate_maintenance(PG_FUNCTION_ARGS) char *count_colname = NULL; /* check if the modified table is in EXISTS clause. */ - for (i = 0; i< list_length(rte_path); i++) + for (i = 0; i < list_length(rte_path); i++) { int index = lfirst_int(list_nth_cell(rte_path, i)); - rte = (RangeTblEntry *)lfirst(list_nth_cell(querytree->rtable, index - 1)); + rte = (RangeTblEntry *) lfirst(list_nth_cell(querytree->rtable, index - 1)); if (rte != NULL && rte->rtekind == RTE_SUBQUERY) { @@ -1540,7 +1540,7 @@ rewrite_query_for_distinct_and_aggregates(Query *query, ParseState *pstate) /* Add count(*) used for EXISTS clause */ foreach(tbl_lc, query->rtable) { - RangeTblEntry *rte = (RangeTblEntry *)lfirst(tbl_lc); + RangeTblEntry *rte = (RangeTblEntry *) lfirst(tbl_lc); varno++; if (rte->subquery) { @@ -1600,11 +1600,14 @@ rewrite_exists_subquery_walker(Query *query, Node *node, int *count) FromExpr *fromexpr; /* get subquery in WHERE clause */ - fromexpr = (FromExpr *)query->jointree; - query = rewrite_exists_subquery_walker(query, fromexpr->quals, count); - /* drop subquery in WHERE clause */ - if (IsA(fromexpr->quals, SubLink)) - fromexpr->quals = NULL; + fromexpr = (FromExpr *) query->jointree; + if (fromexpr->quals != NULL) + { + query = rewrite_exists_subquery_walker(query, fromexpr->quals, count); + /* drop subquery in WHERE clause */ + if (IsA(fromexpr->quals, SubLink)) + fromexpr->quals = NULL; + } break; } case T_BoolExpr: