rte->rtekind = RTE_RELATION;
rte->relid = intoRelationId;
rte->relkind = relkind;
- rte->isResultRel = true;
rte->requiredPerms = ACL_INSERT;
for (attnum = 1; attnum <= intoRelationDesc->rd_att->natts; attnum++)
List *rewritten;
PlannedStmt *plan;
QueryDesc *queryDesc;
- List *rtable;
- RangeTblEntry *initial_rte;
- RangeTblEntry *second_rte;
+ /* Rewrite, copying the given Query to make sure it's not changed */
rewritten = QueryRewrite((Query *) copyObject(query));
/* SELECT should never rewrite to more or less than one SELECT query */
/* Check for user-requested abort. */
CHECK_FOR_INTERRUPTS();
- /*
- * Kludge here to allow refresh of a materialized view which is invalid
- * (that is, it was created or refreshed WITH NO DATA. We flag the first
- * two RangeTblEntry list elements, which were added to the front of the
- * rewritten Query to keep the rules system happy, with the isResultRel
- * flag to indicate that it is OK if they are flagged as invalid. See
- * UpdateRangeTableOfViewParse() for details.
- *
- * NOTE: The rewrite has switched the frist two RTEs, but they are still
- * in the first two positions. If that behavior changes, the asserts here
- * will fail.
- */
- rtable = query->rtable;
- initial_rte = ((RangeTblEntry *) linitial(rtable));
- Assert(strcmp(initial_rte->alias->aliasname, "new"));
- initial_rte->isResultRel = true;
- second_rte = ((RangeTblEntry *) lsecond(rtable));
- Assert(strcmp(second_rte->alias->aliasname, "old"));
- second_rte->isResultRel = true;
-
/* Plan the query which will generate data for the refresh. */
plan = pg_plan_query(query, 0, NULL);
int maxfieldlen);
static void EvalPlanQualStart(EPQState *epqstate, EState *parentestate,
Plan *planTree);
-static bool RelationIdIsScannable(Oid relid);
/* end of local decls */
}
-/*
- * ExecCheckRelationsScannable
- * Check that relations which are to be accessed are in a scannable
- * state.
- *
- * Currently the only relations which are not are materialized views which
- * have not been populated by their queries.
- */
-static void
-ExecCheckRelationsScannable(List *rangeTable)
-{
- ListCell *l;
-
- foreach(l, rangeTable)
- {
- RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
-
- if (rte->rtekind != RTE_RELATION)
- continue;
-
- if (rte->relkind != RELKIND_MATVIEW)
- continue;
-
- /* It is OK to target an unpopulated materialized for results. */
- if (rte->isResultRel)
- continue;
-
- if (!RelationIdIsScannable(rte->relid))
- ereport(ERROR,
- (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
- errmsg("materialized view \"%s\" has not been populated",
- get_rel_name(rte->relid)),
- errhint("Use the REFRESH MATERIALIZED VIEW command.")));
- }
-}
-
-/*
- * Tells whether a relation is scannable based on its OID.
- *
- * Currently only non-populated materialized views are not. This is likely to
- * change to include other conditions.
- *
- * This should only be called while a lock is held on the relation.
- */
-static bool
-RelationIdIsScannable(Oid relid)
-{
- Relation relation;
- bool result;
-
- relation = heap_open(relid, NoLock);
- result = RelationIsScannable(relation);
- heap_close(relation, NoLock);
-
- return result;
-}
-
/*
* ExecCheckRTPerms
* Check access permissions for all relations listed in a range table.
*/
planstate = ExecInitNode(plan, estate, eflags);
- /*
- * Unless we are creating a view or are creating a materialized view WITH
- * NO DATA, ensure that all referenced relations are scannable. The
- * omitted cases will be checked as SELECT statements in a different
- * phase, so checking again here would be wasteful and it would generate
- * errors on a materialized view referenced as a target.
- *
- * NB: This is being done after all relations are locked, files have been
- * opened, etc., to avoid duplicating that effort or creating deadlock
- * possibilities.
- */
- if ((eflags & EXEC_FLAG_WITH_NO_DATA) == 0)
- ExecCheckRelationsScannable(rangeTable);
-
/*
* Get the tuple descriptor describing the type of tuples to return.
*/
* ----------------------------------------------------------------
*/
Relation
-ExecOpenScanRelation(EState *estate, Index scanrelid)
+ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags)
{
+ Relation rel;
Oid reloid;
LOCKMODE lockmode;
}
}
- /* OK, open the relation and acquire lock as needed */
+ /* Open the relation and acquire lock as needed */
reloid = getrelid(scanrelid, estate->es_range_table);
- return heap_open(reloid, lockmode);
+ rel = heap_open(reloid, lockmode);
+
+ /*
+ * Complain if we're attempting a scan of an unscannable relation, except
+ * when the query won't actually be run. This is a slightly klugy place
+ * to do this, perhaps, but there is no better place.
+ */
+ if ((eflags & (EXEC_FLAG_EXPLAIN_ONLY | EXEC_FLAG_WITH_NO_DATA)) == 0 &&
+ !RelationIsScannable(rel))
+ ereport(ERROR,
+ (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ errmsg("materialized view \"%s\" has not been populated",
+ RelationGetRelationName(rel)),
+ errhint("Use the REFRESH MATERIALIZED VIEW command.")));
+
+ return rel;
}
/* ----------------------------------------------------------------
/*
* open the base relation and acquire appropriate lock on it.
*/
- currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid);
+ currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
scanstate->ss.ss_currentRelation = currentRelation;
/*
* open the base relation and acquire appropriate lock on it.
*/
- currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid);
+ currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
scanstate->ss.ss_currentRelation = currentRelation;
/*
/*
* open the base relation and acquire appropriate lock on it.
*/
- currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid);
+ currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
indexstate->ss.ss_currentRelation = currentRelation;
indexstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
/*
* open the base relation and acquire appropriate lock on it.
*/
- currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid);
+ currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
indexstate->ss.ss_currentRelation = currentRelation;
indexstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
#include "executor/nodeSeqscan.h"
#include "utils/rel.h"
-static void InitScanRelation(SeqScanState *node, EState *estate);
+static void InitScanRelation(SeqScanState *node, EState *estate, int eflags);
static TupleTableSlot *SeqNext(SeqScanState *node);
/* ----------------------------------------------------------------
/* ----------------------------------------------------------------
* InitScanRelation
*
- * This does the initialization for scan relations and
- * subplans of scans.
+ * Set up to access the scan relation.
* ----------------------------------------------------------------
*/
static void
-InitScanRelation(SeqScanState *node, EState *estate)
+InitScanRelation(SeqScanState *node, EState *estate, int eflags)
{
Relation currentRelation;
HeapScanDesc currentScanDesc;
* open that relation and acquire appropriate lock on it.
*/
currentRelation = ExecOpenScanRelation(estate,
- ((SeqScan *) node->ps.plan)->scanrelid);
+ ((SeqScan *) node->ps.plan)->scanrelid,
+ eflags);
+ /* initialize a heapscan */
currentScanDesc = heap_beginscan(currentRelation,
estate->es_snapshot,
0,
node->ss_currentRelation = currentRelation;
node->ss_currentScanDesc = currentScanDesc;
+ /* and report the scan tuple slot's rowtype */
ExecAssignScanType(node, RelationGetDescr(currentRelation));
}
/*
* initialize scan relation
*/
- InitScanRelation(scanstate, estate);
+ InitScanRelation(scanstate, estate, eflags);
scanstate->ps.ps_TupFromTlist = false;
/*
* open the base relation and acquire appropriate lock on it.
*/
- currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid);
+ currentRelation = ExecOpenScanRelation(estate, node->scan.scanrelid, eflags);
tidstate->ss.ss_currentRelation = currentRelation;
tidstate->ss.ss_currentScanDesc = NULL; /* no heap scan here */
COPY_SCALAR_FIELD(rtekind);
COPY_SCALAR_FIELD(relid);
COPY_SCALAR_FIELD(relkind);
- COPY_SCALAR_FIELD(isResultRel);
COPY_NODE_FIELD(subquery);
COPY_SCALAR_FIELD(security_barrier);
COPY_SCALAR_FIELD(jointype);
COMPARE_SCALAR_FIELD(rtekind);
COMPARE_SCALAR_FIELD(relid);
COMPARE_SCALAR_FIELD(relkind);
- COMPARE_SCALAR_FIELD(isResultRel);
COMPARE_NODE_FIELD(subquery);
COMPARE_SCALAR_FIELD(security_barrier);
COMPARE_SCALAR_FIELD(jointype);
case RTE_RELATION:
WRITE_OID_FIELD(relid);
WRITE_CHAR_FIELD(relkind);
- WRITE_BOOL_FIELD(isResultRel);
break;
case RTE_SUBQUERY:
WRITE_NODE_FIELD(subquery);
case RTE_RELATION:
READ_OID_FIELD(relid);
READ_CHAR_FIELD(relkind);
- READ_BOOL_FIELD(isResultRel);
break;
case RTE_SUBQUERY:
READ_NODE_FIELD(subquery);
result = true;
break;
- case T_RefreshMatViewStmt:
- /* yes, because the SELECT from pg_rewrite must be analyzed */
- result = true;
- break;
-
default:
/* other utility statements don't have any real parse analysis */
result = false;
if (rte->rtekind != RTE_RELATION)
continue;
+ /*
+ * Always ignore RIR rules for materialized views referenced in
+ * queries. (This does not prevent refreshing MVs, since they aren't
+ * referenced in their own query definitions.)
+ *
+ * Note: in the future we might want to allow MVs to be conditionally
+ * expanded as if they were regular views, if they are not scannable.
+ * In that case this test would need to be postponed till after we've
+ * opened the rel, so that we could check its state.
+ */
+ if (rte->relkind == RELKIND_MATVIEW)
+ continue;
+
/*
* If the table is not referenced in the query, then we ignore it.
* This prevents infinite expansion loop due to new rtable entries
*/
rel = heap_open(rte->relid, NoLock);
- /*
- * Ignore RIR rules for a materialized view, if it is scannable.
- *
- * NOTE: This is assuming that if an MV is scannable then we always
- * want to use the existing contents, and if it is not scannable we
- * cannot have gotten to this point unless it is being populated
- * (otherwise an error should be thrown). It would be nice to have
- * some way to confirm that we're doing the right thing here, but rule
- * expansion doesn't give us a lot to work with, so we are trusting
- * earlier validations to throw error if needed.
- */
- if (rel->rd_rel->relkind == RELKIND_MATVIEW &&
- RelationIsScannable(rel))
- {
- heap_close(rel, NoLock);
- continue;
- }
-
/*
* Collect the RIR rules that we must apply
*/
*/
/* yyyymmddN */
-#define CATALOG_VERSION_NO 201304151
+#define CATALOG_VERSION_NO 201304271
#endif
extern bool ExecRelationIsTargetRelation(EState *estate, Index scanrelid);
-extern Relation ExecOpenScanRelation(EState *estate, Index scanrelid);
+extern Relation ExecOpenScanRelation(EState *estate, Index scanrelid, int eflags);
extern void ExecCloseScanRelation(Relation scanrel);
extern void ExecOpenIndices(ResultRelInfo *resultRelInfo);
*/
Oid relid; /* OID of the relation */
char relkind; /* relation kind (see pg_class.relkind) */
- bool isResultRel; /* used in target of SELECT INTO or similar */
/*
* Fields valid for a subquery RTE (else NULL):
NodeTag type;
Node *query; /* the query (see comments above) */
IntoClause *into; /* destination table */
- ObjectType relkind; /* type of object */
+ ObjectType relkind; /* OBJECT_TABLE or OBJECT_MATVIEW */
bool is_select_into; /* it was written as SELECT INTO */
} CreateTableAsStmt;