Is it possible and worthy to optimize scanRTEForColumn()? - Mailing list pgsql-hackers

From Rui Hai Jiang
Subject Is it possible and worthy to optimize scanRTEForColumn()?
Date
Msg-id SG2PR02MB1102919F35405ABC8AFD7FFCA0300@SG2PR02MB1102.apcprd02.prod.outlook.com
Whole thread Raw
Responses Re: Is it possible and worthy to optimize scanRTEForColumn()?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers

Hello,

 

When I run a select query, e.g. select id from t,  all columns in table "t" are checked to see if a column named "id" exists or not, and a Var is created for "id" if the column does exist.

 

Function scanRTEForColumn() does this job.

 

But I see in scanRTEForColumn(), the loop does not stop when a match is found, it continues to compare all other columns. And this will waste lots of computing.

 

 

I guess there may be some reasons for this. But I don't know yet.  I just wonder if it is possible and worthy to optimize this. And please note, "select *" does not call this function.

 

 

Node *

scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, char *colname,

                                 int location, int fuzzy_rte_penalty,

                                 FuzzyAttrMatchState *fuzzystate)

{

      foreach(c, rte->eref->colnames)

        {

                const char *attcolname = strVal(lfirst(c));

                attnum++;

                if (strcmp(attcolname, colname) == 0)

                {

                         if (result)

                                ereport(ERROR,

                                                (errcode(ERRCODE_AMBIGUOUS_COLUMN),

                                                 errmsg("column reference \"%s\" is ambiguous",

                                                                colname),

                                                 parser_errposition(pstate, location)));

                        var = make_var(pstate, rte, attnum, location);

                        /* Require read access to the column */

                        markVarForSelectPriv(pstate, var, rte);

                        result = (Node *) var;

                }

 

                /* Updating fuzzy match state, if provided. */

                if (fuzzystate != NULL)

                        updateFuzzyAttrMatchState(fuzzy_rte_penalty, fuzzystate,

                                                                          rte, attcolname, colname, attnum);

        }

 

        /*

         * If we have a unique match, return it.  Note that this allows a user

         * alias to override a system column name (such as OID) without error.

         */

        if (result)

                return result;

 

....................

.....................

}

pgsql-hackers by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: Speeding up pg_upgrade
Next
From: Sergei Kornilov
Date:
Subject: Re: proposal: alternative psql commands quit and exit