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

Commit2395a03

Browse files
committed
Converted documentation of sr_plan to sgml and integrated it into main tree
1 parent24572d3 commit2395a03

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

‎doc/src/sgml/contrib.sgml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ CREATE EXTENSION <replaceable>module_name</> FROM unpackaged;
137137
&seg;
138138
&sepgsql;
139139
&contrib-spi;
140+
&sr-plan;
140141
&sslinfo;
141142
&tablefunc;
142143
&tcn;

‎doc/src/sgml/filelist.sgml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<!ENTITY seg SYSTEM "seg.sgml">
142142
<!ENTITY contrib-spi SYSTEM "contrib-spi.sgml">
143143
<!ENTITY sepgsql SYSTEM "sepgsql.sgml">
144+
<!ENTITY sr-plan SYSTEM "sr_plan.sgml">
144145
<!ENTITY sslinfo SYSTEM "sslinfo.sgml">
145146
<!ENTITY tablefunc SYSTEM "tablefunc.sgml">
146147
<!ENTITY tcn SYSTEM "tcn.sgml">

‎doc/src/sgml/sr_plan.sgml

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<!-- doc/src/sgml/sr_plan.sgml -->
2+
3+
<sect1 id="sr-plan" xreflabel="sr-plan">
4+
<title>sr_plan</title>
5+
<indexterm zone="sr-plan">
6+
<primary>sr_plan</primary>
7+
</indexterm>
8+
<sect2 id="rationale">
9+
<title>Rationale</title>
10+
<para>
11+
sr_plan is an extension which allows to save query execution plans
12+
and use these plans for all repetitions of same query, instead of
13+
optimizing identical query again and again/
14+
</para>
15+
<para>
16+
sr_plan looks like Oracle Outline system. It can be used to lock
17+
the execution plan. It is necessary if you do not trust the
18+
planner or able to form a better plan.
19+
</para>
20+
<para>
21+
Typically, DBA would play with queries interactively, and save
22+
their plans and then enable use of saved plans for the queries,
23+
where predictable responce time is essential.
24+
</para>
25+
<para>
26+
Then application which uses these queries would use saved plans.
27+
</para>
28+
</sect2>
29+
<sect2>
30+
<title>Installation</title>
31+
<para>
32+
In your db:
33+
</para>
34+
<programlisting >
35+
CREATE EXTENSION sr_plan;
36+
</programlisting>
37+
<para>
38+
and modify your postgresql.conf:
39+
</para>
40+
<programlisting>
41+
shared_preload_libraries = 'sr_plan.so'
42+
</programlisting>
43+
<para>
44+
It is essential that library is preloaded during server startup,
45+
because use of saved plans is enabled on per-database basis and
46+
doesn't require any per-connection actions.
47+
</para>
48+
</sect2>
49+
<sect2>
50+
<title>Usage</title>
51+
<para>
52+
If you want to save the query plan is necessary to set the
53+
variable:
54+
</para>
55+
<programlisting >
56+
set sr_plan.write_mode = true;
57+
</programlisting>
58+
<para>
59+
Now plans for all subsequent queries will be stored in the table
60+
sr_plans, until this variable is set to false. Don't forget that
61+
all queries will be stored including duplicates. Making an example
62+
query:
63+
</para>
64+
<programlisting >
65+
select query_hash from sr_plans where query_hash=10;
66+
</programlisting>
67+
<para>
68+
Disable saving the query:
69+
</para>
70+
<programlisting >
71+
set sr_plan.write_mode = false;
72+
</programlisting>
73+
<para>
74+
Now verify that your query is saved:
75+
</para>
76+
<programlisting >
77+
select query_hash, enable, valid, query, explain_jsonb_plan(plan) from sr_plans;
78+
79+
query_hash | enable | valid | query | explain_jsonb_plan
80+
------------+--------+-------+------------------------------------------------------+----------------------------------------------------
81+
1783086253 | f | t | select query_hash from sr_plans where query_hash=10; | Bitmap Heap Scan on sr_plans +
82+
| | | | Recheck Cond: (query_hash = 10) +
83+
| | | | -&gt; Bitmap Index Scan on sr_plans_query_hash_idx+
84+
| | | | Index Cond: (query_hash = 10) +
85+
| | | |
86+
</programlisting>
87+
<para>
88+
Note use of <literal>explain_jsonb_plan</> function, that allows you to
89+
visualize execution plan in the similar way as EXPLAIN command
90+
does.
91+
</para>
92+
<para>
93+
In the database plans are stored as jsonb. By default, all the
94+
newly saved plans are disabled, you need enable it manually:
95+
</para>
96+
<para>
97+
To enable use of the saved plan
98+
</para>
99+
<programlisting >
100+
update sr_plans set enable=true where query_hash=1783086253;
101+
</programlisting>
102+
<para>
103+
(1783086253 for example only) After that, the plan for the query
104+
will be taken from the <literal>sr_plans</> table.
105+
</para>
106+
<para>
107+
In addition sr plan allows you to save a parameterized query plan.
108+
In this case, we have some constants in the query that, as we
109+
know, do not affect plan.
110+
</para>
111+
<para>
112+
During plan saving mode we can mark these constants as query
113+
parameters using a special function <literal>_p (anyelement)</>. For example:
114+
</para>
115+
<programlisting >
116+
117+
=&gt;create table test_table (a numeric, b text);
118+
CREATE TABLE
119+
=&gt;insert into test_table values (1,'1'),(2,'2'),(3,'3');
120+
INSERT 0 3
121+
=&gt; set sr_plan.write_mode = true;
122+
SET
123+
=&gt; select a,b from test_table where a = _p(1);
124+
a | b
125+
---+---
126+
1 | 1
127+
(1 row)
128+
129+
=&gt; set sr_plan.write_mode = false;
130+
SET
131+
</programlisting>
132+
<para>
133+
Now plan for query from our table is saved with parameter. So, if
134+
we enable saved plan in this table, this plan would be used for
135+
query with any value for a, as long as this value is wrapped with
136+
<literal>_p()</> function.
137+
</para>
138+
<programlisting >
139+
=&gt;update sr_plans set enable = true where quesry=
140+
'select a,b from test_table where a = _p(1)';
141+
UPDATE 1
142+
-- These queries would use saved plan
143+
144+
=&gt;select a,b from test_table where a = _p(2);
145+
a | b
146+
---+---
147+
2 | 2
148+
(1 row)
149+
150+
=&gt;select a,b from test_table where a = _p(3);
151+
a | b
152+
---+---
153+
3 | 3
154+
(1 row)
155+
156+
-- This query wouldn't use saved plan, because constant is not wrapped
157+
-- with _p()
158+
159+
=&gt;select a,b from test_table where a = 1;
160+
a | b
161+
---+---
162+
1 | 1
163+
(1 row)
164+
</programlisting>
165+
</sect2>
166+
</sect1>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp