7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.71 2000/04/12 17:15:32 momjian Exp $
10
+ * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteHandler.c,v 1.72 2000/04/20 00:31:49 tgl Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -704,8 +704,7 @@ ApplyRetrieveRule(Query *parsetree,
704
704
int rt_index ,
705
705
int relation_level ,
706
706
Relation relation ,
707
- bool relWasInJoinSet ,
708
- int * modified )
707
+ bool relWasInJoinSet )
709
708
{
710
709
Query * rule_action = NULL ;
711
710
Node * rule_qual ;
@@ -714,6 +713,7 @@ ApplyRetrieveRule(Query *parsetree,
714
713
* l ;
715
714
int nothing ,
716
715
rt_length ;
716
+ int modified = false;
717
717
int badsql = false;
718
718
719
719
rule_qual = rule -> qual ;
@@ -809,18 +809,23 @@ ApplyRetrieveRule(Query *parsetree,
809
809
parsetree = (Query * ) apply_RIR_view ((Node * ) parsetree ,
810
810
rt_index , rte ,
811
811
rule_action -> targetList ,
812
- modified , 0 );
812
+ & modified , 0 );
813
813
rule_action = (Query * ) apply_RIR_view ((Node * ) rule_action ,
814
814
rt_index , rte ,
815
815
rule_action -> targetList ,
816
- modified , 0 );
816
+ & modified , 0 );
817
+ /* always apply quals of relation-level rules, whether we found a
818
+ * var to substitute or not.
819
+ */
820
+ modified = true;
817
821
}
818
822
else
819
823
{
820
824
HandleRIRAttributeRule (parsetree , rtable , rule_action -> targetList ,
821
- rt_index , rule -> attrno , modified , & badsql );
825
+ rt_index , rule -> attrno , & modified , & badsql );
826
+ /* quals will be inserted only if we found uses of the attribute */
822
827
}
823
- if (* modified && !badsql )
828
+ if (modified && !badsql )
824
829
{
825
830
AddQual (parsetree , rule_action -> qual );
826
831
AddGroupClause (parsetree , rule_action -> groupClause ,
@@ -894,7 +899,6 @@ fireRIRrules(Query *parsetree)
894
899
RewriteRule * rule ;
895
900
RewriteRule RIRonly ;
896
901
bool relWasInJoinSet ;
897
- int modified = false;
898
902
int i ;
899
903
List * l ;
900
904
@@ -910,18 +914,15 @@ fireRIRrules(Query *parsetree)
910
914
rte = rt_fetch (rt_index , parsetree -> rtable );
911
915
912
916
/*
913
- * If the table is not one named in the original FROM clause then
914
- * it must be referenced in the query, or we ignore it. This
915
- * prevents infinite expansion loop due to new rtable entries
916
- * inserted by expansion of a rule.
917
+ * If the table is not referenced in the query, then we ignore it.
918
+ * This prevents infinite expansion loop due to new rtable entries
919
+ * inserted by expansion of a rule. A table is referenced if it is
920
+ * part of the join set (a source table), or is the result table,
921
+ * or is referenced by any Var nodes.
917
922
*/
918
- if (!rte -> inFromCl && rt_index != parsetree -> resultRelation &&
923
+ if (!rte -> inJoinSet && rt_index != parsetree -> resultRelation &&
919
924
!rangeTableEntry_used ((Node * ) parsetree , rt_index , 0 ))
920
- {
921
- /* Make sure the planner ignores it too... */
922
- rte -> inJoinSet = false;
923
925
continue ;
924
- }
925
926
926
927
rel = heap_openr (rte -> relname , AccessShareLock );
927
928
rules = rel -> rd_rules ;
@@ -989,8 +990,7 @@ fireRIRrules(Query *parsetree)
989
990
rt_index ,
990
991
RIRonly .attrno == -1 ,
991
992
rel ,
992
- relWasInJoinSet ,
993
- & modified );
993
+ relWasInJoinSet );
994
994
}
995
995
996
996
heap_close (rel , AccessShareLock );
0 commit comments