Check for out-of-range varoattno in deparse_context_for_subplan.
authorTom Lane <[email protected]>
Fri, 15 Jul 2005 18:40:20 +0000 (18:40 +0000)
committerTom Lane <[email protected]>
Fri, 15 Jul 2005 18:40:20 +0000 (18:40 +0000)
I have seen this case in CVS tip due to new "physical tlist" optimization
for subqueries.  I believe it probably can't happen in existing releases,
but the check is not going to hurt anything, so backpatch to 8.0 just
in case.

src/backend/utils/adt/ruleutils.c

index 5b0b990130579fdb0ef143be1d2978f5711324c3..f35991903615dd34782a4324c0b5d0145ba55956 100644 (file)
@@ -723,7 +723,7 @@ pg_get_indexdef_worker(Oid indexrelid, int colno, int prettyFlags)
                AttrNumber      attnum = idxrec->indkey[keyno];
 
                if (!colno)
-                       appendStringInfo(&buf, sep);
+                       appendStringInfoString(&buf, sep);
                sep = ", ";
 
                if (attnum != 0)
@@ -1500,11 +1500,17 @@ deparse_context_for_subplan(const char *name, List *tlist,
                        if (var->varnoold > 0 && var->varnoold <= rtablelength)
                        {
                                RangeTblEntry *varrte = rt_fetch(var->varnoold, rtable);
-                               char       *varname;
+                               AttrNumber varattnum = var->varoattno;
 
-                               varname = get_rte_attribute_name(varrte, var->varoattno);
-                               attrs = lappend(attrs, makeString(varname));
-                               continue;
+                               /* need this test in case it's referencing a resjunk col */
+                               if (varattnum <= list_length(varrte->eref->colnames))
+                               {
+                                       char       *varname;
+
+                                       varname = get_rte_attribute_name(varrte, varattnum);
+                                       attrs = lappend(attrs, makeString(varname));
+                                       continue;
+                               }
                        }
                }
                /* Fallback if can't get name */
@@ -1876,7 +1882,7 @@ get_select_query_def(Query *query, deparse_context *context,
                        Oid                     sortcoltype;
                        TypeCacheEntry *typentry;
 
-                       appendStringInfo(buf, sep);
+                       appendStringInfoString(buf, sep);
                        sortexpr = get_rule_sortgroupclause(srt, query->targetList,
                                                                                                force_colno, context);
                        sortcoltype = exprType(sortexpr);
@@ -1945,7 +1951,7 @@ get_basic_select_query(Query *query, deparse_context *context,
                        {
                                SortClause *srt = (SortClause *) lfirst(l);
 
-                               appendStringInfo(buf, sep);
+                               appendStringInfoString(buf, sep);
                                get_rule_sortgroupclause(srt, query->targetList,
                                                                                 false, context);
                                sep = ", ";
@@ -1967,7 +1973,7 @@ get_basic_select_query(Query *query, deparse_context *context,
                if (tle->resdom->resjunk)
                        continue;                       /* ignore junk entries */
 
-               appendStringInfo(buf, sep);
+               appendStringInfoString(buf, sep);
                sep = ", ";
                colno++;
 
@@ -2031,7 +2037,7 @@ get_basic_select_query(Query *query, deparse_context *context,
                {
                        GroupClause *grp = (GroupClause *) lfirst(l);
 
-                       appendStringInfo(buf, sep);
+                       appendStringInfoString(buf, sep);
                        get_rule_sortgroupclause(grp, query->targetList,
                                                                         false, context);
                        sep = ", ";
@@ -2220,7 +2226,7 @@ get_insert_query_def(Query *query, deparse_context *context)
                if (tle->resdom->resjunk)
                        continue;                       /* ignore junk entries */
 
-               appendStringInfo(buf, sep);
+               appendStringInfoString(buf, sep);
                sep = ", ";
 
                /*
@@ -2292,7 +2298,7 @@ get_update_query_def(Query *query, deparse_context *context)
                if (tle->resdom->resjunk)
                        continue;                       /* ignore junk entries */
 
-               appendStringInfo(buf, sep);
+               appendStringInfoString(buf, sep);
                sep = ", ";
 
                /*
@@ -3256,7 +3262,7 @@ get_rule_expr(Node *node, deparse_context *context,
                                        if (tupdesc == NULL ||
                                                !tupdesc->attrs[i]->attisdropped)
                                        {
-                                               appendStringInfo(buf, sep);
+                                               appendStringInfoString(buf, sep);
                                                get_rule_expr(e, context, true);
                                                sep = ", ";
                                        }
@@ -3268,7 +3274,7 @@ get_rule_expr(Node *node, deparse_context *context,
                                        {
                                                if (!tupdesc->attrs[i]->attisdropped)
                                                {
-                                                       appendStringInfo(buf, sep);
+                                                       appendStringInfoString(buf, sep);
                                                        appendStringInfo(buf, "NULL");
                                                        sep = ", ";
                                                }
@@ -3403,7 +3409,7 @@ get_rule_expr(Node *node, deparse_context *context,
                                sep = "";
                                foreach(l, (List *) node)
                                {
-                                       appendStringInfo(buf, sep);
+                                       appendStringInfoString(buf, sep);
                                        get_rule_expr((Node *) lfirst(l), context, showimplicit);
                                        sep = ", ";
                                }