99 *
1010 *
1111 * IDENTIFICATION
12- * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.257 2007/03/27 23:21:10 tgl Exp $
12+ * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.258 2007/05/24 18:58:42 tgl Exp $
1313 *
1414 *-------------------------------------------------------------------------
1515 */
@@ -2598,20 +2598,14 @@ get_variable(Var *var, int levelsup, bool showstar, deparse_context *context)
25982598
25992599/*
26002600 * Try to find the relevant RTE in this rtable. In a plan tree, it's
2601- * likely that varno is OUTER or INNER, in which case we try to use
2602- * varnoold instead. If the Var references an expression computed by a
2603- * subplan, varnoold will be 0, and we must dig down into the subplans.
2601+ * likely that varno is OUTER or INNER, in which case we must dig down
2602+ * into the subplans.
26042603 */
26052604if (var -> varno >=1 && var -> varno <=list_length (dpns -> rtable ))
26062605{
26072606rte = rt_fetch (var -> varno ,dpns -> rtable );
26082607attnum = var -> varattno ;
26092608}
2610- else if (var -> varnoold >=1 && var -> varnoold <=list_length (dpns -> rtable ))
2611- {
2612- rte = rt_fetch (var -> varnoold ,dpns -> rtable );
2613- attnum = var -> varoattno ;
2614- }
26152609else if (var -> varno == OUTER && dpns -> outer_plan )
26162610{
26172611TargetEntry * tle ;
@@ -2631,9 +2625,11 @@ get_variable(Var *var, int levelsup, bool showstar, deparse_context *context)
26312625 * Force parentheses because our caller probably assumed a Var is a
26322626 * simple expression.
26332627 */
2634- appendStringInfoChar (buf ,'(' );
2628+ if (!IsA (tle -> expr ,Var ))
2629+ appendStringInfoChar (buf ,'(' );
26352630get_rule_expr ((Node * )tle -> expr ,context , true);
2636- appendStringInfoChar (buf ,')' );
2631+ if (!IsA (tle -> expr ,Var ))
2632+ appendStringInfoChar (buf ,')' );
26372633
26382634dpns -> outer_plan = save_outer ;
26392635dpns -> inner_plan = save_inner ;
@@ -2658,9 +2654,11 @@ get_variable(Var *var, int levelsup, bool showstar, deparse_context *context)
26582654 * Force parentheses because our caller probably assumed a Var is a
26592655 * simple expression.
26602656 */
2661- appendStringInfoChar (buf ,'(' );
2657+ if (!IsA (tle -> expr ,Var ))
2658+ appendStringInfoChar (buf ,'(' );
26622659get_rule_expr ((Node * )tle -> expr ,context , true);
2663- appendStringInfoChar (buf ,')' );
2660+ if (!IsA (tle -> expr ,Var ))
2661+ appendStringInfoChar (buf ,')' );
26642662
26652663dpns -> outer_plan = save_outer ;
26662664dpns -> inner_plan = save_inner ;
@@ -2700,7 +2698,13 @@ get_variable(Var *var, int levelsup, bool showstar, deparse_context *context)
27002698 * simple reference, we have to just print the unqualified
27012699 * variable name (this can only happen with columns that were
27022700 * merged by USING or NATURAL clauses).
2701+ *
2702+ * This wouldn't work in decompiling plan trees, because we don't
2703+ * store joinaliasvars lists after planning; but a plan tree
2704+ * should never contain a join alias variable.
27032705 */
2706+ if (rte -> joinaliasvars == NIL )
2707+ elog (ERROR ,"cannot decompile join alias var in plan tree" );
27042708if (attnum > 0 )
27052709{
27062710Var * aliasvar ;
@@ -2798,9 +2802,7 @@ get_name_for_var_field(Var *var, int fieldno,
27982802/*
27992803 * Try to find the relevant RTE in this rtable. In a plan tree, it's
28002804 * likely that varno is OUTER or INNER, in which case we must dig down
2801- * into the subplans. (We can't shortcut with varnoold here, because
2802- * it might reference a SUBQUERY RTE; we have to dig down to the
2803- * SubqueryScan plan level to cope with that. See below.)
2805+ * into the subplans.
28042806 */
28052807if (var -> varno >=1 && var -> varno <=list_length (dpns -> rtable ))
28062808{
@@ -2963,6 +2965,8 @@ get_name_for_var_field(Var *var, int fieldno,
29632965break ;
29642966case RTE_JOIN :
29652967/* Join RTE --- recursively inspect the alias variable */
2968+ if (rte -> joinaliasvars == NIL )
2969+ elog (ERROR ,"cannot decompile join alias var in plan tree" );
29662970Assert (attnum > 0 && attnum <=list_length (rte -> joinaliasvars ));
29672971expr = (Node * )list_nth (rte -> joinaliasvars ,attnum - 1 );
29682972if (IsA (expr ,Var ))