]> granicus.if.org Git - postgresql/commitdiff
Make INSERT/UPDATE queries depend on their specific target columns.
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 11 Mar 2012 22:14:23 +0000 (18:14 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 11 Mar 2012 22:14:23 +0000 (18:14 -0400)
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.

src/backend/catalog/dependency.c

index 1b92f5c38a1efa97dc644cb769ba1feae55e35a5..fed724c51cb8cc178c675277d75a1dd742ba386a 100644 (file)
@@ -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
                 */