@@ -65,6 +65,7 @@ static ParseNamespaceItem *scanNameSpaceForRelid(ParseState *pstate, Oid relid,
6565static void check_lateral_ref_ok (ParseState * pstate ,ParseNamespaceItem * nsitem ,
6666int location );
6767static int scanRTEForColumn (ParseState * pstate ,RangeTblEntry * rte ,
68+ Alias * eref ,
6869const char * colname ,int location ,
6970int fuzzy_rte_penalty ,
7071FuzzyAttrMatchState * fuzzystate );
@@ -184,7 +185,6 @@ scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)
184185foreach (l ,pstate -> p_namespace )
185186{
186187ParseNamespaceItem * nsitem = (ParseNamespaceItem * )lfirst (l );
187- RangeTblEntry * rte = nsitem -> p_rte ;
188188
189189/* Ignore columns-only items */
190190if (!nsitem -> p_rel_visible )
@@ -193,7 +193,7 @@ scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)
193193if (nsitem -> p_lateral_only && !pstate -> p_lateral_active )
194194continue ;
195195
196- if (strcmp (rte -> eref -> aliasname ,refname )== 0 )
196+ if (strcmp (nsitem -> p_names -> aliasname ,refname )== 0 )
197197{
198198if (result )
199199ereport (ERROR ,
@@ -420,7 +420,7 @@ checkNameSpaceConflicts(ParseState *pstate, List *namespace1,
420420{
421421ParseNamespaceItem * nsitem1 = (ParseNamespaceItem * )lfirst (l1 );
422422RangeTblEntry * rte1 = nsitem1 -> p_rte ;
423- const char * aliasname1 = rte1 -> eref -> aliasname ;
423+ const char * aliasname1 = nsitem1 -> p_names -> aliasname ;
424424ListCell * l2 ;
425425
426426if (!nsitem1 -> p_rel_visible )
@@ -430,10 +430,11 @@ checkNameSpaceConflicts(ParseState *pstate, List *namespace1,
430430{
431431ParseNamespaceItem * nsitem2 = (ParseNamespaceItem * )lfirst (l2 );
432432RangeTblEntry * rte2 = nsitem2 -> p_rte ;
433+ const char * aliasname2 = nsitem2 -> p_names -> aliasname ;
433434
434435if (!nsitem2 -> p_rel_visible )
435436continue ;
436- if (strcmp (rte2 -> eref -> aliasname ,aliasname1 )!= 0 )
437+ if (strcmp (aliasname2 ,aliasname1 )!= 0 )
437438continue ;/* definitely no conflict */
438439if (rte1 -> rtekind == RTE_RELATION && rte1 -> alias == NULL &&
439440rte2 -> rtekind == RTE_RELATION && rte2 -> alias == NULL &&
@@ -466,7 +467,7 @@ check_lateral_ref_ok(ParseState *pstate, ParseNamespaceItem *nsitem,
466467{
467468/* SQL:2008 demands this be an error, not an invisible item */
468469RangeTblEntry * rte = nsitem -> p_rte ;
469- char * refname = rte -> eref -> aliasname ;
470+ char * refname = nsitem -> p_names -> aliasname ;
470471
471472ereport (ERROR ,
472473(errcode (ERRCODE_INVALID_COLUMN_REFERENCE ),
@@ -672,10 +673,10 @@ scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem,
672673Var * var ;
673674
674675/*
675- * Scan theRTE 's column names (or aliases) for a match. Complain if
676+ * Scan thensitem 's column names (or aliases) for a match. Complain if
676677 * multiple matches.
677678 */
678- attnum = scanRTEForColumn (pstate ,rte ,
679+ attnum = scanRTEForColumn (pstate ,rte ,nsitem -> p_names ,
679680colname ,location ,
6806810 ,NULL );
681682
@@ -712,7 +713,7 @@ scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem,
712713(errcode (ERRCODE_UNDEFINED_COLUMN ),
713714errmsg ("column \"%s\" of relation \"%s\" does not exist" ,
714715colname ,
715- rte -> eref -> aliasname )));
716+ nsitem -> p_names -> aliasname )));
716717
717718var = makeVar (nscol -> p_varno ,
718719nscol -> p_varattno ,
@@ -765,6 +766,7 @@ scanNSItemForColumn(ParseState *pstate, ParseNamespaceItem *nsitem,
765766 */
766767static int
767768scanRTEForColumn (ParseState * pstate ,RangeTblEntry * rte ,
769+ Alias * eref ,
768770const char * colname ,int location ,
769771int fuzzy_rte_penalty ,
770772FuzzyAttrMatchState * fuzzystate )
@@ -786,7 +788,7 @@ scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte,
786788 * Callers interested in finding match with shortest distance need to
787789 * defend against this directly, though.
788790 */
789- foreach (c ,rte -> eref -> colnames )
791+ foreach (c ,eref -> colnames )
790792{
791793const char * attcolname = strVal (lfirst (c ));
792794
@@ -970,7 +972,7 @@ searchRangeTableForCol(ParseState *pstate, const char *alias, const char *colnam
970972 * Scan for a matching column; if we find an exact match, we're
971973 * done. Otherwise, update fuzzystate.
972974 */
973- if (scanRTEForColumn (orig_pstate ,rte ,colname ,location ,
975+ if (scanRTEForColumn (orig_pstate ,rte ,rte -> eref , colname ,location ,
974976fuzzy_rte_penalty ,fuzzystate )
975977&& fuzzy_rte_penalty == 0 )
976978{
@@ -1252,6 +1254,7 @@ buildNSItemFromTupleDesc(RangeTblEntry *rte, Index rtindex, TupleDesc tupdesc)
12521254
12531255/* ... and build the nsitem */
12541256nsitem = (ParseNamespaceItem * )palloc (sizeof (ParseNamespaceItem ));
1257+ nsitem -> p_names = rte -> eref ;
12551258nsitem -> p_rte = rte ;
12561259nsitem -> p_rtindex = rtindex ;
12571260nsitem -> p_nscolumns = nscolumns ;
@@ -1313,6 +1316,7 @@ buildNSItemFromLists(RangeTblEntry *rte, Index rtindex,
13131316
13141317/* ... and build the nsitem */
13151318nsitem = (ParseNamespaceItem * )palloc (sizeof (ParseNamespaceItem ));
1319+ nsitem -> p_names = rte -> eref ;
13161320nsitem -> p_rte = rte ;
13171321nsitem -> p_rtindex = rtindex ;
13181322nsitem -> p_nscolumns = nscolumns ;
@@ -2198,6 +2202,7 @@ addRangeTableEntryForJoin(ParseState *pstate,
21982202 * list --- caller must do that if appropriate.
21992203 */
22002204nsitem = (ParseNamespaceItem * )palloc (sizeof (ParseNamespaceItem ));
2205+ nsitem -> p_names = rte -> eref ;
22012206nsitem -> p_rte = rte ;
22022207nsitem -> p_rtindex = list_length (pstate -> p_rtable );
22032208nsitem -> p_nscolumns = nscolumns ;
@@ -2356,7 +2361,7 @@ addRangeTableEntryForCTE(ParseState *pstate,
23562361 */
23572362if (rte -> ctelevelsup > 0 )
23582363for (int i = 0 ;i < n_dontexpand_columns ;i ++ )
2359- psi -> p_nscolumns [list_length (psi -> p_rte -> eref -> colnames )- 1 - i ].p_dontexpand = true;
2364+ psi -> p_nscolumns [list_length (psi -> p_names -> colnames )- 1 - i ].p_dontexpand = true;
23602365
23612366return psi ;
23622367}
@@ -3037,7 +3042,7 @@ expandNSItemVars(ParseNamespaceItem *nsitem,
30373042if (colnames )
30383043* colnames = NIL ;
30393044colindex = 0 ;
3040- foreach (lc ,nsitem -> p_rte -> eref -> colnames )
3045+ foreach (lc ,nsitem -> p_names -> colnames )
30413046{
30423047Value * colnameval = (Value * )lfirst (lc );
30433048const char * colname = strVal (colnameval );