Commit c6be1f43 authored by Tom Lane's avatar Tom Lane

Make INSERT/UPDATE queries depend on their specific target columns.

We have always created a whole-table dependency for the target relation,
but that's not really good enough, as it doesn't prevent scenarios such
as dropping an individual target column or altering its type.  So we
have to create an individual dependency for each target column, as well.

Per report from Bill MacArthur of a rule containing UPDATE breaking
after such an alteration.  Note that this patch doesn't try to make
such cases work, only to ensure that the attempted ALTER TABLE throws
an error telling you it can't cope with adjusting the rule.

This is a long-standing bug, but given the lack of prior reports
I'm not going to risk back-patching it.  A back-patch wouldn't do
anything to fix existing rules' dependency lists, anyway.
parent 81421661
......@@ -1743,6 +1743,37 @@ find_expr_references_walker(Node *node,
}
}
/*
* If the query is an INSERT or UPDATE, we should create a dependency
* on each target column, to prevent the specific target column from
* being dropped. Although we will visit the TargetEntry nodes again
* during query_tree_walker, we won't have enough context to do this
* conveniently, so do it here.
*/
if (query->commandType == CMD_INSERT ||
query->commandType == CMD_UPDATE)
{
RangeTblEntry *rte;
if (query->resultRelation <= 0 ||
query->resultRelation > list_length(query->rtable))
elog(ERROR, "invalid resultRelation %d",
query->resultRelation);
rte = rt_fetch(query->resultRelation, query->rtable);
if (rte->rtekind == RTE_RELATION)
{
foreach(lc, query->targetList)
{
TargetEntry *tle = (TargetEntry *) lfirst(lc);
if (tle->resjunk)
continue; /* ignore junk tlist items */
add_object_address(OCLASS_CLASS, rte->relid, tle->resno,
context->addrs);
}
}
}
/*
* Add dependencies on constraints listed in query's constraintDeps
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment