Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 524f4b2

Browse files
committed
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.
1 parent 858a3b5 commit 524f4b2

File tree

6 files changed

+442
-495
lines changed

6 files changed

+442
-495
lines changed

src/backend/commands/explain.c

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.24 1998/09/01 04:27:53 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/explain.c,v 1.25 1998/10/21 16:21:20 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -26,6 +26,7 @@
2626
#include <optimizer/planner.h>
2727
#include <access/xact.h>
2828
#include <utils/relcache.h>
29+
#include <rewrite/rewriteHandler.h>
2930

3031
typedef struct ExplainState
3132
{
@@ -37,6 +38,8 @@ typedef struct ExplainState
3738
} ExplainState;
3839

3940
static char *Explain_PlanToString(Plan *plan, ExplainState *es);
41+
static void ExplainOneQuery(Query *query, bool verbose, CommandDest dest);
42+
4043

4144
/*
4245
* ExplainQuery -
@@ -46,11 +49,8 @@ static char *Explain_PlanToString(Plan *plan, ExplainState *es);
4649
void
4750
ExplainQuery(Query *query, bool verbose, CommandDest dest)
4851
{
49-
char *s = NULL,
50-
*s2;
51-
Plan *plan;
52-
ExplainState *es;
53-
int len;
52+
List *rewritten;
53+
List *l;
5454

5555
if (IsAbortedTransactionBlockState())
5656
{
@@ -64,6 +64,35 @@ ExplainQuery(Query *query, bool verbose, CommandDest dest)
6464
return;
6565
}
6666

67+
/* Rewrite through rule system */
68+
rewritten = QueryRewrite(query);
69+
70+
/* In the case of an INSTEAD NOTHING, tell at least that */
71+
if (rewritten == NIL)
72+
{
73+
elog(NOTICE, "query rewrites to nothing");
74+
return;
75+
}
76+
77+
/* Explain every plan */
78+
foreach(l, rewritten)
79+
ExplainOneQuery(lfirst(l), verbose, dest);
80+
}
81+
82+
/*
83+
* ExplainOneQuery -
84+
* print out the execution plan for one query
85+
*
86+
*/
87+
static void
88+
ExplainOneQuery(Query *query, bool verbose, CommandDest dest)
89+
{
90+
char *s = NULL,
91+
*s2;
92+
Plan *plan;
93+
ExplainState *es;
94+
int len;
95+
6796
/* plan the queries (XXX we've ignored rewrite!!) */
6897
plan = planner(query);
6998

@@ -202,8 +231,13 @@ explain_outNode(StringInfo str, Plan *plan, int indent, ExplainState *es)
202231
{
203232
RangeTblEntry *rte = nth(((Scan *) plan)->scanrelid - 1, es->rtable);
204233

205-
sprintf(buf, " on %s", rte->refname);
206-
appendStringInfo(str, buf);
234+
appendStringInfo(str, " on ");
235+
if (strcmp(rte->refname, rte->relname) != 0)
236+
{
237+
sprintf(buf, "%s ", rte->relname);
238+
appendStringInfo(str, buf);
239+
}
240+
appendStringInfo(str, rte->refname);
207241
}
208242
break;
209243
default:
@@ -232,7 +266,7 @@ explain_outNode(StringInfo str, Plan *plan, int indent, ExplainState *es)
232266
for (i = 0; i < indent; i++)
233267
appendStringInfo(str, " ");
234268
appendStringInfo(str, " -> ");
235-
explain_outNode(str, ((SubPlan *) lfirst(lst))->plan, indent + 4, es);
269+
explain_outNode(str, ((SubPlan *) lfirst(lst))->plan, indent + 2, es);
236270
}
237271
es->rtable = saved_rtable;
238272
}

src/backend/commands/recipe.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/recipe.c,v 1.24 1998/09/01 04:27:56 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/Attic/recipe.c,v 1.25 1998/10/21 16:21:21 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -350,8 +350,8 @@ tg_rewriteQuery(TgRecipe * r,
350350
* need to offset the var nodes in the qual and targetlist
351351
* because they are indexed off the original rtable
352352
*/
353-
OffsetVarNodes((Node *) inputQ->qual, rt_length);
354-
OffsetVarNodes((Node *) inputQ->targetList, rt_length);
353+
OffsetVarNodes((Node *) inputQ->qual, rt_length, 0);
354+
OffsetVarNodes((Node *) inputQ->targetList, rt_length, 0);
355355

356356
/* append the range tables from the children nodes */
357357
rtable = nconc(rtable, input_rtable);

src/backend/commands/view.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/view.c,v 1.25 1998/09/01 04:28:10 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/view.c,v 1.26 1998/10/21 16:21:22 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -219,10 +219,10 @@ UpdateRangeTableOfViewParse(char *viewName, Query *viewParse)
219219
/*
220220
* first offset all var nodes by 2
221221
*/
222-
OffsetVarNodes((Node *) viewParse->targetList, 2);
223-
OffsetVarNodes(viewParse->qual, 2);
222+
OffsetVarNodes((Node *) viewParse->targetList, 2, 0);
223+
OffsetVarNodes(viewParse->qual, 2, 0);
224224

225-
OffsetVarNodes(viewParse->havingQual, 2);
225+
OffsetVarNodes(viewParse->havingQual, 2, 0);
226226

227227

228228
/*

0 commit comments

Comments
 (0)