66 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
77 * Portions Copyright (c) 1994, Regents of the University of California
88 *
9- *$PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.321 2005/04/28 21:47:14 tgl Exp $
9+ *$PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.322 2005/06/05 00:38:09 tgl Exp $
1010 *
1111 *-------------------------------------------------------------------------
1212 */
@@ -512,7 +512,8 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt,
512512Query * qry = makeNode (Query );
513513Query * selectQuery = NULL ;
514514List * sub_rtable ;
515- List * sub_namespace ;
515+ List * sub_relnamespace ;
516+ List * sub_varnamespace ;
516517List * icolumns ;
517518List * attrnos ;
518519ListCell * icols ;
@@ -528,20 +529,23 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt,
528529 * SELECT.This can only happen if we are inside a CREATE RULE, and
529530 * in that case we want the rule's OLD and NEW rtable entries to
530531 * appear as part of the SELECT's rtable, not as outer references for
531- * it. (Kluge!) The SELECT's joinlist is not affected however. We
532+ * it. (Kluge!) The SELECT's joinlist is not affected however. We
532533 * must do this before adding the target table to the INSERT's rtable.
533534 */
534535if (stmt -> selectStmt )
535536{
536537sub_rtable = pstate -> p_rtable ;
537538pstate -> p_rtable = NIL ;
538- sub_namespace = pstate -> p_namespace ;
539- pstate -> p_namespace = NIL ;
539+ sub_relnamespace = pstate -> p_relnamespace ;
540+ pstate -> p_relnamespace = NIL ;
541+ sub_varnamespace = pstate -> p_varnamespace ;
542+ pstate -> p_varnamespace = NIL ;
540543}
541544else
542545{
543546sub_rtable = NIL ;/* not used, but keep compiler quiet */
544- sub_namespace = NIL ;
547+ sub_relnamespace = NIL ;
548+ sub_varnamespace = NIL ;
545549}
546550
547551/*
@@ -578,7 +582,8 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt,
578582 * through 6.5 had bugs of just that nature...)
579583 */
580584sub_pstate -> p_rtable = sub_rtable ;
581- sub_pstate -> p_namespace = sub_namespace ;
585+ sub_pstate -> p_relnamespace = sub_relnamespace ;
586+ sub_pstate -> p_varnamespace = sub_varnamespace ;
582587
583588/*
584589 * Note: we are not expecting that extras_before and extras_after
@@ -605,7 +610,7 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt,
605610rte = addRangeTableEntryForSubquery (pstate ,
606611selectQuery ,
607612makeAlias ("*SELECT*" ,NIL ),
608- true );
613+ false );
609614rtr = makeNode (RangeTblRef );
610615/* assume new rte is at end */
611616rtr -> rtindex = list_length (pstate -> p_rtable );
@@ -1481,8 +1486,8 @@ transformIndexStmt(ParseState *pstate, IndexStmt *stmt)
14811486 */
14821487rte = addRangeTableEntry (pstate ,stmt -> relation ,NULL , false, true);
14831488
1484- /* no to join list, yes tonamespace */
1485- addRTEtoQuery (pstate ,rte , false, true);
1489+ /* no to join list, yes tonamespaces */
1490+ addRTEtoQuery (pstate ,rte , false, true, true );
14861491
14871492stmt -> whereClause = transformWhereClause (pstate ,stmt -> whereClause ,
14881493"WHERE" );
@@ -1500,8 +1505,8 @@ transformIndexStmt(ParseState *pstate, IndexStmt *stmt)
15001505{
15011506rte = addRangeTableEntry (pstate ,stmt -> relation ,NULL ,
15021507 false, true);
1503- /* no to join list, yes tonamespace */
1504- addRTEtoQuery (pstate ,rte , false, true);
1508+ /* no to join list, yes tonamespaces */
1509+ addRTEtoQuery (pstate ,rte , false, true, true );
15051510}
15061511ielem -> expr = transformExpr (pstate ,ielem -> expr );
15071512
@@ -1559,10 +1564,10 @@ transformRuleStmt(ParseState *pstate, RuleStmt *stmt,
15591564Assert (pstate -> p_rtable == NIL );
15601565oldrte = addRangeTableEntryForRelation (pstate ,rel ,
15611566makeAlias ("*OLD*" ,NIL ),
1562- false,true );
1567+ false,false );
15631568newrte = addRangeTableEntryForRelation (pstate ,rel ,
15641569makeAlias ("*NEW*" ,NIL ),
1565- false,true );
1570+ false,false );
15661571/* Must override addRangeTableEntry's default access-check flags */
15671572oldrte -> requiredPerms = 0 ;
15681573newrte -> requiredPerms = 0 ;
@@ -1572,24 +1577,22 @@ transformRuleStmt(ParseState *pstate, RuleStmt *stmt,
15721577 * the one(s) that are relevant for the current kind of rule. In an
15731578 * UPDATE rule, quals must refer to OLD.field or NEW.field to be
15741579 * unambiguous, but there's no need to be so picky for INSERT &
1575- * DELETE. (Note we marked the RTEs "inFromCl = true" above to allow
1576- * unqualified references to their fields.) We do not add them to the
1577- * joinlist.
1580+ * DELETE. We do not add them to the joinlist.
15781581 */
15791582switch (stmt -> event )
15801583{
15811584case CMD_SELECT :
1582- addRTEtoQuery (pstate ,oldrte , false, true);
1585+ addRTEtoQuery (pstate ,oldrte , false, true, true );
15831586break ;
15841587case CMD_UPDATE :
1585- addRTEtoQuery (pstate ,oldrte , false, true);
1586- addRTEtoQuery (pstate ,newrte , false, true);
1588+ addRTEtoQuery (pstate ,oldrte , false, true, true );
1589+ addRTEtoQuery (pstate ,newrte , false, true, true );
15871590break ;
15881591case CMD_INSERT :
1589- addRTEtoQuery (pstate ,newrte , false, true);
1592+ addRTEtoQuery (pstate ,newrte , false, true, true );
15901593break ;
15911594case CMD_DELETE :
1592- addRTEtoQuery (pstate ,oldrte , false, true);
1595+ addRTEtoQuery (pstate ,oldrte , false, true, true );
15931596break ;
15941597default :
15951598elog (ERROR ,"unrecognized event type: %d" ,
@@ -1651,10 +1654,9 @@ transformRuleStmt(ParseState *pstate, RuleStmt *stmt,
16511654
16521655/*
16531656 * Set up OLD/NEW in the rtable for this statement. The
1654- * entries are marked not inFromCl because we don't want them
1655- * to be referred to by unqualified field names nor "*" in the
1656- * rule actions. We must add them to the namespace, however,
1657- * or they won't be accessible at all. We decide later
1657+ * entries are added only to relnamespace, not varnamespace,
1658+ * because we don't want them to be referred to by unqualified
1659+ * field names nor "*" in the rule actions. We decide later
16581660 * whether to put them in the joinlist.
16591661 */
16601662oldrte = addRangeTableEntryForRelation (sub_pstate ,rel ,
@@ -1665,8 +1667,8 @@ transformRuleStmt(ParseState *pstate, RuleStmt *stmt,
16651667 false, false);
16661668oldrte -> requiredPerms = 0 ;
16671669newrte -> requiredPerms = 0 ;
1668- addRTEtoQuery (sub_pstate ,oldrte , false, true);
1669- addRTEtoQuery (sub_pstate ,newrte , false, true);
1670+ addRTEtoQuery (sub_pstate ,oldrte , false, true, false );
1671+ addRTEtoQuery (sub_pstate ,newrte , false, true, false );
16701672
16711673/* Transform the rule action statement */
16721674top_subqry = transformStmt (sub_pstate ,action ,
@@ -1776,7 +1778,7 @@ transformRuleStmt(ParseState *pstate, RuleStmt *stmt,
17761778/* hack so we can use addRTEtoQuery() */
17771779sub_pstate -> p_rtable = sub_qry -> rtable ;
17781780sub_pstate -> p_joinlist = sub_qry -> jointree -> fromlist ;
1779- addRTEtoQuery (sub_pstate ,oldrte , true, false);
1781+ addRTEtoQuery (sub_pstate ,oldrte , true, false, false );
17801782sub_qry -> jointree -> fromlist = sub_pstate -> p_joinlist ;
17811783}
17821784
@@ -1906,10 +1908,10 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
19061908* dtlist ;
19071909List * targetvars ,
19081910* targetnames ,
1909- * sv_namespace ,
1911+ * sv_relnamespace ,
1912+ * sv_varnamespace ,
19101913* sv_rtable ;
19111914RangeTblEntry * jrte ;
1912- RangeTblRef * jrtr ;
19131915int tllen ;
19141916
19151917qry -> commandType = CMD_SELECT ;
@@ -2027,7 +2029,7 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
20272029
20282030/*
20292031 * As a first step towards supporting sort clauses that are
2030- * expressions using the output columns, generate anamespace entry
2032+ * expressions using the output columns, generate avarnamespace entry
20312033 * that makes the output columns visible. A Join RTE node is handy
20322034 * for this, since we can easily control the Vars generated upon
20332035 * matches.
@@ -2041,15 +2043,16 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
20412043JOIN_INNER ,
20422044targetvars ,
20432045NULL ,
2044- true);
2045- jrtr = makeNode (RangeTblRef );
2046- jrtr -> rtindex = 1 ;/* only entry in dummy rtable */
2046+ false);
20472047
20482048sv_rtable = pstate -> p_rtable ;
20492049pstate -> p_rtable = list_make1 (jrte );
20502050
2051- sv_namespace = pstate -> p_namespace ;
2052- pstate -> p_namespace = list_make1 (jrtr );
2051+ sv_relnamespace = pstate -> p_relnamespace ;
2052+ pstate -> p_relnamespace = NIL ;/* no qualified names allowed */
2053+
2054+ sv_varnamespace = pstate -> p_varnamespace ;
2055+ pstate -> p_varnamespace = list_make1 (jrte );
20532056
20542057/*
20552058 * For now, we don't support resjunk sort clauses on the output of a
@@ -2064,8 +2067,9 @@ transformSetOperationStmt(ParseState *pstate, SelectStmt *stmt)
20642067& qry -> targetList ,
20652068 false/* no unknowns expected */ );
20662069
2067- pstate -> p_namespace = sv_namespace ;
20682070pstate -> p_rtable = sv_rtable ;
2071+ pstate -> p_relnamespace = sv_relnamespace ;
2072+ pstate -> p_varnamespace = sv_varnamespace ;
20692073
20702074if (tllen != list_length (qry -> targetList ))
20712075ereport (ERROR ,
@@ -2164,7 +2168,7 @@ transformSetOperationTree(ParseState *pstate, SelectStmt *stmt)
21642168 * happen because the namespace will be empty, but it could happen
21652169 * if we are inside a rule.
21662170 */
2167- if (pstate -> p_namespace )
2171+ if (pstate -> p_relnamespace || pstate -> p_varnamespace )
21682172{
21692173if (contain_vars_of_level ((Node * )selectQuery ,1 ))
21702174ereport (ERROR ,