Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b8b4b76

Browse files
committed
Impelment autoprepare for extended protocol
1 parent fd6ec93 commit b8b4b76

File tree

14 files changed

+420
-16
lines changed

14 files changed

+420
-16
lines changed

doc/src/sgml/autoprepare.sgml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!-- doc/src/sgml/autoprepare.sgml -->
2+
3+
<chapter id="autoprepare">
4+
<title>Autoprepared statements</title>
5+
6+
<indexterm zone="autoprepare">
7+
<primary>autoprepared statements</primary>
8+
</indexterm>
9+
10+
<para>
11+
<productname>PostgreSQL</productname> makes it possible <firstterm>prepare</firstterm>
12+
frequently used statements to eliminate cost of their compilation
13+
and optimization on each execution of the query. On simple queries
14+
(like ones in <filename>pgbench -S</filename>) using prepared statements
15+
increase performance more than two times.
16+
</para>
17+
18+
<para>
19+
Unfortunately not all database applications are using prepared statements
20+
and, moreover, it is not always possible. For example, in case of using
21+
<productname>pgbouncer</productname> or any other session pooler,
22+
there is no session state (transactions of one client may be executed at different
23+
backends) and so prepared statements can not be used.
24+
</para>
25+
26+
<para>
27+
Autoprepare mode allows to overcome this limitation.
28+
In this mode Postgres stores plans for all queries pass using extended protocol.
29+
Speed of execution of autoprepared statements is almost the same as of explicitly prepared statements.
30+
</para>
31+
32+
<para>
33+
By default autoprepare mode is switched off. To enable it, assign non-zero
34+
value to GUC variable <varname>autoprepare_limit</varname> or <varname>autoprepare_memory_limit</varname>.
35+
This variables specify limit for number of autoprepared statement or memory used by them.
36+
Autoprepare is enabled if one of thme is non zero. Value -1 means unlimited.
37+
Please notice that event autoprepare is anabled, Postgres makes a decision about using
38+
generalized plan vs. customized execution plans based on the results
39+
of comparison of average time of five customized plans with
40+
time of generalized plan.
41+
</para>
42+
43+
<para>
44+
Too large number of autoprepared statements can cause memory overflow
45+
(especially if there are many active clients, because prepared statements cache
46+
is local to the backend). So using unlimited autoprepare hash is debgerous and not recommended.
47+
Postgres is using LRU policy to keep in memory most frequently used queries.
48+
</para>
49+
50+
<para>
51+
Autoprepare hash is local to the backend. It is implicitely reseted on any change of database schema or
52+
session variables.
53+
</para>
54+
55+
<para>
56+
It is possible to inspect autoprepared queries in the backend using
57+
<literal>pg_autoprepared_statements</literal> view. It shows original text of the
58+
query, types of the extracted parameters (replacing literals) and
59+
query execution counter.
60+
</para>
61+
62+
</chapter>

doc/src/sgml/config.sgml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5326,6 +5326,39 @@ SELECT * FROM parent WHERE key = 2400;
53265326
</listitem>
53275327
</varlistentry>
53285328

5329+
<varlistentry id="guc-autoprepare-limit" xreflabel="autoprepare_limit">
5330+
<term><varname>autoprepare_limit</varname> (<type>integer/type>)
5331+
<indexterm>
5332+
<primary><varname>autoprepare_limit</varname> configuration parameter</primary>
5333+
</indexterm>
5334+
</term>
5335+
<listitem>
5336+
<para>
5337+
Maximal number of autoprepared queries.
5338+
Zero value disables autoprepare, -1 means unlimited number of autoprepared queries.
5339+
Too large number of prepared queries can cause backend memory overflow and slowdown execution speed
5340+
(because of increased lookup time). Default value is <literal>0</literal>.
5341+
</para>
5342+
</listitem>
5343+
</varlistentry>
5344+
5345+
<varlistentry id="guc-autoprepare-memory-limit" xreflabel="autoprepare_memory_limit">
5346+
<term><varname>autoprepare_memory_limit</varname> (<type>integer/type>)
5347+
<indexterm>
5348+
<primary><varname>autoprepare_memory_limit</varname> configuration parameter</primary>
5349+
</indexterm>
5350+
</term>
5351+
<listitem>
5352+
<para>
5353+
Maximal size of memory used by autoprepared queries.
5354+
Zero value disables autoprepare, -1 means means that there is no memory limit.
5355+
Default value is <literal>0</literal>. Calculating memory used by prepared queries adds some extra overhead,
5356+
so positive value of this parameter may cause some slowdown.
5357+
<varname>autoprepare_limit</varname> is much faster way to limit number of autoprepared statements.
5358+
</para>
5359+
</listitem>
5360+
</varlistentry>
5361+
53295362
</variablelist>
53305363
</sect2>
53315364
</sect1>

doc/src/sgml/filelist.sgml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<!ENTITY json SYSTEM "json.sgml">
2323
<!ENTITY mvcc SYSTEM "mvcc.sgml">
2424
<!ENTITY parallel SYSTEM "parallel.sgml">
25+
<!ENTITY autoprepare SYSTEM "autoprepare.sgml">
2526
<!ENTITY perform SYSTEM "perform.sgml">
2627
<!ENTITY queries SYSTEM "queries.sgml">
2728
<!ENTITY rangetypes SYSTEM "rangetypes.sgml">

doc/src/sgml/postgres.sgml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
&mvcc;
110110
&perform;
111111
&parallel;
112+
&autoprepare;
112113

113114
</part>
114115

src/backend/catalog/system_views.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ CREATE VIEW pg_prepared_xacts AS
332332
CREATE VIEW pg_prepared_statements AS
333333
SELECT * FROM pg_prepared_statement() AS P;
334334

335+
CREATE VIEW pg_autoprepared_statements AS
336+
SELECT * FROM pg_autoprepared_statement() AS P;
337+
335338
CREATE VIEW pg_seclabels AS
336339
SELECT
337340
l.objoid, l.classoid, l.objsubid,

src/backend/commands/prepare.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ static HTAB *prepared_queries = NULL;
4848
static void InitQueryHashTable(void);
4949
static ParamListInfo EvaluateParams(PreparedStatement *pstmt, List *params,
5050
const char *queryString, EState *estate);
51-
static Datum build_regtype_array(Oid *param_types, int num_params);
5251

5352
/*
5453
* Implements the 'PREPARE' utility statement.
@@ -787,7 +786,7 @@ pg_prepared_statement(PG_FUNCTION_ARGS)
787786
* pointing to a one-dimensional Postgres array of regtypes. An empty
788787
* array is returned as a zero-element array, not NULL.
789788
*/
790-
static Datum
789+
Datum
791790
build_regtype_array(Oid *param_types, int num_params)
792791
{
793792
Datum *tmp_ary;

0 commit comments

Comments
 (0)