Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitccad6d6

Browse files
author
Thomas G. Lockhart
committed
Writeup from Tom Lane on how costs are estimated.
1 parent99281cf commitccad6d6

File tree

1 file changed

+236
-0
lines changed

1 file changed

+236
-0
lines changed

‎doc/src/sgml/indexcost.sgml

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
<chapter>
2+
<title>Index Cost Estimation Functions</title>
3+
4+
<note>
5+
<title>Author</title>
6+
7+
<para>
8+
Written by <ulink url="mailto:tgl@sss.pgh.pa.us">Tom Lane</ulink>
9+
on 2000-01-24.
10+
</para>
11+
</note>
12+
13+
<!--
14+
I have written the attached bit of doco about the new index cost
15+
estimator procedure definition, but I am not sure where to put it.
16+
There isn't (AFAICT) any existing documentation about how to make
17+
a new kind of index, which would be the proper place for it.
18+
May I impose on you to find/make a place for this and mark it up
19+
properly?
20+
21+
Also, doc/src/graphics/catalogs.ag needs to be updated, but I have
22+
no idea how. (The amopselect and amopnpages fields of pg_amop
23+
are gone; pg_am has a new field amcostestimate.)
24+
25+
regards, tom lane
26+
-->
27+
28+
<para>
29+
Every index access method must provide a cost estimation function for
30+
use by the planner/optimizer. The procedure OID of this function is
31+
given in the <literal>amcostestimate</literal> field of the access
32+
method's <literal>pg_am</literal> entry.
33+
34+
<note>
35+
<para>
36+
Prior to Postgres 7.0, a different scheme was used for registering
37+
index-specific cost estimation functions.
38+
</para>
39+
</note>
40+
</para>
41+
42+
<para>
43+
The amcostestimate function is given a list of WHERE clauses that have
44+
been determined to be usable with the index. It must return estimates
45+
of the cost of accessing the index and the selectivity of the WHERE
46+
clauses (that is, the fraction of main-table tuples that will be
47+
retrieved during the index scan). For simple cases, nearly all the
48+
work of the cost estimator can be done by calling standard routines
49+
in the optimizer; the point of having an amcostestimate function is
50+
to allow index access methods to provide index-type-specific knowledge,
51+
in case it is possible to improve on the standard estimates.
52+
</para>
53+
54+
<para>
55+
Each amcostestimate function must have the signature:
56+
57+
<programlisting>
58+
void
59+
amcostestimate (Query *root,
60+
RelOptInfo *rel,
61+
IndexOptInfo *index,
62+
List *indexQuals,
63+
Cost *indexAccessCost,
64+
Selectivity *indexSelectivity);
65+
</programlisting>
66+
67+
The first four parameters are inputs:
68+
69+
<variablelist>
70+
<varlistentry>
71+
<term>root</term>
72+
<listitem>
73+
<para>
74+
The query being processed.
75+
</para>
76+
</listitem>
77+
</varlistentry>
78+
79+
<varlistentry>
80+
<term>rel</term>
81+
<listitem>
82+
<para>
83+
The relation the index is on.
84+
</para>
85+
</listitem>
86+
</varlistentry>
87+
88+
<varlistentry>
89+
<term>index</term>
90+
<listitem>
91+
<para>
92+
The index itself.
93+
</para>
94+
</listitem>
95+
</varlistentry>
96+
97+
<varlistentry>
98+
<term>indexQuals</term>
99+
<listitem>
100+
<para>
101+
List of index qual clauses (implicitly ANDed);
102+
a NIL list indicates no qualifiers are available.
103+
</para>
104+
</listitem>
105+
</varlistentry>
106+
</variablelist>
107+
</para>
108+
109+
<para>
110+
The last two parameters are pass-by-reference outputs:
111+
112+
<variablelist>
113+
<varlistentry>
114+
<term>*indexAccessCost</term>
115+
<listitem>
116+
<para>
117+
Set to cost of index processing.
118+
</para>
119+
</listitem>
120+
</varlistentry>
121+
122+
<varlistentry>
123+
<term>*indexSelectivity</term>
124+
<listitem>
125+
<para>
126+
Set to index selectivity
127+
</para>
128+
</listitem>
129+
</varlistentry>
130+
</variablelist>
131+
</para>
132+
133+
<para>
134+
Note that cost estimate functions must be written in C, not in SQL or
135+
any available procedural language, because they must access internal
136+
data structures of the planner/optimizer.
137+
</para>
138+
139+
<para>
140+
The indexAccessCost should be computed in the units used by
141+
src/backend/optimizer/path/costsize.c: a disk block fetch has cost 1.0,
142+
and the cost of processing one index tuple should usually be taken as
143+
cpu_index_page_weight (which is a user-adjustable optimizer parameter).
144+
The access cost should include all disk and CPU costs associated with
145+
scanning the index itself, but NOT the cost of retrieving or processing
146+
the main-table tuples that are identified by the index.
147+
</para>
148+
149+
<para>
150+
The indexSelectivity should be set to the estimated fraction of the main
151+
table tuples that will be retrieved during the index scan. In the case
152+
of a lossy index, this will typically be higher than the fraction of
153+
tuples that actually pass the given qual conditions.
154+
</para>
155+
156+
<procedure>
157+
<title>Cost Estimation</title>
158+
<para>
159+
A typical cost estimator will proceed as follows:
160+
</para>
161+
162+
<step>
163+
<para>
164+
Estimate and return the fraction of main-table tuples that will be visited
165+
based on the given qual conditions. In the absence of any index-type-specific
166+
knowledge, use the standard optimizer function clauselist_selec():
167+
168+
<programlisting>
169+
*indexSelectivity = clauselist_selec(root, indexQuals);
170+
</programlisting>
171+
</para>
172+
</step>
173+
174+
<step>
175+
<para>
176+
Estimate the number of index tuples that will be visited during the
177+
scan. For many index types this is the same as indexSelectivity times
178+
the number of tuples in the index, but it might be more. (Note that the
179+
index's size in pages and tuples is available from the IndexOptInfo struct.)
180+
</para>
181+
</step>
182+
183+
<step>
184+
<para>
185+
Estimate the number of index pages that will be retrieved during the scan.
186+
This might be just indexSelectivity times the index's size in pages.
187+
</para>
188+
</step>
189+
190+
<step>
191+
<para>
192+
Compute the index access cost as
193+
194+
<programlisting>
195+
*indexAccessCost = numIndexPages + cpu_index_page_weight * numIndexTuples;
196+
</programlisting>
197+
</para>
198+
</step>
199+
</procedure>
200+
201+
<para>
202+
Examples of cost estimator functions can be found in
203+
<filename>src/backend/utils/adt/selfuncs.c</filename>.
204+
</para>
205+
206+
<para>
207+
By convention, the <literal>pg_proc</literal> entry for an
208+
<literal>amcostestimate</literal> function should show
209+
210+
<programlisting>
211+
prorettype = 0
212+
pronargs = 6
213+
proargtypes = 0 0 0 0 0 0
214+
</programlisting>
215+
216+
We use zero ("opaque") for all the arguments since none of them have types
217+
that are known in pg_type.
218+
</para>
219+
</chapter>
220+
221+
<!-- Keep this comment at the end of the file
222+
Local variables:
223+
mode:sgml
224+
sgml-omittag:nil
225+
sgml-shorttag:t
226+
sgml-minimize-attributes:nil
227+
sgml-always-quote-attributes:t
228+
sgml-indent-step:1
229+
sgml-indent-data:t
230+
sgml-parent-document:nil
231+
sgml-default-dtd-file:"./reference.ced"
232+
sgml-exposed-tags:nil
233+
sgml-local-catalogs:("/usr/lib/sgml/CATALOG")
234+
sgml-local-ecat-files:nil
235+
End:
236+
-->

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp