Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toggles for caching exceptions in in-memory query cache #56

Open
lc-guy opened this issue Jun 27, 2024 · 6 comments
Open

Toggles for caching exceptions in in-memory query cache #56

lc-guy opened this issue Jun 27, 2024 · 6 comments
Assignees

Comments

@lc-guy
Copy link

lc-guy commented Jun 27, 2024

The documentation says:

image

I'm facing a use-case with Grafana, where I wish to cache some heavy queries being made on some dashboards. These queries frequently feature timestamps with timezones or somesuch, functions running on timestamps, etc. I'm using pgpool in raw mode for that, as the caching functionality is all I wish to use.

However, despite moving these queries around, there always seems to be some element somewhere or other that triggers the immutability filter in pgpool, and renders the queries I want uncacheable.

Would it be possible to add a configuration toggle for each of the big "categories" of caching exceptions, such that one can bypass this initial filtering? I understand the footguns involved (in particular with non-immutable functions, timestamps with timezones potentially giving different results, ...), but in my specific use case, these do not really matter as there is only one well-known client requesting this information.

Thank you in advance.

@tatsuo-ishii tatsuo-ishii self-assigned this Jul 1, 2024
@tatsuo-ishii
Copy link
Collaborator

[previous example was not appropriate and removed.]
You can create a view and register it to cache_safe_memqcache_table_list.
cache_safe_memqcache_table_list = 'v1'

test=# create table t1(id int, tm text);
CREATE TABLE
test=# insert into t1 values(1, '2024-07-01 20:36:00+0900');
INSERT 0 1
test=# select id,tm::timestamptz from t1 where id = 1;    -- this result is not cached because of type cast
 id |           tm           
----+------------------------
  1 | 2024-07-01 20:36:00+09
(1 row)
test=#  create view v1 as select id,tm::timestamptz from t1;    -- create wrapping view
CREATE VIEW
test=# select * from v1 where id = 1;     -- this result is cached

A downside of this method is, even if t1 gets updated, query result of v1 does not automatically get updated. You need to execute a "dummy update" (e,g, update v1 set id = 1 where id = 1) or drop/re-create v1.

@tatsuo-ishii
Copy link
Collaborator

You need to execute a "dummy update" (e,g, update v1 set id = 1 where id = 1) or drop/re-create v1.

Actually more safer dummy update would be:
update v1 set id = 1 where false;

BTW, this method (using view) cannot be used if the SELECT in question uses joins because PostgreSQL's updatable view does not allow to use joins.

@tatsuo-ishii
Copy link
Collaborator

Would it be possible to add a configuration toggle for each of the big "categories" of caching exceptions, such that one can bypass this initial filtering?

I think defining the reasonable "category" for everyone is difficult as each user has their own use case.
What about more simple solution?: allow to add a comment to queries like "/* QUERY CACHE*/" which force to create a cache entry for the query.

@lc-guy
Copy link
Author

lc-guy commented Jul 2, 2024

You need to execute a "dummy update" (e,g, update v1 set id = 1 where id = 1) or drop/re-create v1.

Actually more safer dummy update would be: update v1 set id = 1 where false;

BTW, this method (using view) cannot be used if the SELECT in question uses joins because PostgreSQL's updatable view does not allow to use joins.

This seems to work after testing. However, the tables I use in my queries tend to be generated dynamically; having to generate a view for each of them, and adding the views to the configuration every time, would be non-trivial.

Would it be possible to add a configuration toggle for each of the big "categories" of caching exceptions, such that one can bypass this initial filtering?

I think defining the reasonable "category" for everyone is difficult as each user has their own use case. What about more simple solution?: allow to add a comment to queries like "/* QUERY CACHE*/" which force to create a cache entry for the query.

This would be perfect for me, though, if that could be implemented. Thank you!

@tatsuo-ishii
Copy link
Collaborator

I have posted a patch to implement this in the master branch. I change the comment string to "/FORCE QUERY CACHE/" as it seems cleaner to users.
https://www.pgpool.net/pipermail/pgpool-hackers/2024-July/004480.html

@tatsuo-ishii
Copy link
Collaborator

The patch has been committed.
https://git.postgresql.org/gitweb/?p=pgpool2.git;a=commit;h=bdbee93ceb9f1452f9eab56077c8041a68e53ba3
Will appear in upcoming Pgpool-II 4.6.

pengbo0328 pushed a commit that referenced this issue Sep 28, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants