H
H
hbrmdc2015-12-30 23:08:10
PostgreSQL
hbrmdc, 2015-12-30 23:08:10

How to long-term "cache" results in Postgres?

Postgres 9.5
There is a heavy query. But the data in it does not have to be absolutely fresh.
I know about Materialized view, but apparently, this is not what I need.
I'd like to run the query even if it takes a few seconds, and then store the results somewhere where they're available until I decide to update them.
This is almost a Materialized view, but the fact is that there are variables there, and it makes no sense to do MW for with all the variations of these variables. A variable is, say, a combination of id's of two different users. Doing MW for all variations of all existing users is pointless. But saving the results on the first call and showing them before the expiration of some trigger or deadline is what you need. Is there some simple solution without having to write a bunch of trigger functions?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nirvimel, 2015-12-31
@nirvimel

  1. Create a separate table for the cache.
  2. Instead of your heavy request write:
    SELECT ... FROM cache_table;
    if you get an empty result, do:
    1. UPDATE cache_table SET ... FROM SELECT ...(here is your heavy request);
    2. To update the initial data, make a trigger to clear the cache:
      DELETE FROM cache_table WHETE ....

R
romy4, 2015-12-30
@romy4

MW is very suitable, I swear

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question