Skip to content

Commit 7df721a

Browse files
committed
Compute reasonable cost and output-row-count estimates for LIMIT plan
nodes.
1 parent 37c55f9 commit 7df721a

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/backend/optimizer/plan/createplan.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.101 2000/11/16 22:30:24 tgl Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/createplan.c,v 1.102 2000/12/23 18:49:41 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -1733,6 +1733,49 @@ make_limit(List *tlist, Plan *lefttree,
17331733

17341734
copy_plan_costsize(plan, lefttree);
17351735

1736+
/*
1737+
* If offset/count are constants, adjust the output rows count and costs
1738+
* accordingly. This is only a cosmetic issue if we are at top level,
1739+
* but if we are building a subquery then it's important to report
1740+
* correct info to the outer planner.
1741+
*/
1742+
if (limitOffset && IsA(limitOffset, Const))
1743+
{
1744+
Const *limito = (Const *) limitOffset;
1745+
int32 offset = DatumGetInt32(limito->constvalue);
1746+
1747+
if (!limito->constisnull && offset > 0)
1748+
{
1749+
if (offset > plan->plan_rows)
1750+
offset = (int32) plan->plan_rows;
1751+
if (plan->plan_rows > 0)
1752+
plan->startup_cost +=
1753+
(plan->total_cost - plan->startup_cost)
1754+
* ((double) offset) / plan->plan_rows;
1755+
plan->plan_rows -= offset;
1756+
if (plan->plan_rows < 1)
1757+
plan->plan_rows = 1;
1758+
}
1759+
}
1760+
if (limitCount && IsA(limitCount, Const))
1761+
{
1762+
Const *limitc = (Const *) limitCount;
1763+
int32 count = DatumGetInt32(limitc->constvalue);
1764+
1765+
if (!limitc->constisnull && count >= 0)
1766+
{
1767+
if (count > plan->plan_rows)
1768+
count = (int32) plan->plan_rows;
1769+
if (plan->plan_rows > 0)
1770+
plan->total_cost = plan->startup_cost +
1771+
(plan->total_cost - plan->startup_cost)
1772+
* ((double) count) / plan->plan_rows;
1773+
plan->plan_rows = count;
1774+
if (plan->plan_rows < 1)
1775+
plan->plan_rows = 1;
1776+
}
1777+
}
1778+
17361779
plan->state = (EState *) NULL;
17371780
plan->targetlist = tlist;
17381781
plan->qual = NIL;

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