11/*-------------------------------------------------------------------------
22 *
33 * rewriteHandler.c
4+ * Primary module of query rewriter.
45 *
56 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
67 * Portions Copyright (c) 1994, Regents of the University of California
78 *
8- *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.94 2001/06/12 18:54:22 tgl Exp $
10+ * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.95 2001/06/13 18:56:30 tgl Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
2727#include "parser/parse_target.h"
2828#include "parser/parsetree.h"
2929#include "parser/parse_type.h"
30+ #include "rewrite/rewriteHandler.h"
3031#include "rewrite/rewriteManip.h"
3132#include "utils/lsyscache.h"
3233
3334
34- static RewriteInfo * gatherRewriteMeta (Query * parsetree ,
35- Query * rule_action ,
36- Node * rule_qual ,
37- int rt_index ,
38- CmdType event ,
39- bool instead_flag );
35+ static Query * rewriteRuleAction (Query * parsetree ,
36+ Query * rule_action ,
37+ Node * rule_qual ,
38+ int rt_index ,
39+ CmdType event );
4040static List * adjustJoinTreeList (Query * parsetree , bool removert , int rt_index );
4141static void markQueryForUpdate (Query * qry , bool skipOldNew );
4242static List * matchLocks (CmdType event , RuleLock * rulelocks ,
@@ -45,40 +45,33 @@ static Query *fireRIRrules(Query *parsetree);
4545
4646
4747/*
48- * gatherRewriteMeta -
49- * Gather meta information about parsetree, and rule. Fix rule body
50- * and qualifier so that they can be mixed with the parsetree and
51- * maintain semantic validity
48+ * rewriteRuleAction -
49+ * Rewrite the rule action with appropriate qualifiers (taken from
50+ * the triggering query).
5251 */
53- static RewriteInfo *
54- gatherRewriteMeta (Query * parsetree ,
52+ static Query *
53+ rewriteRuleAction (Query * parsetree ,
5554 Query * rule_action ,
5655 Node * rule_qual ,
5756 int rt_index ,
58- CmdType event ,
59- bool instead_flag )
57+ CmdType event )
6058{
61- RewriteInfo * info ;
59+ int current_varno ,
60+ new_varno ;
61+ int rt_length ;
6262 Query * sub_action ;
6363 Query * * sub_action_ptr ;
64- int rt_length ;
6564
66- info = (RewriteInfo * ) palloc (sizeof (RewriteInfo ));
67- info -> rt_index = rt_index ;
68- info -> event = event ;
69- info -> instead_flag = instead_flag ;
70- info -> rule_action = (Query * ) copyObject (rule_action );
71- info -> rule_qual = (Node * ) copyObject (rule_qual );
72- if (info -> rule_action == NULL )
73- {
74- info -> nothing = TRUE;
75- return info ;
76- }
77- info -> nothing = FALSE;
78- info -> action = info -> rule_action -> commandType ;
79- info -> current_varno = rt_index ;
65+ /*
66+ * Make modifiable copies of rule action and qual (what we're passed
67+ * are the stored versions in the relcache; don't touch 'em!).
68+ */
69+ rule_action = (Query * ) copyObject (rule_action );
70+ rule_qual = (Node * ) copyObject (rule_qual );
71+
72+ current_varno = rt_index ;
8073 rt_length = length (parsetree -> rtable );
81- info -> new_varno = PRS2_NEW_VARNO + rt_length ;
74+ new_varno = PRS2_NEW_VARNO + rt_length ;
8275
8376 /*
8477 * Adjust rule action and qual to offset its varnos, so that we can
@@ -88,14 +81,14 @@ gatherRewriteMeta(Query *parsetree,
8881 * will be in the SELECT part, and we have to modify that rather than
8982 * the top-level INSERT (kluge!).
9083 */
91- sub_action = getInsertSelectQuery (info -> rule_action , & sub_action_ptr );
84+ sub_action = getInsertSelectQuery (rule_action , & sub_action_ptr );
9285
9386 OffsetVarNodes ((Node * ) sub_action , rt_length , 0 );
94- OffsetVarNodes (info -> rule_qual , rt_length , 0 );
87+ OffsetVarNodes (rule_qual , rt_length , 0 );
9588 /* but references to *OLD* should point at original rt_index */
9689 ChangeVarNodes ((Node * ) sub_action ,
9790 PRS2_OLD_VARNO + rt_length , rt_index , 0 );
98- ChangeVarNodes (info -> rule_qual ,
91+ ChangeVarNodes (rule_qual ,
9992 PRS2_OLD_VARNO + rt_length , rt_index , 0 );
10093
10194 /*
@@ -135,8 +128,8 @@ gatherRewriteMeta(Query *parsetree,
135128
136129 keeporig = (!rangeTableEntry_used ((Node * ) sub_action -> jointree ,
137130 rt_index , 0 )) &&
138- (rangeTableEntry_used (info -> rule_qual , rt_index , 0 ) ||
139- rangeTableEntry_used (parsetree -> jointree -> quals , rt_index , 0 ));
131+ (rangeTableEntry_used (rule_qual , rt_index , 0 ) ||
132+ rangeTableEntry_used (parsetree -> jointree -> quals , rt_index , 0 ));
140133 newjointree = adjustJoinTreeList (parsetree , !keeporig , rt_index );
141134 sub_action -> jointree -> fromlist =
142135 nconc (newjointree , sub_action -> jointree -> fromlist );
@@ -146,7 +139,8 @@ gatherRewriteMeta(Query *parsetree,
146139 * We copy the qualifications of the parsetree to the action and vice
147140 * versa. So force hasSubLinks if one of them has it. If this is not
148141 * right, the flag will get cleared later, but we mustn't risk having
149- * it not set when it needs to be.
142+ * it not set when it needs to be. (XXX this should probably be handled
143+ * by AddQual and friends, not here...)
150144 */
151145 if (parsetree -> hasSubLinks )
152146 sub_action -> hasSubLinks = TRUE;
@@ -158,7 +152,7 @@ gatherRewriteMeta(Query *parsetree,
158152 * two queries one w/rule_qual, one w/NOT rule_qual. Also add user
159153 * query qual onto rule action
160154 */
161- AddQual (sub_action , info -> rule_qual );
155+ AddQual (sub_action , rule_qual );
162156
163157 AddQual (sub_action , parsetree -> jointree -> quals );
164158
@@ -168,23 +162,23 @@ gatherRewriteMeta(Query *parsetree,
168162 *
169163 * KLUGE ALERT: since ResolveNew returns a mutated copy, we can't just
170164 * apply it to sub_action; we have to remember to update the sublink
171- * inside info-> rule_action, too.
165+ * inside rule_action, too.
172166 */
173- if (info -> event == CMD_INSERT || info -> event == CMD_UPDATE )
167+ if (event == CMD_INSERT || event == CMD_UPDATE )
174168 {
175169 sub_action = (Query * ) ResolveNew ((Node * ) sub_action ,
176- info -> new_varno ,
170+ new_varno ,
177171 0 ,
178172 parsetree -> targetList ,
179- info -> event ,
180- info -> current_varno );
173+ event ,
174+ current_varno );
181175 if (sub_action_ptr )
182176 * sub_action_ptr = sub_action ;
183177 else
184- info -> rule_action = sub_action ;
178+ rule_action = sub_action ;
185179 }
186180
187- return info ;
181+ return rule_action ;
188182}
189183
190184/*
@@ -708,26 +702,14 @@ fireRules(Query *parsetree,
708702 foreach (r , actions )
709703 {
710704 Query * rule_action = lfirst (r );
711- RewriteInfo * info ;
712705
713706 if (rule_action -> commandType == CMD_NOTHING )
714707 continue ;
715708
716- info = gatherRewriteMeta (parsetree , rule_action , event_qual ,
717- rt_index , event , * instead_flag );
718-
719- /* handle escapable cases, or those handled by other code */
720- if (info -> nothing )
721- {
722- if (* instead_flag )
723- return NIL ;
724- else
725- continue ;
726- }
727-
728- results = lappend (results , info -> rule_action );
709+ rule_action = rewriteRuleAction (parsetree , rule_action ,
710+ event_qual , rt_index , event );
729711
730- pfree ( info );
712+ results = lappend ( results , rule_action );
731713 }
732714
733715 /*
0 commit comments