Klugy fix for bug #2447: we can't expand a whole-row reference to NEW
authorTom Lane <[email protected]>
Tue, 23 May 2006 17:09:18 +0000 (17:09 +0000)
committerTom Lane <[email protected]>
Tue, 23 May 2006 17:09:18 +0000 (17:09 +0000)
in a rule WHERE expression while inserting it into the original query,
because the 8.0 ResolveNew API is wrongly designed.  This is fixed in 8.1
but I'm disinclined to risk back-porting the changes.  Instead, just stop
the coredump and instead issue the same 'cannot handle whole-row reference'
message that 7.4 and before generated in this situation.

src/backend/rewrite/rewriteHandler.c
src/backend/rewrite/rewriteManip.c

index 53088370b46fd0e0625ebe0111a12e0206be6ec0..cb75abd235dfcc118f054e00b605d8f0a288076a 100644 (file)
@@ -1046,7 +1046,7 @@ CopyAndAddInvertedQual(Query *parsetree,
                new_qual = ResolveNew(new_qual,
                                                          PRS2_NEW_VARNO,
                                                          0,
-                                                         parsetree->rtable,
+                                                         NIL,                          /* fix bug #2447 */
                                                          parsetree->targetList,
                                                          event,
                                                          rt_index);
index 66dd4142982e86518755b3e9ff11774355d89518..cf8e776ad68d9df2af3855d950a89bd0da4c123f 100644 (file)
@@ -942,6 +942,24 @@ ResolveNew_mutator(Node *node, ResolveNew_context *context)
                                RowExpr    *rowexpr;
                                List       *fields;
 
+                               /*
+                                * Klugy fix for bug #2447: we can't expand a whole-row
+                                * reference to "NEW" in a rule WHERE expression while
+                                * inserting it into the original query, because its varno
+                                * will be PRS2_NEW_VARNO which is not the rtindex of the RTE
+                                * we should use.  The 8.0 ResolveNew API is wrongly designed.
+                                * We aren't going to try to back-port the 8.1 API, just avoid
+                                * crashing.  (This never worked before 8.0, either.)
+                                * CopyAndAddInvertedQual passes NIL for target_rtable, so
+                                * testing for NIL would be sufficient, but let's put in a
+                                * full check on the rtindex for safety.
+                                */
+                               if (this_varno < 1 ||
+                                       this_varno > list_length(context->target_rtable))
+                                       ereport(ERROR,
+                                                       (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                                        errmsg("cannot handle whole-row reference")));
+
                                /*
                                 * If generating an expansion for a var of a named rowtype
                                 * (ie, this is a plain relation RTE), then we must