OSDN Git Service

Change target version to PG11 and fix the SPEC file
[pghintplan/pg_hint_plan.git] / doc / pg_hint_plan.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD html 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2 <html lang="en_US">
3 <head>
4 <title>pg_hint_plan</title>
5 <!-- Uncoment after the tool has been hosted somewhere.
6 <link rel="home" title="pg_hint_plan" href="index.html">
7 -->
8 <link rel="stylesheet" type="text/css" href="style.css">
9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
10 <!--
11 <style type="text/css">
12 h2 { border-style:solid none none none; margin-top: 2em; }
13 h3 { margin-top: 2em; }
14 dt { margin-top: 0.5em; margin-bottom: 0.3em;}
15 pre { margin: 0 2em 0 2em; padding:0.3em; border-style: solid; border-width:0.1em; border-color: #97A69F; background-color: #DEFFEF; }
16 </style>
17 -->
18 </head>
19
20 <body>
21 <h1 id="pg_hint_plan">pg_hint_plan 1.3</h1>
22 <div class="navigation">
23   <a href="pg_hint_plan-ja.html">pg_hint_plan</a>
24 </div>
25 <hr>
26
27 <div class="index">
28 <ol>
29 <li><a href="#name">Name</a></li>
30 <li><a href="#synopsis">Synopsis</a></li>
31 <li><a href="#description">Description</a>
32 <li><a href="#install">Installation</a></li>
33 <li><a href="#uninstall">Uninstallation</a></li>
34 <li><a href="#examples">Hint descriptions</a></li>
35 <li><a href="#hint_syntax">Hint syntax</a></li>
36 <li><a href="#restrictions">Restrictions</a></li>
37 <li><a href="#techniques">Techniques to hint on disired targets</a></li>
38 <li><a href="#errors">Errors of hints</a></li>
39 <li><a href="#func_limits">Functional limitations</a></li>
40 <li><a href="#requirement">Requirements</a></li>
41 <li><a href="#seealso">See Also</a></li>
42 <li><a href="hint_list.html">Appendix A. Hints list</a></li>
43 </div>
44
45 <h2 id="name">Name</h2>
46 <p>pg_hint_plan -- controls execution plan with hinting phrases in comment of special form.</p>
47
48 <h2 id="synopsis">Synopsis</h2>
49 <p>PostgreSQL uses cost based optimizer, which utilizes data statistics, not static rules. The planner (optimizer) esitimates costs of each possible execution plans for a SQL statement then the execution plan with the lowest cost finally be executed.  The planner does its best to select the best best execution plan, but not perfect, since it doesn't count some properties of the data, for example, correlation between columns.</p>
50 <p>pg_hint_plan makes it possible to tweak execution plans using so-called "hints", which are simple descriptions in the SQL comment of special form.</p>
51
52 <h2 id="description">Description</h2>
53
54 <h3 id="hint-rule">Basic Usage</h3>
55 <p>pg_hint_plan reads hinting phrases in a comment of special form given with the target SQL statement. The special form is beginning by the character sequence "/*+" and ends with "*/". Hint phrases are consists of hint name and following parameters enclosed by parentheses and delimited by spaces. Each hinting phrases can be delimited by new lines for readability.</p>
56
57 <p>In the example below , hash join is selected as the joning method and scanning pgbench_accounts by sequential scan method.</p>
58 <pre>
59 postgres=# /*+
60 postgres*#    <b><u>HashJoin(a b)</u></b>
61 postgres*#    <b><u>>SeqScan(a)</u></b>
62 postgres*#  */
63 postgres-# EXPLAIN SELECT *
64 postgres-#    FROM pgbench_branches b
65 postgres-#    JOIN pgbench_accounts a ON b.bid = a.bid
66 postgres-#   ORDER BY a.aid;
67                                       QUERY PLAN
68 ---------------------------------------------------------------------------------------
69  Sort  (cost=31465.84..31715.84 rows=100000 width=197)
70    Sort Key: a.aid
71    ->  <b><u>Hash Join</u></b>  (cost=1.02..4016.02 rows=100000 width=197)
72          Hash Cond: (a.bid = b.bid)
73          ->  <b><u>Seq Scan on pgbench_accounts a</u></b>  (cost=0.00..2640.00 rows=100000 width=97)
74          ->  Hash  (cost=1.01..1.01 rows=1 width=100)
75                ->  Seq Scan on pgbench_branches b  (cost=0.00..1.01 rows=1 width=100)
76 (7 rows)
77
78 postgres=# </pre>
79
80 <h2 id="examples">Hint table</h2>
81 <p> The above section mentioned that
82 hints are placed in a comment with special form. This way of hinting
83 is inconvenient for certain cases where queries cannot be edited. For
84 such cases, hints can be placed in a special table named
85 "hint_plan.hints". The table consists of the following columns.</p>
86 <table>
87 <thead>
88 <tr>
89 <tr><th>column</th><th>description</th></tr>
90 </tr></thead>
91 <tbody>
92 <tr><td>id</td>
93   <td>Unique number to identify a row for a hint. This column is filled automatically by sequence.</td></tr>
94 <tr><td>norm_query_string</td>
95   <td>A pattern matches to the query to be hinted. Constants in the query have to be replace with '?' as in the following example. White space is significant in the pattern.</td></tr>
96 <tr><td>application_name</td>
97   <td>The value of application_name of sessions to apply the hint. The hint in the example below applies to sessions connected from psql. An empty string means sessions of any application_name.</td></tr>
98 <tr><td>hints</td>
99   <td>Hint phrase. This must be a series of hints excluding surrounding comment marks.</td></tr>
100 </tbody>
101 </table>
102
103 <p>The following example shows how to operate with the hint table.</p>
104 <pre>
105 <b>postgres=#</b> INSERT INTO hint_plan.hints(norm_query_string, application_name, hints)
106 <b>postgres-#</b>     VALUES (
107 <b>postgres(#</b>         'EXPLAIN (COSTS false) SELECT * FROM t1 WHERE t1.id = ?;',
108 <b>postgres(#</b>         '',
109 <b>postgres(#</b>         'SeqScan(t1)'
110 <b>postgres(#</b>     );
111 INSERT 0 1
112 <b>postgres=#</b> UPDATE hint_plan.hints
113 <b>postgres-#</b>    SET hints = 'IndexScan(t1)'
114 <b>postgres-#</b>  WHERE id = 1;
115 UPDATE 1
116 <b>postgres=#</b> DELETE FROM hint_plan.hints
117 <b>postgres-#</b>  WHERE id = 1;
118 DELETE 1
119 <b>postgres=#</b>
120 </pre>
121 <p>The hint tables is owned by the creator user and having the default previledges  at the time of creation.
122 during CREATE EXTENSION. Table hints are prioritized than comment hits.</p>
123
124 <h3 id="hint-group">The types of hints</h3>
125 <p>Hinting phrases are classified into six types based on what kind of object and how they can affect planning. Scaning methods, join methods, joining order, row number correction, parallel query and GUC setting. You will see the lists of hint phrases of each type in <a href="hint_list.html">Hint list</a>.</p>
126
127 <h4>Hints for scan methods </h4>
128 <p>Scan method hints enforce specific scanning method on the target table. pg_hint_plan recognizes the target table by alias names if any. They are 'SeqScan' , 'IndexScan' and so on in this kind of hint.</p>
129 <p>Scan hints are effective on ordinary tables, inheritance tables, UNLOGGED tables, temporary tables and  system catalogs. External(foreign) tables, table functions, VALUES clause, CTEs, views and subquiries are not affected.</p>
130 <pre>
131 <b>postgres=# /*+</b>
132 <b>postgres*#     SeqScan(t1)</b>
133 <b>postgres*#     IndexScan(t2 t2_pkey)</b>
134 <b>postgres*#  */</b>
135 <b>postgres-#</b> SELECT * FROM table1 t1 JOIN table table2 t2 ON (t1.key = t2.key);
136 </pre>
137
138
139 <h4>Hints for join methods</h4>
140 <p>Join method hints enforce the join methods of the joins involving specified tables. </p>
141 <p>This can affect on joins only on ordinary tables, inheritance tables, UNLOGGED tables, temporary tables, external (foreign) tables, system catalogs, table functions, VALUES command results and CTEs are allowed to be in the parameter list. But joins on views and sub query are not affected.</p>
142
143 <h4>Hint for joining order</h4>
144 <p> This hint "Leading" enforces the order of join on two or more tables. There are two ways of enforcing. One is enforcing specific order of joining but not restricting direction at each join level. Another enfoces join direction additionaly. Details are seen in the <a href="hint_list.html">hint list</a> table.</p>
145
146 <pre>
147 <b>postgres=# /*+</b>
148 <b>postgres*#     NestLoop(t1 t2)</b>
149 <b>postgres*#     MergeJoin(t1 t2 t3)</b>
150 <b>postgres*#     Leading(t1 t2 t3)</b>
151 <b>postgres*#  */</b>
152 <b>postgres-#</b> SELECT * FROM table1 t1
153 <b>postgres-#</b>     JOIN table table2 t2 ON (t1.key = t2.key)
154 <b>postgres-#</b>     JOIN table table3 t3 ON (t2.key = t3.key);
155 </pre>
156
157 <h4>Hint for row number correction</h4>
158 <p>This hint "Rows" corrects row number misestimation of joins that comes from restrictions of the planner. </p>
159
160 <pre>
161 <b>postgres=# /*+ Rows(a b #10) */</b> SELECT... ; Sets rows of join result to 10
162 <b>postgres=# /*+ Rows(a b +10) */</b> SELECT... ; Increments row number by 10
163 <b>postgres=# /*+ Rows(a b -10) */</b> SELECT... ; Subtracts 10 from the row number.
164 <b>postgres=# /*+ Rows(a b *10) */</b> SELECT... ; Makes the number 10 times larger.
165 </pre>
166
167 <h4>Hint for parallel plan</h4>
168 <p>This hint "Parallel" enforces parallel execution configuration on
169 scans. The third parameter specifies the strength of enfocement. "soft" means that pg_hint_plan only changes max_parallel_worker_per_gather and leave all others to planner. "hard" changes other planner parameters so as to forcibly apply the number. This can affect on ordinary tables, inheritnce parents, unlogged
170 tables and system catalogues. External tables, table functions, values clause, CTEs, views and subqueries are not affected. Internal tables of a view can be specified by its real name/alias as the target object. The following example shows that the query is enforced differently on each table.</p>
171
172 <pre>
173 <b>postgres=#</b> explain <b>/*+ Parallel(c1 3 hard) Parallel(c2 5 hard) */</b>
174        SELECT c2.a FROM c1 JOIN c2 ON (c1.a = c2.a);
175                                   QUERY PLAN                                   
176 -------------------------------------------------------------------------------
177  Hash Join  (cost=2.86..11406.38 rows=101 width=4)
178    Hash Cond: (c1.a = c2.a)
179    ->  Gather  (cost=0.00..7652.13 rows=1000101 width=4)
180          <b><u>Workers Planned: 3</u></b>
181          ->  Parallel Seq Scan on <b><u>c1</u></b>  (cost=0.00..7652.13 rows=322613 width=4)
182    ->  Hash  (cost=1.59..1.59 rows=101 width=4)
183          ->  Gather  (cost=0.00..1.59 rows=101 width=4)
184                <b><u>Workers Planned: 5</u></b>
185                ->  Parallel Seq Scan on <b><u>c2</u></b>  (cost=0.00..1.59 rows=59 width=4)
186
187 <b>postgres=#</b> EXPLAIN <b>/*+ Parallel(tl 5 hard) */</b> SELECT sum(a) FROM tl;
188                                     QUERY PLAN                                  
189 -----------------------------------------------------------------------------------
190  <b><u>Finalize Aggregate</u></b>  (cost=693.02..693.03 rows=1 width=8)
191    ->  Gather  (cost=693.00..693.01 rows=5 width=8)
192          <b><u>Workers Planned: 5</u></b>
193          ->  <b><u>Partial Aggregate</u></b>  (cost=693.00..693.01 rows=1 width=8)
194                ->  Parallel Seq Scan on tl  (cost=0.00..643.00 rows=20000 width=4)
195 </pre>
196 </dd>
197
198
199 </pre>
200
201
202 <h4>GUC parameters temporarily setting</h4>
203 <p>'Set' hint changes GUC parameters just while planning. GUC parameter shown in <a href="http://www.postgresql.org/docs/current/static/runtime-config-query.html">Query Planning</a> can have the expected effects on planning unless any other hint conflicts with the planner method configuration parameters. The last one among hints on the same GUC parameter makes effect. <a href="#hint-GUC">GUC parameters for pg_hint_plan</a> are also settable by  this hint but it won't work as your expectation. See <a href="#restrictions">Restrictions</a> for details.</p>
204 <pre>
205 postgres=# <b>/*+ Set(random_page_cost 2.0) */</b>
206 postgres-# SELECT * FROM table1 t1 WHERE key = 'value';
207 ...
208 </pre>
209
210 <h3 id="hint-GUC">GUC parameters for pg_hint_plan</h3>
211 <p>GUC parameters below affect the behavior of pg_hint_planpg_hint_plan.</p>
212 <table>
213 <thead>
214 <tr>
215 <tr><th>Parameter name</th><th>discription</th><th>Default</th></tr>
216 </tr></thead>
217 <tbody>
218 <tr><td>pg_hint_plan.enable_hint</td>
219   <td>True enbles pg_hint_plan.</td><td>on</td></tr>
220 <tr><td>pg_hint_plan.enable_hint_table</td>
221   <td>True enbles hinting by table. true or false.</td><td>off</td></tr>
222 <tr><td>pg_hint_plan.parse_messages</td>
223   <td>Specifies the log level of hint parse error. Valid values are error, warning, notice, info, log, debug<n>.</td><td>INFO</td></tr>
224 <tr><td>pg_hint_plan.debug_print</td>
225   <td>Controls debug print and verbosity. Valid vaiues are off, on, detailed and verbose.</td><td>off</td></tr>
226 <tr><td>pg_hint_plan.message_level</td>
227   <td>Specifies message level of debug print. Valid values are error, warning, notice, info, log, debug<n>.</td><td>INFO</td></tr>
228 </tbody>
229 </table>
230
231 <h2 id="install">Installation</h2>
232 This section describes the installation steps.
233 <h3 id="build">building binary module</h3>
234 <p>Simplly run "make" in the top of the source tree, then "make install" as appropriate user. The PATH environment variable should be set properly for the target PostgreSQL for this process. </p>
235 <pre>
236 $ tar xzvf pg_hint_plan-1.x.x.tar.gz
237 $ cd pg_hint_plan-1.x.x
238 $ make
239 $ su
240 # make install
241 </pre>
242
243 <h3 id="hint-load">Loding pg_hint_plan</h3>
244 <p>Basically pg_hint_plan does not requires CREATE EXTENSION. Simplly loading it by LOAD command will activate it and of course you can load it globally by setting shared_preload_libraries in postgresql.conf. Or you might be interested in ALTER USER SET/ALTER DATABASE SET for automatic loading for specific sessions. 
245 <pre>
246 postgres=# LOAD 'pg_hint_plan';
247 LOAD
248 postgres=# </pre></p>
249
250 <p>Do CREATE EXTENSION and SET pg_hint_plan.enable_hint_tables TO on if you are planning to hint tables.</p>
251
252
253 <h2 id="uninstall">Unistallation</h2>
254 <p>"make uninstall" in the top directory of source tree will uninstall the installed files if you installed from the source tree and it is left available. </p>
255
256 <pre>
257 $ cd pg_hint_plan-1.x.x
258 $ su
259 # make uninstall
260 </pre>
261
262 <h2 id="hint_syntax">Details in hinting</h2>
263 <h3>Syntax and placement</h3>
264 <dd>pg_hint_plan reads hints from only the first block comment and any characters except alphabets, digits, spaces, underscores, commas and parentheses stops parsing immediately. In the following example HashJoin(a b) and SeqScan(a) are parsed as hints but IndexScan(a) and MergeJoin(a b) are not.
265
266 <pre>
267 <b>postgres=# /*+</b>
268 <b>postgres*#    HashJoin(a b)</b>
269 <b>postgres*#    SeqScan(a)</b>
270 <b>postgres*#  */</b>
271 <b>postgres-# /*+ IndexScan(a) */</b>
272 <b>postgres-#</b> EXPLAIN SELECT <b>/*+ MergeJoin(a b) */</b> *
273 <b>postgres-#</b>    FROM pgbench_branches b
274 <b>postgres-#</b>    JOIN pgbench_accounts a ON b.bid = a.bid
275 <b>postgres-#</b>   ORDER BY a.aid;
276                                       QUERY PLAN
277 ---------------------------------------------------------------------------------------
278  Sort  (cost=31465.84..31715.84 rows=100000 width=197)
279    Sort Key: a.aid
280    ->  <b><u>Hash Join</u></b>  (cost=1.02..4016.02 rows=100000 width=197)
281          Hash Cond: (a.bid = b.bid)
282          ->  <b><u>Seq Scan on pgbench_accounts a</u></b>  (cost=0.00..2640.00 rows=100000 width=97)
283          ->  Hash  (cost=1.01..1.01 rows=1 width=100)
284                ->  Seq Scan on pgbench_branches b  (cost=0.00..1.01 rows=1 width=100)
285 (7 rows)
286
287 postgres=# </pre>
288 </dd>
289
290 <h3>Using with PL/pgSQL</h3>
291 <dd>pg_hint_plan works for queries in PL/pgSQL scripts with some restrictions.
292 <ul>
293  <li>Hints affect only on the following kind of queires.
294  <ul>
295   <li>Queries that returns one row. (SELECT, INSERT, UPDATE and DELETE)</li>
296   <li>Queries that returns multiple rows. (RETURN QUERY)</li>
297   <li>Dynamic SQL statements. (EXECUTE)</li>
298   <li>Cursor open. (OPEN)</li>
299   <li>Loop over result of a query (FOR)</li>
300  </ul>
301
302  <li>A hint comment have to be placed after the first word in a query
303      as the following since preceding comments are not sent as a part
304      of the query.</li>
305 </ul>
306 <pre>
307 postgres=# CREATE FUNCTION hints_func(integer) RETURNS integer AS $$
308 postgres$# DECLARE
309 postgres$#     id  integer;
310 postgres$#     cnt integer;
311 postgres$# BEGIN
312 postgres$#     SELECT <b><u>/*+ NoIndexScan(a) */</u></b> aid
313 postgres$#         INTO id FROM pgbench_accounts a WHERE aid = $1;
314 postgres$#     SELECT <b><u>/*+ SeqScan(a) */</u></b> count(*)
315 postgres$#         INTO cnt FROM pgbench_accounts a;
316 postgres$#     RETURN id + cnt;
317 postgres$# END;
318 postgres$# $$ LANGUAGE plpgsql;
319 </pre>
320 </dd>
321
322 <h3>Letter case in a hinted object</h3>
323 <dd>Unlike the way PostgreSQL handles object names, pg_hint_plan compares bare object names in hints against the database internal object names in case sensitive way. Therefore an object name TBL in a hint matches only "TBL" in database and does not match any unquoted names like TBL, tbl or Tbl.
324 </dd>
325
326 <h3>Escaping special chacaters in object names</h3>
327 <dd>The objects as the hint parameter should be enclosed by double quotes if they includes parentheses, double quotes and white spaces. The escaping rule is the same as PostgreSQL.
328 </dd>
329
330 <h3>Distinction between multiple occurances of a table</h3>
331 <dd>pg_hint_plan identifies the target object by using aliases if
332 exists. This behavior is usable to point a specific occurance
333 among multiple occurances of one table.
334 <pre>
335 <b>postgres=# /*+ HashJoin(t1 t1) */</b>
336 <b>postgres-#</b> EXPLAIN SELECT * FROM s1.t1
337 <b>postgres-#</b> JOIN public.t1 ON (s1.t1.id=public.t1.id);
338 INFO:  hint syntax error at or near "HashJoin(t1 t1)"
339 <b><u>DETAIL:  Relation name "t1" is ambiguous.</u></b>
340 ...
341 <b>postgres=# /*+ HashJoin(pt st) */</b>
342 <b>postgres-#</b> EXPLAIN SELECT * FROM s1.t1 st
343 <b>postgres-#</b> JOIN public.t1 pt ON (st.id=pt.id);
344                              QUERY PLAN
345 ---------------------------------------------------------------------
346  <span class="strong">Hash Join</span>  (cost=64.00..1112.00 rows=28800 width=8)
347    Hash Cond: (st.id = pt.id)
348    ->  Seq Scan on t1 st  (cost=0.00..34.00 rows=2400 width=4)
349    ->  Hash  (cost=34.00..34.00 rows=2400 width=4)
350          ->  Seq Scan on t1 pt  (cost=0.00..34.00 rows=2400 width=4)
351 </pre>
352 </dd>
353
354 <h3>Underlying tables of views or rules</h3>
355 <dd>Hints are not applicable on views itself, but they can affect the
356 queries within if the object names match the object names in the
357 expanded query on the view. Assigning aliases to the tables in a view
358 enables them to be manipulated from outside the view.
359 <pre>
360 <b>postgres=#</b> CREATE VIEW v1 AS SELECT * FROM <b><u>t2</u></b>;
361 <b>postgres=#</b> EXPLAIN <b>/*+ HashJoin(t1 v1) */</b>
362           SELECT * FROM t1 JOIN v1 ON (c1.a = v1.a);
363                             QUERY PLAN                            
364 ------------------------------------------------------------------
365  Hash Join  (cost=3.27..18181.67 rows=101 width=8)
366    Hash Cond: (t1.a = t2.a)
367    ->  Seq Scan on t1  (cost=0.00..14427.01 rows=1000101 width=4)
368    ->  Hash  (cost=2.01..2.01 rows=101 width=4)
369          ->  Seq Scan on t2  (cost=0.00..2.01 rows=101 width=4)
370 </pre>
371 </dd>
372
373 <h3>Inheritance tables</h3>
374 <dd>Hints can point only the parent of an inheritance tables and the
375 hint affect all the inheritance. Hints simultaneously point directly
376 to children are not in effect.
377 </dd>
378
379 <h3>Hinting on multistatements</h3>
380 <dd>One multistatement can have exactly one hint comment and the hints affects all of the individual statement in the multistatement. Notice that the seemingly multistatement on the interactive interface of psql is internally a sequence of single statements so hints affects only on the statement just following.</dd>
381
382 <h3>VALUES expressions</h3>
383 <dd>VALUES expressions in FROM clause are named as *VALUES* internally
384 so it is hintable if it is the only VALUES in a query. Two or more
385 VALUES expressions in a query seems distinguishable looking its
386 explain result. But in reality it is mere a cosmetic and they are not
387 distinguisable.
388 <pre>
389 <b>postgres=# /*+ MergeJoin(*VALUES*_1 *VALUES*) */</b>
390       EXPLAIN SELECT * FROM (VALUES (1, 1), (2, 2)) v (a, b)
391       JOIN (VALUES (1, 5), (2, 8), (3, 4)) w (a, c) ON v.a = w.a;
392 INFO:  pg_hint_plan: hint syntax error at or near "MergeJoin(*VALUES*_1 *VALUES*) "
393 <b><u>DETAIL:  Relation name "*VALUES*" is ambiguous.</u></b>
394                                QUERY PLAN                                
395 -------------------------------------------------------------------------
396  Hash Join  (cost=0.05..0.12 rows=2 width=16)
397    Hash Cond: ("*VALUES*_1".column1 = "*VALUES*".column1)
398    ->  Values Scan on "*VALUES*_1"  (cost=0.00..0.04 rows=3 width=8)
399    ->  Hash  (cost=0.03..0.03 rows=2 width=8)
400          ->  Values Scan on "*VALUES*"  (cost=0.00..0.03 rows=2 width=8)
401 </pre>
402 </dd>
403
404 <h3>Subqueries</h3>
405 <dd>
406 <p>Subqueries in the following context occasionally can be hinted using the
407 name "ANY_subquery".</p>
408 <pre>
409 IN (SELECT ... {<b>LIMIT | OFFSET</b> ...} ...)
410 = ANY (SELECT ... {<b>LIMIT | OFFSET</b> ...} ...)
411 = SOME (SELECT ... {<b>LIMIT | OFFSET</b> ...} ...)
412 </pre>
413 <p>For these syntaxes, planner internally assigns the name to the subquery when planning joins on tables including it, so join hints are applicable on such joins using the implicit name as the following.</p>
414 <pre>
415 <b>postgres=# /*+HashJoin(a1 ANY_subquery)*/</b>
416 <b>postgres=#</b> EXPLAIN SELECT *
417 <b>postgres=#</b>    FROM pgbench_accounts a1
418 <b>postgres=#</b>   WHERE aid IN (SELECT bid FROM pgbench_accounts a2 LIMIT 10);
419                                          QUERY PLAN
420
421 ---------------------------------------------------------------------------------------------
422  <b><u>Hash Semi Join</u></b>  (cost=0.49..2903.00 rows=1 width=97)
423    Hash Cond: (a1.aid = a2.bid)
424    ->  Seq Scan on pgbench_accounts a1  (cost=0.00..2640.00 rows=100000 width=97)
425    ->  Hash  (cost=0.36..0.36 rows=10 width=4)
426          ->  Limit  (cost=0.00..0.26 rows=10 width=4)
427                ->  Seq Scan on pgbench_accounts a2  (cost=0.00..2640.00 rows=100000 width=4)
428 </pre>
429 </dd>
430
431 <h3>Using IndexOnlyScan hint</h3>
432 <dd>An additional hint to enforce an index that can perform index only scan is required for IndexOnlyScan hint to work when any index that can not perform index only scan is also defined on the same table. </dd>
433
434 <h3>Behavior of NoIndexScan</h3>
435 <dd>NoIndexScan hint involes NoIndexOnlyScan.</dd>
436
437 <h3>Parallel hint and UNION</h3>
438 <dd>A UNION can run in parallel only when all underlying subqueries
439 are parallel-safe. Conversely enforcing parallel on any of
440 the subqueries let a parallel-executable UNION run in
441 parallel. Meanwhile, a parallel hint with zero workers hinhibits a scan
442 from executed in parallel.</dd>
443
444 <h3>Setting pg_hint_plan parameters by Set hints</h3>
445 <dd><p>pg_hint_plan paramters change the behavior of itself so some parameters doesn't work as expected.</p>
446 <ul>
447 <li>Hints to change enable_hint, enable_hint_tables are ignored even though they are reported as "used hints" in debug logs.</li>
448 <li>Setting debug_print and message_level works from midst of the processing of the target query.</li>
449 </ul>
450 </dd>
451
452
453 <h2 id="errors">Errors</h2>
454 <p>pg_hint_plan stops parsing on any error and uses hints already parsed on the most cases. Followings are the typical errors.</p>
455 <h3>Syntax errors </h3>
456 <p>Any syntactical errors or wrong hint names are reported as an syntax error. These errors are reported in the server log with the message level which specified by pg_hint_plan.message_level if pg_hint_plan.debug_print is on and aboves.</p>
457 <h3>Object misspecifications</h3>
458 <p>Object misspecifications results silent ingorance of the hints. This kind of error is reported as "not used hints" in the server log by the same condtion to syntax errors.</p>
459
460 <h3>Redundant or conflicting hints</h3>
461 <p>The last hint will be active when redundant hints or hints conflicting with each other. This kind of error is reported as "duplication hints" in the server log by the same condition to syntax errors.</p>
462
463 <h3>Nested comments</h3>
464 <p>Hint comment cannot include another block comment within. If pg_hint_plan finds it, differently from other erros, it stops parsing and abandans all hints already parsed. This kind of error is reported in the same manner as other errors. </p>
465
466 <h2 id="func_limits">Functional limitations</h2>
467 <h3>Influences of some of planner GUC parameters</h3>
468 <p>The planner does not try to consider joining order for FROM clause entries more than from_collapse_limit. pg_hint_plan cannot affect joining order as expected for the case.</p>
469
470 <h3>Cases that pg_hint_plan essentially does not work </h3>
471 <p>By the nature of pg_hint_plan, it cannot affect some cases that out of scope of the planner like following.</p>
472 <ul>
473 <li>FULL OUTER JOIN to use nested loop</li>
474 <li>To use indexes that does not have columns used in quals</li>
475 <li>To do TID scans for queries without ctid conditions</li>
476 </ul>
477
478 <h3>Queries in ECPG</h3>
479 <p>ECPG removes comments in queries written as embedded SQLs so hints cannot be passed form those queries. The only exception is that EXECUTE command passes given string unmodifed. Please consider hint tables for this case.</p>
480
481 <h3>Work with pg_stat_statements</h3>
482 <p>pg_stat_statements generates a query id ignoring comments. As the result the identical queires with different hints are summerized as the same query.</p>
483
484 <h2 id="requirement">Requirements</h2>
485 pg_hint_plan 1.3 requires PostgreSQL 10.
486 <dl>
487 <dt>PostgreSQL versions tested</dt>
488   <dd>Version 10</dd>
489 <dt>OS versions tested</dt>
490   <dd>CentOS 7.4</dd>
491 </dl>
492
493 <h2 id="seealso">See also</h2>
494 <h3 id="postgresql_document">PostgreSQL documents</h3>
495 <a href="http://www.postgresql.org/docs/current/static/sql-explain.html">EXPLAIN</a>
496 <a href="http://www.postgresql.org/docs/current/static/sql-set.html">SET</a>
497 <a href="http://www.postgresql.org/docs/current/static/runtime-config.html">Server Config</a>
498 <a href="http://www.postgresql.org/docs/current/static/parallel-plans.html">Parallel Plans</a>
499 <hr>
500
501 <div class="navigation">
502   <a href="pg_hint_plan.html">pg_hint_plan</a>
503 </div>
504
505 <p class="footer">Copyright (c) 2012-2018, NIPPON TELEGRAPH AND TELEPHONE CORPORATION</p>
506 </body>
507 </html>