Skip to content

Commit 6771220

Browse files
committed
* Hack for non-functional btree npages estimation:
* npages = index_pages * selectivity_of_1st_attr_clause(s)
1 parent 72d2711 commit 6771220

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

src/backend/optimizer/util/plancat.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/plancat.c,v 1.5 1997/04/09 01:52:04 vadim Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/optimizer/util/plancat.c,v 1.6 1997/04/24 16:07:14 vadim Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -499,6 +499,8 @@ IndexSelectivity(Oid indexrelid,
499499
float64data npages, select;
500500
float64 amopnpages, amopselect;
501501
Oid relam;
502+
bool nphack = false;
503+
float64data fattr_select = 1.0;
502504

503505
indRel = SearchSysCacheTuple(RELOID,
504506
ObjectIdGetDatum(indexrelid),
@@ -515,6 +517,15 @@ IndexSelectivity(Oid indexrelid,
515517
elog(WARN, "IndexSelectivity: index %d not found",
516518
indexrelid);
517519
index = (IndexTupleForm)GETSTRUCT(indexTuple);
520+
521+
/*
522+
* Hack for non-functional btree npages estimation:
523+
* npages = index_pages * selectivity_of_1st_attr_clause(s)
524+
* - vadim 04/24/97
525+
*/
526+
if ( relam == BTREE_AM_OID &&
527+
varAttributeNumbers[0] != InvalidAttrNumber )
528+
nphack = true;
518529

519530
npages = 0.0;
520531
select = 1.0;
@@ -556,7 +567,10 @@ IndexSelectivity(Oid indexrelid,
556567
elog(WARN, "IndexSelectivity: no amop %d %d",
557568
indclass, operatorObjectIds[n]);
558569
amop = (Form_pg_amop)GETSTRUCT(amopTuple);
559-
amopnpages = (float64) fmgr(amop->amopnpages,
570+
571+
if ( !nphack )
572+
{
573+
amopnpages = (float64) fmgr(amop->amopnpages,
560574
(char *) operatorObjectIds[n],
561575
(char *) indrelid,
562576
(char *) varAttributeNumbers[n],
@@ -573,8 +587,9 @@ IndexSelectivity(Oid indexrelid,
573587
if ((i = npages) < npages) /* ceil(npages)? */
574588
npages += 1.0;
575589
#endif
576-
npages += PointerIsValid(amopnpages) ? *amopnpages : 0.0;
577-
590+
npages += PointerIsValid(amopnpages) ? *amopnpages : 0.0;
591+
}
592+
578593
amopselect = (float64) fmgr(amop->amopselect,
579594
(char *) operatorObjectIds[n],
580595
(char *) indrelid,
@@ -583,15 +598,27 @@ IndexSelectivity(Oid indexrelid,
583598
(char *) constFlags[n],
584599
(char *) nIndexKeys,
585600
(char *) indexrelid);
601+
602+
if ( nphack && varAttributeNumbers[n] == index->indkey[0] )
603+
fattr_select *= PointerIsValid(amopselect) ? *amopselect : 1.0;
604+
586605
select *= PointerIsValid(amopselect) ? *amopselect : 1.0;
587606
}
588607
/*
589608
* Estimation of npages below is hack of course, but it's
590609
* better than it was before. - vadim 04/09/97
591610
*/
592-
if ( nIndexKeys > 1 )
593-
npages = npages / (1.0 + nIndexKeys);
594-
*idxPages = ceil ((double)(npages/nIndexKeys));
611+
if ( nphack )
612+
{
613+
npages = fattr_select * ((Form_pg_class)GETSTRUCT(indRel))->relpages;
614+
*idxPages = ceil ((double)npages);
615+
}
616+
else
617+
{
618+
if ( nIndexKeys > 1 )
619+
npages = npages / (1.0 + nIndexKeys);
620+
*idxPages = ceil ((double)(npages/nIndexKeys));
621+
}
595622
*idxSelec = select;
596623
}
597624

0 commit comments

Comments
 (0)