Skip to content

Commit bdbee93

Browse files
committed
[New feature] Force to make query cache.
Recognize /*FORCE QUERY CACHE*/ SQL statement comment so that any read only SELECT/with queries are cached. This is opposite to /*NO QUERY CACHE*/ comment. This feature should be used carefully. See the manual for more details. Discussion: #56
1 parent 4ac2f88 commit bdbee93

File tree

5 files changed

+129
-21
lines changed

5 files changed

+129
-21
lines changed

doc.ja/src/sgml/memcache.sgml

+75-12
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,93 @@
2222
<note>
2323
<para>
2424
<!--
25-
Basically following SELECTs will not be cached:
25+
First of all if the query starts with SQL comment:
2626
<programlisting>
27-
SELECTs including non immutable functions
28-
SELECTs including temp tables, unlogged tables
29-
SELECT result is too large (memqcache_maxcache)
30-
SELECT FOR SHARE/UPDATE
31-
SELECT starting with "/*NO QUERY CACHE*/" comment
32-
SELECT including system catalogs
33-
SELECT uses TABLESAMPLE
34-
</programlisting>
27+
/*FORCE QUERY CACHE*/
28+
</programlisting>
29+
is checked (case insensitive). If so, the result of the query is
30+
cached unconditionally as long as it is not SELECT or WITH + SELECT.
31+
However you must be very careful to use this feature. For example,
32+
<programlisting>
33+
/*FORCE QUERY CACHE*/SELECT now();
34+
</programlisting>
35+
will return the same timestamp until pgpool restarts, once the
36+
query is cached. The query cache will not be discarded even with a
37+
query:
38+
<programlisting>
39+
/*NO QUERY CACHE*/SELECT now();
40+
</programlisting>
41+
Because it just prevents to create a cache entry for the query, and
42+
does not affect the query using the FORCE QUERY CACHE comment.
43+
</programlisting>
44+
Because it just prevents to create a cache entry for the query, and
45+
does not affect the query using the FORCE QUERY CACHE comment.
46+
-->
47+
まず、クエリが以下のSQLコメントで始まるかどうかがチェックされます(大文字小文字は区別されません)。
48+
<programlisting>
49+
/*FORCE QUERY CACHE*/
50+
</programlisting>
51+
もしこのコメントで始まっているなら、そのクエリがSELECTあるいはWITH + SELECTである限り無条件にキャッシュされます。
52+
しかし、この機能は注意深く使う必要があります。
53+
たとえば、
54+
<programlisting>
55+
/*FORCE QUERY CACHE*/SELECT now();
56+
</programlisting>
57+
このクエリがキャッシュされると、pgpoolが再起動するまで同じタイムスタンプを返し続けます。
58+
このクエリキャッシュは、以下のクエリによってさえも削除されません。
59+
<programlisting>
60+
/*NO QUERY CACHE*/SELECT now();
61+
</programlisting>
62+
なぜなら、これはこのクエリのキャッシュエントリが作成されないようにするだけで、FORCE QUERY CACHEコメントを使ったクエリには影響を与えないからです。
63+
</para>
64+
<para>
65+
<!--
66+
Note that for following query:
67+
<programlisting>
68+
/*FORCE QUERY CACHE*/SELECT * FROM t1;
69+
</programlisting>
70+
usual cache validation due to an update to the table (in this case
71+
t1) works.
72+
-->
73+
次のクエリ
74+
<programlisting>
75+
/*FORCE QUERY CACHE*/SELECT * FROM t1;
76+
</programlisting>
77+
に対しては、テーブル(この場合はt1)の更新によるキャッシュの削除は機能することに注意してください。
78+
</para>
79+
<para>
80+
<!--
81+
If the query does not start with FORCE QUERY CACHE comment,
82+
following checks are performed. If one of followings is satisfied,
83+
SELECT will not be cached.
84+
<programlisting>
85+
SELECT including non immutable functions
86+
SELECT including temp tables, unlogged tables
87+
SELECT including TIMESTAMP WITH TIMEZONE or TIME WITH TIMEZONE
88+
SELECT including CAST to TIMESTAMP WITH TIMEZONE or TIME WITH TIMEZONE
89+
SELECT including SQLValueFunction (CURRENT_TIME, CURRENT_USER etc.)
90+
SELECT result is too large (memqcache_maxcache)
91+
SELECT FOR SHARE/UPDATE
92+
SELECT starting with "/*NO QUERY CACHE*/" comment (case insensitive)
93+
SELECT including system catalogs
94+
SELECT using TABLESAMPLE
95+
SELECT uses row security enabled tables
96+
</programlisting>
3597
However, VIEWs and SELECTs accessing unlogged tables can be
3698
cached by specifying in
3799
the <xref linkend="guc-cache-safe-memqcache-table-list">.
38100
-->
39-
基本的に以下のSELECTはキャッシュされません。
101+
クエリがFORCE QUERY CACHEコメントで始まっていなければ、以下のチェックが行われます。
102+
以下の一つでも該当すれば、SELECTはキャッシュされません。
40103
<programlisting>
41104
immutableでない関数を含むSELECT
105+
一時テーブル、unloggedテーブルを使ったSELECT
42106
TIMESTAMP WITH TIMEZONE、TIME WITH TIMEZONEを返す関数を使っているSELECT
43107
TIMESTAMP WITH TIMEZONE、TIME WITH TIMEZONEへのキャストを含むSELECT
44108
SQLValueFunction (CURRENT_TIME, CURRENT_USERなど)を含むSELECT
45-
一時テーブル、unloggedテーブルを使ったSELECT
46109
検索結果が memqcache_maxcache を越えるようなSELECT
47110
SELECT FOR SHARE/UPDATE
48-
/*NO QUERY CACHE*/コメントで始まるSELECT
111+
/*NO QUERY CACHE*/コメント(大文字小文字は区別されません)で始まるSELECT
49112
システムカタログを使用しているSELECT
50113
TABLESAMPLEを使っているSELECT
51114
行セキュリティが設定されているテーブルを使っているSELECT

doc/src/sgml/memcache.sgml

+38-8
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,48 @@
2222

2323
<note>
2424
<para>
25-
Basically following SELECTs will not be cached:
25+
First of all if the query starts with SQL comment:
2626
<programlisting>
27-
SELECTs including non immutable functions
28-
SELECTs including temp tables, unlogged tables
29-
SELECTs including TIMESTAMP WITH TIMEZONE or TIME WITH TIMEZONE
30-
SELECTs including CAST to TIMESTAMP WITH TIMEZONE or TIME WITH TIMEZONE
31-
SELECTs including SQLValueFunction (CURRENT_TIME, CURRENT_USER etc.)
27+
/*FORCE QUERY CACHE*/
28+
</programlisting>
29+
is checked (case insensitive). If so, the result of the query is
30+
cached unconditionally as long as it is not SELECT or WITH + SELECT.
31+
However you must be very careful to use this feature. For example,
32+
<programlisting>
33+
/*FORCE QUERY CACHE*/SELECT now();
34+
</programlisting>
35+
will return the same timestamp until pgpool restarts, once the
36+
query is cached. The query cache will not be discarded even with a
37+
query:
38+
<programlisting>
39+
/*NO QUERY CACHE*/SELECT now();
40+
</programlisting>
41+
Because it just prevents to create a cache entry for the query, and
42+
does not affect the query using the FORCE QUERY CACHE comment.
43+
</para>
44+
<para>
45+
Note that for following query:
46+
<programlisting>
47+
/*FORCE QUERY CACHE*/SELECT * FROM t1;
48+
</programlisting>
49+
usual cache validation due to an update to the table (in this case
50+
t1) works.
51+
</para>
52+
<para>
53+
If the query does not start with FORCE QUERY CACHE comment,
54+
following checks are performed. If one of followings is satisfied,
55+
SELECT will not be cached.
56+
<programlisting>
57+
SELECT including non immutable functions
58+
SELECT including temp tables, unlogged tables
59+
SELECT including TIMESTAMP WITH TIMEZONE or TIME WITH TIMEZONE
60+
SELECT including CAST to TIMESTAMP WITH TIMEZONE or TIME WITH TIMEZONE
61+
SELECT including SQLValueFunction (CURRENT_TIME, CURRENT_USER etc.)
3262
SELECT result is too large (memqcache_maxcache)
3363
SELECT FOR SHARE/UPDATE
34-
SELECT starting with "/*NO QUERY CACHE*/" comment
64+
SELECT starting with "/*NO QUERY CACHE*/" comment (case insensitive)
3565
SELECT including system catalogs
36-
SELECT uses TABLESAMPLE
66+
SELECT using TABLESAMPLE
3767
SELECT uses row security enabled tables
3868
</programlisting>
3969
However, VIEWs and SELECTs accessing unlogged tables can be

src/include/query_cache/pool_memqcache.h

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#include "pool.h"
3030
#include <sys/time.h>
3131

32+
#define FORCE_QUERY_CACHE "/*FORCE QUERY CACHE*/"
33+
#define FORCE_QUERY_CACHE_COMMENT_SZ (sizeof(FORCE_QUERY_CACHE)-1)
34+
3235
#define NO_QUERY_CACHE "/*NO QUERY CACHE*/"
3336
#define NO_QUERY_CACHE_COMMENT_SZ (sizeof(NO_QUERY_CACHE)-1)
3437

src/query_cache/pool_memqcache.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,13 @@ pool_is_allow_to_cache(Node *node, char *query)
925925
SelectContext ctx;
926926

927927
/*
928-
* If NO QUERY CACHE comment exists, do not cache.
928+
* If FORCE QUERY CACHE comment exists, cache it unconditionally.
929+
*/
930+
if (!strncasecmp(query, FORCE_QUERY_CACHE, FORCE_QUERY_CACHE_COMMENT_SZ))
931+
return true;
932+
933+
/*
934+
* If NO QUERY CACHE comment exists, do not cache unconditionally.
929935
*/
930936
if (!strncasecmp(query, NO_QUERY_CACHE, NO_QUERY_CACHE_COMMENT_SZ))
931937
return false;

src/test/regression/tests/006.memqcache/test.sh

+6
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ SELECT '2022-07-05 10:00:00'::TIMETZ;
9191
SELECT '2022-07-05 10:00:00'::TIMETZ;
9292
SELECT to_timestamp(0);
9393
SELECT to_timestamp(0);
94+
/*FORCE QUERY CACHE*/SELECT now();
95+
/*FORCE QUERY CACHE*/SELECT now();
96+
/*NO QUERY CACHE*/SELECT 1;
97+
/*NO QUERY CACHE*/SELECT 1;
9498
EOF
9599

96100
success=true
@@ -110,6 +114,8 @@ EOF
110114
grep "fetched from cache" log/pgpool.log | grep 'TIMESTAMPTZ;' > /dev/null && success=false
111115
grep "fetched from cache" log/pgpool.log | grep 'TIMETZ;' > /dev/null && success=false
112116
grep "fetched from cache" log/pgpool.log | grep 'to_timestamp' > /dev/null && success=false
117+
grep "fetched from cache" log/pgpool.log | grep 'FORCE QUERY CACHE' > /dev/null || success=false
118+
grep "fetched from cache" log/pgpool.log | grep 'NO QUERY CACHE' > /dev/null && success=false
113119

114120
if [ $success = false ];then
115121
./shutdownall

0 commit comments

Comments
 (0)