]> granicus.if.org Git - postgresql/blobdiff - src/backend/parser/parse_clause.c
Fix parser so that we don't modify the user-written ORDER BY list in order
[postgresql] / src / backend / parser / parse_clause.c
index b31e70205dd890cf0bfea0d130b3b6aa2b728d53..d82573fd03d9e20c6713c5ae2698950c3242fbc5 100644 (file)
@@ -3,12 +3,12 @@
  * parse_clause.c
  *       handle clauses in parser
  *
- * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/backend/parser/parse_clause.c,v 1.122 2003/08/17 19:58:05 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/parser/parse_clause.c,v 1.171 2008/07/31 22:47:56 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -17,6 +17,8 @@
 
 #include "access/heapam.h"
 #include "catalog/heap.h"
+#include "catalog/pg_type.h"
+#include "commands/defrem.h"
 #include "nodes/makefuncs.h"
 #include "optimizer/clauses.h"
 #include "optimizer/tlist.h"
 #include "parser/parse_oper.h"
 #include "parser/parse_relation.h"
 #include "parser/parse_target.h"
-#include "parser/parse_type.h"
 #include "rewrite/rewriteManip.h"
-#include "utils/builtins.h"
 #include "utils/guc.h"
+#include "utils/lsyscache.h"
+#include "utils/rel.h"
 
 
 #define ORDER_CLAUSE 0
@@ -47,29 +49,34 @@ static void extractRemainingColumns(List *common_colnames,
 static Node *transformJoinUsingClause(ParseState *pstate,
                                                 List *leftVars, List *rightVars);
 static Node *transformJoinOnClause(ParseState *pstate, JoinExpr *j,
-                                         List *containedRels);
-static RangeTblRef *transformTableEntry(ParseState *pstate, RangeVar *r);
-static RangeTblRef *transformRangeSubselect(ParseState *pstate,
+                                         RangeTblEntry *l_rte,
+                                         RangeTblEntry *r_rte,
+                                         List *relnamespace,
+                                         Relids containedRels);
+static RangeTblEntry *transformTableEntry(ParseState *pstate, RangeVar *r);
+static RangeTblEntry *transformRangeSubselect(ParseState *pstate,
                                                RangeSubselect *r);
-static RangeTblRef *transformRangeFunction(ParseState *pstate,
+static RangeTblEntry *transformRangeFunction(ParseState *pstate,
                                           RangeFunction *r);
 static Node *transformFromClauseItem(ParseState *pstate, Node *n,
-                                               List **containedRels);
+                                               RangeTblEntry **top_rte, int *top_rti,
+                                               List **relnamespace,
+                                               Relids *containedRels);
 static Node *buildMergedJoinVar(ParseState *pstate, JoinType jointype,
                                   Var *l_colvar, Var *r_colvar);
 static TargetEntry *findTargetlistEntry(ParseState *pstate, Node *node,
-                                       List *tlist, int clause);
+                                       List **tlist, int clause);
 
 
 /*
  * transformFromClause -
  *       Process the FROM clause and add items to the query's range table,
- *       joinlist, and namespace.
+ *       joinlist, and namespaces.
  *
- * Note: we assume that pstate's p_rtable, p_joinlist, and p_namespace lists
- * were initialized to NIL when the pstate was created.  We will add onto
- * any entries already present --- this is needed for rule processing, as
- * well as for UPDATE and DELETE.
+ * Note: we assume that pstate's p_rtable, p_joinlist, p_relnamespace, and
+ * p_varnamespace lists were initialized to NIL when the pstate was created.
+ * We will add onto any entries already present --- this is needed for rule
+ * processing, as well as for UPDATE and DELETE.
  *
  * The range table may grow still further when we transform the expressions
  * in the query's quals and target list. (This is possible because in
@@ -79,23 +86,33 @@ static TargetEntry *findTargetlistEntry(ParseState *pstate, Node *node,
 void
 transformFromClause(ParseState *pstate, List *frmList)
 {
-       List       *fl;
+       ListCell   *fl;
 
        /*
-        * The grammar will have produced a list of RangeVars,
-        * RangeSubselects, RangeFunctions, and/or JoinExprs. Transform each
-        * one (possibly adding entries to the rtable), check for duplicate
-        * refnames, and then add it to the joinlist and namespace.
+        * The grammar will have produced a list of RangeVars, RangeSubselects,
+        * RangeFunctions, and/or JoinExprs. Transform each one (possibly adding
+        * entries to the rtable), check for duplicate refnames, and then add it
+        * to the joinlist and namespaces.
         */
        foreach(fl, frmList)
        {
                Node       *n = lfirst(fl);
-               List       *containedRels;
-
-               n = transformFromClauseItem(pstate, n, &containedRels);
-               checkNameSpaceConflicts(pstate, (Node *) pstate->p_namespace, n);
+               RangeTblEntry *rte;
+               int                     rtindex;
+               List       *relnamespace;
+               Relids          containedRels;
+
+               n = transformFromClauseItem(pstate, n,
+                                                                       &rte,
+                                                                       &rtindex,
+                                                                       &relnamespace,
+                                                                       &containedRels);
+               checkNameSpaceConflicts(pstate, pstate->p_relnamespace, relnamespace);
                pstate->p_joinlist = lappend(pstate->p_joinlist, n);
-               pstate->p_namespace = lappend(pstate->p_namespace, n);
+               pstate->p_relnamespace = list_concat(pstate->p_relnamespace,
+                                                                                        relnamespace);
+               pstate->p_varnamespace = lappend(pstate->p_varnamespace, rte);
+               bms_free(containedRels);
        }
 }
 
@@ -116,11 +133,14 @@ transformFromClause(ParseState *pstate, List *frmList)
  *       to check for namespace conflict; we assume that the namespace was
  *       initially empty in these cases.)
  *
+ *       Finally, we mark the relation as requiring the permissions specified
+ *       by requiredPerms.
+ *
  *       Returns the rangetable index of the target relation.
  */
 int
 setTargetTable(ParseState *pstate, RangeVar *relation,
-                          bool inh, bool alsoSource)
+                          bool inh, bool alsoSource, AclMode requiredPerms)
 {
        RangeTblEntry *rte;
        int                     rtindex;
@@ -130,41 +150,42 @@ setTargetTable(ParseState *pstate, RangeVar *relation,
                heap_close(pstate->p_target_relation, NoLock);
 
        /*
-        * Open target rel and grab suitable lock (which we will hold till end
-        * of transaction).
+        * Open target rel and grab suitable lock (which we will hold till end of
+        * transaction).
         *
-        * analyze.c will eventually do the corresponding heap_close(), but *not*
-        * release the lock.
+        * free_parsestate() will eventually do the corresponding heap_close(),
+        * but *not* release the lock.
         */
        pstate->p_target_relation = heap_openrv(relation, RowExclusiveLock);
 
        /*
         * Now build an RTE.
         */
-       rte = addRangeTableEntry(pstate, relation, NULL, inh, false);
+       rte = addRangeTableEntryForRelation(pstate, pstate->p_target_relation,
+                                                                               relation->alias, inh, false);
        pstate->p_target_rangetblentry = rte;
 
        /* assume new rte is at end */
-       rtindex = length(pstate->p_rtable);
+       rtindex = list_length(pstate->p_rtable);
        Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
 
        /*
-        * Override addRangeTableEntry's default checkForRead, and instead
-        * mark target table as requiring write access.
+        * Override addRangeTableEntry's default ACL_SELECT permissions check, and
+        * instead mark target table as requiring exactly the specified
+        * permissions.
         *
         * If we find an explicit reference to the rel later during parse
-        * analysis, scanRTEForColumn will change checkForRead to 'true'
-        * again.  That can't happen for INSERT but it is possible for UPDATE
-        * and DELETE.
+        * analysis, we will add the ACL_SELECT bit back again; see
+        * scanRTEForColumn (for simple field references), ExpandColumnRefStar
+        * (for foo.*) and ExpandAllTables (for *).
         */
-       rte->checkForRead = false;
-       rte->checkForWrite = true;
+       rte->requiredPerms = requiredPerms;
 
        /*
-        * If UPDATE/DELETE, add table to joinlist and namespace.
+        * If UPDATE/DELETE, add table to joinlist and namespaces.
         */
        if (alsoSource)
-               addRTEtoQuery(pstate, rte, true, true);
+               addRTEtoQuery(pstate, rte, true, true, true);
 
        return rtindex;
 }
@@ -173,7 +194,7 @@ setTargetTable(ParseState *pstate, RangeVar *relation,
  * Simplify InhOption (yes/no/default) into boolean yes/no.
  *
  * The reason we do things this way is that we don't want to examine the
- * SQL_inheritance option flag until parse_analyze is run.     Otherwise,
+ * SQL_inheritance option flag until parse_analyze() is run.   Otherwise,
  * we'd do the wrong thing with query strings that intermix SET commands
  * with queries.
  */
@@ -189,10 +210,34 @@ interpretInhOption(InhOption inhOpt)
                case INH_DEFAULT:
                        return SQL_inheritance;
        }
-       elog(ERROR, "bogus InhOption value");
+       elog(ERROR, "bogus InhOption value: %d", inhOpt);
        return false;                           /* keep compiler quiet */
 }
 
+/*
+ * Given a relation-options list (of DefElems), return true iff the specified
+ * table/result set should be created with OIDs. This needs to be done after
+ * parsing the query string because the return value can depend upon the
+ * default_with_oids GUC var.
+ */
+bool
+interpretOidsOption(List *defList)
+{
+       ListCell   *cell;
+
+       /* Scan list to see if OIDS was included */
+       foreach(cell, defList)
+       {
+               DefElem    *def = (DefElem *) lfirst(cell);
+
+               if (pg_strcasecmp(def->defname, "oids") == 0)
+                       return defGetBoolean(def);
+       }
+
+       /* OIDS option was not specified, so use default. */
+       return default_with_oids;
+}
+
 /*
  * Extract all not-in-common columns from column lists of a source table
  */
@@ -203,14 +248,16 @@ extractRemainingColumns(List *common_colnames,
 {
        List       *new_colnames = NIL;
        List       *new_colvars = NIL;
-       List       *lnames,
-                          *lvars = src_colvars;
+       ListCell   *lnames,
+                          *lvars;
+
+       Assert(list_length(src_colnames) == list_length(src_colvars));
 
-       foreach(lnames, src_colnames)
+       forboth(lnames, src_colnames, lvars, src_colvars)
        {
                char       *colname = strVal(lfirst(lnames));
                bool            match = false;
-               List       *cnames;
+               ListCell   *cnames;
 
                foreach(cnames, common_colnames)
                {
@@ -228,8 +275,6 @@ extractRemainingColumns(List *common_colnames,
                        new_colnames = lappend(new_colnames, lfirst(lnames));
                        new_colvars = lappend(new_colvars, lfirst(lvars));
                }
-
-               lvars = lnext(lvars);
        }
 
        *res_colnames = new_colnames;
@@ -245,22 +290,23 @@ static Node *
 transformJoinUsingClause(ParseState *pstate, List *leftVars, List *rightVars)
 {
        Node       *result = NULL;
-       List       *lvars,
-                          *rvars = rightVars;
+       ListCell   *lvars,
+                          *rvars;
 
        /*
-        * We cheat a little bit here by building an untransformed operator
-        * tree whose leaves are the already-transformed Vars.  This is OK
-        * because transformExpr() won't complain about already-transformed
-        * subnodes.
+        * We cheat a little bit here by building an untransformed operator tree
+        * whose leaves are the already-transformed Vars.  This is OK because
+        * transformExpr() won't complain about already-transformed subnodes.
         */
-       foreach(lvars, leftVars)
+       forboth(lvars, leftVars, rvars, rightVars)
        {
                Node       *lvar = (Node *) lfirst(lvars);
                Node       *rvar = (Node *) lfirst(rvars);
                A_Expr     *e;
 
-               e = makeSimpleA_Expr(AEXPR_OP, "=", copyObject(lvar), copyObject(rvar));
+               e = makeSimpleA_Expr(AEXPR_OP, "=",
+                                                        copyObject(lvar), copyObject(rvar),
+                                                        -1);
 
                if (result == NULL)
                        result = (Node *) e;
@@ -268,25 +314,23 @@ transformJoinUsingClause(ParseState *pstate, List *leftVars, List *rightVars)
                {
                        A_Expr     *a;
 
-                       a = makeA_Expr(AEXPR_AND, NIL, result, (Node *) e);
+                       a = makeA_Expr(AEXPR_AND, NIL, result, (Node *) e, -1);
                        result = (Node *) a;
                }
-
-               rvars = lnext(rvars);
        }
 
        /*
-        * Since the references are already Vars, and are certainly from the
-        * input relations, we don't have to go through the same pushups that
-        * transformJoinOnClause() does.  Just invoke transformExpr() to fix
-        * up the operators, and we're done.
+        * Since the references are already Vars, and are certainly from the input
+        * relations, we don't have to go through the same pushups that
+        * transformJoinOnClause() does.  Just invoke transformExpr() to fix up
+        * the operators, and we're done.
         */
        result = transformExpr(pstate, result);
 
        result = coerce_to_boolean(pstate, result, "JOIN/USING");
 
        return result;
-}      /* transformJoinUsingClause() */
+}
 
 /* transformJoinOnClause()
  *       Transform the qual conditions for JOIN/ON.
@@ -294,48 +338,52 @@ transformJoinUsingClause(ParseState *pstate, List *leftVars, List *rightVars)
  */
 static Node *
 transformJoinOnClause(ParseState *pstate, JoinExpr *j,
-                                         List *containedRels)
+                                         RangeTblEntry *l_rte,
+                                         RangeTblEntry *r_rte,
+                                         List *relnamespace,
+                                         Relids containedRels)
 {
        Node       *result;
-       List       *save_namespace;
+       List       *save_relnamespace;
+       List       *save_varnamespace;
        Relids          clause_varnos;
        int                     varno;
 
        /*
-        * This is a tad tricky, for two reasons.  First, the namespace that
-        * the join expression should see is just the two subtrees of the JOIN
-        * plus any outer references from upper pstate levels.  So,
-        * temporarily set this pstate's namespace accordingly.  (We need not
-        * check for refname conflicts, because transformFromClauseItem()
-        * already did.) NOTE: this code is OK only because the ON clause
-        * can't legally alter the namespace by causing implicit relation refs
-        * to be added.
+        * This is a tad tricky, for two reasons.  First, the namespace that the
+        * join expression should see is just the two subtrees of the JOIN plus
+        * any outer references from upper pstate levels.  So, temporarily set
+        * this pstate's namespace accordingly.  (We need not check for refname
+        * conflicts, because transformFromClauseItem() already did.) NOTE: this
+        * code is OK only because the ON clause can't legally alter the namespace
+        * by causing implicit relation refs to be added.
         */
-       save_namespace = pstate->p_namespace;
-       pstate->p_namespace = makeList2(j->larg, j->rarg);
+       save_relnamespace = pstate->p_relnamespace;
+       save_varnamespace = pstate->p_varnamespace;
+
+       pstate->p_relnamespace = relnamespace;
+       pstate->p_varnamespace = list_make2(l_rte, r_rte);
 
        result = transformWhereClause(pstate, j->quals, "JOIN/ON");
 
-       pstate->p_namespace = save_namespace;
+       pstate->p_relnamespace = save_relnamespace;
+       pstate->p_varnamespace = save_varnamespace;
 
        /*
         * Second, we need to check that the ON condition doesn't refer to any
-        * rels outside the input subtrees of the JOIN.  It could do that
-        * despite our hack on the namespace if it uses fully-qualified names.
-        * So, grovel through the transformed clause and make sure there are
-        * no bogus references.  (Outer references are OK, and are ignored
-        * here.)
+        * rels outside the input subtrees of the JOIN.  It could do that despite
+        * our hack on the namespace if it uses fully-qualified names. So, grovel
+        * through the transformed clause and make sure there are no bogus
+        * references.  (Outer references are OK, and are ignored here.)
         */
        clause_varnos = pull_varnos(result);
-       while ((varno = bms_first_member(clause_varnos)) >= 0)
+       clause_varnos = bms_del_members(clause_varnos, containedRels);
+       if ((varno = bms_first_member(clause_varnos)) >= 0)
        {
-               if (!intMember(varno, containedRels))
-               {
-                       ereport(ERROR,
-                                       (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
-                                        errmsg("JOIN/ON clause refers to \"%s\", which is not part of JOIN",
-                                  rt_fetch(varno, pstate->p_rtable)->eref->aliasname)));
-               }
+               ereport(ERROR,
+                               (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
+                errmsg("JOIN/ON clause refers to \"%s\", which is not part of JOIN",
+                               rt_fetch(varno, pstate->p_rtable)->eref->aliasname)));
        }
        bms_free(clause_varnos);
 
@@ -345,100 +393,80 @@ transformJoinOnClause(ParseState *pstate, JoinExpr *j,
 /*
  * transformTableEntry --- transform a RangeVar (simple relation reference)
  */
-static RangeTblRef *
+static RangeTblEntry *
 transformTableEntry(ParseState *pstate, RangeVar *r)
 {
        RangeTblEntry *rte;
-       RangeTblRef *rtr;
 
        /*
-        * mark this entry to indicate it comes from the FROM clause. In SQL,
-        * the target list can only refer to range variables specified in the
-        * from clause but we follow the more powerful POSTQUEL semantics and
+        * mark this entry to indicate it comes from the FROM clause. In SQL, the
+        * target list can only refer to range variables specified in the from
+        * clause but we follow the more powerful POSTQUEL semantics and
         * automatically generate the range variable if not specified. However
         * there are times we need to know whether the entries are legitimate.
         */
        rte = addRangeTableEntry(pstate, r, r->alias,
                                                         interpretInhOption(r->inhOpt), true);
 
-       /*
-        * We create a RangeTblRef, but we do not add it to the joinlist or
-        * namespace; our caller must do that if appropriate.
-        */
-       rtr = makeNode(RangeTblRef);
-       /* assume new rte is at end */
-       rtr->rtindex = length(pstate->p_rtable);
-       Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
-
-       return rtr;
+       return rte;
 }
 
 
 /*
  * transformRangeSubselect --- transform a sub-SELECT appearing in FROM
  */
-static RangeTblRef *
+static RangeTblEntry *
 transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
 {
-       List       *parsetrees;
        Query      *query;
        RangeTblEntry *rte;
-       RangeTblRef *rtr;
 
        /*
-        * We require user to supply an alias for a subselect, per SQL92. To
-        * relax this, we'd have to be prepared to gin up a unique alias for
-        * an unlabeled subselect.
+        * We require user to supply an alias for a subselect, per SQL92. To relax
+        * this, we'd have to be prepared to gin up a unique alias for an
+        * unlabeled subselect.
         */
        if (r->alias == NULL)
                ereport(ERROR,
                                (errcode(ERRCODE_SYNTAX_ERROR),
-                                errmsg("sub-select in FROM must have an alias")));
+                                errmsg("subquery in FROM must have an alias")));
 
        /*
         * Analyze and transform the subquery.
         */
-       parsetrees = parse_sub_analyze(r->subquery, pstate);
+       query = parse_sub_analyze(r->subquery, pstate);
 
        /*
-        * Check that we got something reasonable.      Most of these conditions
-        * are probably impossible given restrictions of the grammar, but
-        * check 'em anyway.
+        * Check that we got something reasonable.      Many of these conditions are
+        * impossible given restrictions of the grammar, but check 'em anyway.
         */
-       if (length(parsetrees) != 1)
-               elog(ERROR, "unexpected parse analysis result for sub-select in FROM");
-       query = (Query *) lfirst(parsetrees);
-       if (query == NULL || !IsA(query, Query))
-               elog(ERROR, "unexpected parse analysis result for sub-select in FROM");
-
-       if (query->commandType != CMD_SELECT)
-               elog(ERROR, "expected SELECT query from sub-select in FROM");
-       if (query->resultRelation != 0 || query->into != NULL)
+       if (query->commandType != CMD_SELECT ||
+               query->utilityStmt != NULL)
+               elog(ERROR, "expected SELECT query from subquery in FROM");
+       if (query->intoClause != NULL)
                ereport(ERROR,
                                (errcode(ERRCODE_SYNTAX_ERROR),
-                                errmsg("sub-select in FROM may not have SELECT INTO")));
+                                errmsg("subquery in FROM cannot have SELECT INTO")));
 
        /*
-        * The subquery cannot make use of any variables from FROM items
-        * created earlier in the current query.  Per SQL92, the scope of a
-        * FROM item does not include other FROM items.  Formerly we hacked
-        * the namespace so that the other variables weren't even visible, but
-        * it seems more useful to leave them visible and give a specific
-        * error message.
+        * The subquery cannot make use of any variables from FROM items created
+        * earlier in the current query.  Per SQL92, the scope of a FROM item does
+        * not include other FROM items.  Formerly we hacked the namespace so that
+        * the other variables weren't even visible, but it seems more useful to
+        * leave them visible and give a specific error message.
         *
         * XXX this will need further work to support SQL99's LATERAL() feature,
         * wherein such references would indeed be legal.
         *
         * We can skip groveling through the subquery if there's not anything
-        * visible in the current query.  Also note that outer references are
-        * OK.
+        * visible in the current query.  Also note that outer references are OK.
         */
-       if (pstate->p_namespace)
+       if (pstate->p_relnamespace || pstate->p_varnamespace)
        {
                if (contain_vars_of_level((Node *) query, 1))
                        ereport(ERROR,
                                        (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
-                                        errmsg("sub-select in FROM may not refer to other relations of same query level")));
+                                        errmsg("subquery in FROM cannot refer to other relations of same query level")));
        }
 
        /*
@@ -446,59 +474,53 @@ transformRangeSubselect(ParseState *pstate, RangeSubselect *r)
         */
        rte = addRangeTableEntryForSubquery(pstate, query, r->alias, true);
 
-       /*
-        * We create a RangeTblRef, but we do not add it to the joinlist or
-        * namespace; our caller must do that if appropriate.
-        */
-       rtr = makeNode(RangeTblRef);
-       /* assume new rte is at end */
-       rtr->rtindex = length(pstate->p_rtable);
-       Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
-
-       return rtr;
+       return rte;
 }
 
 
 /*
  * transformRangeFunction --- transform a function call appearing in FROM
  */
-static RangeTblRef *
+static RangeTblEntry *
 transformRangeFunction(ParseState *pstate, RangeFunction *r)
 {
        Node       *funcexpr;
        char       *funcname;
        RangeTblEntry *rte;
-       RangeTblRef *rtr;
 
-       /* Get function name for possible use as alias */
-       Assert(IsA(r->funccallnode, FuncCall));
-       funcname = strVal(llast(((FuncCall *) r->funccallnode)->funcname));
+       /*
+        * Get function name for possible use as alias.  We use the same
+        * transformation rules as for a SELECT output expression.      For a FuncCall
+        * node, the result will be the function name, but it is possible for the
+        * grammar to hand back other node types.
+        */
+       funcname = FigureColname(r->funccallnode);
 
        /*
-        * Transform the raw FuncCall node.
+        * Transform the raw expression.
         */
        funcexpr = transformExpr(pstate, r->funccallnode);
 
        /*
         * The function parameters cannot make use of any variables from other
         * FROM items.  (Compare to transformRangeSubselect(); the coding is
-        * different though because we didn't parse as a sub-select with its
-        * own level of namespace.)
+        * different though because we didn't parse as a sub-select with its own
+        * level of namespace.)
         *
         * XXX this will need further work to support SQL99's LATERAL() feature,
         * wherein such references would indeed be legal.
         */
-       if (pstate->p_namespace)
+       if (pstate->p_relnamespace || pstate->p_varnamespace)
        {
                if (contain_vars_of_level(funcexpr, 0))
                        ereport(ERROR,
                                        (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
-                                        errmsg("function expression in FROM may not refer to other relations of same query level")));
+                                        errmsg("function expression in FROM cannot refer to other relations of same query level")));
        }
 
        /*
-        * Disallow aggregate functions in the expression.      (No reason to
-        * postpone this check until parseCheckAggregates.)
+        * Disallow aggregate functions in the expression.      (No reason to postpone
+        * this check until parseCheckAggregates.)
         */
        if (pstate->p_hasAggs)
        {
@@ -509,33 +531,28 @@ transformRangeFunction(ParseState *pstate, RangeFunction *r)
        }
 
        /*
-        * If a coldeflist is supplied, ensure it defines a legal set of names
+        * OK, build an RTE for the function.
+        */
+       rte = addRangeTableEntryForFunction(pstate, funcname, funcexpr,
+                                                                               r, true);
+
+       /*
+        * If a coldeflist was supplied, ensure it defines a legal set of names
         * (no duplicates) and datatypes (no pseudo-types, for instance).
+        * addRangeTableEntryForFunction looked up the type names but didn't check
+        * them further than that.
         */
        if (r->coldeflist)
        {
                TupleDesc       tupdesc;
 
-               tupdesc = BuildDescForRelation(r->coldeflist);
+               tupdesc = BuildDescFromLists(rte->eref->colnames,
+                                                                        rte->funccoltypes,
+                                                                        rte->funccoltypmods);
                CheckAttributeNamesTypes(tupdesc, RELKIND_COMPOSITE_TYPE);
        }
 
-       /*
-        * OK, build an RTE for the function.
-        */
-       rte = addRangeTableEntryForFunction(pstate, funcname, funcexpr,
-                                                                               r, true);
-
-       /*
-        * We create a RangeTblRef, but we do not add it to the joinlist or
-        * namespace; our caller must do that if appropriate.
-        */
-       rtr = makeNode(RangeTblRef);
-       /* assume new rte is at end */
-       rtr->rtindex = length(pstate->p_rtable);
-       Assert(rte == rt_fetch(rtr->rtindex, pstate->p_rtable));
-
-       return rtr;
+       return rte;
 }
 
 
@@ -543,122 +560,166 @@ transformRangeFunction(ParseState *pstate, RangeFunction *r)
  * transformFromClauseItem -
  *       Transform a FROM-clause item, adding any required entries to the
  *       range table list being built in the ParseState, and return the
- *       transformed item ready to include in the joinlist and namespace.
+ *       transformed item ready to include in the joinlist and namespaces.
  *       This routine can recurse to handle SQL92 JOIN expressions.
  *
- *       Aside from the primary return value (the transformed joinlist item)
- *       this routine also returns an integer list of the rangetable indexes
- *       of all the base and join relations represented in the joinlist item.
- *       This list is needed for checking JOIN/ON conditions in higher levels.
+ * The function return value is the node to add to the jointree (a
+ * RangeTblRef or JoinExpr).  Additional output parameters are:
+ *
+ * *top_rte: receives the RTE corresponding to the jointree item.
+ * (We could extract this from the function return node, but it saves cycles
+ * to pass it back separately.)
+ *
+ * *top_rti: receives the rangetable index of top_rte. (Ditto.)
+ *
+ * *relnamespace: receives a List of the RTEs exposed as relation names
+ * by this item.
+ *
+ * *containedRels: receives a bitmap set of the rangetable indexes
+ * of all the base and join relations represented in this jointree item.
+ * This is needed for checking JOIN/ON conditions in higher levels.
+ *
+ * We do not need to pass back an explicit varnamespace value, because
+ * in all cases the varnamespace contribution is exactly top_rte.
  */
 static Node *
-transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels)
+transformFromClauseItem(ParseState *pstate, Node *n,
+                                               RangeTblEntry **top_rte, int *top_rti,
+                                               List **relnamespace,
+                                               Relids *containedRels)
 {
        if (IsA(n, RangeVar))
        {
                /* Plain relation reference */
                RangeTblRef *rtr;
+               RangeTblEntry *rte;
+               int                     rtindex;
 
-               rtr = transformTableEntry(pstate, (RangeVar *) n);
-               *containedRels = makeListi1(rtr->rtindex);
+               rte = transformTableEntry(pstate, (RangeVar *) n);
+               /* assume new rte is at end */
+               rtindex = list_length(pstate->p_rtable);
+               Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
+               *top_rte = rte;
+               *top_rti = rtindex;
+               *relnamespace = list_make1(rte);
+               *containedRels = bms_make_singleton(rtindex);
+               rtr = makeNode(RangeTblRef);
+               rtr->rtindex = rtindex;
                return (Node *) rtr;
        }
        else if (IsA(n, RangeSubselect))
        {
                /* sub-SELECT is like a plain relation */
                RangeTblRef *rtr;
+               RangeTblEntry *rte;
+               int                     rtindex;
 
-               rtr = transformRangeSubselect(pstate, (RangeSubselect *) n);
-               *containedRels = makeListi1(rtr->rtindex);
+               rte = transformRangeSubselect(pstate, (RangeSubselect *) n);
+               /* assume new rte is at end */
+               rtindex = list_length(pstate->p_rtable);
+               Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
+               *top_rte = rte;
+               *top_rti = rtindex;
+               *relnamespace = list_make1(rte);
+               *containedRels = bms_make_singleton(rtindex);
+               rtr = makeNode(RangeTblRef);
+               rtr->rtindex = rtindex;
                return (Node *) rtr;
        }
        else if (IsA(n, RangeFunction))
        {
                /* function is like a plain relation */
                RangeTblRef *rtr;
+               RangeTblEntry *rte;
+               int                     rtindex;
 
-               rtr = transformRangeFunction(pstate, (RangeFunction *) n);
-               *containedRels = makeListi1(rtr->rtindex);
+               rte = transformRangeFunction(pstate, (RangeFunction *) n);
+               /* assume new rte is at end */
+               rtindex = list_length(pstate->p_rtable);
+               Assert(rte == rt_fetch(rtindex, pstate->p_rtable));
+               *top_rte = rte;
+               *top_rti = rtindex;
+               *relnamespace = list_make1(rte);
+               *containedRels = bms_make_singleton(rtindex);
+               rtr = makeNode(RangeTblRef);
+               rtr->rtindex = rtindex;
                return (Node *) rtr;
        }
        else if (IsA(n, JoinExpr))
        {
                /* A newfangled join expression */
                JoinExpr   *j = (JoinExpr *) n;
-               List       *my_containedRels,
-                                  *l_containedRels,
-                                  *r_containedRels,
+               RangeTblEntry *l_rte;
+               RangeTblEntry *r_rte;
+               int                     l_rtindex;
+               int                     r_rtindex;
+               Relids          l_containedRels,
+                                       r_containedRels,
+                                       my_containedRels;
+               List       *l_relnamespace,
+                                  *r_relnamespace,
+                                  *my_relnamespace,
                                   *l_colnames,
                                   *r_colnames,
                                   *res_colnames,
                                   *l_colvars,
                                   *r_colvars,
                                   *res_colvars;
-               Index           leftrti,
-                                       rightrti;
                RangeTblEntry *rte;
 
                /*
                 * Recursively process the left and right subtrees
                 */
-               j->larg = transformFromClauseItem(pstate, j->larg, &l_containedRels);
-               j->rarg = transformFromClauseItem(pstate, j->rarg, &r_containedRels);
+               j->larg = transformFromClauseItem(pstate, j->larg,
+                                                                                 &l_rte,
+                                                                                 &l_rtindex,
+                                                                                 &l_relnamespace,
+                                                                                 &l_containedRels);
+               j->rarg = transformFromClauseItem(pstate, j->rarg,
+                                                                                 &r_rte,
+                                                                                 &r_rtindex,
+                                                                                 &r_relnamespace,
+                                                                                 &r_containedRels);
 
                /*
-                * Generate combined list of relation indexes for possible use by
-                * transformJoinOnClause below.
+                * Check for conflicting refnames in left and right subtrees. Must do
+                * this because higher levels will assume I hand back a self-
+                * consistent namespace subtree.
                 */
-               my_containedRels = nconc(l_containedRels, r_containedRels);
+               checkNameSpaceConflicts(pstate, l_relnamespace, r_relnamespace);
 
                /*
-                * Check for conflicting refnames in left and right subtrees. Must
-                * do this because higher levels will assume I hand back a self-
-                * consistent namespace subtree.
+                * Generate combined relation membership info for possible use by
+                * transformJoinOnClause below.
                 */
-               checkNameSpaceConflicts(pstate, j->larg, j->rarg);
+               my_relnamespace = list_concat(l_relnamespace, r_relnamespace);
+               my_containedRels = bms_join(l_containedRels, r_containedRels);
+
+               pfree(r_relnamespace);  /* free unneeded list header */
 
                /*
                 * Extract column name and var lists from both subtrees
                 *
                 * Note: expandRTE returns new lists, safe for me to modify
                 */
-               if (IsA(j->larg, RangeTblRef))
-                       leftrti = ((RangeTblRef *) j->larg)->rtindex;
-               else if (IsA(j->larg, JoinExpr))
-                       leftrti = ((JoinExpr *) j->larg)->rtindex;
-               else
-               {
-                       elog(ERROR, "unrecognized node type: %d", (int) nodeTag(j->larg));
-                       leftrti = 0;            /* keep compiler quiet */
-               }
-               rte = rt_fetch(leftrti, pstate->p_rtable);
-               expandRTE(pstate, rte, &l_colnames, &l_colvars);
-
-               if (IsA(j->rarg, RangeTblRef))
-                       rightrti = ((RangeTblRef *) j->rarg)->rtindex;
-               else if (IsA(j->rarg, JoinExpr))
-                       rightrti = ((JoinExpr *) j->rarg)->rtindex;
-               else
-               {
-                       elog(ERROR, "unrecognized node type: %d", (int) nodeTag(j->rarg));
-                       rightrti = 0;           /* keep compiler quiet */
-               }
-               rte = rt_fetch(rightrti, pstate->p_rtable);
-               expandRTE(pstate, rte, &r_colnames, &r_colvars);
+               expandRTE(l_rte, l_rtindex, 0, false,
+                                 &l_colnames, &l_colvars);
+               expandRTE(r_rte, r_rtindex, 0, false,
+                                 &r_colnames, &r_colvars);
 
                /*
                 * Natural join does not explicitly specify columns; must generate
-                * columns to join. Need to run through the list of columns from
-                * each table or join result and match up the column names. Use
-                * the first table, and check every column in the second table for
-                * a match.  (We'll check that the matches were unique later on.)
-                * The result of this step is a list of column names just like an
-                * explicitly-written USING list.
+                * columns to join. Need to run through the list of columns from each
+                * table or join result and match up the column names. Use the first
+                * table, and check every column in the second table for a match.
+                * (We'll check that the matches were unique later on.) The result of
+                * this step is a list of column names just like an explicitly-written
+                * USING list.
                 */
                if (j->isNatural)
                {
                        List       *rlist = NIL;
-                       List       *lx,
+                       ListCell   *lx,
                                           *rx;
 
                        Assert(j->using == NIL);        /* shouldn't have USING() too */
@@ -696,21 +757,21 @@ transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels)
                if (j->using)
                {
                        /*
-                        * JOIN/USING (or NATURAL JOIN, as transformed above).
-                        * Transform the list into an explicit ON-condition, and
-                        * generate a list of merged result columns.
+                        * JOIN/USING (or NATURAL JOIN, as transformed above). Transform
+                        * the list into an explicit ON-condition, and generate a list of
+                        * merged result columns.
                         */
                        List       *ucols = j->using;
                        List       *l_usingvars = NIL;
                        List       *r_usingvars = NIL;
-                       List       *ucol;
+                       ListCell   *ucol;
 
                        Assert(j->quals == NULL);       /* shouldn't have ON() too */
 
                        foreach(ucol, ucols)
                        {
                                char       *u_colname = strVal(lfirst(ucol));
-                               List       *col;
+                               ListCell   *col;
                                int                     ndx;
                                int                     l_index = -1;
                                int                     r_index = -1;
@@ -725,7 +786,7 @@ transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels)
                                        if (strcmp(res_colname, u_colname) == 0)
                                                ereport(ERROR,
                                                                (errcode(ERRCODE_DUPLICATE_COLUMN),
-                                                                errmsg("USING column name \"%s\" appears more than once",
+                                                                errmsg("column name \"%s\" appears more than once in USING clause",
                                                                                u_colname)));
                                }
 
@@ -749,7 +810,7 @@ transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels)
                                if (l_index < 0)
                                        ereport(ERROR,
                                                        (errcode(ERRCODE_UNDEFINED_COLUMN),
-                                                        errmsg("JOIN/USING column \"%s\" not found in left table",
+                                                        errmsg("column \"%s\" specified in USING clause does not exist in left table",
                                                                        u_colname)));
 
                                /* Find it in right input */
@@ -772,12 +833,12 @@ transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels)
                                if (r_index < 0)
                                        ereport(ERROR,
                                                        (errcode(ERRCODE_UNDEFINED_COLUMN),
-                                                        errmsg("JOIN/USING column \"%s\" not found in right table",
+                                                        errmsg("column \"%s\" specified in USING clause does not exist in right table",
                                                                        u_colname)));
 
-                               l_colvar = nth(l_index, l_colvars);
+                               l_colvar = list_nth(l_colvars, l_index);
                                l_usingvars = lappend(l_usingvars, l_colvar);
-                               r_colvar = nth(r_index, r_colvars);
+                               r_colvar = list_nth(r_colvars, r_index);
                                r_usingvars = lappend(r_usingvars, r_colvar);
 
                                res_colnames = lappend(res_colnames, lfirst(ucol));
@@ -795,7 +856,10 @@ transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels)
                else if (j->quals)
                {
                        /* User-written ON-condition; transform it */
-                       j->quals = transformJoinOnClause(pstate, j, my_containedRels);
+                       j->quals = transformJoinOnClause(pstate, j,
+                                                                                        l_rte, r_rte,
+                                                                                        my_relnamespace,
+                                                                                        my_containedRels);
                }
                else
                {
@@ -809,10 +873,10 @@ transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels)
                extractRemainingColumns(res_colnames,
                                                                r_colnames, r_colvars,
                                                                &r_colnames, &r_colvars);
-               res_colnames = nconc(res_colnames, l_colnames);
-               res_colvars = nconc(res_colvars, l_colvars);
-               res_colnames = nconc(res_colnames, r_colnames);
-               res_colvars = nconc(res_colvars, r_colvars);
+               res_colnames = list_concat(res_colnames, l_colnames);
+               res_colvars = list_concat(res_colvars, l_colvars);
+               res_colnames = list_concat(res_colnames, r_colnames);
+               res_colvars = list_concat(res_colvars, r_colvars);
 
                /*
                 * Check alias (AS clause), if any.
@@ -821,7 +885,7 @@ transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels)
                {
                        if (j->alias->colnames != NIL)
                        {
-                               if (length(j->alias->colnames) > length(res_colnames))
+                               if (list_length(j->alias->colnames) > list_length(res_colnames))
                                        ereport(ERROR,
                                                        (errcode(ERRCODE_SYNTAX_ERROR),
                                                         errmsg("column alias list for \"%s\" has too many entries",
@@ -840,13 +904,30 @@ transformFromClauseItem(ParseState *pstate, Node *n, List **containedRels)
                                                                                true);
 
                /* assume new rte is at end */
-               j->rtindex = length(pstate->p_rtable);
+               j->rtindex = list_length(pstate->p_rtable);
                Assert(rte == rt_fetch(j->rtindex, pstate->p_rtable));
 
+               *top_rte = rte;
+               *top_rti = j->rtindex;
+
                /*
-                * Include join RTE in returned containedRels list
+                * Prepare returned namespace list.  If the JOIN has an alias then it
+                * hides the contained RTEs as far as the relnamespace goes;
+                * otherwise, put the contained RTEs and *not* the JOIN into
+                * relnamespace.
                 */
-               *containedRels = lconsi(j->rtindex, my_containedRels);
+               if (j->alias)
+               {
+                       *relnamespace = list_make1(rte);
+                       list_free(my_relnamespace);
+               }
+               else
+                       *relnamespace = my_relnamespace;
+
+               /*
+                * Include join RTE in returned containedRels set
+                */
+               *containedRels = bms_add_member(my_containedRels, j->rtindex);
 
                return (Node *) j;
        }
@@ -876,8 +957,8 @@ buildMergedJoinVar(ParseState *pstate, JoinType jointype,
        outcoltypmod = l_colvar->vartypmod;
        if (outcoltype != r_colvar->vartype)
        {
-               outcoltype = select_common_type(makeListo2(l_colvar->vartype,
-                                                                                                  r_colvar->vartype),
+               outcoltype = select_common_type(list_make2_oid(l_colvar->vartype,
+                                                                                                          r_colvar->vartype),
                                                                                "JOIN/USING");
                outcoltypmod = -1;              /* ie, unknown */
        }
@@ -888,14 +969,14 @@ buildMergedJoinVar(ParseState *pstate, JoinType jointype,
        }
 
        /*
-        * Insert coercion functions if needed.  Note that a difference in
-        * typmod can only happen if input has typmod but outcoltypmod is -1.
-        * In that case we insert a RelabelType to clearly mark that result's
-        * typmod is not same as input.
+        * Insert coercion functions if needed.  Note that a difference in typmod
+        * can only happen if input has typmod but outcoltypmod is -1. In that
+        * case we insert a RelabelType to clearly mark that result's typmod is
+        * not same as input.  We never need coerce_type_typmod.
         */
        if (l_colvar->vartype != outcoltype)
                l_node = coerce_type(pstate, (Node *) l_colvar, l_colvar->vartype,
-                                                        outcoltype,
+                                                        outcoltype, outcoltypmod,
                                                         COERCION_IMPLICIT, COERCE_IMPLICIT_CAST);
        else if (l_colvar->vartypmod != outcoltypmod)
                l_node = (Node *) makeRelabelType((Expr *) l_colvar,
@@ -906,7 +987,7 @@ buildMergedJoinVar(ParseState *pstate, JoinType jointype,
 
        if (r_colvar->vartype != outcoltype)
                r_node = coerce_type(pstate, (Node *) r_colvar, r_colvar->vartype,
-                                                        outcoltype,
+                                                        outcoltype, outcoltypmod,
                                                         COERCION_IMPLICIT, COERCE_IMPLICIT_CAST);
        else if (r_colvar->vartypmod != outcoltypmod)
                r_node = (Node *) makeRelabelType((Expr *) r_colvar,
@@ -943,13 +1024,13 @@ buildMergedJoinVar(ParseState *pstate, JoinType jointype,
                case JOIN_FULL:
                        {
                                /*
-                                * Here we must build a COALESCE expression to ensure that
-                                * the join output is non-null if either input is.
+                                * Here we must build a COALESCE expression to ensure that the
+                                * join output is non-null if either input is.
                                 */
                                CoalesceExpr *c = makeNode(CoalesceExpr);
 
                                c->coalescetype = outcoltype;
-                               c->args = makeList2(l_node, r_node);
+                               c->args = list_make2(l_node, r_node);
                                res_node = (Node *) c;
                                break;
                        }
@@ -989,9 +1070,12 @@ transformWhereClause(ParseState *pstate, Node *clause,
 
 /*
  * transformLimitClause -
- *       Transform the expression and make sure it is of type integer.
+ *       Transform the expression and make sure it is of type bigint.
  *       Used for LIMIT and allied clauses.
  *
+ * Note: as of Postgres 8.2, LIMIT expressions are expected to yield int8,
+ * rather than int4 as before.
+ *
  * constructName does not affect the semantics, but is used in error messages
  */
 Node *
@@ -1005,12 +1089,10 @@ transformLimitClause(ParseState *pstate, Node *clause,
 
        qual = transformExpr(pstate, clause);
 
-       qual = coerce_to_integer(pstate, qual, constructName);
+       qual = coerce_to_specific_type(pstate, qual, INT8OID, constructName);
 
        /*
-        * LIMIT can't refer to any vars or aggregates of the current query;
-        * we don't allow subselects either (though that case would at least
-        * be sensible)
+        * LIMIT can't refer to any vars or aggregates of the current query
         */
        if (contain_vars_of_level(qual, 0))
        {
@@ -1028,14 +1110,6 @@ transformLimitClause(ParseState *pstate, Node *clause,
                                 errmsg("argument of %s must not contain aggregates",
                                                constructName)));
        }
-       if (contain_subplans(qual))
-       {
-               ereport(ERROR,
-                               (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
-               /* translator: %s is name of a SQL construct, eg LIMIT */
-                                errmsg("argument of %s must not contain sub-selects",
-                                               constructName)));
-       }
 
        return qual;
 }
@@ -1048,15 +1122,14 @@ transformLimitClause(ParseState *pstate, Node *clause,
  *       list as a "resjunk" node.
  *
  * node                the ORDER BY, GROUP BY, or DISTINCT ON expression to be matched
- * tlist       the existing target list (NB: this will never be NIL, which is a
- *                     good thing since we'd be unable to append to it if it were...)
- * clause      identifies clause type being processed.
+ * tlist       the target list (passed by reference so we can append to it)
+ * clause      identifies clause type being processed
  */
 static TargetEntry *
-findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause)
+findTargetlistEntry(ParseState *pstate, Node *node, List **tlist, int clause)
 {
        TargetEntry *target_result = NULL;
-       List       *tl;
+       ListCell   *tl;
        Node       *expr;
 
        /*----------
@@ -1099,35 +1172,41 @@ findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause)
         *----------
         */
        if (IsA(node, ColumnRef) &&
-               length(((ColumnRef *) node)->fields) == 1 &&
-               ((ColumnRef *) node)->indirection == NIL)
+               list_length(((ColumnRef *) node)->fields) == 1)
        {
-               char       *name = strVal(lfirst(((ColumnRef *) node)->fields));
+               char       *name = strVal(linitial(((ColumnRef *) node)->fields));
+               int                     location = ((ColumnRef *) node)->location;
 
                if (clause == GROUP_CLAUSE)
                {
                        /*
                         * In GROUP BY, we must prefer a match against a FROM-clause
-                        * column to one against the targetlist.  Look to see if there
-                        * is a matching column.  If so, fall through to let
-                        * transformExpr() do the rest.  NOTE: if name could refer
-                        * ambiguously to more than one column name exposed by FROM,
-                        * colnameToVar will ereport(ERROR).  That's just what we want
-                        * here.
+                        * column to one against the targetlist.  Look to see if there is
+                        * a matching column.  If so, fall through to let transformExpr()
+                        * do the rest.  NOTE: if name could refer ambiguously to more
+                        * than one column name exposed by FROM, colNameToVar will
+                        * ereport(ERROR).      That's just what we want here.
+                        *
+                        * Small tweak for 7.4.3: ignore matches in upper query levels.
+                        * This effectively changes the search order for bare names to (1)
+                        * local FROM variables, (2) local targetlist aliases, (3) outer
+                        * FROM variables, whereas before it was (1) (3) (2). SQL92 and
+                        * SQL99 do not allow GROUPing BY an outer reference, so this
+                        * breaks no cases that are legal per spec, and it seems a more
+                        * self-consistent behavior.
                         */
-                       if (colnameToVar(pstate, name) != NULL)
+                       if (colNameToVar(pstate, name, true, location) != NULL)
                                name = NULL;
                }
 
                if (name != NULL)
                {
-                       foreach(tl, tlist)
+                       foreach(tl, *tlist)
                        {
                                TargetEntry *tle = (TargetEntry *) lfirst(tl);
-                               Resdom     *resnode = tle->resdom;
 
-                               if (!resnode->resjunk &&
-                                       strcmp(resnode->resname, name) == 0)
+                               if (!tle->resjunk &&
+                                       strcmp(tle->resname, name) == 0)
                                {
                                        if (target_result != NULL)
                                        {
@@ -1135,12 +1214,11 @@ findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause)
                                                        ereport(ERROR,
                                                                        (errcode(ERRCODE_AMBIGUOUS_COLUMN),
 
-                                                       /*
-                                                        * translator: first %s is name of a SQL
-                                                        * construct, eg ORDER BY
-                                                        */
+                                                       /*------
+                                                         translator: first %s is name of a SQL construct, eg ORDER BY */
                                                                         errmsg("%s \"%s\" is ambiguous",
-                                                                                       clauseText[clause], name)));
+                                                                                       clauseText[clause], name),
+                                                                        parser_errposition(pstate, location)));
                                        }
                                        else
                                                target_result = tle;
@@ -1164,12 +1242,11 @@ findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause)
                                         errmsg("non-integer constant in %s",
                                                        clauseText[clause])));
                target_pos = intVal(val);
-               foreach(tl, tlist)
+               foreach(tl, *tlist)
                {
                        TargetEntry *tle = (TargetEntry *) lfirst(tl);
-                       Resdom     *resnode = tle->resdom;
 
-                       if (!resnode->resjunk)
+                       if (!tle->resjunk)
                        {
                                if (++targetlist_pos == target_pos)
                                        return tle; /* return the unique match */
@@ -1178,7 +1255,7 @@ findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause)
                ereport(ERROR,
                                (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
                /* translator: %s is name of a SQL construct, eg ORDER BY */
-                                errmsg("%s position %d is not in target list",
+                                errmsg("%s position %d is not in select list",
                                                clauseText[clause], target_pos)));
        }
 
@@ -1192,7 +1269,7 @@ findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause)
         */
        expr = transformExpr(pstate, node);
 
-       foreach(tl, tlist)
+       foreach(tl, *tlist)
        {
                TargetEntry *tle = (TargetEntry *) lfirst(tl);
 
@@ -1201,97 +1278,158 @@ findTargetlistEntry(ParseState *pstate, Node *node, List *tlist, int clause)
        }
 
        /*
-        * If no matches, construct a new target entry which is appended to
-        * the end of the target list.  This target is given resjunk = TRUE so
-        * that it will not be projected into the final tuple.
+        * If no matches, construct a new target entry which is appended to the
+        * end of the target list.      This target is given resjunk = TRUE so that it
+        * will not be projected into the final tuple.
         */
        target_result = transformTargetEntry(pstate, node, expr, NULL, true);
-       lappend(tlist, target_result);
+
+       *tlist = lappend(*tlist, target_result);
 
        return target_result;
 }
 
+static GroupClause *
+make_group_clause(TargetEntry *tle, List *targetlist,
+                                 Oid sortop, bool nulls_first)
+{
+       GroupClause *result;
+
+       result = makeNode(GroupClause);
+       result->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
+       result->sortop = sortop;
+       result->nulls_first = nulls_first;
+       return result;
+}
 
 /*
  * transformGroupClause -
  *       transform a GROUP BY clause
+ *
+ * GROUP BY items will be added to the targetlist (as resjunk columns)
+ * if not already present, so the targetlist must be passed by reference.
+ *
+ * The order of the elements of the grouping clause does not affect
+ * the semantics of the query. However, the optimizer is not currently
+ * smart enough to reorder the grouping clause, so we try to do some
+ * primitive reordering here.
  */
 List *
 transformGroupClause(ParseState *pstate, List *grouplist,
-                                        List *targetlist, List *sortClause)
+                                        List **targetlist, List *sortClause)
 {
-       List       *glist = NIL,
-                          *gl;
+       List       *result = NIL;
+       List       *tle_list = NIL;
+       ListCell   *l;
 
-       foreach(gl, grouplist)
+       /* Preprocess the grouping clause, lookup TLEs */
+       foreach(l, grouplist)
        {
                TargetEntry *tle;
                Oid                     restype;
-               Oid                     ordering_op;
-               GroupClause *grpcl;
 
-               tle = findTargetlistEntry(pstate, lfirst(gl),
+               tle = findTargetlistEntry(pstate, lfirst(l),
                                                                  targetlist, GROUP_CLAUSE);
 
-               /* avoid making duplicate grouplist entries */
-               if (targetIsInSortList(tle, glist))
-                       continue;
-
                /* if tlist item is an UNKNOWN literal, change it to TEXT */
-               restype = tle->resdom->restype;
+               restype = exprType((Node *) tle->expr);
 
                if (restype == UNKNOWNOID)
-               {
                        tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
-                                                                                        restype, TEXTOID,
+                                                                                        restype, TEXTOID, -1,
                                                                                         COERCION_IMPLICIT,
                                                                                         COERCE_IMPLICIT_CAST);
-                       restype = tle->resdom->restype = TEXTOID;
-                       tle->resdom->restypmod = -1;
+
+               tle_list = lappend(tle_list, tle);
+       }
+
+       /*
+        * Now iterate through the ORDER BY clause. If we find a grouping element
+        * that matches the ORDER BY element, append the grouping element to the
+        * result set immediately. Otherwise, stop iterating. The effect of this
+        * is to look for a prefix of the ORDER BY list in the grouping clauses,
+        * and to move that prefix to the front of the GROUP BY.
+        */
+       foreach(l, sortClause)
+       {
+               SortClause *sc = (SortClause *) lfirst(l);
+               ListCell   *prev = NULL;
+               ListCell   *tl;
+               bool            found = false;
+
+               foreach(tl, tle_list)
+               {
+                       TargetEntry *tle = (TargetEntry *) lfirst(tl);
+
+                       if (sc->tleSortGroupRef == tle->ressortgroupref)
+                       {
+                               GroupClause *gc;
+
+                               tle_list = list_delete_cell(tle_list, tl, prev);
+
+                               /* Use the sort clause's sorting information */
+                               gc = make_group_clause(tle, *targetlist,
+                                                                          sc->sortop, sc->nulls_first);
+                               result = lappend(result, gc);
+                               found = true;
+                               break;
+                       }
+
+                       prev = tl;
                }
 
+               /* As soon as we've failed to match an ORDER BY element, stop */
+               if (!found)
+                       break;
+       }
+
+       /*
+        * Now add any remaining elements of the GROUP BY list in the order we
+        * received them.
+        *
+        * XXX: are there any additional criteria to consider when ordering
+        * grouping clauses?
+        */
+       foreach(l, tle_list)
+       {
+               TargetEntry *tle = (TargetEntry *) lfirst(l);
+               GroupClause *gc;
+               Oid                     sort_op;
+
                /*
-                * If the GROUP BY clause matches the ORDER BY clause, we want to
-                * adopt the ordering operators from the latter rather than using
-                * the default ops.  This allows "GROUP BY foo ORDER BY foo DESC"
-                * to be done with only one sort step.  Note we are assuming that
-                * any user-supplied ordering operator will bring equal values
-                * together, which is all that GROUP BY needs.
+                * Avoid making duplicate grouplist entries.  Note that we don't
+                * enforce a particular sortop here.  Along with the copying of sort
+                * information above, this means that if you write something like
+                * "GROUP BY foo ORDER BY foo USING <<<", the GROUP BY operation
+                * silently takes on the equality semantics implied by the ORDER BY.
                 */
-               if (sortClause &&
-                       ((SortClause *) lfirst(sortClause))->tleSortGroupRef ==
-                       tle->resdom->ressortgroupref)
-               {
-                       ordering_op = ((SortClause *) lfirst(sortClause))->sortop;
-                       sortClause = lnext(sortClause);
-               }
-               else
-               {
-                       ordering_op = ordering_oper_opid(restype);
-                       sortClause = NIL;       /* disregard ORDER BY once match fails */
-               }
+               if (targetIsInSortList(tle, InvalidOid, result))
+                       continue;
 
-               grpcl = makeNode(GroupClause);
-               grpcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
-               grpcl->sortop = ordering_op;
-               glist = lappend(glist, grpcl);
+               sort_op = ordering_oper_opid(exprType((Node *) tle->expr));
+               gc = make_group_clause(tle, *targetlist, sort_op, false);
+               result = lappend(result, gc);
        }
 
-       return glist;
+       list_free(tle_list);
+       return result;
 }
 
 /*
  * transformSortClause -
  *       transform an ORDER BY clause
+ *
+ * ORDER BY items will be added to the targetlist (as resjunk columns)
+ * if not already present, so the targetlist must be passed by reference.
  */
 List *
 transformSortClause(ParseState *pstate,
                                        List *orderlist,
-                                       List *targetlist,
+                                       List **targetlist,
                                        bool resolveUnknown)
 {
        List       *sortlist = NIL;
-       List       *olitem;
+       ListCell   *olitem;
 
        foreach(olitem, orderlist)
        {
@@ -1302,8 +1440,9 @@ transformSortClause(ParseState *pstate,
                                                                  targetlist, ORDER_CLAUSE);
 
                sortlist = addTargetToSortList(pstate, tle,
-                                                                          sortlist, targetlist,
-                                                                          sortby->sortby_kind,
+                                                                          sortlist, *targetlist,
+                                                                          sortby->sortby_dir,
+                                                                          sortby->sortby_nulls,
                                                                           sortby->useOp,
                                                                           resolveUnknown);
        }
@@ -1315,216 +1454,262 @@ transformSortClause(ParseState *pstate,
  * transformDistinctClause -
  *       transform a DISTINCT or DISTINCT ON clause
  *
- * Since we may need to add items to the query's sortClause list, that list
- * is passed by reference.     We might also need to add items to the query's
- * targetlist, but we assume that cannot be empty initially, so we can
- * lappend to it even though the pointer is passed by value.
+ * Since we may need to add items to the query's targetlist, that list
+ * is passed by reference.
+ *
+ * As with GROUP BY, we absorb the sorting semantics of ORDER BY as much as
+ * possible into the distinctClause.  This avoids a possible need to re-sort,
+ * and allows the user to determine the equality semantics used by DISTINCT,
+ * should she be working with a datatype that has more than one btree equality
+ * operator.
  */
 List *
 transformDistinctClause(ParseState *pstate, List *distinctlist,
-                                               List *targetlist, List **sortClause)
+                                               List **targetlist, List *sortClause)
 {
        List       *result = NIL;
-       List       *slitem;
-       List       *dlitem;
+       ListCell   *slitem;
+       ListCell   *dlitem;
+       ListCell   *tlitem;
 
        /* No work if there was no DISTINCT clause */
        if (distinctlist == NIL)
                return NIL;
 
-       if (lfirst(distinctlist) == NIL)
+       if (linitial(distinctlist) == NULL)
        {
                /* We had SELECT DISTINCT */
 
                /*
-                * All non-resjunk elements from target list that are not already
-                * in the sort list should be added to it.      (We don't really care
-                * what order the DISTINCT fields are checked in, so we can leave
-                * the user's ORDER BY spec alone, and just add additional sort
-                * keys to it to ensure that all targetlist items get sorted.)
-                */
-               *sortClause = addAllTargetsToSortList(pstate,
-                                                                                         *sortClause,
-                                                                                         targetlist,
-                                                                                         true);
-
-               /*
-                * Now, DISTINCT list consists of all non-resjunk sortlist items.
-                * Actually, all the sortlist items had better be non-resjunk!
-                * Otherwise, user wrote SELECT DISTINCT with an ORDER BY item
-                * that does not appear anywhere in the SELECT targetlist, and we
-                * can't implement that with only one sorting pass...
+                * The distinctClause should consist of all ORDER BY items followed
+                * by all other non-resjunk targetlist items.  There must not be any
+                * resjunk ORDER BY items --- that would imply that we are sorting
+                * by a value that isn't necessarily unique within a DISTINCT group,
+                * so the results wouldn't be well-defined.  This construction
+                * ensures we follow the rule that sortClause and distinctClause match;
+                * in fact the sortClause will always be a prefix of distinctClause.
+                *
+                * Note a corner case: the same TLE could be in the ORDER BY list
+                * multiple times with different sortops.  We have to include it in
+                * the distinctClause the same way to preserve the prefix property.
+                * The net effect will be that the TLE value will be made unique
+                * according to both sortops.
                 */
-               foreach(slitem, *sortClause)
+               foreach(slitem, sortClause)
                {
                        SortClause *scl = (SortClause *) lfirst(slitem);
-                       TargetEntry *tle = get_sortgroupclause_tle(scl, targetlist);
+                       TargetEntry *tle = get_sortgroupclause_tle(scl, *targetlist);
 
-                       if (tle->resdom->resjunk)
+                       if (tle->resjunk)
                                ereport(ERROR,
                                                (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
-                                                errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in target list")));
+                                                errmsg("for SELECT DISTINCT, ORDER BY expressions must appear in select list")));
                        else
                                result = lappend(result, copyObject(scl));
                }
+
+               /*
+                * Now add any remaining non-resjunk tlist items, using default
+                * sorting semantics for their data types.
+                */
+               foreach(tlitem, *targetlist)
+               {
+                       TargetEntry *tle = (TargetEntry *) lfirst(tlitem);
+
+                       if (tle->resjunk)
+                               continue;               /* ignore junk */
+                       if (targetIsInSortList(tle, InvalidOid, result))
+                               continue;               /* already in list (with some semantics) */
+                       result = addTargetToSortList(pstate, tle,
+                                                                                result, *targetlist,
+                                                                                SORTBY_DEFAULT,
+                                                                                SORTBY_NULLS_DEFAULT,
+                                                                                NIL, true);
+               }
        }
        else
        {
                /* We had SELECT DISTINCT ON (expr, ...) */
+               Bitmapset  *refnos = NULL;
+               int                     sortgroupref;
+               bool            skipped_sortitem;
 
                /*
-                * If the user writes both DISTINCT ON and ORDER BY, then the two
-                * expression lists must match (until one or the other runs out).
-                * Otherwise the ORDER BY requires a different sort order than the
-                * DISTINCT does, and we can't implement that with only one sort
-                * pass (and if we do two passes, the results will be rather
-                * unpredictable). However, it's OK to have more DISTINCT ON
-                * expressions than ORDER BY expressions; we can just add the
-                * extra DISTINCT values to the sort list, much as we did above
-                * for ordinary DISTINCT fields.
-                *
-                * Actually, it'd be OK for the common prefixes of the two lists to
-                * match in any order, but implementing that check seems like more
-                * trouble than it's worth.
+                * Add all the DISTINCT ON expressions to the tlist (if not already
+                * present, they are added as resjunk items).  Assign sortgroupref
+                * numbers to them, and form a bitmapset of these numbers.  (A
+                * bitmapset is convenient here because we don't care about order
+                * and we can discard duplicates.)
                 */
-               List       *nextsortlist = *sortClause;
-
                foreach(dlitem, distinctlist)
                {
+                       Node   *dexpr = (Node *) lfirst(dlitem);
                        TargetEntry *tle;
 
-                       tle = findTargetlistEntry(pstate, lfirst(dlitem),
+                       tle = findTargetlistEntry(pstate, dexpr,
                                                                          targetlist, DISTINCT_ON_CLAUSE);
+                       sortgroupref = assignSortGroupRef(tle, *targetlist);
+                       refnos = bms_add_member(refnos, sortgroupref);
+               }
 
-                       if (nextsortlist != NIL)
-                       {
-                               SortClause *scl = (SortClause *) lfirst(nextsortlist);
+               /*
+                * If the user writes both DISTINCT ON and ORDER BY, adopt the
+                * sorting semantics from ORDER BY items that match DISTINCT ON
+                * items, and also adopt their column sort order.  We insist that
+                * the distinctClause and sortClause match, so throw error if we
+                * find the need to add any more distinctClause items after we've
+                * skipped an ORDER BY item that wasn't in DISTINCT ON.
+                */
+               skipped_sortitem = false;
+               foreach(slitem, sortClause)
+               {
+                       SortClause *scl = (SortClause *) lfirst(slitem);
 
-                               if (tle->resdom->ressortgroupref != scl->tleSortGroupRef)
+                       if (bms_is_member(scl->tleSortGroupRef, refnos))
+                       {
+                               if (skipped_sortitem)
                                        ereport(ERROR,
                                                        (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
                                                         errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions")));
-                               result = lappend(result, copyObject(scl));
-                               nextsortlist = lnext(nextsortlist);
+                               else
+                                       result = lappend(result, copyObject(scl));
                        }
                        else
                        {
-                               *sortClause = addTargetToSortList(pstate, tle,
-                                                                                                 *sortClause, targetlist,
-                                                                                                 SORTBY_ASC, NIL, true);
+                               skipped_sortitem = true;
+                       }
+               }
 
-                               /*
-                                * Probably, the tle should always have been added at the
-                                * end of the sort list ... but search to be safe.
-                                */
-                               foreach(slitem, *sortClause)
-                               {
-                                       SortClause *scl = (SortClause *) lfirst(slitem);
+               /*
+                * Now add any remaining DISTINCT ON items, using default sorting
+                * semantics for their data types.  (Note: this is pretty
+                * questionable; if the ORDER BY list doesn't include all the DISTINCT
+                * ON items and more besides, you certainly aren't using DISTINCT ON
+                * in the intended way, and you probably aren't going to get
+                * consistent results.  It might be better to throw an error or warning
+                * here.  But historically we've allowed it, so keep doing so.)
+                */
+               while ((sortgroupref = bms_first_member(refnos)) >= 0)
+               {
+                       TargetEntry *tle = get_sortgroupref_tle(sortgroupref, *targetlist);
 
-                                       if (tle->resdom->ressortgroupref == scl->tleSortGroupRef)
-                                       {
-                                               result = lappend(result, copyObject(scl));
-                                               break;
-                                       }
-                               }
-                               if (slitem == NIL)              /* should not happen */
-                                       elog(ERROR, "failed to add DISTINCT ON clause to target list");
-                       }
+                       if (targetIsInSortList(tle, InvalidOid, result))
+                               continue;               /* already in list (with some semantics) */
+                       if (skipped_sortitem)
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
+                                                errmsg("SELECT DISTINCT ON expressions must match initial ORDER BY expressions")));
+                       result = addTargetToSortList(pstate, tle,
+                                                                                result, *targetlist,
+                                                                                SORTBY_DEFAULT,
+                                                                                SORTBY_NULLS_DEFAULT,
+                                                                                NIL, true);
                }
        }
 
        return result;
 }
 
-/*
- * addAllTargetsToSortList
- *             Make sure all non-resjunk targets in the targetlist are in the
- *             ORDER BY list, adding the not-yet-sorted ones to the end of the list.
- *             This is typically used to help implement SELECT DISTINCT.
- *
- * See addTargetToSortList for info about pstate and resolveUnknown inputs.
- *
- * Returns the updated ORDER BY list.
- */
-List *
-addAllTargetsToSortList(ParseState *pstate, List *sortlist,
-                                               List *targetlist, bool resolveUnknown)
-{
-       List       *i;
-
-       foreach(i, targetlist)
-       {
-               TargetEntry *tle = (TargetEntry *) lfirst(i);
-
-               if (!tle->resdom->resjunk)
-                       sortlist = addTargetToSortList(pstate, tle,
-                                                                                  sortlist, targetlist,
-                                                                                  SORTBY_ASC, NIL,
-                                                                                  resolveUnknown);
-       }
-       return sortlist;
-}
-
 /*
  * addTargetToSortList
- *             If the given targetlist entry isn't already in the ORDER BY list,
- *             add it to the end of the list, using the sortop with given name
- *             or the default sort operator if opname == NIL.
+ *             If the given targetlist entry isn't already in the SortClause list,
+ *             add it to the end of the list, using the given sort ordering info.
  *
  * If resolveUnknown is TRUE, convert TLEs of type UNKNOWN to TEXT.  If not,
  * do nothing (which implies the search for a sort operator will fail).
  * pstate should be provided if resolveUnknown is TRUE, but can be NULL
  * otherwise.
  *
- * Returns the updated ORDER BY list.
+ * Returns the updated SortClause list.
  */
 List *
 addTargetToSortList(ParseState *pstate, TargetEntry *tle,
                                        List *sortlist, List *targetlist,
-                                       int sortby_kind, List *sortby_opname,
-                                       bool resolveUnknown)
+                                       SortByDir sortby_dir, SortByNulls sortby_nulls,
+                                       List *sortby_opname, bool resolveUnknown)
 {
+       Oid                     restype = exprType((Node *) tle->expr);
+       Oid                     sortop;
+       Oid                     cmpfunc;
+       bool            reverse;
+
+       /* if tlist item is an UNKNOWN literal, change it to TEXT */
+       if (restype == UNKNOWNOID && resolveUnknown)
+       {
+               tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
+                                                                                restype, TEXTOID, -1,
+                                                                                COERCION_IMPLICIT,
+                                                                                COERCE_IMPLICIT_CAST);
+               restype = TEXTOID;
+       }
+
+       /* determine the sortop */
+       switch (sortby_dir)
+       {
+               case SORTBY_DEFAULT:
+               case SORTBY_ASC:
+                       sortop = ordering_oper_opid(restype);
+                       reverse = false;
+                       break;
+               case SORTBY_DESC:
+                       sortop = reverse_ordering_oper_opid(restype);
+                       reverse = true;
+                       break;
+               case SORTBY_USING:
+                       Assert(sortby_opname != NIL);
+                       sortop = compatible_oper_opid(sortby_opname,
+                                                                                 restype,
+                                                                                 restype,
+                                                                                 false);
+
+                       /*
+                        * Verify it's a valid ordering operator, and determine whether to
+                        * consider it like ASC or DESC.
+                        */
+                       if (!get_compare_function_for_ordering_op(sortop,
+                                                                                                         &cmpfunc, &reverse))
+                               ereport(ERROR,
+                                               (errcode(ERRCODE_WRONG_OBJECT_TYPE),
+                                          errmsg("operator %s is not a valid ordering operator",
+                                                         strVal(llast(sortby_opname))),
+                                                errhint("Ordering operators must be \"<\" or \">\" members of btree operator families.")));
+                       break;
+               default:
+                       elog(ERROR, "unrecognized sortby_dir: %d", sortby_dir);
+                       sortop = InvalidOid;    /* keep compiler quiet */
+                       reverse = false;
+                       break;
+       }
+
        /* avoid making duplicate sortlist entries */
-       if (!targetIsInSortList(tle, sortlist))
+       if (!targetIsInSortList(tle, sortop, sortlist))
        {
                SortClause *sortcl = makeNode(SortClause);
-               Oid                     restype = tle->resdom->restype;
-
-               /* if tlist item is an UNKNOWN literal, change it to TEXT */
-               if (restype == UNKNOWNOID && resolveUnknown)
-               {
-                       tle->expr = (Expr *) coerce_type(pstate, (Node *) tle->expr,
-                                                                                        restype, TEXTOID,
-                                                                                        COERCION_IMPLICIT,
-                                                                                        COERCE_IMPLICIT_CAST);
-                       restype = tle->resdom->restype = TEXTOID;
-                       tle->resdom->restypmod = -1;
-               }
 
                sortcl->tleSortGroupRef = assignSortGroupRef(tle, targetlist);
 
-               switch (sortby_kind)
+               sortcl->sortop = sortop;
+
+               switch (sortby_nulls)
                {
-                       case SORTBY_ASC:
-                               sortcl->sortop = ordering_oper_opid(restype);
+                       case SORTBY_NULLS_DEFAULT:
+                               /* NULLS FIRST is default for DESC; other way for ASC */
+                               sortcl->nulls_first = reverse;
                                break;
-                       case SORTBY_DESC:
-                               sortcl->sortop = reverse_ordering_oper_opid(restype);
+                       case SORTBY_NULLS_FIRST:
+                               sortcl->nulls_first = true;
                                break;
-                       case SORTBY_USING:
-                               Assert(sortby_opname != NIL);
-                               sortcl->sortop = compatible_oper_opid(sortby_opname,
-                                                                                                         restype,
-                                                                                                         restype,
-                                                                                                         false);
+                       case SORTBY_NULLS_LAST:
+                               sortcl->nulls_first = false;
                                break;
                        default:
-                               elog(ERROR, "unrecognized sortby_kind: %d", sortby_kind);
+                               elog(ERROR, "unrecognized sortby_nulls: %d", sortby_nulls);
                                break;
                }
 
                sortlist = lappend(sortlist, sortcl);
        }
+
        return sortlist;
 }
 
@@ -1539,47 +1724,60 @@ Index
 assignSortGroupRef(TargetEntry *tle, List *tlist)
 {
        Index           maxRef;
-       List       *l;
+       ListCell   *l;
 
-       if (tle->resdom->ressortgroupref)       /* already has one? */
-               return tle->resdom->ressortgroupref;
+       if (tle->ressortgroupref)       /* already has one? */
+               return tle->ressortgroupref;
 
        /* easiest way to pick an unused refnumber: max used + 1 */
        maxRef = 0;
        foreach(l, tlist)
        {
-               Index           ref = ((TargetEntry *) lfirst(l))->resdom->ressortgroupref;
+               Index           ref = ((TargetEntry *) lfirst(l))->ressortgroupref;
 
                if (ref > maxRef)
                        maxRef = ref;
        }
-       tle->resdom->ressortgroupref = maxRef + 1;
-       return tle->resdom->ressortgroupref;
+       tle->ressortgroupref = maxRef + 1;
+       return tle->ressortgroupref;
 }
 
 /*
  * targetIsInSortList
  *             Is the given target item already in the sortlist?
+ *             If sortop is not InvalidOid, also test for a match to the sortop.
+ *
+ * It is not an oversight that this function ignores the nulls_first flag.
+ * We check sortop when determining if an ORDER BY item is redundant with
+ * earlier ORDER BY items, because it's conceivable that "ORDER BY
+ * foo USING <, foo USING <<<" is not redundant, if <<< distinguishes
+ * values that < considers equal.  We need not check nulls_first
+ * however, because a lower-order column with the same sortop but
+ * opposite nulls direction is redundant.  Also, we can consider
+ * ORDER BY foo ASC, foo DESC redundant, so check for a commutator match.
  *
  * Works for both SortClause and GroupClause lists.  Note that the main
  * reason we need this routine (and not just a quick test for nonzeroness
  * of ressortgroupref) is that a TLE might be in only one of the lists.
  */
 bool
-targetIsInSortList(TargetEntry *tle, List *sortList)
+targetIsInSortList(TargetEntry *tle, Oid sortop, List *sortList)
 {
-       Index           ref = tle->resdom->ressortgroupref;
-       List       *i;
+       Index           ref = tle->ressortgroupref;
+       ListCell   *l;
 
        /* no need to scan list if tle has no marker */
        if (ref == 0)
                return false;
 
-       foreach(i, sortList)
+       foreach(l, sortList)
        {
-               SortClause *scl = (SortClause *) lfirst(i);
+               SortClause *scl = (SortClause *) lfirst(l);
 
-               if (scl->tleSortGroupRef == ref)
+               if (scl->tleSortGroupRef == ref &&
+                       (sortop == InvalidOid ||
+                        sortop == scl->sortop ||
+                        sortop == get_commutator(scl->sortop)))
                        return true;
        }
        return false;