Skip to content

Commit ca3d14f

Browse files
committed
Tweak set_rel_width() to avoid redundant executions of getrelid().
In very large queries this accounts for a noticeable fraction of planning time. Per an example from Greg Stark.
1 parent 8073fff commit ca3d14f

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/backend/optimizer/path/costsize.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
* Portions Copyright (c) 1994, Regents of the University of California
5555
*
5656
* IDENTIFICATION
57-
* $PostgreSQL: pgsql/src/backend/optimizer/path/costsize.c,v 1.179 2007/03/27 23:21:09 tgl Exp $
57+
* $PostgreSQL: pgsql/src/backend/optimizer/path/costsize.c,v 1.180 2007/04/21 02:41:13 tgl Exp $
5858
*
5959
*-------------------------------------------------------------------------
6060
*/
@@ -2357,12 +2357,22 @@ set_rel_width(PlannerInfo *root, RelOptInfo *rel)
23572357
{
23582358
int32 tuple_width = 0;
23592359
ListCell *tllist;
2360+
Oid rel_reloid;
2361+
2362+
/*
2363+
* Usually (perhaps always), all the Vars have the same reloid, so we can
2364+
* save some redundant list-searching by doing getrelid just once.
2365+
*/
2366+
if (rel->relid > 0)
2367+
rel_reloid = getrelid(rel->relid, root->parse->rtable);
2368+
else
2369+
rel_reloid = InvalidOid; /* probably can't happen */
23602370

23612371
foreach(tllist, rel->reltargetlist)
23622372
{
23632373
Var *var = (Var *) lfirst(tllist);
23642374
int ndx;
2365-
Oid relid;
2375+
Oid var_reloid;
23662376
int32 item_width;
23672377

23682378
/* For now, punt on whole-row child Vars */
@@ -2383,10 +2393,14 @@ set_rel_width(PlannerInfo *root, RelOptInfo *rel)
23832393
continue;
23842394
}
23852395

2386-
relid = getrelid(var->varno, root->parse->rtable);
2387-
if (relid != InvalidOid)
2396+
if (var->varno == rel->relid)
2397+
var_reloid = rel_reloid;
2398+
else
2399+
var_reloid = getrelid(var->varno, root->parse->rtable);
2400+
2401+
if (var_reloid != InvalidOid)
23882402
{
2389-
item_width = get_attavgwidth(relid, var->varattno);
2403+
item_width = get_attavgwidth(var_reloid, var->varattno);
23902404
if (item_width > 0)
23912405
{
23922406
rel->attr_widths[ndx] = item_width;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

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

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


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy