Skip to content

Commit e64bb65

Browse files
committed
Fix GetCTEForRTE() to deal with the possibility that the RTE it's given came
from a query level above the current ParseState.
1 parent 5f853c6 commit e64bb65

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

src/backend/parser/analyze.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
1818
* Portions Copyright (c) 1994, Regents of the University of California
1919
*
20-
* $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.380 2008/10/04 21:56:54 tgl Exp $
20+
* $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.381 2008/10/06 15:15:22 tgl Exp $
2121
*
2222
*-------------------------------------------------------------------------
2323
*/
@@ -1896,7 +1896,7 @@ transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc)
18961896
ereport(ERROR,
18971897
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
18981898
errmsg("SELECT FOR UPDATE/SHARE cannot be applied to an outer-level WITH query")));
1899-
cte = GetCTEForRTE(pstate, rte);
1899+
cte = GetCTEForRTE(pstate, rte, -1);
19001900
/* should be analyzed by now */
19011901
Assert(IsA(cte->ctequery, Query));
19021902
transformLockingClause(pstate,
@@ -1989,7 +1989,7 @@ transformLockingClause(ParseState *pstate, Query *qry, LockingClause *lc)
19891989
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
19901990
errmsg("SELECT FOR UPDATE/SHARE cannot be applied to an outer-level WITH query"),
19911991
parser_errposition(pstate, thisrel->location)));
1992-
cte = GetCTEForRTE(pstate, rte);
1992+
cte = GetCTEForRTE(pstate, rte, -1);
19931993
/* should be analyzed by now */
19941994
Assert(IsA(cte->ctequery, Query));
19951995
transformLockingClause(pstate,

src/backend/parser/parse_relation.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/parser/parse_relation.c,v 1.137 2008/10/06 02:12:56 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/parser/parse_relation.c,v 1.138 2008/10/06 15:15:22 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -371,15 +371,23 @@ GetRTEByRangeTablePosn(ParseState *pstate,
371371

372372
/*
373373
* Fetch the CTE for a CTE-reference RTE.
374+
*
375+
* rtelevelsup is the number of query levels above the given pstate that the
376+
* RTE came from. Callers that don't have this information readily available
377+
* may pass -1 instead.
374378
*/
375379
CommonTableExpr *
376-
GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte)
380+
GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte, int rtelevelsup)
377381
{
378382
Index levelsup;
379383
ListCell *lc;
380384

385+
/* Determine RTE's levelsup if caller didn't know it */
386+
if (rtelevelsup < 0)
387+
(void) RTERangeTablePosn(pstate, rte, &rtelevelsup);
388+
381389
Assert(rte->rtekind == RTE_CTE);
382-
levelsup = rte->ctelevelsup;
390+
levelsup = rte->ctelevelsup + rtelevelsup;
383391
while (levelsup-- > 0)
384392
{
385393
pstate = pstate->parentParseState;

src/backend/parser/parse_target.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/parser/parse_target.c,v 1.166 2008/10/05 22:20:16 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/parser/parse_target.c,v 1.167 2008/10/06 15:15:22 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -308,7 +308,7 @@ markTargetListOrigin(ParseState *pstate, TargetEntry *tle,
308308
*/
309309
if (attnum != InvalidAttrNumber && !rte->self_reference)
310310
{
311-
CommonTableExpr *cte = GetCTEForRTE(pstate, rte);
311+
CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
312312
TargetEntry *ste;
313313

314314
/* should be analyzed by now */
@@ -1206,7 +1206,7 @@ expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
12061206
/* CTE reference: examine subquery's output expr */
12071207
if (!rte->self_reference)
12081208
{
1209-
CommonTableExpr *cte = GetCTEForRTE(pstate, rte);
1209+
CommonTableExpr *cte = GetCTEForRTE(pstate, rte, netlevelsup);
12101210
TargetEntry *ste;
12111211

12121212
/* should be analyzed by now */
@@ -1230,7 +1230,9 @@ expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
12301230

12311231
MemSet(&mypstate, 0, sizeof(mypstate));
12321232
/* this loop must work, since GetCTEForRTE did */
1233-
for (levelsup = 0; levelsup < rte->ctelevelsup; levelsup++)
1233+
for (levelsup = 0;
1234+
levelsup < rte->ctelevelsup + netlevelsup;
1235+
levelsup++)
12341236
pstate = pstate->parentParseState;
12351237
mypstate.parentParseState = pstate;
12361238
mypstate.p_rtable = ((Query *) cte->ctequery)->rtable;

src/include/parser/parse_relation.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/parser/parse_relation.h,v 1.60 2008/10/06 02:12:56 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/parser/parse_relation.h,v 1.61 2008/10/06 15:15:22 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -34,7 +34,8 @@ extern int RTERangeTablePosn(ParseState *pstate,
3434
extern RangeTblEntry *GetRTEByRangeTablePosn(ParseState *pstate,
3535
int varno,
3636
int sublevels_up);
37-
extern CommonTableExpr *GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte);
37+
extern CommonTableExpr *GetCTEForRTE(ParseState *pstate, RangeTblEntry *rte,
38+
int rtelevelsup);
3839
extern Node *scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte,
3940
char *colname, int location);
4041
extern Node *colNameToVar(ParseState *pstate, char *colname, bool localonly,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy