Caching query result using temporary table

Caching query result using temporary table



I want to cache result of big select query for further use. The idea is to use temporary table (I believe that it persists for session or at least for transaction):


CREATE TEMP TABLE cache AS (SELECT * FROM t)



but when cache is already created I encounter following error:


Invalid operation: relation "cache" already exists;



There are no IF NOT EXISTS condition for CREATE TEMP TABLE ... AS statement. So question is, is there some workaround for this case?





What kind of workaround are you looking for? The table exists, you cannot create another table with the same name. Just don't create it multiple times within a session.
– zerkms
Aug 27 at 3:45






@zerkms I'm looking for alternatives of IF NOT EXISTS to avoid multiple creation
– Panlantic82
Aug 27 at 3:48





@a_horse_with_no_name yeap, I use Amazon Redshift, which is based on Postgres 8.0.2
– Panlantic82
Aug 28 at 0:09





Then you shouldn't have tagged it with Postgres.
– a_horse_with_no_name
Aug 28 at 5:36





@Panlantic82 what is your expected result. What do you want to store in cache table - result for all "big queries" in session, result of last "big query" or something else?
– Grzegorz Grabek
Aug 28 at 10:37




1 Answer
1



If you want to store just results for last "big query" in session then


Drop table if exists cache;
Create temp table cache as
select * from t where "your's conditions";



Dropping the cache table before will not block your creating table statement.



If you want to store results for all of your "big queries" then you will need some extra info in the naming of your cache.



You could check if temp table already exists in if statment


IF (select count(*)
from information_schema.tables
where table_name'cache'
and table_schema like 'pg_temp%')=0 THEN

create temp table cache as
select * from t where "your's conditions";
ELSE
select * from cache where "your's conditions";
END;





Cześć Grzegorz. Your answer has been flagged as low quality. So, consider adding some text explaining your answer to avoid deletion.
– Jim Jones
Aug 27 at 14:28





What is your purpose to store result for just one query , and this is not possible to change during session or do you want to store results for all of your "big queries" in that session or do you want to store results just for last big query? I assumed that you want to store results for last big query then why there is a drop.
– Grzegorz Grabek
Aug 28 at 10:33





@Panlantic82 ok. Pls describe (best in your question) how do you see that functionality? Because we don't know if queries results are always the same structure or different structure. How do you want to call them back (what will be unique ID of results)? And most important why do you want to store cache results on DB side, not on the client side?
– Grzegorz Grabek
Aug 29 at 8:11





@Panlantic82 - if this is same large select then materialized view should be a perfect solution for your problem. Just remember to refresh it on a daily (or often) routine postgresql.org/docs/current/static/…
– Grzegorz Grabek
Aug 29 at 20:31





@Panlantic82 I edited the answer. Hope it will be useful for you now. If not give a sign.
– Grzegorz Grabek
Aug 31 at 7:38






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế