Commit 524f4b2d authored by Bruce Momjian's avatar Bruce Momjian

The patch does 2 things:

        Fixes  a  bug  in  the rule system that caused a crashing
        backend when a join-view with calculated column  is  used
        in subselect.

        Modifies  EXPLAIN to explain rewritten queries instead of
        the plain SeqScan on a view. Rules can produce very  deep
MORE

Jan.
parent 858a3b57
......@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.24 1998/09/01 04:27:53 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.25 1998/10/21 16:21:20 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -26,6 +26,7 @@
#include <optimizer/planner.h>
#include <access/xact.h>
#include <utils/relcache.h>
#include <rewrite/rewriteHandler.h>
typedef struct ExplainState
{
......@@ -37,6 +38,8 @@ typedef struct ExplainState
} ExplainState;
static char *Explain_PlanToString(Plan *plan, ExplainState *es);
static void ExplainOneQuery(Query *query, bool verbose, CommandDest dest);
/*
* ExplainQuery -
......@@ -46,11 +49,8 @@ static char *Explain_PlanToString(Plan *plan, ExplainState *es);
void
ExplainQuery(Query *query, bool verbose, CommandDest dest)
{
char *s = NULL,
*s2;
Plan *plan;
ExplainState *es;
int len;
List *rewritten;
List *l;
if (IsAbortedTransactionBlockState())
{
......@@ -64,6 +64,35 @@ ExplainQuery(Query *query, bool verbose, CommandDest dest)
return;
}
/* Rewrite through rule system */
rewritten = QueryRewrite(query);
/* In the case of an INSTEAD NOTHING, tell at least that */
if (rewritten == NIL)
{
elog(NOTICE, "query rewrites to nothing");
return;
}
/* Explain every plan */
foreach(l, rewritten)
ExplainOneQuery(lfirst(l), verbose, dest);
}
/*
* ExplainOneQuery -
* print out the execution plan for one query
*
*/
static void
ExplainOneQuery(Query *query, bool verbose, CommandDest dest)
{
char *s = NULL,
*s2;
Plan *plan;
ExplainState *es;
int len;
/* plan the queries (XXX we've ignored rewrite!!) */
plan = planner(query);
......@@ -202,8 +231,13 @@ explain_outNode(StringInfo str, Plan *plan, int indent, ExplainState *es)
{
RangeTblEntry *rte = nth(((Scan *) plan)->scanrelid - 1, es->rtable);
sprintf(buf, " on %s", rte->refname);
appendStringInfo(str, buf);
appendStringInfo(str, " on ");
if (strcmp(rte->refname, rte->relname) != 0)
{
sprintf(buf, "%s ", rte->relname);
appendStringInfo(str, buf);
}
appendStringInfo(str, rte->refname);
}
break;
default:
......@@ -232,7 +266,7 @@ explain_outNode(StringInfo str, Plan *plan, int indent, ExplainState *es)
for (i = 0; i < indent; i++)
appendStringInfo(str, " ");
appendStringInfo(str, " -> ");
explain_outNode(str, ((SubPlan *) lfirst(lst))->plan, indent + 4, es);
explain_outNode(str, ((SubPlan *) lfirst(lst))->plan, indent + 2, es);
}
es->rtable = saved_rtable;
}
......
......@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/recipe.c,v 1.24 1998/09/01 04:27:56 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/recipe.c,v 1.25 1998/10/21 16:21:21 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -350,8 +350,8 @@ tg_rewriteQuery(TgRecipe * r,
* need to offset the var nodes in the qual and targetlist
* because they are indexed off the original rtable
*/
OffsetVarNodes((Node *) inputQ->qual, rt_length);
OffsetVarNodes((Node *) inputQ->targetList, rt_length);
OffsetVarNodes((Node *) inputQ->qual, rt_length, 0);
OffsetVarNodes((Node *) inputQ->targetList, rt_length, 0);
/* append the range tables from the children nodes */
rtable = nconc(rtable, input_rtable);
......
......@@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/view.c,v 1.25 1998/09/01 04:28:10 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/commands/view.c,v 1.26 1998/10/21 16:21:22 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -219,10 +219,10 @@ UpdateRangeTableOfViewParse(char *viewName, Query *viewParse)
/*
* first offset all var nodes by 2
*/
OffsetVarNodes((Node *) viewParse->targetList, 2);
OffsetVarNodes(viewParse->qual, 2);
OffsetVarNodes((Node *) viewParse->targetList, 2, 0);
OffsetVarNodes(viewParse->qual, 2, 0);
OffsetVarNodes(viewParse->havingQual, 2);
OffsetVarNodes(viewParse->havingQual, 2, 0);
/*
......
This diff is collapsed.
This diff is collapsed.
......@@ -6,7 +6,7 @@
*
* Copyright (c) 1994, Regents of the University of California
*
* $Id: rewriteManip.h,v 1.10 1998/10/08 18:30:41 momjian Exp $
* $Id: rewriteManip.h,v 1.11 1998/10/21 16:21:29 momjian Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -18,7 +18,7 @@
#include "rewrite/rewriteHandler.h"
/* RewriteManip.c */
void OffsetVarNodes(Node *node, int offset);
void OffsetVarNodes(Node *node, int offset, int sublevels_up);
void ChangeVarNodes(Node *node, int old_varno, int new_varno,
int sublevels_up);
void AddQual(Query *parsetree, Node *qual);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment